Skip to content

Commit

Permalink
winit/wayland: Apply scale when creating drag surface
Browse files Browse the repository at this point in the history
  • Loading branch information
ids1024 committed Jan 7, 2025
1 parent 3a598ad commit cbd34fb
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
4 changes: 2 additions & 2 deletions winit/src/platform_specific/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,10 @@ impl PlatformSpecific {
None
}

pub(crate) fn update_surface_shm(&mut self, surface: &dyn HasWindowHandle, width: u32, height: u32, data: &[u8], offset: Vector) {
pub(crate) fn update_surface_shm(&mut self, surface: &dyn HasWindowHandle, width: u32, height: u32, scale: f64, data: &[u8], offset: Vector) {
#[cfg(all(feature = "wayland", target_os = "linux"))]
{
return self.wayland.update_surface_shm(surface, width, height, data, offset);
return self.wayland.update_surface_shm(surface, width, height, scale, data, offset);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions winit/src/platform_specific/wayland/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,12 @@ impl WaylandSpecific {
}
}

pub(crate) fn update_surface_shm(&mut self, window: &dyn HasWindowHandle, width: u32, height: u32, data: &[u8], offset: Vector) {
pub(crate) fn update_surface_shm(&mut self, window: &dyn HasWindowHandle, width: u32, height: u32, scale: f64, data: &[u8], offset: Vector) {
if let Some(subsurface_state) = self.subsurface_state.as_mut() {
if let RawWindowHandle::Wayland(window) = window.window_handle().unwrap().as_raw() {
let id = unsafe { ObjectId::from_ptr(WlSurface::interface(), window.surface.as_ptr().cast()).unwrap() };
let surface = WlSurface::from_id(self.conn.as_ref().unwrap(), id).unwrap();
subsurface_state.update_surface_shm(&surface, width, height, data, offset);
subsurface_state.update_surface_shm(&surface, width, height, scale, data, offset);
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion winit/src/platform_specific/wayland/subsurface_widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,15 +377,22 @@ impl SubsurfaceState {
.create_surface(&self.qh, SurfaceData::new(None, 1))
}

pub fn update_surface_shm(&self, surface: &WlSurface, width: u32, height: u32, data: &[u8], offset: Vector) {
pub fn update_surface_shm(&self, surface: &WlSurface, width: u32, height: u32, scale: f64, data: &[u8], offset: Vector) {
let wp_viewport = self.wp_viewporter.get_viewport(
&surface,
&self.qh,
cctk::sctk::globals::GlobalData,
);
let shm = ShmGlobal(&self.wl_shm);
let mut pool = SlotPool::new(width as usize * height as usize * 4, &shm).unwrap();
let (buffer, canvas) = pool.create_buffer(width as i32, height as i32, width as i32 * 4, wl_shm::Format::Argb8888).unwrap();
canvas[0..width as usize * height as usize * 4].copy_from_slice(data);
surface.damage_buffer(0, 0, width as i32, height as i32);
buffer.attach_to(&surface);
surface.offset(offset.x as i32, offset.y as i32);
wp_viewport.set_destination((width as f64 / scale) as i32, (height as f64 / scale) as i32);
surface.commit();
wp_viewport.destroy();
}

fn create_subsurface(&self, parent: &WlSurface) -> SubsurfaceInstance {
Expand Down
6 changes: 3 additions & 3 deletions winit/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,7 @@ async fn run_instance<'a, P, C>(
.update_subsurfaces(id, &surface);
let surface = Arc::new(surface);
dnd_surface = Some(surface.clone());
dnd_buffer = Some((viewport.physical_size(), bytes, icon_surface.offset));
dnd_buffer = Some((viewport.physical_size(), state.scale_factor(), bytes, icon_surface.offset));
Icon::Surface(dnd::DndSurface(surface))
} else {
platform_specific_handler
Expand All @@ -982,8 +982,8 @@ async fn run_instance<'a, P, C>(
);

// This needs to be after `wl_data_device::start_drag` for the offset to have an effect
if let (Some(surface), Some((size, bytes, offset))) = (dnd_surface.as_ref(), dnd_buffer) {
platform_specific_handler.update_surface_shm(&surface, size.width, size.height, &bytes, offset);
if let (Some(surface), Some((size, scale, bytes, offset))) = (dnd_surface.as_ref(), dnd_buffer) {
platform_specific_handler.update_surface_shm(&surface, size.width, size.height, scale, &bytes, offset);
}
}
}
Expand Down

0 comments on commit cbd34fb

Please sign in to comment.