Panels: floating panels reopen on the monitor where they were left

Squashed from #6038 onto main.
This commit is contained in:
dotCooCoo
2026-11-08 11:22:40 +07:00
committed by bflatastic
parent 2f09d9998e
commit b17a744a9c
8 changed files with 162 additions and 0 deletions
+1
View File
@@ -114,6 +114,7 @@ mod services;
pub use app::MAX_RECENT_FONTS;
pub use caches::{CacheKey, CanvasCache, LivePaintGapsCache, Perf, PrintTilingCache, SliceCache};
pub use clock::{now_ms, now_secs};
pub use craft_ui::docking::Monitor as DockMonitor;
#[cfg(test)]
pub(crate) use services::browsable_url;
pub use services::{
+1
View File
@@ -1147,6 +1147,7 @@ pub(crate) fn show(app: &mut VectorcraftApp, ui: &mut egui::Ui) {
let mut dock = craft_ui::docking::DockArea::new(area)
.blocked_rects(&blocked)
.input_blocked(modal)
.monitors(app.services.dock_monitors.clone())
.auto_collapse_iconic(app.session.prefs.auto_collapse_icon_panels);
#[cfg(not(target_arch = "wasm32"))]
{
@@ -42,6 +42,8 @@ struct Host {
time: f64,
/// Every command sent to a window, in order.
commands: Vec<(ViewportId, ViewportCommand)>,
/// The monitor size each window reports.
monitor_size: Option<egui::Vec2>,
}
impl Host {
@@ -51,6 +53,7 @@ impl Host {
outer_rect: Some(rect),
focused: Some(self.focus == id),
native_pixels_per_point: Some(1.0),
monitor_size: self.monitor_size,
..Default::default()
};
std::iter::once((ViewportId::ROOT, info(ViewportId::ROOT, SCREEN.translate(ORIGIN.to_vec2()))))
@@ -199,6 +202,7 @@ impl Harness {
focus: ViewportId::ROOT,
time: 0.0,
commands: Vec::new(),
monitor_size: None,
}));
let shared = host.clone();
egui::Context::set_immediate_viewport_renderer(move |ctx, viewport| Host::render(&shared, ctx, viewport));
@@ -477,6 +481,61 @@ fn assert_submenu_reaches_its_end(h: &mut Harness, panel: &str, height: f32, row
assert!(end.bottom() <= height, "{last} at {end:?} is inside the {height} pt window");
}
/// A new launch on a desktop of `monitors`, with `saved` as the panel layout: where the window of
/// the floating Swatches panel opens.
fn reopened(saved: &craft_ui::docking::Layout<String>, monitors: &[crate::DockMonitor]) -> Rect {
let mut h = Harness::new();
h.host.borrow_mut().monitor_size = Some(vec2(1920.0, 1080.0));
h.app.services.dock_monitors = monitors.to_vec();
h.app.ui.docking = Some(saved.clone());
h.frames(3);
let viewport = h.viewport("swatches");
h.host.borrow().windows[&viewport].rect
}
/// Two monitors side by side, with the main window on the left one (#5582).
#[test]
fn a_floating_panel_reopens_on_the_monitor_where_it_was_left() {
let left = crate::DockMonitor { name: Some("left".into()), rect: [0.0, 0.0, 1920.0, 1080.0], scale: 1.0 };
let right = crate::DockMonitor { name: Some("right".into()), rect: [1920.0, 0.0, 1920.0, 1080.0], scale: 1.0 };
let both = [left.clone(), right.clone()];
let mut h = Harness::new();
h.host.borrow_mut().monitor_size = Some(vec2(1920.0, 1080.0));
h.app.services.dock_monitors = both.to_vec();
let viewport = h.float("swatches");
let size = h.host.borrow().windows[&viewport].rect.size();
let placed_at = pos2(2400.0, 300.0);
h.host.borrow_mut().windows.get_mut(&viewport).unwrap().rect = Rect::from_min_size(placed_at, size);
// The layout follows the window after craft-ui's wait for a move to settle, half a second.
h.frames(30);
let layout = layout_of(&h.app.ui);
let window = &layout.floating[0];
assert_eq!(window.native.map(|[x, y, ..]| pos2(x, y)), Some(placed_at));
assert_eq!(window.monitor.as_ref(), Some(&right), "the window stores the monitor it is on");
// The preferences keep the layout as JSON.
let saved: craft_ui::docking::Layout<String> = serde_json::from_str(&serde_json::to_string(&layout).unwrap()).unwrap();
assert_eq!(reopened(&saved, &both).min, placed_at, "the monitor is connected: the saved place");
// The monitor got a new name and the same place.
let renamed = [left.clone(), crate::DockMonitor { name: Some("DP-2".into()), ..right.clone() }];
assert_eq!(reopened(&saved, &renamed).min, placed_at);
// A layout saved before the monitors were listed stores none.
let mut unstored = saved.clone();
unstored.floating[0].monitor = None;
assert_eq!(reopened(&unstored, &both).min, placed_at, "a place on a connected monitor is kept");
// The monitor is gone: the main window's monitor, at the same offset from its origin.
assert_eq!(reopened(&saved, std::slice::from_ref(&left)).min, pos2(placed_at.x - 1920.0, placed_at.y));
// No monitor list (a host that can't list them): onto the main window's monitor by egui's size.
let no_list = reopened(&saved, &[]);
assert_eq!(no_list.min, pos2(1920.0 - size.x, placed_at.y));
}
#[test]
fn keys_typed_in_a_native_window_run_the_main_windows_shortcuts() {
let mut h = Harness::new();
+5
View File
@@ -169,6 +169,11 @@ pub struct Services {
/// bar's open menu) for floating panels in OS windows of their own: eframe runs the host's
/// `raw_input_hook` for the main window only.
pub native_input_hook: Option<NativeInputHook>,
/// The connected monitors, kept current by the desktop host: floating panels in OS windows of
/// their own store the monitor they are on and reopen on it. The rects are in the points egui
/// reports for a window on that monitor. Empty on the web, and where the host cannot place a
/// window by these points, which sends floating panels to the main window's monitor.
pub dock_monitors: Vec<craft_ui::docking::Monitor>,
}
/// Rewrites one window's raw input before egui reads it ([`Services::native_input_hook`]).