diff --git a/crates/ui-canvas/src/canvas/pasteboard/mod.rs b/crates/ui-canvas/src/canvas/pasteboard/mod.rs index 8f2077e63..24f5d69b0 100644 --- a/crates/ui-canvas/src/canvas/pasteboard/mod.rs +++ b/crates/ui-canvas/src/canvas/pasteboard/mod.rs @@ -8,11 +8,10 @@ mod stories; use super::space::canvas_space; use crate::contract::{Access, Common, Component, Cx, MeasureCx, Name, NoValue, Outcome, PaintUi, PartsCx, Props as _, Role}; -use crate::primitives::{CheckerArea, PaintShape, Rule, StrokeWidth, TextRun}; +use crate::primitives::{CheckerArea, PaintShape, Rule, StrokeWidth, TextRun, paint_surround}; use crate::runtime::env::Label; use crate::style::{ ComponentStyle, DataColor, RuleLook, RuleWidth, StyleColor, StyleCx, SurfaceEdge, SurfaceKind, SurfaceStyle, SurroundLook, TypeRole, - paint_surround, }; use craft_behavior::kurbo::{Point, Rect as DocRect}; use craft_behavior::{CanvasView, HitLevel, HitPriority, HitShape, Metrics, PartKey, Sense}; diff --git a/crates/ui-canvas/src/canvas/tool_host.rs b/crates/ui-canvas/src/canvas/tool_host.rs index 1083eca96..a0f5bc7a1 100644 --- a/crates/ui-canvas/src/canvas/tool_host.rs +++ b/crates/ui-canvas/src/canvas/tool_host.rs @@ -720,7 +720,7 @@ impl<'m> ToolHost<'m> { /// controls' own gesture records in the viewport the canvas last ran in; false before the canvas's first pass. pub fn controls_pressing(ctx: &Context, canvas: Id) -> bool { let viewport = with_session(ctx, state_id(canvas), |state: &mut HostState| state.viewport); - crate::runtime::edit::member_gesture_running(ctx, viewport, canvas) + crate::primitives::member_gesture_running(ctx, viewport, canvas) } /// The canvas space the host of `canvas` used in its last pass, `None` before its first. diff --git a/crates/ui-contract/src/contract/cx/edit.rs b/crates/ui-contract/src/contract/cx/edit.rs index f7546fb8f..dcca58ad1 100644 --- a/crates/ui-contract/src/contract/cx/edit.rs +++ b/crates/ui-contract/src/contract/cx/edit.rs @@ -6,7 +6,7 @@ use crate::primitives::Adopted; use craft_behavior::{ CancelReason, EditEvent, Gesture, GestureNote, GesturePhase, Mixed, PartRef, PointerButton, PressCancel, PressPhase, Sense, Value, WheelPhase, }; -use egui::{Id, Pos2}; +use egui::Pos2; use std::sync::Arc; impl CxCore<'_> { @@ -77,10 +77,7 @@ pub trait Edit { fn cancel(&mut self, reason: CancelReason); } -/// The key of the gesture slot under a component's base id. -pub(crate) fn gesture_key(id: Id) -> Id { - crate::runtime::memory::sub(id, "edit") -} +pub(crate) use crate::runtime::edit::gesture_key; impl Cx<'_, C> { fn gesture_slot(&mut self) -> &mut Gesture> { diff --git a/crates/ui-contract/src/contract/ghost.rs b/crates/ui-contract/src/contract/ghost.rs index d0b99bf11..21174da4b 100644 --- a/crates/ui-contract/src/contract/ghost.rs +++ b/crates/ui-contract/src/contract/ghost.rs @@ -3,11 +3,7 @@ //! cursor image instead where the window takes one. They come from the drag session's data and the cursor record; the //! layer takes no input. -mod art; -mod pointers; - -pub(crate) use art::Art; -pub(crate) use pointers::paint_badge; +use crate::primitives::pointer_art::{self as pointers, Art}; use crate::primitives::{Draw, DrawEnv, IconSet, Mirror, PaintRecord, PaintShape, RuleKind, SvgIcon, TextRun, outline}; use crate::runtime::drag::{self, DragPreview, DropMark}; diff --git a/crates/ui-contract/src/contract/mod.rs b/crates/ui-contract/src/contract/mod.rs index 141006204..28b42969d 100644 --- a/crates/ui-contract/src/contract/mod.rs +++ b/crates/ui-contract/src/contract/mod.rs @@ -67,7 +67,6 @@ pub use container::{ pub use craft_ui_macros::{CommandSet, Component, Pattern, Props, register_contract}; #[doc(hidden)] pub use cx::THUMB; -pub(crate) use cx::gesture_key; pub use cx::{ Accessible, Activation, BodyCx, Cx, CxCore, Edit, Focus, ItemCx, ItemOpener, MeasureCx, PartBuilder, PartsCx, Pointer, Styled, WordSetCx, }; diff --git a/crates/ui-contract/src/primitives/icon.rs b/crates/ui-contract/src/primitives/icon.rs index 4c2c6b8a4..aad919f2f 100644 --- a/crates/ui-contract/src/primitives/icon.rs +++ b/crates/ui-contract/src/primitives/icon.rs @@ -231,7 +231,7 @@ pub(crate) fn paint(painter: &Painter, tokens: &UiTokens, direction: Direction, icon.paint(painter, square, color); } } - IconSource::Mark(badge) => crate::contract::ghost::paint_badge(painter, tokens, direction, square, *badge, color), + IconSource::Mark(badge) => super::pointer_art::paint_badge(painter, tokens, direction, square, *badge, color), } } diff --git a/crates/ui-contract/src/primitives/mod.rs b/crates/ui-contract/src/primitives/mod.rs index e6b80fd6b..028534f9f 100644 --- a/crates/ui-contract/src/primitives/mod.rs +++ b/crates/ui-contract/src/primitives/mod.rs @@ -12,6 +12,7 @@ mod icon; mod ime_surface; mod kit; mod marker; +pub(crate) mod pointer_art; mod record; mod reset_mark; mod rule; @@ -19,6 +20,7 @@ mod scroll_bar; pub(crate) mod sensing; mod state_layer; mod surface; +mod surround; mod svg_set; #[doc(hidden)] pub mod svg_tree; @@ -44,10 +46,11 @@ pub use marker::{Marker, MarkerSize}; pub use reset_mark::ResetMark; pub use rule::{RectSide, Rule, RuleKind}; pub use scroll_bar::{HiddenContent, ScrollBar}; -pub use sensing::{PartResult, TextOutcome}; +pub use sensing::{PartResult, TextOutcome, member_gesture_running}; pub use state_layer::{StateLayer, StateLayerKind}; pub use surface::Surface; pub(crate) use surface::SurfaceSlot; +pub use surround::paint_surround; pub use svg_set::{AppIcons, register_app_icons}; pub use text::{ChordText, HAlign, LayoutRun, ReadingMeasure, Span, TextLayout, TextRun, Wrap}; pub use text_input::{Completer, Completion, EditorLine, EditorPurpose, EditorRun, EditorState, TextKey, TextOptions}; diff --git a/crates/ui-contract/src/contract/ghost/pointers.rs b/crates/ui-contract/src/primitives/pointer_art.rs similarity index 95% rename from crates/ui-contract/src/contract/ghost/pointers.rs rename to crates/ui-contract/src/primitives/pointer_art.rs index e8570d1ed..5d9f1e972 100644 --- a/crates/ui-contract/src/contract/ghost/pointers.rs +++ b/crates/ui-contract/src/primitives/pointer_art.rs @@ -3,9 +3,12 @@ //! art is drawn in points around the hotspot, like an OS cursor bitmap, so its coordinates are data tables, not layout //! sizes. -use super::art::{Art, Mark}; -use crate::primitives::SvgIcon; +mod art; + +use super::SvgIcon; use crate::style::UiTokens; +pub(crate) use art::Art; +use art::Mark; use craft_behavior::{CountMark, CursorBadge, PaintedCursor}; use craft_i18n::Direction; use egui::{Color32, Pos2, Rect, Stroke, Vec2}; @@ -114,7 +117,7 @@ fn v(point: [f32; 2]) -> Vec2 { /// What the pointers draw with: the art they add marks to, the mark colour, its halo, the hairline width, and whether /// the reading direction is right to left (marks that point along it mirror). -pub(super) struct Pen { +pub(crate) struct Pen { pub art: RefCell, pub fill: Color32, pub halo: Color32, @@ -185,18 +188,18 @@ impl Pen { } /// A circle of `radius` points around the pointer. -pub(super) fn circle(pen: &Pen, radius: f32, width: f32, color: Color32) { +pub(crate) fn circle(pen: &Pen, radius: f32, width: f32, color: Color32) { pen.lines(width, color, &[Line::Circle(Vec2::ZERO, radius)]); } /// A crosshair with arms of `arm` points. -pub(super) fn crosshair(pen: &Pen, arm: f32, width: f32, color: Color32) { +pub(crate) fn crosshair(pen: &Pen, arm: f32, width: f32, color: Color32) { pen.lines(width, color, &[Line::Segment(v([-arm, 0.0]), v([arm, 0.0])), Line::Segment(v([0.0, -arm]), v([0.0, arm]))]); } /// A brush tip's marks on the pointer: the centre mark when `centre`, a crosshair of `PaintedCursor::CENTRE_ARM` arms /// in the tip's own line of `width` in `color`, and `badge` centred over it. -pub(super) fn tip_marks(pen: &Pen, centre: bool, badge: Option, width: f32, color: Color32) { +pub(crate) fn tip_marks(pen: &Pen, centre: bool, badge: Option, width: f32, color: Color32) { if centre { crosshair(pen, PaintedCursor::CENTRE_ARM, width, color); } @@ -206,7 +209,7 @@ pub(super) fn tip_marks(pen: &Pen, centre: bool, badge: Option, wid } /// Rings of a brush tip, as offsets from the pointer. -pub(super) fn outline(pen: &Pen, rings: &[Vec]) { +pub(crate) fn outline(pen: &Pen, rings: &[Vec]) { for ring in rings.iter().filter(|ring| ring.len() >= 2) { let points: Vec = ring.iter().map(|p| p.to_vec2()).collect(); pen.push(Mark::Path(points.clone(), true, Stroke::new(pen.thin * 3.0, pen.halo))); @@ -216,7 +219,7 @@ pub(super) fn outline(pen: &Pen, rings: &[Vec]) { /// The hand: open, or the fist while `closed`. Its silhouette is in the mark colour over the same grown by a hairline in /// the halo colour, with the creases between the fingers. -pub(super) fn hand(pen: &Pen, closed: bool) { +pub(crate) fn hand(pen: &Pen, closed: bool) { let shift = |p: [f32; 2]| v(p) + v(HAND_SHIFT); for (grow, color) in [(pen.thin, pen.halo), (0.0, pen.fill)] { let (center, half, round) = if closed { (FIST_CENTER, FIST_HALF, FIST_ROUND) } else { (PALM_CENTER, PALM_HALF, PALM_ROUND) }; @@ -252,7 +255,7 @@ pub(super) fn hand(pen: &Pen, closed: bool) { } /// The magnifier with a plus, or a minus when `out`. -pub(super) fn zoom(pen: &Pen, out: bool) { +pub(crate) fn zoom(pen: &Pen, out: bool) { let (a, b, r) = HANDLE; for (grow, color) in [(pen.thin, pen.halo), (0.0, pen.fill)] { pen.disc(Vec2::ZERO, LENS + grow, color); @@ -268,7 +271,7 @@ pub(super) fn zoom(pen: &Pen, out: bool) { /// A tool's `icon` of `side` points with `hotspot` (a point of the icon's 24-unit box) on the pointer, in the mark /// colour over a halo of the icon drawn a hairline off in every direction, and its badge at the icon's lower right. An /// icon the catalog lacks leaves only the badge. -pub(super) fn tool(pen: &Pen, icon: Option, hotspot: Vec2, badge: Option, side: f32) { +pub(crate) fn tool(pen: &Pen, icon: Option, hotspot: Vec2, badge: Option, side: f32) { let rect = Rect::from_min_size((-hotspot * (side / ICON_UNITS)).to_pos2(), Vec2::splat(side)); if let Some(icon) = icon { icon_on_halo(pen, rect, icon); @@ -297,7 +300,7 @@ fn i_beam() -> [Line; 3] { } /// The type tool's pointer: the I-beam inside a dotted ellipse, or crossed by a dotted wave when `path`. -pub(super) fn type_on(pen: &Pen, path: bool) { +pub(crate) fn type_on(pen: &Pen, path: bool) { let dots: Vec = if path { (0..WAVE_DOTS) .map(|i| { @@ -327,7 +330,7 @@ pub(super) fn type_on(pen: &Pen, path: bool) { } /// The locator: a square frame centred on the pointer between halo frames a hairline outside and inside it. -pub(super) fn locator(pen: &Pen) { +pub(crate) fn locator(pen: &Pen) { let rect = Rect::from_center_size(Pos2::ZERO, Vec2::splat(LOCATOR_SIDE)); let halo = Stroke::new(pen.thin, pen.halo); pen.push(Mark::Frame(rect.expand(pen.thin), halo)); @@ -337,7 +340,7 @@ pub(super) fn locator(pen: &Pen) { /// The arrow with its tip on the pointer: filled with the halo and edged in the mark colour, or the other way round when /// `hollow`. -pub(super) fn arrow(pen: &Pen, hollow: bool) { +pub(crate) fn arrow(pen: &Pen, hollow: bool) { let (fill, edge) = if hollow { (pen.fill, pen.halo) } else { (pen.halo, pen.fill) }; for triangle in ARROW_TRIANGLES { pen.push(Mark::Polygon(triangle.iter().map(|&i| v(ARROW[i])).collect(), fill, Stroke::NONE)); @@ -346,7 +349,7 @@ pub(super) fn arrow(pen: &Pen, hollow: bool) { } /// The I-beam across a path with a filled triangle toward each grabbable end: right for `start`, left for `end`. -pub(super) fn path_text(pen: &Pen, start: bool, end: bool) { +pub(crate) fn path_text(pen: &Pen, start: bool, end: bool) { pen.marks(&i_beam()); for sign in [start.then_some(1.0f32), end.then_some(-1.0)].into_iter().flatten() { pen.polygon(GRAB_TRIANGLE.iter().map(|&[x, y]| Vec2::new(x * sign, y)).collect()); @@ -356,7 +359,7 @@ pub(super) fn path_text(pen: &Pen, start: bool, end: bool) { /// A crosshair with arms of `side` points in a line of `width` in `color`, as the plain crosshair pointer, with the tool's /// `icon` of `side` points below and right of it. An icon the catalog lacks leaves only the crosshair. // A missing icon still shows the crosshair. -pub(super) fn crosshair_tool(pen: &Pen, icon: Option, side: f32, width: f32, color: Color32) { +pub(crate) fn crosshair_tool(pen: &Pen, icon: Option, side: f32, width: f32, color: Color32) { crosshair(pen, side, width, color); if let Some(icon) = icon { icon_on_halo(pen, Rect::from_min_size(v(CROSSHAIR_ICON).to_pos2(), Vec2::splat(side)), icon); @@ -386,7 +389,7 @@ pub(crate) fn paint_badge(painter: &egui::Painter, tokens: &UiTokens, direction: } /// A badge centred `center` from the pointer. -pub(super) fn badge(pen: &Pen, center: Vec2, badge: CursorBadge) { +pub(crate) fn badge(pen: &Pen, center: Vec2, badge: CursorBadge) { let h = BADGE_HALF; let at = |x: f32, y: f32| center + Vec2::new(x, y); let axes = [Vec2::X, -Vec2::X, Vec2::Y, -Vec2::Y]; @@ -476,12 +479,12 @@ pub(super) fn badge(pen: &Pen, center: Vec2, badge: CursorBadge) { } /// A badge beside a system pointer arrow. -pub(super) fn badge_beside(pen: &Pen, kind: CursorBadge) { +pub(crate) fn badge_beside(pen: &Pen, kind: CursorBadge) { badge(pen, v(BADGE_BESIDE), kind); } /// The rotate pointer: an arc with an arrowhead at each end, bent round the corner in direction `toward`. -pub(super) fn turn(pen: &Pen, toward: Vec2) { +pub(crate) fn turn(pen: &Pen, toward: Vec2) { let len = toward.length(); if !len.is_finite() || len < f32::EPSILON { return; @@ -513,7 +516,7 @@ fn unit(dir: Vec2, fallback: Vec2) -> Vec2 { } /// The free-distort pointer: a dart with its tip on the pointer, pointing along `dir` (up and left for zero). -pub(super) fn distort(pen: &Pen, dir: Vec2) { +pub(crate) fn distort(pen: &Pen, dir: Vec2) { let u = unit(dir, v([-1.0, -1.0]).normalized()); let perp = Vec2::new(-u.y, u.x); let base = -u * DART_LENGTH; @@ -526,7 +529,7 @@ pub(super) fn distort(pen: &Pen, dir: Vec2) { } /// The skew pointer: two opposed arrows beside each other along `dir` (along x for zero). -pub(super) fn skew(pen: &Pen, dir: Vec2) { +pub(crate) fn skew(pen: &Pen, dir: Vec2) { let u = unit(dir, Vec2::X); let perp = Vec2::new(-u.y, u.x); for (offset, sign) in [(SKEW_GAP, 1.0f32), (-SKEW_GAP, -1.0)] { diff --git a/crates/ui-contract/src/contract/ghost/art.rs b/crates/ui-contract/src/primitives/pointer_art/art.rs similarity index 100% rename from crates/ui-contract/src/contract/ghost/art.rs rename to crates/ui-contract/src/primitives/pointer_art/art.rs diff --git a/crates/ui-contract/src/primitives/sensing.rs b/crates/ui-contract/src/primitives/sensing.rs index bb987c964..f5a341de5 100644 --- a/crates/ui-contract/src/primitives/sensing.rs +++ b/crates/ui-contract/src/primitives/sensing.rs @@ -5,12 +5,13 @@ //! hover and presses itself against the shapes registered last pass, so the winner never depends on show order. //! Every root runs the lifecycle, arena root or not: a leaf is an arena of its own id with no members. +use crate::runtime::edit::{RemovalWatch, gesture_key}; use crate::runtime::egui::areas::{self, Layer, LayerMap}; use crate::runtime::egui::pass; use crate::runtime::egui::response::{self, MemberView, PressView}; use crate::runtime::env::ArenaId; use crate::runtime::frame::PerViewport; -use crate::runtime::memory::sub; +use crate::runtime::memory::{MemoryStage, sub}; use crate::runtime::pointer; use crate::runtime::pointer::PassPointer; use craft_behavior::{ @@ -371,6 +372,15 @@ pub(crate) fn members(ctx: &Context, viewport: ViewportId, arena: ArenaId) -> Sm }) } +/// Whether a member of the arena root `arena` (a canvas control in a canvas, say) has a running gesture, in +/// `viewport`: begun and not yet committed, cancelled or removed. It reads each member's own gesture record, so a +/// reader before or after the arena's pass reads the same fact. +pub fn member_gesture_running(ctx: &Context, viewport: ViewportId, arena: Id) -> bool { + members(ctx, viewport, ArenaId::new(arena)) + .into_iter() + .any(|member| MemoryStage::peek_with::(ctx, gesture_key(member), RemovalWatch::armed)) +} + /// Notes a root shown in the body of `arena` that is not one of its members and takes a press, over `rect`: the /// members shown after it over that rect stack above it. pub(crate) fn note_nested(ctx: &Context, arena: ArenaId, rect: Rect) { diff --git a/crates/ui-contract/src/primitives/surround.rs b/crates/ui-contract/src/primitives/surround.rs new file mode 100644 index 000000000..65df35a0a --- /dev/null +++ b/crates/ui-contract/src/primitives/surround.rs @@ -0,0 +1,70 @@ +//! Painting the canvas surround: the fill around a document and the theme's pattern over it. + +use super::textures::{TextureCache, UNTINTED}; +use super::{CheckerArea, fill_polygon}; +use crate::style::SurroundLook; +use egui::epaint::{RectShape, Vertex}; +use egui::{Color32, ColorImage, Mesh, Pos2, Rect, Shape, TextureFilter, TextureOptions, TextureWrapMode}; + +/// The side of the pattern's tile, in points: one dot per tile. +const TILE: f32 = 22.0; + +/// The dot's four texels at the tile centre and their share of the pattern colour's alpha, the dot an antialiased +/// point slightly off the texel grid. +const DOT: [((usize, usize), u8); 4] = [((10, 10), 255), ((11, 10), 110), ((10, 11), 110), ((11, 11), 60)]; + +/// Paints the surround `look` over `area`: the fill, and with a pattern one textured shape of the dot tile over it. +/// The tile is laid in screen space from the origin, so the dots hold still while the view scrolls or turns. A rect takes its corners; a quad (a piece of a turned pasteboard) is filled as `Draw` fills a polygon. +pub fn paint_surround(painter: &egui::Painter, area: CheckerArea, look: &SurroundLook) { + match area { + CheckerArea::Rect(rect, radius) => { + painter.add(Shape::Rect(RectShape::filled(rect, radius, look.fill))); + } + CheckerArea::Quad(corners) => { + let feather = painter.ctx().tessellation_options(|options| { + if options.feathering { options.feathering_size_in_pixels / painter.pixels_per_point() } else { 0.0 } + }); + let mesh = fill_polygon(&corners, look.fill, feather); + if !mesh.is_empty() { + painter.add(Shape::mesh(mesh)); + } + } + } + let Some(pattern) = look.pattern else { return }; + let texture = dot_texture(painter.ctx(), pattern); + let uv = |p: Pos2| (p.to_vec2() / TILE).to_pos2(); + let shape = match area { + CheckerArea::Rect(rect, radius) => { + Shape::Rect(RectShape::filled(rect, radius, UNTINTED).with_texture(texture, Rect::from_min_max(uv(rect.min), uv(rect.max)))) + } + CheckerArea::Quad(corners) => { + let mut mesh = Mesh::with_texture(texture); + mesh.vertices.extend(corners.iter().map(|&pos| Vertex { pos, uv: uv(pos), color: UNTINTED })); + mesh.indices.extend([0, 1, 2, 0, 2, 3]); + Shape::mesh(mesh) + } + }; + painter.add(shape); +} + +/// The repeating tile of one `pattern` dot, from the runtime's texture cache: one texture per pattern colour. +fn dot_texture(ctx: &egui::Context, pattern: Color32) -> egui::TextureId { + let options = TextureOptions { + magnification: TextureFilter::Linear, + minification: TextureFilter::Linear, + wrap_mode: TextureWrapMode::Repeat, + mipmap_mode: None, + }; + TextureCache::texture_with(ctx, ("craft-ui-surround", pattern), options, || { + let side = TILE as usize; + let [r, g, b, a] = pattern.to_srgba_unmultiplied(); + let mut pixels = vec![Color32::TRANSPARENT; side * side]; + for ((x, y), share) in DOT { + let alpha = (u16::from(a) * u16::from(share) / 255) as u8; + if let Some(pixel) = pixels.get_mut(y * side + x) { + *pixel = Color32::from_rgba_unmultiplied(r, g, b, alpha); + } + } + ColorImage::new([side, side], pixels) + }) +} diff --git a/crates/ui-contract/src/runtime/edit.rs b/crates/ui-contract/src/runtime/edit.rs index 5ac865026..803d63023 100644 --- a/crates/ui-contract/src/runtime/edit.rs +++ b/crates/ui-contract/src/runtime/edit.rs @@ -4,7 +4,7 @@ use super::egui::repaint; use crate::runtime::memory::MemoryStage; use crate::runtime::pass_state; use craft_behavior::{CancelReason, Gesture, GestureId, Value}; -use egui::{Context, Id, ViewportId}; +use egui::{Context, Id}; /// The latest `cancel_all`: a serial that grows with each call, and why. #[derive(Clone, Copy, Default)] @@ -29,11 +29,16 @@ pub(crate) fn latest(ctx: &Context) -> (u64, Option) { pass_state::with::(ctx, |all| (all.serial, all.reason)) } +/// The key of the gesture slot under a component's base id. +pub(crate) fn gesture_key(id: Id) -> Id { + crate::runtime::memory::sub(id, "edit") +} + /// The note the component `id` set on its running gesture with [`Cx::propose_with`](crate::contract::Cx::propose_with), /// when it is an `N`; `None` while no gesture of the component runs. `V` is the component's value type, under which /// its gesture is kept. A reader outside the component's pass, drawn before or after it, reads the same note. pub fn gesture_note(ctx: &Context, id: Id) -> Option { - MemoryStage::peek_with::, _>(ctx, crate::contract::gesture_key(id), |gesture| { + MemoryStage::peek_with::, _>(ctx, gesture_key(id), |gesture| { if !gesture.is_running() { return None; } @@ -41,15 +46,6 @@ pub fn gesture_note(ctx: &Context, id: Id) -> Opti }) } -/// Whether a member of the arena root `arena` (a canvas control in a canvas, say) has a running gesture, in -/// `viewport`: begun and not yet committed, cancelled or removed. It reads each member's own gesture record, so a -/// reader before or after the arena's pass reads the same fact. -pub fn member_gesture_running(ctx: &Context, viewport: ViewportId, arena: Id) -> bool { - crate::primitives::sensing::members(ctx, viewport, crate::runtime::env::ArenaId::new(arena)) - .into_iter() - .any(|member| MemoryStage::peek_with::(ctx, crate::contract::gesture_key(member), RemovalWatch::armed)) -} - /// The newest gesture id handed out in this context, and how many times a component has reported its gesture running /// at an apply step. #[derive(Clone, Copy, Default)] diff --git a/crates/ui-contract/src/style/mod.rs b/crates/ui-contract/src/style/mod.rs index 8ef178156..d13c35b33 100644 --- a/crates/ui-contract/src/style/mod.rs +++ b/crates/ui-contract/src/style/mod.rs @@ -33,7 +33,7 @@ pub use replacements::{DuplicateStyle, StyleReplacements}; pub use span::Span; pub use state::StateSet; pub use surface::{BandFill, SurfaceEdge, SurfaceStyle}; -pub use surround::{SurroundLook, paint_surround}; +pub use surround::SurroundLook; pub use tokens::UiTokens; #[doc(hidden)] diff --git a/crates/ui-contract/src/style/surround.rs b/crates/ui-contract/src/style/surround.rs index fd48924ca..bb060230f 100644 --- a/crates/ui-contract/src/style/surround.rs +++ b/crates/ui-contract/src/style/surround.rs @@ -2,17 +2,7 @@ use super::UiTokens; use super::paint::color as rgba_color; -use crate::primitives::textures::UNTINTED; -use crate::primitives::{CheckerArea, TextureCache, fill_polygon}; -use egui::epaint::{RectShape, Vertex}; -use egui::{Color32, ColorImage, Mesh, Pos2, Rect, Shape, TextureFilter, TextureOptions, TextureWrapMode}; - -/// The side of the pattern's tile, in points: one dot per tile. -const TILE: f32 = 22.0; - -/// The dot's four texels at the tile centre and their share of the pattern colour's alpha, the dot an antialiased -/// point slightly off the texel grid. -const DOT: [((usize, usize), u8); 4] = [((10, 10), 255), ((11, 10), 110), ((10, 11), 110), ((11, 11), 60)]; +use egui::Color32; /// How the surround around a canvas looks: its fill and the pattern drawn over it. #[derive(Clone, Copy, Debug, PartialEq)] @@ -41,59 +31,3 @@ impl SurroundLook { Self { pattern: self.pattern.filter(|_| pattern_on), ..self } } } - -/// Paints the surround `look` over `area`: the fill, and with a pattern one textured shape of the dot tile over it. -/// The tile is laid in screen space from the origin, so the dots hold still while the view scrolls or turns. A rect takes its corners; a quad (a piece of a turned pasteboard) is filled as `Draw` fills a polygon. -pub fn paint_surround(painter: &egui::Painter, area: CheckerArea, look: &SurroundLook) { - match area { - CheckerArea::Rect(rect, radius) => { - painter.add(Shape::Rect(RectShape::filled(rect, radius, look.fill))); - } - CheckerArea::Quad(corners) => { - let feather = painter.ctx().tessellation_options(|options| { - if options.feathering { options.feathering_size_in_pixels / painter.pixels_per_point() } else { 0.0 } - }); - let mesh = fill_polygon(&corners, look.fill, feather); - if !mesh.is_empty() { - painter.add(Shape::mesh(mesh)); - } - } - } - let Some(pattern) = look.pattern else { return }; - let texture = dot_texture(painter.ctx(), pattern); - let uv = |p: Pos2| (p.to_vec2() / TILE).to_pos2(); - let shape = match area { - CheckerArea::Rect(rect, radius) => { - Shape::Rect(RectShape::filled(rect, radius, UNTINTED).with_texture(texture, Rect::from_min_max(uv(rect.min), uv(rect.max)))) - } - CheckerArea::Quad(corners) => { - let mut mesh = Mesh::with_texture(texture); - mesh.vertices.extend(corners.iter().map(|&pos| Vertex { pos, uv: uv(pos), color: UNTINTED })); - mesh.indices.extend([0, 1, 2, 0, 2, 3]); - Shape::mesh(mesh) - } - }; - painter.add(shape); -} - -/// The repeating tile of one `pattern` dot, from the runtime's texture cache: one texture per pattern colour. -fn dot_texture(ctx: &egui::Context, pattern: Color32) -> egui::TextureId { - let options = TextureOptions { - magnification: TextureFilter::Linear, - minification: TextureFilter::Linear, - wrap_mode: TextureWrapMode::Repeat, - mipmap_mode: None, - }; - TextureCache::texture_with(ctx, ("craft-ui-surround", pattern), options, || { - let side = TILE as usize; - let [r, g, b, a] = pattern.to_srgba_unmultiplied(); - let mut pixels = vec![Color32::TRANSPARENT; side * side]; - for ((x, y), share) in DOT { - let alpha = (u16::from(a) * u16::from(share) / 255) as u8; - if let Some(pixel) = pixels.get_mut(y * side + x) { - *pixel = Color32::from_rgba_unmultiplied(r, g, b, alpha); - } - } - ColorImage::new([side, side], pixels) - }) -} diff --git a/crates/ui-contract/tests/it/fonts/on_demand.rs b/crates/ui-contract/tests/it/fonts/on_demand.rs index e91bb1c23..869003f92 100644 --- a/crates/ui-contract/tests/it/fonts/on_demand.rs +++ b/crates/ui-contract/tests/it/fonts/on_demand.rs @@ -319,6 +319,7 @@ fn the_han_preference_follows_the_ui_language_then_the_os_locale() { #[trial] fn the_deflated_defaults_are_eguis_default_faces_and_draw_its_icons() { + use epaint_default_fonts::special_emojis::{GIT, GITHUB, OS_ANDROID, OS_APPLE, OS_WINDOWS}; use epaint_default_fonts::{EGUI_ICONS, EMOJI_ICON, HACK_REGULAR, NOTO_EMOJI_REGULAR, UBUNTU_LIGHT}; let defaults = deflated_default_definitions(); for (name, bytes, scale) in [("Hack", HACK_REGULAR, 1.0), ("Ubuntu-Light", UBUNTU_LIGHT, 1.0), ("egui-icons", EGUI_ICONS, 0.90)] { @@ -333,14 +334,16 @@ fn the_deflated_defaults_are_eguis_default_faces_and_draw_its_icons() { assert_eq!(data.tweak.scale, scale); } - // egui's own icons draw in both families, which the strict missing-glyph policy of headless runs depends on. + // egui's own icons draw in both families, which the strict missing-glyph policy of headless runs depends on. The + // penguin (`OS_LINUX`) is a plain emoji that only an emoji face draws, so it is left out: a runner's system fonts + // decide whether it draws. let mut frames = Frames::new(); frames.ctx.set_fonts(defaults); for _ in 0..2 { frames.frame(vec![], |_| {}); } for family in [FontId::proportional(14.0), FontId::monospace(14.0)] { - for icon in [epaint_default_fonts::special_emojis::GITHUB, epaint_default_fonts::special_emojis::OS_LINUX] { + for icon in [GIT, GITHUB, OS_ANDROID, OS_APPLE, OS_WINDOWS] { assert!(frames.ctx.fonts_mut(|f| f.has_glyph(&family, icon)), "{icon} in {:?}", family.family); } } diff --git a/crates/ui-contract/tests/it/paint/surround.rs b/crates/ui-contract/tests/it/paint/surround.rs index 779da7be9..4e33571fc 100644 --- a/crates/ui-contract/tests/it/paint/surround.rs +++ b/crates/ui-contract/tests/it/paint/surround.rs @@ -2,8 +2,8 @@ //! turned off, draws the fill alone and makes no texture (#4542). use craft_theme::{Contrast, Mode, Rgba, Size}; -use craft_ui::primitives::CheckerArea; -use craft_ui::style::{SurroundLook, UiTokens, paint_surround}; +use craft_ui::primitives::{CheckerArea, paint_surround}; +use craft_ui::style::{SurroundLook, UiTokens}; use craft_ui::testing::{Frames, trial}; use egui::{CornerRadius, Rect, Shape, TextureId, pos2, vec2}; use std::sync::Arc; diff --git a/crates/ui-shell/src/shell/canvas_shell.rs b/crates/ui-shell/src/shell/canvas_shell.rs index 250d8a244..53bed3638 100644 --- a/crates/ui-shell/src/shell/canvas_shell.rs +++ b/crates/ui-shell/src/shell/canvas_shell.rs @@ -24,9 +24,9 @@ mod stories; use super::app_shell::{RegionBody, RegionFn, ShellRegion}; use super::words::ShellWords; use crate::contract::{BodyCx, NoValue, Outcome, PaneEdge, Props as _, ShellPane, ShowContainer as _}; -use crate::primitives::CheckerArea; +use crate::primitives::{CheckerArea, paint_surround}; use crate::runtime::env::{BodyMode, Env, Label, ShellEntry}; -use crate::style::{SurroundLook, paint_surround}; +use crate::style::SurroundLook; use egui::{CornerRadius, Rect, Response, Ui}; /// The slot keys of the preset. diff --git a/crates/ui-shell/src/shell/start.rs b/crates/ui-shell/src/shell/start.rs index cb12342c5..311e10148 100644 --- a/crates/ui-shell/src/shell/start.rs +++ b/crates/ui-shell/src/shell/start.rs @@ -18,12 +18,12 @@ use crate::list::{List, ListRow, RowEvent, ThumbAspect, ThumbSize, TrailingFace, use crate::menu::{Menu, MenuItem}; use crate::notices::{Notice, NoticeLayout}; use crate::pickers::Select; -use crate::primitives::{CheckerArea, ContentPainterCx, IconSpec, Priority, ThumbPaint, ThumbRequest, ThumbSource, Thumbnails}; +use crate::primitives::{CheckerArea, ContentPainterCx, IconSpec, Priority, ThumbPaint, ThumbRequest, ThumbSource, Thumbnails, paint_surround}; use crate::runtime::commands::{self, CommandRef, FireSource}; use crate::runtime::commands::{CommandSet as _, Commands as _}; use crate::runtime::env::{AgeStyle, Env, Label}; use crate::sections::Section; -use crate::style::{Corners, SurfaceKind, SurroundLook, paint_surround}; +use crate::style::{Corners, SurfaceKind, SurroundLook}; use crate::tile_grid::{CaptionPlace, Tile, TileGrid, TilePartEvent}; use craft_behavior::{IconId, Proposal, SelectionChange, keyed}; use craft_theme::Variant;