diff options
Diffstat (limited to 'winit/src')
-rw-r--r-- | winit/src/application.rs | 560 | ||||
-rw-r--r-- | winit/src/application/profiler.rs | 101 | ||||
-rw-r--r-- | winit/src/application/state.rs | 24 | ||||
-rw-r--r-- | winit/src/clipboard.rs | 3 | ||||
-rw-r--r-- | winit/src/conversion.rs | 751 | ||||
-rw-r--r-- | winit/src/lib.rs | 9 | ||||
-rw-r--r-- | winit/src/multi_window.rs | 1189 | ||||
-rw-r--r-- | winit/src/multi_window/state.rs | 242 | ||||
-rw-r--r-- | winit/src/multi_window/window_manager.rs | 157 | ||||
-rw-r--r-- | winit/src/position.rs | 22 | ||||
-rw-r--r-- | winit/src/settings.rs | 228 | ||||
-rw-r--r-- | winit/src/settings/linux.rs | 11 | ||||
-rw-r--r-- | winit/src/settings/macos.rs | 12 | ||||
-rw-r--r-- | winit/src/settings/other.rs | 3 | ||||
-rw-r--r-- | winit/src/settings/wasm.rs | 11 | ||||
-rw-r--r-- | winit/src/settings/windows.rs | 21 |
16 files changed, 2367 insertions, 977 deletions
diff --git a/winit/src/application.rs b/winit/src/application.rs index 8105f8d9..09bf63cc 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -1,6 +1,4 @@ //! Create interactive, native cross-platform applications. -#[cfg(feature = "trace")] -mod profiler; mod state; pub use state::State; @@ -26,11 +24,7 @@ use crate::{Clipboard, Error, Proxy, Settings}; use futures::channel::mpsc; use std::mem::ManuallyDrop; - -#[cfg(feature = "trace")] -pub use profiler::Profiler; -#[cfg(feature = "trace")] -use tracing::{info_span, instrument::Instrument}; +use std::sync::Arc; /// An interactive, native cross-platform application. /// @@ -119,16 +113,12 @@ where use futures::Future; use winit::event_loop::EventLoopBuilder; - #[cfg(feature = "trace")] - let _guard = Profiler::init(); - let mut debug = Debug::new(); debug.startup_started(); - #[cfg(feature = "trace")] - let _ = info_span!("Application", "RUN").entered(); - - let event_loop = EventLoopBuilder::with_user_event().build(); + let event_loop = EventLoopBuilder::with_user_event() + .build() + .expect("Create event loop"); let proxy = event_loop.create_proxy(); let runtime = { @@ -148,26 +138,29 @@ where let target = settings.window.platform_specific.target.clone(); let should_be_visible = settings.window.visible; - let builder = settings - .window - .into_builder( - &application.title(), - event_loop.primary_monitor(), - settings.id, - ) - .with_visible(false); + let exit_on_close_request = settings.window.exit_on_close_request; + + let builder = conversion::window_settings( + settings.window, + &application.title(), + event_loop.primary_monitor(), + settings.id, + ) + .with_visible(false); log::debug!("Window builder: {builder:#?}"); - let window = builder - .build(&event_loop) - .map_err(Error::WindowCreationFailed)?; + let window = Arc::new( + builder + .build(&event_loop) + .map_err(Error::WindowCreationFailed)?, + ); #[cfg(target_arch = "wasm32")] { use winit::platform::web::WindowExtWebSys; - let canvas = window.canvas(); + let canvas = window.canvas().expect("Get window canvas"); let window = web_sys::window().unwrap(); let document = window.document().unwrap(); @@ -193,75 +186,57 @@ where }; } - let (compositor, renderer) = C::new(compositor_settings, Some(&window))?; + let compositor = C::new(compositor_settings, window.clone())?; + let mut renderer = compositor.create_renderer(); - let (mut event_sender, event_receiver) = mpsc::unbounded(); - let (control_sender, mut control_receiver) = mpsc::unbounded(); + for font in settings.fonts { + use crate::core::text::Renderer; - let mut instance = Box::pin({ - let run_instance = run_instance::<A, E, C>( - application, - compositor, - renderer, - runtime, - proxy, - debug, - event_receiver, - control_sender, - init_command, - window, - should_be_visible, - settings.exit_on_close_request, - ); + renderer.load_font(font); + } - #[cfg(feature = "trace")] - let run_instance = - run_instance.instrument(info_span!("Application", "LOOP")); + let (mut event_sender, event_receiver) = mpsc::unbounded(); + let (control_sender, mut control_receiver) = mpsc::unbounded(); - run_instance - }); + let mut instance = Box::pin(run_instance::<A, E, C>( + application, + compositor, + renderer, + runtime, + proxy, + debug, + event_receiver, + control_sender, + init_command, + window, + should_be_visible, + exit_on_close_request, + )); let mut context = task::Context::from_waker(task::noop_waker_ref()); - platform::run(event_loop, move |event, _, control_flow| { - use winit::event_loop::ControlFlow; - - if let ControlFlow::ExitWithCode(_) = control_flow { + let _ = event_loop.run(move |event, event_loop| { + if event_loop.exiting() { return; } - let event = match event { - winit::event::Event::WindowEvent { - event: - winit::event::WindowEvent::ScaleFactorChanged { - new_inner_size, - .. - }, - window_id, - } => Some(winit::event::Event::WindowEvent { - event: winit::event::WindowEvent::Resized(*new_inner_size), - window_id, - }), - _ => event.to_static(), - }; + event_sender.start_send(event).expect("Send event"); - if let Some(event) = event { - event_sender.start_send(event).expect("Send event"); + let poll = instance.as_mut().poll(&mut context); - let poll = instance.as_mut().poll(&mut context); - - match poll { - task::Poll::Pending => { - if let Ok(Some(flow)) = control_receiver.try_next() { - *control_flow = flow; - } + match poll { + task::Poll::Pending => { + if let Ok(Some(flow)) = control_receiver.try_next() { + event_loop.set_control_flow(flow); } - task::Poll::Ready(_) => { - *control_flow = ControlFlow::Exit; - } - }; - } - }) + } + task::Poll::Ready(_) => { + event_loop.exit(); + } + }; + }); + + Ok(()) } async fn run_instance<A, E, C>( @@ -272,11 +247,11 @@ async fn run_instance<A, E, C>( mut proxy: winit::event_loop::EventLoopProxy<A::Message>, mut debug: Debug, mut event_receiver: mpsc::UnboundedReceiver< - winit::event::Event<'_, A::Message>, + winit::event::Event<A::Message>, >, mut control_sender: mpsc::UnboundedSender<winit::event_loop::ControlFlow>, init_command: Command<A::Message>, - window: winit::window::Window, + window: Arc<winit::window::Window>, should_be_visible: bool, exit_on_close_request: bool, ) where @@ -296,7 +271,7 @@ async fn run_instance<A, E, C>( let mut clipboard = Clipboard::connect(&window); let mut cache = user_interface::Cache::default(); let mut surface = compositor.create_surface( - &window, + window.clone(), physical_size.width, physical_size.height, ); @@ -340,77 +315,56 @@ async fn run_instance<A, E, C>( while let Some(event) = event_receiver.next().await { match event { - event::Event::NewEvents(start_cause) => { - redraw_pending = matches!( - start_cause, - event::StartCause::Init - | event::StartCause::Poll - | event::StartCause::ResumeTimeReached { .. } - ); + event::Event::NewEvents( + event::StartCause::Init + | event::StartCause::ResumeTimeReached { .. }, + ) if !redraw_pending => { + window.request_redraw(); + redraw_pending = true; } - event::Event::MainEventsCleared => { - if !redraw_pending && events.is_empty() && messages.is_empty() { - continue; - } + event::Event::PlatformSpecific(event::PlatformSpecific::MacOS( + event::MacOS::ReceivedUrl(url), + )) => { + use crate::core::event; - debug.event_processing_started(); + events.push(Event::PlatformSpecific( + event::PlatformSpecific::MacOS(event::MacOS::ReceivedUrl( + url, + )), + )); + } + event::Event::UserEvent(message) => { + messages.push(message); + } + event::Event::WindowEvent { + event: event::WindowEvent::RedrawRequested { .. }, + .. + } => { + let physical_size = state.physical_size(); - let (interface_state, statuses) = user_interface.update( - &events, - state.cursor(), - &mut renderer, - &mut clipboard, - &mut messages, - ); + if physical_size.width == 0 || physical_size.height == 0 { + continue; + } - debug.event_processing_finished(); + let current_viewport_version = state.viewport_version(); - for (event, status) in - events.drain(..).zip(statuses.into_iter()) - { - runtime.broadcast(event, status); - } + if viewport_version != current_viewport_version { + let logical_size = state.logical_size(); - if !messages.is_empty() - || matches!( - interface_state, - user_interface::State::Outdated - ) - { - let mut cache = - ManuallyDrop::into_inner(user_interface).into_cache(); + debug.layout_started(); + user_interface = ManuallyDrop::new( + ManuallyDrop::into_inner(user_interface) + .relayout(logical_size, &mut renderer), + ); + debug.layout_finished(); - // Update application - update( - &mut application, - &mut compositor, + compositor.configure_surface( &mut surface, - &mut cache, - &state, - &mut renderer, - &mut runtime, - &mut clipboard, - &mut should_exit, - &mut proxy, - &mut debug, - &mut messages, - &window, + physical_size.width, + physical_size.height, ); - // Update window - state.synchronize(&application, &window); - - user_interface = ManuallyDrop::new(build_user_interface( - &application, - cache, - &mut renderer, - state.logical_size(), - &mut debug, - )); - - if should_exit { - break; - } + viewport_version = current_viewport_version; } // TODO: Avoid redrawing all the time by forcing widgets to @@ -419,6 +373,7 @@ async fn run_instance<A, E, C>( // Then, we can use the `interface_state` here to decide if a redraw // is needed right away, or simply wait until a specific time. let redraw_event = Event::Window( + window::Id::MAIN, window::Event::RedrawRequested(Instant::now()), ); @@ -430,6 +385,24 @@ async fn run_instance<A, E, C>( &mut messages, ); + let _ = control_sender.start_send(match interface_state { + user_interface::State::Updated { + redraw_request: Some(redraw_request), + } => match redraw_request { + window::RedrawRequest::NextFrame => { + window.request_redraw(); + + ControlFlow::Wait + } + window::RedrawRequest::At(at) => { + ControlFlow::WaitUntil(at) + } + }, + _ => ControlFlow::Wait, + }); + + runtime.broadcast(redraw_event, core::event::Status::Ignored); + debug.draw_started(); let new_mouse_interaction = user_interface.draw( &mut renderer, @@ -439,6 +412,7 @@ async fn run_instance<A, E, C>( }, state.cursor(), ); + redraw_pending = false; debug.draw_finished(); if new_mouse_interaction != mouse_interaction { @@ -449,88 +423,7 @@ async fn run_instance<A, E, C>( mouse_interaction = new_mouse_interaction; } - window.request_redraw(); - runtime.broadcast(redraw_event, core::event::Status::Ignored); - - let _ = control_sender.start_send(match interface_state { - user_interface::State::Updated { - redraw_request: Some(redraw_request), - } => match redraw_request { - window::RedrawRequest::NextFrame => ControlFlow::Poll, - window::RedrawRequest::At(at) => { - ControlFlow::WaitUntil(at) - } - }, - _ => ControlFlow::Wait, - }); - - redraw_pending = false; - } - event::Event::PlatformSpecific(event::PlatformSpecific::MacOS( - event::MacOS::ReceivedUrl(url), - )) => { - use crate::core::event; - - events.push(Event::PlatformSpecific( - event::PlatformSpecific::MacOS(event::MacOS::ReceivedUrl( - url, - )), - )); - } - event::Event::UserEvent(message) => { - messages.push(message); - } - event::Event::RedrawRequested(_) => { - #[cfg(feature = "trace")] - let _ = info_span!("Application", "FRAME").entered(); - - let physical_size = state.physical_size(); - - if physical_size.width == 0 || physical_size.height == 0 { - continue; - } - debug.render_started(); - let current_viewport_version = state.viewport_version(); - - if viewport_version != current_viewport_version { - let logical_size = state.logical_size(); - - debug.layout_started(); - user_interface = ManuallyDrop::new( - ManuallyDrop::into_inner(user_interface) - .relayout(logical_size, &mut renderer), - ); - debug.layout_finished(); - - debug.draw_started(); - let new_mouse_interaction = user_interface.draw( - &mut renderer, - state.theme(), - &renderer::Style { - text_color: state.text_color(), - }, - state.cursor(), - ); - - if new_mouse_interaction != mouse_interaction { - window.set_cursor_icon(conversion::mouse_interaction( - new_mouse_interaction, - )); - - mouse_interaction = new_mouse_interaction; - } - debug.draw_finished(); - - compositor.configure_surface( - &mut surface, - physical_size.width, - physical_size.height, - ); - - viewport_version = current_viewport_version; - } - match compositor.present( &mut renderer, &mut surface, @@ -571,13 +464,81 @@ async fn run_instance<A, E, C>( state.update(&window, &window_event, &mut debug); if let Some(event) = conversion::window_event( - &window_event, + window::Id::MAIN, + window_event, state.scale_factor(), state.modifiers(), ) { events.push(event); } } + event::Event::AboutToWait => { + if events.is_empty() && messages.is_empty() { + continue; + } + + debug.event_processing_started(); + + let (interface_state, statuses) = user_interface.update( + &events, + state.cursor(), + &mut renderer, + &mut clipboard, + &mut messages, + ); + + debug.event_processing_finished(); + + for (event, status) in + events.drain(..).zip(statuses.into_iter()) + { + runtime.broadcast(event, status); + } + + if !messages.is_empty() + || matches!( + interface_state, + user_interface::State::Outdated + ) + { + let mut cache = + ManuallyDrop::into_inner(user_interface).into_cache(); + + // Update application + update( + &mut application, + &mut compositor, + &mut surface, + &mut cache, + &mut state, + &mut renderer, + &mut runtime, + &mut clipboard, + &mut should_exit, + &mut proxy, + &mut debug, + &mut messages, + &window, + ); + + user_interface = ManuallyDrop::new(build_user_interface( + &application, + cache, + &mut renderer, + state.logical_size(), + &mut debug, + )); + + if should_exit { + break; + } + } + + if !redraw_pending { + window.request_redraw(); + redraw_pending = true; + } + } _ => {} } } @@ -589,8 +550,8 @@ async fn run_instance<A, E, C>( /// Returns true if the provided event should cause an [`Application`] to /// exit. pub fn requests_exit( - event: &winit::event::WindowEvent<'_>, - _modifiers: winit::event::ModifiersState, + event: &winit::event::WindowEvent, + _modifiers: winit::keyboard::ModifiersState, ) -> bool { use winit::event::WindowEvent; @@ -598,14 +559,14 @@ pub fn requests_exit( WindowEvent::CloseRequested => true, #[cfg(target_os = "macos")] WindowEvent::KeyboardInput { - input: - winit::event::KeyboardInput { - virtual_keycode: Some(winit::event::VirtualKeyCode::Q), + event: + winit::event::KeyEvent { + logical_key: winit::keyboard::Key::Character(c), state: winit::event::ElementState::Pressed, .. }, .. - } if _modifiers.logo() => true, + } if c == "q" && _modifiers.super_key() => true, _ => false, } } @@ -622,24 +583,12 @@ pub fn build_user_interface<'a, A: Application>( where <A::Renderer as core::Renderer>::Theme: StyleSheet, { - #[cfg(feature = "trace")] - let view_span = info_span!("Application", "VIEW").entered(); - debug.view_started(); let view = application.view(); - - #[cfg(feature = "trace")] - let _ = view_span.exit(); debug.view_finished(); - #[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(); user_interface @@ -652,7 +601,7 @@ pub fn update<A: Application, C, E: Executor>( compositor: &mut C, surface: &mut C::Surface, cache: &mut user_interface::Cache, - state: &State<A>, + state: &mut State<A>, renderer: &mut A::Renderer, runtime: &mut Runtime<E, Proxy<A::Message>, A::Message>, clipboard: &mut Clipboard, @@ -666,16 +615,10 @@ pub fn update<A: Application, C, E: Executor>( <A::Renderer as core::Renderer>::Theme: StyleSheet, { for message in messages.drain(..) { - #[cfg(feature = "trace")] - let update_span = info_span!("Application", "UPDATE").entered(); - debug.log_message(&message); debug.update_started(); let command = runtime.enter(|| application.update(message)); - - #[cfg(feature = "trace")] - let _ = update_span.exit(); debug.update_finished(); run_command( @@ -695,6 +638,8 @@ pub fn update<A: Application, C, E: Executor>( ); } + state.synchronize(application, window); + let subscription = application.subscription(); runtime.track(subscription.into_recipes()); } @@ -729,6 +674,9 @@ pub fn run_command<A, C, E>( command::Action::Future(future) => { runtime.spawn(future); } + command::Action::Stream(stream) => { + runtime.run(stream); + } command::Action::Clipboard(action) => match action { clipboard::Action::Read(tag) => { let message = tag(clipboard.read()); @@ -742,20 +690,28 @@ pub fn run_command<A, C, E>( } }, command::Action::Window(action) => match action { - window::Action::Close => { + window::Action::Close(_id) => { *should_exit = true; } - window::Action::Drag => { + window::Action::Drag(_id) => { let _res = window.drag_window(); } - window::Action::Resize(size) => { - window.set_inner_size(winit::dpi::LogicalSize { - width: size.width, - height: size.height, - }); + window::Action::Spawn { .. } => { + log::warn!( + "Spawning a window is only available with \ + multi-window applications." + ); } - window::Action::FetchSize(callback) => { - let size = window.inner_size(); + window::Action::Resize(_id, size) => { + let _ = + window.request_inner_size(winit::dpi::LogicalSize { + width: size.width, + height: size.height, + }); + } + window::Action::FetchSize(_id, callback) => { + let size = + window.inner_size().to_logical(window.scale_factor()); proxy .send_event(callback(Size::new( @@ -764,29 +720,39 @@ pub fn run_command<A, C, E>( ))) .expect("Send message to event loop"); } - window::Action::Maximize(maximized) => { + window::Action::FetchMaximized(_id, callback) => { + proxy + .send_event(callback(window.is_maximized())) + .expect("Send message to event loop"); + } + window::Action::Maximize(_id, maximized) => { window.set_maximized(maximized); } - window::Action::Minimize(minimized) => { + window::Action::FetchMinimized(_id, callback) => { + proxy + .send_event(callback(window.is_minimized())) + .expect("Send message to event loop"); + } + window::Action::Minimize(_id, minimized) => { window.set_minimized(minimized); } - window::Action::Move { x, y } => { + window::Action::Move(_id, position) => { window.set_outer_position(winit::dpi::LogicalPosition { - x, - y, + x: position.x, + y: position.y, }); } - window::Action::ChangeMode(mode) => { + window::Action::ChangeMode(_id, mode) => { window.set_visible(conversion::visible(mode)); window.set_fullscreen(conversion::fullscreen( window.current_monitor(), mode, )); } - window::Action::ChangeIcon(icon) => { + window::Action::ChangeIcon(_id, icon) => { window.set_window_icon(conversion::icon(icon)); } - window::Action::FetchMode(tag) => { + window::Action::FetchMode(_id, tag) => { let mode = if window.is_visible().unwrap_or(true) { conversion::mode(window.fullscreen()) } else { @@ -797,29 +763,29 @@ pub fn run_command<A, C, E>( .send_event(tag(mode)) .expect("Send message to event loop"); } - window::Action::ToggleMaximize => { + window::Action::ToggleMaximize(_id) => { window.set_maximized(!window.is_maximized()); } - window::Action::ToggleDecorations => { + window::Action::ToggleDecorations(_id) => { window.set_decorations(!window.is_decorated()); } - window::Action::RequestUserAttention(user_attention) => { + window::Action::RequestUserAttention(_id, user_attention) => { window.request_user_attention( user_attention.map(conversion::user_attention), ); } - window::Action::GainFocus => { + window::Action::GainFocus(_id) => { window.focus_window(); } - window::Action::ChangeLevel(level) => { + window::Action::ChangeLevel(_id, level) => { window.set_window_level(conversion::window_level(level)); } - window::Action::FetchId(tag) => { + window::Action::FetchId(_id, tag) => { proxy .send_event(tag(window.id().into())) .expect("Send message to event loop"); } - window::Action::Screenshot(tag) => { + window::Action::Screenshot(_id, tag) => { let bytes = compositor.screenshot( renderer, surface, @@ -900,43 +866,3 @@ pub fn run_command<A, C, E>( } } } - -#[cfg(not(target_arch = "wasm32"))] -mod platform { - pub fn run<T, F>( - mut event_loop: winit::event_loop::EventLoop<T>, - event_handler: F, - ) -> Result<(), super::Error> - where - F: 'static - + FnMut( - winit::event::Event<'_, T>, - &winit::event_loop::EventLoopWindowTarget<T>, - &mut winit::event_loop::ControlFlow, - ), - { - use winit::platform::run_return::EventLoopExtRunReturn; - - let _ = event_loop.run_return(event_handler); - - Ok(()) - } -} - -#[cfg(target_arch = "wasm32")] -mod platform { - pub fn run<T, F>( - event_loop: winit::event_loop::EventLoop<T>, - event_handler: F, - ) -> ! - where - F: 'static - + FnMut( - winit::event::Event<'_, T>, - &winit::event_loop::EventLoopWindowTarget<T>, - &mut winit::event_loop::ControlFlow, - ), - { - event_loop.run(event_handler) - } -} diff --git a/winit/src/application/profiler.rs b/winit/src/application/profiler.rs deleted file mode 100644 index 7031507a..00000000 --- a/winit/src/application/profiler.rs +++ /dev/null @@ -1,101 +0,0 @@ -//! A simple profiler for Iced. -use std::ffi::OsStr; -use std::path::Path; -use std::time::Duration; -use tracing_subscriber::prelude::*; -use tracing_subscriber::Registry; -#[cfg(feature = "chrome-trace")] -use { - tracing_chrome::FlushGuard, - tracing_subscriber::fmt::{format::DefaultFields, FormattedFields}, -}; - -/// Profiler state. This will likely need to be updated or reworked when adding new tracing backends. -#[allow(missing_debug_implementations)] -pub struct Profiler { - #[cfg(feature = "chrome-trace")] - /// [`FlushGuard`] must not be dropped until the application scope is dropped for accurate tracing. - _guard: FlushGuard, -} - -impl Profiler { - /// Initializes the [`Profiler`]. - pub fn init() -> Self { - // Registry stores the spans & generates unique span IDs - let subscriber = Registry::default(); - - let default_path = Path::new(env!("CARGO_MANIFEST_DIR")); - let curr_exe = std::env::current_exe() - .unwrap_or_else(|_| default_path.to_path_buf()); - let out_dir = curr_exe.parent().unwrap_or(default_path).join("traces"); - - #[cfg(feature = "chrome-trace")] - let (chrome_layer, guard) = { - let mut layer = tracing_chrome::ChromeLayerBuilder::new(); - - // Optional configurable env var: CHROME_TRACE_FILE=/path/to/trace_file/file.json, - // for uploading to chrome://tracing (old) or ui.perfetto.dev (new). - if let Ok(path) = std::env::var("CHROME_TRACE_FILE") { - layer = layer.file(path); - } else if std::fs::create_dir_all(&out_dir).is_ok() { - let time = std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .unwrap_or(Duration::from_millis(0)) - .as_millis(); - - let curr_exe_name = curr_exe - .file_name() - .unwrap_or_else(|| OsStr::new("trace")) - .to_str() - .unwrap_or("trace"); - - let path = - out_dir.join(format!("{curr_exe_name}_trace_{time}.json")); - - layer = layer.file(path); - } else { - layer = layer.file(env!("CARGO_MANIFEST_DIR")) - } - - let (chrome_layer, guard) = layer - .name_fn(Box::new(|event_or_span| match event_or_span { - tracing_chrome::EventOrSpan::Event(event) => { - event.metadata().name().into() - } - tracing_chrome::EventOrSpan::Span(span) => { - if let Some(fields) = span - .extensions() - .get::<FormattedFields<DefaultFields>>() - { - format!( - "{}: {}", - span.metadata().name(), - fields.fields.as_str() - ) - } else { - span.metadata().name().into() - } - } - })) - .build(); - - (chrome_layer, guard) - }; - - let fmt_layer = tracing_subscriber::fmt::Layer::default(); - let subscriber = subscriber.with(fmt_layer); - - #[cfg(feature = "chrome-trace")] - let subscriber = subscriber.with(chrome_layer); - - // create dispatcher which will forward span events to the subscriber - // this can only be set once or will panic - tracing::subscriber::set_global_default(subscriber) - .expect("Tracer could not set the global default subscriber."); - - Profiler { - #[cfg(feature = "chrome-trace")] - _guard: guard, - } - } -} diff --git a/winit/src/application/state.rs b/winit/src/application/state.rs index e655529a..8c9b20e0 100644 --- a/winit/src/application/state.rs +++ b/winit/src/application/state.rs @@ -22,7 +22,7 @@ where viewport: Viewport, viewport_version: usize, cursor_position: Option<winit::dpi::PhysicalPosition<f64>>, - modifiers: winit::event::ModifiersState, + modifiers: winit::keyboard::ModifiersState, theme: <A::Renderer as core::Renderer>::Theme, appearance: application::Appearance, application: PhantomData<A>, @@ -54,7 +54,7 @@ where viewport, viewport_version: 0, cursor_position: None, - modifiers: winit::event::ModifiersState::default(), + modifiers: winit::keyboard::ModifiersState::default(), theme, appearance, application: PhantomData, @@ -102,7 +102,7 @@ where } /// Returns the current keyboard modifiers of the [`State`]. - pub fn modifiers(&self) -> winit::event::ModifiersState { + pub fn modifiers(&self) -> winit::keyboard::ModifiersState { self.modifiers } @@ -126,7 +126,7 @@ where pub fn update( &mut self, window: &Window, - event: &WindowEvent<'_>, + event: &WindowEvent, _debug: &mut Debug, ) { match event { @@ -142,10 +142,9 @@ where } WindowEvent::ScaleFactorChanged { scale_factor: new_scale_factor, - new_inner_size, + .. } => { - let size = - Size::new(new_inner_size.width, new_inner_size.height); + let size = self.viewport.physical_size(); self.viewport = Viewport::with_physical_size( size, @@ -164,13 +163,16 @@ where self.cursor_position = None; } WindowEvent::ModifiersChanged(new_modifiers) => { - self.modifiers = *new_modifiers; + self.modifiers = new_modifiers.state(); } #[cfg(feature = "debug")] WindowEvent::KeyboardInput { - input: - winit::event::KeyboardInput { - virtual_keycode: Some(winit::event::VirtualKeyCode::F12), + event: + winit::event::KeyEvent { + logical_key: + winit::keyboard::Key::Named( + winit::keyboard::NamedKey::F12, + ), state: winit::event::ElementState::Pressed, .. }, diff --git a/winit/src/clipboard.rs b/winit/src/clipboard.rs index f7a32868..8f5c5e63 100644 --- a/winit/src/clipboard.rs +++ b/winit/src/clipboard.rs @@ -15,7 +15,8 @@ enum State { impl Clipboard { /// Creates a new [`Clipboard`] for the given window. pub fn connect(window: &winit::window::Window) -> Clipboard { - let state = window_clipboard::Clipboard::connect(window) + #[allow(unsafe_code)] + let state = unsafe { window_clipboard::Clipboard::connect(window) } .ok() .map(State::Connected) .unwrap_or(State::Unavailable); diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index 3ecd044c..90a5d27f 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -6,14 +6,131 @@ use crate::core::keyboard; use crate::core::mouse; use crate::core::touch; use crate::core::window; -use crate::core::{Event, Point}; -use crate::Position; +use crate::core::{Event, Point, Size}; + +/// Converts some [`window::Settings`] into a `WindowBuilder` from `winit`. +pub fn window_settings( + settings: window::Settings, + title: &str, + primary_monitor: Option<winit::monitor::MonitorHandle>, + _id: Option<String>, +) -> winit::window::WindowBuilder { + let mut window_builder = winit::window::WindowBuilder::new(); + + window_builder = window_builder + .with_title(title) + .with_inner_size(winit::dpi::LogicalSize { + width: settings.size.width, + height: settings.size.height, + }) + .with_resizable(settings.resizable) + .with_enabled_buttons(if settings.resizable { + winit::window::WindowButtons::all() + } else { + winit::window::WindowButtons::CLOSE + | winit::window::WindowButtons::MINIMIZE + }) + .with_decorations(settings.decorations) + .with_transparent(settings.transparent) + .with_window_icon(settings.icon.and_then(icon)) + .with_window_level(window_level(settings.level)) + .with_visible(settings.visible); + + if let Some(position) = + position(primary_monitor.as_ref(), settings.size, settings.position) + { + window_builder = window_builder.with_position(position); + } + + if let Some(min_size) = settings.min_size { + window_builder = + window_builder.with_min_inner_size(winit::dpi::LogicalSize { + width: min_size.width, + height: min_size.height, + }); + } + + if let Some(max_size) = settings.max_size { + window_builder = + window_builder.with_max_inner_size(winit::dpi::LogicalSize { + width: max_size.width, + height: max_size.height, + }); + } + + #[cfg(any( + target_os = "dragonfly", + target_os = "freebsd", + target_os = "netbsd", + target_os = "openbsd" + ))] + { + // `with_name` is available on both `WindowBuilderExtWayland` and `WindowBuilderExtX11` and they do + // exactly the same thing. We arbitrarily choose `WindowBuilderExtWayland` here. + use ::winit::platform::wayland::WindowBuilderExtWayland; + + if let Some(id) = _id { + window_builder = window_builder.with_name(id.clone(), id); + } + } + + #[cfg(target_os = "windows")] + { + use winit::platform::windows::WindowBuilderExtWindows; + #[allow(unsafe_code)] + unsafe { + window_builder = window_builder + .with_parent_window(settings.platform_specific.parent); + } + window_builder = window_builder + .with_drag_and_drop(settings.platform_specific.drag_and_drop); + } + + #[cfg(target_os = "macos")] + { + use winit::platform::macos::WindowBuilderExtMacOS; + + window_builder = window_builder + .with_title_hidden(settings.platform_specific.title_hidden) + .with_titlebar_transparent( + settings.platform_specific.titlebar_transparent, + ) + .with_fullsize_content_view( + settings.platform_specific.fullsize_content_view, + ); + } + + #[cfg(target_os = "linux")] + { + #[cfg(feature = "x11")] + { + use winit::platform::x11::WindowBuilderExtX11; + + window_builder = window_builder.with_name( + &settings.platform_specific.application_id, + &settings.platform_specific.application_id, + ); + } + #[cfg(feature = "wayland")] + { + use winit::platform::wayland::WindowBuilderExtWayland; + + window_builder = window_builder.with_name( + &settings.platform_specific.application_id, + &settings.platform_specific.application_id, + ); + } + } + + window_builder +} /// Converts a winit window event into an iced event. pub fn window_event( - event: &winit::event::WindowEvent<'_>, + id: window::Id, + event: winit::event::WindowEvent, scale_factor: f64, - modifiers: winit::event::ModifiersState, + modifiers: winit::keyboard::ModifiersState, ) -> Option<Event> { use winit::event::WindowEvent; @@ -21,21 +138,16 @@ pub fn window_event( WindowEvent::Resized(new_size) => { let logical_size = new_size.to_logical(scale_factor); - Some(Event::Window(window::Event::Resized { - width: logical_size.width, - height: logical_size.height, - })) - } - WindowEvent::ScaleFactorChanged { new_inner_size, .. } => { - let logical_size = new_inner_size.to_logical(scale_factor); - - Some(Event::Window(window::Event::Resized { - width: logical_size.width, - height: logical_size.height, - })) + Some(Event::Window( + id, + window::Event::Resized { + width: logical_size.width, + height: logical_size.height, + }, + )) } WindowEvent::CloseRequested => { - Some(Event::Window(window::Event::CloseRequested)) + Some(Event::Window(id, window::Event::CloseRequested)) } WindowEvent::CursorMoved { position, .. } => { let position = position.to_logical::<f64>(scale_factor); @@ -51,7 +163,7 @@ pub fn window_event( Some(Event::Mouse(mouse::Event::CursorLeft)) } WindowEvent::MouseInput { button, state, .. } => { - let button = mouse_button(*button); + let button = mouse_button(button); Some(Event::Mouse(match state { winit::event::ElementState::Pressed => { @@ -66,8 +178,8 @@ pub fn window_event( winit::event::MouseScrollDelta::LineDelta(delta_x, delta_y) => { Some(Event::Mouse(mouse::Event::WheelScrolled { delta: mouse::ScrollDelta::Lines { - x: *delta_x, - y: *delta_y, + x: delta_x, + y: delta_y, }, })) } @@ -80,61 +192,81 @@ pub fn window_event( })) } }, - WindowEvent::ReceivedCharacter(c) if !is_private_use_character(*c) => { - Some(Event::Keyboard(keyboard::Event::CharacterReceived(*c))) - } WindowEvent::KeyboardInput { - input: - winit::event::KeyboardInput { - virtual_keycode: Some(virtual_keycode), + event: + winit::event::KeyEvent { + logical_key, state, + text, + location, .. }, .. } => Some(Event::Keyboard({ - let key_code = key_code(*virtual_keycode); + let key = key(logical_key); let modifiers = self::modifiers(modifiers); + let location = match location { + winit::keyboard::KeyLocation::Standard => { + keyboard::Location::Standard + } + winit::keyboard::KeyLocation::Left => keyboard::Location::Left, + winit::keyboard::KeyLocation::Right => { + keyboard::Location::Right + } + winit::keyboard::KeyLocation::Numpad => { + keyboard::Location::Numpad + } + }; + match state { winit::event::ElementState::Pressed => { keyboard::Event::KeyPressed { - key_code, + key, modifiers, + location, + text, } } winit::event::ElementState::Released => { keyboard::Event::KeyReleased { - key_code, + key, modifiers, + location, } } } })), - WindowEvent::ModifiersChanged(new_modifiers) => Some(Event::Keyboard( - keyboard::Event::ModifiersChanged(self::modifiers(*new_modifiers)), + WindowEvent::ModifiersChanged(new_modifiers) => { + Some(Event::Keyboard(keyboard::Event::ModifiersChanged( + self::modifiers(new_modifiers.state()), + ))) + } + WindowEvent::Focused(focused) => Some(Event::Window( + id, + if focused { + window::Event::Focused + } else { + window::Event::Unfocused + }, )), - WindowEvent::Focused(focused) => Some(Event::Window(if *focused { - window::Event::Focused - } else { - window::Event::Unfocused - })), WindowEvent::HoveredFile(path) => { - Some(Event::Window(window::Event::FileHovered(path.clone()))) + Some(Event::Window(id, window::Event::FileHovered(path.clone()))) } WindowEvent::DroppedFile(path) => { - Some(Event::Window(window::Event::FileDropped(path.clone()))) + Some(Event::Window(id, window::Event::FileDropped(path.clone()))) } WindowEvent::HoveredFileCancelled => { - Some(Event::Window(window::Event::FilesHoveredLeft)) + Some(Event::Window(id, window::Event::FilesHoveredLeft)) } WindowEvent::Touch(touch) => { - Some(Event::Touch(touch_event(*touch, scale_factor))) + Some(Event::Touch(touch_event(touch, scale_factor))) } WindowEvent::Moved(position) => { let winit::dpi::LogicalPosition { x, y } = position.to_logical(scale_factor); - Some(Event::Window(window::Event::Moved { x, y })) + Some(Event::Window(id, window::Event::Moved { x, y })) } _ => None, } @@ -153,23 +285,23 @@ pub fn window_level(level: window::Level) -> winit::window::WindowLevel { } } -/// Converts a [`Position`] to a [`winit`] logical position for a given monitor. +/// Converts a [`window::Position`] to a [`winit`] logical position for a given monitor. /// /// [`winit`]: https://github.com/rust-windowing/winit pub fn position( monitor: Option<&winit::monitor::MonitorHandle>, - (width, height): (u32, u32), - position: Position, + size: Size, + position: window::Position, ) -> Option<winit::dpi::Position> { match position { - Position::Default => None, - Position::Specific(x, y) => { + window::Position::Default => None, + window::Position::Specific(position) => { Some(winit::dpi::Position::Logical(winit::dpi::LogicalPosition { - x: f64::from(x), - y: f64::from(y), + x: f64::from(position.x), + y: f64::from(position.y), })) } - Position::Centered => { + window::Position::Centered => { if let Some(monitor) = monitor { let start = monitor.position(); @@ -178,8 +310,8 @@ pub fn position( let centered: winit::dpi::PhysicalPosition<i32> = winit::dpi::LogicalPosition { - x: (resolution.width - f64::from(width)) / 2.0, - y: (resolution.height - f64::from(height)) / 2.0, + x: (resolution.width - f64::from(size.width)) / 2.0, + y: (resolution.height - f64::from(size.height)) / 2.0, } .to_physical(monitor.scale_factor()); @@ -239,7 +371,7 @@ pub fn mouse_interaction( match interaction { Interaction::Idle => winit::window::CursorIcon::Default, - Interaction::Pointer => winit::window::CursorIcon::Hand, + Interaction::Pointer => winit::window::CursorIcon::Pointer, Interaction::Working => winit::window::CursorIcon::Progress, Interaction::Grab => winit::window::CursorIcon::Grab, Interaction::Grabbing => winit::window::CursorIcon::Grabbing, @@ -262,6 +394,8 @@ pub fn mouse_button(mouse_button: winit::event::MouseButton) -> mouse::Button { winit::event::MouseButton::Left => mouse::Button::Left, winit::event::MouseButton::Right => mouse::Button::Right, winit::event::MouseButton::Middle => mouse::Button::Middle, + winit::event::MouseButton::Back => mouse::Button::Back, + winit::event::MouseButton::Forward => mouse::Button::Forward, winit::event::MouseButton::Other(other) => mouse::Button::Other(other), } } @@ -272,14 +406,14 @@ pub fn mouse_button(mouse_button: winit::event::MouseButton) -> mouse::Button { /// [`winit`]: https://github.com/rust-windowing/winit /// [`iced`]: https://github.com/iced-rs/iced/tree/0.10 pub fn modifiers( - modifiers: winit::event::ModifiersState, + modifiers: winit::keyboard::ModifiersState, ) -> keyboard::Modifiers { let mut result = keyboard::Modifiers::empty(); - result.set(keyboard::Modifiers::SHIFT, modifiers.shift()); - result.set(keyboard::Modifiers::CTRL, modifiers.ctrl()); - result.set(keyboard::Modifiers::ALT, modifiers.alt()); - result.set(keyboard::Modifiers::LOGO, modifiers.logo()); + result.set(keyboard::Modifiers::SHIFT, modifiers.shift_key()); + result.set(keyboard::Modifiers::CTRL, modifiers.control_key()); + result.set(keyboard::Modifiers::ALT, modifiers.alt_key()); + result.set(keyboard::Modifiers::LOGO, modifiers.super_key()); result } @@ -329,179 +463,328 @@ pub fn touch_event( /// /// [`winit`]: https://github.com/rust-windowing/winit /// [`iced`]: https://github.com/iced-rs/iced/tree/0.10 -pub fn key_code( - virtual_keycode: winit::event::VirtualKeyCode, -) -> keyboard::KeyCode { - use keyboard::KeyCode; - - match virtual_keycode { - winit::event::VirtualKeyCode::Key1 => KeyCode::Key1, - winit::event::VirtualKeyCode::Key2 => KeyCode::Key2, - winit::event::VirtualKeyCode::Key3 => KeyCode::Key3, - winit::event::VirtualKeyCode::Key4 => KeyCode::Key4, - winit::event::VirtualKeyCode::Key5 => KeyCode::Key5, - winit::event::VirtualKeyCode::Key6 => KeyCode::Key6, - winit::event::VirtualKeyCode::Key7 => KeyCode::Key7, - winit::event::VirtualKeyCode::Key8 => KeyCode::Key8, - winit::event::VirtualKeyCode::Key9 => KeyCode::Key9, - winit::event::VirtualKeyCode::Key0 => KeyCode::Key0, - winit::event::VirtualKeyCode::A => KeyCode::A, - winit::event::VirtualKeyCode::B => KeyCode::B, - winit::event::VirtualKeyCode::C => KeyCode::C, - winit::event::VirtualKeyCode::D => KeyCode::D, - winit::event::VirtualKeyCode::E => KeyCode::E, - winit::event::VirtualKeyCode::F => KeyCode::F, - winit::event::VirtualKeyCode::G => KeyCode::G, - winit::event::VirtualKeyCode::H => KeyCode::H, - winit::event::VirtualKeyCode::I => KeyCode::I, - winit::event::VirtualKeyCode::J => KeyCode::J, - winit::event::VirtualKeyCode::K => KeyCode::K, - winit::event::VirtualKeyCode::L => KeyCode::L, - winit::event::VirtualKeyCode::M => KeyCode::M, - winit::event::VirtualKeyCode::N => KeyCode::N, - winit::event::VirtualKeyCode::O => KeyCode::O, - winit::event::VirtualKeyCode::P => KeyCode::P, - winit::event::VirtualKeyCode::Q => KeyCode::Q, - winit::event::VirtualKeyCode::R => KeyCode::R, - winit::event::VirtualKeyCode::S => KeyCode::S, - winit::event::VirtualKeyCode::T => KeyCode::T, - winit::event::VirtualKeyCode::U => KeyCode::U, - winit::event::VirtualKeyCode::V => KeyCode::V, - winit::event::VirtualKeyCode::W => KeyCode::W, - winit::event::VirtualKeyCode::X => KeyCode::X, - winit::event::VirtualKeyCode::Y => KeyCode::Y, - winit::event::VirtualKeyCode::Z => KeyCode::Z, - winit::event::VirtualKeyCode::Escape => KeyCode::Escape, - winit::event::VirtualKeyCode::F1 => KeyCode::F1, - winit::event::VirtualKeyCode::F2 => KeyCode::F2, - winit::event::VirtualKeyCode::F3 => KeyCode::F3, - winit::event::VirtualKeyCode::F4 => KeyCode::F4, - winit::event::VirtualKeyCode::F5 => KeyCode::F5, - winit::event::VirtualKeyCode::F6 => KeyCode::F6, - winit::event::VirtualKeyCode::F7 => KeyCode::F7, - winit::event::VirtualKeyCode::F8 => KeyCode::F8, - winit::event::VirtualKeyCode::F9 => KeyCode::F9, - winit::event::VirtualKeyCode::F10 => KeyCode::F10, - winit::event::VirtualKeyCode::F11 => KeyCode::F11, - winit::event::VirtualKeyCode::F12 => KeyCode::F12, - winit::event::VirtualKeyCode::F13 => KeyCode::F13, - winit::event::VirtualKeyCode::F14 => KeyCode::F14, - winit::event::VirtualKeyCode::F15 => KeyCode::F15, - winit::event::VirtualKeyCode::F16 => KeyCode::F16, - winit::event::VirtualKeyCode::F17 => KeyCode::F17, - winit::event::VirtualKeyCode::F18 => KeyCode::F18, - winit::event::VirtualKeyCode::F19 => KeyCode::F19, - winit::event::VirtualKeyCode::F20 => KeyCode::F20, - winit::event::VirtualKeyCode::F21 => KeyCode::F21, - winit::event::VirtualKeyCode::F22 => KeyCode::F22, - winit::event::VirtualKeyCode::F23 => KeyCode::F23, - winit::event::VirtualKeyCode::F24 => KeyCode::F24, - winit::event::VirtualKeyCode::Snapshot => KeyCode::Snapshot, - winit::event::VirtualKeyCode::Scroll => KeyCode::Scroll, - winit::event::VirtualKeyCode::Pause => KeyCode::Pause, - winit::event::VirtualKeyCode::Insert => KeyCode::Insert, - winit::event::VirtualKeyCode::Home => KeyCode::Home, - winit::event::VirtualKeyCode::Delete => KeyCode::Delete, - winit::event::VirtualKeyCode::End => KeyCode::End, - winit::event::VirtualKeyCode::PageDown => KeyCode::PageDown, - winit::event::VirtualKeyCode::PageUp => KeyCode::PageUp, - winit::event::VirtualKeyCode::Left => KeyCode::Left, - winit::event::VirtualKeyCode::Up => KeyCode::Up, - winit::event::VirtualKeyCode::Right => KeyCode::Right, - winit::event::VirtualKeyCode::Down => KeyCode::Down, - winit::event::VirtualKeyCode::Back => KeyCode::Backspace, - winit::event::VirtualKeyCode::Return => KeyCode::Enter, - winit::event::VirtualKeyCode::Space => KeyCode::Space, - winit::event::VirtualKeyCode::Compose => KeyCode::Compose, - winit::event::VirtualKeyCode::Caret => KeyCode::Caret, - winit::event::VirtualKeyCode::Numlock => KeyCode::Numlock, - winit::event::VirtualKeyCode::Numpad0 => KeyCode::Numpad0, - winit::event::VirtualKeyCode::Numpad1 => KeyCode::Numpad1, - winit::event::VirtualKeyCode::Numpad2 => KeyCode::Numpad2, - winit::event::VirtualKeyCode::Numpad3 => KeyCode::Numpad3, - winit::event::VirtualKeyCode::Numpad4 => KeyCode::Numpad4, - winit::event::VirtualKeyCode::Numpad5 => KeyCode::Numpad5, - winit::event::VirtualKeyCode::Numpad6 => KeyCode::Numpad6, - winit::event::VirtualKeyCode::Numpad7 => KeyCode::Numpad7, - winit::event::VirtualKeyCode::Numpad8 => KeyCode::Numpad8, - winit::event::VirtualKeyCode::Numpad9 => KeyCode::Numpad9, - winit::event::VirtualKeyCode::AbntC1 => KeyCode::AbntC1, - winit::event::VirtualKeyCode::AbntC2 => KeyCode::AbntC2, - winit::event::VirtualKeyCode::NumpadAdd => KeyCode::NumpadAdd, - winit::event::VirtualKeyCode::Plus => KeyCode::Plus, - winit::event::VirtualKeyCode::Apostrophe => KeyCode::Apostrophe, - winit::event::VirtualKeyCode::Apps => KeyCode::Apps, - winit::event::VirtualKeyCode::At => KeyCode::At, - winit::event::VirtualKeyCode::Ax => KeyCode::Ax, - winit::event::VirtualKeyCode::Backslash => KeyCode::Backslash, - winit::event::VirtualKeyCode::Calculator => KeyCode::Calculator, - winit::event::VirtualKeyCode::Capital => KeyCode::Capital, - winit::event::VirtualKeyCode::Colon => KeyCode::Colon, - winit::event::VirtualKeyCode::Comma => KeyCode::Comma, - winit::event::VirtualKeyCode::Convert => KeyCode::Convert, - winit::event::VirtualKeyCode::NumpadDecimal => KeyCode::NumpadDecimal, - winit::event::VirtualKeyCode::NumpadDivide => KeyCode::NumpadDivide, - winit::event::VirtualKeyCode::Equals => KeyCode::Equals, - winit::event::VirtualKeyCode::Grave => KeyCode::Grave, - winit::event::VirtualKeyCode::Kana => KeyCode::Kana, - winit::event::VirtualKeyCode::Kanji => KeyCode::Kanji, - winit::event::VirtualKeyCode::LAlt => KeyCode::LAlt, - winit::event::VirtualKeyCode::LBracket => KeyCode::LBracket, - winit::event::VirtualKeyCode::LControl => KeyCode::LControl, - winit::event::VirtualKeyCode::LShift => KeyCode::LShift, - winit::event::VirtualKeyCode::LWin => KeyCode::LWin, - winit::event::VirtualKeyCode::Mail => KeyCode::Mail, - winit::event::VirtualKeyCode::MediaSelect => KeyCode::MediaSelect, - winit::event::VirtualKeyCode::MediaStop => KeyCode::MediaStop, - winit::event::VirtualKeyCode::Minus => KeyCode::Minus, - winit::event::VirtualKeyCode::NumpadMultiply => KeyCode::NumpadMultiply, - winit::event::VirtualKeyCode::Mute => KeyCode::Mute, - winit::event::VirtualKeyCode::MyComputer => KeyCode::MyComputer, - winit::event::VirtualKeyCode::NavigateForward => { - KeyCode::NavigateForward - } - winit::event::VirtualKeyCode::NavigateBackward => { - KeyCode::NavigateBackward +pub fn key(key: winit::keyboard::Key) -> keyboard::Key { + use keyboard::key::Named; + use winit::keyboard::NamedKey; + + match key { + winit::keyboard::Key::Character(c) => keyboard::Key::Character(c), + winit::keyboard::Key::Named(named_key) => { + keyboard::Key::Named(match named_key { + NamedKey::Alt => Named::Alt, + NamedKey::AltGraph => Named::AltGraph, + NamedKey::CapsLock => Named::CapsLock, + NamedKey::Control => Named::Control, + NamedKey::Fn => Named::Fn, + NamedKey::FnLock => Named::FnLock, + NamedKey::NumLock => Named::NumLock, + NamedKey::ScrollLock => Named::ScrollLock, + NamedKey::Shift => Named::Shift, + NamedKey::Symbol => Named::Symbol, + NamedKey::SymbolLock => Named::SymbolLock, + NamedKey::Meta => Named::Meta, + NamedKey::Hyper => Named::Hyper, + NamedKey::Super => Named::Super, + NamedKey::Enter => Named::Enter, + NamedKey::Tab => Named::Tab, + NamedKey::Space => Named::Space, + NamedKey::ArrowDown => Named::ArrowDown, + NamedKey::ArrowLeft => Named::ArrowLeft, + NamedKey::ArrowRight => Named::ArrowRight, + NamedKey::ArrowUp => Named::ArrowUp, + NamedKey::End => Named::End, + NamedKey::Home => Named::Home, + NamedKey::PageDown => Named::PageDown, + NamedKey::PageUp => Named::PageUp, + NamedKey::Backspace => Named::Backspace, + NamedKey::Clear => Named::Clear, + NamedKey::Copy => Named::Copy, + NamedKey::CrSel => Named::CrSel, + NamedKey::Cut => Named::Cut, + NamedKey::Delete => Named::Delete, + NamedKey::EraseEof => Named::EraseEof, + NamedKey::ExSel => Named::ExSel, + NamedKey::Insert => Named::Insert, + NamedKey::Paste => Named::Paste, + NamedKey::Redo => Named::Redo, + NamedKey::Undo => Named::Undo, + NamedKey::Accept => Named::Accept, + NamedKey::Again => Named::Again, + NamedKey::Attn => Named::Attn, + NamedKey::Cancel => Named::Cancel, + NamedKey::ContextMenu => Named::ContextMenu, + NamedKey::Escape => Named::Escape, + NamedKey::Execute => Named::Execute, + NamedKey::Find => Named::Find, + NamedKey::Help => Named::Help, + NamedKey::Pause => Named::Pause, + NamedKey::Play => Named::Play, + NamedKey::Props => Named::Props, + NamedKey::Select => Named::Select, + NamedKey::ZoomIn => Named::ZoomIn, + NamedKey::ZoomOut => Named::ZoomOut, + NamedKey::BrightnessDown => Named::BrightnessDown, + NamedKey::BrightnessUp => Named::BrightnessUp, + NamedKey::Eject => Named::Eject, + NamedKey::LogOff => Named::LogOff, + NamedKey::Power => Named::Power, + NamedKey::PowerOff => Named::PowerOff, + NamedKey::PrintScreen => Named::PrintScreen, + NamedKey::Hibernate => Named::Hibernate, + NamedKey::Standby => Named::Standby, + NamedKey::WakeUp => Named::WakeUp, + NamedKey::AllCandidates => Named::AllCandidates, + NamedKey::Alphanumeric => Named::Alphanumeric, + NamedKey::CodeInput => Named::CodeInput, + NamedKey::Compose => Named::Compose, + NamedKey::Convert => Named::Convert, + NamedKey::FinalMode => Named::FinalMode, + NamedKey::GroupFirst => Named::GroupFirst, + NamedKey::GroupLast => Named::GroupLast, + NamedKey::GroupNext => Named::GroupNext, + NamedKey::GroupPrevious => Named::GroupPrevious, + NamedKey::ModeChange => Named::ModeChange, + NamedKey::NextCandidate => Named::NextCandidate, + NamedKey::NonConvert => Named::NonConvert, + NamedKey::PreviousCandidate => Named::PreviousCandidate, + NamedKey::Process => Named::Process, + NamedKey::SingleCandidate => Named::SingleCandidate, + NamedKey::HangulMode => Named::HangulMode, + NamedKey::HanjaMode => Named::HanjaMode, + NamedKey::JunjaMode => Named::JunjaMode, + NamedKey::Eisu => Named::Eisu, + NamedKey::Hankaku => Named::Hankaku, + NamedKey::Hiragana => Named::Hiragana, + NamedKey::HiraganaKatakana => Named::HiraganaKatakana, + NamedKey::KanaMode => Named::KanaMode, + NamedKey::KanjiMode => Named::KanjiMode, + NamedKey::Katakana => Named::Katakana, + NamedKey::Romaji => Named::Romaji, + NamedKey::Zenkaku => Named::Zenkaku, + NamedKey::ZenkakuHankaku => Named::ZenkakuHankaku, + NamedKey::Soft1 => Named::Soft1, + NamedKey::Soft2 => Named::Soft2, + NamedKey::Soft3 => Named::Soft3, + NamedKey::Soft4 => Named::Soft4, + NamedKey::ChannelDown => Named::ChannelDown, + NamedKey::ChannelUp => Named::ChannelUp, + NamedKey::Close => Named::Close, + NamedKey::MailForward => Named::MailForward, + NamedKey::MailReply => Named::MailReply, + NamedKey::MailSend => Named::MailSend, + NamedKey::MediaClose => Named::MediaClose, + NamedKey::MediaFastForward => Named::MediaFastForward, + NamedKey::MediaPause => Named::MediaPause, + NamedKey::MediaPlay => Named::MediaPlay, + NamedKey::MediaPlayPause => Named::MediaPlayPause, + NamedKey::MediaRecord => Named::MediaRecord, + NamedKey::MediaRewind => Named::MediaRewind, + NamedKey::MediaStop => Named::MediaStop, + NamedKey::MediaTrackNext => Named::MediaTrackNext, + NamedKey::MediaTrackPrevious => Named::MediaTrackPrevious, + NamedKey::New => Named::New, + NamedKey::Open => Named::Open, + NamedKey::Print => Named::Print, + NamedKey::Save => Named::Save, + NamedKey::SpellCheck => Named::SpellCheck, + NamedKey::Key11 => Named::Key11, + NamedKey::Key12 => Named::Key12, + NamedKey::AudioBalanceLeft => Named::AudioBalanceLeft, + NamedKey::AudioBalanceRight => Named::AudioBalanceRight, + NamedKey::AudioBassBoostDown => Named::AudioBassBoostDown, + NamedKey::AudioBassBoostToggle => Named::AudioBassBoostToggle, + NamedKey::AudioBassBoostUp => Named::AudioBassBoostUp, + NamedKey::AudioFaderFront => Named::AudioFaderFront, + NamedKey::AudioFaderRear => Named::AudioFaderRear, + NamedKey::AudioSurroundModeNext => Named::AudioSurroundModeNext, + NamedKey::AudioTrebleDown => Named::AudioTrebleDown, + NamedKey::AudioTrebleUp => Named::AudioTrebleUp, + NamedKey::AudioVolumeDown => Named::AudioVolumeDown, + NamedKey::AudioVolumeUp => Named::AudioVolumeUp, + NamedKey::AudioVolumeMute => Named::AudioVolumeMute, + NamedKey::MicrophoneToggle => Named::MicrophoneToggle, + NamedKey::MicrophoneVolumeDown => Named::MicrophoneVolumeDown, + NamedKey::MicrophoneVolumeUp => Named::MicrophoneVolumeUp, + NamedKey::MicrophoneVolumeMute => Named::MicrophoneVolumeMute, + NamedKey::SpeechCorrectionList => Named::SpeechCorrectionList, + NamedKey::SpeechInputToggle => Named::SpeechInputToggle, + NamedKey::LaunchApplication1 => Named::LaunchApplication1, + NamedKey::LaunchApplication2 => Named::LaunchApplication2, + NamedKey::LaunchCalendar => Named::LaunchCalendar, + NamedKey::LaunchContacts => Named::LaunchContacts, + NamedKey::LaunchMail => Named::LaunchMail, + NamedKey::LaunchMediaPlayer => Named::LaunchMediaPlayer, + NamedKey::LaunchMusicPlayer => Named::LaunchMusicPlayer, + NamedKey::LaunchPhone => Named::LaunchPhone, + NamedKey::LaunchScreenSaver => Named::LaunchScreenSaver, + NamedKey::LaunchSpreadsheet => Named::LaunchSpreadsheet, + NamedKey::LaunchWebBrowser => Named::LaunchWebBrowser, + NamedKey::LaunchWebCam => Named::LaunchWebCam, + NamedKey::LaunchWordProcessor => Named::LaunchWordProcessor, + NamedKey::BrowserBack => Named::BrowserBack, + NamedKey::BrowserFavorites => Named::BrowserFavorites, + NamedKey::BrowserForward => Named::BrowserForward, + NamedKey::BrowserHome => Named::BrowserHome, + NamedKey::BrowserRefresh => Named::BrowserRefresh, + NamedKey::BrowserSearch => Named::BrowserSearch, + NamedKey::BrowserStop => Named::BrowserStop, + NamedKey::AppSwitch => Named::AppSwitch, + NamedKey::Call => Named::Call, + NamedKey::Camera => Named::Camera, + NamedKey::CameraFocus => Named::CameraFocus, + NamedKey::EndCall => Named::EndCall, + NamedKey::GoBack => Named::GoBack, + NamedKey::GoHome => Named::GoHome, + NamedKey::HeadsetHook => Named::HeadsetHook, + NamedKey::LastNumberRedial => Named::LastNumberRedial, + NamedKey::Notification => Named::Notification, + NamedKey::MannerMode => Named::MannerMode, + NamedKey::VoiceDial => Named::VoiceDial, + NamedKey::TV => Named::TV, + NamedKey::TV3DMode => Named::TV3DMode, + NamedKey::TVAntennaCable => Named::TVAntennaCable, + NamedKey::TVAudioDescription => Named::TVAudioDescription, + NamedKey::TVAudioDescriptionMixDown => { + Named::TVAudioDescriptionMixDown + } + NamedKey::TVAudioDescriptionMixUp => { + Named::TVAudioDescriptionMixUp + } + NamedKey::TVContentsMenu => Named::TVContentsMenu, + NamedKey::TVDataService => Named::TVDataService, + NamedKey::TVInput => Named::TVInput, + NamedKey::TVInputComponent1 => Named::TVInputComponent1, + NamedKey::TVInputComponent2 => Named::TVInputComponent2, + NamedKey::TVInputComposite1 => Named::TVInputComposite1, + NamedKey::TVInputComposite2 => Named::TVInputComposite2, + NamedKey::TVInputHDMI1 => Named::TVInputHDMI1, + NamedKey::TVInputHDMI2 => Named::TVInputHDMI2, + NamedKey::TVInputHDMI3 => Named::TVInputHDMI3, + NamedKey::TVInputHDMI4 => Named::TVInputHDMI4, + NamedKey::TVInputVGA1 => Named::TVInputVGA1, + NamedKey::TVMediaContext => Named::TVMediaContext, + NamedKey::TVNetwork => Named::TVNetwork, + NamedKey::TVNumberEntry => Named::TVNumberEntry, + NamedKey::TVPower => Named::TVPower, + NamedKey::TVRadioService => Named::TVRadioService, + NamedKey::TVSatellite => Named::TVSatellite, + NamedKey::TVSatelliteBS => Named::TVSatelliteBS, + NamedKey::TVSatelliteCS => Named::TVSatelliteCS, + NamedKey::TVSatelliteToggle => Named::TVSatelliteToggle, + NamedKey::TVTerrestrialAnalog => Named::TVTerrestrialAnalog, + NamedKey::TVTerrestrialDigital => Named::TVTerrestrialDigital, + NamedKey::TVTimer => Named::TVTimer, + NamedKey::AVRInput => Named::AVRInput, + NamedKey::AVRPower => Named::AVRPower, + NamedKey::ColorF0Red => Named::ColorF0Red, + NamedKey::ColorF1Green => Named::ColorF1Green, + NamedKey::ColorF2Yellow => Named::ColorF2Yellow, + NamedKey::ColorF3Blue => Named::ColorF3Blue, + NamedKey::ColorF4Grey => Named::ColorF4Grey, + NamedKey::ColorF5Brown => Named::ColorF5Brown, + NamedKey::ClosedCaptionToggle => Named::ClosedCaptionToggle, + NamedKey::Dimmer => Named::Dimmer, + NamedKey::DisplaySwap => Named::DisplaySwap, + NamedKey::DVR => Named::DVR, + NamedKey::Exit => Named::Exit, + NamedKey::FavoriteClear0 => Named::FavoriteClear0, + NamedKey::FavoriteClear1 => Named::FavoriteClear1, + NamedKey::FavoriteClear2 => Named::FavoriteClear2, + NamedKey::FavoriteClear3 => Named::FavoriteClear3, + NamedKey::FavoriteRecall0 => Named::FavoriteRecall0, + NamedKey::FavoriteRecall1 => Named::FavoriteRecall1, + NamedKey::FavoriteRecall2 => Named::FavoriteRecall2, + NamedKey::FavoriteRecall3 => Named::FavoriteRecall3, + NamedKey::FavoriteStore0 => Named::FavoriteStore0, + NamedKey::FavoriteStore1 => Named::FavoriteStore1, + NamedKey::FavoriteStore2 => Named::FavoriteStore2, + NamedKey::FavoriteStore3 => Named::FavoriteStore3, + NamedKey::Guide => Named::Guide, + NamedKey::GuideNextDay => Named::GuideNextDay, + NamedKey::GuidePreviousDay => Named::GuidePreviousDay, + NamedKey::Info => Named::Info, + NamedKey::InstantReplay => Named::InstantReplay, + NamedKey::Link => Named::Link, + NamedKey::ListProgram => Named::ListProgram, + NamedKey::LiveContent => Named::LiveContent, + NamedKey::Lock => Named::Lock, + NamedKey::MediaApps => Named::MediaApps, + NamedKey::MediaAudioTrack => Named::MediaAudioTrack, + NamedKey::MediaLast => Named::MediaLast, + NamedKey::MediaSkipBackward => Named::MediaSkipBackward, + NamedKey::MediaSkipForward => Named::MediaSkipForward, + NamedKey::MediaStepBackward => Named::MediaStepBackward, + NamedKey::MediaStepForward => Named::MediaStepForward, + NamedKey::MediaTopMenu => Named::MediaTopMenu, + NamedKey::NavigateIn => Named::NavigateIn, + NamedKey::NavigateNext => Named::NavigateNext, + NamedKey::NavigateOut => Named::NavigateOut, + NamedKey::NavigatePrevious => Named::NavigatePrevious, + NamedKey::NextFavoriteChannel => Named::NextFavoriteChannel, + NamedKey::NextUserProfile => Named::NextUserProfile, + NamedKey::OnDemand => Named::OnDemand, + NamedKey::Pairing => Named::Pairing, + NamedKey::PinPDown => Named::PinPDown, + NamedKey::PinPMove => Named::PinPMove, + NamedKey::PinPToggle => Named::PinPToggle, + NamedKey::PinPUp => Named::PinPUp, + NamedKey::PlaySpeedDown => Named::PlaySpeedDown, + NamedKey::PlaySpeedReset => Named::PlaySpeedReset, + NamedKey::PlaySpeedUp => Named::PlaySpeedUp, + NamedKey::RandomToggle => Named::RandomToggle, + NamedKey::RcLowBattery => Named::RcLowBattery, + NamedKey::RecordSpeedNext => Named::RecordSpeedNext, + NamedKey::RfBypass => Named::RfBypass, + NamedKey::ScanChannelsToggle => Named::ScanChannelsToggle, + NamedKey::ScreenModeNext => Named::ScreenModeNext, + NamedKey::Settings => Named::Settings, + NamedKey::SplitScreenToggle => Named::SplitScreenToggle, + NamedKey::STBInput => Named::STBInput, + NamedKey::STBPower => Named::STBPower, + NamedKey::Subtitle => Named::Subtitle, + NamedKey::Teletext => Named::Teletext, + NamedKey::VideoModeNext => Named::VideoModeNext, + NamedKey::Wink => Named::Wink, + NamedKey::ZoomToggle => Named::ZoomToggle, + NamedKey::F1 => Named::F1, + NamedKey::F2 => Named::F2, + NamedKey::F3 => Named::F3, + NamedKey::F4 => Named::F4, + NamedKey::F5 => Named::F5, + NamedKey::F6 => Named::F6, + NamedKey::F7 => Named::F7, + NamedKey::F8 => Named::F8, + NamedKey::F9 => Named::F9, + NamedKey::F10 => Named::F10, + NamedKey::F11 => Named::F11, + NamedKey::F12 => Named::F12, + NamedKey::F13 => Named::F13, + NamedKey::F14 => Named::F14, + NamedKey::F15 => Named::F15, + NamedKey::F16 => Named::F16, + NamedKey::F17 => Named::F17, + NamedKey::F18 => Named::F18, + NamedKey::F19 => Named::F19, + NamedKey::F20 => Named::F20, + NamedKey::F21 => Named::F21, + NamedKey::F22 => Named::F22, + NamedKey::F23 => Named::F23, + NamedKey::F24 => Named::F24, + NamedKey::F25 => Named::F25, + NamedKey::F26 => Named::F26, + NamedKey::F27 => Named::F27, + NamedKey::F28 => Named::F28, + NamedKey::F29 => Named::F29, + NamedKey::F30 => Named::F30, + NamedKey::F31 => Named::F31, + NamedKey::F32 => Named::F32, + NamedKey::F33 => Named::F33, + NamedKey::F34 => Named::F34, + NamedKey::F35 => Named::F35, + _ => return keyboard::Key::Unidentified, + }) } - winit::event::VirtualKeyCode::NextTrack => KeyCode::NextTrack, - winit::event::VirtualKeyCode::NoConvert => KeyCode::NoConvert, - winit::event::VirtualKeyCode::NumpadComma => KeyCode::NumpadComma, - winit::event::VirtualKeyCode::NumpadEnter => KeyCode::NumpadEnter, - winit::event::VirtualKeyCode::NumpadEquals => KeyCode::NumpadEquals, - winit::event::VirtualKeyCode::OEM102 => KeyCode::OEM102, - winit::event::VirtualKeyCode::Period => KeyCode::Period, - winit::event::VirtualKeyCode::PlayPause => KeyCode::PlayPause, - winit::event::VirtualKeyCode::Power => KeyCode::Power, - winit::event::VirtualKeyCode::PrevTrack => KeyCode::PrevTrack, - winit::event::VirtualKeyCode::RAlt => KeyCode::RAlt, - winit::event::VirtualKeyCode::RBracket => KeyCode::RBracket, - winit::event::VirtualKeyCode::RControl => KeyCode::RControl, - winit::event::VirtualKeyCode::RShift => KeyCode::RShift, - winit::event::VirtualKeyCode::RWin => KeyCode::RWin, - winit::event::VirtualKeyCode::Semicolon => KeyCode::Semicolon, - winit::event::VirtualKeyCode::Slash => KeyCode::Slash, - winit::event::VirtualKeyCode::Sleep => KeyCode::Sleep, - winit::event::VirtualKeyCode::Stop => KeyCode::Stop, - winit::event::VirtualKeyCode::NumpadSubtract => KeyCode::NumpadSubtract, - winit::event::VirtualKeyCode::Sysrq => KeyCode::Sysrq, - winit::event::VirtualKeyCode::Tab => KeyCode::Tab, - winit::event::VirtualKeyCode::Underline => KeyCode::Underline, - winit::event::VirtualKeyCode::Unlabeled => KeyCode::Unlabeled, - winit::event::VirtualKeyCode::VolumeDown => KeyCode::VolumeDown, - winit::event::VirtualKeyCode::VolumeUp => KeyCode::VolumeUp, - winit::event::VirtualKeyCode::Wake => KeyCode::Wake, - winit::event::VirtualKeyCode::WebBack => KeyCode::WebBack, - winit::event::VirtualKeyCode::WebFavorites => KeyCode::WebFavorites, - winit::event::VirtualKeyCode::WebForward => KeyCode::WebForward, - winit::event::VirtualKeyCode::WebHome => KeyCode::WebHome, - winit::event::VirtualKeyCode::WebRefresh => KeyCode::WebRefresh, - winit::event::VirtualKeyCode::WebSearch => KeyCode::WebSearch, - winit::event::VirtualKeyCode::WebStop => KeyCode::WebStop, - winit::event::VirtualKeyCode::Yen => KeyCode::Yen, - winit::event::VirtualKeyCode::Copy => KeyCode::Copy, - winit::event::VirtualKeyCode::Paste => KeyCode::Paste, - winit::event::VirtualKeyCode::Cut => KeyCode::Cut, - winit::event::VirtualKeyCode::Asterisk => KeyCode::Asterisk, + _ => keyboard::Key::Unidentified, } } @@ -529,13 +812,3 @@ pub fn icon(icon: window::Icon) -> Option<winit::window::Icon> { winit::window::Icon::from_rgba(pixels, size.width, size.height).ok() } - -// As defined in: http://www.unicode.org/faq/private_use.html -pub(crate) fn is_private_use_character(c: char) -> bool { - matches!( - c, - '\u{E000}'..='\u{F8FF}' - | '\u{F0000}'..='\u{FFFFD}' - | '\u{100000}'..='\u{10FFFD}' - ) -} diff --git a/winit/src/lib.rs b/winit/src/lib.rs index 95b55bb9..948576a2 100644 --- a/winit/src/lib.rs +++ b/winit/src/lib.rs @@ -33,6 +33,9 @@ pub use iced_runtime::futures; pub use iced_style as style; pub use winit; +#[cfg(feature = "multi-window")] +pub mod multi_window; + #[cfg(feature = "application")] pub mod application; pub mod clipboard; @@ -43,17 +46,11 @@ pub mod settings; pub mod system; mod error; -mod position; mod proxy; #[cfg(feature = "application")] pub use application::Application; -#[cfg(feature = "trace")] -pub use application::Profiler; pub use clipboard::Clipboard; pub use error::Error; -pub use position::Position; pub use proxy::Proxy; pub use settings::Settings; - -pub use iced_graphics::Viewport; diff --git a/winit/src/multi_window.rs b/winit/src/multi_window.rs new file mode 100644 index 00000000..3f0ba056 --- /dev/null +++ b/winit/src/multi_window.rs @@ -0,0 +1,1189 @@ +//! Create interactive, native cross-platform applications for WGPU. +mod state; +mod window_manager; + +pub use state::State; + +use crate::conversion; +use crate::core; +use crate::core::renderer; +use crate::core::widget::operation; +use crate::core::window; +use crate::core::Size; +use crate::futures::futures::channel::mpsc; +use crate::futures::futures::{task, Future, StreamExt}; +use crate::futures::{Executor, Runtime, Subscription}; +use crate::graphics::{compositor, Compositor}; +use crate::multi_window::window_manager::WindowManager; +use crate::runtime::command::{self, Command}; +use crate::runtime::multi_window::Program; +use crate::runtime::user_interface::{self, UserInterface}; +use crate::runtime::Debug; +use crate::style::application::StyleSheet; +use crate::{Clipboard, Error, Proxy, Settings}; + +use std::collections::HashMap; +use std::mem::ManuallyDrop; +use std::sync::Arc; +use std::time::Instant; + +/// An interactive, native, cross-platform, multi-windowed application. +/// +/// 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. +/// +/// An [`Application`] can execute asynchronous actions by returning a +/// [`Command`] in some of its methods. +/// +/// When using an [`Application`] with the `debug` feature enabled, a debug view +/// can be toggled by pressing `F12`. +pub trait Application: Program +where + <Self::Renderer as core::Renderer>::Theme: StyleSheet, +{ + /// The data needed to initialize your [`Application`]. + type Flags; + + /// Initializes the [`Application`] with the flags provided to + /// [`run`] as part of the [`Settings`]. + /// + /// Here is where you should return the initial state of your app. + /// + /// Additionally, you can return a [`Command`] if you need to perform some + /// async action in the background on startup. This is useful if you want to + /// 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 the [`Application`]. + /// + /// This title can be dynamic! The runtime will automatically update the + /// title of your application when necessary. + fn title(&self, window: window::Id) -> String; + + /// Returns the current `Theme` of the [`Application`]. + fn theme( + &self, + window: window::Id, + ) -> <Self::Renderer as core::Renderer>::Theme; + + /// Returns the `Style` variation of the `Theme`. + fn style( + &self, + ) -> <<Self::Renderer as core::Renderer>::Theme as StyleSheet>::Style { + Default::default() + } + + /// Returns the event `Subscription` for the current state of the + /// application. + /// + /// The messages produced by the `Subscription` will be handled by + /// [`update`](#tymethod.update). + /// + /// A `Subscription` will be kept alive as long as you keep returning it! + /// + /// By default, it returns an empty subscription. + fn subscription(&self) -> Subscription<Self::Message> { + Subscription::none() + } + + /// Returns the scale factor of the window of the [`Application`]. + /// + /// It can be used to dynamically control the size of the UI at runtime + /// (i.e. zooming). + /// + /// For instance, a scale factor of `2.0` will make widgets twice as big, + /// while a scale factor of `0.5` will shrink them to half their size. + /// + /// By default, it returns `1.0`. + #[allow(unused_variables)] + fn scale_factor(&self, window: window::Id) -> f64 { + 1.0 + } +} + +/// Runs an [`Application`] with an executor, compositor, and the provided +/// settings. +pub fn run<A, E, C>( + settings: Settings<A::Flags>, + compositor_settings: C::Settings, +) -> Result<(), Error> +where + A: Application + 'static, + E: Executor + 'static, + C: Compositor<Renderer = A::Renderer> + 'static, + <A::Renderer as core::Renderer>::Theme: StyleSheet, +{ + use winit::event_loop::EventLoopBuilder; + + let mut debug = Debug::new(); + debug.startup_started(); + + let event_loop = EventLoopBuilder::with_user_event() + .build() + .expect("Create event loop"); + + let proxy = event_loop.create_proxy(); + + let runtime = { + let proxy = Proxy::new(event_loop.create_proxy()); + let executor = E::new().map_err(Error::ExecutorCreationFailed)?; + + Runtime::new(executor, proxy) + }; + + let (application, init_command) = { + let flags = settings.flags; + + runtime.enter(|| A::new(flags)) + }; + + let should_main_be_visible = settings.window.visible; + let exit_on_close_request = settings.window.exit_on_close_request; + + let builder = conversion::window_settings( + settings.window, + &application.title(window::Id::MAIN), + event_loop.primary_monitor(), + settings.id, + ) + .with_visible(false); + + log::info!("Window builder: {:#?}", builder); + + let main_window = Arc::new( + builder + .build(&event_loop) + .map_err(Error::WindowCreationFailed)?, + ); + + #[cfg(target_arch = "wasm32")] + { + use winit::platform::web::WindowExtWebSys; + + let canvas = main_window.canvas(); + + let window = web_sys::window().unwrap(); + let document = window.document().unwrap(); + let body = document.body().unwrap(); + + let target = target.and_then(|target| { + body.query_selector(&format!("#{}", target)) + .ok() + .unwrap_or(None) + }); + + match target { + Some(node) => { + let _ = node + .replace_with_with_node_1(&canvas) + .expect(&format!("Could not replace #{}", node.id())); + } + None => { + let _ = body + .append_child(&canvas) + .expect("Append canvas to HTML body"); + } + }; + } + + let mut compositor = C::new(compositor_settings, main_window.clone())?; + + let mut window_manager = WindowManager::new(); + let _ = window_manager.insert( + window::Id::MAIN, + main_window, + &application, + &mut compositor, + exit_on_close_request, + ); + + let (mut event_sender, event_receiver) = mpsc::unbounded(); + let (control_sender, mut control_receiver) = mpsc::unbounded(); + + let mut instance = Box::pin(run_instance::<A, E, C>( + application, + compositor, + runtime, + proxy, + debug, + event_receiver, + control_sender, + init_command, + window_manager, + should_main_be_visible, + )); + + let mut context = task::Context::from_waker(task::noop_waker_ref()); + + let _ = event_loop.run(move |event, event_loop| { + if event_loop.exiting() { + return; + } + + event_sender + .start_send(Event::EventLoopAwakened(event)) + .expect("Send event"); + + loop { + let poll = instance.as_mut().poll(&mut context); + + match poll { + task::Poll::Pending => match control_receiver.try_next() { + Ok(Some(control)) => match control { + Control::ChangeFlow(flow) => { + use winit::event_loop::ControlFlow; + + match (event_loop.control_flow(), flow) { + ( + ControlFlow::WaitUntil(current), + ControlFlow::WaitUntil(new), + ) if new < current => {} + ( + ControlFlow::WaitUntil(target), + ControlFlow::Wait, + ) if target > Instant::now() => {} + _ => { + event_loop.set_control_flow(flow); + } + } + } + Control::CreateWindow { + id, + settings, + title, + monitor, + } => { + let exit_on_close_request = + settings.exit_on_close_request; + + let window = conversion::window_settings( + settings, &title, monitor, None, + ) + .build(event_loop) + .expect("Failed to build window"); + + event_sender + .start_send(Event::WindowCreated { + id, + window, + exit_on_close_request, + }) + .expect("Send event"); + } + Control::Exit => { + event_loop.exit(); + } + }, + _ => { + break; + } + }, + task::Poll::Ready(_) => { + event_loop.exit(); + break; + } + }; + } + }); + + Ok(()) +} + +enum Event<Message: 'static> { + WindowCreated { + id: window::Id, + window: winit::window::Window, + exit_on_close_request: bool, + }, + EventLoopAwakened(winit::event::Event<Message>), +} + +enum Control { + ChangeFlow(winit::event_loop::ControlFlow), + Exit, + CreateWindow { + id: window::Id, + settings: window::Settings, + title: String, + monitor: Option<winit::monitor::MonitorHandle>, + }, +} + +async fn run_instance<A, E, C>( + mut application: A, + mut compositor: C, + mut runtime: Runtime<E, Proxy<A::Message>, A::Message>, + mut proxy: winit::event_loop::EventLoopProxy<A::Message>, + mut debug: Debug, + mut event_receiver: mpsc::UnboundedReceiver<Event<A::Message>>, + mut control_sender: mpsc::UnboundedSender<Control>, + init_command: Command<A::Message>, + mut window_manager: WindowManager<A, C>, + should_main_window_be_visible: bool, +) where + A: Application + 'static, + E: Executor + 'static, + C: Compositor<Renderer = A::Renderer> + 'static, + <A::Renderer as core::Renderer>::Theme: StyleSheet, +{ + use winit::event; + use winit::event_loop::ControlFlow; + + let main_window = window_manager + .get_mut(window::Id::MAIN) + .expect("Get main window"); + + if should_main_window_be_visible { + main_window.raw.set_visible(true); + } + + let mut clipboard = Clipboard::connect(&main_window.raw); + let mut events = { + vec![( + Some(window::Id::MAIN), + core::Event::Window( + window::Id::MAIN, + window::Event::Opened { + position: main_window.position(), + size: main_window.size(), + }, + ), + )] + }; + + let mut ui_caches = HashMap::new(); + let mut user_interfaces = ManuallyDrop::new(build_user_interfaces( + &application, + &mut debug, + &mut window_manager, + HashMap::from_iter([( + window::Id::MAIN, + user_interface::Cache::default(), + )]), + )); + + run_command( + &application, + &mut compositor, + init_command, + &mut runtime, + &mut clipboard, + &mut control_sender, + &mut proxy, + &mut debug, + &mut window_manager, + &mut ui_caches, + ); + + runtime.track(application.subscription().into_recipes()); + + let mut messages = Vec::new(); + + debug.startup_finished(); + + 'main: while let Some(event) = event_receiver.next().await { + match event { + Event::WindowCreated { + id, + window, + exit_on_close_request, + } => { + let window = window_manager.insert( + id, + Arc::new(window), + &application, + &mut compositor, + exit_on_close_request, + ); + + let logical_size = window.state.logical_size(); + + let _ = user_interfaces.insert( + id, + build_user_interface( + &application, + user_interface::Cache::default(), + &mut window.renderer, + logical_size, + &mut debug, + id, + ), + ); + let _ = ui_caches.insert(id, user_interface::Cache::default()); + + events.push(( + Some(id), + core::Event::Window( + id, + window::Event::Opened { + position: window.position(), + size: window.size(), + }, + ), + )); + } + Event::EventLoopAwakened(event) => { + match event { + event::Event::NewEvents( + event::StartCause::Init + | event::StartCause::ResumeTimeReached { .. }, + ) => { + for (_id, window) in window_manager.iter_mut() { + // TODO once widgets can request to be redrawn, we can avoid always requesting a + // redraw + window.raw.request_redraw(); + } + } + event::Event::PlatformSpecific( + event::PlatformSpecific::MacOS( + event::MacOS::ReceivedUrl(url), + ), + ) => { + use crate::core::event; + + events.push(( + None, + event::Event::PlatformSpecific( + event::PlatformSpecific::MacOS( + event::MacOS::ReceivedUrl(url), + ), + ), + )); + } + event::Event::UserEvent(message) => { + messages.push(message); + } + event::Event::WindowEvent { + window_id: id, + event: event::WindowEvent::RedrawRequested, + .. + } => { + let Some((id, window)) = + window_manager.get_mut_alias(id) + else { + continue; + }; + + // TODO: Avoid redrawing all the time by forcing widgets to + // request redraws on state changes + // + // Then, we can use the `interface_state` here to decide if a redraw + // is needed right away, or simply wait until a specific time. + let redraw_event = core::Event::Window( + id, + window::Event::RedrawRequested(Instant::now()), + ); + + let cursor = window.state.cursor(); + + let ui = user_interfaces + .get_mut(&id) + .expect("Get user interface"); + + let (ui_state, _) = ui.update( + &[redraw_event.clone()], + cursor, + &mut window.renderer, + &mut clipboard, + &mut messages, + ); + + debug.draw_started(); + let new_mouse_interaction = ui.draw( + &mut window.renderer, + window.state.theme(), + &renderer::Style { + text_color: window.state.text_color(), + }, + cursor, + ); + debug.draw_finished(); + + if new_mouse_interaction != window.mouse_interaction { + window.raw.set_cursor_icon( + conversion::mouse_interaction( + new_mouse_interaction, + ), + ); + + window.mouse_interaction = new_mouse_interaction; + } + + runtime.broadcast( + redraw_event.clone(), + core::event::Status::Ignored, + ); + + let _ = control_sender.start_send(Control::ChangeFlow( + match ui_state { + user_interface::State::Updated { + redraw_request: Some(redraw_request), + } => match redraw_request { + window::RedrawRequest::NextFrame => { + window.raw.request_redraw(); + + ControlFlow::Wait + } + window::RedrawRequest::At(at) => { + ControlFlow::WaitUntil(at) + } + }, + _ => ControlFlow::Wait, + }, + )); + + let physical_size = window.state.physical_size(); + + if physical_size.width == 0 || physical_size.height == 0 + { + continue; + } + + if window.viewport_version + != window.state.viewport_version() + { + let logical_size = window.state.logical_size(); + + debug.layout_started(); + let ui = user_interfaces + .remove(&id) + .expect("Remove user interface"); + + let _ = user_interfaces.insert( + id, + ui.relayout(logical_size, &mut window.renderer), + ); + debug.layout_finished(); + + debug.draw_started(); + let new_mouse_interaction = user_interfaces + .get_mut(&id) + .expect("Get user interface") + .draw( + &mut window.renderer, + window.state.theme(), + &renderer::Style { + text_color: window.state.text_color(), + }, + window.state.cursor(), + ); + debug.draw_finished(); + + if new_mouse_interaction != window.mouse_interaction + { + window.raw.set_cursor_icon( + conversion::mouse_interaction( + new_mouse_interaction, + ), + ); + + window.mouse_interaction = + new_mouse_interaction; + } + + compositor.configure_surface( + &mut window.surface, + physical_size.width, + physical_size.height, + ); + + window.viewport_version = + window.state.viewport_version(); + } + + debug.render_started(); + match compositor.present( + &mut window.renderer, + &mut window.surface, + window.state.viewport(), + window.state.background_color(), + &debug.overlay(), + ) { + Ok(()) => { + debug.render_finished(); + + // TODO: Handle animations! + // Maybe we can use `ControlFlow::WaitUntil` for this. + } + Err(error) => match error { + // This is an unrecoverable error. + compositor::SurfaceError::OutOfMemory => { + panic!("{:?}", error); + } + _ => { + debug.render_finished(); + + log::error!( + "Error {error:?} when \ + presenting surface." + ); + + // Try rendering all windows again next frame. + for (_id, window) in + window_manager.iter_mut() + { + window.raw.request_redraw(); + } + } + }, + } + } + event::Event::WindowEvent { + event: window_event, + window_id, + } => { + let Some((id, window)) = + window_manager.get_mut_alias(window_id) + else { + continue; + }; + + if matches!( + window_event, + winit::event::WindowEvent::CloseRequested + ) && window.exit_on_close_request + { + let _ = window_manager.remove(id); + let _ = user_interfaces.remove(&id); + let _ = ui_caches.remove(&id); + + events.push(( + None, + core::Event::Window(id, window::Event::Closed), + )); + + if window_manager.is_empty() { + break 'main; + } + } else { + window.state.update( + &window.raw, + &window_event, + &mut debug, + ); + + if let Some(event) = conversion::window_event( + id, + window_event, + window.state.scale_factor(), + window.state.modifiers(), + ) { + events.push((Some(id), event)); + } + } + } + event::Event::AboutToWait => { + if events.is_empty() && messages.is_empty() { + continue; + } + + debug.event_processing_started(); + let mut uis_stale = false; + + for (id, window) in window_manager.iter_mut() { + let mut window_events = vec![]; + + events.retain(|(window_id, event)| { + if *window_id == Some(id) || window_id.is_none() + { + window_events.push(event.clone()); + false + } else { + true + } + }); + + if window_events.is_empty() && messages.is_empty() { + continue; + } + + let (ui_state, statuses) = user_interfaces + .get_mut(&id) + .expect("Get user interface") + .update( + &window_events, + window.state.cursor(), + &mut window.renderer, + &mut clipboard, + &mut messages, + ); + + window.raw.request_redraw(); + + if !uis_stale { + uis_stale = matches!( + ui_state, + user_interface::State::Outdated + ); + } + + for (event, status) in window_events + .into_iter() + .zip(statuses.into_iter()) + { + runtime.broadcast(event, status); + } + } + + debug.event_processing_finished(); + + // TODO mw application update returns which window IDs to update + if !messages.is_empty() || uis_stale { + let mut cached_interfaces: HashMap< + window::Id, + user_interface::Cache, + > = ManuallyDrop::into_inner(user_interfaces) + .drain() + .map(|(id, ui)| (id, ui.into_cache())) + .collect(); + + // Update application + update( + &mut application, + &mut compositor, + &mut runtime, + &mut clipboard, + &mut control_sender, + &mut proxy, + &mut debug, + &mut messages, + &mut window_manager, + &mut cached_interfaces, + ); + + // we must synchronize all window states with application state after an + // application update since we don't know what changed + for (id, window) in window_manager.iter_mut() { + window.state.synchronize( + &application, + id, + &window.raw, + ); + + // TODO once widgets can request to be redrawn, we can avoid always requesting a + // redraw + window.raw.request_redraw(); + } + + // rebuild UIs with the synchronized states + user_interfaces = + ManuallyDrop::new(build_user_interfaces( + &application, + &mut debug, + &mut window_manager, + cached_interfaces, + )); + } + } + _ => {} + } + } + } + } + + let _ = ManuallyDrop::into_inner(user_interfaces); +} + +/// Builds a window's [`UserInterface`] for the [`Application`]. +fn build_user_interface<'a, A: Application>( + application: &'a A, + cache: user_interface::Cache, + renderer: &mut A::Renderer, + size: Size, + debug: &mut Debug, + id: window::Id, +) -> UserInterface<'a, A::Message, A::Renderer> +where + <A::Renderer as core::Renderer>::Theme: StyleSheet, +{ + debug.view_started(); + let view = application.view(id); + debug.view_finished(); + + debug.layout_started(); + let user_interface = UserInterface::build(view, size, cache, renderer); + debug.layout_finished(); + + user_interface +} + +/// Updates a multi-window [`Application`] by feeding it messages, spawning any +/// resulting [`Command`], and tracking its [`Subscription`]. +fn update<A: Application, C, E: Executor>( + application: &mut A, + compositor: &mut C, + runtime: &mut Runtime<E, Proxy<A::Message>, A::Message>, + clipboard: &mut Clipboard, + control_sender: &mut mpsc::UnboundedSender<Control>, + proxy: &mut winit::event_loop::EventLoopProxy<A::Message>, + debug: &mut Debug, + messages: &mut Vec<A::Message>, + window_manager: &mut WindowManager<A, C>, + ui_caches: &mut HashMap<window::Id, user_interface::Cache>, +) where + C: Compositor<Renderer = A::Renderer> + 'static, + <A::Renderer as core::Renderer>::Theme: StyleSheet, +{ + for message in messages.drain(..) { + debug.log_message(&message); + debug.update_started(); + + let command = runtime.enter(|| application.update(message)); + debug.update_finished(); + + run_command( + application, + compositor, + command, + runtime, + clipboard, + control_sender, + proxy, + debug, + window_manager, + ui_caches, + ); + } + + let subscription = application.subscription(); + runtime.track(subscription.into_recipes()); +} + +/// Runs the actions of a [`Command`]. +fn run_command<A, C, E>( + application: &A, + compositor: &mut C, + command: Command<A::Message>, + runtime: &mut Runtime<E, Proxy<A::Message>, A::Message>, + clipboard: &mut Clipboard, + control_sender: &mut mpsc::UnboundedSender<Control>, + proxy: &mut winit::event_loop::EventLoopProxy<A::Message>, + debug: &mut Debug, + window_manager: &mut WindowManager<A, C>, + ui_caches: &mut HashMap<window::Id, user_interface::Cache>, +) where + A: Application, + E: Executor, + C: Compositor<Renderer = A::Renderer> + 'static, + <A::Renderer as core::Renderer>::Theme: StyleSheet, +{ + use crate::runtime::clipboard; + use crate::runtime::system; + use crate::runtime::window; + + for action in command.actions() { + match action { + command::Action::Future(future) => { + runtime.spawn(Box::pin(future)); + } + command::Action::Stream(stream) => { + runtime.run(Box::pin(stream)); + } + command::Action::Clipboard(action) => match action { + clipboard::Action::Read(tag) => { + let message = tag(clipboard.read()); + + proxy + .send_event(message) + .expect("Send message to event loop"); + } + clipboard::Action::Write(contents) => { + clipboard.write(contents); + } + }, + command::Action::Window(action) => match action { + window::Action::Spawn(id, settings) => { + let monitor = window_manager.last_monitor(); + + control_sender + .start_send(Control::CreateWindow { + id, + settings, + title: application.title(id), + monitor, + }) + .expect("Send control action"); + } + window::Action::Close(id) => { + let _ = window_manager.remove(id); + let _ = ui_caches.remove(&id); + + if window_manager.is_empty() { + control_sender + .start_send(Control::Exit) + .expect("Send control action"); + } + } + window::Action::Drag(id) => { + if let Some(window) = window_manager.get_mut(id) { + let _ = window.raw.drag_window(); + } + } + window::Action::Resize(id, size) => { + if let Some(window) = window_manager.get_mut(id) { + let _ = window.raw.request_inner_size( + winit::dpi::LogicalSize { + width: size.width, + height: size.height, + }, + ); + } + } + window::Action::FetchSize(id, callback) => { + if let Some(window) = window_manager.get_mut(id) { + let size = window + .raw + .inner_size() + .to_logical(window.raw.scale_factor()); + + proxy + .send_event(callback(Size::new( + size.width, + size.height, + ))) + .expect("Send message to event loop"); + } + } + window::Action::FetchMaximized(id, callback) => { + if let Some(window) = window_manager.get_mut(id) { + proxy + .send_event(callback(window.raw.is_maximized())) + .expect("Send message to event loop"); + } + } + window::Action::Maximize(id, maximized) => { + if let Some(window) = window_manager.get_mut(id) { + window.raw.set_maximized(maximized); + } + } + window::Action::FetchMinimized(id, callback) => { + if let Some(window) = window_manager.get_mut(id) { + proxy + .send_event(callback(window.raw.is_minimized())) + .expect("Send message to event loop"); + } + } + window::Action::Minimize(id, minimized) => { + if let Some(window) = window_manager.get_mut(id) { + window.raw.set_minimized(minimized); + } + } + window::Action::Move(id, position) => { + if let Some(window) = window_manager.get_mut(id) { + window.raw.set_outer_position( + winit::dpi::LogicalPosition { + x: position.x, + y: position.y, + }, + ); + } + } + window::Action::ChangeMode(id, mode) => { + if let Some(window) = window_manager.get_mut(id) { + window.raw.set_visible(conversion::visible(mode)); + window.raw.set_fullscreen(conversion::fullscreen( + window.raw.current_monitor(), + mode, + )); + } + } + window::Action::ChangeIcon(id, icon) => { + if let Some(window) = window_manager.get_mut(id) { + window.raw.set_window_icon(conversion::icon(icon)); + } + } + window::Action::FetchMode(id, tag) => { + if let Some(window) = window_manager.get_mut(id) { + let mode = if window.raw.is_visible().unwrap_or(true) { + conversion::mode(window.raw.fullscreen()) + } else { + core::window::Mode::Hidden + }; + + proxy + .send_event(tag(mode)) + .expect("Event loop doesn't exist."); + } + } + window::Action::ToggleMaximize(id) => { + if let Some(window) = window_manager.get_mut(id) { + window.raw.set_maximized(!window.raw.is_maximized()); + } + } + window::Action::ToggleDecorations(id) => { + if let Some(window) = window_manager.get_mut(id) { + window.raw.set_decorations(!window.raw.is_decorated()); + } + } + window::Action::RequestUserAttention(id, attention_type) => { + if let Some(window) = window_manager.get_mut(id) { + window.raw.request_user_attention( + attention_type.map(conversion::user_attention), + ); + } + } + window::Action::GainFocus(id) => { + if let Some(window) = window_manager.get_mut(id) { + window.raw.focus_window(); + } + } + window::Action::ChangeLevel(id, level) => { + if let Some(window) = window_manager.get_mut(id) { + window + .raw + .set_window_level(conversion::window_level(level)); + } + } + window::Action::FetchId(id, tag) => { + if let Some(window) = window_manager.get_mut(id) { + proxy + .send_event(tag(window.raw.id().into())) + .expect("Event loop doesn't exist."); + } + } + window::Action::Screenshot(id, tag) => { + if let Some(window) = window_manager.get_mut(id) { + let bytes = compositor.screenshot( + &mut window.renderer, + &mut window.surface, + window.state.viewport(), + window.state.background_color(), + &debug.overlay(), + ); + + proxy + .send_event(tag(window::Screenshot::new( + bytes, + window.state.physical_size(), + ))) + .expect("Event loop doesn't exist."); + } + } + }, + command::Action::System(action) => match action { + system::Action::QueryInformation(_tag) => { + #[cfg(feature = "system")] + { + let graphics_info = compositor.fetch_information(); + let proxy = proxy.clone(); + + let _ = std::thread::spawn(move || { + let information = + crate::system::information(graphics_info); + + let message = _tag(information); + + proxy + .send_event(message) + .expect("Event loop doesn't exist."); + }); + } + } + }, + command::Action::Widget(action) => { + let mut current_operation = Some(action); + + let mut uis = build_user_interfaces( + application, + debug, + window_manager, + std::mem::take(ui_caches), + ); + + 'operate: while let Some(mut operation) = + current_operation.take() + { + for (id, ui) in uis.iter_mut() { + if let Some(window) = window_manager.get_mut(*id) { + ui.operate(&window.renderer, operation.as_mut()); + + match operation.finish() { + operation::Outcome::None => {} + operation::Outcome::Some(message) => { + proxy + .send_event(message) + .expect("Event loop doesn't exist."); + + // operation completed, don't need to try to operate on rest of UIs + break 'operate; + } + operation::Outcome::Chain(next) => { + current_operation = Some(next); + } + } + } + } + } + + *ui_caches = + uis.drain().map(|(id, ui)| (id, ui.into_cache())).collect(); + } + command::Action::LoadFont { bytes, tagger } => { + use crate::core::text::Renderer; + + // TODO change this once we change each renderer to having a single backend reference.. :pain: + // TODO: Error handling (?) + for (_, window) in window_manager.iter_mut() { + window.renderer.load_font(bytes.clone()); + } + + proxy + .send_event(tagger(Ok(()))) + .expect("Send message to event loop"); + } + } + } +} + +/// Build the user interface for every window. +pub fn build_user_interfaces<'a, A: Application, C: Compositor>( + application: &'a A, + debug: &mut Debug, + window_manager: &mut WindowManager<A, C>, + mut cached_user_interfaces: HashMap<window::Id, user_interface::Cache>, +) -> HashMap<window::Id, UserInterface<'a, A::Message, A::Renderer>> +where + <A::Renderer as core::Renderer>::Theme: StyleSheet, + C: Compositor<Renderer = A::Renderer>, +{ + cached_user_interfaces + .drain() + .filter_map(|(id, cache)| { + let window = window_manager.get_mut(id)?; + + Some(( + id, + build_user_interface( + application, + cache, + &mut window.renderer, + window.state.logical_size(), + debug, + id, + ), + )) + }) + .collect() +} + +/// Returns true if the provided event should cause an [`Application`] to +/// exit. +pub fn user_force_quit( + event: &winit::event::WindowEvent, + _modifiers: winit::keyboard::ModifiersState, +) -> bool { + match event { + #[cfg(target_os = "macos")] + winit::event::WindowEvent::KeyboardInput { + event: + winit::event::KeyEvent { + logical_key: winit::keyboard::Key::Character(c), + state: winit::event::ElementState::Pressed, + .. + }, + .. + } if c == "q" && _modifiers.super_key() => true, + _ => false, + } +} diff --git a/winit/src/multi_window/state.rs b/winit/src/multi_window/state.rs new file mode 100644 index 00000000..235771f4 --- /dev/null +++ b/winit/src/multi_window/state.rs @@ -0,0 +1,242 @@ +use crate::conversion; +use crate::core; +use crate::core::{mouse, window}; +use crate::core::{Color, Size}; +use crate::graphics::Viewport; +use crate::multi_window::Application; +use crate::style::application; +use std::fmt::{Debug, Formatter}; + +use iced_style::application::StyleSheet; +use winit::event::{Touch, WindowEvent}; +use winit::window::Window; + +/// The state of a multi-windowed [`Application`]. +pub struct State<A: Application> +where + <A::Renderer as core::Renderer>::Theme: application::StyleSheet, +{ + title: String, + scale_factor: f64, + viewport: Viewport, + viewport_version: u64, + cursor_position: Option<winit::dpi::PhysicalPosition<f64>>, + modifiers: winit::keyboard::ModifiersState, + theme: <A::Renderer as core::Renderer>::Theme, + appearance: application::Appearance, +} + +impl<A: Application> Debug for State<A> +where + <A::Renderer as core::Renderer>::Theme: application::StyleSheet, +{ + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + f.debug_struct("multi_window::State") + .field("title", &self.title) + .field("scale_factor", &self.scale_factor) + .field("viewport", &self.viewport) + .field("viewport_version", &self.viewport_version) + .field("cursor_position", &self.cursor_position) + .field("appearance", &self.appearance) + .finish() + } +} + +impl<A: Application> State<A> +where + <A::Renderer as core::Renderer>::Theme: application::StyleSheet, +{ + /// Creates a new [`State`] for the provided [`Application`]'s `window`. + pub fn new( + application: &A, + window_id: window::Id, + window: &Window, + ) -> Self { + let title = application.title(window_id); + let scale_factor = application.scale_factor(window_id); + let theme = application.theme(window_id); + let appearance = theme.appearance(&application.style()); + + let viewport = { + let physical_size = window.inner_size(); + + Viewport::with_physical_size( + Size::new(physical_size.width, physical_size.height), + window.scale_factor() * scale_factor, + ) + }; + + Self { + title, + scale_factor, + viewport, + viewport_version: 0, + cursor_position: None, + modifiers: winit::keyboard::ModifiersState::default(), + theme, + appearance, + } + } + + /// Returns the current [`Viewport`] of the [`State`]. + pub fn viewport(&self) -> &Viewport { + &self.viewport + } + + /// Returns the version of the [`Viewport`] of the [`State`]. + /// + /// The version is incremented every time the [`Viewport`] changes. + pub fn viewport_version(&self) -> u64 { + self.viewport_version + } + + /// Returns the physical [`Size`] of the [`Viewport`] of the [`State`]. + pub fn physical_size(&self) -> Size<u32> { + self.viewport.physical_size() + } + + /// Returns the logical [`Size`] of the [`Viewport`] of the [`State`]. + pub fn logical_size(&self) -> Size<f32> { + self.viewport.logical_size() + } + + /// Returns the current scale factor of the [`Viewport`] of the [`State`]. + pub fn scale_factor(&self) -> f64 { + self.viewport.scale_factor() + } + + /// Returns the current cursor position of the [`State`]. + pub fn cursor(&self) -> mouse::Cursor { + self.cursor_position + .map(|cursor_position| { + conversion::cursor_position( + cursor_position, + self.viewport.scale_factor(), + ) + }) + .map(mouse::Cursor::Available) + .unwrap_or(mouse::Cursor::Unavailable) + } + + /// Returns the current keyboard modifiers of the [`State`]. + pub fn modifiers(&self) -> winit::keyboard::ModifiersState { + self.modifiers + } + + /// Returns the current theme of the [`State`]. + pub fn theme(&self) -> &<A::Renderer as core::Renderer>::Theme { + &self.theme + } + + /// Returns the current background [`Color`] of the [`State`]. + pub fn background_color(&self) -> Color { + self.appearance.background_color + } + + /// Returns the current text [`Color`] of the [`State`]. + pub fn text_color(&self) -> Color { + self.appearance.text_color + } + + /// Processes the provided window event and updates the [`State`] accordingly. + pub fn update( + &mut self, + window: &Window, + event: &WindowEvent, + _debug: &mut crate::runtime::Debug, + ) { + match event { + WindowEvent::Resized(new_size) => { + let size = Size::new(new_size.width, new_size.height); + + self.viewport = Viewport::with_physical_size( + size, + window.scale_factor() * self.scale_factor, + ); + + self.viewport_version = self.viewport_version.wrapping_add(1); + } + WindowEvent::ScaleFactorChanged { + scale_factor: new_scale_factor, + .. + } => { + let size = self.viewport.physical_size(); + + self.viewport = Viewport::with_physical_size( + size, + new_scale_factor * self.scale_factor, + ); + + self.viewport_version = self.viewport_version.wrapping_add(1); + } + WindowEvent::CursorMoved { position, .. } + | WindowEvent::Touch(Touch { + location: position, .. + }) => { + self.cursor_position = Some(*position); + } + WindowEvent::CursorLeft { .. } => { + self.cursor_position = None; + } + WindowEvent::ModifiersChanged(new_modifiers) => { + self.modifiers = new_modifiers.state(); + } + #[cfg(feature = "debug")] + WindowEvent::KeyboardInput { + event: + winit::event::KeyEvent { + logical_key: + winit::keyboard::Key::Named( + winit::keyboard::NamedKey::F12, + ), + state: winit::event::ElementState::Pressed, + .. + }, + .. + } => _debug.toggle(), + _ => {} + } + } + + /// Synchronizes the [`State`] with its [`Application`] and its respective + /// window. + /// + /// Normally, an [`Application`] should be synchronized with its [`State`] + /// and window after calling [`State::update`]. + pub fn synchronize( + &mut self, + application: &A, + window_id: window::Id, + window: &Window, + ) { + // Update window title + let new_title = application.title(window_id); + + if self.title != new_title { + window.set_title(&new_title); + self.title = new_title; + } + + // Update scale factor and size + let new_scale_factor = application.scale_factor(window_id); + let new_size = window.inner_size(); + let current_size = self.viewport.physical_size(); + + if self.scale_factor != new_scale_factor + || (current_size.width, current_size.height) + != (new_size.width, new_size.height) + { + self.viewport = Viewport::with_physical_size( + Size::new(new_size.width, new_size.height), + window.scale_factor() * new_scale_factor, + ); + self.viewport_version = self.viewport_version.wrapping_add(1); + + self.scale_factor = new_scale_factor; + } + + // Update theme and appearance + self.theme = application.theme(window_id); + self.appearance = self.theme.appearance(&application.style()); + } +} diff --git a/winit/src/multi_window/window_manager.rs b/winit/src/multi_window/window_manager.rs new file mode 100644 index 00000000..9e15f9ea --- /dev/null +++ b/winit/src/multi_window/window_manager.rs @@ -0,0 +1,157 @@ +use crate::core::mouse; +use crate::core::window::Id; +use crate::core::{Point, Size}; +use crate::graphics::Compositor; +use crate::multi_window::{Application, State}; +use crate::style::application::StyleSheet; + +use std::collections::BTreeMap; +use std::sync::Arc; +use winit::monitor::MonitorHandle; + +#[allow(missing_debug_implementations)] +pub struct WindowManager<A: Application, C: Compositor> +where + <A::Renderer as crate::core::Renderer>::Theme: StyleSheet, + C: Compositor<Renderer = A::Renderer>, +{ + aliases: BTreeMap<winit::window::WindowId, Id>, + entries: BTreeMap<Id, Window<A, C>>, +} + +impl<A, C> WindowManager<A, C> +where + A: Application, + C: Compositor<Renderer = A::Renderer>, + <A::Renderer as crate::core::Renderer>::Theme: StyleSheet, +{ + pub fn new() -> Self { + Self { + aliases: BTreeMap::new(), + entries: BTreeMap::new(), + } + } + + pub fn insert( + &mut self, + id: Id, + window: Arc<winit::window::Window>, + application: &A, + compositor: &mut C, + exit_on_close_request: bool, + ) -> &mut Window<A, C> { + let state = State::new(application, id, &window); + let viewport_version = state.viewport_version(); + let physical_size = state.physical_size(); + let surface = compositor.create_surface( + window.clone(), + physical_size.width, + physical_size.height, + ); + let renderer = compositor.create_renderer(); + + let _ = self.aliases.insert(window.id(), id); + + let _ = self.entries.insert( + id, + Window { + raw: window, + state, + viewport_version, + exit_on_close_request, + surface, + renderer, + mouse_interaction: mouse::Interaction::Idle, + }, + ); + + self.entries + .get_mut(&id) + .expect("Get window that was just inserted") + } + + pub fn is_empty(&self) -> bool { + self.entries.is_empty() + } + + pub fn iter_mut( + &mut self, + ) -> impl Iterator<Item = (Id, &mut Window<A, C>)> { + self.entries.iter_mut().map(|(k, v)| (*k, v)) + } + + pub fn get_mut(&mut self, id: Id) -> Option<&mut Window<A, C>> { + self.entries.get_mut(&id) + } + + pub fn get_mut_alias( + &mut self, + id: winit::window::WindowId, + ) -> Option<(Id, &mut Window<A, C>)> { + let id = self.aliases.get(&id).copied()?; + + Some((id, self.get_mut(id)?)) + } + + pub fn last_monitor(&self) -> Option<MonitorHandle> { + self.entries.values().last()?.raw.current_monitor() + } + + pub fn remove(&mut self, id: Id) -> Option<Window<A, C>> { + let window = self.entries.remove(&id)?; + let _ = self.aliases.remove(&window.raw.id()); + + Some(window) + } +} + +impl<A, C> Default for WindowManager<A, C> +where + A: Application, + C: Compositor<Renderer = A::Renderer>, + <A::Renderer as crate::core::Renderer>::Theme: StyleSheet, +{ + fn default() -> Self { + Self::new() + } +} + +#[allow(missing_debug_implementations)] +pub struct Window<A, C> +where + A: Application, + C: Compositor<Renderer = A::Renderer>, + <A::Renderer as crate::core::Renderer>::Theme: StyleSheet, +{ + pub raw: Arc<winit::window::Window>, + pub state: State<A>, + pub viewport_version: u64, + pub exit_on_close_request: bool, + pub mouse_interaction: mouse::Interaction, + pub surface: C::Surface, + pub renderer: A::Renderer, +} + +impl<A, C> Window<A, C> +where + A: Application, + C: Compositor<Renderer = A::Renderer>, + <A::Renderer as crate::core::Renderer>::Theme: StyleSheet, +{ + pub fn position(&self) -> Option<Point> { + self.raw + .inner_position() + .ok() + .map(|position| position.to_logical(self.raw.scale_factor())) + .map(|position| Point { + x: position.x, + y: position.y, + }) + } + + pub fn size(&self) -> Size { + let size = self.raw.inner_size().to_logical(self.raw.scale_factor()); + + Size::new(size.width, size.height) + } +} diff --git a/winit/src/position.rs b/winit/src/position.rs deleted file mode 100644 index c260c29e..00000000 --- a/winit/src/position.rs +++ /dev/null @@ -1,22 +0,0 @@ -/// The position of a window in a given screen. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum Position { - /// The platform-specific default position for a new window. - Default, - /// The window is completely centered on the screen. - Centered, - /// The window is positioned with specific coordinates: `(X, Y)`. - /// - /// When the decorations of the window are enabled, Windows 10 will add some - /// invisible padding to the window. This padding gets included in the - /// position. So if you have decorations enabled and want the window to be - /// at (0, 0) you would have to set the position to - /// `(PADDING_X, PADDING_Y)`. - Specific(i32, i32), -} - -impl Default for Position { - fn default() -> Self { - Self::Default - } -} diff --git a/winit/src/settings.rs b/winit/src/settings.rs index 867dad0f..2e541128 100644 --- a/winit/src/settings.rs +++ b/winit/src/settings.rs @@ -1,39 +1,7 @@ //! Configure your application. -#[cfg(target_os = "windows")] -#[path = "settings/windows.rs"] -mod platform; +use crate::core::window; -#[cfg(target_os = "macos")] -#[path = "settings/macos.rs"] -mod platform; - -#[cfg(target_os = "linux")] -#[path = "settings/linux.rs"] -mod platform; - -#[cfg(target_arch = "wasm32")] -#[path = "settings/wasm.rs"] -mod platform; - -#[cfg(not(any( - target_os = "windows", - target_os = "macos", - target_os = "linux", - target_arch = "wasm32" -)))] -#[path = "settings/other.rs"] -mod platform; - -pub use platform::PlatformSpecific; - -use crate::conversion; -use crate::core::window::{Icon, Level}; -use crate::Position; - -use winit::monitor::MonitorHandle; -use winit::window::WindowBuilder; - -use std::fmt; +use std::borrow::Cow; /// The settings of an application. #[derive(Debug, Clone, Default)] @@ -44,198 +12,14 @@ pub struct Settings<Flags> { /// communicate with it through the windowing system. pub id: Option<String>, - /// The [`Window`] settings. - pub window: Window, + /// The [`window::Settings`]. + pub window: window::Settings, /// The data needed to initialize an [`Application`]. /// /// [`Application`]: crate::Application pub flags: Flags, - /// Whether the [`Application`] should exit when the user requests the - /// window to close (e.g. the user presses the close button). - /// - /// [`Application`]: crate::Application - pub exit_on_close_request: bool, -} - -/// The window settings of an application. -#[derive(Clone)] -pub struct Window { - /// The size of the window. - pub size: (u32, u32), - - /// The position of the window. - pub position: Position, - - /// The minimum size of the window. - pub min_size: Option<(u32, u32)>, - - /// The maximum size of the window. - pub max_size: Option<(u32, u32)>, - - /// Whether the window should be visible or not. - pub visible: bool, - - /// Whether the window should be resizable or not. - pub resizable: bool, - - /// Whether the window should have a border, a title bar, etc. - pub decorations: bool, - - /// Whether the window should be transparent. - pub transparent: bool, - - /// The window [`Level`]. - pub level: Level, - - /// The window icon, which is also usually used in the taskbar - pub icon: Option<Icon>, - - /// Platform specific settings. - pub platform_specific: platform::PlatformSpecific, -} - -impl fmt::Debug for Window { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("Window") - .field("size", &self.size) - .field("position", &self.position) - .field("min_size", &self.min_size) - .field("max_size", &self.max_size) - .field("visible", &self.visible) - .field("resizable", &self.resizable) - .field("decorations", &self.decorations) - .field("transparent", &self.transparent) - .field("level", &self.level) - .field("icon", &self.icon.is_some()) - .field("platform_specific", &self.platform_specific) - .finish() - } -} - -impl Window { - /// Converts the window settings into a `WindowBuilder` from `winit`. - pub fn into_builder( - self, - title: &str, - primary_monitor: Option<MonitorHandle>, - _id: Option<String>, - ) -> WindowBuilder { - let mut window_builder = WindowBuilder::new(); - - let (width, height) = self.size; - - window_builder = window_builder - .with_title(title) - .with_inner_size(winit::dpi::LogicalSize { width, height }) - .with_resizable(self.resizable) - .with_decorations(self.decorations) - .with_transparent(self.transparent) - .with_window_icon(self.icon.and_then(conversion::icon)) - .with_window_level(conversion::window_level(self.level)) - .with_visible(self.visible); - - if let Some(position) = conversion::position( - primary_monitor.as_ref(), - self.size, - self.position, - ) { - window_builder = window_builder.with_position(position); - } - - if let Some((width, height)) = self.min_size { - window_builder = window_builder - .with_min_inner_size(winit::dpi::LogicalSize { width, height }); - } - - if let Some((width, height)) = self.max_size { - window_builder = window_builder - .with_max_inner_size(winit::dpi::LogicalSize { width, height }); - } - - #[cfg(any( - target_os = "dragonfly", - target_os = "freebsd", - target_os = "netbsd", - target_os = "openbsd" - ))] - { - // `with_name` is available on both `WindowBuilderExtWayland` and `WindowBuilderExtX11` and they do - // exactly the same thing. We arbitrarily choose `WindowBuilderExtWayland` here. - use ::winit::platform::wayland::WindowBuilderExtWayland; - - if let Some(id) = _id { - window_builder = window_builder.with_name(id.clone(), id); - } - } - - #[cfg(target_os = "windows")] - { - use winit::platform::windows::WindowBuilderExtWindows; - #[allow(unsafe_code)] - unsafe { - window_builder = window_builder - .with_parent_window(self.platform_specific.parent); - } - window_builder = window_builder - .with_drag_and_drop(self.platform_specific.drag_and_drop); - } - - #[cfg(target_os = "macos")] - { - use winit::platform::macos::WindowBuilderExtMacOS; - - window_builder = window_builder - .with_title_hidden(self.platform_specific.title_hidden) - .with_titlebar_transparent( - self.platform_specific.titlebar_transparent, - ) - .with_fullsize_content_view( - self.platform_specific.fullsize_content_view, - ); - } - - #[cfg(target_os = "linux")] - { - #[cfg(feature = "x11")] - { - use winit::platform::x11::WindowBuilderExtX11; - - window_builder = window_builder.with_name( - &self.platform_specific.application_id, - &self.platform_specific.application_id, - ); - } - #[cfg(feature = "wayland")] - { - use winit::platform::wayland::WindowBuilderExtWayland; - - window_builder = window_builder.with_name( - &self.platform_specific.application_id, - &self.platform_specific.application_id, - ); - } - } - - window_builder - } -} - -impl Default for Window { - fn default() -> Window { - Window { - size: (1024, 768), - position: Position::default(), - min_size: None, - max_size: None, - visible: true, - resizable: true, - decorations: true, - transparent: false, - level: Level::default(), - icon: None, - platform_specific: PlatformSpecific::default(), - } - } + /// The fonts to load on boot. + pub fonts: Vec<Cow<'static, [u8]>>, } diff --git a/winit/src/settings/linux.rs b/winit/src/settings/linux.rs deleted file mode 100644 index 009b9d9e..00000000 --- a/winit/src/settings/linux.rs +++ /dev/null @@ -1,11 +0,0 @@ -//! Platform specific settings for Linux. - -/// The platform specific window settings of an application. -#[derive(Debug, Clone, PartialEq, Eq, Default)] -pub struct PlatformSpecific { - /// Sets the application id of the window. - /// - /// As a best practice, it is suggested to select an application id that match - /// the basename of the application’s .desktop file. - pub application_id: String, -} diff --git a/winit/src/settings/macos.rs b/winit/src/settings/macos.rs deleted file mode 100644 index f86e63ad..00000000 --- a/winit/src/settings/macos.rs +++ /dev/null @@ -1,12 +0,0 @@ -//! Platform specific settings for macOS. - -/// The platform specific window settings of an application. -#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] -pub struct PlatformSpecific { - /// Hides the window title. - pub title_hidden: bool, - /// Makes the titlebar transparent and allows the content to appear behind it. - pub titlebar_transparent: bool, - /// Makes the window content appear behind the titlebar. - pub fullsize_content_view: bool, -} diff --git a/winit/src/settings/other.rs b/winit/src/settings/other.rs deleted file mode 100644 index b1103f62..00000000 --- a/winit/src/settings/other.rs +++ /dev/null @@ -1,3 +0,0 @@ -/// The platform specific window settings of an application. -#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] -pub struct PlatformSpecific; diff --git a/winit/src/settings/wasm.rs b/winit/src/settings/wasm.rs deleted file mode 100644 index 8e0f1bbc..00000000 --- a/winit/src/settings/wasm.rs +++ /dev/null @@ -1,11 +0,0 @@ -//! Platform specific settings for WebAssembly. - -/// The platform specific window settings of an application. -#[derive(Debug, Clone, PartialEq, Eq, Default)] -pub struct PlatformSpecific { - /// The identifier of a DOM element that will be replaced with the - /// application. - /// - /// If set to `None`, the application will be appended to the HTML body. - pub target: Option<String>, -} diff --git a/winit/src/settings/windows.rs b/winit/src/settings/windows.rs deleted file mode 100644 index 45d753bd..00000000 --- a/winit/src/settings/windows.rs +++ /dev/null @@ -1,21 +0,0 @@ -//! Platform specific settings for Windows. -use raw_window_handle::RawWindowHandle; - -/// The platform specific window settings of an application. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub struct PlatformSpecific { - /// Parent window - pub parent: Option<RawWindowHandle>, - - /// Drag and drop support - pub drag_and_drop: bool, -} - -impl Default for PlatformSpecific { - fn default() -> Self { - Self { - parent: None, - drag_and_drop: true, - } - } -} |