mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 00:57:26 +00:00
ui-contract: a text part keeps its selection while a menu bar or a menu the keyboard goes back to it from has the focus, as it did for its own edit menu: the window's Edit menu showed Copy disabled for a field's selected text
This commit is contained in:
@@ -438,6 +438,12 @@ fn prepare(core: &mut CxCore<'_>, ui: &mut Ui, decl: &TextPart, state: &mut Text
|
||||
let ink = decl.options.ink.map_or(ink, |own| crate::style::ink_color(&crate::runtime::env::Env::of(ui).tokens, own));
|
||||
let ink = if enabled { ink } else { crate::style::rgba_color(crate::runtime::env::Env::of(ui).tokens.color.text.disabled) };
|
||||
let held = core.popup(EDIT_MENU).is_open_in(core);
|
||||
// A menu bar or a menu the keyboard goes back to the part from has the focus: their Cut and Copy act on the
|
||||
// selection the part keeps for them.
|
||||
let returns_here = crate::runtime::focus::focused_stop(&ctx)
|
||||
.filter(|(focused, _)| *focused != id)
|
||||
.and_then(|(focused, _)| crate::runtime::focus::returning(&ctx, focused))
|
||||
.is_some_and(|((to, _), _)| to == id);
|
||||
// The bound caret is compared with the widget's before anything else this pass moves it (AccessKit, Select All,
|
||||
// the typing): last pass reported the widget's caret, so only an app write differs here. A composition keeps its
|
||||
// caret.
|
||||
@@ -468,6 +474,7 @@ fn prepare(core: &mut CxCore<'_>, ui: &mut Ui, decl: &TextPart, state: &mut Text
|
||||
rtl,
|
||||
focused,
|
||||
held,
|
||||
keeps_selection: held || returns_here,
|
||||
ink,
|
||||
intents: &mut core.intents,
|
||||
list_open,
|
||||
|
||||
@@ -83,9 +83,10 @@ pub(crate) struct TextState {
|
||||
pub change: Option<DraftChange>,
|
||||
/// `Cx::select_all_text` asked for the whole text to be selected at the start of the next pass.
|
||||
pub select_all: bool,
|
||||
/// The selection, `(caret, anchor)`, the text part had when its edit menu took the focus: egui collapses the
|
||||
/// selection of a widget without focus, and the menu's command acts on it once the focus is back, which reaches the
|
||||
/// widget the pass after the menu closes.
|
||||
/// The selection, `(caret, anchor)`, the text part had when its edit menu, or a menu bar or menu the keyboard goes
|
||||
/// back to it from, took the focus (`AdapterIn::keeps_selection`): egui collapses the selection of a widget without
|
||||
/// focus, and the menu's command acts on it once the focus is back, which reaches the widget the pass after the
|
||||
/// menu closes.
|
||||
pub held_selection: Option<(usize, usize)>,
|
||||
}
|
||||
|
||||
@@ -99,8 +100,8 @@ pub(crate) fn text_id(id: Id, key: PartKey) -> Id {
|
||||
/// picks still reach the part and the edit menu shows them disabled. Cut, Copy and Delete then do nothing and never
|
||||
/// reach the app; an Undo or Redo with no step goes on to the app command (`commands::fall_through`), which the board
|
||||
/// resolves it to. The board reads the keys the stop registered; a selection or text change repaints, which registers
|
||||
/// them again. The selection is the one the part acts on: `held`, the one it kept while its edit menu has the focus
|
||||
/// (egui collapses the selection of a widget without focus), else its widget's. `text` is the part's current text, which
|
||||
/// them again. The selection is the one the part acts on: `held`, the one it kept while its edit menu or a menu bar has
|
||||
/// the focus (egui collapses the selection of a widget without focus), else its widget's. `text` is the part's current text, which
|
||||
/// the widget's undo steps are judged against.
|
||||
pub(crate) fn keys_of(ctx: &Context, widget: Id, held: Option<(usize, usize)>, text: &str, base: KeyUse, options: &TextOptions) -> KeyUse {
|
||||
// A part whose Tab inserts, rewrites or reports keeps Tab, so the focus service never moves focus on it.
|
||||
@@ -151,6 +152,9 @@ pub(crate) struct AdapterIn<'a> {
|
||||
/// The text part's own edit menu is open and holds the focus: losing the focus to it ends no typing run, and
|
||||
/// getting it back is no new focus.
|
||||
pub held: bool,
|
||||
/// The part keeps the selection it had for the surface that holds the focus: its own edit menu, or a menu bar or
|
||||
/// menu the keyboard goes back to the part from, whose picks act on it.
|
||||
pub keeps_selection: bool,
|
||||
pub ink: egui::Color32,
|
||||
pub intents: &'a mut Intents,
|
||||
/// A list the component opened on the text is shown (a dropdown, the completions): Escape is the list's, except
|
||||
@@ -444,7 +448,7 @@ fn held_mods(ctx: &Context) -> Mods {
|
||||
pub(crate) fn run(state: &mut TextState, mut input: AdapterIn<'_>) -> AdapterOut {
|
||||
let (ctx, widget) = (input.ctx, text_id(input.id, input.key));
|
||||
let focused_before = input.focused;
|
||||
if input.held {
|
||||
if input.keeps_selection {
|
||||
if state.held_selection.is_none() {
|
||||
state.held_selection = selection(ctx, widget);
|
||||
}
|
||||
|
||||
@@ -1860,11 +1860,7 @@ impl Returning {
|
||||
/// the menus close (`focus::out_of_menus`); from the bar, on to the stop it took the focus from. `None` when the
|
||||
/// focus is in neither, or a menu opened with nothing focused.
|
||||
fn of(ctx: &Context, focused: Id) -> Option<Self> {
|
||||
match focus::focused_rec(ctx).and_then(|rec| focus::out_of_menus(ctx, rec.scope)) {
|
||||
Some(out) if out.bar => Some(Self::Bar(out.to.0)),
|
||||
Some(out) => Some(Self::Stop(out.to)),
|
||||
None => focus::bar_return_of(ctx, focused).map(|(held, _)| Self::Bar(held)),
|
||||
}
|
||||
focus::returning(ctx, focused).map(|(to, bar)| if bar { Self::Bar(to.0) } else { Self::Stop(to) })
|
||||
}
|
||||
|
||||
/// The component whose command chain the shortcut matches in.
|
||||
|
||||
@@ -618,6 +618,16 @@ pub(crate) fn bar_return_of(ctx: &Context, focused: Id) -> Option<StopKey> {
|
||||
with(ctx, |state| state.bar_return.as_ref().filter(|(bar, _)| *bar == focused).map(|(_, key)| *key))
|
||||
}
|
||||
|
||||
/// The stop the keyboard goes back to while `focused` holds it inside open menus or on a menu bar, and whether a menu
|
||||
/// bar took it from that stop: a shortcut pressed there matches at that stop, and a text part keeps its selection for
|
||||
/// the menus' picks. `None` when the focus is in neither, or a menu opened with nothing focused.
|
||||
pub(crate) fn returning(ctx: &Context, focused: Id) -> Option<(StopKey, bool)> {
|
||||
match focused_rec(ctx).and_then(|rec| out_of_menus(ctx, rec.scope)) {
|
||||
Some(out) => Some((out.to, out.bar)),
|
||||
None => bar_return_of(ctx, focused).map(|held| (held, true)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Where focus goes once every menu around the scope `from` closes, as `out_of_menus` finds it.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub(crate) struct OutOfMenus {
|
||||
|
||||
@@ -574,3 +574,30 @@ fn space_on_a_bar_focused_from_nothing_goes_to_the_document_canvas() {
|
||||
assert_eq!(fixture.focused_stop().map(|(id, _)| id), Some(canvas), "and the keyboard goes to the canvas (alt {alt})");
|
||||
}
|
||||
}
|
||||
|
||||
/// Tc-9 (3): the selection of a focused field holds while a menu the window's menu bar opened has the keyboard, which
|
||||
/// goes back to the field: the bar's Edit menu shows Copy enabled for it. The field kept its selection only for its own
|
||||
/// edit menu, so Copy was disabled there.
|
||||
#[trial]
|
||||
fn a_fields_selection_holds_while_the_menu_bar_has_the_keyboard() {
|
||||
use craft_ui::contract::{Bind, Props as _};
|
||||
use craft_ui::text::TextField;
|
||||
use std::cell::RefCell;
|
||||
let text = RefCell::new("hello".to_owned());
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let titles = vec![MenuTitle::new("&Edit", |ui| {
|
||||
MenuItem::command(EditCommands::Copy).show(ui);
|
||||
})];
|
||||
MenuBar::new("Main menu", titles).show(ui);
|
||||
TextField::new("Name", Bind::bound(&mut *text.borrow_mut())).id_salt("name").show(ui);
|
||||
});
|
||||
fixture.os(Os::Linux);
|
||||
fixture.settle();
|
||||
let field = fixture.by_label("Name").expect("the field");
|
||||
fixture.click(&field).settle();
|
||||
fixture.key(Key::A, Modifiers::CTRL).settle();
|
||||
let edit = fixture.by_label("Edit").expect("the Edit title");
|
||||
fixture.click(&edit).settle();
|
||||
let copy = fixture.by_label("Copy").expect("the Edit menu is open");
|
||||
assert!(!fixture.tree().is_disabled_key(copy.node), "Copy applies to the field's selection");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user