Add an offset to IconSurface, so icon can be offset from custor

This commit is contained in:
Ian Douglas Scott 2025-01-03 15:00:11 -08:00 committed by Ashley Wulber
parent ed5de12e69
commit b4168e69cc
No known key found for this signature in database
GPG key ID: 5216D4F46A90A820
3 changed files with 32 additions and 14 deletions

View file

@ -6,7 +6,10 @@ use std::collections::HashMap;
#[cfg(all(feature = "wayland", target_os = "linux"))]
use cctk::sctk::reexports::client::Connection;
use iced_graphics::{Compositor, compositor};
use iced_runtime::{core::window, platform_specific, user_interface};
use iced_runtime::{
core::{Vector, window},
platform_specific, user_interface,
};
use raw_window_handle::HasWindowHandle;
#[cfg(all(feature = "wayland", target_os = "linux"))]
@ -149,12 +152,13 @@ impl PlatformSpecific {
width: u32,
height: u32,
data: &[u8],
offset: Vector,
) {
#[cfg(all(feature = "wayland", target_os = "linux"))]
{
return self
.wayland
.update_surface_shm(surface, width, height, data);
.update_surface_shm(surface, width, height, data, offset);
}
}
}

View file

@ -17,7 +17,7 @@ use cctk::sctk::seat::keyboard::Modifiers;
use cursor_icon::CursorIcon;
use iced_futures::futures::channel::mpsc;
use iced_graphics::{Compositor, compositor};
use iced_runtime::core::window;
use iced_runtime::core::{Vector, window};
use raw_window_handle::{DisplayHandle, HasDisplayHandle, HasWindowHandle};
use raw_window_handle::{HasRawDisplayHandle, RawWindowHandle};
use sctk_event::SctkEvent;
@ -245,6 +245,7 @@ impl WaylandSpecific {
width: u32,
height: u32,
data: &[u8],
offset: Vector,
) {
if let Some(subsurface_state) = self.subsurface_state.as_mut() {
if let RawWindowHandle::Wayland(window) =
@ -261,7 +262,7 @@ impl WaylandSpecific {
WlSurface::from_id(self.conn.as_ref().unwrap(), id)
.unwrap();
subsurface_state
.update_surface_shm(&surface, width, height, data);
.update_surface_shm(&surface, width, height, data, offset);
}
}
}

View file

@ -1,7 +1,7 @@
// TODO z-order option?
use crate::core::{
ContentFit, Element, Length, Rectangle, Size,
ContentFit, Element, Length, Rectangle, Size, Vector,
layout::{self, Layout},
mouse, renderer,
widget::{self, Widget},
@ -25,7 +25,6 @@ use cctk::sctk::{
compositor::SurfaceData,
error::GlobalError,
globals::{GlobalData, ProvidesBoundGlobal},
shm::slot::SlotPool,
reexports::client::{
Connection, Dispatch, Proxy, QueueHandle, delegate_noop,
protocol::{
@ -39,6 +38,7 @@ use cctk::sctk::{
wl_surface::WlSurface,
},
},
shm::slot::SlotPool,
};
use iced_futures::core::window;
use wayland_backend::client::ObjectId;
@ -252,7 +252,6 @@ impl PartialEq for SubsurfaceBuffer {
}
}
impl Dispatch<WlShmPool, GlobalData> for SctkState {
fn event(
_: &mut SctkState,
@ -300,8 +299,7 @@ impl Dispatch<WlBuffer, GlobalData> for SctkState {
_: &QueueHandle<SctkState>,
) {
match event {
wl_buffer::Event::Release => {
}
wl_buffer::Event::Release => {}
_ => unreachable!(),
}
}
@ -371,18 +369,33 @@ pub struct SubsurfaceState {
impl SubsurfaceState {
pub fn create_surface(&self) -> WlSurface {
self
.wl_compositor
self.wl_compositor
.create_surface(&self.qh, SurfaceData::new(None, 1))
}
pub fn update_surface_shm(&self, surface: &WlSurface, width: u32, height: u32, data: &[u8]) {
pub fn update_surface_shm(
&self,
surface: &WlSurface,
width: u32,
height: u32,
data: &[u8],
offset: Vector,
) {
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();
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);
surface.commit();
}