mirror of
https://github.com/storytold/deckcraft.git
synced 2026-12-02 10:38:15 -06:00
thumbs: a menu the control protocol asks for hangs from its slide or section row
ui.contextMenu {part: thumbs} opened the menu from the whole pane, so it
showed above the pane and scrolled. It now hangs from the requested row's
rect, as the context-menu key's menu does; a right click still opens at the
pointer.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
57a854e086
commit
9b8f00494f
@@ -3,6 +3,7 @@
|
||||
use std::collections::{BTreeMap, HashMap};
|
||||
|
||||
use craft::behavior::{IconId, Selection, SelectionModel as _, keyed};
|
||||
use craft::ui::contract::overlay::Anchor;
|
||||
use craft::ui::contract::{
|
||||
Bind, BodyCx, CollectionEvent, Props as _, ReorderRule, SelectMode, Show as _, ShowCollection as _, ShowContainer as _, WordSetCx as _,
|
||||
};
|
||||
@@ -161,23 +162,27 @@ pub(crate) fn body(app: &mut SlideApp, ui: &mut Ui, cx: &mut BodyCx<'_>) {
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
// Requests use the control protocol's parts (`ui.contextMenu`): a slide by its index, a section by its index.
|
||||
// Requests use the control protocol's parts (`ui.contextMenu`): a slide by its index, a section by its index. A
|
||||
// requested menu hangs from its row, as the context-menu key's does; a right click's hangs at the pointer.
|
||||
let row = |key: u64| out.response.shown.iter().find(|row| row.key == key).map(|row| Anchor::Rect(row.rect));
|
||||
let request = app.pane_menu.clone();
|
||||
if let Some((_, key)) = request.as_ref().filter(|(kind, _)| kind == "thumbs-section") {
|
||||
section_menu(ui, app, *key as usize, &out.response.response);
|
||||
section_menu(ui, app, *key as usize, &out.response.response, row(SECTION_KEYS + key));
|
||||
} else if let Some(key) = request
|
||||
.as_ref()
|
||||
.filter(|(kind, _)| kind == "thumbs")
|
||||
.and_then(|(_, at)| usize::try_from(*at).ok().and_then(|at| doc.slides.get(at)))
|
||||
.map(|slide| u64::from(slide.id.0))
|
||||
.or_else(|| out.response.menu.as_ref().map(|request| request.key).filter(|key| *key < SECTION_KEYS))
|
||||
{
|
||||
slide_menu(ui, app, key, &out.response.response);
|
||||
slide_menu(ui, app, key, &out.response.response, row(key));
|
||||
} else if let Some(key) = out.response.menu.as_ref().map(|request| request.key).filter(|key| *key < SECTION_KEYS) {
|
||||
slide_menu(ui, app, key, &out.response.response, None);
|
||||
}
|
||||
}
|
||||
|
||||
/// Shows the menu for a slide id, preserving a selection that already contains it.
|
||||
pub(crate) fn slide_menu(ui: &mut Ui, app: &mut SlideApp, key: u64, opener: &Response) {
|
||||
/// Shows the menu for a slide id, preserving a selection that already contains it. `anchor` places a menu the control
|
||||
/// protocol asked for.
|
||||
pub(crate) fn slide_menu(ui: &mut Ui, app: &mut SlideApp, key: u64, opener: &Response, anchor: Option<Anchor>) {
|
||||
let Some(state) = app.session.active() else { return };
|
||||
let Some(at) = state.doc.slides.iter().position(|slide| u64::from(slide.id.0) == key) else {
|
||||
app.pane_menu = None;
|
||||
@@ -192,6 +197,9 @@ pub(crate) fn slide_menu(ui: &mut Ui, app: &mut SlideApp, key: u64, opener: &Res
|
||||
let mut menu = Menu::context(words.slide_menu.clone(), opener);
|
||||
if requested {
|
||||
menu = menu.open(Bind::given(true));
|
||||
if let Some(anchor) = anchor {
|
||||
menu = menu.anchor(anchor);
|
||||
}
|
||||
}
|
||||
let out = menu.show(ui, |ui, _| {
|
||||
if !craft::ui::runtime::measure::active(ui.ctx())
|
||||
@@ -226,14 +234,18 @@ pub(crate) fn slide_menu(ui: &mut Ui, app: &mut SlideApp, key: u64, opener: &Res
|
||||
}
|
||||
}
|
||||
|
||||
fn section_menu(ui: &mut Ui, app: &mut SlideApp, at: usize, opener: &Response) {
|
||||
fn section_menu(ui: &mut Ui, app: &mut SlideApp, at: usize, opener: &Response, anchor: Option<Anchor>) {
|
||||
let Some(section) = app.session.active().and_then(|state| state.doc.sections.get(at)) else {
|
||||
app.pane_menu = None;
|
||||
return;
|
||||
};
|
||||
let name = section.name.clone();
|
||||
let words = Env::of(ui).view().words::<ThumbWords>();
|
||||
let out = Menu::context(words.section_menu.clone(), opener).open(Bind::given(true)).show(ui, |ui, cx| {
|
||||
let mut menu = Menu::context(words.section_menu.clone(), opener).open(Bind::given(true));
|
||||
if let Some(anchor) = anchor {
|
||||
menu = menu.anchor(anchor);
|
||||
}
|
||||
let out = menu.show(ui, |ui, cx| {
|
||||
if MenuItem::new(words.rename_section.clone()).show(ui).ran {
|
||||
app.open_dialog("renameSection", json!({"index": at, "name": name}));
|
||||
cx.request_close();
|
||||
@@ -397,6 +409,19 @@ mod tests {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// A slide menu the control protocol asks for hangs from that slide's tile, as the context-menu key's does, not
|
||||
/// from the whole pane.
|
||||
#[test]
|
||||
fn a_requested_slide_menu_hangs_from_its_tile() -> Result<(), String> {
|
||||
let mut h = editor(slides(4)?, 900.0);
|
||||
h.state_mut().pane_menu = Some(("thumbs".into(), 1));
|
||||
h.run_steps(4);
|
||||
let menu = h.get_by_role_and_label(Role::Menu, ThumbWords::english().slide_menu.as_str()).rect();
|
||||
let (row, next) = (tile(&h, 2), tile(&h, 3));
|
||||
assert!(menu.top() >= row.top() && menu.top() < next.top(), "the menu {menu:?} opens at the second tile {row:?}");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// A header counts in the grid's destination, but never in the engine's slide order.
|
||||
#[test]
|
||||
fn slides_move_dest_counts_without_the_moved_slides() -> Result<(), String> {
|
||||
|
||||
@@ -176,7 +176,7 @@ pub fn show(app: &mut SlideApp, ui: &mut Ui) {
|
||||
// PLAN: plan error: panels-w2 must retain pointer GroupMenu requests in CollectionResponse::menu; grid.rs emits them outside the collection menu lifecycle.
|
||||
if let Some(request) = &out.response.menu {
|
||||
match request.key {
|
||||
Key::Slide(id) => crate::thumbs::slide_menu(ui, app, u64::from(id.0), opener),
|
||||
Key::Slide(id) => crate::thumbs::slide_menu(ui, app, u64::from(id.0), opener, None),
|
||||
Key::Section(index) => section_menu(app, ui, index, opener, &words),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user