Skip to content

Commit

Permalink
Web: Window::canvas() now returns a reference
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Jul 19, 2024
1 parent 61e4cca commit d0b5a30
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 133 deletions.
1 change: 1 addition & 0 deletions src/changelog/unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ changelog entry.
- Change signature of `EventLoop::run_app`, `EventLoopExtPumpEvents::pump_app_events` and
`EventLoopExtRunOnDemand::run_app_on_demand` to accept a `impl ApplicationHandler` directly,
instead of requiring a `&mut` reference to it.
- On Web, `Window::canvas()` now returns a reference.

### Removed

Expand Down
5 changes: 3 additions & 2 deletions src/platform/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
//! [`WindowEvent::Touch`]: crate::event::WindowEvent::Touch
//! [`Window::set_outer_position()`]: crate::window::Window::set_outer_position
use std::cell::Ref;
use std::error::Error;
use std::fmt::{self, Display, Formatter};
use std::future::Future;
Expand Down Expand Up @@ -74,7 +75,7 @@ pub struct HtmlCanvasElement;
pub trait WindowExtWeb {
/// Only returns the canvas if called from inside the window context (the
/// main thread).
fn canvas(&self) -> Option<HtmlCanvasElement>;
fn canvas(&self) -> Option<Ref<'_, HtmlCanvasElement>>;

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

impl WindowExtWeb for Window {
#[inline]
fn canvas(&self) -> Option<HtmlCanvasElement> {
fn canvas(&self) -> Option<Ref<'_, HtmlCanvasElement>> {
self.window.canvas()
}

Expand Down
13 changes: 6 additions & 7 deletions src/platform_impl/web/event_loop/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ struct Execution {
navigator: Navigator,
document: Document,
#[allow(clippy::type_complexity)]
all_canvases: RefCell<Vec<(WindowId, Weak<RefCell<backend::Canvas>>, DispatchRunner<Inner>)>>,
all_canvases: RefCell<Vec<(WindowId, Weak<backend::Canvas>, DispatchRunner<Inner>)>>,
redraw_pending: RefCell<HashSet<WindowId>>,
destroy_pending: RefCell<VecDeque<WindowId>>,
pub(crate) monitor: Rc<MonitorHandler>,
Expand Down Expand Up @@ -121,7 +121,7 @@ impl Runner {
EventWrapper::Event(event) => (self.event_handler)(event),
EventWrapper::ScaleChange { canvas, size, scale } => {
if let Some(canvas) = canvas.upgrade() {
canvas.borrow().handle_scale_change(
canvas.handle_scale_change(
runner,
|event| (self.event_handler)(event),
size,
Expand Down Expand Up @@ -209,7 +209,7 @@ impl Shared {
pub fn add_canvas(
&self,
id: WindowId,
canvas: Weak<RefCell<backend::Canvas>>,
canvas: Weak<backend::Canvas>,
runner: DispatchRunner<Inner>,
) {
self.0.all_canvases.borrow_mut().push((id, canvas, runner));
Expand Down Expand Up @@ -456,7 +456,7 @@ impl Shared {
// - not visible and we don't know if it intersects yet
// - visible and intersects
if let (false, Some(true) | None) | (true, Some(true)) =
(is_visible, canvas.borrow().is_intersecting)
(is_visible, canvas.is_intersecting.get())
{
runner.send_event(Event::WindowEvent {
window_id: *id,
Expand Down Expand Up @@ -772,7 +772,6 @@ impl Shared {
// In case any remaining `Window`s are still not dropped, we will need
// to explicitly remove the event handlers associated with their canvases.
if let Some(canvas) = canvas.upgrade() {
let mut canvas = canvas.borrow_mut();
canvas.remove_listeners();
}
}
Expand Down Expand Up @@ -816,7 +815,7 @@ impl Shared {
DeviceEvents::WhenFocused => {
self.0.all_canvases.borrow().iter().any(|(_, canvas, _)| {
if let Some(canvas) = canvas.upgrade() {
canvas.borrow().has_focus.get()
canvas.has_focus.get()
} else {
false
}
Expand Down Expand Up @@ -886,7 +885,7 @@ impl WeakShared {

pub(crate) enum EventWrapper {
Event(Event),
ScaleChange { canvas: Weak<RefCell<backend::Canvas>>, size: PhysicalSize<u32>, scale: f64 },
ScaleChange { canvas: Weak<backend::Canvas>, size: PhysicalSize<u32>, scale: f64 },
}

impl From<Event> for EventWrapper {
Expand Down
10 changes: 4 additions & 6 deletions src/platform_impl/web/event_loop/window_target.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::cell::{Cell, RefCell};
use std::cell::Cell;
use std::clone::Clone;
use std::iter;
use std::rc::Rc;
Expand Down Expand Up @@ -79,9 +79,8 @@ impl ActiveEventLoop {
CustomCursorFuture(CustomCursor::new_async(self, source.inner))
}

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

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

move |new_size| {
let canvas = canvas.borrow();
canvas.set_current_size(new_size);
if canvas.old_size() != new_size {
canvas.set_old_size(new_size);
Expand All @@ -622,15 +620,15 @@ impl ActiveEventLoop {
canvas.on_intersection(move |is_intersecting| {
// only fire if visible while skipping the first event if it's intersecting
if backend::is_visible(runner.document())
&& !(is_intersecting && canvas_clone.borrow().is_intersecting.is_none())
&& !(is_intersecting && canvas_clone.is_intersecting.get().is_none())
{
runner.send_event(Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::Occluded(!is_intersecting),
});
}

canvas_clone.borrow_mut().is_intersecting = Some(is_intersecting);
canvas_clone.is_intersecting.set(Some(is_intersecting));
});

let runner = self.runner.clone();
Expand Down
Loading

0 comments on commit d0b5a30

Please sign in to comment.