Skip to content

Commit

Permalink
winit: Remove unused function and don't depend on copypasta on wasm
Browse files Browse the repository at this point in the history
Nightly Rust start warning about these.
And in the case of wasm we don't even need to depend on copypasta since
we handle the copy paste ourselves
  • Loading branch information
ogoffart committed Feb 21, 2024
1 parent 88b0b74 commit fd5a687
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 28 deletions.
2 changes: 1 addition & 1 deletion internal/backends/winit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ const-field-offset = { version = "0.1", path = "../../../helper_crates/const-fie
vtable = { version = "0.1.6", path = "../../../helper_crates/vtable" }

cfg-if = "1"
copypasta = { version = "0.10", default-features = false }
derive_more = "0.99.5"
lyon_path = "1.0"
once_cell = "1.5"
Expand Down Expand Up @@ -69,6 +68,7 @@ glutin = { workspace = true, optional = true, default-features = false, features
glutin-winit = { version = "0.4.2", optional = true, default-features = false, features = ["egl", "wgl"] }
accesskit = { version = "0.12.2", optional = true }
accesskit_winit = { version = "0.18.0", optional = true }
copypasta = { version = "0.10", default-features = false }

[target.'cfg(target_os = "macos")'.dependencies]
# For GL rendering
Expand Down
46 changes: 19 additions & 27 deletions internal/backends/winit/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
use crate::winitwindowadapter::WinitWindowAdapter;
use crate::SlintUserEvent;
#[cfg(not(target_arch = "wasm32"))]
use copypasta::ClipboardProvider;
use corelib::api::EventLoopError;
use corelib::graphics::euclid;
Expand All @@ -18,15 +19,18 @@ use corelib::lengths::LogicalPoint;
use corelib::platform::PlatformError;
use corelib::window::*;
use i_slint_core as corelib;
#[allow(unused_imports)]
use std::cell::{RefCell, RefMut};
use std::rc::{Rc, Weak};
use winit::event::{Event, WindowEvent};
use winit::event_loop::EventLoopWindowTarget;

#[cfg(not(target_arch = "wasm32"))]
/// The Default, and the selection clippoard
type ClipboardPair = (Box<dyn ClipboardProvider>, Box<dyn ClipboardProvider>);

struct NotRunningEventLoop {
#[cfg(not(target_arch = "wasm32"))]
clipboard: RefCell<ClipboardPair>,
instance: winit::event_loop::EventLoop<SlintUserEvent>,
event_loop_proxy: winit::event_loop::EventLoopProxy<SlintUserEvent>,
Expand Down Expand Up @@ -58,20 +62,24 @@ impl NotRunningEventLoop {
let instance =
builder.build().map_err(|e| format!("Error initializing winit event loop: {e}"))?;
let event_loop_proxy = instance.create_proxy();
let clipboard = RefCell::new(create_clipboard(&instance));
Ok(Self { clipboard, instance, event_loop_proxy })
Ok(Self {
#[cfg(not(target_arch = "wasm32"))]
clipboard: RefCell::new(create_clipboard(&instance)),
instance,
event_loop_proxy,
})
}
}

struct RunningEventLoop<'a> {
event_loop_target: &'a winit::event_loop::EventLoopWindowTarget<SlintUserEvent>,
event_loop_proxy: &'a winit::event_loop::EventLoopProxy<SlintUserEvent>,
#[cfg(not(target_arch = "wasm32"))]
clipboard: &'a RefCell<ClipboardPair>,
}

pub(crate) trait EventLoopInterface {
fn event_loop_target(&self) -> &winit::event_loop::EventLoopWindowTarget<SlintUserEvent>;
fn event_loop_proxy(&self) -> &winit::event_loop::EventLoopProxy<SlintUserEvent>;
#[cfg(not(target_arch = "wasm32"))]
fn clipboard(
&self,
_: i_slint_core::platform::Clipboard,
Expand All @@ -83,10 +91,7 @@ impl EventLoopInterface for NotRunningEventLoop {
&self.instance
}

fn event_loop_proxy(&self) -> &winit::event_loop::EventLoopProxy<SlintUserEvent> {
&self.event_loop_proxy
}

#[cfg(not(target_arch = "wasm32"))]
fn clipboard(
&self,
clipboard: i_slint_core::platform::Clipboard,
Expand All @@ -108,10 +113,7 @@ impl<'a> EventLoopInterface for RunningEventLoop<'a> {
self.event_loop_target
}

fn event_loop_proxy(&self) -> &winit::event_loop::EventLoopProxy<SlintUserEvent> {
self.event_loop_proxy
}

#[cfg(not(target_arch = "wasm32"))]
fn clipboard(
&self,
clipboard: i_slint_core::platform::Clipboard,
Expand Down Expand Up @@ -524,18 +526,17 @@ impl EventLoopState {
});

let mut winit_loop = not_running_loop_instance.instance;
let clipboard = not_running_loop_instance.clipboard;

#[cfg(not(target_arch = "wasm32"))]
{
use winit::platform::run_on_demand::EventLoopExtRunOnDemand as _;
let clipboard = not_running_loop_instance.clipboard;
winit_loop
.run_on_demand(
|event: Event<SlintUserEvent>,
event_loop_target: &EventLoopWindowTarget<SlintUserEvent>| {
let running_instance = RunningEventLoop {
event_loop_target,
event_loop_proxy: &event_loop_proxy,
clipboard: &clipboard,
};
CURRENT_WINDOW_TARGET.set(&running_instance, || {
Expand Down Expand Up @@ -566,8 +567,6 @@ impl EventLoopState {
event_loop_target: &EventLoopWindowTarget<SlintUserEvent>| {
let running_instance = RunningEventLoop {
event_loop_target,
event_loop_proxy: &event_loop_proxy,
clipboard: &clipboard,
};
CURRENT_WINDOW_TARGET.set(&running_instance, || {
self.process_event(event, event_loop_target)
Expand Down Expand Up @@ -611,11 +610,8 @@ impl EventLoopState {
timeout,
|event: Event<SlintUserEvent>,
event_loop_target: &EventLoopWindowTarget<SlintUserEvent>| {
let running_instance = RunningEventLoop {
event_loop_target,
event_loop_proxy: &event_loop_proxy,
clipboard: &clipboard,
};
let running_instance =
RunningEventLoop { event_loop_target, clipboard: &clipboard };
CURRENT_WINDOW_TARGET
.set(&running_instance, || self.process_event(event, event_loop_target))
},
Expand Down Expand Up @@ -653,17 +649,12 @@ pub fn spawn() -> Result<(), corelib::platform::PlatformError> {
.set_proxy(event_loop_proxy.clone())
});

let clipboard = not_running_loop_instance.clipboard;
let mut loop_state = EventLoopState::default();

not_running_loop_instance.instance.spawn(
move |event: Event<SlintUserEvent>,
event_loop_target: &EventLoopWindowTarget<SlintUserEvent>| {
let running_instance = RunningEventLoop {
event_loop_target,
event_loop_proxy: &event_loop_proxy,
clipboard: &clipboard,
};
let running_instance = RunningEventLoop { event_loop_target };
CURRENT_WINDOW_TARGET
.set(&running_instance, || loop_state.process_event(event, event_loop_target))
},
Expand All @@ -672,6 +663,7 @@ pub fn spawn() -> Result<(), corelib::platform::PlatformError> {
Ok(())
}

#[cfg(not(target_arch = "wasm32"))]
fn create_clipboard<T>(_event_loop: &winit::event_loop::EventLoopWindowTarget<T>) -> ClipboardPair {
// Provide a truly silent no-op clipboard context, as copypasta's NoopClipboard spams stdout with
// println.
Expand Down

0 comments on commit fd5a687

Please sign in to comment.