mirror of
https://github.com/zed-industries/zed.git
synced 2026-11-19 06:59:22 +01:00
gpui: Resolve window placement from active window (#61459)
Closes #33314 ### Objective On multi-monitor setups, opening a new window placed it on the primary display instead of the display I'm currently on. ### Solution GPUI now retains the active window bounds and display ID while it is on the update stack. New windows use this for placement, falling back to the primary display when no active display is available. ### Testing - Tested it on macOS 26 ### Self-Review Checklist: - [x] I've reviewed my own diff for quality, security, and reliability - [ ] Unsafe blocks (if any) have justifying comments - [x] The content adheres to Zed's UI standards ([UX/UI](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist) and [icon](https://github.com/zed-industries/zed/blob/main/crates/icons/README.md) guidelines) - [ ] Tests cover the new/changed behavior - [x] Performance impact has been considered and is acceptable Release Notes: - Fixed new windows always opening on the primary display - [GPUI] Added `App::active_display`; `WindowBounds::centered` now takes a `display_id`
This commit is contained in:
+55
-6
@@ -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<Cell<TextRenderingMode>>,
|
||||
|
||||
pub(crate) window_update_stack: Vec<WindowId>,
|
||||
pub(crate) window_update_stack: Vec<WindowUpdateEntry>,
|
||||
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<RwLock<EntityRefCounts>>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub(crate) struct WindowUpdateEntry {
|
||||
window_id: WindowId,
|
||||
bounds: WindowBounds,
|
||||
display_id: Option<DisplayId>,
|
||||
}
|
||||
|
||||
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<Rc<dyn PlatformDisplay>> {
|
||||
let (display_id, _) = self.active_window_placement()?;
|
||||
self.find_display(display_id?)
|
||||
}
|
||||
|
||||
pub(crate) fn active_window_placement(&self) -> Option<(Option<DisplayId>, 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<Window>, 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::<T>(),
|
||||
window: cx.window_update_stack.last().cloned(),
|
||||
window: cx.window_update_stack.last().map(|state| state.window_id),
|
||||
});
|
||||
|
||||
cx.entities.insert(slot, entity)
|
||||
|
||||
@@ -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<DisplayId>,
|
||||
|
||||
/// 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<Pixels>, 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<DisplayId>, size: Size<Pixels>, cx: &App) -> Self {
|
||||
WindowBounds::Windowed(Bounds::centered(display_id, size, cx))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+77
-16
@@ -1226,6 +1226,7 @@ pub struct Window {
|
||||
pub(crate) display_observers: SingleSubscriberSet<AnyObserver>,
|
||||
is_resizable: bool,
|
||||
is_minimizable: bool,
|
||||
last_window_bounds: WindowBounds,
|
||||
sprite_atlas: Arc<dyn PlatformAtlas>,
|
||||
text_system: Rc<WindowTextSystem>,
|
||||
text_rendering_mode: Rc<Cell<TextRenderingMode>>,
|
||||
@@ -1569,26 +1570,16 @@ pub(crate) struct ElementStateBox {
|
||||
pub(crate) type_name: &'static str,
|
||||
}
|
||||
|
||||
fn default_bounds(display_id: Option<DisplayId>, 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<WindowBounds>,
|
||||
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<DisplayId>, 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<DisplayId> {
|
||||
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<Self>) -> 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);
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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()
|
||||
},
|
||||
|
||||
@@ -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<Pixels> = 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| {
|
||||
|
||||
@@ -42,10 +42,16 @@ pub fn init(app_state: Arc<AppState>, 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| {
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user