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

@ -13,7 +13,7 @@ use std::{
ops::{Add, Div, Mul, MulAssign, Sub},
};
use crate::AppContext;
use crate::{AppContext, DisplayId};
/// An axis along which a measurement can be made.
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
@ -363,11 +363,11 @@ pub struct Size<T: Clone + Default + Debug> {
pub height: T,
}
impl From<Size<GlobalPixels>> for Size<Pixels> {
fn from(size: Size<GlobalPixels>) -> Self {
impl From<Size<DevicePixels>> for Size<Pixels> {
fn from(size: Size<DevicePixels>) -> Self {
Size {
width: Pixels(size.width.0),
height: Pixels(size.height.0),
width: Pixels(size.width.0 as f32),
height: Pixels(size.height.0 as f32),
}
}
}
@ -604,11 +604,11 @@ impl<T: Clone + Default + Debug> From<Point<T>> for Size<T> {
}
}
impl From<Size<Pixels>> for Size<GlobalPixels> {
impl From<Size<Pixels>> for Size<DevicePixels> {
fn from(size: Size<Pixels>) -> Self {
Size {
width: GlobalPixels(size.width.0),
height: GlobalPixels(size.height.0),
width: DevicePixels(size.width.0 as i32),
height: DevicePixels(size.height.0 as i32),
}
}
}
@ -693,31 +693,43 @@ pub struct Bounds<T: Clone + Default + Debug> {
pub size: Size<T>,
}
impl Bounds<GlobalPixels> {
/// Generate a centered bounds for the primary display
pub fn centered(size: impl Into<Size<GlobalPixels>>, cx: &mut AppContext) -> Self {
impl Bounds<DevicePixels> {
/// Generate a centered bounds for the given display or primary display if none is provided
pub fn centered(
display_id: Option<DisplayId>,
size: impl Into<Size<DevicePixels>>,
cx: &mut AppContext,
) -> Self {
let display = display_id
.and_then(|id| cx.find_display(id))
.or_else(|| cx.primary_display());
let size = size.into();
cx.primary_display()
display
.map(|display| {
let center = display.bounds().center();
Bounds {
origin: point(center.x - size.width / 2.0, center.y - size.height / 2.0),
origin: point(center.x - size.width / 2, center.y - size.height / 2),
size,
}
})
.unwrap_or_else(|| Bounds {
origin: point(GlobalPixels(0.0), GlobalPixels(0.0)),
origin: point(DevicePixels(0), DevicePixels(0)),
size,
})
}
/// Generate maximized bounds for the primary display
pub fn maximized(cx: &mut AppContext) -> Self {
cx.primary_display()
/// Generate maximized bounds for the given display or primary display if none is provided
pub fn maximized(display_id: Option<DisplayId>, cx: &mut AppContext) -> Self {
let display = display_id
.and_then(|id| cx.find_display(id))
.or_else(|| cx.primary_display());
display
.map(|display| display.bounds())
.unwrap_or_else(|| Bounds {
origin: point(GlobalPixels(0.0), GlobalPixels(0.0)),
size: size(GlobalPixels(1024.0), GlobalPixels(768.0)),
origin: point(DevicePixels(0), DevicePixels(0)),
size: size(DevicePixels(1024), DevicePixels(768)),
})
}
}
@ -2455,34 +2467,6 @@ impl From<ScaledPixels> for f64 {
}
}
/// Represents pixels in a global coordinate space, which can span across multiple displays.
///
/// `GlobalPixels` is used when dealing with a coordinate system that is not limited to a single
/// display's boundaries. This type is particularly useful in multi-monitor setups where
/// positioning and measurements need to be consistent and relative to a "global" origin point
/// rather than being relative to any individual display.
#[derive(Clone, Copy, Default, Add, AddAssign, Sub, SubAssign, Div, PartialEq, PartialOrd)]
#[repr(transparent)]
pub struct GlobalPixels(pub(crate) f32);
impl Debug for GlobalPixels {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} px (global coordinate space)", self.0)
}
}
impl From<GlobalPixels> for f64 {
fn from(global_pixels: GlobalPixels) -> Self {
global_pixels.0 as f64
}
}
impl From<f64> for GlobalPixels {
fn from(global_pixels: f64) -> Self {
GlobalPixels(global_pixels as f32)
}
}
/// Represents a length in rems, a unit based on the font-size of the window, which can be assigned with [`WindowContext::set_rem_size`][set_rem_size].
///
/// Rems are used for defining lengths that are scalable and consistent across different UI elements.
@ -2834,12 +2818,6 @@ impl Half for Rems {
}
}
impl Half for GlobalPixels {
fn half(&self) -> Self {
Self(self.0 / 2.)
}
}
/// Provides a trait for types that can negate their values.
pub trait Negate {
/// Returns the negation of the given value
@ -2882,12 +2860,6 @@ impl Negate for Rems {
}
}
impl Negate for GlobalPixels {
fn negate(self) -> Self {
Self(-self.0)
}
}
/// A trait for checking if a value is zero.
///
/// This trait provides a method to determine if a value is considered to be zero.