Skip to content

Commit

Permalink
Add an offset to IconSurface, so icon can be offset from custor
Browse files Browse the repository at this point in the history
  • Loading branch information
ids1024 committed Jan 3, 2025
1 parent b58c55d commit 892b3f4
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 12 deletions.
9 changes: 6 additions & 3 deletions core/src/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,27 @@ use std::any::Any;
use dnd::{DndAction, DndDestinationRectangle, DndSurface};
use mime::{self, AllowedMimeTypes, AsMimeTypes, ClipboardStoreData};

use crate::{widget::tree::State, window, Element};
use crate::{widget::tree::State, window, Element, Vector};

#[derive(Debug)]
pub struct IconSurface<E> {
pub element: E,
pub state: State,
pub offset: Vector,
}

pub type DynIconSurface = IconSurface<Box<dyn Any>>;

impl<T: 'static, R: 'static> IconSurface<Element<'static, (), T, R>> {
pub fn new(element: Element<'static, (), T, R>, state: State) -> Self {
Self { element, state }
pub fn new(element: Element<'static, (), T, R>, state: State, offset: Vector) -> Self {
Self { element, state, offset }
}

fn upcast(self) -> DynIconSurface {
IconSurface {
element: Box::new(self.element),
state: self.state,
offset: self.offset,
}
}
}
Expand All @@ -36,6 +38,7 @@ impl DynIconSurface {
IconSurface {
element: *self.element.downcast().expect("drag-and-drop icon surface has invalid element type"),
state: self.state,
offset: self.offset,
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions winit/src/platform_specific/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use std::collections::HashMap;

use iced_graphics::Compositor;
use iced_runtime::{core::window, user_interface, Debug};
use iced_runtime::{core::{window, Vector}, user_interface, Debug};
use raw_window_handle::HasWindowHandle;

Check failure on line 8 in winit/src/platform_specific/mod.rs

View workflow job for this annotation

GitHub Actions / web

unresolved import `raw_window_handle`

Check failure on line 8 in winit/src/platform_specific/mod.rs

View workflow job for this annotation

GitHub Actions / wasm

unresolved import `raw_window_handle`

Check failure on line 8 in winit/src/platform_specific/mod.rs

View workflow job for this annotation

GitHub Actions / web

unresolved import `raw_window_handle`

Check failure on line 8 in winit/src/platform_specific/mod.rs

View workflow job for this annotation

GitHub Actions / wasm

unresolved import `raw_window_handle`

#[cfg(all(feature = "wayland", target_os = "linux"))]
Expand Down 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]) {
pub(crate) fn update_surface_shm(&mut self, surface: &dyn HasWindowHandle, 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);
return self.wayland.update_surface_shm(surface, width, height, data, offset);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions winit/src/platform_specific/wayland/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use cctk::sctk::reexports::client::protocol::wl_surface::WlSurface;
use cctk::sctk::seat::keyboard::Modifiers;
use iced_futures::futures::channel::mpsc;
use iced_graphics::Compositor;
use iced_runtime::core::window;
use iced_runtime::core::{window, Vector};
use iced_runtime::Debug;
use raw_window_handle::{DisplayHandle, HasDisplayHandle, HasWindowHandle};
use raw_window_handle::{HasRawDisplayHandle, RawWindowHandle};
Expand Down 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]) {
pub(crate) fn update_surface_shm(&mut self, window: &dyn HasWindowHandle, width: u32, height: u32, 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);
subsurface_state.update_surface_shm(&surface, width, height, data, offset);
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions winit/src/platform_specific/wayland/subsurface_widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::core::{
layout::{self, Layout},
mouse, renderer,
widget::{self, Widget},
ContentFit, Element, Length, Rectangle, Size,
ContentFit, Element, Length, Rectangle, Size, Vector,
};
use std::{
cell::RefCell,
Expand Down Expand Up @@ -377,13 +377,14 @@ impl SubsurfaceState {
.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();
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();
}

Expand Down
2 changes: 1 addition & 1 deletion winit/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,7 @@ async fn run_instance<'a, P, C>(
let id = window::Id::unique();
platform_specific_handler
.update_subsurfaces(id, &surface);
platform_specific_handler.update_surface_shm(&surface, viewport.physical_width(), viewport.physical_height(), &bytes);
platform_specific_handler.update_surface_shm(&surface, viewport.physical_width(), viewport.physical_height(), &bytes, icon_surface.offset);
let surface = Arc::new(surface);
dnd_surface = Some(surface.clone());
Icon::Surface(dnd::DndSurface(surface))
Expand Down

0 comments on commit 892b3f4

Please sign in to comment.