Fix issues where screen and window sizes contained Pixels, but were declared as DevicePixels (#12991)

On most platforms, things were working correctly, but had the wrong
type. On X11, there were some problems with window and display size
calculations.

Release Notes:

- Fixed issues with window positioning on X11

---------

Co-authored-by: Conrad Irwin <conrad.irwin@gmail.com>
Co-authored-by: Mikayla <mikayla@zed.dev>
This commit is contained in:
Max Brunsfeld
2024-06-13 10:48:37 -07:00
committed by GitHub
co-authored by Conrad Irwin Mikayla
parent 22dc88ed3d
commit da281d6d8f
25 changed files with 331 additions and 367 deletions
+12 -4
View File
@@ -908,8 +908,11 @@ impl LinuxClient for X11Client {
.iter()
.enumerate()
.filter_map(|(root_id, _)| {
Some(Rc::new(X11Display::new(&state.xcb_connection, root_id)?)
as Rc<dyn PlatformDisplay>)
Some(Rc::new(X11Display::new(
&state.xcb_connection,
state.scale_factor,
root_id,
)?) as Rc<dyn PlatformDisplay>)
})
.collect()
}
@@ -918,8 +921,12 @@ impl LinuxClient for X11Client {
let state = self.0.borrow();
Some(Rc::new(
X11Display::new(&state.xcb_connection, state.x_root_index)
.expect("There should always be a root index"),
X11Display::new(
&state.xcb_connection,
state.scale_factor,
state.x_root_index,
)
.expect("There should always be a root index"),
))
}
@@ -928,6 +935,7 @@ impl LinuxClient for X11Client {
Some(Rc::new(X11Display::new(
&state.xcb_connection,
state.scale_factor,
id.0 as usize,
)?))
}
+11 -7
View File
@@ -2,25 +2,29 @@ use anyhow::Result;
use uuid::Uuid;
use x11rb::{connection::Connection as _, xcb_ffi::XCBConnection};
use crate::{Bounds, DevicePixels, DisplayId, PlatformDisplay, Size};
use crate::{px, Bounds, DisplayId, Pixels, PlatformDisplay, Size};
#[derive(Debug)]
pub(crate) struct X11Display {
x_screen_index: usize,
bounds: Bounds<DevicePixels>,
bounds: Bounds<Pixels>,
uuid: Uuid,
}
impl X11Display {
pub(crate) fn new(xc: &XCBConnection, x_screen_index: usize) -> Option<Self> {
pub(crate) fn new(
xc: &XCBConnection,
scale_factor: f32,
x_screen_index: usize,
) -> Option<Self> {
let screen = xc.setup().roots.get(x_screen_index).unwrap();
Some(Self {
x_screen_index: x_screen_index,
x_screen_index,
bounds: Bounds {
origin: Default::default(),
size: Size {
width: DevicePixels(screen.width_in_pixels as i32),
height: DevicePixels(screen.height_in_pixels as i32),
width: px(screen.width_in_pixels as f32 / scale_factor),
height: px(screen.height_in_pixels as f32 / scale_factor),
},
},
uuid: Uuid::from_bytes([0; 16]),
@@ -37,7 +41,7 @@ impl PlatformDisplay for X11Display {
Ok(self.uuid)
}
fn bounds(&self) -> Bounds<DevicePixels> {
fn bounds(&self) -> Bounds<Pixels> {
self.bounds
}
}
+22 -14
View File
@@ -1,6 +1,6 @@
use crate::{
platform::blade::{BladeRenderer, BladeSurfaceConfig},
size, AnyWindowHandle, Bounds, DevicePixels, ForegroundExecutor, Modifiers, Pixels,
px, size, AnyWindowHandle, Bounds, DevicePixels, ForegroundExecutor, Modifiers, Pixels,
PlatformAtlas, PlatformDisplay, PlatformInput, PlatformInputHandler, PlatformWindow, Point,
PromptLevel, Scene, Size, WindowAppearance, WindowBackgroundAppearance, WindowBounds,
WindowKind, WindowParams, X11ClientStatePtr,
@@ -162,7 +162,7 @@ pub struct X11WindowState {
atoms: XcbAtoms,
x_root_window: xproto::Window,
_raw: RawWindow,
bounds: Bounds<i32>,
bounds: Bounds<Pixels>,
scale_factor: f32,
renderer: BladeRenderer,
display: Rc<dyn PlatformDisplay>,
@@ -273,10 +273,10 @@ impl X11WindowState {
visual.depth,
x_window,
visual_set.root,
params.bounds.origin.x.0 as i16,
params.bounds.origin.y.0 as i16,
params.bounds.size.width.0 as u16,
params.bounds.size.height.0 as u16,
(params.bounds.origin.x.0 * scale_factor) as i16,
(params.bounds.origin.y.0 * scale_factor) as i16,
(params.bounds.size.width.0 * scale_factor) as u16,
(params.bounds.size.height.0 * scale_factor) as u16,
0,
xproto::WindowClass::INPUT_OUTPUT,
visual.id,
@@ -370,10 +370,12 @@ impl X11WindowState {
Ok(Self {
client,
executor,
display: Rc::new(X11Display::new(xcb_connection, x_screen_index).unwrap()),
display: Rc::new(
X11Display::new(xcb_connection, scale_factor, x_screen_index).unwrap(),
),
_raw: raw,
x_root_window: visual_set.root,
bounds: params.bounds.map(|v| v.0),
bounds: params.bounds,
scale_factor,
renderer: BladeRenderer::new(gpu, config),
atoms: *atoms,
@@ -627,6 +629,7 @@ impl X11WindowStatePtr {
let is_resize;
{
let mut state = self.state.borrow_mut();
let bounds = bounds.map(|f| px(f as f32 / state.scale_factor));
is_resize = bounds.size.width != state.bounds.size.width
|| bounds.size.height != state.bounds.size.height;
@@ -641,9 +644,10 @@ impl X11WindowStatePtr {
let gpu_size = query_render_extent(&self.xcb_connection, self.x_window);
if state.renderer.viewport_size() != gpu_size {
state
.renderer
.update_drawable_size(size(gpu_size.width as f64, gpu_size.height as f64));
state.renderer.update_drawable_size(size(
DevicePixels(gpu_size.width as i32),
DevicePixels(gpu_size.height as i32),
));
resize_args = Some((state.content_size(), state.scale_factor));
}
}
@@ -678,8 +682,8 @@ impl X11WindowStatePtr {
}
impl PlatformWindow for X11Window {
fn bounds(&self) -> Bounds<DevicePixels> {
self.0.state.borrow().bounds.map(|v| v.into())
fn bounds(&self) -> Bounds<Pixels> {
self.0.state.borrow().bounds
}
fn is_maximized(&self) -> bool {
@@ -693,7 +697,11 @@ impl PlatformWindow for X11Window {
fn window_bounds(&self) -> WindowBounds {
let state = self.0.state.borrow();
WindowBounds::Windowed(state.bounds.map(|p| DevicePixels(p)))
if self.is_maximized() {
WindowBounds::Maximized(state.bounds)
} else {
WindowBounds::Windowed(state.bounds)
}
}
fn content_size(&self) -> Size<Pixels> {