mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-22 18:41:27 +01:00
ui-shell: embed::before_ui takes the root window's handle, outside the app feature
An app with its own eframe App and without ui-shell's app feature (which turns on egui's default fonts) could not call before_ui, so its root window on Windows got no hit sink: corner resizes and title-bar moves started late, through the grips' StartResize. before_ui now takes any raw_window_handle::HasWindowHandle (eframe's Frame), and it and the first fit build without app. Native only: the browser has no native window. The trial checks that with the sink installed, the window region's corner and title bar answer the platform's hit test and a corner drag sends no BeginResize.
This commit is contained in:
1 parent
5814eb87b1
commit
8d39ecb4db
6 files changed
+81
-12
No files matched your search
Generated
+1
@@ -4457,6 +4457,7 @@ dependencies = [
|
||||
"craft-window",
|
||||
"eframe",
|
||||
"egui",
|
||||
"raw-window-handle",
|
||||
"serde",
|
||||
"web-time",
|
||||
]
|
||||
|
||||
@@ -18,7 +18,7 @@ workspace = true
|
||||
suite = ["testing", "craft-ui-panels/suite"]
|
||||
default = []
|
||||
# `shell::app`: the eframe entry point.
|
||||
app = ["dep:eframe", "craft-window/ui"]
|
||||
app = ["dep:eframe"]
|
||||
# `craft_ui::testing`.
|
||||
testing = ["craft-ui-panels/testing"]
|
||||
|
||||
@@ -32,7 +32,8 @@ craft-ui-macros = { workspace = true }
|
||||
egui = { workspace = true }
|
||||
eframe = { version = "0.37", default-features = false, features = ["wgpu", "x11", "default_fonts"], optional = true }
|
||||
craft-host = { workspace = true }
|
||||
craft-window = { workspace = true }
|
||||
# `ui`: `desktop::fits`, for `embed::before_ui`'s first fit.
|
||||
craft-window = { workspace = true, features = ["ui"] }
|
||||
craft-i18n = { workspace = true, features = ["collate"] }
|
||||
# The saved forms of `ScreenMode`, `RecentView` and `RecentSort`.
|
||||
serde = { workspace = true }
|
||||
@@ -41,6 +42,10 @@ craft-svg = { workspace = true, features = ["draw"] }
|
||||
# `std::time::Instant` on native; the browser clock on wasm, where `Instant::now` panics.
|
||||
web-time = "1.1"
|
||||
|
||||
# `embed::before_ui` takes the root window's handle (eframe's `Frame`).
|
||||
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
|
||||
raw-window-handle = "0.6"
|
||||
|
||||
# `AppSpec::new` installs `craft_window::system::SystemWindows`, which reads AppKit without craft-window's egui layer.
|
||||
[target.'cfg(target_os = "macos")'.dependencies]
|
||||
craft-window = { workspace = true, features = ["appkit"] }
|
||||
|
||||
@@ -135,7 +135,10 @@ impl<A: ShellApp> eframe::App for Driver<A> {
|
||||
}
|
||||
|
||||
fn ui(&mut self, ui: &mut Ui, frame: &mut eframe::Frame) {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
embed::before_ui(ui.ctx(), frame);
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
let _ = frame;
|
||||
self.app.ui(ui);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
//! [`crate::runtime::keys::with_window_intents`], last in the app's UI.
|
||||
|
||||
use crate::fonts::{Stack, UiFonts};
|
||||
#[cfg(feature = "app")]
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
use crate::runtime::egui::memory;
|
||||
use crate::runtime::frame::{FrameInput, FrameOutput, set_input, take_output};
|
||||
use crate::runtime::windows;
|
||||
@@ -54,23 +54,25 @@ pub fn logic(ctx: &Context, close_requested: impl FnOnce() -> CloseDecision) {
|
||||
}
|
||||
|
||||
/// Each pass of the root window, from eframe's `App::ui`, before the app's UI: hands the runtime the root's native
|
||||
/// window where the runtime drives it (Windows), and fits the root window to its monitor once the monitor is known.
|
||||
#[cfg(feature = "app")]
|
||||
pub fn before_ui(ctx: &Context, frame: &eframe::Frame) {
|
||||
/// 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(frame));
|
||||
windows::route_root(ctx, || craft_window::native::NativeWindow::of_handle(window));
|
||||
#[cfg(not(windows))]
|
||||
let _ = frame;
|
||||
let _ = window;
|
||||
fit_once(ctx);
|
||||
}
|
||||
|
||||
/// Whether the fit has run for this context.
|
||||
#[cfg(feature = "app")]
|
||||
#[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(feature = "app")]
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn fit_once(ctx: &Context) {
|
||||
if memory::with::<Fitted, _>(ctx, |fitted| fitted.0) {
|
||||
return;
|
||||
@@ -84,7 +86,7 @@ fn fit_once(ctx: &Context) {
|
||||
/// 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(feature = "app")]
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn fit_first(ctx: &Context) -> bool {
|
||||
use crate::runtime::windows::{WindowAction, WindowHost};
|
||||
use craft_host::target::{Os, WindowState};
|
||||
|
||||
@@ -148,3 +148,61 @@ fn caption_events_from_the_window_procedure_drive_the_maximize_button() {
|
||||
button = maximize(&windows);
|
||||
assert!(!button.clicked() && !button.is_pointer_button_down_on(), "a press that ended away from the button is no click");
|
||||
}
|
||||
|
||||
/// The root window of an app on Windows that draws its own frame and hands the runtime its native window
|
||||
/// (`embed::before_ui`), so the hit sink is installed: the platform's hit test finds a corner resize 3 points inside the
|
||||
/// window's corner and the move area on the title bar, so the OS starts both at the press. A drag on the corner then
|
||||
/// sends no resize of the app's.
|
||||
#[trial]
|
||||
fn with_the_hit_sink_the_window_region_answers_the_platforms_hit_test() {
|
||||
use craft_host::target::{Caps, Os, ShellInfo, WindowChrome, WindowInfo, WindowKind};
|
||||
use craft_ui::contract::ShowContainer as _;
|
||||
use craft_ui::runtime::InstallSpec;
|
||||
use craft_ui::shell::{CaptionFamily, Captions, Frame, ShellRegion, Title, TitleBar};
|
||||
use craft_ui::testing::pointer::{moved, press, release};
|
||||
use craft_ui::testing::windows::NoWindows;
|
||||
use craft_window::policy::{Decision, Desktop, DesktopKind};
|
||||
use egui::ViewportCommand;
|
||||
use std::cell::Cell;
|
||||
use std::sync::Arc;
|
||||
|
||||
let shell = ShellInfo::default()
|
||||
.os(Os::Windows)
|
||||
.caps(Caps::native(Os::Windows))
|
||||
.window(WindowInfo::new(WindowKind::Main).chrome(WindowChrome::Integrated));
|
||||
let layout = craft_window::Layout { left: vec![], right: vec![Caption::Minimize, Caption::Maximize, Caption::Close] };
|
||||
let frame = Frame::Integrated(Captions { family: CaptionFamily::Windows, layout });
|
||||
let desktop = Desktop { kind: DesktopKind::Other, ssd: None, gtk_theme: None, icon_theme: None };
|
||||
let windows = Windows::with(InstallSpec::new(shell, Arc::new(NoWindows)).frame(Decision { frame, overridden: None, desktop }));
|
||||
let host = WindowHost::of(&windows.ctx);
|
||||
let _sink = host.install_hit_sink(ViewportId::ROOT);
|
||||
let (window, bar) = (Cell::new(Rect::NOTHING), Cell::new(Rect::NOTHING));
|
||||
let frame = |events: Vec<egui::Event>| -> Vec<ViewportCommand> {
|
||||
windows
|
||||
.frame(events, vec![], |ui| {
|
||||
let shown = ShellRegion::window().id_salt("root").show(ui, |ui, _| {
|
||||
bar.set(TitleBar::new().show_titled(ui, Title::new("Landscape.psd"), |_, _| {}, |_, _| {}).response.rect);
|
||||
});
|
||||
window.set(shown.output.response.rect);
|
||||
})
|
||||
.commands
|
||||
.into_iter()
|
||||
.map(|(_, command)| command)
|
||||
.collect()
|
||||
};
|
||||
for _ in 0..3 {
|
||||
frame(vec![]);
|
||||
}
|
||||
let regions = host.caption_regions(ViewportId::ROOT).expect("the window registered its parts");
|
||||
let corner = window.get().max - vec2(3.0, 3.0);
|
||||
assert_eq!(regions.hit(corner), Some(WindowHit::Resize(ResizeEdge::BottomRight)), "the corner is a resize for the platform: {regions:?}");
|
||||
let band = bar.get().center();
|
||||
assert_eq!(regions.hit(band), Some(WindowHit::Move), "the title bar is the move area for the platform: {regions:?}");
|
||||
let mut sent = frame(vec![moved(corner)]);
|
||||
sent.extend(frame(vec![press(corner)]));
|
||||
for step in 1..=4 {
|
||||
sent.extend(frame(vec![moved(corner + vec2(10.0, 10.0) * step as f32)]));
|
||||
}
|
||||
sent.extend(frame(vec![release(corner + vec2(40.0, 40.0))]));
|
||||
assert!(sent.iter().all(|command| !matches!(command, ViewportCommand::BeginResize(_))), "the platform resizes the window, not the app: {sent:?}");
|
||||
}
|
||||
@@ -143,7 +143,7 @@ An app with its own eframe `App` calls the hooks in `craft::ui::shell::embed`. `
|
||||
| Once, when the app is created | with the `CreationContext` | `embed::install(ctx, InstallSpec, &UiFonts)`: the runtime, then the app's fonts. Returns the font `Stack`. |
|
||||
| Each frame | `raw_input_hook` | `embed::before_input(ctx, \|output\| { ...; input })`: the closure receives the `FrameOutput` of the frame before (fired commands, announcements, chrome toggles) and returns the `FrameInput` for this frame (`RootEnv`, command states, the `Host`). |
|
||||
| Each frame | `logic`, after the app's own logic | `embed::logic(ctx, \|\| decision)`: the root window's close path. The closure runs only when the root window was asked to close, and returns `CloseDecision::Close` or `Hold`. |
|
||||
| Each pass | `ui`, before the app's UI | `embed::before_ui(ctx, frame)`: hands the runtime the native window on Windows, and fits the root window to its monitor once. Needs `ui-app`. |
|
||||
| Each pass | `ui`, before the app's UI | `embed::before_ui(ctx, frame)`: hands the runtime the root's native window (eframe's `Frame`, or any `raw_window_handle::HasWindowHandle`) on Windows, where the runtime answers the window's hit test (resize edges, the title bar's move area, snap layouts on the maximize button), and fits the root window to its monitor once. Native only; it needs no eframe feature of the facade. |
|
||||
| Each pass | `ui`, where the app draws its own document canvas | Optional: `ui::runtime::focus::host_canvas(ctx, id, KeyUse::CANVAS)` declares it as the window's canvas (Tab row below). |
|
||||
| Each pass | `ui`, last | Optional: `ui::runtime::keys::with_window_intents(ctx, rtl, \|intents\| ...)` reads the keys no component took (below). |
|
||||
|
||||
|
||||
Reference in new issue
Block a user