From 0e84052a1513b0c655ca1a713f0be5466451527d Mon Sep 17 00:00:00 2001 From: DWAA1660 Date: Tue, 17 Nov 2026 14:15:39 -0700 Subject: [PATCH 1/2] feat(draw): ruler, ink help, steadier strokes, video, reader Replaces four "not built" placeholders and improves pen strokes: - Draw > Ruler (#293): a straightedge on the page. Drag to move, drag the handle or press [ and ] to turn it. A stroke begun just outside a long edge is laid along that edge. Never saved. - Pen strokes (#44): tremor is taken out when the pen is lifted, and a wide nib's edges follow the line over a quarter of its width, so highlighter edges and ends no longer zigzag. - Draw > Ink Help (#296): opens the Welcome notebook's handwriting page in the interface language, else English. - Insert > Online Video (#284): a checked link to the video at the caret. Nothing is loaded until the link is clicked. - View > Immersive Reader (#299): the page as plain reading with text size, line spacing and a line focus. Read aloud is not built. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Y4im4nDYbxmtByUCBaBzNq --- crates/ink/src/lib.rs | 61 +++++ crates/ink/src/ruler.rs | 211 +++++++++++++++++ crates/ink/src/shape.rs | 42 +++- crates/ui-egui/src/app.rs | 164 +++++++++++++ crates/ui-egui/src/inkpad.rs | 217 ++++++++++++++++- crates/ui-egui/src/lib.rs | 2 + crates/ui-egui/src/onlinevideo.rs | 138 +++++++++++ crates/ui-egui/src/reader.rs | 378 ++++++++++++++++++++++++++++++ crates/ui-egui/src/ribbon.rs | 40 +++- crates/ui-egui/tests/ui.rs | 206 ++++++++++++++++ docs/gaps.md | 23 +- i18n/ar-XB.tsv | 36 +++ i18n/en-XA.tsv | 36 +++ i18n/en.tsv | 36 +++ 14 files changed, 1568 insertions(+), 22 deletions(-) create mode 100644 crates/ink/src/ruler.rs create mode 100644 crates/ui-egui/src/onlinevideo.rs create mode 100644 crates/ui-egui/src/reader.rs diff --git a/crates/ink/src/lib.rs b/crates/ink/src/lib.rs index f73250f2..9d0e0e65 100644 --- a/crates/ink/src/lib.rs +++ b/crates/ink/src/lib.rs @@ -28,6 +28,7 @@ pub mod pick; pub mod rack; +pub mod ruler; pub mod shape; pub mod shapes; pub mod svg; @@ -246,6 +247,37 @@ impl Stroke { self.dabs.is_empty() } + /// The stroke with the hand's tremor taken out, for when the pen is lifted (issue #44). + /// + /// A pen reports where it is to a fraction of a point, and a hand shakes by about that + /// much. The spline that draws a stroke passes through every reading, so each wobble + /// became a kink, and on a wide highlighter a kink turns the ribbon's edges into a zigzag. + /// Each reading between the ends is drawn towards the average of itself and its + /// neighbours, but never further than a tremor: at most a third of a point, or an eighth of + /// the stroke's width for a wide one. A deliberate corner moves less than that relative to + /// its size, so writing keeps its shape. The first and last readings and every pressure + /// stay exactly as they were. + #[must_use] + pub fn settled(mut self) -> Self { + if self.dabs.len() < 3 { + return self; + } + let limit = (self.width / 8.0).max(0.35); + for _ in 0..2 { + let before = self.dabs.clone(); + for (i, dab) in self.dabs.iter_mut().enumerate().skip(1) { + let (Some(prev), Some(next)) = (before.get(i - 1), before.get(i + 1)) else { continue }; + let (ax, ay) = ((prev.x + 2.0 * dab.x + next.x) / 4.0, (prev.y + 2.0 * dab.y + next.y) / 4.0); + let (dx, dy) = (ax - dab.x, ay - dab.y); + let len = dx.hypot(dy); + let scale = if len > limit { limit / len } else { 1.0 }; + dab.x += dx * scale; + dab.y += dy * scale; + } + } + self + } + /// The box the ink covers, including smoothing and the selected full-width padding. /// `None` for a stroke with no readings. pub fn bounds(&self) -> Option<(f32, f32, f32, f32)> { @@ -381,6 +413,35 @@ mod tests { assert_eq!(Tool::Highlighter.floor(), 1.0, "and it is a flat chisel, so pressure does not thin it"); } + #[test] + fn settling_a_stroke_takes_out_tremor_but_keeps_corners_and_ends() { + // Issue #44: a straight line drawn by a shaking hand. + let mut shaky = Stroke::new(Tool::Highlighter, Rgb::BLACK, 12.0); + for i in 0..60 { + let wobble = if i % 2 == 0 { 0.6 } else { -0.6 }; + shaky.add(Dab::new(i as f32 * 2.0, 50.0 + wobble, 0.7)); + } + let worst = |s: &Stroke| s.dabs.iter().skip(1).take(s.dabs.len() - 2).map(|d| (d.y - 50.0).abs()).fold(0.0_f32, f32::max); + let settled = shaky.clone().settled(); + assert!(worst(&settled) < worst(&shaky) / 3.0, "{} then {}", worst(&shaky), worst(&settled)); + assert_eq!(settled.dabs.first(), shaky.dabs.first(), "the start stays put"); + assert_eq!(settled.dabs.last(), shaky.dabs.last(), "the end stays put"); + assert!(settled.dabs.iter().zip(&shaky.dabs).all(|(a, b)| a.pressure == b.pressure), "pressure is untouched"); + + // A V written with a fine pen: the corner moves no further than a tremor. + let mut vee = Stroke::new(Tool::Pen, Rgb::BLACK, 2.0); + for (x, y) in [(0.0, 0.0), (5.0, 10.0), (10.0, 20.0), (15.0, 10.0), (20.0, 0.0)] { + vee.add(Dab::new(x, y, 0.5)); + } + let corner = vee.clone().settled().dabs[2]; + assert!((corner.y - 20.0).abs() <= 0.35 * 2.0 + 1e-4, "{corner:?}"); + // Too short to have a middle: left alone. + let mut two = Stroke::new(Tool::Pen, Rgb::BLACK, 2.0); + two.add(Dab::new(0.0, 0.0, 0.5)); + two.add(Dab::new(9.0, 1.0, 0.5)); + assert_eq!(two.clone().settled(), two); + } + #[test] fn readings_that_say_nothing_new_are_dropped() { let mut s = Stroke::new(Tool::Pen, Rgb::BLACK, 3.0); diff --git a/crates/ink/src/ruler.rs b/crates/ink/src/ruler.rs new file mode 100644 index 00000000..a69f1b3e --- /dev/null +++ b/crates/ink/src/ruler.rs @@ -0,0 +1,211 @@ +//! Draw > Ruler (issue #293): a straightedge laid on the page. +//! +//! The ruler is something the user holds, not page content: it is never written to a file. +//! A stroke begun just outside one of its long edges is laid along that edge, so a line +//! drawn against it comes out straight, and the ink is ordinary ink after that. All of it is +//! arithmetic in page points, tested here without a window. + +/// How long the ruler is, in points (about 20 cm). +pub const LENGTH: f32 = 567.0; +/// How wide it is, in points. +pub const WIDTH: f32 = 46.0; +/// How far outside an edge a pen may land and still be drawn along it, in points. +pub const REACH: f32 = 14.0; +/// The round handle that turns the ruler sits this far in from its right-hand end. +pub const HANDLE_IN: f32 = 18.0; +/// The handle's radius, in points. +pub const HANDLE_R: f32 = 10.0; + +/// Which long edge a stroke is drawn along. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Edge { + /// The edge above the middle when the ruler lies flat. + Upper, + /// The edge below it. + Lower, +} + +/// Where the ruler lies: its middle, in page points, and how far it is turned, in degrees +/// clockwise on screen (y grows down the page). +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Ruler { + pub x: f32, + pub y: f32, + pub angle: f32, +} + +impl Ruler { + /// A ruler lying flat with its middle at (`x`, `y`). + pub fn at(x: f32, y: f32) -> Self { + Self { x, y, angle: 0.0 } + } + + fn axis(&self) -> (f32, f32) { + let a = self.angle.to_radians(); + (a.cos(), a.sin()) + } + + /// A page point in the ruler's own terms: how far along it from the middle, and how + /// far across (negative towards the upper edge). + pub fn to_local(&self, x: f32, y: f32) -> (f32, f32) { + let (c, s) = self.axis(); + let (dx, dy) = (x - self.x, y - self.y); + (dx * c + dy * s, -dx * s + dy * c) + } + + /// Back from the ruler's terms to the page. + pub fn to_page(&self, along: f32, across: f32) -> (f32, f32) { + let (c, s) = self.axis(); + (self.x + along * c - across * s, self.y + along * s + across * c) + } + + /// The ruler's four corners on the page, clockwise from the upper left. + pub fn corners(&self) -> [(f32, f32); 4] { + let (h, w) = (LENGTH / 2.0, WIDTH / 2.0); + [self.to_page(-h, -w), self.to_page(h, -w), self.to_page(h, w), self.to_page(-h, w)] + } + + /// Whether the point is on the ruler, where a press holds it rather than drawing. + pub fn contains(&self, x: f32, y: f32) -> bool { + let (along, across) = self.to_local(x, y); + along.abs() <= LENGTH / 2.0 && across.abs() <= WIDTH / 2.0 + } + + /// The middle of the round handle that turns the ruler. + pub fn handle(&self) -> (f32, f32) { + self.to_page(LENGTH / 2.0 - HANDLE_IN, 0.0) + } + + /// Whether the point is on the turning handle. + pub fn on_handle(&self, x: f32, y: f32) -> bool { + let (hx, hy) = self.handle(); + (x - hx).hypot(y - hy) <= HANDLE_R + 2.0 + } + + /// The edge a pen landing here draws along, if it landed close enough outside one. + /// `reach` is in page points (smaller when the page is zoomed in, so it stays the same + /// on screen). + pub fn edge_near(&self, x: f32, y: f32, reach: f32) -> Option { + let (along, across) = self.to_local(x, y); + if along.abs() > LENGTH / 2.0 { + return None; + } + let w = WIDTH / 2.0; + if (-w - reach..-w).contains(&across) { + Some(Edge::Upper) + } else if across > w && across <= w + reach { + Some(Edge::Lower) + } else { + None + } + } + + /// Where a reading is drawn when the pen runs along `edge`: on the line half the pen's + /// `width` outside the edge, so the ink touches the ruler and does not run under it, and + /// no further along than the ruler is long. + pub fn along(&self, edge: Edge, x: f32, y: f32, width: f32) -> (f32, f32) { + let (along, _) = self.to_local(x, y); + let off = WIDTH / 2.0 + width.max(0.0) / 2.0; + let across = match edge { + Edge::Upper => -off, + Edge::Lower => off, + }; + self.to_page(along.clamp(-LENGTH / 2.0, LENGTH / 2.0), across) + } + + /// Turns the ruler by `degrees` (clockwise on screen), kept within a half turn either way. + pub fn turn(&mut self, degrees: f32) { + self.angle = normal(self.angle + degrees); + } + + /// Turns the ruler about its middle so that its handle points at (`x`, `y`). + pub fn point_at(&mut self, x: f32, y: f32) { + let (dx, dy) = (x - self.x, y - self.y); + if dx.hypot(dy) > 1.0 { + self.angle = normal(dy.atan2(dx).to_degrees()); + } + } + + /// The angle shown to the user: whole degrees from 0 to 179, as a protractor reads a + /// line (a ruler turned half round lies along the same line). + pub fn shown_degrees(&self) -> u16 { + #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)] + let d = (-self.angle).rem_euclid(180.0).round() as u16; + d % 180 + } +} + +fn normal(degrees: f32) -> f32 { + let d = degrees.rem_euclid(360.0); + if d > 180.0 { d - 360.0 } else { d } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn close(a: (f32, f32), b: (f32, f32)) -> bool { + (a.0 - b.0).abs() < 1e-3 && (a.1 - b.1).abs() < 1e-3 + } + + #[test] + fn page_and_ruler_terms_agree_at_any_angle() { + for angle in [0.0, 30.0, -45.0, 90.0, 179.0] { + let r = Ruler { x: 200.0, y: 300.0, angle }; + for (x, y) in [(200.0, 300.0), (10.0, 20.0), (450.0, 280.0)] { + let (a, c) = r.to_local(x, y); + assert!(close(r.to_page(a, c), (x, y)), "angle {angle}"); + } + } + } + + #[test] + fn a_press_on_the_ruler_holds_it_and_one_just_outside_an_edge_draws_along_it() { + let r = Ruler::at(300.0, 200.0); + assert!(r.contains(300.0, 200.0)); + assert!(r.contains(300.0 - LENGTH / 2.0 + 1.0, 200.0 + WIDTH / 2.0 - 1.0)); + assert!(!r.contains(300.0, 200.0 - WIDTH / 2.0 - 1.0)); + assert_eq!(r.edge_near(300.0, 200.0 - WIDTH / 2.0 - 4.0, REACH), Some(Edge::Upper)); + assert_eq!(r.edge_near(300.0, 200.0 + WIDTH / 2.0 + 4.0, REACH), Some(Edge::Lower)); + assert_eq!(r.edge_near(300.0, 200.0 - WIDTH / 2.0 - REACH - 1.0, REACH), None, "too far off to mean the ruler"); + assert_eq!(r.edge_near(300.0, 200.0, REACH), None, "on the ruler is holding it, not drawing"); + assert_eq!(r.edge_near(300.0 + LENGTH, 200.0 - WIDTH / 2.0 - 4.0, REACH), None, "past its end"); + assert!(r.on_handle(r.handle().0, r.handle().1)); + } + + #[test] + fn a_shaky_hand_along_a_turned_ruler_draws_a_straight_line() { + let r = Ruler { x: 100.0, y: 100.0, angle: 30.0 }; + let width = 4.0; + let wobbly: Vec<(f32, f32)> = + (0..20).map(|i| r.to_page(-100.0 + i as f32 * 10.0, -WIDTH / 2.0 - 3.0 + if i % 2 == 0 { 2.0 } else { -1.5 })).collect(); + let drawn: Vec<(f32, f32)> = wobbly.iter().map(|&(x, y)| r.along(Edge::Upper, x, y, width)).collect(); + for &(x, y) in &drawn { + let (_, across) = r.to_local(x, y); + assert!((across + WIDTH / 2.0 + width / 2.0).abs() < 1e-3, "every point is on the one line beside the edge"); + } + // And it follows the pen along the ruler. + let first = r.to_local(drawn[0].0, drawn[0].1).0; + let last = r.to_local(drawn[19].0, drawn[19].1).0; + assert!((first + 100.0).abs() < 1e-3 && (last - 90.0).abs() < 1e-3); + // No further than the ruler is long. + let past = r.along(Edge::Lower, r.to_page(LENGTH, 0.0).0, r.to_page(LENGTH, 0.0).1, width); + assert!((r.to_local(past.0, past.1).0 - LENGTH / 2.0).abs() < 1e-3); + } + + #[test] + fn turning_shows_the_angle_a_protractor_would() { + let mut r = Ruler::at(0.0, 0.0); + assert_eq!(r.shown_degrees(), 0); + r.turn(-30.0); + assert_eq!(r.shown_degrees(), 30, "turned anticlockwise on screen reads 30"); + r.turn(-180.0); + assert_eq!(r.shown_degrees(), 30, "half round lies on the same line"); + r.turn(400.0); + assert!(r.angle > -180.0 && r.angle <= 180.0); + r.point_at(0.0, -50.0); + assert_eq!(r.shown_degrees(), 90); + r.point_at(50.0, 0.0); + assert_eq!(r.shown_degrees(), 0); + } +} diff --git a/crates/ink/src/shape.rs b/crates/ink/src/shape.rs index a30d49d7..ba048a1d 100644 --- a/crates/ink/src/shape.rs +++ b/crates/ink/src/shape.rs @@ -78,6 +78,14 @@ pub fn width_at(stroke: &Stroke, pressure: f32) -> f32 { (stroke.width * share).max(crate::MIN_WIDTH) } +/// How many points of the smoothed middle apart the direction of a stroke is read from: +/// one for a fine pen, more for a wide one, so that it covers about a quarter of the width. +pub fn heading_reach(stroke: &Stroke) -> usize { + #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)] + let reach = (stroke.width / 4.0 / STEP).round() as usize; + reach.clamp(1, 12) +} + /// The two edges of the ribbon, left and right of the smoothed middle, point for point /// with it. A painter that fills triangles wants these; SVG wants them joined up, which is /// what [`outline`] does. Both come from here, so the file and the screen agree. @@ -85,11 +93,14 @@ pub fn sides(stroke: &Stroke) -> (Vec<(f32, f32)>, Vec<(f32, f32)>) { let line = centre(stroke); let mut left: Vec<(f32, f32)> = Vec::with_capacity(line.len()); let mut right: Vec<(f32, f32)> = Vec::with_capacity(line.len()); + let reach = heading_reach(stroke); for (i, d) in line.iter().enumerate() { // The direction here is the way the line is going, taken from the points either - // side so a corner leans the right way. - let before = line.get(i.saturating_sub(1)).copied().unwrap_or(*d); - let after = line.get(i + 1).copied().unwrap_or(*d); + // side so a corner leans the right way. For a wide nib the points are taken a little + // further off, about a quarter of its width: the edges are that far from the middle, + // so a wobble the eye cannot see in the middle would swing them into a zigzag (#44). + let before = line.get(i.saturating_sub(reach)).copied().unwrap_or(*d); + let after = line.get((i + reach).min(line.len().saturating_sub(1))).copied().unwrap_or(*d); let (dx, dy) = (after.x - before.x, after.y - before.y); let len = (dx * dx + dy * dy).sqrt(); // Two readings in the same place say nothing about direction; the ends are round @@ -115,14 +126,16 @@ pub fn outline(stroke: &Stroke) -> Vec<(f32, f32)> { let mut out = left; // Where the readings give no direction, `sides` assumed the pen was going across the // page, left to right; each end assumes the same, so a tap still closes into a dot. + // The ends face the way the ribbon's edges arrive there, read over the same reach. + let reach = heading_reach(stroke).min(line.len() - 1); out.extend(half_circle( last, width_at(stroke, last.pressure) / 2.0, - line.get(line.len().saturating_sub(2)).copied().unwrap_or(first), + line.get(line.len().saturating_sub(1 + reach)).copied().unwrap_or(first), (1.0, 0.0), )); out.extend(right.into_iter().rev()); - out.extend(half_circle(first, width_at(stroke, first.pressure) / 2.0, line.get(1).copied().unwrap_or(last), (-1.0, 0.0))); + out.extend(half_circle(first, width_at(stroke, first.pressure) / 2.0, line.get(reach).copied().unwrap_or(last), (-1.0, 0.0))); out } @@ -239,6 +252,25 @@ mod tests { s } + #[test] + fn a_wide_highlighter_edge_does_not_fold_back_on_a_shaky_line() { + // Issue #44: a highlighter drawn left to right by a hand that shakes a little. Each + // edge must keep going the same way; an edge that steps backwards is a fold, which a + // see-through highlighter paints twice, and that is the zigzag users saw. + let mut s = Stroke::new(Tool::Highlighter, Rgb::BLACK, 14.0); + for i in 0..40 { + let wobble = [0.0, 0.5, -0.3, 0.4, -0.5][i % 5]; + s.dabs.push(Dab::new(i as f32 * 1.5, 40.0 + wobble, 1.0)); + } + let folds = |s: &Stroke| { + let (left, right) = sides(s); + [left, right].iter().map(|edge| edge.windows(2).filter(|w| w[1].0 < w[0].0 - 0.05).count()).sum::() + }; + assert!(folds(&s) > 0, "the shaky readings as they came do fold"); + // As the pen lays it down: settled when lifted, then given its width. + assert_eq!(folds(&s.clone().settled()), 0, "a settled stroke's edges do not fold"); + } + #[test] fn a_smoothed_line_passes_through_every_reading() { let s = drawn(Tool::Pen, &[(0.0, 0.0, 1.0), (20.0, 14.0, 1.0), (40.0, 0.0, 1.0), (60.0, 14.0, 1.0)]); diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rs index 415317ed..af161dc9 100644 --- a/crates/ui-egui/src/app.rs +++ b/crates/ui-egui/src/app.rs @@ -852,6 +852,14 @@ enum Dialog { editing: bool, first: bool, }, + /// Insert > Online Video: the address of a video to link to at the caret (issue #284). + OnlineVideo { + block: usize, + cell: Option<(usize, usize)>, + range: (usize, usize), + address: String, + first: bool, + }, /// Insert > Table > Insert Table...: a table of any size up to the limits, typed in. InsertTable { rows: String, @@ -1093,6 +1101,10 @@ pub struct NoteCraftApp { page_scroll_strip: Option, /// True while Insert Space is in hand: the next drag on the page adds or takes away room. space_tool: bool, + /// Draw > Ruler is out on the page (issue #293). Where it lies is the ink layer's. + ruler: bool, + /// View > Immersive Reader, while it is open (issue #299). + reader: Option, /// What the pointer does on the page (the Draw tab). pointer: ribbon::Pointer, /// The tools on the rack, as the owner has arranged them, and which is in hand. Both @@ -1278,6 +1290,8 @@ impl NoteCraftApp { page_hbar: None, page_scroll_strip: None, space_tool: false, + ruler: false, + reader: None, canvas_drag: None, typed_while_inking: Vec::new(), typed_wait: 0, @@ -1600,6 +1614,20 @@ impl NoteCraftApp { } } + /// Draw > Ink Help (issue #296): the Welcome notebook's handwriting and drawing page, in + /// the interface language when the notebook has it and in English otherwise. Only opens a + /// page; the note that was open is not changed. + fn open_ink_help(&mut self) { + let notebooks: Vec = self.open_notebooks().into_iter().map(|n| n.path).collect(); + match drawing_help_page(¬ebooks, &crate::i18n::current_code()) { + Some(page) => { + self.expand_to(&page); + self.open_page(&page); + } + None => self.say(crate::tl_id!("ui.ink_help_missing")), + } + } + /// Back to the page before this one. fn go_back(&mut self) { match self.nav.back(|p| p.exists()) { @@ -4158,6 +4186,7 @@ impl NoteCraftApp { nib: self.nib, in_table: self.caret_in_table(), space_tool: self.space_tool, + ruler: self.ruler, table_borders: self.caret_table_has_borders(), table_align: self.caret_cells_align(), table_shade: self.caret_cells_shade(), @@ -4509,6 +4538,7 @@ impl NoteCraftApp { self.save_block_marks(marks); } "link" => self.open_link_dialog(), + "online-video" => self.open_online_video_dialog(), "equation" => self.insert_in_page("$E = mc^2$"), "date" | "time" | "datetime" => { let now = notecraft_model::time::iso_now(); @@ -4636,6 +4666,18 @@ impl NoteCraftApp { ribbon::Act::CustomizeTags => { self.custom_tags = Some(crate::customtags::Dialog::new(notecraft_model::tag::custom())); } + ribbon::Act::InkHelp => self.open_ink_help(), + ribbon::Act::ImmersiveReader => match self.editor.as_ref() { + Some(ed) => self.reader = Some(crate::reader::Reader::new(&ed.title_buf, &ed.blocks, !ed.ink_files.is_empty())), + None => self.say(crate::tl_id!("ui.reader_open_a_page")), + }, + ribbon::Act::ToggleRuler => { + self.ruler = !self.ruler; + // The ruler is for drawing: with the caret in hand, the pen last used comes up. + if self.ruler && self.pointer == ribbon::Pointer::Write { + self.ribbon_act(ribbon::Act::InkPen(self.nib)); + } + } ribbon::Act::CollectAll => { if self.editor.as_ref().is_none_or(|ed| !ed.writable) { self.say(crate::tl_id!("ui.open_a_page_first")); @@ -6945,6 +6987,26 @@ impl NoteCraftApp { self.dialog = Some(Dialog::Link { block: caret.block, cell: caret.cell, range, text, address, editing, first: true }); } + /// Opens Insert > Online Video (issue #284): the video goes in at the caret, in place of + /// any words selected in one line. + pub fn open_online_video_dialog(&mut self) { + let Some(ed) = self.editor.as_mut().filter(|ed| ed.writable) else { + self.say(crate::tl_id!("ui.open_a_page_first")); + return; + }; + if ed.across.is_some_and(|a| !a.inside_one_block()) { + self.say(crate::tl_id!("ui.select_words_in_one_line_for_a_link")); + return; + } + Self::place_caret_for_format(ed); + let Some(caret) = ed.caret.as_ref() else { + self.say(crate::tl_id!("ui.click_in_the_page_first_then_use_the_ribbon")); + return; + }; + let range = (caret.selection.0.min(caret.selection.1), caret.selection.0.max(caret.selection.1)); + self.dialog = Some(Dialog::OnlineVideo { block: caret.block, cell: caret.cell, range, address: String::new(), first: true }); + } + /// Puts `replacement` in place of the characters `range` of the caret's line, when the /// caret is still in the line the Link dialog was opened for. One step of the history. fn replace_in_caret_line(&mut self, block: usize, cell: Option<(usize, usize)>, range: (usize, usize), replacement: &str) { @@ -8039,6 +8101,7 @@ impl NoteCraftApp { .filter(|bar| bar.min.x > ink_area.min.x) .map_or(ink_area, |bar| ink_area.with_max_x(ink_area.max.x.min(bar.min.x))); let mut ink_ui = ui.new_child(egui::UiBuilder::new().layer_id(layer).max_rect(ink_area)); + crate::inkpad::set_ruler(ink_ui.ctx(), self.ruler); ink_changed |= crate::inkpad::show(&mut ink_ui, ink_area, hit, top, pad, pen, pressure, &tokens, zoom_scale); } // A press lets go of whatever was selected: the drag that may @@ -8560,6 +8623,12 @@ impl NoteCraftApp { // ---------- dialogs ---------- fn dialogs(&mut self, ctx: &Context) { + // View > Immersive Reader (issue #299) covers the window until it is closed. + if let Some(reader) = self.reader.as_mut() + && crate::reader::show(ctx, reader, &self.tokens) == Some(crate::reader::Act::Close) + { + self.reader = None; + } if let Some(mut dialog) = self.custom_tags.take() { let out = dialog.show(ctx, &self.tokens); if let Some(list) = out.changed { @@ -9289,6 +9358,52 @@ impl NoteCraftApp { keep = false; } } + Dialog::OnlineVideo { block, cell, range, address, first } => { + let mut go = false; + let video = crate::onlinevideo::read(address); + let backdrop = modal_backdrop(ctx, "online-video-backdrop"); + egui::Window::new(crate::tl_id!("ui.online_video_title")) + .title_bar(false) + .collapsible(false) + .resizable(false) + .order(egui::Order::Foreground) + .fixed_size(vec2(420.0, 0.0)) + .anchor(Align2::CENTER_CENTER, vec2(0.0, 0.0)) + .frame(modal_frame(&self.tokens)) + .show(ctx, |ui| { + stay_above(ui, backdrop); + modal_title(ui, &self.tokens, crate::tl_id!("ui.online_video_title")); + ui.label(crate::tl_id!("ui.online_video_address")); + let field = ui.add(egui::TextEdit::singleline(address).desired_width(400.0).hint_text("https://")); + if *first { + field.request_focus(); + *first = false; + } + if field.lost_focus() && ui.input(|i| i.key_pressed(Key::Enter)) && video.is_some() { + go = true; + } + if !address.trim().is_empty() && video.is_none() { + ui.colored_label(self.tokens.warn, crate::tl_id!("ui.online_video_bad_address")); + } + ui.label(egui::RichText::new(crate::tl_id!("ui.online_video_explain")).size(11.5).color(self.tokens.muted)); + ui.add_space(6.0); + ui.horizontal(|ui| { + if modal_button(ui, &self.tokens, crate::tl_id!("ui.cancel"), false).clicked() || ui.input(|i| i.key_pressed(Key::Escape)) + { + keep = false; + } + if modal_button_enabled(ui, &self.tokens, crate::tl_id!("ui.ok"), true, video.is_some()).clicked() { + go = true; + } + }); + }); + if go && let Some(video) = video { + let (block, cell, range) = (*block, *cell, *range); + self.replace_in_caret_line(block, cell, range, &crate::onlinevideo::line(&video)); + self.say(crate::tl_id!("ui.online_video_inserted")); + keep = false; + } + } Dialog::Rename { path, title } => { let mut go = false; egui::Window::new(crate::tl_id!("ui.rename")).collapsible(false).resizable(false).anchor(Align2::CENTER_CENTER, vec2(0.0, 0.0)).show( @@ -10289,7 +10404,22 @@ impl NoteCraftApp { "gain": self.gain(), "strokes": self.pad.as_ref().map(crate::inkpad::Pad::strokes), "inked": self.pad.as_ref().is_some_and(|p| !p.is_empty()), + // The readings of the newest stroke, in page points (issues #44, #293). + "lastStroke": self.pad.as_ref().map(crate::inkpad::Pad::last_stroke), + // Draw > Ruler (issue #293): where it lies once it is out, in page points. + "ruler": match crate::inkpad::ruler(ctx) { + Some(crate::inkpad::RulerState::Placed(r)) => json!({"x": r.x, "y": r.y, "angle": r.angle, "degrees": r.shown_degrees()}), + Some(crate::inkpad::RulerState::Unplaced) => json!({}), + None => Value::Null, + }, }, + // View > Immersive Reader (issue #299), while it is open. + "reader": self.reader.as_ref().map(|r| json!({ + "lines": r.lines.iter().map(|l| l.text.clone()).collect::>(), + "size": r.size, + "spacing": r.spacing, + "at": r.at, + })), // What a pen has told this window. `pen` is false until a device reports a // pressure at all, and `hardest` is the hardest press since the window opened. "pen": {"seen": self.stylus.sample().pen, "pressure": self.stylus.sample().pressure, "hardest": self.stylus.seen().0, "readings": self.stylus.seen().1}, @@ -10732,6 +10862,24 @@ fn theme_grid(ui: &mut Ui, current: Theme, pick: &mut Option<(Mode, Accent)>, fo ui.checkbox(follow, crate::tl_id!("ui.follow_the_system_light_or_dark_setting")); } +/// The Welcome notebook's handwriting and drawing page among `notebooks` (issue #296): the +/// one in the section for `language` (`10-en`, `20-ar`, ...), else the English one. The +/// notebook is found by its contents, so a renamed or moved copy still works. +fn drawing_help_page(notebooks: &[PathBuf], language: &str) -> Option { + let base = language.split(['-', '_']).next().unwrap_or("en").to_ascii_lowercase(); + let find = |code: &str| { + notebooks.iter().find_map(|notebook| { + let sections = std::fs::read_dir(notebook).ok()?; + sections.flatten().map(|e| e.path()).find_map(|section| { + let name = section.file_name()?.to_string_lossy().to_ascii_lowercase(); + let page = section.join("how-to").join("handwriting-and-drawing"); + (name.ends_with(&format!("-{code}")) && page.is_dir()).then_some(page) + }) + }) + }; + find(&base).or_else(|| find("en")) +} + /// File > Save a Copy's folder chooser (issue #301). A browser has no folder dialog, so the web /// build chooses nothing and the folder is typed instead. Kept here, below the other choosers, /// and gated in its own right: the web build's rules rewrite the dialogs above by where they sit. @@ -10749,6 +10897,22 @@ fn pick_copy_folder() -> Option { mod alert_tests { use super::*; + #[test] + fn ink_help_finds_the_drawing_page_in_the_welcome_notebook() { + // Issue #296, against the checked-in Welcome notebook in both formats. + let demo = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../docs/demo/DemoNotebook"); + for format in ["html", "cmgfm"] { + let notebook = demo.join(format); + let english = drawing_help_page(std::slice::from_ref(¬ebook), "en").expect("english page"); + assert!(english.ends_with(Path::new("10-en/how-to/handwriting-and-drawing")), "{english:?}"); + let arabic = drawing_help_page(std::slice::from_ref(¬ebook), "ar-XB").expect("arabic page"); + assert!(arabic.ends_with(Path::new("20-ar/how-to/handwriting-and-drawing")), "{arabic:?}"); + let unknown = drawing_help_page(std::slice::from_ref(¬ebook), "xx").expect("falls back"); + assert_eq!(unknown, english, "a language with no section falls back to English"); + } + assert_eq!(drawing_help_page(&[demo.join("no-such-notebook")], "en"), None); + } + #[test] fn portable_control_snapshots_unsaved_text_without_saving_the_source() { let t = notecraft_store::testing::TempDir::new("portable-control"); diff --git a/crates/ui-egui/src/inkpad.rs b/crates/ui-egui/src/inkpad.rs index b8d86d20..4486b357 100644 --- a/crates/ui-egui/src/inkpad.rs +++ b/crates/ui-egui/src/inkpad.rs @@ -290,6 +290,11 @@ impl Pad { self.drawing.strokes.len() } + /// The readings of the newest stroke, as `[x, y]` in page points, for `ui.inspect`. + pub fn last_stroke(&self) -> Vec<[f32; 2]> { + self.drawing.strokes.last().map(|s| s.dabs.iter().map(|d| [d.x, d.y]).collect()).unwrap_or_default() + } + pub fn is_empty(&self) -> bool { self.drawing.is_empty() && self.drawing_now.is_none() } @@ -567,6 +572,12 @@ pub fn show( painter.add(egui::Shape::line(path, egui::Stroke::new(1.2, t.accent))); } + // The ruler is drawn whatever is in hand, and handled only while an ink tool owns the + // page: with the caret as the pointer a press belongs to the words (issue #293). + if pen.pointer == Pointer::Write { + ruler_frame(ui, &painter, body, origin, zoom, t, false); + } + if pen.pointer == Pointer::Write { // A selection box being drawn: the writing it would take is outlined, so it can be aimed. if let Some(shown) = crate::containers::selection_preview(ui.ctx()) { @@ -607,6 +618,10 @@ pub fn show( ui.ctx().set_dragged_id(layer); } let resp = ui.interact(hit, layer, Sense::click_and_drag()); + // A press on the ruler moves or turns it, and draws nothing. + if ruler_frame(ui, &painter, body, origin, zoom, t, matches!(pen.pointer, Pointer::Draw | Pointer::Shape(_))) { + return false; + } if pen.pointer == Pointer::Laser { laser(ui, &painter, body, origin, zoom, resp.is_pointer_button_down_on()); return false; @@ -638,6 +653,19 @@ pub fn show( Pointer::Draw => { if down && pad.drawing_now.is_none() { pad.drawing_now = Some(Stroke::with_gain(pen.tool, pen.colour, pen.width, pen.gain)); + // Begun just outside one of the ruler's edges: the stroke is laid along it. + let edge = match (ruler(ui.ctx()), at) { + (Some(RulerState::Placed(r)), Some((x, y))) => r.edge_near(x, y, notecraft_ink::ruler::REACH / zoom.max(0.25)), + _ => None, + }; + ui.ctx().data_mut(|d| match edge { + Some(e) => { + d.insert_temp(ruler_edge_id(), e); + } + None => { + d.remove::(ruler_edge_id()); + } + }); } if let Some(stroke) = pad.drawing_now.as_mut() { // Every movement the pointer made this frame, not only where it ended: a pen @@ -647,18 +675,21 @@ pub fn show( // frame the pen is lifted in still counts the movements made before it lifted. let moves = frame_moves(ui, body, origin, zoom, pressure); for (x, y, force) in &moves { - stroke.add(Dab::new(*x, *y, *force)); + let (x, y) = along_ruler(ui.ctx(), *x, *y, pen.width); + stroke.add(Dab::new(x, y, *force)); } // A pen put down and held still moves nowhere: where it rests is the point. if let Some((x, y)) = at.filter(|_| down && moves.is_empty()) { + let (x, y) = along_ruler(ui.ctx(), x, y, pen.width); stroke.add(Dab::new(x, y, pressure)); } // The stroke is painted as it grows, so the line follows the pen rather than // appearing when it is lifted. ui.ctx().request_repaint(); } + // Lifted: the hand's tremor is taken out before the stroke is kept (#44). if !down && let Some(stroke) = pad.drawing_now.take() { - changed = pad.drawing.add(stroke); + changed = pad.drawing.add(stroke.settled()); } } Pointer::Shape(shape) => { @@ -849,6 +880,182 @@ pub const LASER: egui::Color32 = egui::Color32::from_rgb(235, 30, 45); /// How long a laser trail takes to fade, in seconds. pub const LASER_FADE: f64 = 0.8; +fn ruler_id() -> egui::Id { + egui::Id::new("ink-ruler") +} + +fn ruler_grab_id() -> egui::Id { + egui::Id::new("ink-ruler-grab") +} + +fn ruler_edge_id() -> egui::Id { + egui::Id::new("ink-ruler-edge") +} + +/// What a press on the ruler took hold of (issue #293). +#[derive(Clone, Copy, Debug, PartialEq)] +enum RulerGrab { + /// The ruler itself, this far (page points) from where the press landed. + Move(f32, f32), + /// Its round handle, which turns it about its middle. + Turn, +} + +/// Draw > Ruler's state (issue #293): whether it is on, and once it has been drawn, where it +/// lies. Kept in the window's memory, never in the page. +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum RulerState { + /// Turned on, but not yet placed: the next frame puts it in the middle of the view. + Unplaced, + Placed(notecraft_ink::ruler::Ruler), +} + +/// The ruler, if it is out. +pub fn ruler(ctx: &egui::Context) -> Option { + ctx.data(|d| d.get_temp::(ruler_id())) +} + +/// Puts the ruler out or away, as the window's Draw > Ruler button says (issue #293). Putting +/// it out again after it was put away places it afresh in the middle of the view. +pub fn set_ruler(ctx: &egui::Context, on: bool) { + if ruler(ctx).is_some() == on { + return; + } + ctx.data_mut(|d| { + if on { + d.insert_temp(ruler_id(), RulerState::Unplaced); + } else { + d.remove::(ruler_id()); + } + d.remove::(ruler_grab_id()); + d.remove::(ruler_edge_id()); + }); +} + +/// Paints the ruler at `ruler`, with its scale, the angle and the turning handle. +#[allow(clippy::cast_precision_loss, clippy::cast_possible_truncation)] +fn paint_ruler(painter: &egui::Painter, origin: egui::Pos2, zoom: f32, ruler: ¬ecraft_ink::ruler::Ruler, t: &Tokens) { + use notecraft_ink::ruler::{HANDLE_R, LENGTH, WIDTH}; + let put = |(x, y): (f32, f32)| origin + vec2(x * zoom, y * zoom); + let corners: Vec = ruler.corners().into_iter().map(put).collect(); + let body = crate::theme::mix(t.panel, t.text, 0.08).gamma_multiply(0.86); + painter.add(egui::Shape::convex_polygon(corners, body, egui::Stroke::new(1.0, crate::theme::mix(t.panel, t.text, 0.55)))); + // A scale in centimetres along both edges: a long mark each centimetre, a short one + // each half. + let cm = 72.0 / 2.54; + let marks = (LENGTH / cm * 2.0) as i32; + let ink = crate::theme::mix(t.panel, t.text, 0.75); + for i in 0..=marks { + let along = -LENGTH / 2.0 + i as f32 * cm / 2.0; + if along > LENGTH / 2.0 { + break; + } + let long = i % 2 == 0; + let depth = if long { 9.0 } else { 5.0 }; + for (edge, inward) in [(-WIDTH / 2.0, 1.0), (WIDTH / 2.0, -1.0)] { + painter.line_segment([put(ruler.to_page(along, edge)), put(ruler.to_page(along, edge + inward * depth))], egui::Stroke::new(1.0, ink)); + } + } + // The angle, in the middle, turned with the ruler so it reads along it. + let label = format!("{}\u{b0}", ruler.shown_degrees()); + let galley = painter.layout_no_wrap(label, egui::FontId::proportional((13.0 * zoom).clamp(9.0, 22.0)), t.text); + let size = galley.size(); + let angle = ruler.angle.to_radians(); + let centre = put((ruler.x, ruler.y)); + let rot = egui::emath::Rot2::from_angle(angle); + let at = centre - rot * (size / 2.0); + painter.add(egui::epaint::TextShape::new(at, galley, t.text).with_angle(angle)); + let (hx, hy) = ruler.handle(); + painter.circle(put((hx, hy)), HANDLE_R * zoom, t.accent.gamma_multiply(0.35), egui::Stroke::new(1.5, t.accent)); +} + +/// The ruler's part of a frame while an ink tool is in hand (issue #293). Returns true when +/// the ruler took the pointer this frame (it is being moved or turned), so nothing draws. +fn ruler_frame(ui: &Ui, painter: &egui::Painter, body: egui::Rect, origin: egui::Pos2, zoom: f32, t: &Tokens, owns_pointer: bool) -> bool { + use notecraft_ink::ruler::Ruler; + let Some(state) = ruler(ui.ctx()) else { return false }; + let zoom = zoom.max(0.01); + let to_page = |p: egui::Pos2| ((p.x - origin.x) / zoom, (p.y - origin.y) / zoom); + let mut placed = match state { + RulerState::Placed(r) => r, + RulerState::Unplaced => { + let (x, y) = to_page(body.center()); + Ruler::at(x, y) + } + }; + let mut took = false; + if owns_pointer { + let (pressed, down, at) = ui.input(|i| (i.pointer.primary_pressed(), i.pointer.primary_down(), i.pointer.interact_pos())); + let at = at.filter(|p| body.contains(*p)).map(to_page); + if pressed && let Some((x, y)) = at { + let grab = if placed.on_handle(x, y) { + Some(RulerGrab::Turn) + } else if placed.contains(x, y) { + Some(RulerGrab::Move(placed.x - x, placed.y - y)) + } else { + None + }; + ui.ctx().data_mut(|d| match grab { + Some(g) => { + d.insert_temp(ruler_grab_id(), g); + } + None => { + d.remove::(ruler_grab_id()); + } + }); + } + let grab = ui.ctx().data(|d| d.get_temp::(ruler_grab_id())); + if let Some(grab) = grab { + took = true; + if down { + if let Some((x, y)) = at { + match grab { + RulerGrab::Move(dx, dy) => (placed.x, placed.y) = (x + dx, y + dy), + RulerGrab::Turn => placed.point_at(x, y), + } + } + ui.ctx().set_cursor_icon(egui::CursorIcon::Grabbing); + } else { + ui.ctx().data_mut(|d| d.remove::(ruler_grab_id())); + } + } else if let Some((x, y)) = at.filter(|&(x, y)| placed.contains(x, y)) { + ui.ctx().set_cursor_icon(if placed.on_handle(x, y) { egui::CursorIcon::Alias } else { egui::CursorIcon::Grab }); + } + // [ and ] turn it a degree, with Shift fifteen; nothing else uses these keys while + // an ink tool is in hand. + let focus_free = ui.ctx().memory(|m| m.focused().is_none()); + if focus_free { + let turn = ui.input(|i| { + let step = if i.modifiers.shift { 15.0 } else { 1.0 }; + let mut turn = 0.0; + if i.key_pressed(egui::Key::OpenBracket) { + turn -= step; + } + if i.key_pressed(egui::Key::CloseBracket) { + turn += step; + } + turn + }); + if turn != 0.0 { + placed.turn(turn); + } + } + } + ui.ctx().data_mut(|d| d.insert_temp(ruler_id(), RulerState::Placed(placed))); + paint_ruler(painter, origin, zoom, &placed, t); + took +} + +/// Where a reading lands with the ruler out: along the edge the stroke began beside, or +/// where the pen is when the stroke did not begin beside it (issue #293). +fn along_ruler(ctx: &egui::Context, x: f32, y: f32, width: f32) -> (f32, f32) { + let edge = ctx.data(|d| d.get_temp::(ruler_edge_id())); + match (ruler(ctx), edge) { + (Some(RulerState::Placed(r)), Some(edge)) => r.along(edge, x, y, width), + _ => (x, y), + } +} + fn laser_trail_id() -> egui::Id { egui::Id::new("laser-trail") } @@ -959,8 +1166,10 @@ pub fn paint_stroke(painter: &egui::Painter, origin: egui::Pos2, stroke: &Stroke } // Only the outward halves belong to the caps. Full circles would paint translucent // ink twice over the ribbon's ends, leaving darker patches within one highlighter stroke. + // Facing the way the edges arrive, read over the same reach as `shape::sides` (#44). let line = shape::centre(stroke); - if let [first, next, ..] = line.as_slice() { + let reach = shape::heading_reach(stroke).min(line.len().saturating_sub(1)).max(1); + if let (Some(first), Some(next)) = (line.first(), line.get(reach)) { round_cap( &mut mesh, put(first.x, first.y), @@ -971,7 +1180,7 @@ pub fn paint_stroke(painter: &egui::Painter, origin: egui::Pos2, stroke: &Stroke colour, ); } - if let [.., before, last] = line.as_slice() { + if let (Some(before), Some(last)) = (line.len().checked_sub(1 + reach).and_then(|i| line.get(i)), line.last()) { round_cap( &mut mesh, put(last.x, last.y), diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index ef8ce8bf..094d51a5 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -46,6 +46,7 @@ pub mod input_language { pub mod inkpad; pub mod mathview; pub mod navhistory; +pub mod onlinevideo; pub mod pageedit; pub mod pagemenu; pub mod pagetext; @@ -55,6 +56,7 @@ pub mod portable; pub mod preview; pub(crate) mod printout; pub mod quickaccess; +pub mod reader; pub mod ribbon; pub mod ribbon_icons; pub mod saver; diff --git a/crates/ui-egui/src/onlinevideo.rs b/crates/ui-egui/src/onlinevideo.rs new file mode 100644 index 00000000..9ccf7f6b --- /dev/null +++ b/crates/ui-egui/src/onlinevideo.rs @@ -0,0 +1,138 @@ +//! Insert > Online Video (issue #284). +//! +//! A video on the web goes into the page as a link: a line that says it is a video, from +//! which site, and the address. Nothing is fetched or played while the page is open; the +//! video plays only when the user clicks the link, in their own browser. So a page with a +//! video in it opens offline, saves the same in HTML and CMGFM (a link is ordinary page +//! text), and never reaches out to a site on its own. +//! +//! The sites below are recognised by name and their addresses are tidied to the plain +//! watch address. Any other `https` address is still accepted, as a video link from that +//! site, because many sites host video and the user knows what they pasted. + +/// A video address that may go into a page. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Video { + /// The site, as shown in the line: "YouTube", "Vimeo", or the host name. + pub site: String, + /// The address the link opens. + pub address: String, +} + +/// Reads what the user pasted. `None` when it is not a web address that can be linked to. +pub fn read(pasted: &str) -> Option