diff --git a/crates/ui-canvas/src/canvas/app_paint.rs b/crates/ui-canvas/src/canvas/app_paint.rs new file mode 100644 index 000000000..a6ad48ad2 --- /dev/null +++ b/crates/ui-canvas/src/canvas/app_paint.rs @@ -0,0 +1,30 @@ +//! `AppPaint`: a picture the app paints itself inside a framework control, which places it and draws its own parts +//! around it. + +use crate::primitives::ContentPainterCx; + +type AppPaintFn<'a, A> = Box, A) + 'a>; + +/// A closure that paints the app's picture where a control places it: the control calls it from its paint with its +/// content painter and the placement `A` (the [`Navigator`](super::Navigator)'s fitted rect, the +/// [`RasterDisplay`](super::RasterDisplay)'s canvas view). The app adds what it likes to the painter, a GPU paint +/// callback included; the control's own parts paint before and after it. +pub struct AppPaint<'a, A>(AppPaintFn<'a, A>); + +impl<'a, A> AppPaint<'a, A> { + /// A painter that draws the picture with `paint`, given the content painter and the placement. + pub fn new(paint: impl FnMut(&ContentPainterCx<'_>, A) + 'a) -> Self { + AppPaint(Box::new(paint)) + } + + /// The same painter lent for one run of a body that may run more than once per pass. + pub fn reborrowed(&mut self) -> AppPaint<'_, A> { + let paint = &mut self.0; + AppPaint::new(move |painter, at| paint(painter, at)) + } + + /// Paints the picture with `painter` at `at`. + pub fn paint(&mut self, painter: &ContentPainterCx<'_>, at: A) { + (self.0)(painter, at); + } +} diff --git a/crates/ui-canvas/src/canvas/mod.rs b/crates/ui-canvas/src/canvas/mod.rs index 3082994dd..2b998bc43 100644 --- a/crates/ui-canvas/src/canvas/mod.rs +++ b/crates/ui-canvas/src/canvas/mod.rs @@ -48,7 +48,9 @@ pub use pasteboard::{PageBorder, PageFrame, Pasteboard, PasteboardColor, Pastebo pub use split::{SplitAt, SplitLayout, SplitPane, SplitStyle, SplitViews, SplitViewsOutput}; pub use words::SplitWords; +mod app_paint; mod raster; +pub use app_paint::AppPaint; pub use raster::{DetailPlan, RasterDisplay, RasterDisplayOutput, RasterStyle, snap_to_texels}; // l4d-space @@ -83,8 +85,8 @@ pub use scrollbars::{CanvasScrollbars, CanvasScrollbarsOutput}; // canvas-navigator-a mod navigator; pub use navigator::{ - Navigator, NavigatorCommands, NavigatorOutput, NavigatorPaint, NavigatorPreview, NavigatorZoomRow, NavigatorZoomRowOutput, preview_rect, - proxy_quad, view_center_for, zoom_to_document_rect, + Navigator, NavigatorCommands, NavigatorOutput, NavigatorPreview, NavigatorZoomRow, NavigatorZoomRowOutput, preview_rect, proxy_quad, + view_center_for, zoom_to_document_rect, }; // End of canvas-navigator-a. // l4e-navprev diff --git a/crates/ui-canvas/src/canvas/navigator/mod.rs b/crates/ui-canvas/src/canvas/navigator/mod.rs index 6da7deae7..87d4d7c8c 100644 --- a/crates/ui-canvas/src/canvas/navigator/mod.rs +++ b/crates/ui-canvas/src/canvas/navigator/mod.rs @@ -4,7 +4,7 @@ //! | Part | Names | //! |---|---| //! | Geometry, for tests and for apps with their own preview | [`preview_rect`], [`proxy_quad`], [`zoom_to_document_rect`], [`view_center_for`] | -//! | The preview box | [`Navigator`], [`NavigatorPreview`], [`NavigatorPaint`], [`NavigatorOutput`], [`NavigatorStyle`] | +//! | The preview box | [`Navigator`], [`NavigatorPreview`], [`NavigatorOutput`], [`NavigatorStyle`] | //! | The zoom row | [`NavigatorZoomRow`], [`NavigatorZoomRowOutput`] | //! | Panel Options | [`NavigatorSettings`], [`NavigatorOptions`], [`ProxyColor`]; the dialog is `patterns::form_dialogs::NavigatorOptionsDialog` | //! | The panel menu | [`NavigatorCommands`] | @@ -17,6 +17,6 @@ mod stories; mod zoom_row; pub use menu::NavigatorCommands; -pub use preview::{Navigator, NavigatorOutput, NavigatorPaint, NavigatorPreview, NavigatorStyle}; +pub use preview::{Navigator, NavigatorOutput, NavigatorPreview, NavigatorStyle}; pub use proxy::{preview_rect, proxy_quad, view_center_for, zoom_to_document_rect}; pub use zoom_row::{NavigatorZoomRow, NavigatorZoomRowOutput}; diff --git a/crates/ui-canvas/src/canvas/navigator/preview.rs b/crates/ui-canvas/src/canvas/navigator/preview.rs index 8b5428816..3fc8d93db 100644 --- a/crates/ui-canvas/src/canvas/navigator/preview.rs +++ b/crates/ui-canvas/src/canvas/navigator/preview.rs @@ -3,6 +3,7 @@ //! wheel and `+` and `-` zoom about the view centre, and the arrow and page keys move it. use super::proxy::{preview_rect, proxy_quad, view_center_for, zoom_to_document_rect}; +use crate::canvas::AppPaint; use crate::canvas::zoom_motion::{self, Steer}; use crate::canvas::{CanvasWords, NavigationOptions, PageKey, ViewCommands, WheelAction, WheelNavigation}; use crate::canvas_data::ProxyColor; @@ -10,8 +11,8 @@ use crate::contract::{ Access, Bind, BindMut, Common, Component, Cx, DisabledLook, Edit as _, EditResponse, Focus as _, Input, MeasureCx, Name, Outcome, PaintUi, PartsCx, Pointer as _, Props as _, Remember as _, Role, WordSetCx as _, }; +use crate::primitives::PaintShape; use crate::primitives::textures::CachedTexture; -use crate::primitives::{ContentPainterCx, PaintShape}; use crate::runtime::access::Numeric; use crate::runtime::commands; use crate::runtime::env::Label; @@ -47,25 +48,13 @@ const SHIFT_STEP: f32 = 0.5; const ZOOM_KEYS: [(Chord, i32); 4] = [(Chord::plain(Key::Plus), 1), (Chord::plain(Key::Equals), 1), (Chord::new(Mods::SHIFT, Key::Equals), 1), (Chord::plain(Key::Minus), -1)]; -type NavigatorPaintFn<'a> = Box, Rect) + 'a>; - -/// A closure that paints the document's picture into the fitted rect. -pub struct NavigatorPaint<'a>(NavigatorPaintFn<'a>); - -impl<'a> NavigatorPaint<'a> { - /// A painter that draws the picture with `paint`, given the content painter and the rect the picture fills. - pub fn new(paint: impl FnMut(&ContentPainterCx<'_>, Rect) + 'a) -> Self { - NavigatorPaint(Box::new(paint)) - } -} - /// What the preview box shows: the document's picture, made and cached by the app. #[non_exhaustive] pub enum NavigatorPreview<'a> { /// A texture from the app's own texture store, drawn over the checkerboard into the fitted rect. Texture(CachedTexture), /// The app paints the picture itself into the fitted rect; the checkerboard is not drawn. - Paint(NavigatorPaint<'a>), + Paint(AppPaint<'a, Rect>), /// No picture yet: the checkerboard alone, with the proxy over it. None, } @@ -75,7 +64,7 @@ impl NavigatorPreview<'_> { pub fn reborrowed(&mut self) -> NavigatorPreview<'_> { match self { NavigatorPreview::Texture(texture) => NavigatorPreview::Texture(*texture), - NavigatorPreview::Paint(NavigatorPaint(paint)) => NavigatorPreview::Paint(NavigatorPaint::new(move |painter, rect| paint(painter, rect))), + NavigatorPreview::Paint(paint) => NavigatorPreview::Paint(paint.reborrowed()), NavigatorPreview::None => NavigatorPreview::None, } } @@ -463,7 +452,7 @@ impl Component for Navigator<'_> { painter.checker(fitted); painter.picture(texture.id, fitted, Rect::from_min_max(pos2(0.0, 0.0), pos2(1.0, 1.0)), 0.0); } - NavigatorPreview::Paint(NavigatorPaint(mut paint)) => paint(&painter, fitted), + NavigatorPreview::Paint(mut paint) => paint.paint(&painter, fitted), NavigatorPreview::None => painter.checker(fitted), } let Some(view) = cx.value().copied() else { return }; diff --git a/crates/ui-canvas/src/canvas/navigator/stories.rs b/crates/ui-canvas/src/canvas/navigator/stories.rs index 069775adb..902bd1dac 100644 --- a/crates/ui-canvas/src/canvas/navigator/stories.rs +++ b/crates/ui-canvas/src/canvas/navigator/stories.rs @@ -1,7 +1,8 @@ //! Stories of the Navigator and its zoom row. use super::preview::WELL; -use super::{Navigator, NavigatorPaint, NavigatorPreview, NavigatorZoomRow}; +use super::{Navigator, NavigatorPreview, NavigatorZoomRow}; +use crate::canvas::AppPaint; use crate::contract::{Bind, Show as _, Validity}; use crate::runtime::env::Message; use crate::testing::{Drive, Grip, StoryData, StoryOutput, story}; @@ -56,7 +57,7 @@ fn views_that_differ(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { fn painted_by_the_app(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { let view = data.value("view", CanvasView::new(CANVAS, Point::new(1000.0, 750.0), 1.0)); Navigator::new("Navigator", Bind::bound(view), DOCUMENT) - .preview(NavigatorPreview::Paint(NavigatorPaint::new(|painter, rect| { + .preview(NavigatorPreview::Paint(AppPaint::new(|painter, rect| { painter.painter.rect_filled(rect, 0.0, Color32::from_gray(PICTURE_GRAY)); }))) .show(ui) diff --git a/crates/ui-canvas/src/canvas/raster/mod.rs b/crates/ui-canvas/src/canvas/raster/mod.rs index 0a5db74bf..a548c0e3b 100644 --- a/crates/ui-canvas/src/canvas/raster/mod.rs +++ b/crates/ui-canvas/src/canvas/raster/mod.rs @@ -1,12 +1,15 @@ -//! `RasterDisplay`: the CPU raster path of a canvas. The app gives the document's composite as RGBA for a region -//! ([`RasterSource`]); the display keeps it in `TextureStore` textures, draws it through the canvas's -//! [`CanvasSpace`](super::CanvasSpace) over a checkerboard, and re-reads only what the source reports damaged. +//! `RasterDisplay`: the document display of a canvas. It draws the document through the canvas's +//! [`CanvasSpace`](super::CanvasSpace) over a checkerboard, with the edge rule around it. The picture is either the +//! document's composite as RGBA for a region ([`RasterSource`]), which the display keeps in `TextureStore` textures and +//! re-reads only where the source reports damage, or the app's own paint ([`AppPaint`]), such as a GPU paint callback +//! over the app's compositor. //! //! A document larger than the device's texture limit (`texture_side`) is uploaded reduced; zoomed in, the part in //! view is added at the screen's resolution as a detail texture ([`DetailPlan`]), so a large document shows its own //! pixels at 100 percent and the cost follows the window, not the document. Pixels are drawn sharp (nearest) from //! 100 percent up and filtered below. //! +//! //! The display takes no pointer and no keys and declares no parts: a press on it reaches the canvas's tool. mod detail; @@ -15,7 +18,7 @@ mod stories; pub use detail::{DetailPlan, snap_to_texels}; -use super::{PixelRect, RasterSource, canvas_space}; +use super::{AppPaint, PixelRect, RasterSource, canvas_space}; use crate::contract::{Access, Common, Component, Cx, NoValue, Outcome, PaintUi, Props as _, Role}; use crate::primitives::textures::UNTINTED; use crate::primitives::{ @@ -50,12 +53,35 @@ enum Level { Detail(DetailPlan), } -/// What `update` leaves for `paint` and `finish`: the textures to draw and the view to draw them through. +/// What a display shows. +enum Picture<'a> { + /// The source's pixels, kept in the display's textures. + Source(&'a dyn RasterSource), + /// The app paints the document of `size` pixels itself, through the view it is given. + Painted { size: PixelSize, paint: AppPaint<'a, CanvasView> }, +} + +impl Picture<'_> { + fn size(&self) -> PixelSize { + match self { + Picture::Source(source) => source.size(), + Picture::Painted { size, .. } => *size, + } + } +} + +/// The textures of a source picture: the whole document at one texel per `factor` pixels, and the part in view at the +/// screen's resolution while zoomed in. +struct Textures { + base: (CachedTexture, u32), + detail: Option<(CachedTexture, DetailPlan)>, +} + +/// What `update` leaves for `paint` and `finish`: the view to draw through and, for a source picture, its textures. struct Shown { view: CanvasView, size: PixelSize, - base: (CachedTexture, u32), - detail: Option<(CachedTexture, DetailPlan)>, + textures: Option, } /// The revision each texture of a display shows, which is what the source is asked about: what changed since then. @@ -83,9 +109,13 @@ impl Outcome for RasterDisplayOutput { } } -/// The CPU raster display of one canvas: the document drawn through the view of the [`Canvas`](super::Canvas) it -/// is shown in, at the canvas rect. Outside a canvas, or in one without a view, it draws nothing. +/// The document display of one canvas: the document drawn through the view of the [`Canvas`](super::Canvas) it is +/// shown in, at the canvas rect. Outside a canvas, or in one without a view, it draws nothing. /// +/// - **Picture.** [`new`](Self::new) draws a [`RasterSource`]'s pixels from textures the display keeps; +/// [`painted`](Self::painted) calls the app's paint once per pass, in the display's place between the checkerboard +/// and the edge, with the view on screen. Both pictures get the same checkerboard and edge; the shadow is the +/// [`Pasteboard`](super::Pasteboard)'s and the pixel grid the `PixelGrid` control's, shown around and over either. /// - **Reading.** The display asks the source for pixels only when it has none for the source's `revision`. A /// changed revision re-reads what [`RasterSource::damage`] reports since the revision the display shows (snapped /// outward to whole texels), or everything where the source cannot tell. @@ -102,7 +132,7 @@ impl Outcome for RasterDisplayOutput { pub struct RasterDisplay<'a> { #[common] common: Common, - source: &'a dyn RasterSource, + picture: Picture<'a>, /// The transparency grid under the document; none draws the document straight on the canvas. #[prop] checkerboard: Option, @@ -127,7 +157,18 @@ impl<'a> RasterDisplay<'a> { /// A display named `label` of the document `source` gives. An empty label makes a display that only paints, with /// no AccessKit node: a further image over a named one. pub fn new(label: impl Into