mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 04:27:49 +00:00
Merge branch 'red/fu-platform' into area/ribbon-fw
This commit is contained in:
@@ -87,6 +87,8 @@ pub enum EditorInput {
|
||||
Newline,
|
||||
/// Tab.
|
||||
Tab,
|
||||
/// Shift+Tab, which reaches only a document canvas's editor part that keeps Tab.
|
||||
BackTab,
|
||||
/// Escape.
|
||||
Cancel,
|
||||
}
|
||||
|
||||
@@ -177,6 +177,8 @@ pub enum Intent {
|
||||
Text(Arc<str>),
|
||||
/// Tab in a component whose key filter keeps it.
|
||||
Tab,
|
||||
/// Shift+Tab in a document canvas's editor part that keeps Tab.
|
||||
BackTab,
|
||||
/// An edit command the focused component lists.
|
||||
Edit(EditCommand),
|
||||
/// Switch to the next or previous page.
|
||||
|
||||
@@ -74,6 +74,10 @@ let scale = store.get().appearance.ui_scale;
|
||||
// On change: only marks the store dirty if the value actually changed.
|
||||
store.update(|p| p.appearance.theme = "dark".into());
|
||||
|
||||
// Values read from another file at startup (a custom dictionary's words) count as loaded: no save is
|
||||
// scheduled, and a later save does not write them over another instance's edit.
|
||||
store.update_loaded(|p| p.spelling.words = words);
|
||||
|
||||
// Every frame: saves 500 ms after the last change, retries failures with backoff.
|
||||
// `web_time::Instant` is `std::time::Instant` on native targets and also works in a browser.
|
||||
let tick = store.tick(web_time::Instant::now());
|
||||
|
||||
@@ -286,6 +286,25 @@ impl<T: Settings> SettingsStore<T> {
|
||||
true
|
||||
}
|
||||
|
||||
/// Changes the settings as loaded rather than as edited: for values that come from another file
|
||||
/// this process reads (a custom dictionary's words), not from the user. The change applies to the
|
||||
/// value and to the merge base alike, so it schedules no save, leaves [`SettingsStore::is_dirty`]
|
||||
/// as it was, and a later save does not count it as this store's change: another instance's edit
|
||||
/// of the same keys is kept. Returns true only if the value changed.
|
||||
pub fn update_loaded(&mut self, f: impl FnOnce(&mut T)) -> bool {
|
||||
let mut next = self.value.clone();
|
||||
f(&mut next);
|
||||
if next == self.value {
|
||||
return false;
|
||||
}
|
||||
// Only the keys this change touched move in the base; a pending edit of other keys stays ours.
|
||||
if let (Some(before), Some(after)) = (value_document(&self.value), value_document(&next)) {
|
||||
merge_changes::<T>(&before, &after, &mut self.base);
|
||||
}
|
||||
self.value = next;
|
||||
true
|
||||
}
|
||||
|
||||
/// Like [`SettingsStore::update`] with an explicit clock, for tests.
|
||||
pub fn update_at(&mut self, now: Instant, f: impl FnOnce(&mut T)) -> bool {
|
||||
let mut next = self.value.clone();
|
||||
@@ -527,6 +546,13 @@ fn default_document<T: Settings>() -> DocumentMut {
|
||||
doc
|
||||
}
|
||||
|
||||
/// `value` serialised as a fresh document; `None` when it does not serialise.
|
||||
fn value_document<T: Settings>(value: &T) -> Option<DocumentMut> {
|
||||
let mut doc = document::new_document(T::SCHEMA_VERSION);
|
||||
merge_into(&mut doc, value).ok()?;
|
||||
Some(doc)
|
||||
}
|
||||
|
||||
/// Exponential backoff: 1 s, 2 s, 4 s, … capped at [`MAX_RETRY_DELAY`].
|
||||
pub fn retry_delay(failures: u32) -> Duration {
|
||||
let exp = failures.saturating_sub(1).min(16);
|
||||
|
||||
@@ -143,6 +143,34 @@ fn a_map_entry_the_app_removes_is_removed_from_the_file() {
|
||||
assert!(last.get().shortcuts.is_empty(), "{}", read_config(tmp.path()));
|
||||
}
|
||||
|
||||
// #3773: words read from another file (a custom dictionary) count as loaded, not as this instance's edit, so a
|
||||
// later save of another change does not write back an entry a second instance removed meanwhile.
|
||||
#[test]
|
||||
fn a_loaded_change_is_not_saved_over_another_instances_edit() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let (mut first, _) = open_in(tmp.path());
|
||||
first.flush().unwrap();
|
||||
let (mut second, _) = open_in(tmp.path());
|
||||
|
||||
let entry = "loaded".to_owned();
|
||||
assert!(first.update_loaded(|p| p.general.recent.push(entry.clone())));
|
||||
assert!(!first.is_dirty(), "a loaded change schedules no save");
|
||||
// The second instance loads the same entry, and its user removes it; the other change makes its file differ from
|
||||
// the one the first instance last read, so the first merges on its next save.
|
||||
second.update_loaded(|p| p.general.recent.push(entry.clone()));
|
||||
second.update(|p| {
|
||||
p.general.recent.retain(|e| *e != entry);
|
||||
p.general.autosave_minutes = 10;
|
||||
});
|
||||
second.flush().unwrap();
|
||||
|
||||
first.update(|p| p.general.language = "fr".into());
|
||||
first.flush().unwrap();
|
||||
let (reloaded, _) = open_in(tmp.path());
|
||||
assert!(!reloaded.get().general.recent.contains(&entry), "{}", read_config(tmp.path()));
|
||||
assert_eq!((reloaded.get().general.language.as_str(), reloaded.get().general.autosave_minutes), ("fr", 10), "both saves merged");
|
||||
}
|
||||
|
||||
/// Options for a `Prefs` store in `dir`, importing `legacy.txt` (its text is the language) when there is no
|
||||
/// `config.toml`.
|
||||
fn read_options(dir: &Path) -> OpenOptions<Prefs> {
|
||||
|
||||
@@ -186,7 +186,7 @@ impl Intents {
|
||||
}
|
||||
|
||||
/// Takes the next intent an editor reads as input: typed text, a move, a delete, an edit command, Enter as a
|
||||
/// newline, Tab, and Escape as a cancel. An arrow move the editor's `keep` does not keep stays for the
|
||||
/// newline, Tab and Shift+Tab, and Escape as a cancel. An arrow move the editor's `keep` does not keep stays for the
|
||||
/// component's navigation (18.4), and with `KeepKeys::None` so do Home, End and the page keys: the editor is
|
||||
/// a part of a grid that navigates until a cell opens.
|
||||
pub(crate) fn take_editor_input(&mut self, keep: focus::KeepKeys) -> Option<EditorInput> {
|
||||
@@ -231,6 +231,7 @@ fn editor_input_of(intent: &Intent) -> Option<EditorInput> {
|
||||
Intent::Edit(command) => Some(EditorInput::Edit(*command)),
|
||||
Intent::Submit => Some(EditorInput::Newline),
|
||||
Intent::Tab => Some(EditorInput::Tab),
|
||||
Intent::BackTab => Some(EditorInput::BackTab),
|
||||
Intent::Cancel => Some(EditorInput::Cancel),
|
||||
_ => None,
|
||||
}
|
||||
|
||||
@@ -542,11 +542,22 @@ fn press_route(ctx: &Context, os: Os, rec: &StopRec, press: &Press, driven: Opti
|
||||
None if press.key == Key::Enter && !press.mods.cmd && !press.mods.ctrl => PressRoute::Intent(Intent::Submit),
|
||||
None if press.key == Key::Space && plain && stop.keys.activate.space() => PressRoute::Activate,
|
||||
None if press.key == Key::Tab && plain && stop.keys.keep == focus::KeepKeys::Tab => PressRoute::Intent(Intent::Tab),
|
||||
// A document canvas's editor reads Shift+Tab itself (a word processor's outdent, the previous slot of an
|
||||
// equation). No other stop that keeps Tab gets it as an intent.
|
||||
None if press.key == Key::Tab && press.mods == Mods::SHIFT && stop.keys.keep == focus::KeepKeys::Tab && is_canvas_editor(stop) => {
|
||||
PressRoute::Intent(Intent::BackTab)
|
||||
}
|
||||
None if stop.role.nudge => nudge_of(press.key, press.mods).map_or(PressRoute::Free, PressRoute::Nudge),
|
||||
None => PressRoute::Free,
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether `stop` is a document canvas's editor part: the canvas's own stop (`KeyUse::chrome_key`, the record the key
|
||||
/// stage reads as the window's canvas) holding its editor's text keys.
|
||||
fn is_canvas_editor(stop: &focus::Stop) -> bool {
|
||||
stop.keys.chrome_key && stop.keys.owner == OwnerClass::Text
|
||||
}
|
||||
|
||||
fn is_vertical(mv: NavMove) -> bool {
|
||||
matches!(mv, NavMove::Up | NavMove::Down | NavMove::PageUp | NavMove::PageDown | NavMove::Start | NavMove::End)
|
||||
}
|
||||
|
||||
@@ -32,12 +32,14 @@ struct Probe<'a> {
|
||||
report: bool,
|
||||
purpose: EditorPurpose,
|
||||
doc: Option<&'a Doc>,
|
||||
/// The probe is a document canvas's editor: it marks its window as showing a document and keeps Tab.
|
||||
canvas: bool,
|
||||
}
|
||||
|
||||
impl<'a> Probe<'a> {
|
||||
/// A code editor that logs into `log` and keeps no text.
|
||||
fn new(log: &'a RefCell<Vec<EditorInput>>, report: bool) -> Self {
|
||||
Probe { common: Common::new(), log, report, purpose: EditorPurpose::Code, doc: None }
|
||||
Probe { common: Common::new(), log, report, purpose: EditorPurpose::Code, doc: None, canvas: false }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,7 +56,13 @@ impl Component for Probe<'_> {
|
||||
|
||||
fn keys(&self) -> KeyUse {
|
||||
// A prose field leaves Tab to the focus order, so Tab can take the keyboard away from it.
|
||||
if self.purpose == EditorPurpose::Code { KeyUse::CODE } else { KeyUse::of(craft_ui::contract::Input::Editor) }
|
||||
if self.canvas {
|
||||
KeyUse::CODE.with_chrome_key(true)
|
||||
} else if self.purpose == EditorPurpose::Code {
|
||||
KeyUse::CODE
|
||||
} else {
|
||||
KeyUse::of(craft_ui::contract::Input::Editor)
|
||||
}
|
||||
}
|
||||
|
||||
fn sense(&self) -> Sense {
|
||||
@@ -182,11 +190,12 @@ struct View<'r> {
|
||||
purpose: EditorPurpose,
|
||||
doc: Option<&'r Doc>,
|
||||
next: Option<&'r RefCell<Seen>>,
|
||||
canvas: bool,
|
||||
}
|
||||
|
||||
impl View<'_> {
|
||||
fn show(self, ui: &mut egui::Ui) {
|
||||
let probe = Probe { purpose: self.purpose, doc: self.doc, ..Probe::new(self.log, true) };
|
||||
let probe = Probe { purpose: self.purpose, doc: self.doc, canvas: self.canvas, ..Probe::new(self.log, true) };
|
||||
let response = probe.label("Source").read_only(self.read_only).id_salt("code").show_at(ui, AT);
|
||||
self.id.set(response.id);
|
||||
if let Some(seen) = self.next {
|
||||
@@ -204,6 +213,8 @@ struct Rig {
|
||||
doc: Option<Doc>,
|
||||
/// The control after the probe that Tab takes the keyboard to, when the rig shows one.
|
||||
next: Option<RefCell<Seen>>,
|
||||
/// The probe is a document canvas's editor.
|
||||
canvas: bool,
|
||||
}
|
||||
|
||||
impl Rig {
|
||||
@@ -221,6 +232,7 @@ impl Rig {
|
||||
purpose,
|
||||
doc,
|
||||
next: next.then(|| RefCell::new(Seen::default())),
|
||||
canvas: false,
|
||||
};
|
||||
for _ in 0..3 {
|
||||
rig.frame(Vec::new());
|
||||
@@ -237,10 +249,19 @@ impl Rig {
|
||||
purpose: self.purpose,
|
||||
doc: self.doc.as_ref(),
|
||||
next: self.next.as_ref(),
|
||||
canvas: self.canvas,
|
||||
};
|
||||
self.frames.frame(events, |ui| view.show(ui))
|
||||
}
|
||||
|
||||
/// A rig whose probe is a document canvas's editor, with a control after it.
|
||||
fn canvas_editor() -> Rig {
|
||||
let mut rig = Rig::with(EditorPurpose::Text, None, true);
|
||||
rig.canvas = true;
|
||||
rig.frame(Vec::new());
|
||||
rig
|
||||
}
|
||||
|
||||
fn focus(&mut self) {
|
||||
let view = View {
|
||||
log: &self.log,
|
||||
@@ -249,6 +270,7 @@ impl Rig {
|
||||
purpose: self.purpose,
|
||||
doc: self.doc.as_ref(),
|
||||
next: self.next.as_ref(),
|
||||
canvas: self.canvas,
|
||||
};
|
||||
click(&mut self.frames, AT.center(), &mut |ui| view.show(ui));
|
||||
self.log.borrow_mut().clear();
|
||||
@@ -544,3 +566,33 @@ fn a_focused_text_field_does_not_interrupt_the_input_method_every_frame() {
|
||||
}
|
||||
}
|
||||
|
||||
/// canvas-w10 F17: Shift+Tab reaches a document canvas's editor as `BackTab` (a word processor's outdent, the
|
||||
/// previous slot of an equation), where an editor that is no canvas's does not read it. A text field keeps Shift+Tab
|
||||
/// as a focus move: the keyboard leaves it, so typing after it no longer lands there.
|
||||
#[trial]
|
||||
fn shift_tab_is_a_canvas_editors_back_tab_and_moves_focus_out_of_a_text_field() {
|
||||
let mut canvas = Rig::canvas_editor();
|
||||
canvas.focus();
|
||||
canvas.frame(vec![key_press(Key::Tab, Modifiers::SHIFT)]);
|
||||
canvas.frame(vec![Event::Text("x".into())]);
|
||||
let inputs = canvas.take();
|
||||
assert!(
|
||||
matches!(inputs.as_slice(), [EditorInput::BackTab, EditorInput::Text(text)] if &**text == "x"),
|
||||
"the canvas editor reads Shift+Tab and keeps the keyboard: {inputs:?}"
|
||||
);
|
||||
|
||||
let mut code = Rig::new();
|
||||
code.focus();
|
||||
code.frame(vec![key_press(Key::Tab, Modifiers::SHIFT)]);
|
||||
let inputs = code.take();
|
||||
assert!(!inputs.contains(&EditorInput::BackTab), "only a canvas's editor reads Shift+Tab: {inputs:?}");
|
||||
|
||||
let mut field = Rig::with(EditorPurpose::Text, None, true);
|
||||
field.focus();
|
||||
field.frame(vec![key_press(Key::Tab, Modifiers::SHIFT)]);
|
||||
field.frame(Vec::new());
|
||||
field.frame(vec![Event::Text("x".into())]);
|
||||
let inputs = field.take();
|
||||
assert!(inputs.is_empty(), "Shift+Tab takes the keyboard out of the text field: {inputs:?}");
|
||||
}
|
||||
|
||||
|
||||
@@ -151,7 +151,7 @@ An app with its own eframe `App` calls the hooks in `craft::ui::shell::embed`. `
|
||||
|
||||
| What | Effect on the app's own egui code |
|
||||
| --- | --- |
|
||||
| Tab | Bare Tab moves framework focus and never reaches egui, unless the focused framework stop keeps Tab or an app's own widget has the focus (below), or nothing has the focus in a window that registered no focus stop: egui then moves its own focus into the app's widgets. A window with stops but no Tab entry (on the Mac, where Tab reaches only text fields by default) keeps the Tab from egui, which would otherwise focus framework controls the platform's rule skips. With `RootEnv::tab_toggles_chrome`, Tab toggles the chrome in a window that shows a document canvas, with nothing focused or the canvas focused, and arrives in `FrameOutput::chrome_toggles`. The canvas is a framework canvas, or the app's own egui canvas declared each pass with `ui::runtime::focus::host_canvas`. With nothing focused, the canvas is the key target: a canvas that keeps Tab (`KeyUse::CANVAS.with_keep(KeepKeys::Tab)`) gets the press in egui's input, as when it is focused, and nothing toggles. |
|
||||
| Tab | Bare Tab moves framework focus and never reaches egui, unless the focused framework stop keeps Tab or an app's own widget has the focus (below), or nothing has the focus in a window that registered no focus stop: egui then moves its own focus into the app's widgets. A window with stops but no Tab entry (on the Mac, where Tab reaches only text fields by default) keeps the Tab from egui, which would otherwise focus framework controls the platform's rule skips. With `RootEnv::tab_toggles_chrome`, Tab toggles the chrome in a window that shows a document canvas, with nothing focused or the canvas focused, and arrives in `FrameOutput::chrome_toggles`. The canvas is a framework canvas, or the app's own egui canvas declared each pass with `ui::runtime::focus::host_canvas`. With nothing focused, the canvas is the key target: a canvas that keeps Tab (`KeyUse::CANVAS.with_keep(KeepKeys::Tab)`) gets the press in egui's input, as when it is focused, and nothing toggles. A focused canvas whose editor part keeps Tab reads Shift+Tab as `Intent::BackTab` (`EditorInput::BackTab`); no other stop gets it as an intent, and a text field keeps Shift+Tab as a focus move. |
|
||||
| F6 | F6 and Shift+F6 move between panes and never reach egui. They are the defaults of the commands `window.nextPane` and `window.previousPane` (`NavCommand`): a default chord that another command binds is that command's, and the overrides move or unbind them (`CommandInput::overrides`). An app whose own shortcut table keeps F6 unbinds them there. |
|
||||
| Escape | Plain Escape (Alt and Shift may be held) goes to the focused framework stop and its scopes; with no framework stop focused, it goes to the window as `Intent::Cancel`. It reaches egui only while an app's own widget has the focus (below). An Escape the runtime takes outside a captured framework press also ends egui's own drag in that pass, as egui's Escape does. |
|
||||
| F10, Alt+letter | Taken while a framework menu bar is registered: F10 (`window.focusMenuBar`, bound and yielded as F6 is) focuses the bar, Alt+letter opens a menu (not on macOS). |
|
||||
|
||||
Reference in New Issue
Block a user