Keyboard Shortcuts: group headers show as heading rows with their disclosure triangle

A group header is a section row of the table's collection, which takes its press, keys and AccessKit heading node
but builds no row for it. The table declares a sense-less part over each header in view and one over its disclosure
column, and paints a heading ListRow and a triangle Disclosure as their faces, inside the rows' view; the faces have
no nodes of their own. The header shows the lead while the table has focus.

ListRow::disclosure_by_host and disclosure_at are public for hosts outside the widgets crate, and Cx::collection_lead
gives a collection the lead's position (tile grids read it there too).
This commit is contained in:
aiongg
2026-11-12 15:12:11 +00:00
parent f8f57d8935
commit ab2be34a61
6 changed files with 105 additions and 17 deletions
+91 -9
View File
@@ -1,20 +1,27 @@
//! The command table: command and shortcut columns over the lines `rows` builds, group headers that open and
//! close, and rows whose shortcut chips, add button and restore button are children of the row.
//!
//! A group header is a section row of the collection, which takes its press, keys and AccessKit node; the table paints
//! its look, a heading row with the disclosure triangle, over the row.
use std::borrow::Cow;
use craft_behavior::{
ColumnKey, ColumnWidth, DisplayOrder, Flow, Insets, ItemSource, Metrics, Proposal, Selection, SelectionChange, Sense, SpanLayout,
ColumnKey, ColumnWidth, DisplayOrder, Flow, Insets, ItemSource, Metrics, PartKey, Proposal, Selection, SelectionChange, Sense, SpanLayout,
};
use craft_keymap::chord::Chord;
use craft_keymap::keymap::{Binding, MAX_BINDINGS};
use craft_ui::buttons::IconButton;
use craft_ui::contract::{
Bind, BindMut, Collection, CollectionEvent, CollectionProps, CollectionResponse, ColumnSpec, Common, Component, ContainerProps, Cx, ItemOutput,
MeasureCx, Outcome, PaintUi, Pointer as _, Props as _, Role, SelectMode, ShowCollection as _, Sizing, Styled as _, clicked,
Access, Bind, BindMut, Collection, CollectionEvent, CollectionProps, CollectionResponse, ColumnSpec, Common, Component, ContainerProps, Cx,
Focus as _, ItemOutput, MeasureCx, Meta as _, Outcome, PaintUi, PartsCx, Pointer as _, Props as _, Role, RowState, SelectMode,
ShowCollection as _, Sizing, Styled as _, clicked,
};
use craft_ui::controls::disclosure::Disclosure;
use craft_ui::display::{Elide, Icon, Kbd, Shade, Text};
use craft_ui::list::{ListRow, ListRowStyle};
use craft_ui::primitives::{IconSpec, Span, TextRun};
use craft_ui::runtime::env::Label;
use craft_ui::style::FocusLook;
use craft_ui::style::{ComponentStyle, Ink, Stateful, StyleCx, TextLook, TypeRole};
use egui::{Pos2, Rect, Response, Ui, Vec2};
@@ -30,6 +37,10 @@ const SHORTCUT: ColumnKey = ColumnKey::new("shortcut");
/// buttons, and the command column takes the rest.
const SHORTCUT_MOST: f32 = 0.4;
const PATH_SEPARATOR: &str = " \u{203a} ";
/// The look of a group header, over its section row; the index is the header's line.
const HEADER: PartKey = PartKey::new("group_header");
/// The disclosure triangle in a group header's disclosure column; the index is the header's line.
const HEADER_DISCLOSURE: PartKey = PartKey::new("group_disclosure");
/// What a row reports besides what the collection reads from its pointer input.
#[derive(Clone, Debug, PartialEq)]
@@ -89,12 +100,29 @@ impl DisplayOrder<usize> for Lines<'_> {
impl ItemSource<usize> for Lines<'_> {}
/// The style of the table: no look of its own.
#[derive(Clone, Debug, Default, PartialEq, ComponentStyle)]
/// The style of the table: the look of its group headers.
#[derive(Clone, Debug, PartialEq, ComponentStyle)]
#[non_exhaustive]
pub(crate) struct ShortcutTableStyle {
#[style(metrics)]
metrics: Metrics,
/// A group header: a heading row, which places the disclosure the table declares.
header: ListRowStyle,
}
/// A group header in view this pass.
#[derive(Clone, Copy, Debug)]
struct PlacedHeader {
/// The header's line.
at: usize,
/// The header row has a disclosure column.
disclosure: bool,
}
/// The look of the group header `name`: a heading row with the group's disclosure, which the table declares and
/// paints.
fn header_row(name: &str, open: bool) -> ListRow<'static> {
ListRow::new(name.to_owned()).heading().node(false).group(open).disclosure_by_host()
}
/// A table of commands with their shortcuts.
@@ -113,16 +141,25 @@ pub(crate) struct ShortcutTable<'a> {
/// Scrolls this line into view once.
#[prop]
reveal: Option<usize>,
/// The lines the rows show, for the group headers the table paints.
lines: &'a [Line],
/// The group headers in view this pass.
headers: Vec<PlacedHeader>,
/// Where the rows show this pass: the headers paint inside it, clear of the column header.
rows_view: Option<Rect>,
}
impl<'a> ShortcutTable<'a> {
fn new(label: impl Into<craft_ui::runtime::env::Label>, columns: Vec<ColumnSpec>) -> Self {
fn new(label: impl Into<Label>, columns: Vec<ColumnSpec>, lines: &'a [Line]) -> Self {
ShortcutTable {
common: Common::new(),
props: ContainerProps::new().sizing(Sizing::Fill),
items: CollectionProps::new().select(SelectMode::Single).columns(columns),
selection: None,
reveal: None,
lines,
headers: Vec::new(),
rows_view: None,
}
.label(label)
}
@@ -133,8 +170,53 @@ impl Component for ShortcutTable<'_> {
type Style = ShortcutTableStyle;
type Output = CollectionResponse<usize, RowAct>;
fn style(_: &StyleCx<'_>, _: ShortcutTableStyleKey) -> ShortcutTableStyle {
ShortcutTableStyle::default()
fn style(cx: &StyleCx<'_>, _: ShortcutTableStyleKey) -> ShortcutTableStyle {
ShortcutTableStyle { metrics: Metrics::new(), header: ListRow::style(cx, ListRow::new(Label::default()).heading().style_key()) }
}
/// A sense-less part over each group header in view, and one over its disclosure column: they only carry the
/// header's look, so the press on them is the section row's.
fn parts(&mut self, _: Rect, cx: &mut PartsCx<'_, Self>) {
self.headers.clear();
self.rows_view = cx.rows_view();
let rtl = cx.flow.is_rtl();
for (at, line) in self.lines.iter().enumerate() {
let Line::Header { name, collapsed } = line else { continue };
let Some(rect) = cx.rect_of(&at) else { continue };
let column = header_row(name, !*collapsed).disclosure_at(&cx.style.header, rect, rtl, &|run| cx.layout_at(run, f32::INFINITY).size().x);
cx.part(HEADER.at(at as u64), rect).sense(Sense::empty()).over();
if let Some(column) = column {
cx.part(HEADER_DISCLOSURE.at(at as u64), column).sense(Sense::empty()).over();
}
self.headers.push(PlacedHeader { at, disclosure: column.is_some() });
}
}
/// The headers' faces have no nodes: the collection's heading node for the section row stands for the header.
fn access(&self, access: &mut Access, _: &Cx<'_, Self>) {
for header in &self.headers {
access.node(HEADER.at(header.at as u64), Role::None);
if header.disclosure {
access.node(HEADER_DISCLOSURE.at(header.at as u64), Role::None);
}
}
}
/// Each group header in view: a heading row, emphasized while it holds the lead, with its disclosure triangle.
fn paint_over(&mut self, ui: &mut PaintUi<'_>, cx: &Cx<'_, Self>) {
let lead = if cx.is_focused() { cx.collection_lead() } else { None };
let (lines, headers) = (self.lines, &self.headers);
ui.clipped(self.rows_view.unwrap_or(cx.rect), |ui| {
for header in headers {
let Some(Line::Header { name, collapsed }) = lines.get(header.at) else { continue };
let state = RowState { emphasized: lead == Some(header.at), ..RowState::default() };
ui.face(HEADER.at(header.at as u64), header_row(name, !*collapsed).state(state));
if header.disclosure {
let disclosure = Disclosure::new(Bind::given(!*collapsed)).node(false).triangle(true).label(name.clone());
ui.face(HEADER_DISCLOSURE.at(header.at as u64), disclosure);
}
}
});
}
fn finish(self, mut cx: Cx<'_, Self>) -> Self::Output {
@@ -528,7 +610,7 @@ pub(super) fn show(ui: &mut Ui, now: f64, env: &mut Env<'_>, state: &mut DialogS
let reveal = if state.reveal { position } else { None };
let source = Lines(&built.lines);
let stamp = stamp_of(&state.area, &state.query, &format!("{:?}", state.filter));
let mut table = ShortcutTable::new(words.commands.clone(), columns)
let mut table = ShortcutTable::new(words.commands.clone(), columns, &built.lines)
.id_salt("table")
.selection(Bind::bound(&mut selection))
.empty(words.no_matching_commands.clone())
+4 -3
View File
@@ -63,9 +63,10 @@ const ITEM_NODE: craft_behavior::PartKey = craft_behavior::PartKey::new("item_no
/// The part of a collection that stands for the section row `key` (`ItemSource::section_stop`): the item step builds
/// no row for it, so the access node that AccessKit focus goes to while the section holds the lead, and the pointer
/// part over it, are this part of the collection. It is the collection contract's one section mechanism: a component
/// that draws its own section headers lays them out as this part, so the lead, the pointer and the access tree treat
/// them as the collection's section rows.
/// part over it, are this part of the collection. A component that senses and reports its own section headers
/// declares them as this part, so the lead, the pointer and the access tree treat them as the collection's section
/// rows. One that only paints them paints each as the face of a sense-less part of its own over `PartsCx::rect_of`,
/// and the collection keeps their press, keys and node.
pub fn section_part<K: std::hash::Hash>(key: &K) -> craft_behavior::PartRef {
SECTION.at(key_hash(key))
}
@@ -12,6 +12,12 @@ impl<C: Collection> Cx<'_, C> {
self.core.collection_selection.as_ref().and_then(|held| held.downcast_ref::<C::Selection>())
}
/// The position of the lead as this pass's item step left it, `None` while there is none: a collection that paints
/// its own section rows shows the lead on them.
pub fn collection_lead(&self) -> Option<usize> {
super::lead_position(&self.core.ctx, self.core.id)
}
/// The header menu, from the item step's header: the request while the menu may be open (a secondary click on a
/// header cell or the empty header, the Menu key on a focused header) and whether it was made in this pass. The
/// collection that shows the menu opens its popup on a fresh request and gives the picks back with
+1 -1
View File
@@ -348,7 +348,7 @@ impl<'a> ListRow<'a> {
/// The host declares and paints the disclosure part of this group row at [`ListRow::disclosure_at`]; the row keeps
/// the column. For a row the host paints as a face, which declares no parts.
#[must_use]
pub(crate) fn disclosure_by_host(mut self) -> Self {
pub fn disclosure_by_host(mut self) -> Self {
self.disclosure_by_host = true;
self
}
+1 -1
View File
@@ -104,7 +104,7 @@ impl ListRow<'_> {
/// Where this row's disclosure lies when it fills `rect` under `style`, with texts `text_width` wide: the rect at
/// which a host that owns the disclosure part (`disclosure_by_host`) declares it. `None` for a row without one.
pub(crate) fn disclosure_at(&self, style: &ListRowStyle, rect: Rect, rtl: bool, text_width: &dyn Fn(&TextRun<'_>) -> f32) -> Option<Rect> {
pub fn disclosure_at(&self, style: &ListRowStyle, rect: Rect, rtl: bool, text_width: &dyn Fn(&TextRun<'_>) -> f32) -> Option<Rect> {
let widths = self.widths(&style.sizes, text_width);
self.place(&style.sizes, &widths, rect, rtl).disclosure
}
+2 -3
View File
@@ -15,9 +15,8 @@ use super::{CaptionPlace, Tile, TilePartEvent, ZoomStep};
use crate::contract::{
Access, Bind, BindMut, Collection, CollectionEvent, CollectionProps, CollectionResponse, ColumnKind, ColumnSpec, Common, Component,
ContainerProps, Cx, DropKind, EditResponse, Focus as _, ListDrag, Meta as _, NoValue, Notch, Outcome, PaintUi, PartsCx, Pointer as _, Props as _,
Role, RowState, SelectMode, Sizing, SpaceAction, SweepApply, SweepStep, ToggleColumn,
Role, RowState, SelectMode, Sizing, SpaceAction, SweepApply, SweepStep, ToggleColumn, section_part,
};
use crate::contract::{lead_position, section_part};
use crate::controls::disclosure::Disclosure;
use crate::list::{DropTest, ListRow, ListRowStyle, RowTest, ThumbSize};
use crate::primitives::{IconSpec, PaintShape, RectSide};
@@ -458,7 +457,7 @@ impl<'a, K: Clone + Eq + Hash + Send + Sync + 'static> TileGrid<'a, K> {
if !cx.is_focused() {
return None;
}
let at = lead_position(&cx.core.ctx, cx.core.id)?;
let at = cx.collection_lead()?;
let laid = self.laid.borrow();
let arrangement = laid.as_ref()?.arrangement()?;
Some(slot_at(&arrangement.starts, arrangement.tiles.len(), at))