diff --git a/crates/ui-contract/src/contract/ghost.rs b/crates/ui-contract/src/contract/ghost.rs index 20422df50..5933c9851 100644 --- a/crates/ui-contract/src/contract/ghost.rs +++ b/crates/ui-contract/src/contract/ghost.rs @@ -5,6 +5,7 @@ //! the pointer during a gesture paint here too, for the pass that asks. use crate::primitives::pointer_art::Art; +use crate::primitives::pointer_art::pointer_art; use crate::primitives::{Draw, DrawEnv, PaintRecord, PaintShape, RuleKind, Span, TextRun, outline}; use crate::runtime::drag::{self, DragPreview, DropMark}; use crate::runtime::egui::areas; @@ -108,10 +109,10 @@ fn paint_pointer(ctx: &Context, env: &Env, painter: &egui::Painter) { let Some(Cursor::Painted(cursor)) = cursor::current(ctx) else { return }; let closed = pointer::frame(ctx).buttons.contains(craft_behavior::Buttons::PRIMARY); let tip = matches!(cursor, PaintedCursor::Circle { .. } | PaintedCursor::Outline { .. }); - let whole = cursor::pointer_art(env, &cursor, closed, false); + let whole = pointer_art(env, &cursor, closed, false); let confined = cursor::current_region(ctx).filter(|region| tip && !shows_whole(ctx, region, &whole, at)); let ring = |painter: &egui::Painter| { - let art = cursor::pointer_art(env, &cursor.clone().marked(false, None), closed, false); + let art = pointer_art(env, &cursor.clone().marked(false, None), closed, false); match confined { Some(region) => paint_in_region(ctx, region, &art, at), None => art.paint(painter, at), @@ -122,7 +123,7 @@ fn paint_pointer(ctx: &Context, env: &Env, painter: &egui::Painter) { if confined.is_none() && cursor::show_image(ctx, &whole, target) { return; } - if tip && cursor::show_image(ctx, &cursor::pointer_art(env, &cursor, closed, true), target) { + if tip && cursor::show_image(ctx, &pointer_art(env, &cursor, closed, true), target) { ring(painter); return; } @@ -130,7 +131,7 @@ fn paint_pointer(ctx: &Context, env: &Env, painter: &egui::Painter) { match confined { Some(_) => { ring(painter); - cursor::pointer_art(env, &cursor, closed, true).paint(painter, at); + pointer_art(env, &cursor, closed, true).paint(painter, at); } None => whole.paint(painter, at), } diff --git a/crates/ui-contract/src/contract/items.rs b/crates/ui-contract/src/contract/items.rs index bae80f142..08746b3d1 100644 --- a/crates/ui-contract/src/contract/items.rs +++ b/crates/ui-contract/src/contract/items.rs @@ -319,3 +319,14 @@ fn key_hash(key: &K) -> u64 { key.hash(&mut hasher); hasher.finish() } + +// `StyleCx::face` sits here, with the measuring it calls, not in `style`: `style` is below `contract`. +impl crate::style::StyleCx<'_> { + /// The size `face` has, measured from the `Ui` the collection hook fills this context from, so a collection's + /// `item_layout` measures a button's label and icon in the pass it lays out in. Zero in a context no collection + /// filled, which has nothing to measure in. + pub fn face(&self, mut face: S) -> egui::Vec2 { + let Some(ui) = self.measuring else { return egui::Vec2::ZERO }; + crate::contract::measure_under(&mut face, ui, None, craft_behavior::Proposal::Ideal, None) + } +} diff --git a/crates/ui-contract/src/primitives/mod.rs b/crates/ui-contract/src/primitives/mod.rs index 06b98fdb2..dc0d78af9 100644 --- a/crates/ui-contract/src/primitives/mod.rs +++ b/crates/ui-contract/src/primitives/mod.rs @@ -45,6 +45,7 @@ pub use kit::{ }; pub use marker::{Marker, MarkerSize}; pub use paint_slot::PaintSlot; +pub use pointer_art::painted_image; pub use reset_mark::ResetMark; pub use rule::{RectSide, Rule, RuleKind}; pub use scroll_bar::{HiddenContent, ScrollBar}; diff --git a/crates/ui-contract/src/primitives/pointer_art.rs b/crates/ui-contract/src/primitives/pointer_art.rs index 5b24a1da6..0fd7be528 100644 --- a/crates/ui-contract/src/primitives/pointer_art.rs +++ b/crates/ui-contract/src/primitives/pointer_art.rs @@ -5,7 +5,11 @@ mod art; +use super::Mirror; use super::SvgIcon; +use crate::runtime::cursor::{CursorArt, CursorBitmap, MAX_SIDE}; +use crate::runtime::env::Env; +use crate::style::Ink; use crate::style::UiTokens; pub(crate) use art::Art; use art::Mark; @@ -15,6 +19,68 @@ use egui::{Color32, Pos2, Rect, Stroke, Vec2}; use std::cell::RefCell; use std::f32::consts::{FRAC_PI_2, PI, TAU}; +/// The window image of the painted pointer `cursor` at `scale` device pixels per point, rasterised from the art the +/// ghost paints; `None` when the art does not fit a cursor image. +pub fn painted_image(env: &Env, cursor: &PaintedCursor, scale: f32) -> Option { + pointer_art(env, cursor, false, false).rasterise(scale, MAX_SIDE) +} + +/// The art of `cursor`: the hand as a fist while `closed`; with `marks_only`, a brush tip's centre cross and badge +/// without its ring. The ghost paints it and the window's cursor image is rasterised from it, so both look the same. +pub(crate) fn pointer_art(env: &Env, cursor: &PaintedCursor, closed: bool, marks_only: bool) -> Art { + use craft_behavior::PointerGlyph; + let overlay = &env.tokens.color.overlay; + let pen = Pen { + art: RefCell::new(Art::default()), + fill: crate::style::rgba_color(overlay.stroke), + halo: crate::style::rgba_color(overlay.halo), + thin: env.tokens.stroke.thin, + rtl: env.direction.is_rtl(), + }; + let ink = crate::style::ink_color(&env.tokens, Ink::OnDark); + let edge = env.tokens.stroke.thin; + match cursor { + PaintedCursor::Outline { rings, centre, badge, .. } => { + if !marks_only { + outline(&pen, rings); + } + tip_marks(&pen, *centre || marks_only, *badge, pen.thin, pen.fill); + } + PaintedCursor::Circle { radius, centre, badge, .. } => { + if !marks_only { + circle(&pen, *radius, edge, ink); + } + tip_marks(&pen, *centre || marks_only, *badge, edge, ink); + } + PaintedCursor::Crosshair => crosshair(&pen, env.tokens.size.icon.sm, edge, ink), + PaintedCursor::Hand => hand(&pen, closed), + PaintedCursor::Zoom { out, .. } => zoom(&pen, *out), + PaintedCursor::Tool { icon, hotspot, badge, .. } => { + let side = env.tokens.size.icon.md; + let icon = SvgIcon::of(&env.tokens, env.direction, side, icon, Mirror::Auto); + tool(&pen, icon, *hotspot, *badge, side); + } + PaintedCursor::Badged { badge, .. } => badge_beside(&pen, *badge), + PaintedCursor::Turn { toward, .. } => turn(&pen, *toward), + PaintedCursor::Distort { dir, .. } => distort(&pen, *dir), + PaintedCursor::Skew { dir, .. } => skew(&pen, *dir), + PaintedCursor::TypeOn { path, .. } => type_on(&pen, *path), + PaintedCursor::IBeam => i_beam_pointer(&pen), + PaintedCursor::Locator => locator(&pen), + PaintedCursor::Arrow { hollow, toward, .. } => arrow(&pen, *hollow, *toward), + PaintedCursor::PathText { start, end, .. } => path_text(&pen, *start, *end), + PaintedCursor::PointerTool { pointer, icon, .. } => { + // The arrow's icon sits under its body, so it takes the larger size to show past the arrow. + let side = if *pointer == PointerGlyph::Arrow { env.tokens.size.icon.md } else { env.tokens.size.icon.sm }; + let icon = SvgIcon::of(&env.tokens, env.direction, side, icon, Mirror::Auto); + pointer_tool(&pen, *pointer, icon, side, edge, ink); + } + // `Hidden` has no marks. + _ => {} + } + pen.art.into_inner() +} + /// The icon sets draw on a box this many units on a side; a tool pointer's hotspot is in these. const ICON_UNITS: f32 = 24.0; /// Where a badge sits beside a system pointer arrow: toward its lower right. diff --git a/crates/ui-contract/src/primitives/text_input.rs b/crates/ui-contract/src/primitives/text_input.rs index 32b69fc5e..4eaa6c044 100644 --- a/crates/ui-contract/src/primitives/text_input.rs +++ b/crates/ui-contract/src/primitives/text_input.rs @@ -296,25 +296,7 @@ pub struct EditorState<'a> { pub surrounding: Option<(u64, EditorSurrounding<'a>)>, } -/// The committed window around the caret that an editor reports to the input method, or why there is none. -#[derive(Clone, Debug)] -#[non_exhaustive] -pub enum EditorSurrounding<'a> { - /// The text around the caret, with UTF-8 offsets of the cursor and the selection anchor into it. - Text { - /// The committed text. - text: &'a str, - /// The cursor's byte offset into `text`. - cursor: usize, - /// The selection anchor's byte offset into `text`. - anchor: usize, - }, - /// The editor has no text to report. - Unavailable { - /// The revision the editor reports it at, if it keeps one. - revision: Option, - }, -} +pub use crate::runtime::ime::EditorSurrounding; /// The input method's mutation stamp, which an editor compares to tell its own edits from the input method's. pub use egui::ime_context::ImeMutationStamp as ImeStamp; diff --git a/crates/ui-contract/src/runtime/cursor.rs b/crates/ui-contract/src/runtime/cursor.rs index 2b774b299..f8496a45a 100644 --- a/crates/ui-contract/src/runtime/cursor.rs +++ b/crates/ui-contract/src/runtime/cursor.rs @@ -1,27 +1,22 @@ //! The cursor service (8.5): requests of a pass collect here, the end hook picks the winner and records it, and a //! painted winner reaches the window as its cursor image where the window shows one. -use crate::primitives::pointer_art::{self as pointers, Art}; -use crate::primitives::{Mirror, SvgIcon}; use crate::runtime::egui::areas::LayerKey; use crate::runtime::egui::{output, pass, viewports}; -use crate::runtime::env::Env; use crate::runtime::frame::PerViewport; use crate::runtime::windows; -use crate::style::Ink; use craft_behavior::cursor::pick_index; use craft_behavior::{Cursor, CursorIcon, CursorPriority, PaintedCursor}; use craft_host::target::Os; use egui::{Context, Rect, ViewportId}; use std::any::Any; -use std::cell::RefCell; use std::sync::Arc; /// The largest side of a cursor image in device pixels on Linux, where X servers and hardware cursor planes commonly /// cap cursors at 256 and fall back to slow software cursors, or reject them, past that. const MAX_SIDE_LINUX: u16 = 255; /// The largest side elsewhere: winit's limit; AppKit sets none on an `NSCursor` image. -const MAX_SIDE: u16 = 2048; +pub(crate) const MAX_SIDE: u16 = 2048; /// A cursor image in straight (not premultiplied) RGBA, row by row, its hotspot on the centre pixel. #[derive(Clone, Debug, PartialEq)] @@ -75,68 +70,6 @@ pub(crate) struct State { image: Option<(Box, f32, CursorBitmap)>, } -/// The window image of the painted pointer `cursor` at `scale` device pixels per point, rasterised from the art the -/// ghost paints; `None` when the art does not fit a cursor image. -pub fn painted_image(env: &Env, cursor: &PaintedCursor, scale: f32) -> Option { - pointer_art(env, cursor, false, false).rasterise(scale, MAX_SIDE) -} - -/// The art of `cursor`: the hand as a fist while `closed`; with `marks_only`, a brush tip's centre cross and badge -/// without its ring. The ghost paints it and the window's cursor image is rasterised from it, so both look the same. -pub(crate) fn pointer_art(env: &Env, cursor: &PaintedCursor, closed: bool, marks_only: bool) -> Art { - use craft_behavior::PointerGlyph; - let overlay = &env.tokens.color.overlay; - let pen = pointers::Pen { - art: RefCell::new(Art::default()), - fill: crate::style::rgba_color(overlay.stroke), - halo: crate::style::rgba_color(overlay.halo), - thin: env.tokens.stroke.thin, - rtl: env.direction.is_rtl(), - }; - let ink = crate::style::ink_color(&env.tokens, Ink::OnDark); - let edge = env.tokens.stroke.thin; - match cursor { - PaintedCursor::Outline { rings, centre, badge, .. } => { - if !marks_only { - pointers::outline(&pen, rings); - } - pointers::tip_marks(&pen, *centre || marks_only, *badge, pen.thin, pen.fill); - } - PaintedCursor::Circle { radius, centre, badge, .. } => { - if !marks_only { - pointers::circle(&pen, *radius, edge, ink); - } - pointers::tip_marks(&pen, *centre || marks_only, *badge, edge, ink); - } - PaintedCursor::Crosshair => pointers::crosshair(&pen, env.tokens.size.icon.sm, edge, ink), - PaintedCursor::Hand => pointers::hand(&pen, closed), - PaintedCursor::Zoom { out, .. } => pointers::zoom(&pen, *out), - PaintedCursor::Tool { icon, hotspot, badge, .. } => { - let side = env.tokens.size.icon.md; - let icon = SvgIcon::of(&env.tokens, env.direction, side, icon, Mirror::Auto); - pointers::tool(&pen, icon, *hotspot, *badge, side); - } - PaintedCursor::Badged { badge, .. } => pointers::badge_beside(&pen, *badge), - PaintedCursor::Turn { toward, .. } => pointers::turn(&pen, *toward), - PaintedCursor::Distort { dir, .. } => pointers::distort(&pen, *dir), - PaintedCursor::Skew { dir, .. } => pointers::skew(&pen, *dir), - PaintedCursor::TypeOn { path, .. } => pointers::type_on(&pen, *path), - PaintedCursor::IBeam => pointers::i_beam_pointer(&pen), - PaintedCursor::Locator => pointers::locator(&pen), - PaintedCursor::Arrow { hollow, toward, .. } => pointers::arrow(&pen, *hollow, *toward), - PaintedCursor::PathText { start, end, .. } => pointers::path_text(&pen, *start, *end), - PaintedCursor::PointerTool { pointer, icon, .. } => { - // The arrow's icon sits under its body, so it takes the larger size to show past the arrow. - let side = if *pointer == PointerGlyph::Arrow { env.tokens.size.icon.md } else { env.tokens.size.icon.sm }; - let icon = SvgIcon::of(&env.tokens, env.direction, side, icon, Mirror::Auto); - pointers::pointer_tool(&pen, *pointer, icon, side, edge, ink); - } - // `Hidden` has no marks. - _ => {} - } - pen.art.into_inner() -} - /// Requests `cursor` for the running pass of this viewport. #[doc(hidden)] pub fn request(ctx: &Context, cursor: Cursor, priority: CursorPriority) { diff --git a/crates/ui-contract/src/runtime/ime.rs b/crates/ui-contract/src/runtime/ime.rs index a736d5427..0e19f1a6d 100644 --- a/crates/ui-contract/src/runtime/ime.rs +++ b/crates/ui-contract/src/runtime/ime.rs @@ -2,7 +2,6 @@ //! caret rect for the OS. While a framework editor owns the IME, the engine is the one reader of the IME events and //! the one writer of `PlatformOutput.ime`; otherwise both stay with egui and the app's own widgets. -use crate::primitives::EditorSurrounding; use crate::runtime::egui::ime as os; use crate::runtime::egui::input::Arrived; use crate::runtime::egui::pass; @@ -67,6 +66,40 @@ struct EditorFacts { /// An owner's surrounding report as the engine keeps it for one pass: an owned copy of at most /// `MAX_IME_SURROUNDING_TEXT_BYTES` around the selection, never the whole document, and the epoch of the target. +/// The committed window around the caret that an editor reports to the input method, or why there is none. +#[derive(Clone, Debug)] +#[non_exhaustive] +pub enum EditorSurrounding<'a> { + /// The text around the caret, with UTF-8 offsets of the cursor and the selection anchor into it. + #[non_exhaustive] + Text { + /// The committed text. + text: &'a str, + /// The cursor's byte offset into `text`. + cursor: usize, + /// The selection anchor's byte offset into `text`. + anchor: usize, + }, + /// The editor has no text to report. + #[non_exhaustive] + Unavailable { + /// The revision the editor reports it at, if it keeps one. + revision: Option, + }, +} + +impl<'a> EditorSurrounding<'a> { + /// The text around the caret, with byte offsets of the cursor and the selection anchor into `text`. + pub const fn text(text: &'a str, cursor: usize, anchor: usize) -> Self { + EditorSurrounding::Text { text, cursor, anchor } + } + + /// No text to report, at `revision` if the editor keeps one. + pub const fn unavailable(revision: Option) -> Self { + EditorSurrounding::Unavailable { revision } + } +} + #[derive(Clone)] struct Surrounding { epoch: u64, diff --git a/crates/ui-contract/src/style/cx.rs b/crates/ui-contract/src/style/cx.rs index 75b818ee6..db46b47fc 100644 --- a/crates/ui-contract/src/style/cx.rs +++ b/crates/ui-contract/src/style/cx.rs @@ -74,7 +74,7 @@ pub struct StyleCx<'a> { /// hook for the layout it asks of the component. owner: Option<(Id, MotionPolicy)>, /// The `Ui` a collection's layout hook measures faces in; none elsewhere. - measuring: Option<&'a egui::Ui>, + pub(crate) measuring: Option<&'a egui::Ui>, } /// A variant's look as style parts. @@ -197,14 +197,6 @@ impl<'a> StyleCx<'a> { f(&StyleCx::of_env(ctx, &env)) } - /// The size `face` has, measured from the `Ui` the collection hook fills this context from, so a collection's - /// `item_layout` measures a button's label and icon in the pass it lays out in. Zero in a context no collection - /// filled, which has nothing to measure in. - pub fn face(&self, mut face: S) -> egui::Vec2 { - let Some(ui) = self.measuring else { return egui::Vec2::ZERO }; - crate::contract::measure_under(&mut face, ui, None, craft_behavior::Proposal::Ideal, None) - } - /// The same context measuring faces in `ui`; the collection hook calls it before it asks for a layout. pub(crate) fn measured_by<'b>(&self, ui: &'b egui::Ui) -> StyleCx<'b> where diff --git a/crates/ui-contract/tests/it/text/editor.rs b/crates/ui-contract/tests/it/text/editor.rs index 59551fcf1..2178db652 100644 --- a/crates/ui-contract/tests/it/text/editor.rs +++ b/crates/ui-contract/tests/it/text/editor.rs @@ -105,7 +105,7 @@ impl Component for Probe<'_> { state.composing = doc.composing.get(); } if let Some(epoch) = doc.epoch.get() { - state = state.surrounding(epoch, EditorSurrounding::Text { text: shown, cursor: shown.len(), anchor: shown.len() }); + state = state.surrounding(epoch, EditorSurrounding::text(shown, shown.len(), shown.len())); } match cx.editor_handle().filter(|_| doc.body.get()) { Some(handle) => body(&handle, &cx.response.ctx, state), diff --git a/crates/ui-gallery/src/foundations/pointers.rs b/crates/ui-gallery/src/foundations/pointers.rs index 6d300f334..d3dfe97a2 100644 --- a/crates/ui-gallery/src/foundations/pointers.rs +++ b/crates/ui-gallery/src/foundations/pointers.rs @@ -4,7 +4,7 @@ use craft_behavior::PaintedCursor; use craft_ui::contract::{Gap, Pattern, Props, Show as _, ShowContainer as _}; use craft_ui::display::{Picture, PictureSize, Shade, Text}; use craft_ui::layout_components::{Row, Stack}; -use craft_ui::runtime::cursor::painted_image; +use craft_ui::primitives::painted_image; use craft_ui::runtime::env::Env; use craft_ui::style::TypeRole; use egui::{Color32, ColorImage, Ui};