mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-29 17:55:45 +01:00
Merge pull request #3753 from dotCooCoo/fix/preset-group-selection
ui-patterns: support opt-in group and preset selection
This commit is contained in:
8 files changed
+199
-25
No files matched your search
@@ -642,7 +642,11 @@ pub(super) fn panel_source<'a>(
|
||||
} else {
|
||||
entries().filter_map(|e| e.color().map(|color| (e.name.as_str(), color.to_srgb(cmyk)))).collect()
|
||||
};
|
||||
PresetSource::new(SwatchInputs { groups, drawn, icons, host }, PresetFns::new(source_rows).thumb(paint_swatch))
|
||||
let fns = PresetFns::new(source_rows).thumb(paint_swatch).accepts(|_, drag, _, _| {
|
||||
let groups = matches!(drag.grabbed, SwatchKey::Group(_));
|
||||
drag.keys.iter().all(|key| matches!(key, SwatchKey::Group(_)) == groups)
|
||||
});
|
||||
PresetSource::new(SwatchInputs { groups, drawn, icons, host }, fns)
|
||||
}
|
||||
|
||||
fn source_rows(inputs: &SwatchInputs<'_>, env: &Env) -> Vec<PresetRow<SwatchKey>> {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
//! `SwatchesPanel`: the panel body, its state and what it reports.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::collections::{BTreeMap, BTreeSet};
|
||||
use std::rc::Rc;
|
||||
use std::sync::Arc;
|
||||
|
||||
@@ -20,7 +20,7 @@ use craft_ui::primitives::IconSpec;
|
||||
use craft_ui::runtime::env::{Count, Env};
|
||||
use egui::{AsIdSalt, IdSalt, Response, Ui};
|
||||
|
||||
use super::library::{HostThumb, find_swatch, has, panel_source};
|
||||
use super::library::{HostThumb, find_swatch, group_of, has, panel_source};
|
||||
use super::menu::event_edits;
|
||||
use super::{SWATCH_ZOOM, SwatchAt, SwatchEdit, SwatchKey, SwatchesFileOutput, SwatchesState, SwatchesWords};
|
||||
use crate::palette::{SwatchGroup, SwatchNote, SwatchReport};
|
||||
@@ -102,6 +102,7 @@ pub struct SwatchesPanel<'a> {
|
||||
payloads: Vec<PayloadAdder<'a>>,
|
||||
row_items: Option<RowItemsFn<'a>>,
|
||||
document: Option<IdSalt>,
|
||||
select_groups: bool,
|
||||
}
|
||||
|
||||
/// What the body decided; `show` stores the state and reports the rest after the real run.
|
||||
@@ -135,6 +136,7 @@ impl<'a> SwatchesPanel<'a> {
|
||||
payloads: Vec::new(),
|
||||
row_items: None,
|
||||
document: None,
|
||||
select_groups: false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -246,6 +248,12 @@ impl<'a> SwatchesPanel<'a> {
|
||||
self
|
||||
}
|
||||
|
||||
/// Lets panel interactions select group rows alongside swatches in both views. Off by default.
|
||||
pub fn select_groups(mut self, enabled: bool) -> Self {
|
||||
self.select_groups = enabled;
|
||||
self
|
||||
}
|
||||
|
||||
/// Draws the panel and returns the user's actions this pass.
|
||||
pub fn show(self, ui: &mut Ui) -> SwatchesPanelOutput {
|
||||
let SwatchesPanel {
|
||||
@@ -267,6 +275,7 @@ impl<'a> SwatchesPanel<'a> {
|
||||
payloads,
|
||||
row_items,
|
||||
document,
|
||||
select_groups,
|
||||
} = self;
|
||||
let cmyk = cmyk.unwrap_or_else(|| CmykConverter::coated());
|
||||
let words = Env::of(ui).words::<SwatchesWords>();
|
||||
@@ -301,6 +310,7 @@ impl<'a> SwatchesPanel<'a> {
|
||||
.tools_at_start(tools_at_start)
|
||||
.search(search)
|
||||
.select(select)
|
||||
.select_groups(select_groups)
|
||||
.reorder(PresetReorder::items_and_groups())
|
||||
.selection(Bind::bound(&mut st.selection));
|
||||
if let Some(id) = document_id {
|
||||
@@ -332,7 +342,21 @@ impl<'a> SwatchesPanel<'a> {
|
||||
let mut used = None;
|
||||
let mut edits = Vec::new();
|
||||
let mut events = Vec::new();
|
||||
let deleting_groups: BTreeSet<_> = out
|
||||
.events
|
||||
.iter()
|
||||
.filter_map(|event| match event {
|
||||
PresetEvent::Delete(key @ SwatchKey::Group(_)) => group_of(groups, key),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
for event in out.events {
|
||||
if !deleting_groups.is_empty()
|
||||
&& let PresetEvent::Delete(key @ SwatchKey::Swatch(_)) = &event
|
||||
&& group_of(groups, key).is_some_and(|group| deleting_groups.contains(&group))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if let PresetEvent::Select { key: SwatchKey::Swatch(name), mods, .. } = &event
|
||||
&& let Some(at) = find_swatch(groups, name)
|
||||
&& let Some(entry) = groups.get(at.group).and_then(|g| g.entries.get(at.index))
|
||||
|
||||
@@ -657,6 +657,54 @@ fn swatches_panel_reports_each_edit_and_leaves_the_library() {
|
||||
assert_eq!(state.rename, Some(SwatchKey::Swatch("Color Swatch 2".to_owned())), "the new swatch opens for renaming");
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn a_mixed_swatches_selection_rejects_reorder_and_deletes_each_entry_once() {
|
||||
use craft_ui::contract::SelectMode;
|
||||
|
||||
let entry = |name: &str, color| craft_color_ui::palette::SwatchEntry::new(name.to_owned(), SwatchColor::Rgb(color));
|
||||
let groups = vec![
|
||||
SwatchGroup { name: "Basics".to_owned(), entries: vec![entry("Red", [1.0, 0.0, 0.0])] },
|
||||
SwatchGroup { name: "Other".to_owned(), entries: vec![entry("Blue", [0.0, 0.0, 1.0])] },
|
||||
];
|
||||
let state = Rc::new(RefCell::new(SwatchesState::default()));
|
||||
let edits = Rc::new(RefCell::new(Vec::new()));
|
||||
let reorders = Rc::new(Cell::new(0));
|
||||
let (seen, moves, held) = (edits.clone(), reorders.clone(), state.clone());
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let out = SwatchesPanel::new(&groups, Bind::bound(&mut *held.borrow_mut()), RED).select(SelectMode::Multi).select_groups(true).show(ui);
|
||||
seen.borrow_mut().extend(out.edits);
|
||||
for event in out.events {
|
||||
if matches!(event, craft_ui::patterns::presets::PresetEvent::Reorder(_)) {
|
||||
moves.set(moves.get() + 1);
|
||||
}
|
||||
}
|
||||
});
|
||||
fixture.room(Vec2::new(320.0, 400.0)).settle();
|
||||
let group = |name: &str| {
|
||||
let tree = fixture.tree();
|
||||
tree.with_role(Role::LIST_BOX_OPTION)
|
||||
.into_iter()
|
||||
.find(|&key| tree.name_of(key).is_some_and(|label| label == name || label.starts_with(&format!("{name},"))))
|
||||
.and_then(|key| fixture.target(key))
|
||||
.unwrap_or_else(|| panic!("the {name} group"))
|
||||
};
|
||||
let basics = group("Basics");
|
||||
let red = fixture.by_label("Red, #ff0000").expect("the Red swatch");
|
||||
let blue = fixture.by_label("Blue, #0000ff").expect("the Blue swatch");
|
||||
let other = group("Other");
|
||||
fixture.click(&basics).hold(Modifiers::COMMAND).click(&red).click(&blue).hold(Modifiers::NONE).settle();
|
||||
let keys = [SwatchKey::Group("Basics".to_owned()), SwatchKey::Swatch("Red".to_owned()), SwatchKey::Swatch("Blue".to_owned())];
|
||||
assert_eq!(state.borrow().selection.items, keys, "the group and swatch keys are selected together");
|
||||
fixture.drag(&red, other.center() - red.center()).settle();
|
||||
assert!(edits.borrow().is_empty());
|
||||
assert_eq!(reorders.get(), 0, "the swatch source refuses a mixed reorder");
|
||||
fixture.key(Key::Delete, Modifiers::NONE).settle();
|
||||
let reported = edits.borrow();
|
||||
assert_eq!(reported.len(), 2, "the group delete already includes Red: {reported:?}");
|
||||
assert!(reported.contains(&SwatchEdit::DeleteGroup { group: "Basics".to_owned() }));
|
||||
assert!(reported.contains(&SwatchEdit::delete("Blue".to_owned())));
|
||||
}
|
||||
|
||||
/// A group with an empty name holds the swatches outside every group: they show at the top with no header, so no
|
||||
/// heading or disclosure button of the panel is left without a name, and a named group keeps its header.
|
||||
#[trial]
|
||||
|
||||
@@ -79,7 +79,8 @@ impl<C: Collection> ItemsHooks<'_, '_, C> {
|
||||
|
||||
/// AccessKit requests on an item row's node or on a section row's collection node (`report_section_node`): Focus
|
||||
/// moves the lead to the row and key focus to the collection; Click also picks an item and activates it when the
|
||||
/// collection activates on click, or activates a section; Expand and Collapse open and close rows with children.
|
||||
/// collection activates on click, or activates a section it does not select. Expand and Collapse open and
|
||||
/// close rows with children.
|
||||
pub(super) fn node_by_access(
|
||||
&mut self,
|
||||
comp: &C,
|
||||
@@ -92,7 +93,11 @@ impl<C: Collection> ItemsHooks<'_, '_, C> {
|
||||
let (_, built) = self.built(layout, place.rect, place.offset);
|
||||
let rows: Vec<(u64, usize)> = built
|
||||
.into_iter()
|
||||
.filter(|&at| !self.source.section_stop(at) || cx.part_if_declared(section_part(&self.source.key(at))).is_none())
|
||||
.filter(|&at| {
|
||||
!self.source.section_stop(at)
|
||||
|| self.source.selectable_section(at)
|
||||
|| cx.part_if_declared(section_part(&self.source.key(at))).is_none()
|
||||
})
|
||||
.map(|at| (key_hash(&self.source.key(at)), at))
|
||||
.collect();
|
||||
let wanted = |request: &craft_behavior::AccessRequest| {
|
||||
@@ -116,7 +121,7 @@ impl<C: Collection> ItemsHooks<'_, '_, C> {
|
||||
}
|
||||
focus::request_now(&cx.core.ctx, focus::FocusTarget::Id(cx.core.id));
|
||||
if request.action == AccessAction::Click {
|
||||
if self.source.section_stop(at) {
|
||||
if self.source.section_stop(at) && !self.source.selectable_section(at) {
|
||||
self.events.push(CollectionEvent::Activate(key));
|
||||
} else {
|
||||
self.click_pick(comp, at, false, false, craft_keymap::Mods::NONE);
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
use super::drag::SlowClick;
|
||||
use super::lead::PickBy;
|
||||
use super::{ItemsHooks, Placement, RowPress, SECTION};
|
||||
use super::{ItemsHooks, Placement, RowPress, SECTION, section_part};
|
||||
use crate::contract::collection::{ActivateOn, CollectionEvent, CollectionProps, ContextMenuRequest, LeadSource, OpenChange, RowPressed, SelectMode};
|
||||
use crate::contract::{Collection, Cx, ItemOpener, ItemOutput};
|
||||
use crate::runtime::egui::response;
|
||||
@@ -196,6 +196,19 @@ impl<C: Collection> ItemsHooks<'_, '_, C> {
|
||||
focus::request_now(&ctx, focus::FocusTarget::Id(cx.core.id));
|
||||
}
|
||||
}
|
||||
// A selectable section owns its pointer surface. Pick on its click, without taking clicks on disclosure.
|
||||
if let (Some(PressPhase::Release { start, click: true, .. }), Some(target)) = (outcome.phase, outcome.target)
|
||||
&& start.button == PointerButton::Primary
|
||||
&& target.owner == cx.core.id
|
||||
&& target.part.is_some_and(|part| part.key == SECTION)
|
||||
&& let Some((key, _)) = &self.drags.part_press
|
||||
&& target.part == Some(section_part(key))
|
||||
&& let Some(at) = self.index_of(key)
|
||||
&& self.source.selectable_section(at)
|
||||
{
|
||||
let mods = crate::runtime::pointer::frame(&ctx).pick_mods;
|
||||
self.click_pick(comp, at, mods.shift, mods.cmd, mods);
|
||||
}
|
||||
// While a part of an item holds the press, and in the pass it ends, the row reads it with whether it held the lead
|
||||
// when the press began.
|
||||
match outcome.phase {
|
||||
|
||||
@@ -98,8 +98,7 @@ impl<K> BrowserKey<K> {
|
||||
}
|
||||
|
||||
/// The app's change of a selection change the browser's collection reports: only the rows `selectable` accepts stay in
|
||||
/// it, so action rows and groups are never part of the host's selection. Cell changes do not occur under the modes the
|
||||
/// browser allows.
|
||||
/// it. Action rows are never part of the host's selection. Cell changes do not occur under the modes the browser allows.
|
||||
fn app_change<K>(change: SelectionChange<BrowserKey<K>>, selectable: impl Fn(&K) -> bool) -> Option<SelectionChange<K>> {
|
||||
match change {
|
||||
SelectionChange::Set(keys) => {
|
||||
@@ -299,6 +298,9 @@ pub struct PresetBrowser<'a, K: Clone + Eq + Hash + Send + Sync + MemoProps + 's
|
||||
/// How picks select: `Single` by default; `Multi` gives the collection rules in both views.
|
||||
#[prop]
|
||||
select: SelectMode,
|
||||
/// Lets selection interactions include titled groups in both views. Group disclosure remains a separate control.
|
||||
#[prop]
|
||||
select_groups: bool,
|
||||
/// Scrolls this preset into view once.
|
||||
#[prop]
|
||||
reveal: Option<K>,
|
||||
@@ -394,6 +396,7 @@ impl<'a, K: Clone + Eq + Hash + Send + Sync + MemoProps + 'static> PresetBrowser
|
||||
source,
|
||||
selection: None,
|
||||
select: SelectMode::Single,
|
||||
select_groups: false,
|
||||
reveal: None,
|
||||
cell_shape: CellShape::Square,
|
||||
tiles: PresetTiles::Thumbnails,
|
||||
@@ -556,7 +559,13 @@ struct Plan<K> {
|
||||
group_rows: Vec<usize>,
|
||||
}
|
||||
|
||||
fn grid_plan<K: Clone>(rows: &[PresetRow<K>], shown: &[usize], open: impl Fn(usize) -> bool, count: impl Fn(usize) -> Option<String>) -> Plan<K> {
|
||||
fn grid_plan<K: Clone>(
|
||||
rows: &[PresetRow<K>],
|
||||
shown: &[usize],
|
||||
open: impl Fn(usize) -> bool,
|
||||
count: impl Fn(usize) -> Option<String>,
|
||||
select_groups: bool,
|
||||
) -> Plan<K> {
|
||||
let mut plan = Plan { tiles: Vec::new(), groups: Vec::new(), group_rows: Vec::new() };
|
||||
let mut counting = false;
|
||||
for &at in shown {
|
||||
@@ -567,6 +576,9 @@ fn grid_plan<K: Clone>(rows: &[PresetRow<K>], shown: &[usize], open: impl Fn(usi
|
||||
.open(open)
|
||||
.depth(row.depth)
|
||||
.icon(folder(open));
|
||||
if select_groups {
|
||||
group = group.selectable();
|
||||
}
|
||||
if let Some(detail) = count(at) {
|
||||
group = group.detail(detail);
|
||||
}
|
||||
@@ -723,6 +735,7 @@ impl<K: Clone + Eq + Hash + Send + Sync + MemoProps + 'static> PresetBrowser<'_,
|
||||
source,
|
||||
selection,
|
||||
select,
|
||||
select_groups,
|
||||
reveal,
|
||||
cell_shape,
|
||||
tiles: tile_size,
|
||||
@@ -831,6 +844,7 @@ impl<K: Clone + Eq + Hash + Send + Sync + MemoProps + 'static> PresetBrowser<'_,
|
||||
zoom: local.zoom,
|
||||
selection: held.clone(),
|
||||
select,
|
||||
select_groups,
|
||||
reveal: reveal.clone(),
|
||||
menu_target,
|
||||
cell_shape,
|
||||
@@ -933,6 +947,7 @@ struct ListProps<'p, 'a, K: 'static> {
|
||||
zoom: f64,
|
||||
selection: Selection<K>,
|
||||
select: SelectMode,
|
||||
select_groups: bool,
|
||||
reveal: Option<K>,
|
||||
menu_target: Option<K>,
|
||||
cell_shape: CellShape,
|
||||
@@ -1005,6 +1020,7 @@ fn list_view<K: Clone + Eq + Hash + Send + Sync + MemoProps + 'static>(props: &m
|
||||
let shown = shown_rows(rows, &open_flags, matcher.as_ref());
|
||||
let counted = |at: usize| rows[at].count.map(|n| words.group_count.text(n as u64, &env.format().integer(n as u64)));
|
||||
let ruled_heads = group_heads == GroupHeads::Rule;
|
||||
let select_groups = props.select_groups && !ruled_heads && select != SelectMode::None;
|
||||
let shown_selection = Selection::new(
|
||||
held.items.iter().cloned().map(BrowserKey::Preset).collect(),
|
||||
held.lead.clone().map(BrowserKey::Preset),
|
||||
@@ -1044,7 +1060,7 @@ fn list_view<K: Clone + Eq + Hash + Send + Sync + MemoProps + 'static>(props: &m
|
||||
let response = Notice::new(Tone::Neutral, message).layout(NoticeLayout::Centred).show(ui).response;
|
||||
(response, Rc::new(RowMap { rows, displayed: Vec::new() }), Pass { seen: Vec::new(), grid: Vec::new() })
|
||||
} else if view == PresetView::Grid {
|
||||
let plan = grid_plan(rows, &shown, |at| open_flags[at], counted);
|
||||
let plan = grid_plan(rows, &shown, |at| open_flags[at], counted, select_groups);
|
||||
let tiles = Shown { rows, at: plan.tiles.clone(), open: vec![false; plan.tiles.len()], tree: false, actions: 0 };
|
||||
// The grid's positions count its group header rows as the list's count its group rows: the shown rows.
|
||||
let map = Rc::new(RowMap { rows, displayed: shown.clone() });
|
||||
@@ -1226,7 +1242,7 @@ fn list_view<K: Clone + Eq + Hash + Send + Sync + MemoProps + 'static>(props: &m
|
||||
toggle(&mut open_flags, &mut events, at, Some(open));
|
||||
}
|
||||
}
|
||||
GridEvent::GroupPicked { group, .. } => {
|
||||
GridEvent::GroupPicked { group, .. } if !select_groups => {
|
||||
if let Some(&at) = grid_group_row(&shown, rows, group) {
|
||||
toggle(&mut open_flags, &mut events, at, None);
|
||||
}
|
||||
@@ -1259,7 +1275,7 @@ fn list_view<K: Clone + Eq + Hash + Send + Sync + MemoProps + 'static>(props: &m
|
||||
for event in pass.seen {
|
||||
match event {
|
||||
Seen::Selection(change) => {
|
||||
let selectable = |key: &K| row_of(key).is_some_and(|at| !rows[at].is_group());
|
||||
let selectable = |key: &K| row_of(key).is_some_and(|at| select_groups || !rows[at].is_group());
|
||||
if let Some(change) = app_change(change, selectable) {
|
||||
held.apply(change.clone());
|
||||
events.push(PresetEvent::Selection(change));
|
||||
|
||||
@@ -4,6 +4,7 @@ use craft_behavior::{Selection, SelectionChange, SelectionModel};
|
||||
use craft_ui::contract::{Bind, DropAt, DropPlace, DropZone, ListDrag, SelectMode};
|
||||
use craft_ui::patterns::presets::{PresetBrowser, PresetDrop, PresetEvent, PresetFns, PresetRow, PresetSource, PresetState, PresetView};
|
||||
use craft_ui::runtime::drag;
|
||||
use craft_ui::testing::focus::AccessActionKind;
|
||||
use craft_ui::testing::{Fixture, trial};
|
||||
use egui::{Event, Id, Key, Modifiers, PointerButton, Pos2, Rect, Sense, UiBuilder, pos2, vec2};
|
||||
use std::cell::RefCell;
|
||||
@@ -246,6 +247,56 @@ fn multi_selection_ranges_toggles_and_reports_select_for_each_plain_pick() {
|
||||
}
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn titled_groups_join_modifier_selection_only_when_enabled() {
|
||||
for view in [PresetView::List, PresetView::Grid] {
|
||||
for enabled in [false, true] {
|
||||
let rows = PresetSource::new(
|
||||
(),
|
||||
PresetFns::new(|_, _| vec![PresetRow::group(GROUP, "Group", 0), PresetRow::item(ALPHA, "Alpha", 1), PresetRow::item(2, "Beta", 1)]),
|
||||
);
|
||||
let mut state = PresetState::new();
|
||||
let selection = Rc::new(RefCell::new(Selection::default()));
|
||||
let events = Rc::new(RefCell::new(Vec::new()));
|
||||
let (held, seen) = (selection.clone(), events.clone());
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let out = PresetBrowser::new("Patterns", Bind::bound(&mut state), &rows)
|
||||
.view_lock(view)
|
||||
.select(SelectMode::Multi)
|
||||
.select_groups(enabled)
|
||||
.selection(Bind::bound(&mut *held.borrow_mut()))
|
||||
.show(ui);
|
||||
seen.borrow_mut().extend(out.events);
|
||||
});
|
||||
fixture.room(vec2(480.0, 320.0)).settle();
|
||||
let alpha = fixture.by_label("Alpha").expect("the first preset is shown");
|
||||
fixture.click(&alpha).settle();
|
||||
assert_eq!(selection.borrow().items, [ALPHA]);
|
||||
events.borrow_mut().clear();
|
||||
let group = fixture.by_label("Group").expect("the group header is shown");
|
||||
fixture.hold(Modifiers::COMMAND).click(&group).hold(Modifiers::NONE).settle();
|
||||
let expected = if enabled { vec![ALPHA, GROUP] } else { vec![ALPHA] };
|
||||
assert_eq!(selection.borrow().items, expected, "{view:?}, group selection enabled: {enabled}");
|
||||
assert!(!events.borrow().iter().any(|event| matches!(event, PresetEvent::Select { .. })));
|
||||
if enabled {
|
||||
assert_eq!(fixture.tree().selected_of(group.node), Some(true));
|
||||
assert!(fixture.by_label("Beta").is_some(), "the group remains open in {view:?}");
|
||||
fixture.advance(1.0);
|
||||
fixture.hold(Modifiers::COMMAND).click(&group).hold(Modifiers::NONE).settle();
|
||||
assert_eq!(selection.borrow().items, [ALPHA], "Cmd/Ctrl toggles the group off in {view:?}");
|
||||
fixture.access(AccessActionKind::Click, &group).settle();
|
||||
assert_eq!(selection.borrow().items, [GROUP], "accessibility Click selects the group in {view:?}");
|
||||
let beta = fixture.by_label("Beta").expect("accessibility Click keeps the group open");
|
||||
fixture.hold(Modifiers::SHIFT).click(&beta).hold(Modifiers::NONE).settle();
|
||||
assert_eq!(selection.borrow().items, [GROUP, ALPHA, 2], "Shift range includes the group in {view:?}");
|
||||
fixture.access(AccessActionKind::Click, &group).settle();
|
||||
fixture.key(Key::Enter, Modifiers::NONE).settle();
|
||||
assert!(fixture.by_label("Beta").is_none(), "Enter closes the selected group in {view:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn a_payload_dropped_onto_a_preset_reports_dropped_and_wins_over_an_enclosing_drop_action() {
|
||||
// (view, row grabbed, row released over, the preset reported)
|
||||
|
||||
@@ -24,7 +24,7 @@ use crate::primitives::{IconSpec, PaintShape, RectSide, StrokeWidth};
|
||||
use crate::runtime::access::Name;
|
||||
use crate::runtime::commands::FireSource;
|
||||
use crate::runtime::env::Label;
|
||||
use crate::style::{ComponentStyle, FocusLook, RuleKind, StyleColor, StyleCx};
|
||||
use crate::style::{ComponentStyle, Flags, FocusLook, RuleKind, StyleColor, StyleCx};
|
||||
use craft_behavior::{
|
||||
ColumnKey, ColumnWidth, EditEvent, HitLevel, HitPriority, Intent, ItemLayout as _, ItemSource, Metrics, Mixed, NavShape, PartKey, PointerButton,
|
||||
PressPhase, Roving, Selection, Sense,
|
||||
@@ -101,7 +101,7 @@ pub enum GridEvent<K> {
|
||||
/// The modifiers held.
|
||||
mods: Mods,
|
||||
},
|
||||
/// The disclosure of a group header, or AccessKit Expand or Collapse on it, asks for a state.
|
||||
/// The disclosure, Enter on a selectable group, or AccessKit Expand or Collapse asks for a state.
|
||||
GroupOpen {
|
||||
/// The group's index.
|
||||
group: usize,
|
||||
@@ -808,13 +808,13 @@ impl<K: Clone + Eq + Hash + Send + Sync + 'static> Component for TileGrid<'_, K>
|
||||
}
|
||||
}
|
||||
if self.collapses() {
|
||||
while let Some(request) = cx.intents().take_access() {
|
||||
// The collection handles selection of a selectable section. Take only disclosure requests here.
|
||||
while let Some(request) = cx
|
||||
.intents()
|
||||
.take_access_where(|request| matches!(request.action, craft_behavior::AccessAction::Expand | craft_behavior::AccessAction::Collapse))
|
||||
{
|
||||
let Some(target) = request.target else { continue };
|
||||
let wanted = match request.action {
|
||||
craft_behavior::AccessAction::Expand => true,
|
||||
craft_behavior::AccessAction::Collapse => false,
|
||||
_ => continue,
|
||||
};
|
||||
let wanted = request.action == craft_behavior::AccessAction::Expand;
|
||||
let owner = self
|
||||
.placed
|
||||
.iter()
|
||||
@@ -840,8 +840,15 @@ impl<K: Clone + Eq + Hash + Send + Sync + 'static> Component for TileGrid<'_, K>
|
||||
match slot {
|
||||
Slot::Header(group) => {
|
||||
let Some(group) = self.groups.get(group) else { continue };
|
||||
let node = access.node(section_part(&group.key), Role::HEADING);
|
||||
node.level(GROUP_LEVEL);
|
||||
let selectable = self.group_heads == GroupHeads::Titled && group.selectable;
|
||||
let role = if selectable { Role::LIST_BOX_OPTION } else { Role::HEADING };
|
||||
let node = access.node(section_part(&group.key), role);
|
||||
if selectable {
|
||||
let selected = cx.collection_selection().is_some_and(|held| held.contains(&group.key));
|
||||
node.flags.set(Flags::SELECTED, selected);
|
||||
} else {
|
||||
node.level(GROUP_LEVEL);
|
||||
}
|
||||
node.name = Name::Text(if group.detail.is_empty() { group.title.clone() } else { format!("{}, {}", group.title, group.detail) });
|
||||
if self.collapses() {
|
||||
node.expanded = Some(group.open);
|
||||
@@ -873,8 +880,10 @@ impl<K: Clone + Eq + Hash + Send + Sync + 'static> Component for TileGrid<'_, K>
|
||||
draw.fill(&PaintShape::Rect(rect, CornerRadius::ZERO), style.pinned);
|
||||
draw.edge_rule(RuleKind::Divider, rect, RectSide::Bottom);
|
||||
}
|
||||
let lead = RowState { emphasized: lead == Some(Slot::Header(placed.group)), ..RowState::default() };
|
||||
ui.face(section_part(&group.key), self.header_row(group, lead));
|
||||
let selected = group.selectable && cx.collection_selection().is_some_and(|held| held.contains(&group.key));
|
||||
let state = RowState { emphasized: lead == Some(Slot::Header(placed.group)), ..RowState::default() };
|
||||
let header = self.header_row(group, state);
|
||||
ui.face(section_part(&group.key), if selected { header.selected(true) } else { header });
|
||||
if placed.toggle {
|
||||
let disclosure = Disclosure::new(Bind::given(group.open)).node(false).label(group.title.clone());
|
||||
ui.face(TOGGLE.at(placed.group as u64), disclosure);
|
||||
@@ -926,6 +935,7 @@ impl<K: Clone + Eq + Hash + Send + Sync + 'static> Component for TileGrid<'_, K>
|
||||
if matches!(self.zoom, GridZoom::Bound(..)) {
|
||||
response.events.retain(|event| !matches!(event, CollectionEvent::Zoom { .. }));
|
||||
}
|
||||
let collapses = self.collapses();
|
||||
let mut events = self.events;
|
||||
// Only a pass with events reads the pointer, so a pass without one does not depend on it.
|
||||
let mods = if response.events.is_empty() { Mods::NONE } else { crate::runtime::pointer::frame(&cx.core.ctx).pick_mods };
|
||||
@@ -945,6 +955,9 @@ impl<K: Clone + Eq + Hash + Send + Sync + 'static> Component for TileGrid<'_, K>
|
||||
};
|
||||
if let Some(group) = header {
|
||||
match event {
|
||||
CollectionEvent::Activate(_) if self.groups[group].selectable && collapses => {
|
||||
events.push(GridEvent::GroupOpen { group, open: !self.groups[group].open });
|
||||
}
|
||||
CollectionEvent::Activate(_) => events.push(GridEvent::GroupPicked { group, mods }),
|
||||
CollectionEvent::ContextMenu(request) => events.push(GridEvent::GroupMenu { group, at: request.at }),
|
||||
CollectionEvent::Open(change) => events.push(GridEvent::GroupOpen { group, open: change.open }),
|
||||
|
||||
Reference in new issue
Block a user