collab ui: Fix notification windows on external monitors (#9817)

Sharing a project displays a notification (window) on every screen.
Previously there was an issue with the positioning of windows on all
screens except the primary screen.

As you can see here:


![image](https://github.com/zed-industries/zed/assets/53836821/314cf367-8c70-4e8e-bc4a-dcbb99cb4f71)

Now:


![image](https://github.com/zed-industries/zed/assets/53836821/42af9ef3-8af9-453a-ad95-147b5f9d90ba)

@mikayla-maki and I also decided to refactor the `WindowOptions` a bit. 
Previously you could specify bounds which controlled the positioning and
size of the window in the global coordinate space, while also providing
a display id (which screen to show the window on). This can lead to
unusual behavior because you could theoretically specify a global bound
which does not even belong to the display id which was provided.

Therefore we changed the api to this:
```rust
struct WindowOptions {
    /// The bounds of the window in screen coordinates
    /// None -> inherit, Some(bounds) -> set bounds.
    pub bounds: Option<Bounds<DevicePixels>>,

    /// The display to create the window on, if this is None,
    /// the window will be created on the main display
    pub display_id: Option<DisplayId>,
}
```

This lets you specify a display id, which maps to the screen where the
window should be created and bounds relative to the upper left of the
screen.

Release Notes:

- Fixed positioning of popup windows (e.g. when sharing a project) when
using multiple external displays.

---------

Co-authored-by: Conrad Irwin <conrad.irwin@gmail.com>
This commit is contained in:
Bennet Bo Fenner 2024-03-26 21:07:38 +01:00 committed by GitHub
parent ffd698be14
commit e272acd1bc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
25 changed files with 331 additions and 352 deletions

View file

@ -59,7 +59,7 @@ impl sqlez::bindable::Column for SerializedAxis {
}
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct SerializedWindowsBounds(pub(crate) Bounds<gpui::GlobalPixels>);
pub(crate) struct SerializedWindowsBounds(pub(crate) Bounds<gpui::DevicePixels>);
impl StaticColumnCount for SerializedWindowsBounds {
fn column_count() -> usize {
@ -73,10 +73,10 @@ impl Bind for SerializedWindowsBounds {
statement.bind(
&(
SerializedGlobalPixels(self.0.origin.x),
SerializedGlobalPixels(self.0.origin.y),
SerializedGlobalPixels(self.0.size.width),
SerializedGlobalPixels(self.0.size.height),
SerializedDevicePixels(self.0.origin.x),
SerializedDevicePixels(self.0.origin.y),
SerializedDevicePixels(self.0.size.width),
SerializedDevicePixels(self.0.size.height),
),
next_index,
)
@ -89,10 +89,10 @@ impl Column for SerializedWindowsBounds {
let bounds = match window_state.as_str() {
"Fixed" => {
let ((x, y, width, height), _) = Column::column(statement, next_index)?;
let x: f64 = x;
let y: f64 = y;
let width: f64 = width;
let height: f64 = height;
let x: i32 = x;
let y: i32 = y;
let width: i32 = width;
let height: i32 = height;
SerializedWindowsBounds(Bounds {
origin: point(x.into(), y.into()),
size: size(width.into(), height.into()),
@ -106,17 +106,16 @@ impl Column for SerializedWindowsBounds {
}
#[derive(Clone, Debug, PartialEq)]
struct SerializedGlobalPixels(gpui::GlobalPixels);
impl sqlez::bindable::StaticColumnCount for SerializedGlobalPixels {}
struct SerializedDevicePixels(gpui::DevicePixels);
impl sqlez::bindable::StaticColumnCount for SerializedDevicePixels {}
impl sqlez::bindable::Bind for SerializedGlobalPixels {
impl sqlez::bindable::Bind for SerializedDevicePixels {
fn bind(
&self,
statement: &sqlez::statement::Statement,
start_index: i32,
) -> anyhow::Result<i32> {
let this: f64 = self.0.into();
let this: f32 = this as _;
let this: i32 = self.0.into();
this.bind(statement, start_index)
}
}

View file

@ -6,7 +6,7 @@ use db::sqlez::{
bindable::{Bind, Column, StaticColumnCount},
statement::Statement,
};
use gpui::{AsyncWindowContext, Bounds, GlobalPixels, Model, Task, View, WeakView};
use gpui::{AsyncWindowContext, Bounds, DevicePixels, Model, Task, View, WeakView};
use project::Project;
use std::{
path::{Path, PathBuf},
@ -69,7 +69,7 @@ pub(crate) struct SerializedWorkspace {
pub(crate) id: WorkspaceId,
pub(crate) location: WorkspaceLocation,
pub(crate) center_group: SerializedPaneGroup,
pub(crate) bounds: Option<Bounds<GlobalPixels>>,
pub(crate) bounds: Option<Bounds<DevicePixels>>,
pub(crate) fullscreen: bool,
pub(crate) display: Option<Uuid>,
pub(crate) docks: DockStructure,

View file

@ -27,8 +27,8 @@ use futures::{
};
use gpui::{
actions, canvas, impl_actions, point, size, Action, AnyElement, AnyView, AnyWeakView,
AppContext, AsyncAppContext, AsyncWindowContext, Bounds, DragMoveEvent, Entity as _, EntityId,
EventEmitter, FocusHandle, FocusableView, Global, GlobalPixels, KeyContext, Keystroke,
AppContext, AsyncAppContext, AsyncWindowContext, Bounds, DevicePixels, DragMoveEvent,
Entity as _, EntityId, EventEmitter, FocusHandle, FocusableView, Global, KeyContext, Keystroke,
LayoutId, ManagedView, Model, ModelContext, PathPromptOptions, Point, PromptLevel, Render,
Size, Subscription, Task, View, WeakView, WindowHandle, WindowOptions,
};
@ -89,11 +89,11 @@ use crate::persistence::{
};
lazy_static! {
static ref ZED_WINDOW_SIZE: Option<Size<GlobalPixels>> = env::var("ZED_WINDOW_SIZE")
static ref ZED_WINDOW_SIZE: Option<Size<DevicePixels>> = env::var("ZED_WINDOW_SIZE")
.ok()
.as_deref()
.and_then(parse_pixel_size_env_var);
static ref ZED_WINDOW_POSITION: Option<Point<GlobalPixels>> = env::var("ZED_WINDOW_POSITION")
static ref ZED_WINDOW_POSITION: Option<Point<DevicePixels>> = env::var("ZED_WINDOW_POSITION")
.ok()
.as_deref()
.and_then(parse_pixel_position_env_var);
@ -745,11 +745,7 @@ impl Workspace {
cx.observe_window_activation(Self::on_window_activation_changed),
cx.observe_window_bounds(move |_, cx| {
if let Some(display) = cx.display() {
// Transform fixed bounds to be stored in terms of the containing display
let mut window_bounds = cx.window_bounds();
let display_bounds = display.bounds();
window_bounds.origin.x -= display_bounds.origin.x;
window_bounds.origin.y -= display_bounds.origin.y;
let window_bounds = cx.window_bounds();
let fullscreen = cx.is_fullscreen();
if let Some(display_uuid) = display.uuid().log_err() {
@ -902,7 +898,7 @@ impl Workspace {
})?;
window
} else {
let window_bounds_override = window_bounds_env_override(&cx);
let window_bounds_override = window_bounds_env_override();
let (bounds, display, fullscreen) = if let Some(bounds) = window_bounds_override {
(Some(bounds), None, false)
@ -917,24 +913,7 @@ impl Workspace {
Some((display?, bounds?.0, fullscreen.unwrap_or(false)))
});
if let Some((serialized_display, mut bounds, fullscreen)) = restorable_bounds {
// Stored bounds are relative to the containing display.
// So convert back to global coordinates if that screen still exists
let screen_bounds = cx
.update(|cx| {
cx.displays()
.into_iter()
.find(|display| display.uuid().ok() == Some(serialized_display))
})
.ok()
.flatten()
.map(|screen| screen.bounds());
if let Some(screen_bounds) = screen_bounds {
bounds.origin.x += screen_bounds.origin.x;
bounds.origin.y += screen_bounds.origin.y;
}
if let Some((serialized_display, bounds, fullscreen)) = restorable_bounds {
(Some(bounds), Some(serialized_display), fullscreen)
} else {
(None, None, false)
@ -3756,14 +3735,11 @@ impl Workspace {
}
}
fn window_bounds_env_override(cx: &AsyncAppContext) -> Option<Bounds<GlobalPixels>> {
let display_origin = cx
.update(|cx| Some(cx.displays().first()?.bounds().origin))
.ok()??;
fn window_bounds_env_override() -> Option<Bounds<DevicePixels>> {
ZED_WINDOW_POSITION
.zip(*ZED_WINDOW_SIZE)
.map(|(position, size)| Bounds {
origin: display_origin + position,
origin: position,
size,
})
}
@ -4662,7 +4638,7 @@ pub fn join_hosted_project(
)
.await?;
let window_bounds_override = window_bounds_env_override(&cx);
let window_bounds_override = window_bounds_env_override();
cx.update(|cx| {
let mut options = (app_state.build_window_options)(None, cx);
options.bounds = window_bounds_override;
@ -4723,7 +4699,7 @@ pub fn join_in_room_project(
})?
.await?;
let window_bounds_override = window_bounds_env_override(&cx);
let window_bounds_override = window_bounds_env_override();
cx.update(|cx| {
let mut options = (app_state.build_window_options)(None, cx);
options.bounds = window_bounds_override;
@ -4817,18 +4793,18 @@ pub fn restart(_: &Restart, cx: &mut AppContext) {
.detach_and_log_err(cx);
}
fn parse_pixel_position_env_var(value: &str) -> Option<Point<GlobalPixels>> {
fn parse_pixel_position_env_var(value: &str) -> Option<Point<DevicePixels>> {
let mut parts = value.split(',');
let x: usize = parts.next()?.parse().ok()?;
let y: usize = parts.next()?.parse().ok()?;
Some(point((x as f64).into(), (y as f64).into()))
Some(point((x as i32).into(), (y as i32).into()))
}
fn parse_pixel_size_env_var(value: &str) -> Option<Size<GlobalPixels>> {
fn parse_pixel_size_env_var(value: &str) -> Option<Size<DevicePixels>> {
let mut parts = value.split(',');
let width: usize = parts.next()?.parse().ok()?;
let height: usize = parts.next()?.parse().ok()?;
Some(size((width as f64).into(), (height as f64).into()))
Some(size((width as i32).into(), (height as i32).into()))
}
struct DisconnectedOverlay;