diff --git a/crates/pages/src/ui/page_rulers.rs b/crates/pages/src/ui/page_rulers.rs index 044bc3500..4c1a45c81 100644 --- a/crates/pages/src/ui/page_rulers.rs +++ b/crates/pages/src/ui/page_rulers.rs @@ -488,7 +488,7 @@ impl<'a> PageRulers<'a> { self } - /// Shows the tab selector in the corner, with the kind a click sets. + /// Shows the tab selector in the canvas's corner, with the kind a click sets. pub fn kind(mut self, kind: Option) -> Self { self.kind = kind; self @@ -554,7 +554,7 @@ impl<'a> PageRulers<'a> { let env = Env::of(ui); (env.words::(), env.view().direction().is_rtl()) }; - let (top_strip, left_strip) = Rulers::strips(ui, canvas_rect); + let (top_strip, left_strip, corner) = Rulers::strips(ui, canvas_rect); let page_size = (f64::from(page.rect.width()) / page.scale, f64::from(page.rect.height()) / page.scale); let shown = edges.reborrow().get().and_then(|edges| edges.same().copied()).unwrap_or_default(); let rules = MarginRules::new(page_size, shown).gutter(gutter).mirrored_even(mirrored_even); @@ -565,8 +565,8 @@ impl<'a> PageRulers<'a> { let scale = (page.scale as f32).max(f32::EPSILON); let rtl = right_to_left.unwrap_or(interface_rtl); - // The ruler counts from the column's start edge and covers the page's width; both are given as screen x, and the - // ruler places its own corner square. + // The ruler counts from the column's start edge and covers the page's width; both are given as screen x. With the + // tab selector it lies along the top strip and the canvas's corner, which holds the selector. let origin = page.rect.min.x + (if rtl { column.1 } else { column.0 }) * scale; let extent = Rangef::new(page.rect.min.x, page.rect.max.x); let mut horizontal = TextRuler::new(label.clone(), ruler, (column.1 - column.0).max(0.0)) @@ -579,13 +579,15 @@ impl<'a> PageRulers<'a> { .scale(scale) .right_to_left(rtl) .id_source(id); + let mut band = top_strip; if let Some(kind) = kind { - horizontal = horizontal.kind(kind); + horizontal = horizontal.kind(kind).corner(corner); + band = top_strip.union(corner); } if let Some(face) = face { horizontal = horizontal.face(face); } - let ruler_out = horizontal.show_at(ui, top_strip); + let ruler_out = horizontal.show_at(ui, band); let vertical_label = Label::from(words.vertical.fill(&[("name", label.as_str())])); let mut vertical = VerticalRuler::new(vertical_label, page).text(Rangef::new(top as f32, bottom as f32)).unit(unit).rows(rows); diff --git a/crates/pages/src/ui/view.rs b/crates/pages/src/ui/view.rs index 8e3af84d3..f04171096 100644 --- a/crates/pages/src/ui/view.rs +++ b/crates/pages/src/ui/view.rs @@ -464,7 +464,7 @@ impl<'a> PageView<'a> { // (`ContainerProps::scroll_owner`), so the app's body runs with the clip opened over the strips again, where // its `PageRulers` show. The canvas's own `with_rulers` would show the general rulers there instead. let (canvas_rect, strips_clip) = if rulers { - let (top, left) = Rulers::strips(ui, rect); + let (top, left, _) = Rulers::strips(ui, rect); let inner = Rect::from_min_max(pos2(left.max.x.min(rect.max.x), top.max.y.min(rect.max.y)), rect.max); (inner, Some(ui.clip_rect().intersect(rect))) } else { diff --git a/crates/pages/tests/it/rulers.rs b/crates/pages/tests/it/rulers.rs index 65512b551..6b893daaf 100644 --- a/crates/pages/tests/it/rulers.rs +++ b/crates/pages/tests/it/rulers.rs @@ -10,7 +10,7 @@ use craft_pages::ui::{PageEdges, PageRulers, RowDrag, VerticalRuler}; use craft_text::ui::DocUnit; use craft_text::ui::ruler::{RulerValues, TabAlign, TabLeader, TabStop, TextRulerWords}; use craft_ui::contract::{Bind, Role, Show as _}; -use craft_ui::runtime::env::UnitKind; +use craft_ui::runtime::env::{Direction, UnitKind}; use craft_ui::testing::{Fixture, trial}; use egui::{Key, Modifiers, Rangef, Rect, pos2, vec2}; use std::cell::{Cell, RefCell}; @@ -228,3 +228,26 @@ fn a_new_salt_during_a_tab_drag_commits_nothing() { fixture.settle(); assert_eq!(commits.get(), 1, "the drag open when the paragraph changed commits nothing"); } + +/// The tab selector shows in the canvas's corner, where the top and left strips meet, in either interface direction: +/// the host places it, so it never lies over the horizontal ruler. +#[trial] +fn the_tab_selector_sits_in_the_canvas_corner_in_both_directions() { + let words = TextRulerWords::default(); + let name = words.tab_selector.fill(&[("kind", words.left_tab.as_str())]); + for direction in [Direction::Ltr, Direction::Rtl] { + let corner = Rc::new(Cell::new(Rect::NOTHING)); + let seen = corner.clone(); + let mut fixture = Fixture::new(move |ui| { + let full = craft_ui::testing::stage(ui); + seen.set(craft_ui::canvas::Rulers::strips(ui, full).2); + let page = demo_placed(1, pos2(full.min.x + PAGE_AT.0, full.min.y + PAGE_AT.1)).remove(0); + let (mut edges, mut values) = (PageEdges::new(Margins::uniform(72.0)), RulerValues::default()); + PageRulers::new("Rulers", &page, Bind::bound(&mut edges), Bind::bound(&mut values)).kind(Some(TabAlign::Left)).show_at(ui, full); + }); + fixture.direction(direction).room(vec2(900.0, 600.0)); + fixture.settle(); + let selector = fixture.by_label(&name).expect("the tab selector shows").rect; + assert!(corner.get().contains_rect(selector), "{direction:?}: the selector {selector:?} lies outside the canvas's corner {:?}", corner.get()); + } +} diff --git a/crates/text/src/ui/ruler.rs b/crates/text/src/ui/ruler.rs index 3eae19469..617c1446c 100644 --- a/crates/text/src/ui/ruler.rs +++ b/crates/text/src/ui/ruler.rs @@ -8,7 +8,7 @@ //! edit of the whole [`RulerValues`], so one drag is one undo step. //! //! ```ignore -//! let out = TextRuler::new("ruler", Bind::given(values.clone()), column_width).unit(unit).kind(tab_kind).show_at(ui, strip); +//! let out = TextRuler::new("ruler", Bind::given(values.clone()), column_width).unit(unit).kind(tab_kind).corner(corner).show_at(ui, band); //! if let Some(EditEvent::Commit { after, .. }) = out.edit.event { doc.set_paragraph_tabs(after); } //! if let Some(kind) = out.kind { tab_kind = kind; } //! if out.open_tabs_dialog { dialogs.open(TabStopsDialog::new(values.stops.clone(), default_tab)); } @@ -16,14 +16,15 @@ use super::length::DocUnit; use craft_behavior::{Axis, GesturePhase, HitLevel, HitPriority, Metrics, NavShape, Roving, Sense}; +use craft_theme::over; use craft_ui::contract::{ Access, Bind, BindMut, Common, Component, Cx, Edit as _, EditResponse, Input, MeasureCx, Name, Numeric, Outcome, PaintUi, PartsCx, Props as _, Role, WordSetCx as _, }; use craft_ui::primitives::RulerFace; use craft_ui::runtime::focus::{ActivateKeys, FocusShape, KeyUse}; -use craft_ui::style::{ComponentStyle, Flags, FocusLook, Stateful, StyleColor, StyleCx, TextLook, TypeRole}; -use egui::{Rect, Response, Vec2}; +use craft_ui::style::{ComponentStyle, Flags, FocusLook, Stateful, States, StyleColor, StyleCx, TextLook, TypeRole, UiTokens}; +use egui::{CornerRadius, Rect, Response, Vec2}; mod geometry; mod items; @@ -302,6 +303,12 @@ pub struct TextRulerStyle { /// The tick numbers. #[style(text)] label: TextLook, + /// The tab selector's box: the column's colour, the state layer over it under the pointer and pressed. + selector: Stateful, + /// The side of the tab selector's box. + selector_side: f32, + /// The rounding of the tab selector's box. + selector_radius: CornerRadius, /// The strip's depth. thickness: f32, /// The gap between a tick and its number. @@ -340,8 +347,8 @@ pub struct TextRulerStyle { /// - A click on the empty column sets a stop of the selector's kind at the snapped position (1/16 in, or the unit's /// minor tick); dragging a stop moves it; dragging it off the ruler ([`dropped_off`]) clears it on release, the stop /// drawn faded meanwhile. Each gesture is one edit of the whole values; Escape restores the start. -/// - The tab selector ([`kind`](Self::kind)) sits in the corner square at the interface's start edge of the rect: -/// click for the next kind, secondary click for the previous one. +/// - The tab selector ([`kind`](Self::kind)) sits in the corner the host gives ([`corner`](Self::corner)), where its +/// rulers meet: click for the next kind, secondary click for the previous one. /// - A table's column borders ([`columns`](Self::columns)): a grip marker on each border of the caret row; dragging /// one reports a [`ColumnDrag`] gesture, snapped like a stop (Alt free). While Alt is held in the drag, each column's /// width shows between its borders. A border is a slider too: the arrows and the screen reader's Increment, @@ -371,7 +378,7 @@ pub struct TextRuler<'a> { #[prop] gaps_draggable: bool, /// The screen x where the ruler's zero sits: a page-wide ruler is measured from the page's text origin. Unset, zero - /// is the strip's start edge. Given against the screen so the corner square, which only the ruler places, does not + /// is the strip's start edge. Given against the screen so the corner, which the rect may hold, does not /// enter the caller's arithmetic. #[prop(quantity = "screen x of the ruler's zero, a position")] origin: Option, @@ -381,9 +388,15 @@ pub struct TextRuler<'a> { /// The unit the markers' values and the ticks show. #[prop] unit: DocUnit, - /// Shows Word's tab selector in the corner, with the kind a click sets. Without it a click sets a left stop. + /// The kind of stop a click sets, which Word's tab selector shows in the [`corner`](Self::corner). Without it a + /// click sets a left stop. #[prop] kind: Option, + /// The square at one end of the rect where the host's rulers meet (the canvas rulers' corner): the tab selector + /// shows there and the strip is the rest of the rect. The host places it, so it sits where the host's corner is in + /// either direction. Without it the whole rect is the strip and no selector shows. + #[prop] + corner: Option, /// Screen points per point (the view's zoom when the ruler sits over a text frame on the canvas); without it the /// column fits into the strip. #[prop(quantity = "scale")] @@ -416,6 +429,7 @@ impl<'a> TextRuler<'a> { extent: None, unit: DocUnit::points(), kind: None, + corner: None, scale: None, right_to_left: None, selected: None, @@ -425,19 +439,18 @@ impl<'a> TextRuler<'a> { .label(label) } - /// The corner square, the strip and the markers' sizes inside `rect`. + /// The part of the host's corner inside `rect`, if any. + fn corner_in(&self, rect: Rect) -> Option { + self.corner.map(|corner| corner.intersect(rect)).filter(|corner| corner.is_positive()) + } + + /// The host's corner, the strip beside it and the markers' sizes inside `rect`. fn layout(&self, rect: Rect, style: &TextRulerStyle, interface_rtl: bool) -> Layout { - let corner_width = if self.kind.is_some() { rect.height().min(rect.width()) } else { 0.0 }; - let (corner, strip) = if interface_rtl { - ( - Rect::from_min_max(egui::pos2(rect.max.x - corner_width, rect.min.y), rect.max), - Rect::from_min_max(rect.min, egui::pos2(rect.max.x - corner_width, rect.max.y)), - ) - } else { - ( - Rect::from_min_max(rect.min, egui::pos2(rect.min.x + corner_width, rect.max.y)), - Rect::from_min_max(egui::pos2(rect.min.x + corner_width, rect.min.y), rect.max), - ) + let corner = self.corner_in(rect); + let strip = match corner { + Some(corner) if corner.center().x < rect.center().x => Rect::from_min_max(egui::pos2(corner.max.x, rect.min.y), rect.max), + Some(corner) => Rect::from_min_max(rect.min, egui::pos2(corner.min.x, rect.max.y)), + None => rect, }; let fit = if self.width > 0.0 { strip.width() / self.width } else { 1.0 }; let scale = self.scale.filter(|scale| scale.is_finite() && *scale > 0.0).unwrap_or(fit).max(f32::EPSILON); @@ -476,11 +489,11 @@ impl<'a> TextRuler<'a> { } } -/// The corner, the strip with its column and the marker sizes. +/// The host's corner, the strip with its column and the marker sizes. pub(super) struct Layout { g: Geometry, shapes: Shapes, - corner: Rect, + corner: Option, } impl Component for TextRuler<'_> { @@ -507,6 +520,13 @@ impl Component for TextRuler<'_> { marker_off: cx.color(colors.text.faint), marker: Stateful::new(cx.color(colors.text.normal)).when(Flags::SELECTED, cx.color(colors.accent.strong)), label: TextLook::new(TypeRole::Caption, Stateful::new(cx.color(colors.text.muted))), + selector: { + let (rest, column) = (cx.color(colors.surface.sunken), colors.surface.sunken); + let (hover, pressed) = (cx.color(over(colors.state.hover, column)), cx.color(over(colors.state.pressed, column))); + Stateful::new(rest).off(States::new(rest, hover, pressed, rest)) + }, + selector_side: tokens.size.icon.sm, + selector_radius: UiTokens::corner(tokens.shape.radius.sm), thickness: tokens.typography.get(TypeRole::Caption).line_height, pad: tokens.space.gap.tight, marker_width: tokens.size.handle.md, @@ -578,8 +598,8 @@ impl Component for TextRuler<'_> { } } cx.roving(Roving::new(NavShape::Flat).page(Some(1)).home_end(false)); - if self.kind.is_some() { - cx.part(SELECTOR, layout.corner) + if let (Some(_), Some(corner)) = (self.kind, layout.corner) { + cx.part(SELECTOR, corner) .sense(Sense::CLICK | Sense::CONTEXT_CLICK) .priority(handle) .pointer_only("the Tabs dialog sets the alignment of a stop"); @@ -615,7 +635,7 @@ impl Component for TextRuler<'_> { node.name = Name::Text(words.table_column.as_str().to_owned()); node.numeric = Some(Numeric::new(at, range.clone()).step(step).text(text.format_value(&numbers, at, 0.0))); } - if let Some(kind) = self.kind { + if let Some(kind) = self.kind.filter(|_| self.corner_in(cx.rect).is_some()) { let name = Self::tab_name(&words, kind); let node = access.node(SELECTOR, Role::BUTTON); node.name = Name::Text(words.tab_selector.fill(&[("kind", name)])); diff --git a/crates/text/src/ui/ruler/paint.rs b/crates/text/src/ui/ruler/paint.rs index 8d51889cf..7c1365b6e 100644 --- a/crates/text/src/ui/ruler/paint.rs +++ b/crates/text/src/ui/ruler/paint.rs @@ -1,7 +1,7 @@ //! The ruler's drawing. use super::geometry::glyph; -use super::items::Item; +use super::items::{Item, SELECTOR}; use super::run::{Held, ruler_text}; use super::{TextRuler, TextRulerStyle}; use craft_ui::canvas::{RulerTick, Rulers, TickRule}; @@ -95,18 +95,23 @@ impl TextRuler<'_> { let end = shapes.right(g.strip, right); triangle(end.left_bottom(), end.right_bottom(), pos2(right, end.min.y), Item::Right); - if let Some(kind) = self.kind { - let corner = layout.corner; - draw.fill(&rect(corner), style.fill); - let side = style.thickness.min(corner.width()).min(corner.height()); - let boxed = Rect::from_center_size(corner.center(), vec2(side, side)); - draw.stroke(&rect(boxed), StrokeWidth::Thin, style.tick); - let (segments, dot) = glyph(&shapes, boxed.center().x, boxed.max.y - (boxed.height() - style.stem) / 2.0, kind, false); + if let (Some(kind), Some(corner)) = (self.kind, layout.corner) { + // The corner continues the strip round it, the box is the column's colour. + let surround = if style.face == RulerFace::Centred { style.strip } else { style.margin }; + draw.fill(&rect(corner), surround); + let side = style.selector_side.min(corner.width()).min(corner.height()); + let boxed = PaintShape::Rect(Rect::from_center_size(corner.center(), vec2(side, side)), style.selector_radius); + draw.fill(&boxed, cx.resolve_part(SELECTOR, &style.selector)); + draw.stroke(&boxed, StrokeWidth::Thin, style.tick); + // The glyph is centred on the box by its own bounds: a left or right stop's foot runs to one side of its stem. + let (segments, dot) = glyph(&shapes, 0.0, 0.0, kind, false); + let bounds = Rect::from_points(&segments.iter().flatten().copied().chain(dot).collect::>()); + let shift = corner.center() - bounds.center(); for [a, b] in segments { - draw.stroke(&path(a, b), StrokeWidth::Thin, cx.resolve(&style.marker)); + draw.stroke(&path(a + shift, b + shift), StrokeWidth::Thin, cx.resolve(&style.marker)); } if let Some(centre) = dot { - draw.fill(&PaintShape::Circle(centre, style.dot), cx.resolve(&style.marker)); + draw.fill(&PaintShape::Circle(centre + shift, style.dot), cx.resolve(&style.marker)); } } } diff --git a/crates/text/src/ui/ruler/stories.rs b/crates/text/src/ui/ruler/stories.rs index 5f4b3ed7a..dd266428b 100644 --- a/crates/text/src/ui/ruler/stories.rs +++ b/crates/text/src/ui/ruler/stories.rs @@ -48,7 +48,16 @@ fn inches() -> DocUnit { fn with_stops_and_indents(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { let kind = *data.value("kind", TabAlign::Left); let (values, values_at) = data.value_at("values", word()); - let out = TextRuler::new("Ruler", values_at.bind(&mut *values), COLUMN).unit(inches()).kind(kind).show(ui); + // The selector shows in the corner the host gives, here at the band's start edge in the interface's direction. + let stage = craft_ui::testing::stage(ui); + let (strip, _, corner) = Rulers::strips(ui, stage); + let band = strip.union(corner); + let corner = if craft_ui::runtime::env::Env::of(ui).direction.is_rtl() { + egui::Rect::from_min_max(egui::pos2(band.max.x - corner.width(), band.min.y), band.max) + } else { + corner + }; + let out = TextRuler::new("Ruler", values_at.bind(&mut *values), COLUMN).unit(inches()).kind(kind).corner(corner).show_at(ui, band); if let Some(next) = out.kind { *data.value("kind", TabAlign::Left) = next; } @@ -90,7 +99,7 @@ const THREE_GAPS: [(f32, f32); 3] = [(108.0, 126.0), (225.0, 243.0), (342.0, 360 fn over_a_page(ui: &mut Ui, data: &mut StoryData, face: Option) -> StoryOutput { let (values, values_at) = data.value_at("values", word()); let stage = craft_ui::testing::stage(ui); - let (strip, _) = Rulers::strips(ui, stage); + let (strip, _, _) = Rulers::strips(ui, stage); // The page starts at the strip's start edge: its right edge in a right-to-left interface. let (origin, extent) = if craft_ui::runtime::env::Env::of(ui).direction.is_rtl() { (strip.max.x - PAGE_MARGIN, egui::Rangef::new(strip.max.x - PAGE, strip.max.x)) diff --git a/crates/text/tests/it/ruler.rs b/crates/text/tests/it/ruler.rs index 48972a46b..d2e02492e 100644 --- a/crates/text/tests/it/ruler.rs +++ b/crates/text/tests/it/ruler.rs @@ -159,7 +159,7 @@ fn ruler_numbers_skip_margins_and_markers_thin_out_mirror_for_rtl_and_keep_clear })); let mut fixture = Fixture::new(move |ui| { let stage = craft_ui::testing::stage(ui); - let (strip, _) = Rulers::strips(ui, stage); + let (strip, _, _) = Rulers::strips(ui, stage); let origin = if rtl { strip.max.x - ORIGIN * scale } else { strip.min.x + ORIGIN * scale }; TextRuler::new("Ruler", Bind::bound(&mut *values.borrow_mut()), WIDTH).origin(origin).scale(scale).right_to_left(rtl).show_at(ui, strip); }); @@ -206,7 +206,7 @@ fn a_ruler_cut_to_an_extent_paints_no_tick_past_it() { let seen = strip_at.clone(); let mut fixture = Fixture::new(move |ui| { let stage = craft_ui::testing::stage(ui); - let (strip, _) = Rulers::strips(ui, stage); + let (strip, _, _) = Rulers::strips(ui, stage); *seen.borrow_mut() = Some(strip); TextRuler::new("Ruler", Bind::bound(&mut *values.borrow_mut()), 140.0) .unit(DocUnit::new(UnitKind::Inches, 96.0)) @@ -243,7 +243,7 @@ fn the_ruler_column_is_lighter_than_its_margins() { let seen = strip_at.clone(); let mut fixture = Fixture::new(move |ui| { let stage = craft_ui::testing::stage(ui); - let (strip, _) = Rulers::strips(ui, stage); + let (strip, _, _) = Rulers::strips(ui, stage); *seen.borrow_mut() = Some(strip); TextRuler::new("Ruler", Bind::bound(&mut *values.borrow_mut()), WIDTH) .origin(strip.min.x + ORIGIN) @@ -345,7 +345,7 @@ fn page_ruler(set: ComponentSet, face: Option, popup: bool) -> (Fixtu let mut fixture = Fixture::new(move |ui| { let ruler = |ui: &mut egui::Ui| { let stage = craft_ui::testing::stage(ui); - let (strip, _) = Rulers::strips(ui, stage); + let (strip, _, _) = Rulers::strips(ui, stage); *seen.borrow_mut() = Some(strip); let mut values = values.borrow_mut(); let mut ruler = TextRuler::new("Ruler", Bind::bound(&mut *values), 432.0) diff --git a/crates/ui-canvas/src/canvas/rulers/mod.rs b/crates/ui-canvas/src/canvas/rulers/mod.rs index 4b7b35bf9..36bfd1070 100644 --- a/crates/ui-canvas/src/canvas/rulers/mod.rs +++ b/crates/ui-canvas/src/canvas/rulers/mod.rs @@ -257,13 +257,12 @@ impl<'a> Rulers<'a> { Self::content_rect_of(full, thickness(cx.tokens)) } - /// The top and the left strip inside the canvas's rect `full`, each as thick as the canvas's own rulers, for a ruler - /// of another component (a text ruler, a page ruler) that lies along them. - pub fn strips(ui: &egui::Ui, full: Rect) -> (Rect, Rect) { + /// The top strip, the left strip and the corner where they meet inside the canvas's rect `full`, each as thick as + /// the canvas's own rulers, for a ruler of another component (a text ruler, a page ruler) that lies along them. + pub fn strips(ui: &egui::Ui, full: Rect) -> (Rect, Rect, Rect) { let env = crate::runtime::env::Env::of(ui); let cx = StyleCx::of_env(ui.ctx(), &env); - let (top, left, _) = strips(full, thickness(cx.tokens)); - (top, left) + strips(full, thickness(cx.tokens)) } /// The ticks of one ruler over `values` (ruler units from the zero point): the one tick rule of every ruler, so a