From c3f000fe161ab495e3e0123a1c79cfc438c1fe72 Mon Sep 17 00:00:00 2001 From: clover caruso Date: Fri, 16 Oct 2026 14:09:29 -0700 Subject: [PATCH] feat: draw split panes The page area divides as a tree, each part one pane or two side by side or one above the other. Each pane has a page box of its own, named by its id where the one page box was. While split, each pane is a group named for its section and page, under a header with them and a close button. Dividers are the frame's gap between papers: dragging one moves it, a double click evens it. A press on a pane, or F6 reaching it, focuses it before it acts, and the toolbar, tabs, page list and title follow; the wheel scrolls the pane under the pointer. Every pane's page is painted and rounded, and the open page tab joins the focused pane only beside the list. Only replays reach it, through the steps split right, split down and close pane; closing back to one pane draws the window as it was. Assisted-by: claude-opus-5.5 --- crates/snowbound/src/conflict_render.rs | 4 +- crates/snowbound/src/link.rs | 4 +- crates/snowbound/src/live.rs | 6 +- crates/snowbound/src/main.rs | 355 ++++++++++++------- crates/snowbound/src/manage.rs | 2 +- crates/snowbound/src/protection.rs | 2 +- crates/snowbound/src/recording.rs | 17 +- crates/snowbound/src/rename.rs | 2 +- crates/snowbound/src/search.rs | 6 +- crates/snowbound/src/server.rs | 2 +- crates/snowbound/src/share.rs | 4 +- crates/snowbound/src/split.rs | 432 +++++++++++++++++++++++- crates/snowbound/src/tags.rs | 2 +- crates/snowbound/src/task_pane.rs | 8 +- crates/snowbound/src/templates.rs | 12 +- crates/snowbound/src/web.rs | 4 +- crates/snowbound/tests/replay.rs | 262 ++++++++++++++ 17 files changed, 967 insertions(+), 157 deletions(-) diff --git a/crates/snowbound/src/conflict_render.rs b/crates/snowbound/src/conflict_render.rs index a15abd31f2103b8b36d7e3c67d7f3692621afd5e..2d6a5cffc19129ccc548a871d4ca46991223173b 100644 --- a/crates/snowbound/src/conflict_render.rs +++ b/crates/snowbound/src/conflict_render.rs @@ -41,7 +41,7 @@ fn layout(ui: &mut Ui, session: &Session, showing: &Showing, menu: bool) -> Vec< .into_iter() .collect(); ui.open_as( - page(), + Id::ROOT.child("page"), Spec { flags: Flags::CUSTOM, size: [fill(), fill()], @@ -87,7 +87,7 @@ fn paint( queue: &wgpu::Queue, ) -> Vec { let size = SIZE.map(|side| (side * SCALE) as u32); - let corner = ui.laid_out(super::page()).unwrap(); + let corner = ui.laid_out(Id::ROOT.child("page")).unwrap(); let box_size = [ ((corner[2] - corner[0]) * SCALE) as u32, ((corner[3] - corner[1]) * SCALE) as u32, diff --git a/crates/snowbound/src/link.rs b/crates/snowbound/src/link.rs index 6c4dc59e3eacfab81b2c309af3909fda5ec302dd..623de9a142ca1f804b42ff6198fc27c6fd0a42b9 100644 --- a/crates/snowbound/src/link.rs +++ b/crates/snowbound/src/link.rs @@ -2,7 +2,7 @@ //! the page's context menu, and following a clicked link, to a page //! of the notebook or with the system's handler. -use crate::{Command, State, art, commands, page, platform}; +use crate::{Command, State, art, commands, platform}; use canvas::interaction::Context; use onestore::ExGuid; use onestore::page::link::{LinkTarget, internal_link}; @@ -245,7 +245,7 @@ impl State { return Ok(()); } self.ui.close_popup(id()); - self.ui.set_focus(Some(page())); + self.ui.set_focus(Some(self.pane.id)); self.link = None; Ok(()) } diff --git a/crates/snowbound/src/live.rs b/crates/snowbound/src/live.rs index 670d8aae1543232ef0662afa954a117402d62581..624c954d0e3dc93eafb3dd12fdfb41c71d999917 100644 --- a/crates/snowbound/src/live.rs +++ b/crates/snowbound/src/live.rs @@ -6,7 +6,7 @@ //! this computer to whoever types its code (`Host`), or opens one another computer shares //! (`Joined`), as `share.rs`'s dialogs start them. -use crate::{Command, Library, Session, State, TAB_ROW, page, platform, settings}; +use crate::{Command, Library, Session, State, TAB_ROW, platform, settings}; use canvas::{document::TextPosition, editor::TextOutline}; use notebook::live::{ self, Caret, Hello, Peer, Reach, Room, Spot, @@ -920,7 +920,7 @@ impl State { let Some(session) = &self.session else { return; }; - let Some([left, top, right, bottom]) = self.ui.laid_out(page()) else { + let Some([left, top, right, bottom]) = self.ui.laid_out(self.pane.id) else { return; }; let here = (section(session), Some(self.pane.showing.space.into())); @@ -1044,7 +1044,7 @@ impl State { [x0, y0, y1]: [f32; 3], selection: &[[f32; 4]], ) { - let Some([left, top, ..]) = self.ui.laid_out(page()) else { + let Some([left, top, ..]) = self.ui.laid_out(self.pane.id) else { return; }; let id = peer.hello.peer; diff --git a/crates/snowbound/src/main.rs b/crates/snowbound/src/main.rs index 7d111c284c6a98a3713734ce6fe5142382042fa3..f4b1e76a06fa8a1a1d6639fcc3e3ad42cbb5b1b8 100644 --- a/crates/snowbound/src/main.rs +++ b/crates/snowbound/src/main.rs @@ -426,6 +426,10 @@ enum Replay { Resize([f32; 2]), /// Draws with this backend from then on, as Options' Renderer does. Renderer(settings::Backend), + /// Shows the focused page in a new pane after the focused one along the axis. + Split(Axis), + /// Closes the focused pane. + ClosePane, Quit, } @@ -542,6 +546,9 @@ fn icloud_listed(proxy: EventLoopProxy) -> impl Fn() + Send + Sync + /// A page view, and which page, locked section or empty notebook it shows. struct Pane { + /// The pane's page box, whose accessibility node holds the page's tree while the pane + /// has the focus. + id: Id, view: PageView, showing: Showing, /// The newest read requested; older ones are dropped when they finish. @@ -566,7 +573,7 @@ struct Pane { } /// Which page of the open section a pane shows, and what the page list shows beneath it. -#[derive(Default)] +#[derive(Clone, Copy, Default)] struct Showing { space: ExGuid, /// The version of page `space` open, by its context. @@ -761,6 +768,15 @@ struct Opening { const HOLD: std::time::Duration = std::time::Duration::from_millis(200); impl Pane { + /// The paper the pane's page lies on in `theme`, in the page's colour. + fn paper(&self, theme: &Theme) -> canvas::gpu::Paper { + canvas::gpu::Paper { + color: theme.paper, + ink: theme.paper_ink, + } + .colored(self.view.editor.page_color()) + } + /// The page opening here, once the pictures it shows first are drawn on `paper` or /// `HOLD` has passed. fn opened(&mut self, paper: canvas::gpu::Paper, redraw: &std::task::Waker) -> Option { @@ -859,6 +875,10 @@ enum Command { Page(Request), /// Runs a command, or one of a list's entries, after the frame's input. Choose(commands::Choice), + /// Shows the focused pane's page in a new pane after it along the axis. + Split(Axis), + /// Closes the pane of this page box. + ClosePane(Id), } struct State { @@ -899,6 +919,8 @@ struct State { /// The focused pane, which the chrome around the page area follows. pane: Pane, parked: Vec, + /// How the panes divide the page area. + split: split::Split, /// The open notebooks in the sidebar's order. notebooks: Vec>, /// The section the focused pane shows. @@ -1070,12 +1092,6 @@ fn tab_row() -> Id { Id::ROOT.child("tab row") } -/// The page's box, only ever built as the graft of the page's accessibility tree, so the -/// tree is sent while it is laid out. -fn page() -> Id { - Id::ROOT.child("page") -} - /// The section's colour around the page and its tabs. fn frame() -> Id { Id::ROOT.child("frame") @@ -1351,7 +1367,8 @@ impl State { // A window shown but never focused hears no focus event; a hidden one draws as focused. ui.window_focused = !visible || window.has_focus(); ui.fall_back_to(&fallbacks); - ui.set_focus(Some(page())); + let page = Id::ROOT.child("page"); + ui.set_focus(Some(page)); for family in FONTS { for (face, _) in engine.substitute(family).map_or(&[][..], |s| &s.faces) { ui.preview_font(face.clone(), family); @@ -1394,6 +1411,7 @@ impl State { unavailable, ui, pane: Pane { + id: page, view: PageView::new( editor, engine, @@ -1414,6 +1432,7 @@ impl State { locked, }, parked: Vec::new(), + split: split::Split::Leaf(page), notebooks, session, shelved: Vec::new(), @@ -1622,9 +1641,15 @@ impl State { platform::resize_direction(&self.window, self.pointer) .map(CursorIcon::from) .or_else(|| self.ui.cursor()) - .unwrap_or_else(|| cursor_icon(self.pane.view.cursor())), + .unwrap_or_else(|| { + let hovered = self.ui.box_at(self.pointer); + let pane = (self.parked.iter().map(|parked| &parked.pane)) + .find(|pane| Some(pane.id) == hovered) + .unwrap_or(&self.pane); + cursor_icon(pane.view.cursor()) + }), ); - let ime = if self.ui.focused() == Some(page()) { + let ime = if self.ui.focused() == Some(self.pane.id) { self.pane.view.accepts_text() } else { search::takes_text(self.ui.focused()) @@ -1711,16 +1736,26 @@ impl State { |_| [0.0; 4], ); } - if let Some(rect) = self.ui.laid_out(page()) { - let size = [ + let sized = |rect: [f32; 4]| { + [ ((rect[2] - rect[0]) * scale).round() as u32, ((rect[3] - rect[1]) * scale).round() as u32, - ]; + ] + }; + if let Some(rect) = self.ui.laid_out(self.pane.id) { + let size = sized(rect); if size != self.pane.view.viewport.size { let response = self.pane.view.resized(size)?; self.respond(response); } } + for Pane { id, view, .. } in self.parked.iter_mut().map(|parked| &mut parked.pane) { + if let Some(size) = self.ui.laid_out(*id).map(sized) + && size != view.viewport.size + { + let _ = view.resized(size)?; + } + } Ok(()) } @@ -1746,6 +1781,7 @@ impl State { /// Builds the frame's boxes and returns the section's colours as they ease, the open /// section tab and the open page's tab, for the edges drawn once they are laid out. fn build(&mut self) -> Result<(ui::Section, Id, Option), Box> { + self.follow_focus()?; let target = self.ui.theme.section(section_color( self.session .as_ref() @@ -2040,6 +2076,43 @@ impl State { ..Spec::default() }, ); + let shown = self.panes(&theme)?; + self.ui.close(); + if !shown { + self.ui.close(); + self.ui.close(); + if self.navigation_bar_right { + self.sidebar(&theme, sidebar); + } + self.sidebar_button(height, sidebar); + self.ui.close(); + self.context_menu(); + return Ok((section, open_tab, None)); + } + if !self.page_tabs_left { + open_page = self.page_list(&theme, §ion, row); + } + self.ui.close(); + self.ui.close(); + if self.navigation_bar_right { + self.sidebar(&theme, sidebar); + } + self.task_pane(&theme); + self.sidebar_button(height, sidebar); + self.ui.close(); + self.context_menu(); + // No tab stands on the frame in Full Page View. + let open_tab = if self.full_page { + frame().child("no tab") + } else { + open_tab + }; + Ok((section, open_tab, open_page)) + } + + /// Builds the focused pane in the box open: its page, under the bars it shows above it, + /// or what it shows in place of one; whether that is a page. + fn pane_content(&mut self, theme: &Theme) -> Result> { if let Some(session) = &self.session && let Some(bar) = session.bar(&self.pane.showing) { @@ -2060,16 +2133,7 @@ impl State { } if self.session.is_none() && self.pane.locked.is_some() { self.locked_page(); - self.ui.close(); - self.ui.close(); - self.ui.close(); - if self.navigation_bar_right { - self.sidebar(&theme, sidebar); - } - self.sidebar_button(height, sidebar); - self.ui.close(); - self.context_menu(); - return Ok((section, open_tab, None)); + return Ok(false); } if let Some(library) = self .pane @@ -2078,19 +2142,10 @@ impl State { .filter(|_| self.session.is_none()) { self.no_sections(library); - self.ui.close(); - self.ui.close(); - self.ui.close(); - if self.navigation_bar_right { - self.sidebar(&theme, sidebar); - } - self.sidebar_button(height, sidebar); - self.ui.close(); - self.context_menu(); - return Ok((section, open_tab, None)); + return Ok(false); } self.ui.open_as( - page(), + self.pane.id, Spec { flags: Flags::CUSTOM | Flags::FOCUSABLE, size: [fill(), fill()], @@ -2099,15 +2154,15 @@ impl State { ..Spec::default() }, ); - if let Some(node) = self.ui.access(page()) { + if let Some(node) = self.ui.access(self.pane.id) { node.set_tree_id(PAGE_TREE); } // A page opening has neither the templates nor the scroll of the page leaving. let opening = self.loading().is_some(); if !opening { - self.template_strip(&theme); + self.template_strip(theme); } - self.transport(&theme)?; + self.transport(theme)?; let scroll = self.pane.view.scroll(); for (index, axis) in [Axis::X, Axis::Y] .into_iter() @@ -2143,7 +2198,7 @@ impl State { ..Spec::default() }, ); - page().child("task") + self.pane.id.child("task") }); self.ui.close(); if let Some(task) = task { @@ -2154,34 +2209,15 @@ impl State { Some("Edit it in OneNote with Outlook"), ); } - let signal = self.ui.signal(page()); - let focused = self.ui.window_focused && self.ui.focused() == Some(page()); + let signal = self.ui.signal(self.pane.id); + let focused = self.ui.window_focused && self.ui.focused() == Some(self.pane.id); if focused != self.pane.page_focused { self.pane.page_focused = focused; let response = self.pane.view.focus_changed(focused)?; self.respond(response); } self.page_events(signal.events)?; - self.ui.close(); - if !self.page_tabs_left { - open_page = self.page_list(&theme, §ion, row); - } - self.ui.close(); - self.ui.close(); - if self.navigation_bar_right { - self.sidebar(&theme, sidebar); - } - self.task_pane(&theme); - self.sidebar_button(height, sidebar); - self.ui.close(); - self.context_menu(); - // No tab stands on the frame in Full Page View. - let open_tab = if self.full_page { - frame().child("no tab") - } else { - open_tab - }; - Ok((section, open_tab, open_page)) + Ok(true) } /// The page's corner radius, sharing its centres with the window's corners. @@ -2193,16 +2229,44 @@ impl State { /// together with the open page's tab, joined where they meet. fn edges(&mut self, section: ui::Section, open_tab: Id, open_page: Option) { let panel = self.ui.laid_out(frame().child("panel")); - let (Some(frame), Some(page)) = (self.ui.laid_out(frame()), self.ui.laid_out(page())) - else { + let column = self.ui.laid_out(frame().child("column")); + let Some(frame) = self.ui.laid_out(frame()) else { return; }; let rounding = self.rounding(); + let height = (frame[3] - frame[1]).max(1.0); + let gradient = |y| ui::mix(section.frame[0], section.frame[1], (y - frame[1]) / height); + let parked: Vec<_> = (self.parked.iter()) + .filter_map(|parked| { + let paper = parked.pane.paper(&self.ui.theme).color; + Some((self.ui.laid_out(parked.pane.id)?, paper)) + }) + .collect(); + for ([left, top, right, bottom], paper) in parked { + let outline = [ + ([left, top], rounding), + ([right, top], rounding), + ([right, bottom], rounding), + ([left, bottom], rounding), + ]; + self.ui.round_corners(&outline, paper, gradient); + self.ui.border(&outline, true, section.edge); + } + let Some(page) = self.ui.laid_out(self.pane.id) else { + return; + }; + // The open page's tab joins the focused pane only beside the page list. + let column = column.unwrap_or(page); // Where the open page tab meets the page, concentric with its neighbours. let join = PILL_MARGIN + rounding; let [left, top, right, bottom] = page; let left_tabs = self.page_tabs_left; + let beside = match left_tabs { + true => left - column[0], + false => column[2] - right, + } < 0.5; let tab = open_page + .filter(|_| beside) .and_then(|id| self.ui.laid_out(id)) .map(|row| [row[0], row[1], row[2], row[3] - ROW_GAP]) .zip(panel) @@ -2265,11 +2329,8 @@ impl State { ([left, bottom], rounding), ]), } - let height = (frame[3] - frame[1]).max(1.0); let paper = self.paper().color; - self.ui.round_corners(&outline, paper, |y| { - ui::mix(section.frame[0], section.frame[1], (y - frame[1]) / height) - }); + self.ui.round_corners(&outline, paper, gradient); self.ui.border(&outline, true, section.edge); // The frame's top corners share the page's centres, and beside the sidebar so does // its bottom one, rounding in as the sidebar opens; the frame's top edge breaks where @@ -3312,7 +3373,7 @@ impl State { let percent = typed.trim().trim_end_matches('%').trim().parse::(); zoom = percent.ok().filter(|_| key == NamedKey::Enter); self.zoom_typed = None; - self.ui.set_focus(Some(page())); + self.ui.set_focus(Some(self.pane.id)); } } else { let id = self.ui.id("level"); @@ -3522,7 +3583,7 @@ impl State { .as_ref() .is_some_and(|session| session.read_only(&self.pane.showing)); let scale = self.ui.scale(); - let corner = self.ui.laid_out(page()).unwrap_or_default(); + let corner = self.ui.laid_out(self.pane.id).unwrap_or_default(); let device = |point: [f32; 2]| { [ (point[0] - corner[0]) * scale, @@ -3826,6 +3887,8 @@ impl State { Command::Page(Request::OpenAttachment(file)) => self.open_attachment(&file)?, Command::Page(Request::Play { file, at_ms }) => self.play(&file, at_ms)?, Command::Choose(choice) => self.run(choice)?, + Command::Split(axis) => self.split_pane(axis)?, + Command::ClosePane(id) => self.close_pane(id)?, Command::TagPicture => self.pick_tag_picture(), Command::Text(text) => { if let Some(context) = self.pane.view.caret_context() { @@ -4064,24 +4127,10 @@ impl State { .as_ref() .is_none_or(|open| open.library.location != session.library.location); self.list(&session.library); - // The section left stays open a while for coming back to; the notebook's - // background sync takes it over once it is closed. self.pane.locked = None; let previous = self.session.replace(*session); if let Some(previous) = previous.and_then(|previous| self.shelve(previous)) { - let path = &previous.tabs[previous.tab].path; - let locked = self.leave(&previous.library, path); - let library = &previous.library; - if !locked && self.notebooks.iter().any(|open| Arc::ptr_eq(open, library)) { - library.keep(&previous.tabs[previous.tab].path, previous.section); - } else { - let section = previous.section; - crate::spawn(move || { - if let Err(error) = section.close() { - eprintln!("Synchronization stopped: {error}"); - } - }); - } + self.release(previous); } self.pane.showing = Showing { space, @@ -4157,6 +4206,25 @@ impl State { Ok(()) } + /// Keeps `session`, a section no pane shows any more, open a while for coming back to, + /// or closes it where it locks as it is left; the notebook's background sync takes it + /// over once it is closed. + fn release(&mut self, session: Session) { + let path = &session.tabs[session.tab].path; + let locked = self.leave(&session.library, path); + let library = &session.library; + if !locked && self.notebooks.iter().any(|open| Arc::ptr_eq(open, library)) { + library.keep(path, session.section); + } else { + let section = session.section; + crate::spawn(move || { + if let Err(error) = section.close() { + eprintln!("Synchronization stopped: {error}"); + } + }); + } + } + /// Takes `library`, a notebook read again after a change, in place of the one it was; /// the open section, now at catalog `path` in it, keeps its replica where it moved. /// Without `path` the notebook has no sections left, and one of its shows none. @@ -4227,7 +4295,7 @@ impl State { } // A page a search result shows leaves the keys with the search. if !search::takes_text(self.ui.focused()) { - self.ui.set_focus(Some(page())); + self.ui.set_focus(Some(self.pane.id)); } self.title(); self.update_accessibility(true)?; @@ -4250,7 +4318,7 @@ impl State { /// Follows the view moving: the input method's position and accessibility. fn after_move(&mut self) -> Result<(), Box> { let scale = self.ui.scale(); - let corner = self.ui.laid_out(page()).unwrap_or_default(); + let corner = self.ui.laid_out(self.pane.id).unwrap_or_default(); let [x0, y0, x1, y1] = self.pane.view.caret_area()?; self.window.set_ime_cursor_area( LogicalPosition::new(x0 / scale + corner[0], y0 / scale + corner[1]), @@ -4397,7 +4465,7 @@ impl State { let at = |offset| canvas::document::TextPosition { paragraph, offset }; self.pane.view.editor.focus_outline(outline)?; self.pane.view.editor.select([at(0), at(length)].into())?; - self.ui.set_focus(Some(page())); + self.ui.set_focus(Some(self.pane.id)); let response = self.pane.view.focus_text()?; self.respond(response); self.window.request_redraw(); @@ -4588,13 +4656,13 @@ impl State { /// `page` asks or its tree is not grafted yet. The interface's tree goes first, since it /// holds the page's. fn update_accessibility(&mut self, page: bool) -> Result<(), Box> { - let built = self.ui.laid_out(self::page()).is_some(); + let built = self.ui.laid_out(self.pane.id).is_some(); if !built && self.page_grafted { // Its graft is gone, and with it the tree, which must be sent whole again. self.accessibility.deactivate(); self.page_grafted = false; } - let graft = self::page().node(); + let graft = self.pane.id.node(); let title = self.window.title(); let scale = self.window.scale_factor(); let grafted = self.page_grafted; @@ -4613,10 +4681,10 @@ impl State { } let mut error = None; let mut sent = false; - let (view, ui, window) = (&self.pane.view, &self.ui, &self.window); + let (pane, ui, window) = (&self.pane, &self.ui, &self.window); self.access_adapter.update_if_active(|| { sent = true; - page_tree(view, ui, window, &mut self.accessibility).unwrap_or_else(|failure| { + page_tree(pane, ui, window, &mut self.accessibility).unwrap_or_else(|failure| { error = Some(failure); self.accessibility.deactivate(); let mut root = accesskit::Node::new(accesskit::Role::Group); @@ -4686,18 +4754,18 @@ impl State { write(&child, depth + 1, out); } } - let graft = page().node(); + let graft = self.pane.id.node(); let mut chrome = self .ui .accessibility_tree(&self.window.title(), self.window.scale_factor()); let focus = std::mem::replace(&mut chrome.focus, ui::Id::ROOT.node()); let mut tree = Tree::new(chrome, true); - if self.ui.laid_out(page()).is_some() { + if self.ui.laid_out(self.pane.id).is_some() { let mut fresh = accessibility::Accessibility::default(); - let page = page_tree(&self.pane.view, &self.ui, &self.window, &mut fresh)?; + let page = page_tree(&self.pane, &self.ui, &self.window, &mut fresh)?; tree.update_and_process_changes(page, &mut Unwatched); } - if focus != graft || self.ui.laid_out(page()).is_some() { + if focus != graft || self.ui.laid_out(self.pane.id).is_some() { let update = accesskit::TreeUpdate { nodes: Vec::new(), tree: None, @@ -4739,7 +4807,7 @@ impl State { if let Some(index) = self.accessibility.read_only_for_node(request.target_node) { if request.action == Action::Focus { self.window.focus_window(); - self.ui.set_focus(Some(page())); + self.ui.set_focus(Some(self.pane.id)); let response = self.pane.view.focus_read_only(index)?; self.respond(response); self.window.request_redraw(); @@ -4772,7 +4840,7 @@ impl State { } _ => return Ok(()), } - self.ui.set_focus(Some(page())); + self.ui.set_focus(Some(self.pane.id)); let response = self.pane.view.focus_text()?; self.respond(response); self.window.request_redraw(); @@ -4791,6 +4859,12 @@ impl State { paper, ); } + for view in self.parked.iter_mut().map(|parked| &mut parked.pane.view) { + let paper = paper.colored(view.editor.page_color()); + if let Some((scene, _)) = &mut view.scene { + scene.settle(Some(&view.editor), view.viewport.scale, paper); + } + } self.snapshot(&path)?; } if let Some((dir, opened)) = &self.frames { @@ -4870,7 +4944,7 @@ impl State { } let theme = &self.ui.theme; let scale = self.ui.scale(); - let corner = self.ui.laid_out(page()).unwrap_or_default(); + let corner = self.ui.laid_out(self.pane.id).unwrap_or_default(); // A page taking a while to open shows its outline, drawn in points from the corner. let (page_primitives, viewport) = match self.loading() { Some(since) => ( @@ -4894,24 +4968,51 @@ impl State { self.pane.view.viewport, ), }; + let mut pages = vec![(self.pane.id, page_primitives, viewport, corner, paper)]; + // Pictures first, as the primitives borrow the views. + for pane in self.parked.iter_mut().map(|parked| &mut parked.pane) { + let paper = pane.paper(theme); + pane.view.update_pictures(paper, &self.redraw); + } + for parked in &self.parked { + let view = &parked.pane.view; + let paper = parked.pane.paper(theme); + let colors = TextColors { + caret: theme.caret, + selection: theme.inactive_selection, + paper, + }; + let corner = self.ui.laid_out(parked.pane.id).unwrap_or_default(); + pages.push(( + parked.pane.id, + view.primitives(colors)?, + view.viewport, + corner, + paper, + )); + } lap("page primitives", start); let interface = self.ui.layers(); let layers: Vec<_> = interface .iter() - .map(|layer| match layer { - ui::Layer::Primitives(primitives) => primitives.layer(scale), - ui::Layer::Custom { rect, .. } => draw::Layer { - scale: viewport.scale, - origin: [ - viewport.origin[0] + corner[0] * scale, - viewport.origin[1] + corner[1] * scale, - ], - clip: Some(rect.map(|value| value * scale)), - backdrop: Some(paper.color), - round: None, - motion: None, - primitives: &page_primitives, - }, + .filter_map(|layer| match layer { + ui::Layer::Primitives(primitives) => Some(primitives.layer(scale)), + ui::Layer::Custom { id, rect } => { + let (_, primitives, viewport, corner, paper) = + pages.iter().find(|(page, ..)| page == id)?; + Some(draw::Layer { + scale: viewport.scale, + origin: [ + viewport.origin[0] + corner[0] * scale, + viewport.origin[1] + corner[1] * scale, + ], + clip: Some(rect.map(|value| value * scale)), + backdrop: Some(paper.color), + round: None, + motion: None, + primitives, + }) + } }) .collect(); trace_input(&("Draw", viewport.origin, viewport.scale)); @@ -4943,14 +5044,14 @@ impl State { } fn over_page(&self) -> bool { - let [x0, y0, x1, y1] = self.ui.laid_out(page()).unwrap_or_default(); + let [x0, y0, x1, y1] = self.ui.laid_out(self.pane.id).unwrap_or_default(); let [x, y] = self.pointer; (x0..x1).contains(&x) && (y0..y1).contains(&y) } /// Window point `point` in the page's device pixels. fn page_point(&self, point: [f32; 2]) -> [f32; 2] { - let [left, top, ..] = self.ui.laid_out(page()).unwrap_or_default(); + let [left, top, ..] = self.ui.laid_out(self.pane.id).unwrap_or_default(); let scale = self.ui.scale(); [(point[0] - left) * scale, (point[1] - top) * scale] } @@ -5001,6 +5102,13 @@ impl State { self.window.request_redraw(); return; } + // A press of either button on a parked pane's page focuses it, then acts there. + if let ui::Event::Button { pressed: true, .. } = event + && let Some(hovered) = self.ui.box_at(self.pointer) + && self.parked.iter().any(|parked| parked.pane.id == hovered) + { + self.ui.set_focus(Some(hovered)); + } if let ui::Event::Button { pressed: true, .. } = event && self.renaming.is_some() && self.ui.box_at(self.pointer) != Some(rename::field()) @@ -5074,16 +5182,17 @@ fn theme(appearance: winit::window::Theme, light_pages: bool, backdrop: bool) -> } } -/// The page's accessibility tree, as changed since `access` last sent it, for the graft at -/// the page's box: bounds in the window's pixels, which the interface's scale renders fewer -/// of when capped. +/// `pane`'s page accessibility tree, as changed since `access` last sent it, for the graft at +/// its page box: bounds in the window's pixels, which the interface's scale renders fewer of +/// when capped. fn page_tree( - view: &PageView, + pane: &Pane, ui: &Ui, window: &Window, access: &mut accessibility::Accessibility, ) -> Result { - let corner = ui.laid_out(page()).unwrap_or_default(); + let view = &pane.view; + let corner = ui.laid_out(pane.id).unwrap_or_default(); let scale = ui.scale(); let ratio = window.scale_factor() as f32 / scale; let viewport = canvas::gpu::Viewport { @@ -6479,7 +6588,7 @@ impl State { self.window.request_redraw(); } UserEvent::InsertText(text) => { - if self.ui.focused() == Some(page()) { + if self.ui.focused() == Some(self.pane.id) { match self.pane.view.insert_text(text) { Ok(response) => self.respond(response), Err(error) => eprintln!("{error}"), @@ -6589,6 +6698,8 @@ impl State { .request_inner_size(LogicalSize::new(width, height)); } Replay::Renderer(_) => unreachable!("The app switches renderers"), + Replay::Split(axis) => self.commands.push(Command::Split(axis)), + Replay::ClosePane => self.commands.push(Command::ClosePane(self.pane.id)), } // A covered window gets no redraws, so each step draws its own frame. if let Err(error) = self.frame() { @@ -7102,6 +7213,12 @@ fn replay(script: String, proxy: EventLoopProxy) -> Result<(), Box Ok(Replay::Renderer(settings::Backend::named(rest)?)), + "split" => Ok(Replay::Split(match rest { + "right" => Axis::X, + "down" => Axis::Y, + _ => return Err(format!("Unknown split {rest}").into()), + })), + "close" if rest == "pane" => Ok(Replay::ClosePane), "appearance" => Ok(Replay::Appearance(match rest { "light" => winit::window::Theme::Light, "dark" => winit::window::Theme::Dark, diff --git a/crates/snowbound/src/manage.rs b/crates/snowbound/src/manage.rs index b3b5fff55da94c338f61f2d24e4f5c994118557d..93bb8d1c3c471c01d679136318ec7f34a6cd9d0e 100644 --- a/crates/snowbound/src/manage.rs +++ b/crates/snowbound/src/manage.rs @@ -250,7 +250,7 @@ impl State { since: now, requested: now, })?; - self.ui.set_focus(Some(crate::page())); + self.ui.set_focus(Some(self.pane.id)); if let Some(title) = self .pane .view diff --git a/crates/snowbound/src/protection.rs b/crates/snowbound/src/protection.rs index 46ee0b0fd796e341d95fa14f6b9d38e3313d8fe8..b917d23c104ec689916f0c1f799cb631be642394 100644 --- a/crates/snowbound/src/protection.rs +++ b/crates/snowbound/src/protection.rs @@ -66,7 +66,7 @@ fn notice() -> Id { Id::ROOT.child("locked-notice") } -fn section_name(path: &str) -> &str { +pub(crate) fn section_name(path: &str) -> &str { let name = path.rsplit('/').next().unwrap_or(path); name.strip_suffix(".one").unwrap_or(name) } diff --git a/crates/snowbound/src/recording.rs b/crates/snowbound/src/recording.rs index 1482587e9b866eb08a891a14485b5b601883649d..a5b15598619734c95b414ae9b83668f08bb01bfe 100644 --- a/crates/snowbound/src/recording.rs +++ b/crates/snowbound/src/recording.rs @@ -57,8 +57,9 @@ pub enum Transport { SeePlayback, } -fn seek_field() -> ui::Id { - crate::page().child("transport").child("seek") +/// The Seek To field of the transport over page box `page`. +fn seek_field(page: ui::Id) -> ui::Id { + page.child("transport").child("seek") } impl State { @@ -263,8 +264,8 @@ impl State { } (Transport::SeekTo, Media::Playing(playback)) => { playback.seeking = Some(clock(playback.player.position_ms())); - self.ui.set_focus(Some(seek_field())); - self.ui.focus_all(seek_field()); + self.ui.set_focus(Some(seek_field(self.pane.id))); + self.ui.focus_all(seek_field(self.pane.id)); } _ => {} } @@ -482,7 +483,7 @@ impl State { Media::Playing(playback) => &mut playback.seeking, _ => &mut None, }; - let focused = self.ui.focused() == Some(seek_field()); + let focused = self.ui.focused() == Some(seek_field(self.pane.id)); let Some(text) = seeking.as_mut().filter(|_| focused) else { *seeking = None; let playing = matches!(self.media, Media::Playing(_)); @@ -519,12 +520,12 @@ impl State { }; let keys = ui::popup::navigation( &mut self.ui, - &[seek_field()], + &[seek_field(self.pane.id)], &[NamedKey::Enter, NamedKey::Escape], ); ui::text_field( &mut self.ui, - seek_field(), + seek_field(self.pane.id), text, "m:ss", Spec { @@ -542,7 +543,7 @@ impl State { .flatten(); if !keys.is_empty() { *seeking = None; - self.ui.set_focus(Some(crate::page())); + self.ui.set_focus(Some(self.pane.id)); } at } diff --git a/crates/snowbound/src/rename.rs b/crates/snowbound/src/rename.rs index df200c649c095c16255ddaed1f6e6db073a79965..45d4c08087140849d7c47e7c85d79f15101e3275 100644 --- a/crates/snowbound/src/rename.rs +++ b/crates/snowbound/src/rename.rs @@ -126,7 +126,7 @@ impl State { let Some(Renaming { target, name }) = self.renaming.take() else { return; }; - self.ui.set_focus(Some(crate::page())); + self.ui.set_focus(Some(self.pane.id)); let name = name.trim().to_owned(); if !keep || name.is_empty() { return; diff --git a/crates/snowbound/src/search.rs b/crates/snowbound/src/search.rs index 167ad40f325147b3ffa02a089896acf3172a3049..71d40c4db5ddc12ca08a34ac9a5098cd0be6dda7 100644 --- a/crates/snowbound/src/search.rs +++ b/crates/snowbound/src/search.rs @@ -3,7 +3,7 @@ //! its own, from the section files and the open section's replica. use crate::task_pane::TaskPane; -use crate::{Command, Library, State, Theme, art, commands, lap, name, page}; +use crate::{Command, Library, State, Theme, art, commands, lap, name}; use accesskit::Role; use canvas::search::{Entry, Found, Index, PageMatch, Query, page_matches, paragraph_match}; use notebook::Replica; @@ -685,7 +685,7 @@ impl State { self.search.finding = false; self.search.current = None; self.ui.close_popup(results()); - self.ui.set_focus(Some(page())); + self.ui.set_focus(Some(self.pane.id)); if !matches!(self.search.task_pane, Some(TaskPane::Search { .. })) { self.search.query.clear(); self.search.scope = self.search.default; @@ -1120,7 +1120,7 @@ impl State { self.open_result(index)?; self.search.open = false; self.ui.close_popup(results()); - self.ui.set_focus(Some(page())); + self.ui.set_focus(Some(self.pane.id)); } Ok(()) } diff --git a/crates/snowbound/src/server.rs b/crates/snowbound/src/server.rs index 93cefc4be64f8bc7b6d42f12c6f295992dd8de82..d748e2afe55ddb803dc53edb6a3582c90c8327cd 100644 --- a/crates/snowbound/src/server.rs +++ b/crates/snowbound/src/server.rs @@ -835,7 +835,7 @@ impl State { ui.close(); if cancel { self.ui.close_popup(id()); - self.ui.set_focus(Some(crate::page())); + self.ui.set_focus(Some(self.pane.id)); self.server = None; return; } diff --git a/crates/snowbound/src/share.rs b/crates/snowbound/src/share.rs index f212c8b1c120f678d695b4f921ba32c47ab5167a..5e8647d6ff5f2a45a72f767be29bd8f55cd84c19 100644 --- a/crates/snowbound/src/share.rs +++ b/crates/snowbound/src/share.rs @@ -590,7 +590,7 @@ impl State { } if done { self.ui.close_popup(share_id()); - self.ui.set_focus(Some(crate::page())); + self.ui.set_focus(Some(self.pane.id)); return; } self.peers.share = Some(dialog); @@ -661,7 +661,7 @@ impl State { ui.close(); if cancel { self.ui.close_popup(join_id()); - self.ui.set_focus(Some(crate::page())); + self.ui.set_focus(Some(self.pane.id)); return; } if open && !matches!(dialog.status, Status::Waiting) { diff --git a/crates/snowbound/src/split.rs b/crates/snowbound/src/split.rs index a652cba662c4afed60a72abd88a651810317c35b..b5f9f5ed194fb91c1f12a1abbaac632990a40540 100644 --- a/crates/snowbound/src/split.rs +++ b/crates/snowbound/src/split.rs @@ -3,13 +3,76 @@ //! are parked. Panes on one section share its `Session`, since a section's replica opens //! once (`locking_mode=EXCLUSIVE`). -use crate::{Command, Library, Loaded, Pane, Session, State}; +use crate::{Command, FRAME, Library, Loaded, Pane, Session, State, art, name, navigation}; +use canvas::{gpu::page::PageScene, interaction::PageView}; +use draw::edit::SelectionUnit; use notebook::{Replica, session::Event}; use onestore::ExGuid; use std::{ error::Error, sync::{Arc, atomic::Ordering}, }; +use ui::{Axis, Extent, Flags, Id, Overflow, Size, Spec, Theme, fill, px}; +use winit::window::CursorIcon; + +/// The least a pane gives way to another beside it as their divider moves. +const LEAST: f32 = 160.0; + +/// How the page area divides among the panes: all to one, or between two parts side by side +/// or one above the other, `ratio` of it going to the first. +pub enum Split { + Leaf(Id), + Pair { + axis: Axis, + ratio: f32, + a: Box, + b: Box, + }, +} + +impl Split { + /// The first pane in reading order. + fn first(&self) -> Id { + match self { + Split::Leaf(id) => *id, + Split::Pair { a, .. } => a.first(), + } + } + + /// Divides pane `at`'s room along `axis` with pane `new`, after it. + fn divide(&mut self, at: Id, axis: Axis, new: Id) -> bool { + match self { + Split::Leaf(id) if *id == at => { + *self = Split::Pair { + axis, + ratio: 0.5, + a: Box::new(Split::Leaf(at)), + b: Box::new(Split::Leaf(new)), + }; + true + } + Split::Leaf(_) => false, + Split::Pair { a, b, .. } => a.divide(at, axis, new) || b.divide(at, axis, new), + } + } + + /// Gives pane `gone`'s room to the part beside it, and names that part's first pane. + fn remove(&mut self, gone: Id) -> Option { + let Split::Pair { a, b, .. } = self else { + return None; + }; + let is = |part: &Split| matches!(part, Split::Leaf(id) if *id == gone); + let kept = if is(a) { + std::mem::replace(&mut **b, Split::Leaf(gone)) + } else if is(b) { + std::mem::replace(&mut **a, Split::Leaf(gone)) + } else { + return a.remove(gone).or_else(|| b.remove(gone)); + }; + *self = kept; + Some(self.first()) + } +} /// A pane out of focus, and the section it shows by `Library::key`; the focused pane's is /// `State::session`. @@ -91,9 +154,16 @@ impl State { }); } - /// Closes the parked panes `gone` picks. + /// Closes the parked panes `gone` picks, their room going to the parts beside them. fn close_parked(&mut self, gone: impl Fn(&Parked) -> bool) { - self.parked.retain(|parked| !gone(parked)); + let ids: Vec = (self.parked.iter()) + .filter(|parked| gone(parked)) + .map(|parked| parked.pane.id) + .collect(); + for id in &ids { + self.split.remove(*id); + } + self.parked.retain(|parked| !ids.contains(&parked.pane.id)); } /// Moves the sections only parked panes show in `library`, a notebook read again, to @@ -370,6 +440,362 @@ impl State { self.commands.push(Command::OpenPage(open)); Ok(true) } + + /// Gives the focus to the parked pane whose page box the keyboard moved to, as a press on + /// it or F6 moves it. + pub(crate) fn follow_focus(&mut self) -> Result<(), Box> { + let focused = self.ui.focused(); + match (self.parked.iter()).position(|parked| Some(parked.pane.id) == focused) { + Some(at) => self.focus_pane(at), + None => Ok(()), + } + } + + /// Moves the focus to parked pane `at`. The pane leaving it stores what was typed in it, + /// and stops recording. + fn focus_pane(&mut self, at: usize) -> Result<(), Box> { + self.stop_recording(true)?; + self.pane.view.editor.finish_composition(); + crate::platform::clear_marked_text(&self.window); + self.persist()?; + self.pane.page_focused = false; + let response = self.pane.view.focus_changed(false)?; + self.respond(response); + self.swap_pane(at); + // The page's tree moves to the pane focused, sent whole. + self.accessibility.deactivate(); + self.page_grafted = false; + self.title(); + self.window.request_redraw(); + Ok(()) + } + + /// Shows the focused pane's page in a new pane after it along `axis`, which takes the + /// focus. + pub(crate) fn split_pane(&mut self, axis: Axis) -> Result<(), Box> { + let Some(session) = &self.session else { + return Ok(()); + }; + let taken = + |id: &Id| *id == self.pane.id || self.parked.iter().any(|parked| parked.pane.id == *id); + let id = (1..) + .map(|count: u32| Id::ROOT.child(("page", count))) + .find(|id| !taken(id)) + .ok_or("No pane is left to make")?; + let (showing, section) = (self.pane.showing, Some(session.key())); + let page = match showing.version { + Some(version) => session.version_reader(showing.space, version)()?, + None => session.reader(showing.space)()?, + }; + let shown = &self.pane.view; + let mut engine = shown.engine.clone(); + let (scene, editor) = PageScene::from_page(page, &mut engine)?; + let size = shown.viewport.size; + let double_click = crate::platform::double_click_interval(); + let scene = Some((scene, [0.0; 2])); + let scale = self.ui.scale(); + let mut view = PageView::new(editor, engine, scene, size, scale, double_click); + view.viewport = shown.viewport; + view.spelling.clone_from(&shown.spelling); + view.snap_to_grid = shown.snap_to_grid; + view.editor.styles = shown.editor.styles.clone(); + view.tag_art = shown.tag_art.clone(); + let pane = Pane { + id, + view, + showing, + loading: 0, + opening: None, + switching: None, + trail: navigation::Trail::default(), + stale: false, + page_focused: false, + sectionless: None, + locked: None, + refused: Arc::default(), + }; + self.parked.push(Parked { pane, section }); + self.split.divide(self.pane.id, axis, id); + self.focus_pane(self.parked.len() - 1)?; + self.visited(); + self.ui.set_focus(Some(self.pane.id)); + Ok(()) + } + + /// Closes pane `id`, its room going to the part beside it; the first pane there takes the + /// focus where the pane closed had it. The only pane stays. + pub(crate) fn close_pane(&mut self, id: Id) -> Result<(), Box> { + let Some(next) = self.split.remove(id) else { + return Ok(()); + }; + if id == self.pane.id { + let at = (self.parked.iter()) + .position(|parked| parked.pane.id == next) + .ok_or("No pane takes the focus")?; + self.focus_pane(at)?; + self.ui.set_focus(Some(self.pane.id)); + } + let at = (self.parked.iter()) + .position(|parked| parked.pane.id == id) + .ok_or("No such pane")?; + let Parked { pane, section } = self.parked.swap_remove(at); + let Some(key) = section else { + return Ok(()); + }; + let space = pane.showing.space; + self.places.insert((key.clone(), space), pane.view.place()); + let focused = self.session.as_ref().filter(|session| session.key() == key); + // The focused pane's page stays the section's last. + if focused.is_none() { + self.last_pages.insert(key.clone(), space); + } + let session = focused.or(self.shelved.iter().find(|session| session.key() == key)); + // A page another pane still shows keeps that pane's history. + if session.is_some_and(|session| !session.read_only(&pane.showing)) + && !self.shown_parked(&key, space) + && (focused.is_none() || self.pane.showing.space != space) + { + self.undo.park(space, pane.view.editor); + } + if !(self.parked.iter()).any(|parked| parked.section.as_ref() == Some(&key)) + && let Some(session) = self.unshelve(&key) + { + self.release(session); + } + Ok(()) + } + + /// Builds the panes in the page area's box, as the split divides them; whether the + /// focused one shows a page. + pub(crate) fn panes(&mut self, theme: &Theme) -> Result> { + if self.parked.is_empty() { + return self.pane_content(theme); + } + let mut split = std::mem::replace(&mut self.split, Split::Leaf(self.pane.id)); + let shown = self.part(&mut split, "panes", [fill(), fill()], theme); + self.split = split; + shown + } + + /// Builds `split`, `size` big, as box `part`; whether the focused pane in it shows a page. + fn part( + &mut self, + split: &mut Split, + part: &str, + size: [Extent; 2], + theme: &Theme, + ) -> Result> { + let (axis, ratio, a, b) = match split { + Split::Leaf(id) => return self.leaf(*id, part, size, theme), + Split::Pair { axis, ratio, a, b } => (axis, ratio, a, b), + }; + let along = usize::from(*axis == Axis::Y); + let id = self.ui.open( + part, + Spec { + axis: *axis, + size, + ..Spec::default() + }, + ); + let share = |fraction: f32| { + let mut size = [fill(); 2]; + size[along] = Extent { + size: Size::Fraction(fraction), + strictness: 0.0, + }; + size + }; + let mut shown = self.part(a, "a", share(*ratio), theme)?; + let mut thin = [fill(); 2]; + thin[along] = px(FRAME); + let divider = self.ui.open( + "divider", + Spec { + flags: Flags::CLICKABLE, + size: thin, + cursor: Some(match axis { + Axis::X => CursorIcon::ColResize, + Axis::Y => CursorIcon::RowResize, + }), + ..Spec::default() + }, + ); + self.ui.close(); + shown |= self.part(b, "b", share(1.0 - *ratio), theme)?; + self.ui.close(); + let signal = self.ui.signal(divider); + if signal.pressed && signal.unit != SelectionUnit::Grapheme { + *ratio = 0.5; + } else if signal.dragging + && let (Some(pointer), Some(rect)) = (self.ui.pointer(), self.ui.laid_out(id)) + { + let length = rect[along + 2] - rect[along] - FRAME; + let least = (LEAST / length).min(0.5); + let at = (pointer[along] - rect[along] - FRAME / 2.0) / length; + *ratio = at.clamp(least, 1.0 - least); + } + Ok(shown) + } + + /// Builds pane `id`, `size` big, as box `part`: a group named for what it shows, under a + /// header with its close button; whether it is the focused pane showing a page. + fn leaf( + &mut self, + id: Id, + part: &str, + size: [Extent; 2], + theme: &Theme, + ) -> Result> { + let parked = (self.parked.iter()).position(|parked| parked.pane.id == id); + let (section, page) = match parked { + Some(at) => { + let parked = &self.parked[at]; + let key = parked.section.as_ref(); + let session = self.sessions().find(|session| Some(&session.key()) == key); + label(&parked.pane, session) + } + None => label(&self.pane, self.session.as_ref()), + }; + let leaf = self.ui.open( + part, + Spec { + axis: Axis::Y, + size, + role: Some(accesskit::Role::Group), + ..Spec::default() + }, + ); + if let Some(node) = self.ui.access(leaf) { + node.set_label(format!("{section}, {page}")); + } + let header = format!("{section} · {page}"); + self.ui.open( + "header", + Spec { + size: [fill(), px(ui::shell::TOOL)], + ..Spec::default() + }, + ); + self.ui.leaf( + "title", + Spec { + size: [fill(), px(ui::shell::TOOL)], + text: Some(&header), + overflow: Overflow::Ellipsis, + bold: parked.is_none(), + pad: [4.0, 0.0], + ..Spec::default() + }, + ); + let close = ui::shell::tool_button(&mut self.ui, "close", art::CLOSE, theme.text_dim, None); + let button = self.ui.id("close"); + name(&mut self.ui, button, "Close Pane"); + self.ui.close(); + if close.clicked { + self.commands.push(Command::ClosePane(id)); + } + let shown = match parked { + Some(at) => { + self.parked_page(at)?; + false + } + None => self.pane_content(theme)?, + }; + self.ui.close(); + Ok(shown) + } + + /// Parked pane `at`'s page, which the wheel scrolls and a press focuses; a pane without + /// a page shows bare paper. + fn parked_page(&mut self, at: usize) -> Result<(), Box> { + let Parked { pane, section } = &self.parked[at]; + let paper = pane.paper(&self.ui.theme); + let id = pane.id; + let shown = section.is_some(); + self.ui.open_as( + id, + Spec { + flags: Flags::FOCUSABLE + | if shown { + Flags::CUSTOM + } else { + Flags::CLICKABLE + }, + size: [fill(), fill()], + fill: Some(paper.color), + role: Some(accesskit::Role::GenericContainer), + ..Spec::default() + }, + ); + if shown { + let view = &self.parked[at].pane.view; + let scroll = view.scroll(); + let (origin, size) = (view.viewport.origin, view.viewport.size); + for (index, axis) in [Axis::X, Axis::Y].into_iter().enumerate() { + if let Some(offset) = ui::scrollbar( + &mut self.ui, + index, + axis, + -origin[index], + [scroll.min[index], scroll.max[index]], + size[index] as f32, + scroll.max[1 - index] > scroll.min[1 - index], + [0.18, 0.18, 0.18, 0.45], + ) { + let _ = self.parked[at].pane.view.scroll_to(index, offset)?; + self.window.request_redraw(); + } + } + } + self.ui.close(); + let scale = self.ui.scale(); + let corner = self.ui.laid_out(id).unwrap_or_default(); + let view = &mut self.parked[at].pane.view; + for event in self.ui.signal(id).events { + let response = match event { + ui::Event::PointerMoved([x, y]) => { + view.pointer_moved([(x - corner[0]) * scale, (y - corner[1]) * scale])? + } + ui::Event::PointerLeft => view.pointer_left(), + ui::Event::Wheel([x, y]) => view.wheel([x * scale, y * scale])?, + ui::Event::Modifiers(modifiers) => { + view.modifiers_changed(ui::edit_modifiers(modifiers))? + } + _ => continue, + }; + if response.changed || response.moved { + self.window.request_redraw(); + } + } + Ok(()) + } +} + +/// What `pane` shows of `session`, its section and page, as its header names them. +fn label(pane: &Pane, session: Option<&Session>) -> (String, String) { + match session { + Some(session) => { + let title = session.title(pane.showing.space); + ( + session.tabs[session.tab].name.clone(), + if title.is_empty() { + "Untitled page" + } else { + title + } + .to_owned(), + ) + } + None => match (&pane.locked, &pane.sectionless) { + (Some(locked), _) => ( + crate::protection::section_name(&locked.path).to_owned(), + "Locked".to_owned(), + ), + (None, Some(library)) => (library.name.clone(), "No sections".to_owned()), + (None, None) => (String::new(), String::new()), + }, + } } /// Shows `library`'s section at catalog `path`, which a parked pane holds open through diff --git a/crates/snowbound/src/tags.rs b/crates/snowbound/src/tags.rs index fc6e44b392e033661a63333286372360880cf70a..85cffd06ed6651a8215a896d49f637226d90ab5e 100644 --- a/crates/snowbound/src/tags.rs +++ b/crates/snowbound/src/tags.rs @@ -578,7 +578,7 @@ impl State { return; } self.ui.close_popup(id()); - self.ui.set_focus(Some(crate::page())); + self.ui.set_focus(Some(self.pane.id)); self.tag_list = None; } } diff --git a/crates/snowbound/src/task_pane.rs b/crates/snowbound/src/task_pane.rs index 7d53d4dbee93fdbda5018290bccae977a33e8d2f..f3619256e47ff51b22b70649e251311bb5da3f2a 100644 --- a/crates/snowbound/src/task_pane.rs +++ b/crates/snowbound/src/task_pane.rs @@ -3,7 +3,7 @@ //! Spelling (F7), walking the page's marked words. use crate::search::{Scope, marked}; -use crate::{State, Theme, art, page, platform}; +use crate::{State, Theme, art, platform}; use canvas::gpu::tag_sources; use canvas::outline::TagIcon; use canvas::search::{Found, Tagged}; @@ -673,7 +673,7 @@ impl State { ], ); if keys.contains(&NamedKey::Escape) { - self.ui.set_focus(Some(page())); + self.ui.set_focus(Some(self.pane.id)); } let border = if self.ui.focused() == Some(field()) { theme.accent @@ -824,7 +824,7 @@ impl State { let found = &self.search.found[order[row]]; self.reveal(found.section.clone(), found.space, None)?; if clicked.is_some() || keys.contains(&NamedKey::Enter) { - self.ui.set_focus(Some(page())); + self.ui.set_focus(Some(self.pane.id)); } } Ok(()) @@ -922,7 +922,7 @@ impl State { { eprintln!("{error}"); } - self.ui.set_focus(Some(page())); + self.ui.set_focus(Some(self.pane.id)); } } diff --git a/crates/snowbound/src/templates.rs b/crates/snowbound/src/templates.rs index 703904294d6e9ac3588ef7d5190a7a5960c4e678..db01c416a74948f0082cd026286d530393745fb8 100644 --- a/crates/snowbound/src/templates.rs +++ b/crates/snowbound/src/templates.rs @@ -174,11 +174,13 @@ fn strip( (chosen, solid) } -/// Dialog `id`, titled `title`, of every template docked inside the page box's trailing edge, -/// `height` tall, while it is open. Returns the template chosen; it stays open to choose again. +/// Dialog `id`, titled `title`, of every template docked inside page box `page`'s trailing +/// edge, `height` tall, while it is open. Returns the template chosen; it stays open to choose +/// again. #[allow(clippy::too_many_arguments)] fn gallery( ui: &mut Ui, + page: Id, theme: &Theme, thumbnails: &mut Thumbnails, paper: Paper, @@ -202,7 +204,7 @@ fn gallery( radius: 8.0, pad: [PAD, PAD], gap: 8.0, - anchor: Some(Anchor::Dock(crate::page())), + anchor: Some(Anchor::Dock(page)), role: Some(accesskit::Role::Dialog), ..Spec::default() }, @@ -389,7 +391,7 @@ impl crate::State { }; let space = self.pane.showing.space; let read_only = session.read_only(&self.pane.showing); - let Some(rect) = self.ui.laid_out(crate::page()) else { + let Some(rect) = self.ui.laid_out(self.pane.id) else { return; }; // Thumbnails show templates on plain paper, not the page's colour. @@ -400,6 +402,7 @@ impl crate::State { let height = rect[3] - rect[1] - 8.0; if let Some(name) = gallery( &mut self.ui, + self.pane.id, theme, &mut self.thumbnails, paper, @@ -447,6 +450,7 @@ impl crate::State { } if let Some(name) = gallery( &mut self.ui, + self.pane.id, theme, &mut self.thumbnails, paper, diff --git a/crates/snowbound/src/web.rs b/crates/snowbound/src/web.rs index 7ece906ba0e4eb61b7e78fc92df95a823cec6494..8b88d7175da3078f998466d2743fd16eb380a354 100644 --- a/crates/snowbound/src/web.rs +++ b/crates/snowbound/src/web.rs @@ -3,7 +3,7 @@ //! event loop are stand-ins with the calls `State` makes of winit's; menus are the kit's own, //! as on Linux; the browser's chords stay the browser's. See arc/platforms.md. -use crate::{Reply, State, UserEvent, commands, page, settings}; +use crate::{Reply, State, UserEvent, commands, settings}; use canvas::date::DateField; use std::{ cell::{Cell, RefCell}, @@ -1816,7 +1816,7 @@ pub fn dictionary_arrived(name: String, affix: String, words: String) { /// Parks the text area while the page takes no text, keeping it writable for the /// interface's fields. fn park_input(state: &State) { - if state.ui.focused() != Some(page()) || !state.pane.view.accepts_text() { + if state.ui.focused() != Some(state.pane.id) || !state.pane.view.accepts_text() { place_input(0.0, 0.0, 1.0, state.ui.focused().is_some()); } } diff --git a/crates/snowbound/tests/replay.rs b/crates/snowbound/tests/replay.rs index c8a098f7d351bc4b8ab420dd1e32c838b93c682f..4a64900aff6c2e2023390fafcea524ce2d9a837d 100644 --- a/crates/snowbound/tests/replay.rs +++ b/crates/snowbound/tests/replay.rs @@ -1524,3 +1524,265 @@ fn an_icloud_notebook_offers_delete_in_place_of_close() { "{menu}" ); } + +/// The line of `tree` that starts, past its indent, with `start`. +fn line<'a>(tree: &'a str, start: &str) -> &'a str { + tree.lines() + .find(|line| line.trim_start().starts_with(start)) + .unwrap_or_else(|| panic!("no {start:?} in:\n{tree}")) +} + +/// Split, each pane is a group named for its section and page, and the window's title, the +/// section tabs and the zoom follow the pane focused. +#[test] +fn split_panes_are_named_groups_and_the_chrome_follows_focus() { + let scratch = Scratch::new("split-focus"); + let notebook = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../corpus/search/notebook"); + let steps = [ + "split right", + "settle", + // The Reading tab, which opens in the pane split off, then zoomed in. + "move 311 51", + "press", + "release", + "settle", + "modifiers command", + "key =", + "modifiers", + "settle", + "accessibility right", + "move 177 300", + "press", + "release", + "settle", + "accessibility left", + // The Reading tab again, now in the pane on Garden, while only the other shows it. + "move 311 51", + "press", + "release", + "settle", + "accessibility held", + ]; + let [right, left, held] = replay(&scratch, Some(¬ebook), &steps) + .try_into() + .unwrap(); + for tree in [&right, &left] { + let groups: Vec<_> = (tree.lines()) + .filter(|line| line.starts_with(" Group ")) + .collect(); + assert_eq!( + groups, + [ + r#" Group "Garden, Spring planting plan""#, + r#" Group "Reading, Reading now""# + ], + "{tree}" + ); + } + assert_eq!( + line(&right, "Window"), + r#"Window "Reading now · Reading.one""# + ); + assert_eq!( + line(&right, "Tab \"Reading\""), + r#" Tab "Reading" [selected]"# + ); + assert_ne!( + line(&right, "Button \"Zoom\""), + r#" Button "Zoom" = "100%""# + ); + assert_eq!( + line(&left, "Window"), + r#"Window "Spring planting plan · Garden.one""# + ); + assert_eq!( + line(&left, "Tab \"Garden\""), + r#" Tab "Garden" [selected]"# + ); + assert_eq!( + line(&left, "Button \"Zoom\""), + r#" Button "Zoom" = "100%""# + ); + // The section opens from the pane holding it, not its file again, which waits for it. + assert_eq!( + pane_groups(&held), + [ + r#" Group "Reading, Reading now""#, + r#" Group "Reading, Reading now""# + ], + "{held}" + ); + assert_eq!( + line(&held, "Window"), + r#"Window "Reading now · Reading.one""# + ); +} + +/// The groups at the top of `tree`: a split pane's each, or the one pane's page. +fn pane_groups(tree: &str) -> Vec<&str> { + (tree.lines()) + .filter(|line| line.starts_with(" Group ")) + .collect() +} + +/// The line of `tree` holding the focused pane's page title. +fn page_title(tree: &str) -> &str { + line(tree, "MultilineTextInput \"Page title\"") +} + +/// What is typed in one pane shows at once in another on the page, which takes it in place. +#[test] +fn an_edit_in_one_pane_shows_in_another_on_the_page() { + let scratch = Scratch::new("split-edit"); + let notebook = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../corpus/search/notebook"); + let steps = [ + "split right", + "settle", + // Into the title of the pane split off, then back to the first. + "move 649 129", + "press", + "release", + "type Both ", + "settle", + "move 177 300", + "press", + "release", + "settle", + "accessibility left", + ]; + let [left] = replay(&scratch, Some(¬ebook), &steps) + .try_into() + .unwrap(); + assert!(page_title(&left).contains("Both "), "{left}"); +} + +/// Undoing a page's new name, done away from the frame, shows in every pane on the page, +/// so none writes the old name back. +#[test] +fn undoing_a_rename_shows_in_every_pane_on_the_page() { + let scratch = Scratch::new("split-undo"); + let notebook = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../corpus/search/notebook"); + let steps = [ + "split right", + "settle", + // A double click on the page's tab renames it. + "move 1003 81", + "press", + "release", + "press", + "release", + "settle", + "type Renamed", + "key Enter", + "settle", + "modifiers command", + "key z", + "modifiers", + "settle", + "move 177 300", + "press", + "release", + "settle", + "accessibility left", + ]; + let [left] = replay(&scratch, Some(¬ebook), &steps) + .try_into() + .unwrap(); + assert!( + page_title(&left).ends_with(r#"= "Spring planting plan""#), + "{left}" + ); +} + +/// A page's version shown beside the page stays as it was while the page is edited. +#[test] +fn a_version_beside_its_page_keeps_its_text() { + let scratch = Scratch::new("split-version"); + let notebook = + Path::new(env!("CARGO_MANIFEST_DIR")).join("../../corpus/page-versions/candidate-restore"); + let mut steps: Vec = ["split right", "settle"].map(String::from).to_vec(); + // The version row under the page, opened in the pane split off. + steps.extend(["modifiers command shift", "key p", "modifiers"].map(String::from)); + steps.extend(["type Page Versions", "settle", "key Enter", "settle"].map(String::from)); + steps.extend(["move 1003 114", "press", "release", "settle"].map(String::from)); + // Typing in the page, in the first pane, then back to the version. + steps.extend(["move 177 200", "press", "release", "settle"].map(String::from)); + steps.extend(["type Zebra ", "accessibility page"].map(String::from)); + steps.extend(["move 640 300", "press", "release", "settle"].map(String::from)); + steps.push("accessibility version".into()); + let [page, version] = run(&scratch, ¬ebook, &steps).try_into().unwrap(); + let outline = "MultilineTextInput \"Text outline 1\""; + assert!(line(&page, outline).contains("Zebra"), "{page}"); + let text = line(&version, outline); + assert!( + text.contains("Second author line") && !text.contains("Zebra"), + "{version}" + ); +} + +/// A version deleted in one pane leaves the other pane showing it for the page itself. +#[test] +fn a_version_deleted_in_one_pane_leaves_the_other() { + let scratch = Scratch::new("split-delete-version"); + let notebook = + Path::new(env!("CARGO_MANIFEST_DIR")).join("../../corpus/page-versions/candidate-restore"); + let mut versions: Vec = ["modifiers command shift", "key p", "modifiers"] + .into_iter() + .chain(["type Page Versions", "settle", "key Enter", "settle"]) + // The version row under the page. + .chain(["move 1003 114", "press", "release", "settle"]) + .map(String::from) + .collect(); + let mut steps: Vec = vec!["split right".into(), "settle".into()]; + steps.append(&mut versions.clone()); + steps.extend(["move 177 300", "press", "release", "settle"].map(String::from)); + steps.append(&mut versions); + // Delete Version on the first pane's bar, then back to the other. + steps.extend(["move 177 116", "press", "release", "settle"].map(String::from)); + steps.extend(["key Down", "key Down", "key Enter", "settle"].map(String::from)); + steps.extend(["move 640 300", "press", "release", "settle"].map(String::from)); + steps.push("accessibility other".into()); + let [other] = run(&scratch, ¬ebook, &steps).try_into().unwrap(); + assert!( + line(&other, "MultilineTextInput \"Text outline 1\"") + .ends_with(r#"= "First state. Typed by virtual.""#), + "{other}" + ); + assert!(other.contains(r#"Tab "Versioned" [selected]"#), "{other}"); +} + +/// Closing the panes split off leaves the window drawn as it was before the split. +#[test] +fn closing_split_panes_leaves_the_window_as_it_was() { + let scratch = Scratch::new("split-close"); + let notebook = + Path::new(env!("CARGO_MANIFEST_DIR")).join("../../corpus/cross-container/candidate"); + let [before, after] = ["before", "after"].map(|name| scratch.0.join(format!("{name}.png"))); + let snapshot = |path: &Path| format!("snapshot {}", path.display()); + let steps = [ + "appearance light".to_owned(), + // Colours ease into the appearance. + "wait 1500".into(), + "settle".into(), + snapshot(&before), + "split right".into(), + "settle".into(), + "split down".into(), + "settle".into(), + "close pane".into(), + "settle".into(), + "close pane".into(), + "wait 1500".into(), + "settle".into(), + snapshot(&after), + "settle".into(), + ]; + let steps: Vec<_> = steps.iter().map(String::as_str).collect(); + replay(&scratch, Some(¬ebook), &steps); + assert!( + std::fs::read(&before).unwrap() == std::fs::read(&after).unwrap(), + "the window changed: {} {}", + before.display(), + after.display() + ); +} -- 2.54.1