chore(rustfmt): use nightly (#2325)

Stable rustfmt lacks a lot of features resulting in worse formatted
code, thus use nightly formatter.
This commit is contained in:
Kirill Chibisov 2024-04-26 19:11:44 +04:00 committed by GitHub
parent 7006c7ceca
commit 7b0c7b6cb2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
154 changed files with 3439 additions and 5891 deletions

View file

@ -17,11 +17,7 @@ impl AnimationFrameHandler {
move || handle.set(None)
});
Self {
window,
closure,
handle,
}
Self { window, closure, handle }
}
pub fn on_animation_frame<F>(&mut self, mut f: F)
@ -37,9 +33,7 @@ impl AnimationFrameHandler {
pub fn request(&self) {
if let Some(handle) = self.handle.take() {
self.window
.cancel_animation_frame(handle)
.expect("Failed to cancel animation frame");
self.window.cancel_animation_frame(handle).expect("Failed to cancel animation frame");
}
let handle = self
@ -52,9 +46,7 @@ impl AnimationFrameHandler {
pub fn cancel(&mut self) {
if let Some(handle) = self.handle.take() {
self.window
.cancel_animation_frame(handle)
.expect("Failed to cancel animation frame");
self.window.cancel_animation_frame(handle).expect("Failed to cancel animation frame");
}
}
}
@ -62,9 +54,7 @@ impl AnimationFrameHandler {
impl Drop for AnimationFrameHandler {
fn drop(&mut self) {
if let Some(handle) = self.handle.take() {
self.window
.cancel_animation_frame(handle)
.expect("Failed to cancel animation frame");
self.window.cancel_animation_frame(handle).expect("Failed to cancel animation frame");
}
}
}

View file

@ -4,7 +4,8 @@ use std::rc::Rc;
use std::sync::{Arc, Mutex};
use smol_str::SmolStr;
use wasm_bindgen::{closure::Closure, JsCast};
use wasm_bindgen::closure::Closure;
use wasm_bindgen::JsCast;
use web_sys::{
CssStyleDeclaration, Document, Event, FocusEvent, HtmlCanvasElement, KeyboardEvent,
PointerEvent, WheelEvent,
@ -53,8 +54,9 @@ pub struct Canvas {
pub struct Common {
pub window: web_sys::Window,
pub document: Document,
/// Note: resizing the HTMLCanvasElement should go through `backend::set_canvas_size` to ensure the DPI factor is maintained.
/// Note: this is read-only because we use a pointer to this for [`WindowHandle`][rwh_06::WindowHandle].
/// Note: resizing the HTMLCanvasElement should go through `backend::set_canvas_size` to ensure
/// the DPI factor is maintained. Note: this is read-only because we use a pointer to this
/// for [`WindowHandle`][rwh_06::WindowHandle].
raw: Rc<HtmlCanvasElement>,
style: Style,
old_size: Rc<Cell<PhysicalSize<u32>>>,
@ -188,10 +190,7 @@ impl Canvas {
pub fn position(&self) -> LogicalPosition<f64> {
let bounds = self.common.raw.get_bounding_client_rect();
let mut position = LogicalPosition {
x: bounds.x(),
y: bounds.y(),
};
let mut position = LogicalPosition { x: bounds.x(), y: bounds.y() };
if self.document().contains(Some(self.raw())) && self.style().get("display") != "none" {
position.x += super::style_size_property(self.style(), "border-left-width")
@ -298,9 +297,8 @@ impl Canvas {
F: 'static + FnMut(PhysicalKey, Key, Option<SmolStr>, KeyLocation, bool, ModifiersState),
{
let prevent_default = Rc::clone(&self.prevent_default);
self.on_keyboard_press = Some(self.common.add_event(
"keydown",
move |event: KeyboardEvent| {
self.on_keyboard_press =
Some(self.common.add_event("keydown", move |event: KeyboardEvent| {
if prevent_default.get() {
event.prevent_default();
}
@ -314,8 +312,7 @@ impl Canvas {
event.repeat(),
modifiers,
);
},
));
}));
}
pub fn on_cursor_leave<F>(&mut self, handler: F)
@ -459,14 +456,12 @@ impl Canvas {
pub(crate) fn on_context_menu(&mut self) {
let prevent_default = Rc::clone(&self.prevent_default);
self.on_context_menu = Some(self.common.add_event(
"contextmenu",
move |event: PointerEvent| {
self.on_context_menu =
Some(self.common.add_event("contextmenu", move |event: PointerEvent| {
if prevent_default.get() {
event.prevent_default();
}
},
));
}));
}
pub fn request_fullscreen(&self) {
@ -580,20 +575,14 @@ impl Style {
}
pub(crate) fn get(&self, property: &str) -> String {
self.read
.get_property_value(property)
.expect("Invalid property")
self.read.get_property_value(property).expect("Invalid property")
}
pub(crate) fn remove(&self, property: &str) {
self.write
.remove_property(property)
.expect("Property is read only");
self.write.remove_property(property).expect("Property is read only");
}
pub(crate) fn set(&self, property: &str, value: &str) {
self.write
.set_property(property, value)
.expect("Property is read only");
self.write.set_property(property, value).expect("Property is read only");
}
}

View file

@ -59,10 +59,9 @@ pub fn mouse_button(event: &MouseEvent) -> Option<MouseButton> {
2 => Some(MouseButton::Right),
3 => Some(MouseButton::Back),
4 => Some(MouseButton::Forward),
i => Some(MouseButton::Other(
i.try_into()
.expect("unexpected negative mouse button value"),
)),
i => {
Some(MouseButton::Other(i.try_into().expect("unexpected negative mouse button value")))
},
}
}
@ -93,10 +92,7 @@ pub fn mouse_position(event: &MouseEvent) -> LogicalPosition<f64> {
let event: &MouseEventExt = event.unchecked_ref();
LogicalPosition {
x: event.offset_x(),
y: event.offset_y(),
}
LogicalPosition { x: event.offset_x(), y: event.offset_y() }
}
// TODO: Remove this when Firefox supports correct movement values in coalesced events.
@ -114,17 +110,15 @@ impl MouseDelta {
// for `pointerrawupdate` support. Presumably an implementation of `pointerrawupdate`
// should require correct movement values, otherwise uncoalesced events might be broken as
// well.
Self(
(!has_pointer_raw_support(window) && has_coalesced_events_support(event)).then(|| {
MouseDeltaInner {
old_position: mouse_position(event),
old_delta: LogicalPosition {
x: event.movement_x() as f64,
y: event.movement_y() as f64,
},
}
}),
)
Self((!has_pointer_raw_support(window) && has_coalesced_events_support(event)).then(|| {
MouseDeltaInner {
old_position: mouse_position(event),
old_delta: LogicalPosition {
x: event.movement_x() as f64,
y: event.movement_y() as f64,
},
}
}))
}
pub fn delta(&mut self, event: &MouseEvent) -> LogicalPosition<f64> {
@ -136,10 +130,7 @@ impl MouseDelta {
inner.old_delta = LogicalPosition::new(0., 0.);
LogicalPosition::new(x, y)
} else {
LogicalPosition {
x: event.movement_x() as f64,
y: event.movement_y() as f64,
}
LogicalPosition { x: event.movement_x() as f64, y: event.movement_y() as f64 }
}
}
}
@ -156,7 +147,7 @@ pub fn mouse_scroll_delta(
WheelEvent::DOM_DELTA_PIXEL => {
let delta = LogicalPosition::new(x, y).to_physical(super::scale_factor(window));
Some(MouseScrollDelta::PixelDelta(delta))
}
},
_ => None,
}
}
@ -192,7 +183,7 @@ pub fn key_location(event: &KeyboardEvent) -> KeyLocation {
location => {
tracing::warn!("Unexpected key location: {location}");
KeyLocation::Standard
}
},
}
}
@ -238,14 +229,9 @@ pub fn pointer_move_event(event: PointerEvent) -> impl Iterator<Item = PointerEv
// make a single iterator depending on the availability of coalesced events
if has_coalesced_events_support(&event) {
None.into_iter().chain(
Some(
event
.get_coalesced_events()
.into_iter()
.map(PointerEvent::unchecked_from_js),
)
.into_iter()
.flatten(),
Some(event.get_coalesced_events().into_iter().map(PointerEvent::unchecked_from_js))
.into_iter()
.flatten(),
)
} else {
Some(event).into_iter().chain(None.into_iter().flatten())

View file

@ -1,4 +1,5 @@
use wasm_bindgen::{prelude::Closure, JsCast};
use wasm_bindgen::prelude::Closure;
use wasm_bindgen::JsCast;
use web_sys::EventTarget;
pub struct EventListenerHandle<T: ?Sized> {
@ -16,11 +17,7 @@ impl<T: ?Sized> EventListenerHandle<T> {
target
.add_event_listener_with_callback(event_type, listener.as_ref().unchecked_ref())
.expect("Failed to add event listener");
EventListenerHandle {
target,
event_type,
listener,
}
EventListenerHandle { target, event_type, listener }
}
}

View file

@ -58,7 +58,7 @@ pub fn is_fullscreen(document: &Document, canvas: &HtmlCanvasElement) -> bool {
Some(element) => {
let canvas: &Element = canvas;
canvas == &element
}
},
None => false,
}
}

View file

@ -1,5 +1,6 @@
use js_sys::Array;
use wasm_bindgen::{prelude::Closure, JsCast};
use wasm_bindgen::prelude::Closure;
use wasm_bindgen::JsCast;
use web_sys::{Element, IntersectionObserver, IntersectionObserverEntry};
pub(super) struct IntersectionObserverHandle {
@ -21,10 +22,7 @@ impl IntersectionObserverHandle {
.expect("Invalid `options`");
observer.observe(element);
Self {
observer,
_closure: closure,
}
Self { observer, _closure: closure }
}
}

View file

@ -1,4 +1,5 @@
use wasm_bindgen::{prelude::Closure, JsCast};
use wasm_bindgen::prelude::Closure;
use wasm_bindgen::JsCast;
use web_sys::MediaQueryList;
pub(super) struct MediaQueryListHandle {
@ -41,8 +42,7 @@ impl Drop for MediaQueryListHandle {
}
fn remove_listener(mql: &MediaQueryList, listener: &Closure<dyn FnMut()>) {
mql.remove_listener_with_opt_callback(Some(listener.as_ref().unchecked_ref()))
.unwrap_or_else(|e| {
web_sys::console::error_2(&"Error removing media query listener".into(), &e)
});
mql.remove_listener_with_opt_callback(Some(listener.as_ref().unchecked_ref())).unwrap_or_else(
|e| web_sys::console::error_2(&"Error removing media query listener".into(), &e),
);
}

View file

@ -9,8 +9,7 @@ mod pointer;
mod resize_scaling;
mod schedule;
pub use self::canvas::Canvas;
pub use self::canvas::Style;
pub use self::canvas::{Canvas, Style};
pub use self::event::ButtonsState;
pub use self::event_handle::EventListenerHandle;
pub use self::resize_scaling::ResizeScaleHandle;
@ -40,10 +39,7 @@ pub fn on_page_transition(
let show_listener =
event_handle::EventListenerHandle::new(window.clone(), "pageshow", show_closure);
let hide_listener = event_handle::EventListenerHandle::new(window, "pagehide", hide_closure);
PageTransitionEventHandle {
_show_listener: show_listener,
_hide_listener: hide_listener,
}
PageTransitionEventHandle { _show_listener: show_listener, _hide_listener: hide_listener }
}
pub fn scale_factor(window: &web_sys::Window) -> f64 {
@ -154,11 +150,7 @@ pub fn style_size_property(style: &Style, property: &str) -> f64 {
}
pub fn is_dark_mode(window: &web_sys::Window) -> Option<bool> {
window
.match_media("(prefers-color-scheme: dark)")
.ok()
.flatten()
.map(|media| media.matches())
window.match_media("(prefers-color-scheme: dark)").ok().flatten().map(|media| media.matches())
}
pub fn is_visible(document: &Document) -> bool {

View file

@ -37,9 +37,8 @@ impl PointerHandler {
where
F: 'static + FnMut(ModifiersState, Option<i32>),
{
self.on_cursor_leave = Some(canvas_common.add_event(
"pointerout",
move |event: PointerEvent| {
self.on_cursor_leave =
Some(canvas_common.add_event("pointerout", move |event: PointerEvent| {
let modifiers = event::mouse_modifiers(&event);
// touch events are handled separately
@ -48,17 +47,15 @@ impl PointerHandler {
let pointer_id = (event.pointer_type() == "mouse").then(|| event.pointer_id());
handler(modifiers, pointer_id);
},
));
}));
}
pub fn on_cursor_enter<F>(&mut self, canvas_common: &Common, mut handler: F)
where
F: 'static + FnMut(ModifiersState, Option<i32>),
{
self.on_cursor_enter = Some(canvas_common.add_event(
"pointerover",
move |event: PointerEvent| {
self.on_cursor_enter =
Some(canvas_common.add_event("pointerover", move |event: PointerEvent| {
let modifiers = event::mouse_modifiers(&event);
// touch events are handled separately
@ -67,8 +64,7 @@ impl PointerHandler {
let pointer_id = (event.pointer_type() == "mouse").then(|| event.pointer_id());
handler(modifiers, pointer_id);
},
));
}));
}
pub fn on_mouse_release<MOD, M, T>(
@ -83,9 +79,8 @@ impl PointerHandler {
T: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, Force),
{
let window = canvas_common.window.clone();
self.on_pointer_release = Some(canvas_common.add_event(
"pointerup",
move |event: PointerEvent| {
self.on_pointer_release =
Some(canvas_common.add_event("pointerup", move |event: PointerEvent| {
let modifiers = event::mouse_modifiers(&event);
match event.pointer_type().as_str() {
@ -103,8 +98,7 @@ impl PointerHandler {
),
_ => modifier_handler(modifiers),
}
},
));
}));
}
pub fn on_mouse_press<MOD, M, T>(
@ -121,9 +115,8 @@ impl PointerHandler {
{
let window = canvas_common.window.clone();
let canvas = canvas_common.raw().clone();
self.on_pointer_press = Some(canvas_common.add_event(
"pointerdown",
move |event: PointerEvent| {
self.on_pointer_press =
Some(canvas_common.add_event("pointerdown", move |event: PointerEvent| {
if prevent_default.get() {
// prevent text selection
event.prevent_default();
@ -141,7 +134,7 @@ impl PointerHandler {
event::mouse_position(&event).to_physical(super::scale_factor(&window)),
Force::Normalized(event.pressure() as f64),
);
}
},
"mouse" => {
mouse_handler(
modifiers,
@ -150,15 +143,15 @@ impl PointerHandler {
event::mouse_button(&event).expect("no mouse button pressed"),
);
// Error is swallowed here since the error would occur every time the mouse is
// clicked when the cursor is grabbed, and there is probably not a situation where
// this could fail, that we care if it fails.
// Error is swallowed here since the error would occur every time the mouse
// is clicked when the cursor is grabbed, and there
// is probably not a situation where this could
// fail, that we care if it fails.
let _e = canvas.set_pointer_capture(event.pointer_id());
}
},
_ => modifier_handler(modifiers),
}
},
));
}));
}
pub fn on_cursor_move<MOD, M, T, B>(
@ -178,9 +171,8 @@ impl PointerHandler {
{
let window = canvas_common.window.clone();
let canvas = canvas_common.raw().clone();
self.on_cursor_move = Some(canvas_common.add_event(
"pointermove",
move |event: PointerEvent| {
self.on_cursor_move =
Some(canvas_common.add_event("pointermove", move |event: PointerEvent| {
let modifiers = event::mouse_modifiers(&event);
let pointer_type = event.pointer_type();
@ -239,8 +231,7 @@ impl PointerHandler {
),
_ => unreachable!("didn't return early before"),
};
},
));
}));
}
pub fn on_touch_cancel<F>(&mut self, canvas_common: &Common, mut handler: F)
@ -248,9 +239,8 @@ impl PointerHandler {
F: 'static + FnMut(i32, PhysicalPosition<f64>, Force),
{
let window = canvas_common.window.clone();
self.on_touch_cancel = Some(canvas_common.add_event(
"pointercancel",
move |event: PointerEvent| {
self.on_touch_cancel =
Some(canvas_common.add_event("pointercancel", move |event: PointerEvent| {
if event.pointer_type() == "touch" {
handler(
event.pointer_id(),
@ -258,8 +248,7 @@ impl PointerHandler {
Force::Normalized(event.pressure() as f64),
);
}
},
));
}));
}
pub fn remove_listeners(&mut self) {

View file

@ -210,9 +210,8 @@ impl ResizeScaleInternal {
// This should never happen, but if it does then apparently the scale factor didn't change.
if mql.matches() {
warn!(
"media query tracking scale factor was triggered without a change:\n\
Media Query: {}\n\
Current Scale: {scale}",
"media query tracking scale factor was triggered without a change:\nMedia Query: \
{}\nCurrent Scale: {scale}",
mql.media(),
);
return;
@ -240,10 +239,8 @@ impl ResizeScaleInternal {
.to_physical(backend::scale_factor(&self.window));
}
let entry: ResizeObserverSize = entry
.device_pixel_content_box_size()
.get(0)
.unchecked_into();
let entry: ResizeObserverSize =
entry.device_pixel_content_box_size().get(0).unchecked_into();
let writing_mode = self.style.get("writing-mode");
@ -259,14 +256,14 @@ impl ResizeScaleInternal {
_ if writing_mode.starts_with("horizontal") => true,
_ if writing_mode.starts_with("vertical") | writing_mode.starts_with("sideways") => {
false
}
},
// deprecated values
"lr" | "lr-tb" | "rl" => true,
"tb" | "tb-lr" | "tb-rl" => false,
_ => {
warn!("unrecognized `writing-mode`, assuming horizontal");
true
}
},
};
if horizontal {

View file

@ -89,10 +89,7 @@ impl Schedule {
.catch(handler);
});
Schedule {
_closure: closure,
inner: Inner::Scheduler { controller },
}
Schedule { _closure: closure, inner: Inner::Scheduler { controller } }
}
fn new_idle_callback<F>(window: web_sys::Window, f: F) -> Schedule
@ -104,10 +101,7 @@ impl Schedule {
.request_idle_callback(closure.as_ref().unchecked_ref())
.expect("Failed to request idle callback");
Schedule {
_closure: closure,
inner: Inner::IdleCallback { window, handle },
}
Schedule { _closure: closure, inner: Inner::IdleCallback { window, handle } }
}
fn new_timeout<F>(window: web_sys::Window, f: F, duration: Option<Duration>) -> Schedule
@ -122,9 +116,7 @@ impl Schedule {
let port_2 = channel.port2();
let timeout_closure = Closure::new(move || {
port_2
.post_message(&JsValue::UNDEFINED)
.expect("Failed to send message")
port_2.post_message(&JsValue::UNDEFINED).expect("Failed to send message")
});
let handle = if let Some(duration) = duration {
// `Duration::as_millis()` always rounds down (because of truncation), we want to round
@ -168,16 +160,11 @@ impl Drop for Schedule {
match &self.inner {
Inner::Scheduler { controller, .. } => controller.abort(),
Inner::IdleCallback { window, handle, .. } => window.cancel_idle_callback(*handle),
Inner::Timeout {
window,
handle,
port,
..
} => {
Inner::Timeout { window, handle, port, .. } => {
window.clear_timeout_with_handle(*handle);
port.close();
port.set_onmessage(None);
}
},
}
}
}