From 62adca094bb9a7edea2ee13a5e31f4f946453ba0 Mon Sep 17 00:00:00 2001 From: Eero Lehtinen Date: Thu, 16 Nov 2023 03:01:40 +0200 Subject: [PATCH] Make web internal cursor more efficient --- src/platform_impl/web/custom_cursor.rs | 25 ++++++++++++++----------- src/platform_impl/web/window.rs | 11 +++++------ 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/platform_impl/web/custom_cursor.rs b/src/platform_impl/web/custom_cursor.rs index d2a5a5dcb6..63aa1a0ea5 100644 --- a/src/platform_impl/web/custom_cursor.rs +++ b/src/platform_impl/web/custom_cursor.rs @@ -26,16 +26,19 @@ impl CustomCursorExtWeb for WebCustomCursor { } } -pub enum CustomCursorInternal { - DataUrl(String), - Url(String), +pub struct CustomCursorInternal { + style: String, + data_url: Option, } 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, + }, } } @@ -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(); }; } diff --git a/src/platform_impl/web/window.rs b/src/platform_impl/web/window.rs index 49fd1da8ce..16524d3b17 100644 --- a/src/platform_impl/web/window.rs +++ b/src/platform_impl/web/window.rs @@ -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; @@ -223,7 +222,7 @@ impl Inner { backend::set_canvas_style_property( self.canvas.borrow().raw(), "cursor", - &cursor.to_style(), + cursor.style(), ); }; } @@ -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); } }