//! `DocumentWindow`: a document's window, a container in its own OS viewport. #[cfg(feature = "testing")] mod stories; use crate::contract::overlay::OverlayFit; use crate::contract::{ Bind, BindMut, BodyCx, Common, Component, Container, ContainerProps, Cx, DragBand, HeaderRegion, Mount, NoValue, Outcome, Overlay, Pad, Props as _, Role, SaveKey, Show as _, Sizing, WordSetCx as _, }; use crate::runtime::env::Label; use crate::runtime::memory::{Remember as _, sub}; use crate::runtime::overlay::CloseReason; use crate::runtime::windows::{WindowAction, WindowHost, WindowSpec}; use crate::sections::{HeaderAction, HeaderStrength, TitleRow}; use crate::style::{ComponentStyle, StyleCx, SurfaceKind}; use craft_behavior::placement::{capped, cascade, monitor_of}; use craft_behavior::{Metrics, Monitor, Sense}; use craft_host::target::WindowState; use craft_window::{WindowId, WindowList}; use egui::{Rect, Response, Ui, Vec2, ViewportId}; /// The style of a document window: the surface comes from the container. #[derive(Clone, Debug, Default, PartialEq, ComponentStyle)] #[non_exhaustive] pub struct DocumentWindowStyle { #[style(metrics)] metrics: Metrics, /// The offset between cascaded windows (`size.window.cascade`). cascade: f32, } /// What a document window reports: its response, how it was asked to close in this pass, and whether it has OS /// focus. #[derive(Debug)] #[non_exhaustive] pub struct DocumentWindowOutput { /// The window's response. pub response: Response, /// How the window was asked to close in this pass: its OS close button or the fallback layer's close action. /// Reported once for each request. The window stays until the app stops showing it. pub close_requested: Option, /// The window has OS focus. False in the fallback layer, which has no window of its own. pub focused: bool, /// The step of a cascade of windows (`size.window.cascade`): the `step` of [`DocumentWindow::cascaded`]. pub cascade: f32, /// The window gained OS focus in this pass. pub focus_gained: bool, /// Whether the window took the size a zoom edit asked for in this pass (`Fits`: the host centres the document) or /// the work area capped it (`Capped`: the zoomed spot stays); `None` when nothing was asked or sent. pub wrapped: Option, } /// How a window answered the size a zoom edit asked for ([`DocumentWindow::wrap_in`]). #[derive(Clone, Copy, Debug, PartialEq, Eq)] #[non_exhaustive] pub enum Wrapped { /// The window took the wanted size. Fits, /// The monitor's work area capped the size. Capped, } impl Outcome for DocumentWindowOutput { type Value = NoValue; fn response(&self) -> &Response { &self.response } } /// A document's window: the container of one entry of the app's window list, in its own OS window. /// /// The app shows one for each open document and stops showing it to close it. The body holds the document's whole /// window, including its title bar and shell; the window adds none of its own where the OS window exists. /// /// - **Placement.** The window opens where [`first_placed`](DocumentWindow::first_placed) puts it (see /// [`cascaded`](DocumentWindow::cascaded)) until a placement saved under [`remember`](DocumentWindow::remember) /// takes over. The minimum and maximum size (through [`overlay`](DocumentWindow::overlay)) and the resizability are the /// fallback layer's, which the OS window reads. /// - **Close.** The OS close button, and the close action of the fallback layer, report `close_requested`. The /// window stays while the app shows it, for example while it asks about unsaved changes, and closes when the app /// stops showing it. /// - **Title and focus.** The title is the label given to `new`; it follows the document while the app changes it. /// [`raise`](DocumentWindow::raise) brings the window forward in the pass it is set. With /// [`in_list`](DocumentWindow::in_list) the window makes itself the list's active window when it gains OS focus. /// - **Fallback.** Where OS windows are unavailable the document shows on a movable, resizable layer with a title row. #[derive(crate::contract::Component, crate::contract::Props)] #[component(container)] pub struct DocumentWindow<'a> { #[common] common: Common, #[container] props: ContainerProps, /// The open state the pipeline reads: always open, so a close request never ends the window. open: Bind<'a, bool>, /// Makes the window the active one of the app's window list. activate: Option>, /// Raises the window and gives it OS focus in this pass. #[prop(flag)] raise: bool, closed: Option, } impl<'a> DocumentWindow<'a> { /// Files the zoom edit's size request from the window's body: the window sizes itself so its `canvas` rect becomes /// `wanted`, in the pass `finish` reads it, keeping its centre and staying inside the monitor's work area. pub fn wrap_in(cx: &mut BodyCx<'_>, canvas: Rect, wanted: Vec2) { let id = cx.id; *cx.memory.shown::(id) = WrapAsk(Some((canvas, wanted))); } /// Sizes the window in `viewport` so the body's `canvas` rect becomes `wanted`: at least the fallback layer's /// minimum, at most the work area of its monitor less its frame, about its centre. `None`, with nothing sent, /// where there is no OS window, it is maximised, minimised or full screen, or it has not reported its facts, its /// outer rect (the frame is unknown without it) and monitor. fn wrap(&self, host: &WindowHost, viewport: ViewportId, canvas: Rect, wanted: Vec2) -> Option { if !host.viewports_available() { return None; } let facts = host.facts(viewport).filter(|facts| facts.state == WindowState::Normal)?; let scale = facts.scale?; let outer = facts.outer?; let frame = outer.size() - facts.inner.size(); let monitors = host.monitors(); // `capped` converts the monitor's work area to points itself, so `scale` only finds the monitor here. let monitor = monitor_of(&monitors, Rect::from_min_max(outer.min * scale, outer.max * scale))?; let min = match &self.props.mount { Mount::Viewport { fallback, .. } => fallback.size_limits.min, _ => Vec2::ZERO, }; let asked = facts.inner.size() - canvas.size() + wanted; let size = capped(asked, monitor, frame, min); host.set_outer(viewport, Rect::from_center_size(outer.center(), size + frame)); // A size the minimum raised still shows the whole document: `Fits`. if size.x < asked.x || size.y < asked.y { Some(Wrapped::Capped) } else { Some(Wrapped::Fits) } } /// A window titled `title`, resizable, with the installed decision's frame. pub fn new(title: impl Into