diff --git a/crates/gpui/src/app.rs b/crates/gpui/src/app.rs index 75da2eb6e50..cee926ba211 100644 --- a/crates/gpui/src/app.rs +++ b/crates/gpui/src/app.rs @@ -55,8 +55,8 @@ use crate::{ PromptButton, PromptHandle, PromptLevel, Render, RenderImage, RenderablePromptHandle, Reservation, ScreenCaptureSource, SharedString, SubscriberSet, Subscription, SvgRenderer, SystemNotification, SystemNotificationResponse, Task, TextRenderingMode, TextSystem, - ThermalState, Window, WindowAppearance, WindowButtonLayout, WindowHandle, WindowId, - WindowInvalidator, WindowingRequest, + ThermalState, Window, WindowAppearance, WindowBounds, WindowButtonLayout, WindowHandle, + WindowId, WindowInvalidator, WindowingRequest, colors::{Colors, GlobalColors}, hash, init_app_menus, }; @@ -971,7 +971,7 @@ pub struct App { pub(crate) name: Option<&'static str>, pub(crate) text_rendering_mode: Rc>, - pub(crate) window_update_stack: Vec, + pub(crate) window_update_stack: Vec, pub(crate) mode: GpuiMode, pub(crate) cursor_hide_mode: CursorHideMode, pub(crate) reduce_motion: bool, @@ -991,6 +991,13 @@ pub struct App { _ref_counts: Arc>, } +#[derive(Clone, Copy)] +pub(crate) struct WindowUpdateEntry { + window_id: WindowId, + bounds: WindowBounds, + display_id: Option, +} + impl App { #[allow(clippy::new_ret_no_self)] pub(crate) fn new_app( @@ -1599,6 +1606,40 @@ impl App { self.platform.active_window() } + /// Returns the display containing the window that is currently focused at the platform level. + pub fn active_display(&self) -> Option> { + let (display_id, _) = self.active_window_placement()?; + self.find_display(display_id?) + } + + pub(crate) fn active_window_placement(&self) -> Option<(Option, WindowBounds)> { + let window_id = self.active_window()?.window_id(); + + if let Some(state) = self + .window_update_stack + .iter() + .rev() + .find(|state| state.window_id == window_id) + { + return Some((state.display_id, state.bounds)); + } + + let window = self.windows.get(window_id)?.as_ref()?; + Some((window.display_id(), window.last_window_bounds())) + } + + pub(crate) fn sync_window_update_entry(&mut self, window: &Window) { + if let Some(state) = self + .window_update_stack + .iter_mut() + .rev() + .find(|state| state.window_id == window.handle.id) + { + state.bounds = window.last_window_bounds(); + state.display_id = window.display_id(); + } + } + /// Opens a new window with the given option and the root view returned by the given function. /// The function is invoked with a `Window`, which can be used to interact with window-specific /// functionality. @@ -1612,7 +1653,11 @@ impl App { let handle = WindowHandle::new(id); match Window::new(handle.into(), options, cx) { Ok(mut window) => { - cx.window_update_stack.push(id); + cx.window_update_stack.push(WindowUpdateEntry { + window_id: id, + bounds: window.last_window_bounds(), + display_id: window.display_id(), + }); let root_view = build_root_view(&mut window, cx); cx.window_update_stack.pop(); window.root.replace(root_view.into()); @@ -3264,7 +3309,11 @@ impl App { let root_view = window.root.clone().unwrap(); - cx.window_update_stack.push(window.handle.id); + cx.window_update_stack.push(WindowUpdateEntry { + window_id: window.handle.id, + bounds: window.last_window_bounds(), + display_id: window.display_id(), + }); update(Some((root_view, &mut window, cx))); fn trail(window_id: WindowId, window: Box, cx: &mut App) -> Option<()> { cx.window_update_stack.pop(); @@ -3349,7 +3398,7 @@ impl AppContext for App { cx.push_effect(Effect::EntityCreated { entity: handle.into_any(), tid: TypeId::of::(), - window: cx.window_update_stack.last().cloned(), + window: cx.window_update_stack.last().map(|state| state.window_id), }); cx.entities.insert(slot, entity) diff --git a/crates/gpui/src/platform.rs b/crates/gpui/src/platform.rs index 8f44cc02308..a1bbff3cd24 100644 --- a/crates/gpui/src/platform.rs +++ b/crates/gpui/src/platform.rs @@ -2687,8 +2687,8 @@ pub struct WindowOptions { /// Whether the window should be minimized by the user pub is_minimizable: bool, - /// The display to create the window on, if this is None, - /// the window will be created on the main display + /// The display to create the window on. If neither this nor `window_bounds` is set, the window + /// is created on the active window's display. Otherwise, it falls back to the primary display. pub display_id: Option, /// The appearance of the window background. @@ -2811,9 +2811,10 @@ impl WindowBounds { } } - /// Creates a new window bounds that centers the window on the screen. - pub fn centered(size: Size, cx: &App) -> Self { - WindowBounds::Windowed(Bounds::centered(None, size, cx)) + /// Creates window bounds centered on the given display, or the primary display if none is + /// provided. + pub fn centered(display_id: Option, size: Size, cx: &App) -> Self { + WindowBounds::Windowed(Bounds::centered(display_id, size, cx)) } } diff --git a/crates/gpui/src/window.rs b/crates/gpui/src/window.rs index 28752574dc8..0bf91d489c3 100644 --- a/crates/gpui/src/window.rs +++ b/crates/gpui/src/window.rs @@ -1226,6 +1226,7 @@ pub struct Window { pub(crate) display_observers: SingleSubscriberSet, is_resizable: bool, is_minimizable: bool, + last_window_bounds: WindowBounds, sprite_atlas: Arc, text_system: Rc, text_rendering_mode: Rc>, @@ -1569,26 +1570,16 @@ pub(crate) struct ElementStateBox { pub(crate) type_name: &'static str, } -fn default_bounds(display_id: Option, cx: &mut App) -> WindowBounds { - // TODO, BUG: if you open a window with the currently active window - // on the stack, this will erroneously fallback to `None` - // - // TODO these should be the initial window bounds not considering maximized/fullscreen - let active_window_bounds = cx - .active_window() - .and_then(|w| w.update(cx, |_, window, _| window.window_bounds()).ok()); - +fn default_bounds( + active_window_bounds: Option, + display: Option<&dyn PlatformDisplay>, +) -> WindowBounds { const CASCADE_OFFSET: f32 = 25.0; - let display = display_id - .map(|id| cx.find_display(id)) - .unwrap_or_else(|| cx.primary_display()); - let default_placement = || Bounds::new(point(px(0.), px(0.)), DEFAULT_WINDOW_SIZE); // Use visible_bounds to exclude taskbar/dock areas let display_bounds = display - .as_ref() .map(|d| d.visible_bounds()) .unwrap_or_else(default_placement); @@ -1606,7 +1597,6 @@ fn default_bounds(display_id: Option, cx: &mut App) -> WindowBounds { }, None => ( display - .as_ref() .map(|d| d.default_bounds()) .unwrap_or_else(default_placement), WindowBounds::Windowed, @@ -1673,7 +1663,25 @@ impl Window { .as_ref() .and_then(|titlebar| titlebar.title.clone()); - let window_bounds = window_bounds.unwrap_or_else(|| default_bounds(display_id, cx)); + let active_window_placement = cx.active_window_placement(); + // Explicit bounds only make sense on the display they were computed for. + let target_display_id = display_id.or_else(|| { + active_window_placement + .filter(|_| window_bounds.is_none()) + .and_then(|(display_id, _)| display_id) + }); + let display = target_display_id + .and_then(|display_id| cx.find_display(display_id)) + .or_else(|| cx.primary_display()); + let display_id = display + .as_ref() + .map(|display| display.id()) + .or(target_display_id); + let active_window_bounds = active_window_placement + .filter(|(active_display_id, _)| *active_display_id == display_id) + .map(|(_, bounds)| bounds); + let window_bounds = window_bounds + .unwrap_or_else(|| default_bounds(active_window_bounds, display.as_deref())); let mut platform_window = cx.platform.open_window( handle, WindowParams { @@ -2357,6 +2365,7 @@ impl Window { display_observers: SingleSubscriberSet::new(), is_resizable, is_minimizable, + last_window_bounds: window_bounds, sprite_atlas, text_system, text_rendering_mode: cx.text_rendering_mode.clone(), @@ -2740,6 +2749,14 @@ impl Window { self.platform_window.window_bounds() } + pub(crate) fn last_window_bounds(&self) -> WindowBounds { + self.last_window_bounds + } + + pub(crate) fn display_id(&self) -> Option { + self.display_id + } + /// Return the `WindowBounds` excluding insets (Wayland and X11) pub fn inner_window_bounds(&self) -> WindowBounds { self.platform_window.inner_window_bounds() @@ -3003,6 +3020,8 @@ impl Window { pub fn bounds_changed(&mut self, cx: &mut App) { self.scale_factor = self.platform_window.scale_factor(); self.viewport_size = self.platform_window.content_size(); + self.last_window_bounds = self.platform_window.window_bounds(); + cx.sync_window_update_entry(self); self.mouse_position = self.platform_window.mouse_position(); self.refresh(); @@ -8608,6 +8627,48 @@ mod tests { } } + #[gpui::test] + fn test_window_opened_from_bounds_observer_uses_new_bounds(cx: &mut TestAppContext) { + use crate::Subscription; + + struct BoundsObserver { + _subscription: Subscription, + } + + impl Render for BoundsObserver { + fn render(&mut self, _: &mut Window, _: &mut Context) -> impl IntoElement { + Empty + } + } + + let opened_window = Rc::new(Cell::new(None)); + let window = cx.add_window({ + let opened_window = opened_window.clone(); + move |window, cx| BoundsObserver { + _subscription: cx.observe_window_bounds(window, move |_, _, cx| { + let handle = cx + .open_window(WindowOptions::default(), |_, cx| cx.new(|_| EmptyView)) + .unwrap(); + opened_window.set(Some(AnyWindowHandle::from(handle))); + }), + } + }); + window + .update(cx, |_, window, _| window.activate_window()) + .unwrap(); + + let new_size = size(px(400.), px(300.)); + cx.simulate_window_resize(window.into(), new_size); + + let opened_window = opened_window + .take() + .expect("bounds observer should have opened a window"); + let opened_size = opened_window + .update(cx, |_, window, _| window.bounds().size) + .unwrap(); + assert_eq!(opened_size, new_size); + } + #[gpui::test] fn test_fully_visible_bounds_preserve_layout_viewport(cx: &mut TestAppContext) { let window = cx.add_window(|_, _| EmptyView); diff --git a/crates/gpui_linux/src/linux/wayland/window.rs b/crates/gpui_linux/src/linux/wayland/window.rs index 6647a8ef7f7..ae4b522d361 100644 --- a/crates/gpui_linux/src/linux/wayland/window.rs +++ b/crates/gpui_linux/src/linux/wayland/window.rs @@ -1114,6 +1114,8 @@ impl WaylandWindowStatePtr { if let Some(mut configure) = state.in_progress_configure.take() { let got_unmaximized = state.maximized && !configure.maximized; + let mode_changed = state.fullscreen != configure.fullscreen + || state.maximized != configure.maximized; state.fullscreen = configure.fullscreen; state.maximized = configure.maximized; state.tiling = configure.tiling; @@ -1141,6 +1143,7 @@ impl WaylandWindowStatePtr { } } } + let resized = configure.size.is_some_and(|size| size != state.bounds.size); drop(state); if visibility_changed { self.report_visibility(configure.visibility); @@ -1151,6 +1154,14 @@ impl WaylandWindowStatePtr { if let Some(size) = configure.size { self.resize(size); } + // Same-size mode changes skip the resize callback, so report them as a move. + if mode_changed && !resized { + let callback = self.callbacks.borrow_mut().moved.take(); + if let Some(mut fun) = callback { + fun(); + self.callbacks.borrow_mut().moved = Some(fun); + } + } } } let state = self.state.borrow_mut(); diff --git a/crates/gpui_linux/src/linux/x11/window.rs b/crates/gpui_linux/src/linux/x11/window.rs index f6ee76ae8ca..b32047cea1b 100644 --- a/crates/gpui_linux/src/linux/x11/window.rs +++ b/crates/gpui_linux/src/linux/x11/window.rs @@ -305,6 +305,11 @@ impl X11WindowState { renderer.update_transparency(is_transparent); } } + + fn is_maximized(&self) -> bool { + // A maximized window that gets minimized will still retain its maximized state. + !self.hidden && self.maximized_vertical && self.maximized_horizontal + } } #[derive(Clone)] @@ -1121,6 +1126,7 @@ impl X11WindowStatePtr { .map(|chunk| u32::from_ne_bytes([chunk[0], chunk[1], chunk[2], chunk[3]])); let was_active = state.active; + let was_maximized = state.maximized_vertical && state.maximized_horizontal; state.active = false; state.fullscreen = false; state.maximized_vertical = false; @@ -1147,6 +1153,19 @@ impl X11WindowStatePtr { set_wm_hints_urgency(&self.xcb, self.x_window, false); } + // Maximized changes without a geometry change get no ConfigureNotify, so report them as a move. + // This ignores `hidden`, since minimizing shouldn't notify bounds observers. + let maximized_changed = + (state.maximized_vertical && state.maximized_horizontal) != was_maximized; + drop(state); + if maximized_changed { + let callback = self.callbacks.borrow_mut().moved.take(); + if let Some(mut fun) = callback { + fun(); + self.callbacks.borrow_mut().moved = Some(fun); + } + } + Ok(()) } @@ -1390,10 +1409,7 @@ impl PlatformWindow for X11Window { } fn is_maximized(&self) -> bool { - let state = self.0.state.borrow(); - - // A maximized window that gets minimized will still retain its maximized state. - !state.hidden && state.maximized_vertical && state.maximized_horizontal + self.0.state.borrow().is_maximized() } fn window_bounds(&self) -> WindowBounds { diff --git a/crates/miniprofiler_ui/src/miniprofiler_ui.rs b/crates/miniprofiler_ui/src/miniprofiler_ui.rs index 6f6ccf7156d..088080a6ac4 100644 --- a/crates/miniprofiler_ui/src/miniprofiler_ui.rs +++ b/crates/miniprofiler_ui/src/miniprofiler_ui.rs @@ -153,6 +153,7 @@ fn open_performance_profiler( let default_bounds = size(px(1280.), px(720.)); cx.defer(move |cx| { + let display_id = cx.active_display().map(|display| display.id()); cx.open_window( WindowOptions { titlebar: Some(TitlebarOptions { @@ -167,7 +168,8 @@ fn open_performance_profiler( window_background, window_decorations: None, window_min_size: Some(default_bounds), - window_bounds: Some(WindowBounds::centered(default_bounds, cx)), + window_bounds: Some(WindowBounds::centered(display_id, default_bounds, cx)), + display_id, ..Default::default() }, |_window, cx| ProfilerWindow::new(startup_time, Some(workspace_handle), cx), diff --git a/crates/settings_ui/src/pages/audio_test_window.rs b/crates/settings_ui/src/pages/audio_test_window.rs index f1441b08339..26de0b460a3 100644 --- a/crates/settings_ui/src/pages/audio_test_window.rs +++ b/crates/settings_ui/src/pages/audio_test_window.rs @@ -282,6 +282,7 @@ pub fn open_audio_test_window(_window: &mut Window, cx: &mut App) { width: px(400.0), height: px(240.0), }; + let display_id = cx.active_display().map(|display| display.id()); cx.open_window( WindowOptions { @@ -297,7 +298,8 @@ pub fn open_audio_test_window(_window: &mut Window, cx: &mut App) { window_background: cx.theme().window_background_appearance(), app_id: Some(app_id.to_owned()), window_decorations: Some(gpui::WindowDecorations::Client), - window_bounds: Some(WindowBounds::centered(window_size, cx)), + window_bounds: Some(WindowBounds::centered(display_id, window_size, cx)), + display_id, window_min_size: Some(window_min_size), ..Default::default() }, diff --git a/crates/settings_ui/src/settings_ui.rs b/crates/settings_ui/src/settings_ui.rs index e167c5cc7e2..8d2de7f8b03 100644 --- a/crates/settings_ui/src/settings_ui.rs +++ b/crates/settings_ui/src/settings_ui.rs @@ -881,6 +881,7 @@ fn open_settings_editor_with( let default_rem_size = 16.0; let scale_factor = current_rem_size / default_rem_size; let scaled_bounds: gpui::Size = default_bounds.map(|axis| axis * scale_factor); + let display_id = cx.active_display().map(|display| display.id()); let app_id = ReleaseChannel::global(cx).app_id(); let window_decorations = match std::env::var("ZED_WINDOW_DECORATIONS") { @@ -914,7 +915,8 @@ fn open_settings_editor_with( width: SIDEBAR_WIDTH + CONTENT_MIN_WIDTH, height: px(240.0), }), - window_bounds: Some(WindowBounds::centered(scaled_bounds, cx)), + window_bounds: Some(WindowBounds::centered(display_id, scaled_bounds, cx)), + display_id, ..Default::default() }, |window, cx| { diff --git a/crates/zed/src/watcher_debug.rs b/crates/zed/src/watcher_debug.rs index bbafd1f0919..7077a729cec 100644 --- a/crates/zed/src/watcher_debug.rs +++ b/crates/zed/src/watcher_debug.rs @@ -42,10 +42,16 @@ pub fn init(app_state: Arc, cx: &mut App) { } let app_state = app_state.clone(); + let display_id = cx.active_display().map(|display| display.id()); cx.open_window( WindowOptions { titlebar: Some(TitlebarOptions::default()), - window_bounds: Some(WindowBounds::centered(size(px(1000.), px(700.)), cx)), + window_bounds: Some(WindowBounds::centered( + display_id, + size(px(1000.), px(700.)), + cx, + )), + display_id, ..Default::default() }, |window, cx| { diff --git a/crates/zed/src/zed.rs b/crates/zed/src/zed.rs index 739202e348b..d22eb41563b 100644 --- a/crates/zed/src/zed.rs +++ b/crates/zed/src/zed.rs @@ -1727,6 +1727,7 @@ fn open_about_window(cx: &mut App) { width: px(440.), height: px(300.), }; + let display_id = cx.active_display().map(|display| display.id()); cx.open_window( WindowOptions { @@ -1735,7 +1736,8 @@ fn open_about_window(cx: &mut App) { appears_transparent: true, traffic_light_position: Some(point(px(12.), px(12.))), }), - window_bounds: Some(WindowBounds::centered(window_size, cx)), + window_bounds: Some(WindowBounds::centered(display_id, window_size, cx)), + display_id, is_resizable: false, is_minimizable: false, kind: WindowKind::Floating,