mirror of
https://github.com/storytold/craft-libs.git
synced 2026-12-01 10:48:15 +01:00
171 lines
7.2 KiB
Rust
171 lines
7.2 KiB
Rust
//! The hooks an app with its own eframe `App` calls to host the runtime: [`install`] once, then [`before_input`] and
|
|
//! [`logic`] each frame and [`before_ui`] each pass, in that order. `app::run`'s driver is such an app and calls the
|
|
//! same hooks. What the app calls where, and what the install takes over, is in docs/craft-framework.md section 2.2.
|
|
//! With feature `app`, native apps can opt into `listed_monitors(cc)` as the window system in [`InstallSpec`].
|
|
//!
|
|
//! The window's keys that no component took (an Escape while nothing has framework focus) are read with
|
|
//! [`crate::runtime::keys::with_window_intents`], last in the app's UI.
|
|
|
|
use crate::fonts::{Stack, UiFonts};
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
use crate::runtime::egui::memory;
|
|
use crate::runtime::frame::{FrameInput, FrameOutput, set_input, take_output};
|
|
use crate::runtime::windows;
|
|
use crate::runtime::{InstallError, InstallSpec};
|
|
use egui::Context;
|
|
|
|
/// What the app decides when the root window is asked to close.
|
|
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
|
#[non_exhaustive]
|
|
pub enum CloseDecision {
|
|
/// The window may close; the platform quit takes it from there.
|
|
#[default]
|
|
Close,
|
|
/// The window stays open, brought forward, while the app asks (for example about unsaved documents) and closes
|
|
/// it with `WindowAction::Close` once the answer allows.
|
|
Hold,
|
|
}
|
|
|
|
/// Installs the runtime on `ctx`, then `fonts`. Call it once, when the app is created (eframe's `CreationContext`).
|
|
/// Returns the installed font stack; an error when the context has the runtime already or `spec` replaces one style
|
|
/// key twice. An app that installs its fonts again later (a language change) calls [`UiFonts::install`] itself.
|
|
pub fn install(ctx: &Context, spec: InstallSpec, fonts: &UiFonts<'_>) -> Result<Stack, InstallError> {
|
|
crate::install(ctx, spec)?;
|
|
Ok(fonts.install(ctx))
|
|
}
|
|
|
|
/// Before each frame, from eframe's `App::raw_input_hook`: hands `app` what the runtime accumulated since the last
|
|
/// frame (fired commands, announcements, the chrome toggle) and gives the coming passes the input `app` returns
|
|
/// (the root env, the command states, the host's services). The app reads the output first, so the input it returns
|
|
/// already reflects it.
|
|
pub fn before_input(ctx: &Context, app: impl FnOnce(FrameOutput) -> FrameInput) {
|
|
let input = app(take_output(ctx));
|
|
set_input(ctx, input);
|
|
}
|
|
|
|
/// Each frame, from eframe's `App::logic`, after the app's own logic: runs the root window's close path. When the
|
|
/// root window was asked to close and no modal layer refuses it, `close_requested` decides; then the platform quit
|
|
/// takes the close where the platform has one. eframe runs `logic` while the window is minimized or hidden too, so
|
|
/// the close path runs there and nowhere else.
|
|
pub fn logic(ctx: &Context, close_requested: impl FnOnce() -> CloseDecision) {
|
|
if windows::root_close_requested(ctx) && close_requested() == CloseDecision::Hold {
|
|
windows::hold_close(ctx);
|
|
}
|
|
windows::logic(ctx);
|
|
}
|
|
|
|
/// Each pass of the root window, from eframe's `App::ui`, before the app's UI: hands the runtime the root's native
|
|
/// window (`window`, eframe's `Frame`) where the runtime drives it (Windows), and fits the root window to its monitor
|
|
/// once the monitor is known. Native only: in the browser the page owns the canvas's size and there is no native
|
|
/// window.
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
pub fn before_ui(ctx: &Context, window: &impl raw_window_handle::HasWindowHandle) {
|
|
#[cfg(windows)]
|
|
windows::route_root(ctx, || craft_window::native::NativeWindow::of_handle(window));
|
|
#[cfg(not(windows))]
|
|
let _ = window;
|
|
fit_once(ctx);
|
|
}
|
|
|
|
/// Whether the fit has run for this context.
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
#[derive(Default)]
|
|
struct Fitted(bool);
|
|
|
|
/// Runs [`fit_first`] from the first passes until its monitor is known, then never again for this context.
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
fn fit_once(ctx: &Context) {
|
|
if memory::with::<Fitted, _>(ctx, |fitted| fitted.0) {
|
|
return;
|
|
}
|
|
if fit_first(ctx) {
|
|
memory::with::<Fitted, _>(ctx, |fitted| fitted.0 = true);
|
|
}
|
|
}
|
|
|
|
/// Maximizes the root window, once its monitor is known, when it is larger than the monitor's usable area (Photoshop
|
|
/// opens maximized on a small display). Never on macOS, which keeps new windows inside the screen's visible frame
|
|
/// itself, and never for a window that is already maximized or in full screen. Returns whether the monitor was
|
|
/// known, which is when the fit has run.
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
fn fit_first(ctx: &Context) -> bool {
|
|
use crate::runtime::windows::{WindowAction, WindowHost};
|
|
use craft_host::target::{Os, WindowState};
|
|
use craft_window::desktop::fits;
|
|
use egui::ViewportId;
|
|
let host = WindowHost::of(ctx);
|
|
let monitor = ctx.input(|input| input.viewport().monitor_size);
|
|
let (Some(facts), Some(monitor)) = (host.facts(ViewportId::ROOT), monitor) else { return false };
|
|
let outer = facts.outer.unwrap_or(facts.inner);
|
|
if windows::os_of(ctx) != Os::Mac
|
|
&& facts.state == WindowState::Normal
|
|
&& monitor.is_finite()
|
|
&& outer.is_finite()
|
|
&& !fits(outer.size(), monitor)
|
|
{
|
|
host.act(ViewportId::ROOT, WindowAction::SetMaximized(true));
|
|
}
|
|
true
|
|
}
|
|
|
|
/// Lists the root window's distinct monitors through an opt-in window system.
|
|
#[cfg(all(feature = "app", not(target_arch = "wasm32")))]
|
|
pub fn listed_monitors(cc: &eframe::CreationContext<'_>) -> std::sync::Arc<dyn craft_window::system::WindowSystem> {
|
|
use craft_behavior::{Monitor, MonitorKey};
|
|
use craft_window::system::{HeldButtons, SystemWindows, WindowSystem};
|
|
use std::collections::BTreeSet;
|
|
use std::sync::Arc;
|
|
|
|
struct ListedMonitors {
|
|
system: SystemWindows,
|
|
monitors: Vec<Monitor>,
|
|
}
|
|
|
|
impl WindowSystem for ListedMonitors {
|
|
fn desktop_pointer(&self) -> Option<[f32; 2]> {
|
|
self.system.desktop_pointer()
|
|
}
|
|
|
|
fn buttons_held(&self) -> Option<HeldButtons> {
|
|
self.system.buttons_held()
|
|
}
|
|
|
|
fn discard_marked_text(&self) -> bool {
|
|
self.system.discard_marked_text()
|
|
}
|
|
|
|
fn command_held(&self) -> Option<bool> {
|
|
self.system.command_held()
|
|
}
|
|
|
|
fn warp_pointer(&self, to: [f32; 2]) -> bool {
|
|
self.system.warp_pointer(to)
|
|
}
|
|
|
|
fn monitors(&self) -> Vec<Monitor> {
|
|
self.monitors.clone()
|
|
}
|
|
}
|
|
|
|
let monitors = cc.winit_window().map_or_else(Vec::new, |window| {
|
|
// Wayland may repeat an output handle; distinct handles can have the same name and size.
|
|
window
|
|
.available_monitors()
|
|
.collect::<BTreeSet<_>>()
|
|
.into_iter()
|
|
.map(|handle| {
|
|
let position = handle.position();
|
|
let size = handle.size();
|
|
let size_px = [size.width, size.height];
|
|
Monitor::new(
|
|
MonitorKey::new(handle.name().unwrap_or_default(), size_px),
|
|
[position.x, position.y],
|
|
size_px,
|
|
handle.scale_factor() as f32,
|
|
)
|
|
})
|
|
.collect()
|
|
});
|
|
Arc::new(ListedMonitors { system: SystemWindows, monitors })
|
|
}
|