diff options
author | 2023-07-12 12:23:18 -0700 | |
---|---|---|
committer | 2023-07-12 12:23:18 -0700 | |
commit | 633f405f3f78bc7f82d2b2061491b0e011137451 (patch) | |
tree | 5ebfc1f45d216a5c14a90492563599e6969eab4d /winit | |
parent | 41836dd80d0534608e7aedfbf2319c540a23de1a (diff) | |
parent | 21bd51426d900e271206f314e0c915dd41065521 (diff) | |
download | iced-633f405f3f78bc7f82d2b2061491b0e011137451.tar.gz iced-633f405f3f78bc7f82d2b2061491b0e011137451.tar.bz2 iced-633f405f3f78bc7f82d2b2061491b0e011137451.zip |
Merge remote-tracking branch 'origin/master' into feat/multi-window-support
# Conflicts:
# Cargo.toml
# core/src/window/icon.rs
# core/src/window/id.rs
# core/src/window/position.rs
# core/src/window/settings.rs
# examples/integration/src/main.rs
# examples/integration_opengl/src/main.rs
# glutin/src/application.rs
# native/src/subscription.rs
# native/src/window.rs
# runtime/src/window/action.rs
# src/lib.rs
# src/window.rs
# winit/Cargo.toml
# winit/src/application.rs
# winit/src/icon.rs
# winit/src/settings.rs
# winit/src/window.rs
Diffstat (limited to 'winit')
-rw-r--r-- | winit/Cargo.toml | 31 | ||||
-rw-r--r-- | winit/README.md | 2 | ||||
-rw-r--r-- | winit/src/application.rs | 208 | ||||
-rw-r--r-- | winit/src/application/state.rs | 41 | ||||
-rw-r--r-- | winit/src/clipboard.rs | 17 | ||||
-rw-r--r-- | winit/src/conversion.rs | 50 | ||||
-rw-r--r-- | winit/src/error.rs | 7 | ||||
-rw-r--r-- | winit/src/icon.rs | 217 | ||||
-rw-r--r-- | winit/src/lib.rs | 18 | ||||
-rw-r--r-- | winit/src/proxy.rs | 2 | ||||
-rw-r--r-- | winit/src/settings.rs | 61 | ||||
-rw-r--r-- | winit/src/settings/windows.rs | 3 | ||||
-rw-r--r-- | winit/src/system.rs | 7 | ||||
-rw-r--r-- | winit/src/window.rs | 128 |
14 files changed, 293 insertions, 499 deletions
diff --git a/winit/Cargo.toml b/winit/Cargo.toml index 9efd1890..a4c0a402 100644 --- a/winit/Cargo.toml +++ b/winit/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "iced_winit" -version = "0.8.0" +version = "0.9.1" authors = ["Héctor Ramón Jiménez <hector0193@gmail.com>"] edition = "2021" description = "A winit runtime for Iced" @@ -11,34 +11,41 @@ keywords = ["gui", "ui", "graphics", "interface", "widgets"] categories = ["gui"] [features] +default = ["x11", "wayland", "wayland-dlopen", "wayland-csd-adwaita"] trace = ["tracing", "tracing-core", "tracing-subscriber"] chrome-trace = ["trace", "tracing-chrome"] -debug = ["iced_native/debug"] +debug = ["iced_runtime/debug"] system = ["sysinfo"] application = [] +x11 = ["winit/x11"] +wayland = ["winit/wayland"] +wayland-dlopen = ["winit/wayland-dlopen"] +wayland-csd-adwaita = ["winit/wayland-csd-adwaita"] multi-window = [] [dependencies] -window_clipboard = "0.2" +window_clipboard = "0.3" log = "0.4" thiserror = "1.0" +raw-window-handle = "0.5" [dependencies.winit] -version = "0.27" +version = "0.28" git = "https://github.com/iced-rs/winit.git" -rev = "940457522e9fb9f5dac228b0ecfafe0138b4048c" +rev = "c52db2045d0a2f1b8d9923870de1d4ab1994146e" +default-features = false -[dependencies.iced_native] -version = "0.9" -path = "../native" +[dependencies.iced_runtime] +version = "0.1" +path = "../runtime" [dependencies.iced_graphics] -version = "0.7" +version = "0.8" path = "../graphics" -[dependencies.iced_futures] -version = "0.6" -path = "../futures" +[dependencies.iced_style] +version = "0.8" +path = "../style" [dependencies.tracing] version = "0.1.37" diff --git a/winit/README.md b/winit/README.md index 83810473..91307970 100644 --- a/winit/README.md +++ b/winit/README.md @@ -20,7 +20,7 @@ It exposes a renderer-agnostic `Application` trait that can be implemented and t Add `iced_winit` as a dependency in your `Cargo.toml`: ```toml -iced_winit = "0.8" +iced_winit = "0.9" ``` __Iced moves fast and the `master` branch can contain breaking changes!__ If diff --git a/winit/src/application.rs b/winit/src/application.rs index fe97486f..ab7b2495 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -3,25 +3,25 @@ mod state; pub use state::State; -use crate::clipboard::{self, Clipboard}; use crate::conversion; -use crate::mouse; -use crate::renderer; -use crate::widget::operation; -use crate::{ - Command, Debug, Error, Event, Executor, Proxy, Runtime, Settings, Size, - Subscription, -}; - -use iced_futures::futures; -use iced_futures::futures::channel::mpsc; -use iced_graphics::compositor; -use iced_graphics::window; -use iced_native::program::Program; -use iced_native::time::Instant; -use iced_native::user_interface::{self, UserInterface}; - -pub use iced_native::application::{Appearance, StyleSheet}; +use crate::core; +use crate::core::mouse; +use crate::core::renderer; +use crate::core::time::Instant; +use crate::core::widget::operation; +use crate::core::window; +use crate::core::{Event, Size}; +use crate::futures::futures; +use crate::futures::{Executor, Runtime, Subscription}; +use crate::graphics::compositor::{self, Compositor}; +use crate::runtime::clipboard; +use crate::runtime::program::Program; +use crate::runtime::user_interface::{self, UserInterface}; +use crate::runtime::{Command, Debug}; +use crate::style::application::{Appearance, StyleSheet}; +use crate::{Clipboard, Error, Proxy, Settings}; + +use futures::channel::mpsc; use std::mem::ManuallyDrop; @@ -43,7 +43,7 @@ use tracing::{info_span, instrument::Instrument}; /// can be toggled by pressing `F12`. pub trait Application: Program where - <Self::Renderer as crate::Renderer>::Theme: StyleSheet, + <Self::Renderer as core::Renderer>::Theme: StyleSheet, { /// The data needed to initialize your [`Application`]. type Flags; @@ -65,12 +65,12 @@ where fn title(&self) -> String; /// Returns the current `Theme` of the [`Application`]. - fn theme(&self) -> <Self::Renderer as crate::Renderer>::Theme; + fn theme(&self) -> <Self::Renderer as core::Renderer>::Theme; /// Returns the `Style` variation of the `Theme`. fn style( &self, - ) -> <<Self::Renderer as crate::Renderer>::Theme as StyleSheet>::Style { + ) -> <<Self::Renderer as core::Renderer>::Theme as StyleSheet>::Style { Default::default() } @@ -110,8 +110,8 @@ pub fn run<A, E, C>( where A: Application + 'static, E: Executor + 'static, - C: window::Compositor<Renderer = A::Renderer> + 'static, - <A::Renderer as crate::Renderer>::Theme: StyleSheet, + C: Compositor<Renderer = A::Renderer> + 'static, + <A::Renderer as core::Renderer>::Theme: StyleSheet, { use futures::task; use futures::Future; @@ -177,13 +177,17 @@ where .unwrap_or(None) }); - let _ = match target { - Some(node) => node - .replace_child(&canvas, &node) - .expect(&format!("Could not replace #{}", node.id())), - None => body - .append_child(&canvas) - .expect("Append canvas to HTML body"), + 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"); + } }; } @@ -276,28 +280,25 @@ async fn run_instance<A, E, C>( ) where A: Application + 'static, E: Executor + 'static, - C: window::Compositor<Renderer = A::Renderer> + 'static, - <A::Renderer as crate::Renderer>::Theme: StyleSheet, + C: Compositor<Renderer = A::Renderer> + 'static, + <A::Renderer as core::Renderer>::Theme: StyleSheet, { - use iced_futures::futures::stream::StreamExt; + use futures::stream::StreamExt; use winit::event; use winit::event_loop::ControlFlow; - let mut clipboard = Clipboard::connect(&window); - let mut cache = user_interface::Cache::default(); - let mut surface = compositor.create_surface(&window); - let mut should_exit = false; - let mut state = State::new(&application, &window); let mut viewport_version = state.viewport_version(); - let physical_size = state.physical_size(); - compositor.configure_surface( - &mut surface, + let mut clipboard = Clipboard::connect(&window); + let mut cache = user_interface::Cache::default(); + let mut surface = compositor.create_surface( + &window, physical_size.width, physical_size.height, ); + let mut should_exit = false; if should_be_visible { window.set_visible(true); @@ -305,6 +306,8 @@ async fn run_instance<A, E, C>( run_command( &application, + &mut compositor, + &mut surface, &mut cache, &state, &mut renderer, @@ -315,9 +318,8 @@ async fn run_instance<A, E, C>( &mut proxy, &mut debug, &window, - || compositor.fetch_information(), ); - runtime.track(application.subscription()); + runtime.track(application.subscription().into_recipes()); let mut user_interface = ManuallyDrop::new(build_user_interface( &application, @@ -353,7 +355,7 @@ async fn run_instance<A, E, C>( let (interface_state, statuses) = user_interface.update( &events, - state.cursor_position(), + state.cursor(), &mut renderer, &mut clipboard, &mut messages, @@ -361,8 +363,10 @@ async fn run_instance<A, E, C>( debug.event_processing_finished(); - for event in events.drain(..).zip(statuses.into_iter()) { - runtime.broadcast(event); + for (event, status) in + events.drain(..).zip(statuses.into_iter()) + { + runtime.broadcast(event, status); } if !messages.is_empty() @@ -377,6 +381,8 @@ async fn run_instance<A, E, C>( // Update application update( &mut application, + &mut compositor, + &mut surface, &mut cache, &state, &mut renderer, @@ -387,7 +393,6 @@ async fn run_instance<A, E, C>( &mut debug, &mut messages, &window, - || compositor.fetch_information(), ); // Update window @@ -412,13 +417,13 @@ 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( - crate::window::Id::MAIN, - crate::window::Event::RedrawRequested(Instant::now()), + window::Id::MAIN, + window::Event::RedrawRequested(Instant::now()), ); let (interface_state, _) = user_interface.update( &[redraw_event.clone()], - state.cursor_position(), + state.cursor(), &mut renderer, &mut clipboard, &mut messages, @@ -431,7 +436,7 @@ async fn run_instance<A, E, C>( &renderer::Style { text_color: state.text_color(), }, - state.cursor_position(), + state.cursor(), ); debug.draw_finished(); @@ -444,17 +449,14 @@ async fn run_instance<A, E, C>( } window.request_redraw(); - runtime - .broadcast((redraw_event, crate::event::Status::Ignored)); + 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 { - crate::window::RedrawRequest::NextFrame => { - ControlFlow::Poll - } - crate::window::RedrawRequest::At(at) => { + window::RedrawRequest::NextFrame => ControlFlow::Poll, + window::RedrawRequest::At(at) => { ControlFlow::WaitUntil(at) } }, @@ -466,9 +468,9 @@ async fn run_instance<A, E, C>( event::Event::PlatformSpecific(event::PlatformSpecific::MacOS( event::MacOS::ReceivedUrl(url), )) => { - use iced_native::event; + use crate::core::event; - events.push(iced_native::Event::PlatformSpecific( + events.push(Event::PlatformSpecific( event::PlatformSpecific::MacOS(event::MacOS::ReceivedUrl( url, )), @@ -507,7 +509,7 @@ async fn run_instance<A, E, C>( &renderer::Style { text_color: state.text_color(), }, - state.cursor_position(), + state.cursor(), ); if new_mouse_interaction != mouse_interaction { @@ -568,7 +570,7 @@ async fn run_instance<A, E, C>( state.update(&window, &window_event, &mut debug); if let Some(event) = conversion::window_event( - crate::window::Id::MAIN, + window::Id::MAIN, &window_event, state.scale_factor(), state.modifiers(), @@ -618,7 +620,7 @@ pub fn build_user_interface<'a, A: Application>( debug: &mut Debug, ) -> UserInterface<'a, A::Message, A::Renderer> where - <A::Renderer as crate::Renderer>::Theme: StyleSheet, + <A::Renderer as core::Renderer>::Theme: StyleSheet, { #[cfg(feature = "trace")] let view_span = info_span!("Application", "VIEW").entered(); @@ -645,8 +647,10 @@ where /// Updates an [`Application`] by feeding it the provided messages, spawning any /// resulting [`Command`], and tracking its [`Subscription`]. -pub fn update<A: Application, E: Executor>( +pub fn update<A: Application, C, E: Executor>( application: &mut A, + compositor: &mut C, + surface: &mut C::Surface, cache: &mut user_interface::Cache, state: &State<A>, renderer: &mut A::Renderer, @@ -657,9 +661,9 @@ pub fn update<A: Application, E: Executor>( debug: &mut Debug, messages: &mut Vec<A::Message>, window: &winit::window::Window, - graphics_info: impl FnOnce() -> compositor::Information + Copy, ) where - <A::Renderer as crate::Renderer>::Theme: StyleSheet, + C: Compositor<Renderer = A::Renderer> + 'static, + <A::Renderer as core::Renderer>::Theme: StyleSheet, { for message in messages.drain(..) { #[cfg(feature = "trace")] @@ -676,6 +680,8 @@ pub fn update<A: Application, E: Executor>( run_command( application, + compositor, + surface, cache, state, renderer, @@ -686,17 +692,18 @@ pub fn update<A: Application, E: Executor>( proxy, debug, window, - graphics_info, ); } let subscription = application.subscription(); - runtime.track(subscription); + runtime.track(subscription.into_recipes()); } /// Runs the actions of a [`Command`]. -pub fn run_command<A, E>( +pub fn run_command<A, C, E>( application: &A, + compositor: &mut C, + surface: &mut C::Surface, cache: &mut user_interface::Cache, state: &State<A>, renderer: &mut A::Renderer, @@ -707,15 +714,15 @@ pub fn run_command<A, E>( proxy: &mut winit::event_loop::EventLoopProxy<A::Message>, debug: &mut Debug, window: &winit::window::Window, - _graphics_info: impl FnOnce() -> compositor::Information + Copy, ) where A: Application, E: Executor, - <A::Renderer as crate::Renderer>::Theme: StyleSheet, + C: Compositor<Renderer = A::Renderer> + 'static, + <A::Renderer as core::Renderer>::Theme: StyleSheet, { - use iced_native::command; - use iced_native::system; - use iced_native::window; + use crate::runtime::command; + use crate::runtime::system; + use crate::runtime::window; for action in command.actions() { match action { @@ -746,12 +753,22 @@ pub fn run_command<A, E>( "Spawning a window is only available with `multi_window::Application`s." ) } - window::Action::Resize { width, height } => { + window::Action::Resize(size) => { window.set_inner_size(winit::dpi::LogicalSize { - width, - height, + width: size.width, + height: size.height, }); } + window::Action::FetchSize(callback) => { + let size = window.inner_size(); + + proxy + .send_event(callback(Size::new( + size.width, + size.height, + ))) + .expect("Send message to event loop") + } window::Action::Maximize(maximized) => { window.set_maximized(maximized); } @@ -771,11 +788,14 @@ pub fn run_command<A, E>( mode, )); } + window::Action::ChangeIcon(icon) => { + window.set_window_icon(conversion::icon(icon)) + } window::Action::FetchMode(tag) => { let mode = if window.is_visible().unwrap_or(true) { conversion::mode(window.fullscreen()) } else { - window::Mode::Hidden + core::window::Mode::Hidden }; proxy @@ -796,20 +816,36 @@ pub fn run_command<A, E>( window::Action::GainFocus => { window.focus_window(); } - window::Action::ChangeAlwaysOnTop(on_top) => { - window.set_always_on_top(on_top); + window::Action::ChangeLevel(level) => { + window.set_window_level(conversion::window_level(level)); } window::Action::FetchId(tag) => { proxy .send_event(tag(window.id().into())) .expect("Send message to event loop"); } + window::Action::Screenshot(tag) => { + let bytes = compositor.screenshot( + renderer, + surface, + state.viewport(), + state.background_color(), + &debug.overlay(), + ); + + proxy + .send_event(tag(window::Screenshot::new( + bytes, + state.physical_size(), + ))) + .expect("Send message to event loop.") + } }, command::Action::System(action) => match action { system::Action::QueryInformation(_tag) => { #[cfg(feature = "system")] { - let graphics_info = _graphics_info(); + let graphics_info = compositor.fetch_information(); let proxy = proxy.clone(); let _ = std::thread::spawn(move || { @@ -827,7 +863,7 @@ pub fn run_command<A, E>( }, command::Action::Widget(action) => { let mut current_cache = std::mem::take(cache); - let mut current_operation = Some(action.into_operation()); + let mut current_operation = Some(action); let mut user_interface = build_user_interface( application, @@ -856,6 +892,16 @@ pub fn run_command<A, E>( current_cache = user_interface.into_cache(); *cache = current_cache; } + command::Action::LoadFont { bytes, tagger } => { + use crate::core::text::Renderer; + + // TODO: Error handling (?) + renderer.load_font(bytes); + + proxy + .send_event(tagger(Ok(()))) + .expect("Send message to event loop"); + } } } } diff --git a/winit/src/application/state.rs b/winit/src/application/state.rs index 8d6a1df1..967f43f2 100644 --- a/winit/src/application/state.rs +++ b/winit/src/application/state.rs @@ -1,6 +1,11 @@ use crate::application::{self, StyleSheet as _}; use crate::conversion; -use crate::{Application, Color, Debug, Point, Size, Viewport}; +use crate::core; +use crate::core::mouse; +use crate::core::{Color, Size}; +use crate::graphics::Viewport; +use crate::runtime::Debug; +use crate::Application; use std::marker::PhantomData; use winit::event::{Touch, WindowEvent}; @@ -10,22 +15,22 @@ use winit::window::Window; #[allow(missing_debug_implementations)] pub struct State<A: Application> where - <A::Renderer as crate::Renderer>::Theme: application::StyleSheet, + <A::Renderer as core::Renderer>::Theme: application::StyleSheet, { title: String, scale_factor: f64, viewport: Viewport, viewport_version: usize, - cursor_position: winit::dpi::PhysicalPosition<f64>, + cursor_position: Option<winit::dpi::PhysicalPosition<f64>>, modifiers: winit::event::ModifiersState, - theme: <A::Renderer as crate::Renderer>::Theme, + theme: <A::Renderer as core::Renderer>::Theme, appearance: application::Appearance, application: PhantomData<A>, } impl<A: Application> State<A> where - <A::Renderer as crate::Renderer>::Theme: application::StyleSheet, + <A::Renderer as core::Renderer>::Theme: application::StyleSheet, { /// Creates a new [`State`] for the provided [`Application`] and window. pub fn new(application: &A, window: &Window) -> Self { @@ -48,8 +53,7 @@ where scale_factor, viewport, viewport_version: 0, - // TODO: Encode cursor availability in the type-system - cursor_position: winit::dpi::PhysicalPosition::new(-1.0, -1.0), + cursor_position: None, modifiers: winit::event::ModifiersState::default(), theme, appearance, @@ -85,11 +89,16 @@ where } /// Returns the current cursor position of the [`State`]. - pub fn cursor_position(&self) -> Point { - conversion::cursor_position( - self.cursor_position, - self.viewport.scale_factor(), - ) + 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`]. @@ -98,7 +107,7 @@ where } /// Returns the current theme of the [`State`]. - pub fn theme(&self) -> &<A::Renderer as crate::Renderer>::Theme { + pub fn theme(&self) -> &<A::Renderer as core::Renderer>::Theme { &self.theme } @@ -149,12 +158,10 @@ where | WindowEvent::Touch(Touch { location: position, .. }) => { - self.cursor_position = *position; + self.cursor_position = Some(*position); } WindowEvent::CursorLeft { .. } => { - // TODO: Encode cursor availability in the type-system - self.cursor_position = - winit::dpi::PhysicalPosition::new(-1.0, -1.0); + self.cursor_position = None; } WindowEvent::ModifiersChanged(new_modifiers) => { self.modifiers = *new_modifiers; diff --git a/winit/src/clipboard.rs b/winit/src/clipboard.rs index c1fd8813..7271441d 100644 --- a/winit/src/clipboard.rs +++ b/winit/src/clipboard.rs @@ -1,7 +1,4 @@ //! Access the clipboard. -pub use iced_native::clipboard::Action; - -use crate::command::{self, Command}; /// A buffer for short-term storage and transfer within and between /// applications. @@ -56,7 +53,7 @@ impl Clipboard { } } -impl iced_native::Clipboard for Clipboard { +impl crate::core::Clipboard for Clipboard { fn read(&self) -> Option<String> { self.read() } @@ -65,15 +62,3 @@ impl iced_native::Clipboard for Clipboard { self.write(contents) } } - -/// Read the current contents of the clipboard. -pub fn read<Message>( - f: impl Fn(Option<String>) -> Message + 'static, -) -> Command<Message> { - Command::single(command::Action::Clipboard(Action::Read(Box::new(f)))) -} - -/// Write the given contents to the clipboard. -pub fn write<Message>(contents: String) -> Command<Message> { - Command::single(command::Action::Clipboard(Action::Write(contents))) -} diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index 5c86b30a..fe0fce19 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -1,12 +1,13 @@ //! Convert [`winit`] types into [`iced_native`] types, and viceversa. //! //! [`winit`]: https://github.com/rust-windowing/winit -//! [`iced_native`]: https://github.com/iced-rs/iced/tree/0.8/native -use crate::keyboard; -use crate::mouse; -use crate::touch; -use crate::window; -use crate::{Event, Point, Position}; +//! [`iced_native`]: https://github.com/iced-rs/iced/tree/0.9/native +use crate::core::keyboard; +use crate::core::mouse; +use crate::core::touch; +use crate::core::window; +use crate::core::{Event, Point}; +use crate::Position; /// Converts a winit window event into an iced event. pub fn window_event( @@ -149,6 +150,19 @@ pub fn window_event( } } +/// Converts a [`window::Level`] to a [`winit`] window level. +/// +/// [`winit`]: https://github.com/rust-windowing/winit +pub fn window_level(level: window::Level) -> winit::window::WindowLevel { + match level { + window::Level::Normal => winit::window::WindowLevel::Normal, + window::Level::AlwaysOnBottom => { + winit::window::WindowLevel::AlwaysOnBottom + } + window::Level::AlwaysOnTop => winit::window::WindowLevel::AlwaysOnTop, + } +} + /// Converts a [`Position`] to a [`winit`] logical position for a given monitor. /// /// [`winit`]: https://github.com/rust-windowing/winit @@ -228,7 +242,7 @@ pub fn mode(mode: Option<winit::window::Fullscreen>) -> window::Mode { /// Converts a `MouseCursor` from [`iced_native`] to a [`winit`] cursor icon. /// /// [`winit`]: https://github.com/rust-windowing/winit -/// [`iced_native`]: https://github.com/iced-rs/iced/tree/0.8/native +/// [`iced_native`]: https://github.com/iced-rs/iced/tree/0.9/native pub fn mouse_interaction( interaction: mouse::Interaction, ) -> winit::window::CursorIcon { @@ -246,21 +260,20 @@ pub fn mouse_interaction( winit::window::CursorIcon::EwResize } Interaction::ResizingVertically => winit::window::CursorIcon::NsResize, + Interaction::NotAllowed => winit::window::CursorIcon::NotAllowed, } } /// Converts a `MouseButton` from [`winit`] to an [`iced_native`] mouse button. /// /// [`winit`]: https://github.com/rust-windowing/winit -/// [`iced_native`]: https://github.com/iced-rs/iced/tree/0.8/native +/// [`iced_native`]: https://github.com/iced-rs/iced/tree/0.9/native pub fn mouse_button(mouse_button: winit::event::MouseButton) -> mouse::Button { match 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::Other(other) => { - mouse::Button::Other(other as u8) - } + winit::event::MouseButton::Other(other) => mouse::Button::Other(other), } } @@ -268,7 +281,7 @@ pub fn mouse_button(mouse_button: winit::event::MouseButton) -> mouse::Button { /// modifiers state. /// /// [`winit`]: https://github.com/rust-windowing/winit -/// [`iced_native`]: https://github.com/iced-rs/iced/tree/0.8/native +/// [`iced_native`]: https://github.com/iced-rs/iced/tree/0.9/native pub fn modifiers( modifiers: winit::event::ModifiersState, ) -> keyboard::Modifiers { @@ -295,7 +308,7 @@ pub fn cursor_position( /// Converts a `Touch` from [`winit`] to an [`iced_native`] touch event. /// /// [`winit`]: https://github.com/rust-windowing/winit -/// [`iced_native`]: https://github.com/iced-rs/iced/tree/0.8/native +/// [`iced_native`]: https://github.com/iced-rs/iced/tree/0.9/native pub fn touch_event( touch: winit::event::Touch, scale_factor: f64, @@ -326,7 +339,7 @@ pub fn touch_event( /// Converts a `VirtualKeyCode` from [`winit`] to an [`iced_native`] key code. /// /// [`winit`]: https://github.com/rust-windowing/winit -/// [`iced_native`]: https://github.com/iced-rs/iced/tree/0.8/native +/// [`iced_native`]: https://github.com/iced-rs/iced/tree/0.9/native pub fn key_code( virtual_keycode: winit::event::VirtualKeyCode, ) -> keyboard::KeyCode { @@ -519,6 +532,15 @@ pub fn user_attention( } } +/// Converts some [`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> { + let (pixels, size) = icon.into_raw(); + + 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!( diff --git a/winit/src/error.rs b/winit/src/error.rs index eaeafd51..7687fb17 100644 --- a/winit/src/error.rs +++ b/winit/src/error.rs @@ -1,4 +1,5 @@ -use iced_futures::futures; +use crate::futures::futures; +use crate::graphics; /// An error that occurred while running an application. #[derive(Debug, thiserror::Error)] @@ -13,10 +14,10 @@ pub enum Error { /// The application graphics context could not be created. #[error("the application graphics context could not be created")] - GraphicsCreationFailed(iced_graphics::Error), + GraphicsCreationFailed(graphics::Error), } -impl From<iced_graphics::Error> for Error { +impl From<graphics::Error> for Error { fn from(error: iced_graphics::Error) -> Error { Error::GraphicsCreationFailed(error) } diff --git a/winit/src/icon.rs b/winit/src/icon.rs index 4113d9d0..0fe010ca 100644 --- a/winit/src/icon.rs +++ b/winit/src/icon.rs @@ -1,182 +1,63 @@ //! Attach an icon to the window of your application. -use std::fmt; -use std::io; - -#[cfg(feature = "image_rs")] -use std::path::Path; - -/// The icon of a window. -#[derive(Clone)] -pub struct Icon(winit::window::Icon); +pub use crate::core::window::icon::*; -impl fmt::Debug for Icon { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_tuple("Icon").field(&format_args!("_")).finish() - } -} - -impl Icon { - /// Creates an icon from 32bpp RGBA data. - pub fn from_rgba( - rgba: Vec<u8>, - width: u32, - height: u32, - ) -> Result<Self, Error> { - let raw = winit::window::Icon::from_rgba(rgba, width, height)?; +use crate::core::window::icon; - Ok(Icon(raw)) - } - - /// Creates an icon from an image file. - /// - /// This will return an error in case the file is missing at run-time. You may prefer [`Self::from_file_data`] instead. - #[cfg(feature = "image_rs")] - pub fn from_file<P: AsRef<Path>>(icon_path: P) -> Result<Self, Error> { - let icon = image_rs::io::Reader::open(icon_path)?.decode()?.to_rgba8(); +use std::io; - Self::from_rgba(icon.to_vec(), icon.width(), icon.height()) - } +#[cfg(feature = "image")] +use std::path::Path; - /// Creates an icon from the content of an image file. - /// - /// This content can be included in your application at compile-time, e.g. using the `include_bytes!` macro. \ - /// You can pass an explicit file format. Otherwise, the file format will be guessed at runtime. - #[cfg(feature = "image_rs")] - pub fn from_file_data( - data: &[u8], - explicit_format: Option<image_rs::ImageFormat>, - ) -> Result<Self, Error> { - let mut icon = image_rs::io::Reader::new(std::io::Cursor::new(data)); - let icon_with_format = match explicit_format { - Some(format) => { - icon.set_format(format); - icon - } - None => icon.with_guessed_format()?, - }; +/// Creates an icon from an image file. +/// +/// This will return an error in case the file is missing at run-time. You may prefer [`Self::from_file_data`] instead. +#[cfg(feature = "image")] +pub fn from_file<P: AsRef<Path>>(icon_path: P) -> Result<Icon, Error> { + let icon = image_rs::io::Reader::open(icon_path)?.decode()?.to_rgba8(); + + Ok(icon::from_rgba(icon.to_vec(), icon.width(), icon.height())?) +} + +/// Creates an icon from the content of an image file. +/// +/// This content can be included in your application at compile-time, e.g. using the `include_bytes!` macro. +/// You can pass an explicit file format. Otherwise, the file format will be guessed at runtime. +#[cfg(feature = "image")] +pub fn from_file_data( + data: &[u8], + explicit_format: Option<image_rs::ImageFormat>, +) -> Result<Icon, Error> { + let mut icon = image_rs::io::Reader::new(std::io::Cursor::new(data)); + let icon_with_format = match explicit_format { + Some(format) => { + icon.set_format(format); + icon + } + None => icon.with_guessed_format()?, + }; - let pixels = icon_with_format.decode()?.to_rgba8(); + let pixels = icon_with_format.decode()?.to_rgba8(); - Self::from_rgba(pixels.to_vec(), pixels.width(), pixels.height()) - } + Ok(icon::from_rgba( + pixels.to_vec(), + pixels.width(), + pixels.height(), + )?) } -/// An error produced when using `Icon::from_rgba` with invalid arguments. -#[derive(Debug)] +/// An error produced when creating an [`Icon`]. +#[derive(Debug, thiserror::Error)] pub enum Error { - /// The provided RGBA data isn't divisble by 4. - /// - /// Therefore, it cannot be safely interpreted as 32bpp RGBA pixels. - InvalidData { - /// The length of the provided RGBA data. - byte_count: usize, - }, - - /// The number of RGBA pixels does not match the provided dimensions. - DimensionsMismatch { - /// The provided width. - width: u32, - /// The provided height. - height: u32, - /// The amount of pixels of the provided RGBA data. - pixel_count: usize, - }, + /// The [`Icon`] is not valid. + #[error("The icon is invalid: {0}")] + InvalidError(#[from] icon::Error), /// The underlying OS failed to create the icon. - OsError(io::Error), - - /// The `image` crate reported an error - #[cfg(feature = "image_rs")] - ImageError(image_rs::error::ImageError), -} - -impl From<std::io::Error> for Error { - fn from(os_error: std::io::Error) -> Self { - Error::OsError(os_error) - } -} - -impl From<winit::window::BadIcon> for Error { - fn from(error: winit::window::BadIcon) -> Self { - use winit::window::BadIcon; - - match error { - BadIcon::ByteCountNotDivisibleBy4 { byte_count } => { - Error::InvalidData { byte_count } - } - BadIcon::DimensionsVsPixelCount { - width, - height, - pixel_count, - .. - } => Error::DimensionsMismatch { - width, - height, - pixel_count, - }, - BadIcon::OsError(os_error) => Error::OsError(os_error), - } - } -} - -impl From<Icon> for winit::window::Icon { - fn from(icon: Icon) -> Self { - icon.0 - } -} - -#[cfg(feature = "image_rs")] -impl From<image_rs::error::ImageError> for Error { - fn from(image_error: image_rs::error::ImageError) -> Self { - Self::ImageError(image_error) - } -} - -impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Error::InvalidData { byte_count } => { - write!( - f, - "The provided RGBA data (with length {byte_count:?}) isn't divisble by \ - 4. Therefore, it cannot be safely interpreted as 32bpp RGBA \ - pixels." - ) - } - Error::DimensionsMismatch { - width, - height, - pixel_count, - } => { - write!( - f, - "The number of RGBA pixels ({pixel_count:?}) does not match the provided \ - dimensions ({width:?}x{height:?})." - ) - } - Error::OsError(e) => write!( - f, - "The underlying OS failed to create the window \ - icon: {e:?}" - ), - #[cfg(feature = "image_rs")] - Error::ImageError(e) => { - write!(f, "Unable to create icon from a file: {e:?}") - } - } - } -} - -impl std::error::Error for Error { - fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { - Some(self) - } -} - -impl TryFrom<iced_native::window::Icon> for Icon { - type Error = Error; + #[error("The underlying OS failted to create the window icon: {0}")] + OsError(#[from] io::Error), - fn try_from(icon: iced_native::window::Icon) -> Result<Self, Self::Error> { - Icon::from_rgba(icon.rgba, icon.width, icon.height) - } + /// The `image` crate reported an error. + #[cfg(feature = "image")] + #[error("Unable to create icon from a file: {0}")] + ImageError(#[from] image_rs::error::ImageError), } diff --git a/winit/src/lib.rs b/winit/src/lib.rs index 54b9c31f..dc163430 100644 --- a/winit/src/lib.rs +++ b/winit/src/lib.rs @@ -11,7 +11,7 @@ //! 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.8/native +//! [`iced_native`]: https://github.com/iced-rs/iced/tree/0.9/native //! [`winit`]: https://github.com/rust-windowing/winit //! [`conversion`]: crate::conversion #![doc( @@ -25,14 +25,17 @@ clippy::from_over_into, clippy::needless_borrow, clippy::new_without_default, - clippy::useless_conversion + clippy::useless_conversion, + unsafe_code )] -#![forbid(rust_2018_idioms, unsafe_code)] +#![forbid(rust_2018_idioms)] #![allow(clippy::inherent_to_string, clippy::type_complexity)] -#![cfg_attr(docsrs, feature(doc_cfg))] - -#[doc(no_inline)] -pub use iced_native::*; +#![cfg_attr(docsrs, feature(doc_auto_cfg))] +pub use iced_graphics as graphics; +pub use iced_runtime as runtime; +pub use iced_runtime::core; +pub use iced_runtime::futures; +pub use iced_style as style; pub use winit; #[cfg(feature = "multi-window")] @@ -43,7 +46,6 @@ pub mod application; pub mod clipboard; pub mod conversion; pub mod settings; -pub mod window; #[cfg(feature = "system")] pub mod system; diff --git a/winit/src/proxy.rs b/winit/src/proxy.rs index 7b9074d7..1d6c48bb 100644 --- a/winit/src/proxy.rs +++ b/winit/src/proxy.rs @@ -1,4 +1,4 @@ -use iced_native::futures::{ +use crate::futures::futures::{ channel::mpsc, task::{Context, Poll}, Sink, diff --git a/winit/src/settings.rs b/winit/src/settings.rs index 88d7c1de..40b3d487 100644 --- a/winit/src/settings.rs +++ b/winit/src/settings.rs @@ -22,7 +22,7 @@ mod platform; pub use platform::PlatformSpecific; use crate::conversion; -use crate::Icon; +use crate::core::window::{Icon, Level}; use crate::Position; use winit::monitor::MonitorHandle; @@ -50,20 +50,8 @@ pub struct Settings<Flags> { /// Whether the [`Application`] should exit when the user requests the /// window to close (e.g. the user presses the close button). /// - /// NOTE: This is not used for `multi-window`, instead check [`Application::close_requested`]. - /// - /// [`close_requested`]: crate::multi_window::Application::close_requested - /// /// [`Application`]: crate::Application pub exit_on_close_request: bool, - - /// Whether the [`Application`] should try to build the context - /// using OpenGL ES first then OpenGL. - /// - /// NOTE: Only works for the `glow` backend. - /// - /// [`Application`]: crate::Application - pub try_opengles_first: bool, } /// The window settings of an application. @@ -93,11 +81,11 @@ pub struct Window { /// Whether the window should be transparent. pub transparent: bool, - /// Whether the window will always be on top of other windows. - pub always_on_top: bool, + /// The window [`Level`]. + pub level: Level, /// The window icon, which is also usually used in the taskbar - pub icon: Option<winit::window::Icon>, + pub icon: Option<Icon>, /// Platform specific settings. pub platform_specific: platform::PlatformSpecific, @@ -114,7 +102,7 @@ impl fmt::Debug for Window { .field("resizable", &self.resizable) .field("decorations", &self.decorations) .field("transparent", &self.transparent) - .field("always_on_top", &self.always_on_top) + .field("level", &self.level) .field("icon", &self.icon.is_some()) .field("platform_specific", &self.platform_specific) .finish() @@ -139,8 +127,9 @@ impl Window { .with_resizable(self.resizable) .with_decorations(self.decorations) .with_transparent(self.transparent) - .with_window_icon(self.icon) - .with_always_on_top(self.always_on_top); + .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(), @@ -168,7 +157,9 @@ impl Window { target_os = "openbsd" ))] { - use ::winit::platform::unix::WindowBuilderExtUnix; + // `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); @@ -178,11 +169,11 @@ impl Window { #[cfg(target_os = "windows")] { use winit::platform::windows::WindowBuilderExtWindows; - - if let Some(parent) = self.platform_specific.parent { - window_builder = window_builder.with_parent_window(parent); + #[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); } @@ -216,29 +207,9 @@ impl Default for Window { resizable: true, decorations: true, transparent: false, - always_on_top: false, + level: Level::default(), icon: None, platform_specific: Default::default(), } } } - -impl From<iced_native::window::Settings> for Window { - fn from(settings: iced_native::window::Settings) -> Self { - Self { - size: settings.size, - position: settings.position, - min_size: settings.min_size, - max_size: settings.max_size, - visible: settings.visible, - resizable: settings.resizable, - decorations: settings.decorations, - transparent: settings.transparent, - always_on_top: settings.always_on_top, - icon: settings.icon.and_then(|icon| { - Icon::try_from(icon).map(winit::window::Icon::from).ok() - }), - platform_specific: Default::default(), - } - } -} diff --git a/winit/src/settings/windows.rs b/winit/src/settings/windows.rs index ff03a9c5..45d753bd 100644 --- a/winit/src/settings/windows.rs +++ b/winit/src/settings/windows.rs @@ -1,10 +1,11 @@ //! 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<winit::platform::windows::HWND>, + pub parent: Option<RawWindowHandle>, /// Drag and drop support pub drag_and_drop: bool, diff --git a/winit/src/system.rs b/winit/src/system.rs index 8d8b018c..145a4d92 100644 --- a/winit/src/system.rs +++ b/winit/src/system.rs @@ -1,8 +1,7 @@ //! Access the native system. -use crate::command::{self, Command}; -pub use iced_native::system::*; - -use iced_graphics::compositor; +use crate::graphics::compositor; +use crate::runtime::command::{self, Command}; +use crate::runtime::system::{Action, Information}; /// Query for available system information. pub fn fetch_information<Message>( diff --git a/winit/src/window.rs b/winit/src/window.rs index 8fd415ef..e69de29b 100644 --- a/winit/src/window.rs +++ b/winit/src/window.rs @@ -1,128 +0,0 @@ -//! Interact with the window of your application. -use crate::command::{self, Command}; -use iced_native::window; - -pub use window::{Event, Id, Mode, RedrawRequest, frames, UserAttention}; - -/// Closes the window. -pub fn close<Message>(id: window::Id) -> Command<Message> { - Command::single(command::Action::Window(id, window::Action::Close)) -} - -/// Begins dragging the window while the left mouse button is held. -pub fn drag<Message>(id: window::Id) -> Command<Message> { - Command::single(command::Action::Window(id, window::Action::Drag)) -} - -/// Spawns a new window. -pub fn spawn<Message>( - id: window::Id, - settings: window::Settings, -) -> Command<Message> { - Command::single(command::Action::Window( - id, - window::Action::Spawn { settings }, - )) -} - -/// Resizes the window to the given logical dimensions. -pub fn resize<Message>( - id: window::Id, - width: u32, - height: u32, -) -> Command<Message> { - Command::single(command::Action::Window( - id, - window::Action::Resize { width, height }, - )) -} - -/// Maximizes the window. -pub fn maximize<Message>(id: window::Id, maximized: bool) -> Command<Message> { - Command::single(command::Action::Window( - id, - window::Action::Maximize(maximized), - )) -} - -/// Minimes the window. -pub fn minimize<Message>(id: window::Id, minimized: bool) -> Command<Message> { - Command::single(command::Action::Window( - id, - window::Action::Minimize(minimized), - )) -} - -/// Moves a window to the given logical coordinates. -pub fn move_to<Message>(id: window::Id, x: i32, y: i32) -> Command<Message> { - Command::single(command::Action::Window(id, window::Action::Move { x, y })) -} - -/// Changes the [`Mode`] of the window. -pub fn change_mode<Message>(id: window::Id, mode: Mode) -> Command<Message> { - Command::single(command::Action::Window(id, window::Action::ChangeMode(mode))) -} - -/// Fetches the current [`Mode`] of the window. -pub fn fetch_mode<Message>( - id: window::Id, - f: impl FnOnce(Mode) -> Message + 'static, -) -> Command<Message> { - Command::single(command::Action::Window( - id, - window::Action::FetchMode(Box::new(f)), - )) -} - -/// Toggles the window to maximized or back. -pub fn toggle_maximize<Message>(id: window::Id) -> Command<Message> { - Command::single(command::Action::Window(id, window::Action::ToggleMaximize)) -} - -/// Toggles the window decorations. -pub fn toggle_decorations<Message>(id: window::Id) -> Command<Message> { - Command::single(command::Action::Window(id, window::Action::ToggleDecorations)) -} - -/// Request user attention to the window, this has no effect if the application -/// is already focused. How requesting for user attention manifests is platform dependent, -/// see [`UserAttention`] for details. -/// -/// Providing `None` will unset the request for user attention. Unsetting the request for -/// user attention might not be done automatically by the WM when the window receives input. -pub fn request_user_attention<Message>( - id: window::Id, - user_attention: Option<UserAttention>, -) -> Command<Message> { - Command::single(command::Action::Window( - id, - window::Action::RequestUserAttention(user_attention), - )) -} - -/// Brings the window to the front and sets input focus. Has no effect if the window is -/// already in focus, minimized, or not visible. -/// -/// This [`Command`] steals input focus from other applications. Do not use this method unless -/// you are certain that's what the user wants. Focus stealing can cause an extremely disruptive -/// user experience. -pub fn gain_focus<Message>(id: window::Id) -> Command<Message> { - Command::single(command::Action::Window(id, window::Action::GainFocus)) -} - -/// Changes whether or not the window will always be on top of other windows. -pub fn change_always_on_top<Message>(id: window::Id, on_top: bool) -> Command<Message> { - Command::single(command::Action::Window(id, window::Action::ChangeAlwaysOnTop( - on_top, - ))) -} - -/// Fetches an identifier unique to the window. -pub fn fetch_id<Message>( - id: window::Id, - f: impl FnOnce(u64) -> Message + 'static, -) -> Command<Message> { - Command::single(command::Action::Window(id, window::Action::FetchId(Box::new( - f, - )))) -} |