diff options
author | 2023-03-13 14:16:45 -0700 | |
---|---|---|
committer | 2023-03-13 14:16:45 -0700 | |
commit | 8ba18430800142965549077373e2a45d0a3429a1 (patch) | |
tree | 0ba36d187e3c66e88476779dc40ba3cf0b28bff0 /winit | |
parent | fa068b904a904c86195ebfaa4e953466426a27aa (diff) | |
download | iced-8ba18430800142965549077373e2a45d0a3429a1.tar.gz iced-8ba18430800142965549077373e2a45d0a3429a1.tar.bz2 iced-8ba18430800142965549077373e2a45d0a3429a1.zip |
Code cleanup, clearer comments + removed some unnecessary dupe;
Removed `Frames` struct return for `window::frames()` since we are just redrawing every window anyways;
Interface dropping;
Diffstat (limited to 'winit')
-rw-r--r-- | winit/Cargo.toml | 2 | ||||
-rw-r--r-- | winit/src/application.rs | 2 | ||||
-rw-r--r-- | winit/src/lib.rs | 2 | ||||
-rw-r--r-- | winit/src/multi_window.rs | 126 | ||||
-rw-r--r-- | winit/src/multi_window/state.rs | 2 |
5 files changed, 50 insertions, 84 deletions
diff --git a/winit/Cargo.toml b/winit/Cargo.toml index 7de69528..9efd1890 100644 --- a/winit/Cargo.toml +++ b/winit/Cargo.toml @@ -16,7 +16,7 @@ chrome-trace = ["trace", "tracing-chrome"] debug = ["iced_native/debug"] system = ["sysinfo"] application = [] -multi_window = [] +multi-window = [] [dependencies] window_clipboard = "0.2" diff --git a/winit/src/application.rs b/winit/src/application.rs index 906da3c6..fe97486f 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -743,7 +743,7 @@ pub fn run_command<A, E>( } window::Action::Spawn { .. } => { log::info!( - "This is only available on `multi_window::Application`" + "Spawning a window is only available with `multi_window::Application`s." ) } window::Action::Resize { width, height } => { diff --git a/winit/src/lib.rs b/winit/src/lib.rs index fe5768df..54b9c31f 100644 --- a/winit/src/lib.rs +++ b/winit/src/lib.rs @@ -35,7 +35,7 @@ pub use iced_native::*; pub use winit; -#[cfg(feature = "multi_window")] +#[cfg(feature = "multi-window")] pub mod multi_window; #[cfg(feature = "application")] diff --git a/winit/src/multi_window.rs b/winit/src/multi_window.rs index eac8b260..d5da406c 100644 --- a/winit/src/multi_window.rs +++ b/winit/src/multi_window.rs @@ -33,7 +33,7 @@ use tracing::{info_span, instrument::Instrument}; /// This is a wrapper around the `Application::Message` associate type /// to allows the `shell` to create internal messages, while still having -/// the current user specified custom messages. +/// the current user-specified custom messages. #[derive(Debug)] pub enum Event<Message> { /// An [`Application`] generated message @@ -53,9 +53,9 @@ pub enum Event<Message> { WindowCreated(window::Id, winit::window::Window), } -/// An interactive, native cross-platform application. +/// An interactive, native, cross-platform, multi-windowed application. /// -/// This trait is the main entrypoint of Iced. Once implemented, you can run +/// This trait is the main entrypoint of multi-window Iced. Once implemented, you can run /// your GUI application by simply calling [`run`]. It will run in /// its own window. /// @@ -105,7 +105,7 @@ where /// load state from a file, perform an initial HTTP request, etc. fn new(flags: Self::Flags) -> (Self, Command<Self::Message>); - /// Returns the current title of each [`Application`] window. + /// Returns the current title of each window of the [`Application`]. /// /// This title can be dynamic! The runtime will automatically update the /// title of your application when necessary. @@ -155,7 +155,8 @@ where false } - /// Requests that the [`window`] be closed. + /// Returns the `Self::Message` that should be processed when a `window` is requested to + /// be closed. fn close_requested(&self, window: window::Id) -> Self::Message; } @@ -462,9 +463,9 @@ async fn run_instance<A, E, C>( } debug.event_processing_finished(); - // Update application with app message(s) - // Note: without tying an app message to a window ID, we must redraw all windows - // as we cannot know what changed without some kind of damage tracking. + // Update application with app messages + // Unless we implement some kind of diffing, we must redraw all windows as we + // cannot know what changed. if !messages.is_empty() || matches!( interface_state, @@ -612,9 +613,7 @@ async fn run_instance<A, E, C>( } Event::WindowCreated(id, window) => { let mut surface = compositor.create_surface(&window); - let state = State::new(&application, id, &window); - let physical_size = state.physical_size(); compositor.configure_surface( @@ -776,14 +775,12 @@ async fn run_instance<A, E, C>( } _ => { debug.render_finished(); + log::error!("Error {error:?} when presenting surface."); - // Try rendering again next frame. - // TODO(derezzedex) - windows - .values() - .next() - .expect("No window found") - .request_redraw(); + // Try rendering windows again next frame. + for window in windows.values() { + window.request_redraw(); + } } }, } @@ -792,80 +789,45 @@ async fn run_instance<A, E, C>( event: window_event, window_id, } => { - // dbg!(window_id); - if let Some(window) = - window_ids.get(&window_id).and_then(|id| windows.get(id)) - { - if let Some(state) = window_ids + if let (Some(window), Some(state)) = ( + window_ids.get(&window_id).and_then(|id| windows.get(id)), + window_ids .get(&window_id) - .and_then(|id| states.get_mut(id)) - { - if requests_exit(&window_event, state.modifiers()) { - if let Some(id) = - window_ids.get(&window_id).cloned() - { - let message = application.close_requested(id); - messages.push(message); - } + .and_then(|id| states.get_mut(id)), + ) { + if crate::application::requests_exit(&window_event, state.modifiers()) { + if let Some(id) = window_ids.get(&window_id).cloned() { + let message = application.close_requested(id); + messages.push(message); } + } - state.update(window, &window_event, &mut debug); - - if let Some(event) = conversion::window_event( - *window_ids.get(&window_id).unwrap(), - &window_event, - state.scale_factor(), - state.modifiers(), - ) { - events.push(( - window_ids.get(&window_id).cloned(), - event, - )); - } - } else { - log::error!( - "No window state found for id: {:?}", - window_id - ); + state.update(window, &window_event, &mut debug); + + if let Some(event) = conversion::window_event( + *window_ids.get(&window_id).unwrap(), + &window_event, + state.scale_factor(), + state.modifiers(), + ) { + events + .push((window_ids.get(&window_id).cloned(), event)); } } else { - log::error!("No window found with id: {:?}", window_id); + log::error!( + "Could not find window or state for id: {window_id:?}" + ); } } _ => {} } } - // Manually drop the user interface - // drop(ManuallyDrop::into_inner(user_interface)); -} - -/// Returns true if the provided event should cause an [`Application`] to -/// exit. -pub fn requests_exit( - event: &winit::event::WindowEvent<'_>, - _modifiers: winit::event::ModifiersState, -) -> bool { - use winit::event::WindowEvent; - - match event { - WindowEvent::CloseRequested => true, - #[cfg(target_os = "macos")] - WindowEvent::KeyboardInput { - input: - winit::event::KeyboardInput { - virtual_keycode: Some(winit::event::VirtualKeyCode::Q), - state: winit::event::ElementState::Pressed, - .. - }, - .. - } if _modifiers.logo() => true, - _ => false, - } + // Manually drop the user interfaces + drop(ManuallyDrop::into_inner(interfaces)); } -/// Builds a [`UserInterface`] for the provided [`Application`], logging -/// [`struct@Debug`] information accordingly. +/// Builds a window's [`UserInterface`] for the [`Application`]. pub fn build_user_interface<'a, A: Application>( application: &'a A, cache: user_interface::Cache, @@ -890,7 +852,9 @@ where #[cfg(feature = "trace")] let layout_span = info_span!("Application", "LAYOUT").entered(); debug.layout_started(); + let user_interface = UserInterface::build(view, size, cache, renderer); + #[cfg(feature = "trace")] let _ = layout_span.exit(); debug.layout_finished(); @@ -898,7 +862,7 @@ where user_interface } -/// Updates an [`Application`] by feeding it the provided messages, spawning any +/// Updates an [`Application`] by feeding it messages, spawning any /// resulting [`Command`], and tracking its [`Subscription`]. pub fn update<A: Application, E: Executor>( application: &mut A, @@ -923,7 +887,9 @@ pub fn update<A: Application, E: Executor>( debug.log_message(&message); debug.update_started(); + let command = runtime.enter(|| application.update(message)); + #[cfg(feature = "trace")] let _ = update_span.exit(); debug.update_finished(); @@ -1023,7 +989,7 @@ pub fn run_command<A, E>( let window = windows.get(&id).expect("No window found"); window.set_visible(conversion::visible(mode)); window.set_fullscreen(conversion::fullscreen( - window.primary_monitor(), + window.current_monitor(), mode, )); } diff --git a/winit/src/multi_window/state.rs b/winit/src/multi_window/state.rs index a7e65de7..d0e442d0 100644 --- a/winit/src/multi_window/state.rs +++ b/winit/src/multi_window/state.rs @@ -179,7 +179,7 @@ where /// Synchronizes the [`State`] with its [`Application`] and its respective /// window. /// - /// Normally an [`Application`] should be synchronized with its [`State`] + /// Normally, an [`Application`] should be synchronized with its [`State`] /// and window after calling [`Application::update`]. /// /// [`Application::update`]: crate::Program::update |