diff options
author | 2023-11-29 22:28:31 +0100 | |
---|---|---|
committer | 2023-11-29 22:28:31 +0100 | |
commit | e09b4e24dda51b8212d8ece52431dacaa3922a7b (patch) | |
tree | 7005e181528134ebdde5bbbe5909273db9f30174 /winit | |
parent | 83c7870c569a2976923ee6243a19813094d44673 (diff) | |
parent | 7f8b17604a31e00becc43130ec516c1a53552c88 (diff) | |
download | iced-e09b4e24dda51b8212d8ece52431dacaa3922a7b.tar.gz iced-e09b4e24dda51b8212d8ece52431dacaa3922a7b.tar.bz2 iced-e09b4e24dda51b8212d8ece52431dacaa3922a7b.zip |
Merge branch 'master' into feat/multi-window-support
Diffstat (limited to 'winit')
-rw-r--r-- | winit/Cargo.toml | 71 | ||||
-rw-r--r-- | winit/src/application.rs | 29 | ||||
-rw-r--r-- | winit/src/application/state.rs | 4 | ||||
-rw-r--r-- | winit/src/clipboard.rs | 4 | ||||
-rw-r--r-- | winit/src/conversion.rs | 137 | ||||
-rw-r--r-- | winit/src/lib.rs | 15 | ||||
-rw-r--r-- | winit/src/multi_window.rs | 25 | ||||
-rw-r--r-- | winit/src/settings.rs | 88 | ||||
-rw-r--r-- | winit/src/system.rs | 2 |
9 files changed, 196 insertions, 179 deletions
diff --git a/winit/Cargo.toml b/winit/Cargo.toml index 30cec0b8..bab05b91 100644 --- a/winit/Cargo.toml +++ b/winit/Cargo.toml @@ -1,14 +1,14 @@ [package] name = "iced_winit" -version = "0.9.1" -authors = ["Héctor Ramón Jiménez <hector0193@gmail.com>"] -edition = "2021" -description = "A winit runtime for Iced" -license = "MIT" -repository = "https://github.com/iced-rs/iced" -documentation = "https://docs.rs/iced_winit" -keywords = ["gui", "ui", "graphics", "interface", "widgets"] -categories = ["gui"] +description = "A runtime for iced on top of winit" +version.workspace = true +edition.workspace = true +authors.workspace = true +license.workspace = true +repository.workspace = true +homepage.workspace = true +categories.workspace = true +keywords.workspace = true [features] default = ["x11", "wayland", "wayland-dlopen", "wayland-csd-adwaita"] @@ -22,36 +22,23 @@ wayland-csd-adwaita = ["winit/wayland-csd-adwaita"] multi-window = ["iced_runtime/multi-window"] [dependencies] -window_clipboard = "0.3" -log = "0.4" -thiserror = "1.0" -raw-window-handle = "0.5" - -[dependencies.winit] -version = "0.28" -git = "https://github.com/iced-rs/winit.git" -rev = "c52db2045d0a2f1b8d9923870de1d4ab1994146e" -default-features = false - -[dependencies.iced_runtime] -version = "0.1" -path = "../runtime" - -[dependencies.iced_graphics] -version = "0.8" -path = "../graphics" - -[dependencies.iced_style] -version = "0.8" -path = "../style" - -[target.'cfg(target_os = "windows")'.dependencies.winapi] -version = "0.3.6" - -[target.'cfg(target_arch = "wasm32")'.dependencies.web-sys] -version = "0.3" -features = ["Document", "Window"] - -[dependencies.sysinfo] -version = "0.28" -optional = true +iced_graphics.workspace = true +iced_runtime.workspace = true +iced_style.workspace = true + +log.workspace = true +raw-window-handle.workspace = true +thiserror.workspace = true +tracing.workspace = true +window_clipboard.workspace = true +winit.workspace = true + +sysinfo.workspace = true +sysinfo.optional = true + +[target.'cfg(target_os = "windows")'.dependencies] +winapi.workspace = true + +[target.'cfg(target_arch = "wasm32")'.dependencies] +web-sys.workspace = true +web-sys.features = ["Document", "Window"] diff --git a/winit/src/application.rs b/winit/src/application.rs index cffcb884..8457fd92 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -18,7 +18,6 @@ use crate::runtime::clipboard; use crate::runtime::program::Program; use crate::runtime::user_interface::{self, UserInterface}; use crate::runtime::{Command, Debug}; -use crate::settings; use crate::style::application::{Appearance, StyleSheet}; use crate::{Clipboard, Error, Proxy, Settings}; @@ -138,7 +137,7 @@ where let should_be_visible = settings.window.visible; let exit_on_close_request = settings.window.exit_on_close_request; - let builder = settings::window_builder( + let builder = conversion::window_settings( settings.window, &application.title(), event_loop.primary_monitor(), @@ -146,7 +145,7 @@ where ) .with_visible(false); - log::debug!("Window builder: {:#?}", builder); + log::debug!("Window builder: {builder:#?}"); let window = builder .build(&event_loop) @@ -163,7 +162,7 @@ where let body = document.body().unwrap(); let target = target.and_then(|target| { - body.query_selector(&format!("#{}", target)) + body.query_selector(&format!("#{target}")) .ok() .unwrap_or(None) }); @@ -182,7 +181,14 @@ where }; } - let (compositor, renderer) = C::new(compositor_settings, Some(&window))?; + let (compositor, mut renderer) = + C::new(compositor_settings, Some(&window))?; + + for font in settings.fonts { + use crate::core::text::Renderer; + + renderer.load_font(font); + } let (mut event_sender, event_receiver) = mpsc::unbounded(); let (control_sender, mut control_receiver) = mpsc::unbounded(); @@ -691,6 +697,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()); @@ -729,7 +738,7 @@ pub fn run_command<A, C, E>( size.width, size.height, ))) - .expect("Send message to event loop") + .expect("Send message to event loop"); } window::Action::Maximize(maximized) => { window.set_maximized(maximized); @@ -751,7 +760,7 @@ pub fn run_command<A, C, E>( )); } window::Action::ChangeIcon(icon) => { - window.set_window_icon(conversion::icon(icon)) + window.set_window_icon(conversion::icon(icon)); } window::Action::FetchMode(tag) => { let mode = if window.is_visible().unwrap_or(true) { @@ -765,7 +774,7 @@ pub fn run_command<A, C, E>( .expect("Send message to event loop"); } window::Action::ToggleMaximize => { - window.set_maximized(!window.is_maximized()) + window.set_maximized(!window.is_maximized()); } window::Action::ToggleDecorations => { window.set_decorations(!window.is_decorated()); @@ -800,7 +809,7 @@ pub fn run_command<A, C, E>( bytes, state.physical_size(), ))) - .expect("Send message to event loop.") + .expect("Send message to event loop."); } }, command::Action::System(action) => match action { @@ -818,7 +827,7 @@ pub fn run_command<A, C, E>( proxy .send_event(message) - .expect("Send message to event loop") + .expect("Send message to event loop"); }); } } diff --git a/winit/src/application/state.rs b/winit/src/application/state.rs index 967f43f2..e655529a 100644 --- a/winit/src/application/state.rs +++ b/winit/src/application/state.rs @@ -184,9 +184,7 @@ where /// window. /// /// Normally an [`Application`] should be synchronized with its [`State`] - /// and window after calling [`Application::update`]. - /// - /// [`Application::update`]: crate::Program::update + /// and window after calling [`crate::application::update`]. pub fn synchronize(&mut self, application: &A, window: &Window) { // Update window title let new_title = application.title(); diff --git a/winit/src/clipboard.rs b/winit/src/clipboard.rs index 7271441d..f7a32868 100644 --- a/winit/src/clipboard.rs +++ b/winit/src/clipboard.rs @@ -45,7 +45,7 @@ impl Clipboard { State::Connected(clipboard) => match clipboard.write(contents) { Ok(()) => {} Err(error) => { - log::warn!("error writing to clipboard: {}", error) + log::warn!("error writing to clipboard: {error}"); } }, State::Unavailable => {} @@ -59,6 +59,6 @@ impl crate::core::Clipboard for Clipboard { } fn write(&mut self, contents: String) { - self.write(contents) + self.write(contents); } } diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index 0625e74b..22e6b9be 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -1,13 +1,123 @@ -//! Convert [`winit`] types into [`iced_native`] types, and viceversa. +//! Convert [`winit`] types into [`iced_runtime`] types, and viceversa. //! //! [`winit`]: https://github.com/rust-windowing/winit -//! [`iced_native`]: https://github.com/iced-rs/iced/tree/0.9/native +//! [`iced_runtime`]: https://github.com/iced-rs/iced/tree/0.10/runtime use crate::core::keyboard; use crate::core::mouse; use crate::core::touch; use crate::core::window; use crate::core::{Event, Point}; +/// 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(); + + let (width, height) = settings.size; + + window_builder = window_builder + .with_title(title) + .with_inner_size(winit::dpi::LogicalSize { width, 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((width, height)) = settings.min_size { + window_builder = window_builder + .with_min_inner_size(winit::dpi::LogicalSize { width, height }); + } + + if let Some((width, height)) = settings.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(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( id: window::Id, @@ -238,10 +348,9 @@ pub fn mode(mode: Option<winit::window::Fullscreen>) -> window::Mode { } } -/// Converts a `MouseCursor` from [`iced_native`] to a [`winit`] cursor icon. +/// Converts a [`mouse::Interaction`] to a [`winit`] cursor icon. /// /// [`winit`]: https://github.com/rust-windowing/winit -/// [`iced_native`]: https://github.com/iced-rs/iced/tree/0.9/native pub fn mouse_interaction( interaction: mouse::Interaction, ) -> winit::window::CursorIcon { @@ -263,10 +372,10 @@ pub fn mouse_interaction( } } -/// Converts a `MouseButton` from [`winit`] to an [`iced_native`] mouse button. +/// Converts a `MouseButton` from [`winit`] to an [`iced`] mouse button. /// /// [`winit`]: https://github.com/rust-windowing/winit -/// [`iced_native`]: https://github.com/iced-rs/iced/tree/0.9/native +/// [`iced`]: https://github.com/iced-rs/iced/tree/0.10 pub fn mouse_button(mouse_button: winit::event::MouseButton) -> mouse::Button { match mouse_button { winit::event::MouseButton::Left => mouse::Button::Left, @@ -276,11 +385,11 @@ pub fn mouse_button(mouse_button: winit::event::MouseButton) -> mouse::Button { } } -/// Converts some `ModifiersState` from [`winit`] to an [`iced_native`] -/// modifiers state. +/// Converts some `ModifiersState` from [`winit`] to an [`iced`] modifiers +/// state. /// /// [`winit`]: https://github.com/rust-windowing/winit -/// [`iced_native`]: https://github.com/iced-rs/iced/tree/0.9/native +/// [`iced`]: https://github.com/iced-rs/iced/tree/0.10 pub fn modifiers( modifiers: winit::event::ModifiersState, ) -> keyboard::Modifiers { @@ -304,10 +413,10 @@ pub fn cursor_position( Point::new(logical_position.x, logical_position.y) } -/// Converts a `Touch` from [`winit`] to an [`iced_native`] touch event. +/// Converts a `Touch` from [`winit`] to an [`iced`] touch event. /// /// [`winit`]: https://github.com/rust-windowing/winit -/// [`iced_native`]: https://github.com/iced-rs/iced/tree/0.9/native +/// [`iced`]: https://github.com/iced-rs/iced/tree/0.10 pub fn touch_event( touch: winit::event::Touch, scale_factor: f64, @@ -335,10 +444,10 @@ pub fn touch_event( } } -/// Converts a `VirtualKeyCode` from [`winit`] to an [`iced_native`] key code. +/// Converts a `VirtualKeyCode` from [`winit`] to an [`iced`] key code. /// /// [`winit`]: https://github.com/rust-windowing/winit -/// [`iced_native`]: https://github.com/iced-rs/iced/tree/0.9/native +/// [`iced`]: https://github.com/iced-rs/iced/tree/0.10 pub fn key_code( virtual_keycode: winit::event::VirtualKeyCode, ) -> keyboard::KeyCode { @@ -531,7 +640,7 @@ pub fn user_attention( } } -/// Converts some [`Icon`] into it's `winit` counterpart. +/// Converts some [`window::Icon`] into it's `winit` counterpart. /// /// Returns `None` if there is an error during the conversion. pub fn icon(icon: window::Icon) -> Option<winit::window::Icon> { diff --git a/winit/src/lib.rs b/winit/src/lib.rs index 31002f51..cc886354 100644 --- a/winit/src/lib.rs +++ b/winit/src/lib.rs @@ -2,7 +2,7 @@ //! //!  //! -//! `iced_winit` offers some convenient abstractions on top of [`iced_native`] +//! `iced_winit` offers some convenient abstractions on top of [`iced_runtime`] //! to quickstart development when using [`winit`]. //! //! It exposes a renderer-agnostic [`Application`] trait that can be implemented @@ -11,25 +11,20 @@ //! Additionally, a [`conversion`] module is available for users that decide to //! implement a custom event loop. //! -//! [`iced_native`]: https://github.com/iced-rs/iced/tree/0.9/native +//! [`iced_runtime`]: https://github.com/iced-rs/iced/tree/0.10/runtime //! [`winit`]: https://github.com/rust-windowing/winit //! [`conversion`]: crate::conversion #![doc( html_logo_url = "https://raw.githubusercontent.com/iced-rs/iced/9ab6923e943f784985e9ef9ca28b10278297225d/docs/logo.svg" )] +#![forbid(rust_2018_idioms)] #![deny( missing_debug_implementations, missing_docs, unused_results, - clippy::extra_unused_lifetimes, - clippy::from_over_into, - clippy::needless_borrow, - clippy::new_without_default, - clippy::useless_conversion, - unsafe_code + unsafe_code, + rustdoc::broken_intra_doc_links )] -#![forbid(rust_2018_idioms)] -#![allow(clippy::inherent_to_string, clippy::type_complexity)] #![cfg_attr(docsrs, feature(doc_auto_cfg))] pub use iced_graphics as graphics; pub use iced_runtime as runtime; diff --git a/winit/src/multi_window.rs b/winit/src/multi_window.rs index b67c0a48..f2452eb3 100644 --- a/winit/src/multi_window.rs +++ b/winit/src/multi_window.rs @@ -4,6 +4,7 @@ mod windows; pub use state::State; +use crate::conversion; use crate::core::widget::operation; use crate::core::{self, mouse, renderer, window, Size}; use crate::futures::futures::channel::mpsc; @@ -15,11 +16,9 @@ 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::settings::window_builder; use crate::style::application::StyleSheet; -use crate::{conversion, settings, Clipboard, Error, Proxy, Settings}; +use crate::{Clipboard, Error, Proxy, Settings}; -use iced_runtime::user_interface::Cache; use std::mem::ManuallyDrop; use std::time::Instant; use winit::monitor::MonitorHandle; @@ -170,7 +169,7 @@ where let should_main_be_visible = settings.window.visible; let exit_on_close_request = settings.window.exit_on_close_request; - let builder = window_builder( + let builder = conversion::window_settings( settings.window, &application.title(window::Id::MAIN), event_loop.primary_monitor(), @@ -270,10 +269,11 @@ where }) => { let exit_on_close_request = settings.exit_on_close_request; - let window = - settings::window_builder(settings, &title, monitor, None) - .build(window_target) - .expect("Failed to build window"); + let window = conversion::window_settings( + settings, &title, monitor, None, + ) + .build(window_target) + .expect("Failed to build window"); Some(winit::event::Event::UserEvent(Event::WindowCreated { id, @@ -434,7 +434,7 @@ async fn run_instance<A, E, C>( // TODO mw application update returns which window IDs to update if !messages.is_empty() || uis_stale { - let mut cached_interfaces: Vec<Cache> = + let mut cached_interfaces: Vec<user_interface::Cache> = ManuallyDrop::into_inner(user_interfaces) .drain(..) .map(UserInterface::into_cache) @@ -859,6 +859,9 @@ pub fn run_command<A, C, E>( command::Action::Future(future) => { runtime.spawn(Box::pin(future.map(Event::Application))); } + command::Action::Stream(stream) => { + runtime.run(Box::pin(stream.map(Event::Application))); + } command::Action::Clipboard(action) => match action { clipboard::Action::Read(tag) => { let message = tag(clipboard.read()); @@ -1108,11 +1111,9 @@ pub fn user_force_quit( event: &winit::event::WindowEvent<'_>, _modifiers: winit::event::ModifiersState, ) -> bool { - use winit::event::WindowEvent; - match event { #[cfg(target_os = "macos")] - WindowEvent::KeyboardInput { + winit::event::WindowEvent::KeyboardInput { input: winit::event::KeyboardInput { virtual_keycode: Some(winit::event::VirtualKeyCode::Q), diff --git a/winit/src/settings.rs b/winit/src/settings.rs index c0b3b047..dc0f65a5 100644 --- a/winit/src/settings.rs +++ b/winit/src/settings.rs @@ -1,9 +1,7 @@ //! Configure your application. use crate::core::window; -use crate::conversion; -use winit::monitor::MonitorHandle; -use winit::window::WindowBuilder; +use std::borrow::Cow; /// The settings of an application. #[derive(Debug, Clone, Default)] @@ -21,87 +19,7 @@ pub struct Settings<Flags> { /// /// [`Application`]: crate::Application pub flags: Flags, -} - -/// Converts the window settings into a `WindowBuilder` from `winit`. -pub fn window_builder( - settings: window::Settings, - title: &str, - monitor: Option<MonitorHandle>, - _id: Option<String>, -) -> WindowBuilder { - let mut window_builder = WindowBuilder::new(); - - let (width, height) = settings.size; - - window_builder = window_builder - .with_title(title) - .with_inner_size(winit::dpi::LogicalSize { width, height }) - .with_resizable(settings.resizable) - .with_decorations(settings.decorations) - .with_transparent(settings.transparent) - .with_window_icon(settings.icon.and_then(conversion::icon)) - .with_window_level(conversion::window_level(settings.level)) - .with_visible(settings.visible); - - if let Some(position) = - conversion::position(monitor.as_ref(), settings.size, settings.position) - { - window_builder = window_builder.with_position(position); - } - - if let Some((width, height)) = settings.min_size { - window_builder = window_builder - .with_min_inner_size(winit::dpi::LogicalSize { width, height }); - } - - if let Some((width, height)) = settings.max_size { - window_builder = window_builder - .with_max_inner_size(winit::dpi::LogicalSize { width, height }); - } - - #[cfg(any( - target_os = "linux", - 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, - ); - } - window_builder + /// The fonts to load on boot. + pub fonts: Vec<Cow<'static, [u8]>>, } diff --git a/winit/src/system.rs b/winit/src/system.rs index 145a4d92..d4cef60e 100644 --- a/winit/src/system.rs +++ b/winit/src/system.rs @@ -23,7 +23,7 @@ pub(crate) fn information( let memory_used = sysinfo::get_current_pid() .and_then(|pid| system.process(pid).ok_or("Process not found")) - .map(|process| process.memory()) + .map(ProcessExt::memory) .ok(); Information { |