Toolbar
A row of what a screen or a part of it can do, as a document's: buttons with words, buttons with an icon, buttons that open a menu, and lines between their groups, which keeps what has no room in a menu behind a "…" button at its end.
import com.pantrixui.components.container.toolbar.PantrixToolbar
- Items.
ToolbarItem.Buttonis a button with words, which may have an icon before them and aButtonVariant;ToolbarItem.IconButtonis an icon alone;ToolbarItem.Menuopens a choice's menu;ToolbarItem.Divideris the line between two groups. - What does not fit. The items make room one at a time, the lowest
priorityfirst and of two alike the later one: a button with words and an icon is drawn as its icon, its words becoming the tooltip and the screen reader's name; where it still does not fit it falls into the menu, and only then does the next one give up anything. A button of a higher priority keeps its words while one of a lower priority has room to give. - Menu. In the "…" menu what fell stands in the order given, and under it
menuItems, the rows the menu always holds. AToolbarItem.Menuis one row there; it opens its rows in the menu's place, under a row that goes back. Where nothing fell and the menu has no rows of its own, there is no "…" button either. - Start and end.
startstands at the bar's start,endat its end, the "…" button last. A line is drawn only where something stands on either side of it. - Automation. With
tagKeythe bar isToolbar_<tagKey>, its itemsAction_<key>in the bar and in the menu alike, and the "…" buttonMore_actions. - Style and colour.
ToolbarStylegives the room between items and the line's height,ToolbarColorsthe line's colour;buttonSizeis the buttons' size.
Examples
A document's actions
A document's actions, at the width chosen: in a wide bar all of them with their words; as it narrows, the button of the lowest priority is drawn as its icon first, then falls into the "…" menu, and then the next one. The copy button is of the highest priority and falls last.
var width by rememberSaveable { mutableStateOf(640) }
var pressed by rememberSaveable { mutableStateOf("") }
Column(verticalArrangement = Arrangement.spacedBy(PantrixTheme.spacing.sm)) {
PantrixSegmentButton(
options = listOf(
SegmentOption(value = 640, label = "640 dp", tagKey = "640"),
SegmentOption(value = 360, label = "360 dp", tagKey = "360"),
SegmentOption(value = 200, label = "200 dp", tagKey = "200")
),
selected = width,
onSelect = { width = it },
tagKey = "width"
)
PantrixToolbar(
start = listOf(
ToolbarItem.Button(
key = "format",
label = "Format",
onClick = { pressed = "Format" },
variant = ButtonVariant.Solid,
priority = 3
),
ToolbarItem.Button(
key = "minify",
label = "Minify",
onClick = { pressed = "Minify" },
icon = PantrixIcons.Minus
),
ToolbarItem.Divider(key = "made"),
ToolbarItem.Button(
key = "convert",
label = "Convert",
onClick = { pressed = "Convert" },
icon = PantrixIcons.Code
),
ToolbarItem.Button(
key = "history",
label = "History",
onClick = { pressed = "History" },
icon = PantrixIcons.History,
priority = 1
)
),
end = listOf(
ToolbarItem.IconButton(
key = "copy",
icon = PantrixIcons.Copy,
label = "Copy",
onClick = { pressed = "Copy" },
priority = 5
)
),
modifier = Modifier.widthIn(max = width.dp).fillMaxWidth(),
contentDescription = "Document",
tagKey = "document"
)
PantrixText(
text = if (pressed.isEmpty()) "No action pressed yet." else "Last pressed: $pressed",
color = PantrixTheme.colors.textSecondary,
tagKey = "pressed"
)
}
A choice's menu, and the menu's own rows
A choice's menu: the document's format, the one chosen marked. The menu's own rows, sharing and closing the document, are behind the "…" button at every width; in a narrow bar the format's button falls there too, and opens its rows in the menu's place.
The format's button writes the chosen format alone, "JSON"; a screen reader reads it as "The document's format", by its contentDescription. So does "Validate": "Validate the document", in the bar and in the "…" menu alike.
var format by rememberSaveable { mutableStateOf("json") }
var narrow by rememberSaveable { mutableStateOf(false) }
var said by rememberSaveable { mutableStateOf("") }
Column(verticalArrangement = Arrangement.spacedBy(PantrixTheme.spacing.sm)) {
PantrixSegmentButton(
options = listOf(
SegmentOption(value = false, label = "Wide", tagKey = "wide"),
SegmentOption(value = true, label = "Narrow", tagKey = "narrow")
),
selected = narrow,
onSelect = { narrow = it },
tagKey = "room"
)
PantrixToolbar(
start = listOf(
ToolbarItem.Menu(
key = "format",
label = FORMATS.getValue(format),
items = FORMATS.map { (key, name) ->
ToolbarMenuItem(key = key, label = name, onClick = { format = key }, selected = key == format)
},
priority = 2,
contentDescription = "The document's format"
),
ToolbarItem.Divider(key = "line"),
ToolbarItem.Button(
key = "validate",
label = "Validate",
onClick = { said = "Validated" },
contentDescription = "Validate the document"
)
),
menuItems = listOf(
ToolbarMenuItem(
key = "share",
label = "Share",
onClick = { said = "Shared" },
icon = PantrixIcons.Share
),
ToolbarMenuItem(
key = "close",
label = "Close the document",
onClick = { said = "Closed" },
icon = PantrixIcons.Delete,
destructive = true
)
),
modifier = Modifier.widthIn(max = if (narrow) 120.dp else 480.dp),
tagKey = "editor"
)
PantrixText(
text = "Format: ${FORMATS.getValue(format)}" + if (said.isEmpty()) "" else " · $said",
color = PantrixTheme.colors.textSecondary,
tagKey = "format"
)
}
private val FORMATS = linkedMapOf("json" to "JSON", "yaml" to "YAML", "toml" to "TOML")
The button of the view shown
A document's views: each a button, the one shown selected. The selected button is drawn in Soft's pale fill whatever its variant, and a screen reader is told it is selected, as a tab is; in a narrow bar it is a checked row of the "…" menu. A button that is not selected is read as the others are.
var view by rememberSaveable { mutableStateOf("tree") }
PantrixToolbar(
start = VIEWS.map { (key, name) ->
ToolbarItem.Button(key = key, label = name, onClick = { view = key }, selected = key == view)
},
tagKey = "views"
)
private val VIEWS = linkedMapOf("tree" to "Tree", "table" to "Table", "raw" to "Raw text")
Its own style and colour
In its own style: small buttons, less room between them, and a shorter line in the app's colour.
PantrixToolbar(
start = listOf(
ToolbarItem.IconButton(key = "undo", icon = PantrixIcons.ArrowBack, label = "Undo", onClick = {}),
ToolbarItem.IconButton(key = "redo", icon = PantrixIcons.ArrowForward, label = "Redo", onClick = {}),
ToolbarItem.Divider(key = "line"),
ToolbarItem.Button(key = "settings", label = "Settings", onClick = {}, icon = PantrixIcons.Settings)
),
buttonSize = ButtonSize.Small,
style = ToolbarStyle(gap = 2.dp, dividerHeight = 16.dp),
colors = ToolbarColors(divider = PantrixTheme.colors.primary.color),
tagKey = "own"
)
API
start
A row of what a screen or a part of it can do, as a document's: buttons with words, buttons with an icon, buttons that open a menu, and lines between their groups, which keeps what has no room in a menu behind a "…" button at its end.
| Parameter | Type | Default | What it is |
|---|---|---|---|
start | List<ToolbarItem> | the items at the bar's start, in order. | |
modifier | Modifier | Modifier | the bar's modifier. |
end | List<ToolbarItem> | emptyList() | the items at the bar's end, before the "…" button, in order. |
menuItems | List<ToolbarMenuItem> | emptyList() | the rows the "…" menu holds whatever the room, under the items that had none. |
buttonSize | ButtonSize | ButtonSize.Medium | how large the bar's buttons are: ButtonSize.Medium by default. |
contentDescription | String? | null | what a screen reader calls the bar, or null for a bar it hears only the buttons of. |
style | ToolbarStyle? | null | this call's own style: over every enclosing one and under the density. Only the fields it sets change, on every platform. |
colors | ToolbarColors? | null | this call's own colours, where they are not the theme's. |
tagKey | String? | null | the bar's automation key: it is Toolbar_<tagKey>, and its items, in the bar and in the menu alike, are Action_<key> inside it; its "…" button is More_actions. |
ToolbarStyle
PantrixToolbar's geometry: the room between two of its items and how tall a line between two groups is. A bar used by touch has less room between its buttons, which take the platform's touch target, and taller lines.
| Parameter | Type | Default | What it is |
|---|---|---|---|
gap | Dp | Dp.Unspecified | The room between two items, and between the last item and the "…" button. |
dividerHeight | Dp | Dp.Unspecified | How tall a ToolbarItem.Divider is drawn. |
ToolbarColors
A PantrixToolbar's own colours, where they are not the theme's. The bar has no fill of its own, and its buttons are drawn in their variants' colours.
| Parameter | Type | Default | What it is |
|---|---|---|---|
divider | Color | Color.Unspecified | The line between two groups: the theme's divider. |
Accessibility
- Keyboard. The bar is one stop of Tab: the arrows walk between the buttons, Home and End go to the ends; in a right-to-left layout the arrows go the other way.
- Screen reader. The bar is one group, by the name
contentDescriptiongives it.