Skip to content

Commit d0b5a30

Browse files
committed
Web: Window::canvas() now returns a reference
1 parent 61e4cca commit d0b5a30

File tree

6 files changed

+159
-133
lines changed

6 files changed

+159
-133
lines changed

src/changelog/unreleased.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ changelog entry.
7676
- Change signature of `EventLoop::run_app`, `EventLoopExtPumpEvents::pump_app_events` and
7777
`EventLoopExtRunOnDemand::run_app_on_demand` to accept a `impl ApplicationHandler` directly,
7878
instead of requiring a `&mut` reference to it.
79+
- On Web, `Window::canvas()` now returns a reference.
7980

8081
### Removed
8182

src/platform/web.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
//! [`WindowEvent::Touch`]: crate::event::WindowEvent::Touch
4343
//! [`Window::set_outer_position()`]: crate::window::Window::set_outer_position
4444
45+
use std::cell::Ref;
4546
use std::error::Error;
4647
use std::fmt::{self, Display, Formatter};
4748
use std::future::Future;
@@ -74,7 +75,7 @@ pub struct HtmlCanvasElement;
7475
pub trait WindowExtWeb {
7576
/// Only returns the canvas if called from inside the window context (the
7677
/// main thread).
77-
fn canvas(&self) -> Option<HtmlCanvasElement>;
78+
fn canvas(&self) -> Option<Ref<'_, HtmlCanvasElement>>;
7879

7980
/// Returns [`true`] if calling `event.preventDefault()` is enabled.
8081
///
@@ -94,7 +95,7 @@ pub trait WindowExtWeb {
9495

9596
impl WindowExtWeb for Window {
9697
#[inline]
97-
fn canvas(&self) -> Option<HtmlCanvasElement> {
98+
fn canvas(&self) -> Option<Ref<'_, HtmlCanvasElement>> {
9899
self.window.canvas()
99100
}
100101

src/platform_impl/web/event_loop/runner.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ struct Execution {
5555
navigator: Navigator,
5656
document: Document,
5757
#[allow(clippy::type_complexity)]
58-
all_canvases: RefCell<Vec<(WindowId, Weak<RefCell<backend::Canvas>>, DispatchRunner<Inner>)>>,
58+
all_canvases: RefCell<Vec<(WindowId, Weak<backend::Canvas>, DispatchRunner<Inner>)>>,
5959
redraw_pending: RefCell<HashSet<WindowId>>,
6060
destroy_pending: RefCell<VecDeque<WindowId>>,
6161
pub(crate) monitor: Rc<MonitorHandler>,
@@ -121,7 +121,7 @@ impl Runner {
121121
EventWrapper::Event(event) => (self.event_handler)(event),
122122
EventWrapper::ScaleChange { canvas, size, scale } => {
123123
if let Some(canvas) = canvas.upgrade() {
124-
canvas.borrow().handle_scale_change(
124+
canvas.handle_scale_change(
125125
runner,
126126
|event| (self.event_handler)(event),
127127
size,
@@ -209,7 +209,7 @@ impl Shared {
209209
pub fn add_canvas(
210210
&self,
211211
id: WindowId,
212-
canvas: Weak<RefCell<backend::Canvas>>,
212+
canvas: Weak<backend::Canvas>,
213213
runner: DispatchRunner<Inner>,
214214
) {
215215
self.0.all_canvases.borrow_mut().push((id, canvas, runner));
@@ -456,7 +456,7 @@ impl Shared {
456456
// - not visible and we don't know if it intersects yet
457457
// - visible and intersects
458458
if let (false, Some(true) | None) | (true, Some(true)) =
459-
(is_visible, canvas.borrow().is_intersecting)
459+
(is_visible, canvas.is_intersecting.get())
460460
{
461461
runner.send_event(Event::WindowEvent {
462462
window_id: *id,
@@ -772,7 +772,6 @@ impl Shared {
772772
// In case any remaining `Window`s are still not dropped, we will need
773773
// to explicitly remove the event handlers associated with their canvases.
774774
if let Some(canvas) = canvas.upgrade() {
775-
let mut canvas = canvas.borrow_mut();
776775
canvas.remove_listeners();
777776
}
778777
}
@@ -816,7 +815,7 @@ impl Shared {
816815
DeviceEvents::WhenFocused => {
817816
self.0.all_canvases.borrow().iter().any(|(_, canvas, _)| {
818817
if let Some(canvas) = canvas.upgrade() {
819-
canvas.borrow().has_focus.get()
818+
canvas.has_focus.get()
820819
} else {
821820
false
822821
}
@@ -886,7 +885,7 @@ impl WeakShared {
886885

887886
pub(crate) enum EventWrapper {
888887
Event(Event),
889-
ScaleChange { canvas: Weak<RefCell<backend::Canvas>>, size: PhysicalSize<u32>, scale: f64 },
888+
ScaleChange { canvas: Weak<backend::Canvas>, size: PhysicalSize<u32>, scale: f64 },
890889
}
891890

892891
impl From<Event> for EventWrapper {

src/platform_impl/web/event_loop/window_target.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::cell::{Cell, RefCell};
1+
use std::cell::Cell;
22
use std::clone::Clone;
33
use std::iter;
44
use std::rc::Rc;
@@ -79,9 +79,8 @@ impl ActiveEventLoop {
7979
CustomCursorFuture(CustomCursor::new_async(self, source.inner))
8080
}
8181

82-
pub fn register(&self, canvas: &Rc<RefCell<backend::Canvas>>, id: WindowId) {
82+
pub fn register(&self, canvas: &Rc<backend::Canvas>, id: WindowId) {
8383
let canvas_clone = canvas.clone();
84-
let mut canvas = canvas.borrow_mut();
8584
#[cfg(any(feature = "rwh_04", feature = "rwh_05"))]
8685
canvas.set_attribute("data-raw-handle", &id.0.to_string());
8786

@@ -604,7 +603,6 @@ impl ActiveEventLoop {
604603
let canvas = canvas_clone.clone();
605604

606605
move |new_size| {
607-
let canvas = canvas.borrow();
608606
canvas.set_current_size(new_size);
609607
if canvas.old_size() != new_size {
610608
canvas.set_old_size(new_size);
@@ -622,15 +620,15 @@ impl ActiveEventLoop {
622620
canvas.on_intersection(move |is_intersecting| {
623621
// only fire if visible while skipping the first event if it's intersecting
624622
if backend::is_visible(runner.document())
625-
&& !(is_intersecting && canvas_clone.borrow().is_intersecting.is_none())
623+
&& !(is_intersecting && canvas_clone.is_intersecting.get().is_none())
626624
{
627625
runner.send_event(Event::WindowEvent {
628626
window_id: RootWindowId(id),
629627
event: WindowEvent::Occluded(!is_intersecting),
630628
});
631629
}
632630

633-
canvas_clone.borrow_mut().is_intersecting = Some(is_intersecting);
631+
canvas_clone.is_intersecting.set(Some(is_intersecting));
634632
});
635633

636634
let runner = self.runner.clone();

0 commit comments

Comments
 (0)