//! `AppShell`: the window skeleton. An ordered stack of top bars starting with the title bar, bottom bars, start and end //! rails, and the body in what is left. Each bar and rail is a container on `Mount::Pane`, so F6 pane focus and the //! name of each region come from the pipeline. mod dialogs; #[cfg(feature = "testing")] mod stories; pub use self::dialogs::{ShellDialogsCx, WindowDialogs}; use super::canvas_shell::slots as keys; use super::regions::{Claim, SlotSize, layout}; use super::reveal::{Aim, Reveal, Tracking}; use super::screen_mode::{ScreenMode, ScreenModeCommands, ScreenModeFollow}; use super::words::ShellWords; use crate::contract::overlay::Resize; use crate::contract::{ Bind, BodyCx, Common, Component, Container, ContainerProps, Cx, Mount, NoValue, Outcome, PaneEdge, Props as _, Remember as _, Role, ShellPane, ShowContainer as _, Sizing, }; use crate::notices::ToastStack; use crate::runtime::commands::{CommandAccess as _, CommandSet as _}; use crate::runtime::egui::pass; use crate::runtime::env::{AppValues, BodyMode, Env, Label, RevealEdge, ShellCx, ShellEntry, ShellRegions, WindowKey}; use crate::runtime::focus::FocusPolicy; use crate::runtime::frame::{note_shell, take_chrome_toggles}; use crate::runtime::overlay::{OpenFilter, any_open}; use crate::runtime::pointer; use crate::shell::Slot; use crate::style::{ComponentStyle, StyleCx, SurfaceKind, UiTokens}; use craft_behavior::{Metrics, Sense}; use egui::{Rect, Response, Ui}; use std::sync::Arc; /// The closure that fills a region. pub(super) type RegionFn<'a> = Box) + 'a>; /// The closure that fills a region, as a `func` prop. pub(super) struct RegionBody<'a>(pub(super) RegionFn<'a>); impl<'a> RegionBody<'a> { /// The body that runs `body`. pub(super) fn new(body: impl FnMut(&mut Ui, &mut BodyCx<'_>) + 'a) -> Self { RegionBody(Box::new(body)) } } /// The closure that fills a slot or the body of an `AppShell`; it gets the app state the shell is shown with. type SlotFn<'a, S> = Box, &mut S) + 'a>; /// The closure that fills the body of an `AppShell`, as a `func` prop. struct SlotBody<'a, S>(SlotFn<'a, S>); impl<'a, S> SlotBody<'a, S> { /// The body that runs `body`. fn new(body: impl FnMut(&mut Ui, &mut BodyCx<'_>, &mut S) + 'a) -> Self { SlotBody(Box::new(body)) } } /// The style of a shell region: the surface comes from the container. #[derive(Clone, Debug, Default, PartialEq, ComponentStyle)] #[non_exhaustive] pub struct ShellRegionStyle { #[style(metrics)] metrics: Metrics, } /// What a shell region reports. #[derive(Debug)] #[non_exhaustive] pub struct ShellRegionOutput { /// The region's response; its rect is the region's place in the window. pub response: Response, } impl Outcome for ShellRegionOutput { type Value = NoValue; fn response(&self) -> &Response { &self.response } } /// A region of the window skeleton: the root window's content, the scope that carries the shell to its slots, or one /// pane (a bar, a rail or the body). #[derive(crate::contract::Component, crate::contract::Props)] #[component(container)] pub struct ShellRegion { #[common] common: Common, #[container] props: ContainerProps, shell: Option, } impl ShellRegion { /// The whole content of the window: its edges resize the window where the app draws the frame. It paints no /// surface of its own: the parts the shell places paint theirs, and a gap between them (a dock's gutter) shows the /// window's clear colour. pub fn window() -> Self { let props = ContainerProps::new().mount(Mount::window(Resize::GRIPS)).command_scope(ScreenModeCommands::SCOPE); ShellRegion { common: Common::new(), props, shell: None } } /// The content of a shell shown inside a region of another window: it fills its room, answers the screen-mode /// commands of its own scope and never frames or resizes the OS window. pub fn embedded() -> Self { let props = ContainerProps::new().sizing(crate::contract::Sizing::Fill).surface(SurfaceKind::Background).command_scope(ScreenModeCommands::SCOPE); ShellRegion { common: Common::new(), props, shell: None } } /// An inline region that fills its room and sets `shell` for everything inside it. pub fn scope(shell: ShellCx) -> Self { let props = ContainerProps::new().sizing(crate::contract::Sizing::Fill).command_scope(crate::runtime::commands::CommandScope::APP); ShellRegion { common: Common::new(), props, shell: Some(shell) } } /// A pane of the window; it fills the room its edge gives it. pub fn pane(pane: ShellPane) -> Self { let props = ContainerProps::new().mount(Mount::Pane(pane)).focus(FocusPolicy::PANE); ShellRegion { common: Common::new(), props, shell: None } } } impl Component for ShellRegion { const ROLE: Role = Role::GROUP; const PARTS: bool = false; const STOPS: bool = false; const OVERLAYS: bool = false; type Style = ShellRegionStyle; type Output = ShellRegionOutput; fn style(_: &StyleCx<'_>, _: ShellRegionStyleKey) -> ShellRegionStyle { ShellRegionStyle::default() } fn sense(&self) -> Sense { Sense::empty() } fn content_env(&self, _: &Cx<'_, Self>, values: &mut AppValues) { if let Some(shell) = &self.shell { shell.put(values); } } fn finish(self, cx: Cx<'_, Self>) -> ShellRegionOutput { ShellRegionOutput { response: cx.response.clone() } } } impl Container for ShellRegion {} /// One bar or rail with the edge it sits on and the closure that fills it. struct Placed<'a, S> { slot: Slot, edge: PaneEdge, /// The size of a slot that sets none: the title bar for the title, else the edge's default. title: bool, /// The slot's name in the shell's words, for a slot of the canvas preset. named: Option Label>, add: SlotFn<'a, S>, } impl Placed<'_, S> { /// The order slots claim space in: the title and top bars, the bottom bars, the start rails, the end rails. fn rank(&self) -> u8 { match self.edge { PaneEdge::Top => 0, PaneEdge::Bottom => 1, PaneEdge::Start => 2, _ => 3, } } /// The slot's extent along its edge: its size, or the default of its edge, with a rail's columns side by side. fn extent(&self, tokens: &UiTokens) -> f32 { let size = self.slot.slot_size().unwrap_or(match self.edge { PaneEdge::Top if self.title => SlotSize::TitleBar, PaneEdge::Top => SlotSize::OptionsBar, PaneEdge::Bottom => SlotSize::StatusBar, _ => SlotSize::RailWidth, }); let columns = if matches!(self.edge, PaneEdge::Start | PaneEdge::End) { self.slot.column_count() } else { 1 }; size.resolve(tokens) * columns as f32 } } /// What the shell keeps between passes, in the root container's slot. #[derive(Clone, Debug, Default)] struct ShellState { reveal: Reveal, screen: ScreenModeFollow, } /// What a pass of the shell leaves. #[derive(Clone, Debug)] struct Parts { regions: ShellRegions, revealed: bool, panels_hidden: bool, mode: ScreenMode, } /// What [`AppShell::show`] drew. #[derive(Debug)] #[non_exhaustive] pub struct AppShellOutput { /// The response of the window's root region. pub response: Response, /// The body's rect and the rects of the shown slots, in the order drawn. pub regions: ShellRegions, /// Slots are shown by a reveal on this pass (Tab or edge). pub revealed: bool, /// The panels are hidden while the tools and bars stay (Shift+Tab). A body that hosts panels reads it with /// `ShellCx::panels_hidden`. pub panels_hidden: bool, /// The screen mode after this pass. pub screen_mode: ScreenMode, } impl Outcome for AppShellOutput { type Value = NoValue; fn response(&self) -> &Response { &self.response } } /// The window skeleton. /// /// - **Order.** Slots claim space in this order, which is also the F6 order: the title, then the `top` slots in call /// order from the window's top edge inward; the `bottom` slots from the bottom edge inward; then `start`, then `end`; /// then the body in what is left. Start and end swap sides in a right-to-left window; top and bottom do not. A theme or /// size never changes the order. /// - **Extent.** A slot is as high (top, bottom) or wide (start, end) as its [`SlotSize`] says, times its columns for a /// rail. Without a size it takes the default of its edge. The shell lays out every shown slot and the body before any /// slot runs, so `BodyCx::shell()` holds this pass's rects. /// - **Screen mode.** The `screen_mode` binding holds the window's mode. The window's `screenMode.*` commands /// (F and Shift+F cycle) change it, and a change to or from full screen is carried out through `WindowHost::act`. /// In `FullScreen` no slot shows but those built with [`Slot::reveal`], during a reveal. /// - **Reveal.** In body-only mode Tab (or the `window.toggle_panels` command) shows the revealing slots until the next /// Tab; the pointer at the top, start or end edge shows that edge's slots, unless a button is held or a popup is /// open or `edge_reveal` is off. Revealed slots cover the body and leave its rect as it is. A screen-mode change ends /// a reveal. The shell takes its own window's Tab toggles from the frame state in the pass that records them: in /// normal mode Tab hides every slot and panel and shows them again. /// - **Canvas preset.** [`title_bar_if`](Self::title_bar_if), [`options_bar`](Self::options_bar), /// [`tools`](Self::tools) and [`status`](Self::status) add the slots of a document editor under the keys of /// [`slots`](super::slots), named by `ShellWords`; each takes the host's `visible` flag, passed every frame. /// - **Shell data.** Slots and the body read the skeleton with `BodyCx::shell()`. /// - **App state.** `S` is the state the slots and the body edit; [`show`](Self::show) hands each of them `&mut S` in /// turn, so a title bar, a rail and a body that edit one document share it without interior mutability. An app with /// no shared state uses `S = ()`. /// - **Dialogs.** The window owns one dialog host, shared by every body in it (`ShellDialogsCx::dialogs`). The shell runs it /// after the body in each pass, so dialogs show over the window. /// - **Embedded.** [`embedded`](Self::embedded) shows a shell inside a region of another window. Its screen modes /// hide and reveal its slots in that region and never ask the OS window for anything. With /// [`content_height`](Self::content_height) it is as tall as its body's content and its top and bottom slots. #[must_use = "show the shell with `show`"] #[derive(crate::contract::Pattern, crate::contract::Props)] #[pattern(probe(AppShell<'static, ()>))] pub struct AppShell<'a, S> { window: WindowKey, bare: bool, embedded: bool, /// An embedded shell as wide as the room of the flow it is shown in and as tall as its body's content and its top /// and bottom slots, from the measure its body keeps while its inputs hold; a body that fills takes at most this /// room. Shown with [`show`](Self::show), it takes that room in the flow. #[prop(name = "content_height", quantity = "room the host's flow offers, in points")] content_room: Option, /// The screen mode. Without a binding the shell keeps it, starting at `Standard`. #[prop] screen_mode: Bind<'a, ScreenMode>, /// Whether the Tab reveal and the edge reveal work in body-only mode. Off for a presentation. #[prop] allow_reveal: bool, /// Whether the pointer at the top, start or end edge reveals the hidden bars in body-only mode. #[prop] edge_reveal: bool, /// The host's flag for the document tabs, read back through `ShellCx::tabs_visible`. #[prop] document_tabs: bool, /// The name of the body region; the translated "Document" without it. #[prop(into)] body_label: Option