Skip to content

Commit

Permalink
Make web internal cursor more efficient
Browse files Browse the repository at this point in the history
  • Loading branch information
eero-lehtinen committed Nov 16, 2023
1 parent 9275c4b commit 62adca0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
25 changes: 14 additions & 11 deletions src/platform_impl/web/custom_cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,19 @@ impl CustomCursorExtWeb for WebCustomCursor {
}
}

pub enum CustomCursorInternal {
DataUrl(String),
Url(String),
pub struct CustomCursorInternal {
style: String,
data_url: Option<String>,
}

impl CustomCursorInternal {
pub fn new(document: &Document, cursor: WebCustomCursor) -> Self {
match cursor {
WebCustomCursor::Image(image) => Self::from_image(document, image),
WebCustomCursor::Url(url) => Self::Url(url),
WebCustomCursor::Url(url) => Self {
style: format!("url({}), auto", url),
data_url: None,
},
}
}

Expand Down Expand Up @@ -79,20 +82,20 @@ impl CustomCursorInternal {

let data_url = cursor_icon_canvas.to_data_url().unwrap();

Self::DataUrl(data_url)
Self {
style: format!("url({}), auto", data_url),
data_url: Some(data_url),
}
}

pub fn to_style(&self) -> String {
match self {
CustomCursorInternal::Url(url) => format!("url({}), auto", url),
CustomCursorInternal::DataUrl(data_url) => format!("url({}), auto", data_url),
}
pub fn style(&self) -> &str {
&self.style
}
}

impl Drop for CustomCursorInternal {
fn drop(&mut self) {
if let CustomCursorInternal::DataUrl(data_url) = self {
if let Some(data_url) = &self.data_url {
Url::revoke_object_url(data_url).unwrap();
};
}
Expand Down
11 changes: 5 additions & 6 deletions src/platform_impl/web/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use super::custom_cursor::CustomCursorInternal;
use super::r#async::Dispatcher;
use super::{backend, monitor::MonitorHandle, EventLoopWindowTarget, Fullscreen};

use std::borrow::Cow;
use std::cell::RefCell;
use std::collections::{HashMap, VecDeque};
use std::rc::Rc;
Expand Down Expand Up @@ -223,7 +222,7 @@ impl Inner {
backend::set_canvas_style_property(
self.canvas.borrow().raw(),
"cursor",
&cursor.to_style(),
cursor.style(),
);
};
}
Expand Down Expand Up @@ -256,16 +255,16 @@ impl Inner {
} else {
let custom_cursors = self.custom_cursors.borrow();
let name = match *self.selected_cursor.borrow() {
SelectedCursor::BuiltIn(cursor) => Cow::Borrowed(cursor.name()),
SelectedCursor::BuiltIn(cursor) => cursor.name(),
SelectedCursor::Custom(key) => {
if let Some(data) = custom_cursors.get(&key) {
Cow::Owned(data.to_style())
data.style()
} else {
Cow::Borrowed(CursorIcon::default().name())
CursorIcon::default().name()
}
}
};
backend::set_canvas_style_property(self.canvas.borrow().raw(), "cursor", &name);
backend::set_canvas_style_property(self.canvas.borrow().raw(), "cursor", name);
}
}

Expand Down

0 comments on commit 62adca0

Please sign in to comment.