08cdea58d7
Tabs: no cap (a 5000 bound on the saved list only); only the active and 5 recent tabs keep editor state, cold tabs keep caret, view and folds and reload on activation, dirty tabs are never dropped; the strip lays out at most 64 tabs and the overflow list is searchable and virtualised; restore loads only the active tab. Hotkeys: one registry with defaults for tab and panel navigation and opening Outline, Related, Backlinks, Assistant, Lint and Comments, shown in the tooltips; F6 cycles focus; Ctrl+h/l in vim mode; panels are accessibility landmarks. Menus: keycap letters that run the item, vim j/k/h/l/gg/G, focus into the menu and back on Esc, menu and menu-item roles. Explorer: horizontal scroll (Shift+wheel) and full-name tooltips. Comments tab in the right panel (MiniMax M3.1 Flash). Delete confirmation dialog from an earlier session, with its debug code and deadlock removed. Built by Claude Sonnet and MiniMax M3.1 Flash; the whole tree verified with cargo xtask ci (fmt, clippy -D warnings, all tests, layers, tree, i18n) green on a copy of the working tree. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
219 lines
8.7 KiB
Rust
219 lines
8.7 KiB
Rust
//! The hotkey registry driven the way a user does: real key events through whole frames.
|
|
|
|
use egui::{Context, Event, Key, Modifiers, Pos2, RawInput, Rect, vec2};
|
|
use notescraft_ui_egui::app::NotesApp;
|
|
use notescraft_ui_egui::hotkeys::{self, Binding, Hotkey, Owner, Registry, Scope, VimMode};
|
|
use notescraft_ui_egui::right_view::RightTab;
|
|
use notescraft_ui_egui::vim_nav::NavFocus;
|
|
|
|
struct Harness {
|
|
ctx: Context,
|
|
app: NotesApp,
|
|
time: f64,
|
|
}
|
|
|
|
impl Harness {
|
|
fn new(name: &str, notes: &[&str]) -> Option<Self> {
|
|
let path = std::env::temp_dir().join(format!("notescraft-hotkeys-{name}-{}", std::process::id()));
|
|
assert!(std::fs::create_dir_all(&path).is_ok());
|
|
for note in notes {
|
|
assert!(std::fs::write(path.join(note), "# Heading\n\nExample note.\n").is_ok());
|
|
}
|
|
let mut session = notescraft_engine::Session::open(&path).ok()?;
|
|
session.use_settings_dir(path.join("config"));
|
|
let mut app = NotesApp::new(session, notescraft_themes::builtin("catppuccin-mocha")?);
|
|
app.state.animations = false;
|
|
for note in notes {
|
|
app.open_note(note);
|
|
}
|
|
let mut harness = Self { ctx: Context::default(), app, time: 0.0 };
|
|
for _ in 0..4 {
|
|
harness.frame(Vec::new());
|
|
}
|
|
Some(harness)
|
|
}
|
|
|
|
fn frame(&mut self, events: Vec<Event>) -> Vec<(String, Rect)> {
|
|
self.time += 1.0 / 60.0;
|
|
let input = RawInput { screen_rect: Some(Rect::from_min_size(Pos2::ZERO, vec2(1440.0, 900.0))), time: Some(self.time), events, ..RawInput::default() };
|
|
let mut out = self.ctx.run_ui(input, |ui| self.app.ui(ui.ctx()));
|
|
let texts = out
|
|
.shapes
|
|
.iter()
|
|
.filter_map(|shape| match &shape.shape {
|
|
egui::epaint::Shape::Text(text) => Some((text.galley.text().to_owned(), text.visual_bounding_rect())),
|
|
_ => None,
|
|
})
|
|
.collect();
|
|
out.textures_delta.clear();
|
|
texts
|
|
}
|
|
|
|
/// A key press, then two quiet frames so a pending focus move lands.
|
|
fn press(&mut self, key: Key, modifiers: Modifiers) {
|
|
self.frame(vec![Event::Key { key, physical_key: None, pressed: true, repeat: false, modifiers }]);
|
|
self.frame(Vec::new());
|
|
self.frame(Vec::new());
|
|
}
|
|
}
|
|
|
|
fn cmd() -> Modifiers {
|
|
Modifiers { command: true, ..Modifiers::NONE }
|
|
}
|
|
|
|
fn cmd_alt() -> Modifiers {
|
|
Modifiers { command: true, alt: true, ..Modifiers::NONE }
|
|
}
|
|
|
|
#[test]
|
|
fn cmd_alt_o_opens_the_outline_shows_the_panel_and_focuses_it() {
|
|
let Some(mut h) = Harness::new("outline", &["note.md"]) else { panic!("setup") };
|
|
h.app.state.right_visible = false;
|
|
h.frame(Vec::new());
|
|
h.press(Key::O, cmd_alt());
|
|
assert!(h.app.state.right_visible);
|
|
assert_eq!(h.app.right_tab, RightTab::Outline);
|
|
assert_eq!(h.app.vim_nav.focus, NavFocus::Right);
|
|
assert_eq!(hotkeys::focused_region(&h.ctx), Some(NavFocus::Right), "keyboard focus is inside the right panel");
|
|
assert!(h.app.tabs.open.len() == 1, "Cmd+Alt+O is not the quick switcher");
|
|
}
|
|
|
|
#[test]
|
|
fn cmd_alt_backslash_toggles_the_right_panel_and_cmd_backslash_the_sidebar() {
|
|
let Some(mut h) = Harness::new("toggles", &["note.md"]) else { panic!("setup") };
|
|
assert!(h.app.state.right_visible && h.app.state.sidebar_visible);
|
|
h.press(Key::Backslash, cmd_alt());
|
|
assert!(!h.app.state.right_visible);
|
|
h.press(Key::Backslash, cmd_alt());
|
|
assert!(h.app.state.right_visible);
|
|
h.press(Key::Backslash, cmd());
|
|
assert!(!h.app.state.sidebar_visible);
|
|
}
|
|
|
|
#[test]
|
|
fn each_opener_selects_its_tab() {
|
|
let Some(mut h) = Harness::new("openers", &["note.md"]) else { panic!("setup") };
|
|
for (key, tab) in [
|
|
(Key::R, RightTab::Related),
|
|
(Key::K, RightTab::Backlinks),
|
|
(Key::L, RightTab::Lint),
|
|
(Key::M, RightTab::Comments),
|
|
(Key::A, RightTab::Assistant),
|
|
(Key::O, RightTab::Outline),
|
|
] {
|
|
h.press(key, cmd_alt());
|
|
assert_eq!(h.app.right_tab, tab, "{key:?}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn f6_cycles_focus_between_the_panels_and_shift_f6_goes_back() {
|
|
let Some(mut h) = Harness::new("cycle", &["note.md"]) else { panic!("setup") };
|
|
h.app.vim_nav.focus = NavFocus::Sidebar;
|
|
let mut seen = Vec::new();
|
|
for _ in 0..4 {
|
|
h.press(Key::F6, Modifiers::NONE);
|
|
seen.push(h.app.vim_nav.focus);
|
|
}
|
|
assert_eq!(seen, [NavFocus::Editor, NavFocus::Right, NavFocus::Sidebar, NavFocus::Editor]);
|
|
h.press(Key::F6, Modifiers::SHIFT);
|
|
assert_eq!(h.app.vim_nav.focus, NavFocus::Sidebar);
|
|
h.press(Key::R, Modifiers { command: true, shift: true, ..Modifiers::NONE });
|
|
assert_eq!(h.app.vim_nav.focus, NavFocus::Right);
|
|
assert_eq!(hotkeys::focused_region(&h.ctx), Some(NavFocus::Right));
|
|
}
|
|
|
|
#[test]
|
|
fn escape_from_a_panel_returns_to_the_editor() {
|
|
let Some(mut h) = Harness::new("escape", &["note.md"]) else { panic!("setup") };
|
|
h.press(Key::O, cmd_alt());
|
|
assert_eq!(hotkeys::focused_region(&h.ctx), Some(NavFocus::Right));
|
|
h.press(Key::Escape, Modifiers::NONE);
|
|
assert_eq!(h.app.vim_nav.focus, NavFocus::Editor);
|
|
assert_eq!(hotkeys::focused_region(&h.ctx), Some(NavFocus::Editor));
|
|
}
|
|
|
|
#[test]
|
|
fn cmd_digits_pick_tabs_and_cmd_9_the_last() {
|
|
let Some(mut h) = Harness::new("goto", &["a.md", "b.md", "c.md", "d.md"]) else { panic!("setup") };
|
|
h.app.open_note("a.md");
|
|
h.press(Key::Num3, cmd());
|
|
assert_eq!(h.app.tabs.active, 2);
|
|
h.press(Key::Num9, cmd());
|
|
assert_eq!(h.app.tabs.active, 3);
|
|
h.press(Key::Num8, cmd());
|
|
assert_eq!(h.app.tabs.active, 3, "tab 8 does not exist: ignored");
|
|
h.press(Key::Num1, cmd());
|
|
assert_eq!(h.app.tabs.active, 0);
|
|
}
|
|
|
|
#[test]
|
|
fn next_and_previous_tab_wrap_and_reopen_brings_a_closed_tab_back() {
|
|
let Some(mut h) = Harness::new("step", &["a.md", "b.md", "c.md"]) else { panic!("setup") };
|
|
h.app.open_note("c.md");
|
|
h.press(Key::ArrowRight, cmd_alt());
|
|
assert_eq!(h.app.tabs.active, 0, "next wraps to the first tab");
|
|
h.press(Key::ArrowLeft, cmd_alt());
|
|
assert_eq!(h.app.tabs.active, 2, "previous wraps to the last tab");
|
|
h.press(Key::Tab, Modifiers { ctrl: true, ..Modifiers::NONE });
|
|
assert_eq!(h.app.tabs.active, 0);
|
|
h.app.close_note("b.md");
|
|
h.frame(Vec::new());
|
|
assert!(!h.app.tabs.is_open("b.md"));
|
|
h.press(Key::T, Modifiers { command: true, shift: true, ..Modifiers::NONE });
|
|
assert!(h.app.tabs.is_open("b.md"), "Cmd+Shift+T reopens the closed tab");
|
|
}
|
|
|
|
#[test]
|
|
fn a_bare_letter_is_ignored_while_a_text_field_has_focus() {
|
|
let registry = Registry::from_hotkeys(&[Hotkey {
|
|
id: "test.letter",
|
|
arg: None,
|
|
default_binding: Binding::key(Key::J),
|
|
scope: Scope::Global,
|
|
owner: Owner::Dispatch,
|
|
vim: VimMode::Any,
|
|
}]);
|
|
let ctx = Context::default();
|
|
let mut text = String::new();
|
|
let field = egui::Id::new("field");
|
|
let mut run = |events: Vec<Event>, focus: bool| {
|
|
let mut fired = Vec::new();
|
|
let mut out = ctx.run_ui(RawInput { events, ..RawInput::default() }, |ui| {
|
|
let response = ui.add(egui::TextEdit::singleline(&mut text).id(field));
|
|
if focus {
|
|
response.request_focus();
|
|
}
|
|
fired = hotkeys::dispatch(ui.ctx(), ®istry);
|
|
});
|
|
out.textures_delta.clear();
|
|
fired.len()
|
|
};
|
|
let press = || vec![Event::Key { key: Key::J, physical_key: None, pressed: true, repeat: false, modifiers: Modifiers::NONE }];
|
|
assert_eq!(run(press(), false), 1, "free keyboard: the letter fires");
|
|
run(Vec::new(), true);
|
|
run(Vec::new(), true);
|
|
assert!(ctx.egui_wants_keyboard_input(), "the field holds the keyboard");
|
|
assert_eq!(run(press(), true), 0, "the field keeps the letter");
|
|
}
|
|
|
|
#[test]
|
|
fn the_outline_tab_tooltip_shows_the_formatted_binding() {
|
|
let chord = Binding::key(Key::O).cmd().alt().format(hotkeys::Platform::current());
|
|
assert!(hotkeys::tooltip("Outline", "ui.show_outline").contains(&chord));
|
|
|
|
let Some(mut h) = Harness::new("tooltip", &["note.md"]) else { panic!("setup") };
|
|
h.app.right_tab = RightTab::Outline;
|
|
let texts = h.frame(Vec::new());
|
|
let tab = texts.iter().filter(|(text, _)| text == "Outline").map(|(_, rect)| *rect).min_by(|a, b| a.top().total_cmp(&b.top()));
|
|
let Some(label) = tab else { panic!("no Outline tab label in {texts:?}") };
|
|
// The pill's icon sits left of its label; the label may spill past a narrow pill.
|
|
let tab = Rect::from_center_size(Pos2::new(label.left() - 14.0, label.center().y), vec2(1.0, 1.0));
|
|
let mut shown = Vec::new();
|
|
for step in 0..90 {
|
|
let events = if step == 0 { vec![Event::PointerMoved(tab.center())] } else { Vec::new() };
|
|
shown = h.frame(events);
|
|
}
|
|
assert!(shown.iter().any(|(text, _)| text.contains(&chord)), "tooltip with {chord} in {shown:?}");
|
|
}
|