Create custom cursor with directly with event loop

Replace the `CustomCursorBuilder` with the `CustomCursorSource` and
perform the loading of the cursor via the
`EventLoop::create_custom_cursor` instead of passing it to the builder
itself.

This follows the `EventLoop::create_window` API.
This commit is contained in:
Kirill Chibisov 2024-02-03 07:27:17 +04:00
parent 3fb93b4f83
commit 7abd427216
26 changed files with 213 additions and 175 deletions

View file

@ -7,7 +7,7 @@ use std::{
use x11rb::connection::Connection;
use crate::{platform_impl::PlatformCustomCursorBuilder, window::CursorIcon};
use crate::{platform_impl::PlatformCustomCursorSource, window::CursorIcon};
use super::super::ActiveEventLoop;
use super::*;
@ -124,32 +124,36 @@ impl PartialEq for CustomCursor {
impl Eq for CustomCursor {}
impl CustomCursor {
pub(crate) fn build(builder: PlatformCustomCursorBuilder, p: &ActiveEventLoop) -> CustomCursor {
pub(crate) fn new(
event_loop: &ActiveEventLoop,
cursor: PlatformCustomCursorSource,
) -> CustomCursor {
unsafe {
let ximage = (p.xconn.xcursor.XcursorImageCreate)(
builder.0.width as i32,
builder.0.height as i32,
let ximage = (event_loop.xconn.xcursor.XcursorImageCreate)(
cursor.0.width as i32,
cursor.0.height as i32,
);
if ximage.is_null() {
panic!("failed to allocate cursor image");
}
(*ximage).xhot = builder.0.hotspot_x as u32;
(*ximage).yhot = builder.0.hotspot_y as u32;
(*ximage).xhot = cursor.0.hotspot_x as u32;
(*ximage).yhot = cursor.0.hotspot_y as u32;
(*ximage).delay = 0;
let dst = slice::from_raw_parts_mut((*ximage).pixels, builder.0.rgba.len() / 4);
for (dst, chunk) in dst.iter_mut().zip(builder.0.rgba.chunks_exact(4)) {
let dst = slice::from_raw_parts_mut((*ximage).pixels, cursor.0.rgba.len() / 4);
for (dst, chunk) in dst.iter_mut().zip(cursor.0.rgba.chunks_exact(4)) {
*dst = (chunk[0] as u32) << 16
| (chunk[1] as u32) << 8
| (chunk[2] as u32)
| (chunk[3] as u32) << 24;
}
let cursor = (p.xconn.xcursor.XcursorImageLoadCursor)(p.xconn.display, ximage);
(p.xconn.xcursor.XcursorImageDestroy)(ximage);
let cursor =
(event_loop.xconn.xcursor.XcursorImageLoadCursor)(event_loop.xconn.display, ximage);
(event_loop.xconn.xcursor.XcursorImageDestroy)(ximage);
Self {
inner: Arc::new(CustomCursorInner {
xconn: p.xconn.clone(),
xconn: event_loop.xconn.clone(),
cursor,
}),
}