Skip to content

Commit

Permalink
Make cursor from_url actually usable and fix hotspot in web
Browse files Browse the repository at this point in the history
  • Loading branch information
eero-lehtinen committed Nov 16, 2023
1 parent 63fec29 commit b0f9f88
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 18 deletions.
8 changes: 4 additions & 4 deletions examples/custom_cursors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ fn main() -> Result<(), impl std::error::Error> {
let mut cursor_key = 0;
let mut cursor_visible = true;

let icon1 = decode_cursor(include_bytes!("data/cross.png"));
let icon2 = decode_cursor(include_bytes!("data/cross2.png"));
window.register_custom_cursor_icon(0, icon1);
window.register_custom_cursor_icon(1, icon2);
let cursor1 = decode_cursor(include_bytes!("data/cross.png"));
let cursor2 = decode_cursor(include_bytes!("data/cross2.png"));
window.register_custom_cursor_icon(0, cursor1);
window.register_custom_cursor_icon(1, cursor2);

event_loop.run(move |event, _elwt| match event {
Event::WindowEvent { event, .. } => match event {
Expand Down
18 changes: 18 additions & 0 deletions src/platform/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@
//! [`border`]: https://developer.mozilla.org/en-US/docs/Web/CSS/border
//! [`padding`]: https://developer.mozilla.org/en-US/docs/Web/CSS/padding
use crate::cursor::CustomCursor;
use crate::event::Event;
use crate::event_loop::EventLoop;
use crate::event_loop::EventLoopWindowTarget;
use crate::platform_impl::PlatformCustomCursor;
use crate::window::{Window, WindowBuilder};
use crate::SendSyncWrapper;

Expand Down Expand Up @@ -200,3 +202,19 @@ pub enum PollStrategy {
#[default]
Scheduler,
}

pub trait CustomCursorExtWeb {
fn from_url(url: String, hotspot_x: u32, hotspot_y: u32) -> Self;
}

impl CustomCursorExtWeb for CustomCursor {
fn from_url(url: String, hotspot_x: u32, hotspot_y: u32) -> Self {
Self {
inner: PlatformCustomCursor::Url {
url,
hotspot_x,
hotspot_y,
},
}
}
}
29 changes: 15 additions & 14 deletions src/platform_impl/web/custom_cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ use web_sys::{CanvasRenderingContext2d, Document, HtmlCanvasElement, ImageData,
#[derive(Debug, Clone)]
pub enum WebCustomCursor {
Image(CursorImage),
Url(String),
Url {
url: String,
hotspot_x: u32,
hotspot_y: u32,
},
}

impl WebCustomCursor {
Expand All @@ -16,16 +20,6 @@ impl WebCustomCursor {
}
}

pub trait CustomCursorExtWeb {
fn from_url(url: String) -> Self;
}

impl CustomCursorExtWeb for WebCustomCursor {
fn from_url(url: String) -> Self {
Self::Url(url)
}
}

pub struct CustomCursorInternal {
style: String,
data_url: Option<String>,
Expand All @@ -35,8 +29,12 @@ impl CustomCursorInternal {
pub fn new(document: &Document, cursor: WebCustomCursor) -> Self {
match cursor {
WebCustomCursor::Image(image) => Self::from_image(document, image),
WebCustomCursor::Url(url) => Self {
style: format!("url({}), auto", url),
WebCustomCursor::Url {
url,
hotspot_x,
hotspot_y,
} => Self {
style: format!("url({}) {} {}, auto", url, hotspot_x, hotspot_y),
data_url: None,
},
}
Expand Down Expand Up @@ -83,7 +81,10 @@ impl CustomCursorInternal {
let data_url = cursor_icon_canvas.to_data_url().unwrap();

Self {
style: format!("url({}), auto", data_url),
style: format!(
"url({}) {} {}, auto",
data_url, image.hotspot_x, image.hotspot_y
),
data_url: Some(data_url),
}
}
Expand Down

0 comments on commit b0f9f88

Please sign in to comment.