On Web, fix setting cursor icon overriding cursor visibility (#3269)

This commit is contained in:
daxpedda 2023-12-17 18:49:45 +01:00 committed by GitHub
parent f2c5127f27
commit cc33212479
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 8 deletions

View file

@ -26,6 +26,7 @@ Unreleased` header.
- On X11, fix `Xft.dpi` detection from Xresources. - On X11, fix `Xft.dpi` detection from Xresources.
- On Windows, fix consecutive calls to `window.set_fullscreen(Some(Fullscreen::Borderless(None)))` resulting in losing previous window state when eventually exiting fullscreen using `window.set_fullscreen(None)`. - On Windows, fix consecutive calls to `window.set_fullscreen(Some(Fullscreen::Borderless(None)))` resulting in losing previous window state when eventually exiting fullscreen using `window.set_fullscreen(None)`.
- On Web, remove queuing fullscreen request in absence of transient activation. - On Web, remove queuing fullscreen request in absence of transient activation.
- On Web, fix setting cursor icon overriding cursor visibility.
# 0.29.4 # 0.29.4

View file

@ -1,5 +1,5 @@
use std::{ use std::{
cell::RefCell, cell::{Cell, RefCell},
ops::Deref, ops::Deref,
rc::{Rc, Weak}, rc::{Rc, Weak},
}; };
@ -44,6 +44,7 @@ impl WebCustomCursor {
document: &Document, document: &Document,
style: &Style, style: &Style,
previous: SelectedCursor, previous: SelectedCursor,
cursor_visible: Rc<Cell<bool>>,
) -> SelectedCursor { ) -> SelectedCursor {
let previous = previous.into(); let previous = previous.into();
@ -54,6 +55,7 @@ impl WebCustomCursor {
style.clone(), style.clone(),
image, image,
previous, previous,
cursor_visible,
)), )),
WebCustomCursor::Url { WebCustomCursor::Url {
url, url,
@ -61,7 +63,11 @@ impl WebCustomCursor {
hotspot_y, hotspot_y,
} => { } => {
let value = previous.style_with_url(url, *hotspot_x, *hotspot_y); let value = previous.style_with_url(url, *hotspot_x, *hotspot_y);
style.set("cursor", &value);
if cursor_visible.get() {
style.set("cursor", &value);
}
SelectedCursor::Url { SelectedCursor::Url {
style: value, style: value,
previous, previous,
@ -200,6 +206,7 @@ impl From<SelectedCursor> for Previous {
pub enum CursorImageState { pub enum CursorImageState {
Loading { Loading {
style: Style, style: Style,
cursor_visible: Rc<Cell<bool>>,
previous: Previous, previous: Previous,
hotspot_x: u16, hotspot_x: u16,
hotspot_y: u16, hotspot_y: u16,
@ -219,6 +226,7 @@ impl CursorImageState {
style: Style, style: Style,
image: &CursorImage, image: &CursorImage,
previous: Previous, previous: Previous,
cursor_visible: Rc<Cell<bool>>,
) -> Rc<RefCell<Option<Self>>> { ) -> Rc<RefCell<Option<Self>>> {
// Can't create array directly when backed by SharedArrayBuffer. // Can't create array directly when backed by SharedArrayBuffer.
// Adapted from https://github.com/rust-windowing/softbuffer/blob/ab7688e2ed2e2eca51b3c4e1863a5bd7fe85800e/src/web.rs#L196-L223 // Adapted from https://github.com/rust-windowing/softbuffer/blob/ab7688e2ed2e2eca51b3c4e1863a5bd7fe85800e/src/web.rs#L196-L223
@ -261,6 +269,7 @@ impl CursorImageState {
let state = Rc::new(RefCell::new(Some(Self::Loading { let state = Rc::new(RefCell::new(Some(Self::Loading {
style, style,
cursor_visible,
previous, previous,
hotspot_x: image.hotspot_x, hotspot_x: image.hotspot_x,
hotspot_y: image.hotspot_y, hotspot_y: image.hotspot_y,
@ -305,7 +314,7 @@ impl CursorImageState {
}; };
let mut state = state.borrow_mut(); let mut state = state.borrow_mut();
// Extract old state. // Extract old state.
let CursorImageState::Loading { style, previous, hotspot_x, hotspot_y, .. } = state.take().unwrap() else { let CursorImageState::Loading { style, cursor_visible, previous, hotspot_x, hotspot_y, .. } = state.take().unwrap() else {
unreachable!("found invalid state") unreachable!("found invalid state")
}; };
@ -316,7 +325,11 @@ impl CursorImageState {
let data_url = Url::create_object_url_with_blob(&blob).unwrap(); let data_url = Url::create_object_url_with_blob(&blob).unwrap();
let value = previous.style_with_url(&data_url, hotspot_x, hotspot_y); let value = previous.style_with_url(&data_url, hotspot_x, hotspot_y);
style.set("cursor", &value);
if cursor_visible.get() {
style.set("cursor", &value);
}
*state = Some( *state = Some(
CursorImageState::Ready { CursorImageState::Ready {
style: value, style: value,

View file

@ -13,7 +13,7 @@ use super::r#async::Dispatcher;
use super::{backend, monitor::MonitorHandle, EventLoopWindowTarget, Fullscreen}; use super::{backend, monitor::MonitorHandle, EventLoopWindowTarget, Fullscreen};
use web_sys::HtmlCanvasElement; use web_sys::HtmlCanvasElement;
use std::cell::RefCell; use std::cell::{Cell, RefCell};
use std::collections::VecDeque; use std::collections::VecDeque;
use std::rc::Rc; use std::rc::Rc;
@ -26,6 +26,7 @@ pub struct Inner {
pub window: web_sys::Window, pub window: web_sys::Window,
canvas: Rc<RefCell<backend::Canvas>>, canvas: Rc<RefCell<backend::Canvas>>,
selected_cursor: RefCell<SelectedCursor>, selected_cursor: RefCell<SelectedCursor>,
cursor_visible: Rc<Cell<bool>>,
destroy_fn: Option<Box<dyn FnOnce()>>, destroy_fn: Option<Box<dyn FnOnce()>>,
} }
@ -55,6 +56,7 @@ impl Window {
window: window.clone(), window: window.clone(),
canvas, canvas,
selected_cursor: Default::default(), selected_cursor: Default::default(),
cursor_visible: Rc::new(Cell::new(true)),
destroy_fn: Some(destroy_fn), destroy_fn: Some(destroy_fn),
}; };
@ -197,7 +199,10 @@ impl Inner {
#[inline] #[inline]
pub fn set_cursor_icon(&self, cursor: CursorIcon) { pub fn set_cursor_icon(&self, cursor: CursorIcon) {
*self.selected_cursor.borrow_mut() = SelectedCursor::Named(cursor); *self.selected_cursor.borrow_mut() = SelectedCursor::Named(cursor);
self.canvas.borrow().style().set("cursor", cursor.name());
if self.cursor_visible.get() {
self.canvas.borrow().style().set("cursor", cursor.name());
}
} }
#[inline] #[inline]
@ -208,6 +213,7 @@ impl Inner {
canvas.document(), canvas.document(),
canvas.style(), canvas.style(),
self.selected_cursor.take(), self.selected_cursor.take(),
self.cursor_visible.clone(),
); );
*self.selected_cursor.borrow_mut() = new_cursor; *self.selected_cursor.borrow_mut() = new_cursor;
} }
@ -235,12 +241,14 @@ impl Inner {
#[inline] #[inline]
pub fn set_cursor_visible(&self, visible: bool) { pub fn set_cursor_visible(&self, visible: bool) {
if !visible { if !visible && self.cursor_visible.get() {
self.canvas.borrow().style().set("cursor", "none"); self.canvas.borrow().style().set("cursor", "none");
} else { self.cursor_visible.set(false);
} else if visible && !self.cursor_visible.get() {
self.selected_cursor self.selected_cursor
.borrow() .borrow()
.set_style(self.canvas.borrow().style()); .set_style(self.canvas.borrow().style());
self.cursor_visible.set(true);
} }
} }