mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 02:54:25 +00:00
Pointer commands on rows: a right-click on a thumbnail names it in the menu request, and command_click makes a Cmd/Ctrl click a command on the row (ChannelsPanel loads the channel; LayersPanel opens the mask's menu)
This commit is contained in:
@@ -12,7 +12,7 @@ use craft_ui::contract::{
|
||||
ToggleColumn, request_rename,
|
||||
};
|
||||
use craft_ui::layout_components::Stack;
|
||||
use craft_ui::list::{List, RowEvent, ThumbSize, ThumbTarget};
|
||||
use craft_ui::list::{List, RowEvent, ThumbSize};
|
||||
use craft_ui::primitives::textures::{ThumbSource, Thumbnails};
|
||||
use craft_ui::runtime::commands::{CommandState, Commands as _};
|
||||
use craft_ui::runtime::env::{Env, Label};
|
||||
@@ -83,7 +83,7 @@ impl<K: Clone> ItemSource<K> for Order<'_, K> {
|
||||
/// - **Layout**: the rows fill the panel's height above the footer: a [`List`] of `ListRow`s in the layer form.
|
||||
/// - **Selection**: the list shows the rows the app marks `targeted` as selected (several while the composite is
|
||||
/// targeted), over a selection rebuilt from the rows each pass; its own changes are not kept. A click or Shift-click
|
||||
/// reports [`Target`](ChannelsEvent::Target); Cmd/Ctrl-click on the thumbnail reports
|
||||
/// reports [`Target`](ChannelsEvent::Target); Cmd/Ctrl-click anywhere on a row reports
|
||||
/// [`LoadSelection`](ChannelsEvent::LoadSelection) with the operation Shift and Alt ask for; ↑ ↓ report `Target`
|
||||
/// for the new lead; the Load Selection command (Cmd/Ctrl+Enter) loads the lead.
|
||||
/// - **Eyes**: the list's toggle column; a click or a sweep reports [`Visibility`](ChannelsEvent::Visibility) steps, one per eye crossed;
|
||||
@@ -184,6 +184,7 @@ impl<'a, K: Clone + Eq + Hash + Debug + Send + Sync + 'static> ChannelsPanel<'a,
|
||||
let out = List::new(words.list.clone())
|
||||
.id(list)
|
||||
.reorder(ReorderRule::new())
|
||||
.command_click(true)
|
||||
.allow_empty(false)
|
||||
.rename(true)
|
||||
.rename_column(NAME)
|
||||
@@ -250,7 +251,7 @@ fn map_events<K: Clone + PartialEq>(
|
||||
events.push(ChannelsEvent::Target(key.clone()));
|
||||
}
|
||||
}
|
||||
CollectionEvent::Item(key, RowEvent::ThumbClicked { target: ThumbTarget::First, mods, .. }) if mods.cmd => {
|
||||
CollectionEvent::CommandClick { key, mods, .. } => {
|
||||
events.push(ChannelsEvent::LoadSelection { key, operation: SelectionOperation::from_mods(mods) });
|
||||
}
|
||||
CollectionEvent::Item(key, RowEvent::ThumbClicked { clicks: 2, .. }) if kind_of(&key).is_some_and(ChannelKind::has_options) => {
|
||||
|
||||
@@ -13,7 +13,7 @@ use super::super::context::PanelContext;
|
||||
use super::super::drag::drop_event;
|
||||
use super::super::events::{ActivateOn, LayerColumn, LayerSelect, LayerToggle, LayersEvent, MenuTarget};
|
||||
use super::super::menu::OpenMenu;
|
||||
use super::super::row::{EYE, FX_PART, LOCK_PART};
|
||||
use super::super::row::{EYE, FX_PART, LOCK_PART, NAME};
|
||||
use super::rows::{ToggleSweeps, thumb_click_replaces_select, thumb_part};
|
||||
use crate::ui::tree::{LayerRowKey, LayerTree};
|
||||
|
||||
@@ -89,7 +89,8 @@ pub(super) fn map_events<T: LayerTree>(
|
||||
CollectionEvent::ContextMenu(request) => {
|
||||
let layer = request.key.layer().clone();
|
||||
let eye = request.column.as_ref() == Some(&EYE) && matches!(request.key, LayerRowKey::Layer(_));
|
||||
let plain = request.column.is_none() || matches!(request.key, LayerRowKey::Effects(..) | LayerRowKey::Filters(..));
|
||||
let plain = request.column.as_ref().is_none_or(|column| *column == NAME)
|
||||
|| matches!(request.key, LayerRowKey::Effects(..) | LayerRowKey::Filters(..));
|
||||
let selected = cx.selection.layers.contains(&layer);
|
||||
if eye {
|
||||
let rows = if selected { cx.selection.layers.clone() } else { vec![layer] };
|
||||
@@ -104,7 +105,10 @@ pub(super) fn map_events<T: LayerTree>(
|
||||
session.anchor = Some(LayerRowKey::Layer(layer));
|
||||
}
|
||||
let on_selection = selected && cx.selection.layers.len() > 1;
|
||||
session.menu = Some(OpenMenu::Row(MenuTarget { row: request.key.clone(), part: None, on_selection }));
|
||||
// A right-click on a thumbnail opens the menu for what it shows (a mask).
|
||||
let part =
|
||||
request.part.and_then(ThumbTarget::of_part).map(|which| thumb_part(&request.key, &tree.info(request.key.layer()), which));
|
||||
session.menu = Some(OpenMenu::Row(MenuTarget { row: request.key.clone(), part, on_selection }));
|
||||
}
|
||||
}
|
||||
CollectionEvent::Reorder(edit) => events.extend(drop_event(edit, order, parent_of, filtered)),
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
use super::props::LeadSource;
|
||||
use crate::runtime::drag::OsFile;
|
||||
use craft_behavior::{ColumnKey, DropOperation, EditEvent, SelectionChange};
|
||||
use craft_behavior::{ColumnKey, DropOperation, EditEvent, PartRef, SelectionChange};
|
||||
use egui::{Id, Pos2, Vec2};
|
||||
|
||||
/// A committed rename.
|
||||
@@ -123,6 +123,8 @@ pub struct ContextMenuRequest<K> {
|
||||
/// The column the press landed in (a toggle, a text or the label column); `None` from the keyboard and in a list
|
||||
/// without columns.
|
||||
pub column: Option<ColumnKey>,
|
||||
/// The part of the item under the pointer (a thumbnail); `None` on the item's own area and from the keyboard.
|
||||
pub part: Option<PartRef>,
|
||||
}
|
||||
|
||||
/// A change to the columns.
|
||||
@@ -176,6 +178,15 @@ pub enum CollectionEvent<K, E> {
|
||||
Preview(K),
|
||||
/// The user asked for an item's context menu.
|
||||
ContextMenu(ContextMenuRequest<K>),
|
||||
/// A Cmd/Ctrl click on an item of a `command_click` collection: a command on the item (load it as a selection),
|
||||
/// not a pick. The selection is unchanged.
|
||||
#[non_exhaustive]
|
||||
CommandClick {
|
||||
/// The item.
|
||||
key: K,
|
||||
/// The modifiers held at the click.
|
||||
mods: craft_keymap::Mods,
|
||||
},
|
||||
/// Delete or Backspace at the lead of a `deletable` collection: the selected items when the lead is selected, else
|
||||
/// the lead alone, in display order.
|
||||
Delete(Vec<K>),
|
||||
|
||||
@@ -197,6 +197,9 @@ pub struct CollectionProps {
|
||||
/// The most rows the collection shows: past them it is as tall as its first `max_rows` rows and scrolls them. It
|
||||
/// owns that scroll inside a scrolling body too, since its height is bounded and the wheel passes on at its ends.
|
||||
pub max_rows: Option<u16>,
|
||||
/// A Cmd/Ctrl click on an item, on its own area or a part that picks it, is a command on the item, not a pick: the
|
||||
/// selection stays and the release reports `CollectionEvent::CommandClick`.
|
||||
pub command_click: bool,
|
||||
/// The distance one wheel notch scrolls the rows; unset, the default notch.
|
||||
pub notch: Option<Notch>,
|
||||
/// The axis the rows overflow along. A collection owns the scroll along it, so it sits only in a scrolling body
|
||||
@@ -284,6 +287,7 @@ impl CollectionProps {
|
||||
natural_height: false,
|
||||
min_rows: 0,
|
||||
max_rows: None,
|
||||
command_click: false,
|
||||
notch: None,
|
||||
overflow: Axis::Y,
|
||||
pages: false,
|
||||
@@ -503,6 +507,13 @@ impl CollectionProps {
|
||||
self
|
||||
}
|
||||
|
||||
/// A Cmd/Ctrl click on an item is a command, not a pick (see [`CollectionProps::command_click`]).
|
||||
#[must_use]
|
||||
pub const fn command_click(mut self, on: bool) -> Self {
|
||||
self.command_click = on;
|
||||
self
|
||||
}
|
||||
|
||||
/// The distance one wheel notch scrolls the rows (see [`CollectionProps::notch`]).
|
||||
#[must_use]
|
||||
pub const fn notch(mut self, notch: Notch) -> Self {
|
||||
|
||||
@@ -160,6 +160,9 @@ pub(super) struct Drags<K> {
|
||||
/// The row a primary press of an `ActivateOn::Click` collection landed on: it activates when the press ends as a
|
||||
/// click, not when it becomes a drag.
|
||||
pub(super) activate: Option<K>,
|
||||
/// The row a Cmd/Ctrl press of a `command_click` collection landed on: the release as a click reports
|
||||
/// `CollectionEvent::CommandClick` with the modifiers held.
|
||||
pub(super) command: Option<K>,
|
||||
/// The marquee a press on empty space started.
|
||||
pub(super) marquee: Option<MarqueeDrag<K>>,
|
||||
/// A plain press on the only selected item of a `slow_click_rename` collection, waiting to become a rename.
|
||||
@@ -181,7 +184,18 @@ pub(super) struct SlowClick<K> {
|
||||
|
||||
impl<K> Default for Drags<K> {
|
||||
fn default() -> Self {
|
||||
Drags { grabbed: None, sweep: None, moving: None, pending: None, collapse: None, rename: None, activate: None, marquee: None, slow: None }
|
||||
Drags {
|
||||
grabbed: None,
|
||||
sweep: None,
|
||||
moving: None,
|
||||
pending: None,
|
||||
collapse: None,
|
||||
rename: None,
|
||||
activate: None,
|
||||
command: None,
|
||||
marquee: None,
|
||||
slow: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -602,7 +602,12 @@ impl<C: Collection> ShapeHooks<C> for ItemsHooks<'_, '_, C> {
|
||||
self.events.push(CollectionEvent::Open(OpenChange { key: key.clone(), open: ask.open, all: ask.all }));
|
||||
}
|
||||
if let Some(at) = out.menu_request() {
|
||||
self.events.push(CollectionEvent::ContextMenu(ContextMenuRequest { key: key.clone(), at: self.on_screen(at), column: None }));
|
||||
self.events.push(CollectionEvent::ContextMenu(ContextMenuRequest {
|
||||
key: key.clone(),
|
||||
at: self.on_screen(at),
|
||||
column: None,
|
||||
part: None,
|
||||
}));
|
||||
}
|
||||
if let Some(event) = out.into_event() {
|
||||
self.events.push(CollectionEvent::Item(key.clone(), event));
|
||||
|
||||
@@ -168,7 +168,7 @@ impl<C: Collection> ItemsHooks<'_, '_, C> {
|
||||
let rect = self.screen(layout, at.rect, row, at.offset, at.rtl);
|
||||
crate::runtime::keys::request_context(&cx.core.ctx, cx.core.id, rect);
|
||||
let place = self.on_screen(rect.left_bottom());
|
||||
self.events.push(CollectionEvent::ContextMenu(ContextMenuRequest { key: self.source.key(row), at: place, column: None }));
|
||||
self.events.push(CollectionEvent::ContextMenu(ContextMenuRequest { key: self.source.key(row), at: place, column: None, part: None }));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -87,7 +87,12 @@ impl<'r, C: Collection> ItemsHooks<'_, 'r, C> {
|
||||
self.events.push(CollectionEvent::Open(OpenChange { key: key.clone(), open: ask.open, all: ask.all }));
|
||||
}
|
||||
if let Some(at) = output.menu_request() {
|
||||
self.events.push(CollectionEvent::ContextMenu(ContextMenuRequest { key: key.clone(), at: self.on_screen(at), column: None }));
|
||||
self.events.push(CollectionEvent::ContextMenu(ContextMenuRequest {
|
||||
key: key.clone(),
|
||||
at: self.on_screen(at),
|
||||
column: None,
|
||||
part: None,
|
||||
}));
|
||||
}
|
||||
if let Some(event) = output.into_event() {
|
||||
self.events.push(CollectionEvent::Item(key.clone(), event));
|
||||
|
||||
@@ -80,6 +80,10 @@ impl<C: Collection> ItemsHooks<'_, '_, C> {
|
||||
let toggle = on_toggle.filter(|_| primary);
|
||||
if let Some((column, toggle)) = toggle {
|
||||
self.begin_toggle(cx, at, column, toggle);
|
||||
} else if primary && props.command_click && mods_now.cmd {
|
||||
// A command on the row, reported when the press ends as a click; the selection stays.
|
||||
self.drags.command = Some(self.source.key(at));
|
||||
self.move_lead(at, LeadSource::Pointer);
|
||||
} else if !secondary_on_toggle {
|
||||
let mods = crate::runtime::pointer::frame(&ctx).mods;
|
||||
let key = self.source.key(at);
|
||||
@@ -170,6 +174,8 @@ impl<C: Collection> ItemsHooks<'_, '_, C> {
|
||||
let mods = crate::runtime::pointer::frame(&ctx).mods;
|
||||
if !(mods.shift || mods.cmd || mods.alt) {
|
||||
self.pick(comp, at, false, false);
|
||||
} else if props.command_click && mods.cmd {
|
||||
self.events.push(CollectionEvent::CommandClick { key: self.source.key(at), mods });
|
||||
}
|
||||
}
|
||||
match cx.core.press.phase {
|
||||
@@ -195,6 +201,12 @@ impl<C: Collection> ItemsHooks<'_, '_, C> {
|
||||
{
|
||||
self.begin_rename(comp, cx, at);
|
||||
}
|
||||
if let Some(key) = self.drags.command.take()
|
||||
&& click
|
||||
&& self.index_of(&key).is_some()
|
||||
{
|
||||
self.events.push(CollectionEvent::CommandClick { key, mods: mods_now });
|
||||
}
|
||||
if let Some(key) = self.drags.activate.take()
|
||||
&& click
|
||||
&& self.index_of(&key).is_some()
|
||||
@@ -219,18 +231,19 @@ impl<C: Collection> ItemsHooks<'_, '_, C> {
|
||||
self.drags.collapse = None;
|
||||
self.drags.rename = None;
|
||||
self.drags.activate = None;
|
||||
self.drags.command = None;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
// A secondary release on a row asks for its menu (9.2 rule 3); the row joins the selection unless it is in it.
|
||||
// Something over the collection, or a part of a row or of the collection that takes presses and is under the
|
||||
// pointer, keeps the request away.
|
||||
// pointer, keeps the request away; a part that picks its item (a thumbnail) is the row's, and the request names
|
||||
// it.
|
||||
let released = crate::runtime::pointer::frame(&ctx);
|
||||
let hover = crate::primitives::sensing::read_outcome(&ctx, ArenaId::new(cx.core.id)).hover;
|
||||
if released.release == Some(craft_behavior::PointerButton::Secondary)
|
||||
&& cx.core.response.hovered()
|
||||
&& crate::primitives::sensing::read_outcome(&ctx, ArenaId::new(cx.core.id))
|
||||
.hover
|
||||
.is_none_or(|hit| hit.part.is_none() || !hit.sense.takes_press())
|
||||
&& hover.is_none_or(|hit| hit.part.is_none() || !hit.sense.takes_press() || hit.sense.contains(Sense::PICKS_ITEM))
|
||||
&& let Some(pos) = released.pos.filter(|pos| rect.contains(xf.map_or(*pos, |map| map.to_layer(*pos))))
|
||||
&& let Some(at) = layout.hit(local(pos), offset)
|
||||
&& self.source.enabled(at)
|
||||
@@ -243,7 +256,8 @@ impl<C: Collection> ItemsHooks<'_, '_, C> {
|
||||
}
|
||||
}
|
||||
self.move_lead(at, LeadSource::Pointer);
|
||||
let request = self.menu_request(layout, Placement { rect, offset, rtl }, at, local(pos));
|
||||
let mut request = self.menu_request(layout, Placement { rect, offset, rtl }, at, local(pos));
|
||||
request.part = hover.filter(|hit| hit.owner != cx.core.id).and_then(|hit| hit.part);
|
||||
self.events.push(CollectionEvent::ContextMenu(request));
|
||||
}
|
||||
self.slow_click_due(comp, cx);
|
||||
@@ -282,7 +296,7 @@ impl<C: Collection> ItemsHooks<'_, '_, C> {
|
||||
let mut cells = Vec::new();
|
||||
layout.cells(row, offset, &mut cells);
|
||||
let column = cells.into_iter().find(|(_, cell)| cell.contains(point)).map(|(column, _)| column);
|
||||
ContextMenuRequest { key: self.source.key(row), at: place, column }
|
||||
ContextMenuRequest { key: self.source.key(row), at: place, column, part: None }
|
||||
}
|
||||
|
||||
/// The response of each part an item asked to open a menu from (`ItemCx::part_opener`): the part's click this pass
|
||||
|
||||
@@ -413,6 +413,8 @@ pub enum CollectionKind {
|
||||
Preview,
|
||||
/// A context menu was asked for.
|
||||
ContextMenu,
|
||||
/// A Cmd/Ctrl click on an item of a `command_click` collection.
|
||||
CommandClick,
|
||||
/// Delete was pressed on items.
|
||||
Delete,
|
||||
/// A tree row opened or closed.
|
||||
@@ -449,6 +451,7 @@ impl CollectionKind {
|
||||
CollectionEvent::EmptyActivated => CollectionKind::EmptyActivated,
|
||||
CollectionEvent::Preview(_) => CollectionKind::Preview,
|
||||
CollectionEvent::ContextMenu(_) => CollectionKind::ContextMenu,
|
||||
CollectionEvent::CommandClick { .. } => CollectionKind::CommandClick,
|
||||
CollectionEvent::Delete(_) => CollectionKind::Delete,
|
||||
CollectionEvent::Open(_) => CollectionKind::Open,
|
||||
CollectionEvent::Reorder(edit) => CollectionKind::Reorder(EditKind::of(edit)?),
|
||||
|
||||
@@ -192,6 +192,7 @@ const COLLECTION: &[Forwarded] = &[
|
||||
("min_rows", "u16", "The collection is at least this many rows tall; rows it does not have count the control height."),
|
||||
("max_rows", "u16", "The collection shows at most this many rows and scrolls the rest, inside a scrolling body too."),
|
||||
("notch", "::craft_ui::contract::Notch", "The distance one wheel notch scrolls the rows."),
|
||||
("command_click", "bool", "A Cmd/Ctrl click on an item is a command (`CollectionEvent::CommandClick`), not a pick."),
|
||||
(
|
||||
"current",
|
||||
"::core::option::Option<impl ::core::marker::Send + ::core::marker::Sync + 'static>",
|
||||
|
||||
@@ -38,7 +38,7 @@ const LINES: PartKey = PartKey::new("lines");
|
||||
pub(super) const TRAILING: PartKey = PartKey::new("trailing");
|
||||
const TRAILING_ICON: PartKey = PartKey::new("trailing-icon");
|
||||
const TRAILING_TEXT: PartKey = PartKey::new("trailing-text");
|
||||
const THUMB: PartKey = PartKey::new("thumb");
|
||||
pub(super) const THUMB: PartKey = PartKey::new("thumb");
|
||||
|
||||
/// The name a pattern with `{row}` gives for the row labelled `row`.
|
||||
pub(super) fn named(pattern: &Label, row: &str) -> String {
|
||||
|
||||
@@ -80,6 +80,15 @@ pub enum ThumbTarget {
|
||||
Extra(usize),
|
||||
}
|
||||
|
||||
impl ThumbTarget {
|
||||
/// The thumbnail a part of a row names, as a collection reports the part under the pointer
|
||||
/// (`ContextMenuRequest::part`); `None` for any other part.
|
||||
pub fn of_part(part: craft_behavior::PartRef) -> Option<ThumbTarget> {
|
||||
let at = part.index.filter(|_| part.key == super::show::THUMB)?;
|
||||
usize::try_from(at).ok().map(target_at)
|
||||
}
|
||||
}
|
||||
|
||||
/// Paints into a thumbnail's rect through the content painter, which is clipped to it.
|
||||
pub type ThumbPaint<'a> = Box<dyn FnOnce(&ContentPainterCx, Rect) + 'a>;
|
||||
|
||||
|
||||
@@ -89,3 +89,43 @@ fn the_row_menu_button_shows_on_the_hovered_and_selected_rows_and_asks_for_the_r
|
||||
"a click on the button asks for its row's menu"
|
||||
);
|
||||
}
|
||||
|
||||
/// A right-click on a row's thumbnail asks for the row's menu and names the thumbnail. With command clicks on, a
|
||||
/// Cmd/Ctrl click on a row's label or on its thumbnail is a command on the row and leaves the selection alone.
|
||||
#[trial]
|
||||
fn pointer_commands_name_the_thumbnail_and_leave_the_selection() {
|
||||
use craft_ui::list::{ThumbSize, ThumbTarget};
|
||||
use craft_ui::testing::pointer;
|
||||
use egui::{Modifiers, Rect};
|
||||
let seen = Rc::new(RefCell::new(Vec::new()));
|
||||
let thumbs = Rc::new(RefCell::new([Rect::NOTHING; 3]));
|
||||
let (held, painted) = (seen.clone(), thumbs.clone());
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let keys = [0_usize, 1, 2];
|
||||
let source = keyed(&keys, |key: &usize| *key);
|
||||
let out = List::new("Channels").command_click(true).show_items(ui, &source, &mut |key, _| {
|
||||
let (at, painted) = (*key, painted.clone());
|
||||
ListRow::new(NAMES[*key]).thumb(ThumbSize::Medium, move |_, rect| painted.borrow_mut()[at] = rect)
|
||||
});
|
||||
held.borrow_mut().extend(out.events);
|
||||
});
|
||||
fixture.settle();
|
||||
let thumb = |at: usize| thumbs.borrow()[at].center();
|
||||
fixture.frame(vec![pointer::moved(thumb(1)), pointer::secondary(thumb(1), true)]);
|
||||
fixture.frame(vec![pointer::secondary(thumb(1), false)]).settle();
|
||||
let menu = seen.borrow().iter().find_map(|event| match event {
|
||||
CollectionEvent::ContextMenu(request) => Some((request.key, request.part.and_then(ThumbTarget::of_part))),
|
||||
_ => None,
|
||||
});
|
||||
assert_eq!(menu, Some((1, Some(ThumbTarget::First))), "the right-click on the thumbnail asks for the row's menu and names it");
|
||||
|
||||
seen.borrow_mut().clear();
|
||||
let label = fixture.by_label(NAMES[0]).expect("the first row").rect.right_center() - Vec2::new(20.0, 0.0);
|
||||
fixture.hold(Modifiers::COMMAND).click_at(label).settle();
|
||||
fixture.click_at(thumb(2)).settle();
|
||||
fixture.hold(Modifiers::NONE);
|
||||
let commands: Vec<usize> =
|
||||
seen.borrow().iter().filter_map(|event| if let CollectionEvent::CommandClick { key, .. } = event { Some(*key) } else { None }).collect();
|
||||
assert_eq!(commands, [0, 2], "a Cmd/Ctrl click on the label and one on the thumbnail are commands on their rows");
|
||||
assert!(!seen.borrow().iter().any(|event| matches!(event, CollectionEvent::Selection(_))), "the selection is left alone");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user