From e19a07d40049f40f36d879a498fab4ce63778b27 Mon Sep 17 00:00:00 2001 From: Sebastian Imlay Date: Mon, 11 Nov 2019 20:29:58 -0800 Subject: Added initial touch events to support iOS --- winit/src/application.rs | 1 - winit/src/conversion.rs | 26 +++++++++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) (limited to 'winit') diff --git a/winit/src/application.rs b/winit/src/application.rs index 891b8f12..65bffd5b 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -310,7 +310,6 @@ pub trait Application: Sized { physical_size.width, physical_size.height, ); - resized = false; } diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index b6a0b64b..2fc76c9d 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -5,7 +5,7 @@ use crate::{ input::{ keyboard::{self, KeyCode, ModifiersState}, - mouse, ButtonState, + mouse, touch, ButtonState, }, window, Event, Mode, MouseCursor, }; @@ -84,6 +84,9 @@ pub fn window_event( WindowEvent::HoveredFileCancelled => { Some(Event::Window(window::Event::FilesHoveredLeft)) } + WindowEvent::Touch(touch) => { + Some(Event::Touch(touch_event(touch))) + } _ => None, } } @@ -342,3 +345,24 @@ pub(crate) fn is_private_use_character(c: char) -> bool { _ => false, } } +pub(crate) fn touch_event(touch: winit::event::Touch) -> touch::Touch { + let location = touch.location; + match touch.phase { + winit::event::TouchPhase::Started => touch::Touch::Started { + x: location.x as f32, + y: location.y as f32, + }, + winit::event::TouchPhase::Ended => touch::Touch::Ended { + x: location.x as f32, + y: location.y as f32, + }, + winit::event::TouchPhase::Moved => touch::Touch::Moved { + x: location.x as f32, + y: location.y as f32, + }, + winit::event::TouchPhase::Cancelled => touch::Touch::Cancelled { + x: location.x as f32, + y: location.y as f32, + }, + } +} -- cgit From d3572e1b819ff4d40de4f39f48eab71b9d0d4d5e Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 19 Mar 2020 12:17:16 +0100 Subject: Turn `Touch` into a struct and add finger id --- winit/src/conversion.rs | 51 ++++++++++++++++++++++--------------------------- 1 file changed, 23 insertions(+), 28 deletions(-) (limited to 'winit') diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index 2fc76c9d..1d008d05 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -5,9 +5,9 @@ use crate::{ input::{ keyboard::{self, KeyCode, ModifiersState}, - mouse, touch, ButtonState, + mouse, touch, ButtonState, Touch, }, - window, Event, Mode, MouseCursor, + window, Event, Mode, MouseCursor, Point, }; /// Converts a winit window event into an iced event. @@ -31,8 +31,7 @@ pub fn window_event( let position = position.to_logical::(scale_factor); Some(Event::Mouse(mouse::Event::CursorMoved { - x: position.x as f32, - y: position.y as f32, + position: Point::new(position.x as f32, position.y as f32), })) } WindowEvent::MouseInput { button, state, .. } => { @@ -84,9 +83,7 @@ pub fn window_event( WindowEvent::HoveredFileCancelled => { Some(Event::Window(window::Event::FilesHoveredLeft)) } - WindowEvent::Touch(touch) => { - Some(Event::Touch(touch_event(touch))) - } + WindowEvent::Touch(touch) => Some(Event::Touch(touch_event(touch))), _ => None, } } @@ -162,6 +159,25 @@ pub fn modifiers_state( } } +/// Converts a `Touch` from [`winit`] to an [`iced_native`] touch event. +/// +/// [`winit`]: https://github.com/rust-windowing/winit +/// [`iced_native`]: https://github.com/hecrj/iced/tree/master/native +pub fn touch_event(touch: winit::event::Touch) -> Touch { + let phase = match touch.phase { + winit::event::TouchPhase::Started => touch::Phase::Started, + winit::event::TouchPhase::Moved => touch::Phase::Moved, + winit::event::TouchPhase::Ended => touch::Phase::Ended, + winit::event::TouchPhase::Cancelled => touch::Phase::Canceled, + }; + + Touch { + finger: touch::Finger(touch.id), + position: Point::new(touch.location.x as f32, touch.location.y as f32), + phase, + } +} + /// Converts a `VirtualKeyCode` from [`winit`] to an [`iced_native`] key code. /// /// [`winit`]: https://github.com/rust-windowing/winit @@ -345,24 +361,3 @@ pub(crate) fn is_private_use_character(c: char) -> bool { _ => false, } } -pub(crate) fn touch_event(touch: winit::event::Touch) -> touch::Touch { - let location = touch.location; - match touch.phase { - winit::event::TouchPhase::Started => touch::Touch::Started { - x: location.x as f32, - y: location.y as f32, - }, - winit::event::TouchPhase::Ended => touch::Touch::Ended { - x: location.x as f32, - y: location.y as f32, - }, - winit::event::TouchPhase::Moved => touch::Touch::Moved { - x: location.x as f32, - y: location.y as f32, - }, - winit::event::TouchPhase::Cancelled => touch::Touch::Cancelled { - x: location.x as f32, - y: location.y as f32, - }, - } -} -- cgit From 3bdf931925067acbaabf040f6c437a54640ed1a0 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 15 Dec 2020 06:38:46 +0100 Subject: Turn `Touch` into a `touch::Event` enum --- winit/src/conversion.rs | 50 +++++++++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 20 deletions(-) (limited to 'winit') diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index e6fc4b96..7a6ab2de 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -2,12 +2,11 @@ //! //! [`winit`]: https://github.com/rust-windowing/winit //! [`iced_native`]: https://github.com/hecrj/iced/tree/master/native -use crate::{ - keyboard::{self, KeyCode, Modifiers}, - mouse, - touch::{self, Touch}, - window, Event, Mode, Point, -}; +use crate::keyboard; +use crate::mouse; +use crate::touch; +use crate::window; +use crate::{Event, Mode, Point}; /// Converts a winit window event into an iced event. pub fn window_event( @@ -183,8 +182,10 @@ pub fn mouse_button(mouse_button: winit::event::MouseButton) -> mouse::Button { /// /// [`winit`]: https://github.com/rust-windowing/winit /// [`iced_native`]: https://github.com/hecrj/iced/tree/master/native -pub fn modifiers(modifiers: winit::event::ModifiersState) -> Modifiers { - Modifiers { +pub fn modifiers( + modifiers: winit::event::ModifiersState, +) -> keyboard::Modifiers { + keyboard::Modifiers { shift: modifiers.shift(), control: modifiers.ctrl(), alt: modifiers.alt(), @@ -206,18 +207,23 @@ pub fn cursor_position( /// /// [`winit`]: https://github.com/rust-windowing/winit /// [`iced_native`]: https://github.com/hecrj/iced/tree/master/native -pub fn touch_event(touch: winit::event::Touch) -> Touch { - let phase = match touch.phase { - winit::event::TouchPhase::Started => touch::Phase::Started, - winit::event::TouchPhase::Moved => touch::Phase::Moved, - winit::event::TouchPhase::Ended => touch::Phase::Ended, - winit::event::TouchPhase::Cancelled => touch::Phase::Canceled, - }; +pub fn touch_event(touch: winit::event::Touch) -> touch::Event { + let id = touch::Finger(touch.id); + let position = Point::new(touch.location.x as f32, touch.location.y as f32); - Touch { - finger: touch::Finger(touch.id), - position: Point::new(touch.location.x as f32, touch.location.y as f32), - phase, + match touch.phase { + winit::event::TouchPhase::Started => { + touch::Event::FingerPressed { id, position } + } + winit::event::TouchPhase::Moved => { + touch::Event::FingerMoved { id, position } + } + winit::event::TouchPhase::Ended => { + touch::Event::FingerLifted { id, position } + } + winit::event::TouchPhase::Cancelled => { + touch::Event::FingerLost { id, position } + } } } @@ -225,7 +231,11 @@ pub fn touch_event(touch: winit::event::Touch) -> Touch { /// /// [`winit`]: https://github.com/rust-windowing/winit /// [`iced_native`]: https://github.com/hecrj/iced/tree/master/native -pub fn key_code(virtual_keycode: winit::event::VirtualKeyCode) -> KeyCode { +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, -- cgit From 056f7e6951f6e96da82f35626b8920f6853bea43 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 15 Dec 2020 06:44:00 +0100 Subject: Change cursor position on touch event --- winit/src/application/state.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'winit') diff --git a/winit/src/application/state.rs b/winit/src/application/state.rs index 58bc7ed6..46297370 100644 --- a/winit/src/application/state.rs +++ b/winit/src/application/state.rs @@ -2,7 +2,7 @@ use crate::conversion; use crate::{Application, Color, Debug, Mode, Point, Size, Viewport}; use std::marker::PhantomData; -use winit::event::WindowEvent; +use winit::event::{Touch, WindowEvent}; use winit::window::Window; /// The state of a windowed [`Application`]. @@ -128,7 +128,10 @@ impl State { self.viewport_version = self.viewport_version.wrapping_add(1); } - WindowEvent::CursorMoved { position, .. } => { + WindowEvent::CursorMoved { position, .. } + | WindowEvent::Touch(Touch { + location: position, .. + }) => { self.cursor_position = *position; } WindowEvent::CursorLeft { .. } => { -- cgit From 277ae74d6817ee6192b5f7a44de19dcbdf8d58f7 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 15 Dec 2020 06:58:15 +0100 Subject: Produce logical coordinates for `touch::Event` --- winit/src/conversion.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'winit') diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index 7a6ab2de..f073c474 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -118,7 +118,9 @@ pub fn window_event( WindowEvent::HoveredFileCancelled => { Some(Event::Window(window::Event::FilesHoveredLeft)) } - WindowEvent::Touch(touch) => Some(Event::Touch(touch_event(*touch))), + WindowEvent::Touch(touch) => { + Some(Event::Touch(touch_event(*touch, scale_factor))) + } _ => None, } } @@ -207,9 +209,16 @@ pub fn cursor_position( /// /// [`winit`]: https://github.com/rust-windowing/winit /// [`iced_native`]: https://github.com/hecrj/iced/tree/master/native -pub fn touch_event(touch: winit::event::Touch) -> touch::Event { +pub fn touch_event( + touch: winit::event::Touch, + scale_factor: f64, +) -> touch::Event { let id = touch::Finger(touch.id); - let position = Point::new(touch.location.x as f32, touch.location.y as f32); + let position = { + let location = touch.location.to_logical::(scale_factor); + + Point::new(location.x as f32, location.y as f32) + }; match touch.phase { winit::event::TouchPhase::Started => { -- cgit From 0b140488b425a7d1fd45ca41592de25b28d3ac17 Mon Sep 17 00:00:00 2001 From: cossonleo Date: Fri, 15 Jan 2021 22:40:16 +0800 Subject: add focus event --- winit/src/conversion.rs | 3 +++ 1 file changed, 3 insertions(+) (limited to 'winit') diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index f073c474..5265b844 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -121,6 +121,9 @@ pub fn window_event( WindowEvent::Touch(touch) => { Some(Event::Touch(touch_event(*touch, scale_factor))) } + WindowEvent::Focused(focused) => { + Some(Event::Window(window::Event::Focused(*focused))) + } _ => None, } } -- cgit From 45dc02e9bd0b4f2c6cc65781b850f460cddf6171 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 15 Jan 2021 18:21:44 +0100 Subject: Split `window::Event::Focused` into two variants --- winit/src/conversion.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'winit') diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index 5265b844..0e04b35d 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -109,6 +109,11 @@ pub fn window_event( WindowEvent::ModifiersChanged(new_modifiers) => Some(Event::Keyboard( keyboard::Event::ModifiersChanged(self::modifiers(*new_modifiers)), )), + 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()))) } @@ -121,9 +126,6 @@ pub fn window_event( WindowEvent::Touch(touch) => { Some(Event::Touch(touch_event(*touch, scale_factor))) } - WindowEvent::Focused(focused) => { - Some(Event::Window(window::Event::Focused(*focused))) - } _ => None, } } -- cgit From b22b0dd7ff56d433c459e0d14e14eb5472a6224d Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 10 Mar 2021 01:16:26 +0100 Subject: Update `window_clipboard` to `0.2` --- winit/Cargo.toml | 2 +- winit/src/application.rs | 2 +- winit/src/clipboard.rs | 8 +++++--- 3 files changed, 7 insertions(+), 5 deletions(-) (limited to 'winit') diff --git a/winit/Cargo.toml b/winit/Cargo.toml index 39a6a5fa..ecee0e2e 100644 --- a/winit/Cargo.toml +++ b/winit/Cargo.toml @@ -15,7 +15,7 @@ debug = ["iced_native/debug"] [dependencies] winit = "0.24" -window_clipboard = "0.1" +window_clipboard = "0.2" log = "0.4" thiserror = "1.0" diff --git a/winit/src/application.rs b/winit/src/application.rs index d1a94864..6f8cfc22 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -194,7 +194,7 @@ async fn run_instance( use winit::event; let surface = compositor.create_surface(&window); - let clipboard = Clipboard::new(&window); + let clipboard = Clipboard::connect(&window); let mut state = State::new(&application, &window); let mut viewport_version = state.viewport_version(); diff --git a/winit/src/clipboard.rs b/winit/src/clipboard.rs index 93d53b11..cce2b371 100644 --- a/winit/src/clipboard.rs +++ b/winit/src/clipboard.rs @@ -5,13 +5,15 @@ pub struct Clipboard(window_clipboard::Clipboard); impl Clipboard { /// Creates a new [`Clipboard`] for the given window. - pub fn new(window: &winit::window::Window) -> Option { - window_clipboard::Clipboard::new(window).map(Clipboard).ok() + pub fn connect(window: &winit::window::Window) -> Option { + window_clipboard::Clipboard::connect(window) + .map(Clipboard) + .ok() } } impl iced_native::Clipboard for Clipboard { - fn content(&self) -> Option { + fn read(&self) -> Option { self.0.read().ok() } } -- cgit From 35425001edcb54d861a42ec6d23f9e57b37745fd Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 10 Mar 2021 01:18:22 +0100 Subject: Introduce `write` method to `Clipboard` trait --- winit/src/clipboard.rs | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'winit') diff --git a/winit/src/clipboard.rs b/winit/src/clipboard.rs index cce2b371..06e7c1a8 100644 --- a/winit/src/clipboard.rs +++ b/winit/src/clipboard.rs @@ -16,4 +16,11 @@ impl iced_native::Clipboard for Clipboard { fn read(&self) -> Option { self.0.read().ok() } + + fn write(&mut self, contents: String) { + match self.0.write(contents) { + Ok(()) => {} + Err(error) => log::warn!("error writing to clipboard: {}", error), + } + } } -- cgit From 21971e0037c2ddcb96fd48ea96332445de4137bb Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 10 Mar 2021 01:59:02 +0100 Subject: Make `Clipboard` argument in `Widget` trait mutable --- winit/src/application.rs | 4 ++-- winit/src/clipboard.rs | 34 ++++++++++++++++++++++++++-------- 2 files changed, 28 insertions(+), 10 deletions(-) (limited to 'winit') diff --git a/winit/src/application.rs b/winit/src/application.rs index 6f8cfc22..2cb2c016 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -194,7 +194,7 @@ async fn run_instance( use winit::event; let surface = compositor.create_surface(&window); - let clipboard = Clipboard::connect(&window); + let mut clipboard = Clipboard::connect(&window); let mut state = State::new(&application, &window); let mut viewport_version = state.viewport_version(); @@ -237,8 +237,8 @@ async fn run_instance( let statuses = user_interface.update( &events, state.cursor_position(), - clipboard.as_ref().map(|c| c as _), &mut renderer, + &mut clipboard, &mut messages, ); diff --git a/winit/src/clipboard.rs b/winit/src/clipboard.rs index 06e7c1a8..15dc989f 100644 --- a/winit/src/clipboard.rs +++ b/winit/src/clipboard.rs @@ -1,26 +1,44 @@ /// A buffer for short-term storage and transfer within and between /// applications. #[allow(missing_debug_implementations)] -pub struct Clipboard(window_clipboard::Clipboard); +pub struct Clipboard { + state: State, +} + +enum State { + Connected(window_clipboard::Clipboard), + Unavailable, +} impl Clipboard { /// Creates a new [`Clipboard`] for the given window. - pub fn connect(window: &winit::window::Window) -> Option { - window_clipboard::Clipboard::connect(window) - .map(Clipboard) + pub fn connect(window: &winit::window::Window) -> Clipboard { + let state = window_clipboard::Clipboard::connect(window) .ok() + .map(State::Connected) + .unwrap_or(State::Unavailable); + + Clipboard { state } } } impl iced_native::Clipboard for Clipboard { fn read(&self) -> Option { - self.0.read().ok() + match &self.state { + State::Connected(clipboard) => clipboard.read().ok(), + State::Unavailable => None, + } } fn write(&mut self, contents: String) { - match self.0.write(contents) { - Ok(()) => {} - Err(error) => log::warn!("error writing to clipboard: {}", error), + match &mut self.state { + State::Connected(clipboard) => match clipboard.write(contents) { + Ok(()) => {} + Err(error) => { + log::warn!("error writing to clipboard: {}", error) + } + }, + State::Unavailable => {} } } } -- cgit From ae517b9fa033ba75df5fc6ce766698fab22504fa Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 11 Mar 2021 03:38:20 +0100 Subject: Add `clipboard` argument to `Application::update` --- winit/src/application.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'winit') diff --git a/winit/src/application.rs b/winit/src/application.rs index 2cb2c016..d7d7660e 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -29,7 +29,7 @@ use std::mem::ManuallyDrop; /// /// When using an [`Application`] with the `debug` feature enabled, a debug view /// can be toggled by pressing `F12`. -pub trait Application: Program { +pub trait Application: Program { /// The data needed to initialize your [`Application`]. type Flags; @@ -257,6 +257,7 @@ async fn run_instance( &mut application, &mut runtime, &mut debug, + &mut clipboard, &mut messages, ); @@ -409,13 +410,14 @@ pub fn update( application: &mut A, runtime: &mut Runtime, A::Message>, debug: &mut Debug, + clipboard: &mut A::Clipboard, messages: &mut Vec, ) { for message in messages.drain(..) { debug.log_message(&message); debug.update_started(); - let command = runtime.enter(|| application.update(message)); + let command = runtime.enter(|| application.update(message, clipboard)); debug.update_finished(); runtime.spawn(command); -- cgit From a365998264420b1dac26a0de8e50ad4a33900885 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 11 Mar 2021 03:40:15 +0100 Subject: Expose `read` and `write` methods in `iced_winit::Clipboard` directly --- winit/src/clipboard.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'winit') diff --git a/winit/src/clipboard.rs b/winit/src/clipboard.rs index 15dc989f..ca25c065 100644 --- a/winit/src/clipboard.rs +++ b/winit/src/clipboard.rs @@ -20,17 +20,17 @@ impl Clipboard { Clipboard { state } } -} -impl iced_native::Clipboard for Clipboard { - fn read(&self) -> Option { + /// Reads the current content of the [`Clipboard`] as text. + pub fn read(&self) -> Option { match &self.state { State::Connected(clipboard) => clipboard.read().ok(), State::Unavailable => None, } } - fn write(&mut self, contents: String) { + /// Writes the given text contents to the [`Clipboard`]. + pub fn write(&mut self, contents: String) { match &mut self.state { State::Connected(clipboard) => match clipboard.write(contents) { Ok(()) => {} @@ -42,3 +42,13 @@ impl iced_native::Clipboard for Clipboard { } } } + +impl iced_native::Clipboard for Clipboard { + fn read(&self) -> Option { + self.read() + } + + fn write(&mut self, contents: String) { + self.write(contents) + } +} -- cgit From d66a34b27272ed9d6c143483b0a700468b00e311 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 24 Mar 2021 04:59:13 +0100 Subject: Convert `ScaleFactorChanged` into `Resized` events in `iced_winit` ... instead of just dropping them when calling `to_static`. --- winit/src/application.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'winit') diff --git a/winit/src/application.rs b/winit/src/application.rs index d7d7660e..5aefb378 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -164,7 +164,22 @@ where return; } - if let Some(event) = event.to_static() { + 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(), + }; + + if let Some(event) = event { sender.start_send(event).expect("Send event"); let poll = instance.as_mut().poll(&mut context); -- cgit From 1207afa7d0862b0fa06b8dd9dd01acb03e49acec Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 26 Mar 2021 14:46:19 +0100 Subject: Skip redrawing if window has no surface --- winit/src/application.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'winit') diff --git a/winit/src/application.rs b/winit/src/application.rs index 5aefb378..ef6c8463 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -299,11 +299,16 @@ async fn run_instance( messages.push(message); } event::Event::RedrawRequested(_) => { + 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 physical_size = state.physical_size(); let logical_size = state.logical_size(); debug.layout_started(); -- cgit From 00de9d0c9ba20b313ffb459ed291ea2b85e53d32 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 30 Mar 2021 21:33:57 +0200 Subject: Add `CloseRequested` variant to `window::Event` --- winit/src/conversion.rs | 3 +++ 1 file changed, 3 insertions(+) (limited to 'winit') diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index 0e04b35d..0fa27413 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -33,6 +33,9 @@ pub fn window_event( height: logical_size.height, })) } + WindowEvent::CloseRequested => { + Some(Event::Window(window::Event::CloseRequested)) + } WindowEvent::CursorMoved { position, .. } => { let position = position.to_logical::(scale_factor); -- cgit From 67db13ff7c727254182a8c4474bd962b205e2e99 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 30 Mar 2021 21:44:19 +0200 Subject: Add support for graceful exits in `Application` - `Settings` now contains an `exit_on_close_request` field - `Application` has a new `should_exit` method --- winit/src/application.rs | 23 ++++++++++++++++++++--- winit/src/settings.rs | 4 ++++ 2 files changed, 24 insertions(+), 3 deletions(-) (limited to 'winit') diff --git a/winit/src/application.rs b/winit/src/application.rs index ef6c8463..106d5218 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -91,6 +91,13 @@ pub trait Application: Program { fn scale_factor(&self) -> f64 { 1.0 } + + /// Returns whether the [`Application`] should be terminated. + /// + /// By default, it returns `false`. + fn should_exit(&self) -> bool { + false + } } /// Runs an [`Application`] with an executor, compositor, and the provided @@ -149,10 +156,11 @@ where application, compositor, renderer, - window, runtime, debug, receiver, + window, + settings.exit_on_close_request, )); let mut context = task::Context::from_waker(task::noop_waker_ref()); @@ -196,10 +204,11 @@ async fn run_instance( mut application: A, mut compositor: C, mut renderer: A::Renderer, - window: winit::window::Window, mut runtime: Runtime, A::Message>, mut debug: Debug, mut receiver: mpsc::UnboundedReceiver>, + window: winit::window::Window, + exit_on_close_request: bool, ) where A: Application + 'static, E: Executor + 'static, @@ -279,6 +288,8 @@ async fn run_instance( // Update window state.synchronize(&application, &window); + let should_exit = application.should_exit(); + user_interface = ManuallyDrop::new(build_user_interface( &mut application, cache, @@ -286,6 +297,10 @@ async fn run_instance( state.logical_size(), &mut debug, )); + + if should_exit { + break; + } } debug.draw_started(); @@ -358,7 +373,9 @@ async fn run_instance( event: window_event, .. } => { - if requests_exit(&window_event, state.modifiers()) { + if requests_exit(&window_event, state.modifiers()) + && exit_on_close_request + { break; } diff --git a/winit/src/settings.rs b/winit/src/settings.rs index 2e8715cd..9ce5cfc5 100644 --- a/winit/src/settings.rs +++ b/winit/src/settings.rs @@ -23,6 +23,10 @@ pub struct Settings { /// /// [`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). + pub exit_on_close_request: bool, } /// The window settings of an application. -- cgit From 0864e63bde129b95261590b960efdc46c6d2d4d0 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 31 Mar 2021 20:06:03 +0200 Subject: Bump versions :tada: --- winit/Cargo.toml | 8 ++++---- winit/README.md | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'winit') diff --git a/winit/Cargo.toml b/winit/Cargo.toml index ecee0e2e..7f117fa1 100644 --- a/winit/Cargo.toml +++ b/winit/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "iced_winit" -version = "0.2.0" +version = "0.3.0" authors = ["Héctor Ramón Jiménez "] edition = "2018" description = "A winit runtime for Iced" @@ -20,15 +20,15 @@ log = "0.4" thiserror = "1.0" [dependencies.iced_native] -version = "0.3" +version = "0.4" path = "../native" [dependencies.iced_graphics] -version = "0.1" +version = "0.2" path = "../graphics" [dependencies.iced_futures] -version = "0.2" +version = "0.3" path = "../futures" [target.'cfg(target_os = "windows")'.dependencies.winapi] diff --git a/winit/README.md b/winit/README.md index 721baa14..58c782c6 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.2" +iced_winit = "0.3" ``` __Iced moves fast and the `master` branch can contain breaking changes!__ If -- cgit From cdab8f90fb525c509e0a15bb1d0b3d7213e176f3 Mon Sep 17 00:00:00 2001 From: Cory Forsstrom Date: Thu, 8 Apr 2021 12:58:08 -0700 Subject: add window visibility --- winit/src/application.rs | 8 ++++++++ winit/src/application/state.rs | 12 ++++++++++++ winit/src/settings.rs | 4 +++- 3 files changed, 23 insertions(+), 1 deletion(-) (limited to 'winit') diff --git a/winit/src/application.rs b/winit/src/application.rs index 106d5218..e73f3df1 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -98,6 +98,13 @@ pub trait Application: Program { fn should_exit(&self) -> bool { false } + + /// Returns whether the [`Application`] should be visible or not + /// + /// By default, it returns `true`. + fn visible(&self) -> bool { + true + } } /// Runs an [`Application`] with an executor, compositor, and the provided @@ -145,6 +152,7 @@ where .into_builder( &application.title(), application.mode(), + application.visible(), event_loop.primary_monitor(), ) .build(&event_loop) diff --git a/winit/src/application/state.rs b/winit/src/application/state.rs index 46297370..d157211a 100644 --- a/winit/src/application/state.rs +++ b/winit/src/application/state.rs @@ -12,6 +12,7 @@ pub struct State { mode: Mode, background_color: Color, scale_factor: f64, + visible: bool, viewport: Viewport, viewport_version: usize, cursor_position: winit::dpi::PhysicalPosition, @@ -26,6 +27,7 @@ impl State { let mode = application.mode(); let background_color = application.background_color(); let scale_factor = application.scale_factor(); + let visible = application.visible(); let viewport = { let physical_size = window.inner_size(); @@ -41,6 +43,7 @@ impl State { mode, background_color, scale_factor, + visible, viewport, viewport_version: 0, // TODO: Encode cursor availability in the type-system @@ -201,5 +204,14 @@ impl State { self.scale_factor = new_scale_factor; } + + // Update window visibility + let new_visible = application.visible(); + + if self.visible != new_visible { + window.set_visible(new_visible); + + self.visible = new_visible; + } } } diff --git a/winit/src/settings.rs b/winit/src/settings.rs index 9ce5cfc5..56be1e07 100644 --- a/winit/src/settings.rs +++ b/winit/src/settings.rs @@ -66,6 +66,7 @@ impl Window { self, title: &str, mode: Mode, + visible: bool, primary_monitor: Option, ) -> WindowBuilder { let mut window_builder = WindowBuilder::new(); @@ -80,7 +81,8 @@ impl Window { .with_transparent(self.transparent) .with_window_icon(self.icon) .with_always_on_top(self.always_on_top) - .with_fullscreen(conversion::fullscreen(primary_monitor, mode)); + .with_fullscreen(conversion::fullscreen(primary_monitor, mode)) + .with_visible(visible); if let Some((width, height)) = self.min_size { window_builder = window_builder -- cgit From 84c0c9bc7ab858793183560739c8fd6087e22f6e Mon Sep 17 00:00:00 2001 From: Cory Forsstrom Date: Fri, 9 Apr 2021 09:00:29 -0700 Subject: use Mode::Hidden instead --- winit/src/application.rs | 8 -------- winit/src/application/state.rs | 14 ++------------ winit/src/conversion.rs | 10 +++++++++- winit/src/mode.rs | 3 +++ winit/src/settings.rs | 3 +-- 5 files changed, 15 insertions(+), 23 deletions(-) (limited to 'winit') diff --git a/winit/src/application.rs b/winit/src/application.rs index e73f3df1..106d5218 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -98,13 +98,6 @@ pub trait Application: Program { fn should_exit(&self) -> bool { false } - - /// Returns whether the [`Application`] should be visible or not - /// - /// By default, it returns `true`. - fn visible(&self) -> bool { - true - } } /// Runs an [`Application`] with an executor, compositor, and the provided @@ -152,7 +145,6 @@ where .into_builder( &application.title(), application.mode(), - application.visible(), event_loop.primary_monitor(), ) .build(&event_loop) diff --git a/winit/src/application/state.rs b/winit/src/application/state.rs index d157211a..b54d3aed 100644 --- a/winit/src/application/state.rs +++ b/winit/src/application/state.rs @@ -12,7 +12,6 @@ pub struct State { mode: Mode, background_color: Color, scale_factor: f64, - visible: bool, viewport: Viewport, viewport_version: usize, cursor_position: winit::dpi::PhysicalPosition, @@ -27,7 +26,6 @@ impl State { let mode = application.mode(); let background_color = application.background_color(); let scale_factor = application.scale_factor(); - let visible = application.visible(); let viewport = { let physical_size = window.inner_size(); @@ -43,7 +41,6 @@ impl State { mode, background_color, scale_factor, - visible, viewport, viewport_version: 0, // TODO: Encode cursor availability in the type-system @@ -185,6 +182,8 @@ impl State { new_mode, )); + window.set_visible(conversion::visible(new_mode)); + self.mode = new_mode; } @@ -204,14 +203,5 @@ impl State { self.scale_factor = new_scale_factor; } - - // Update window visibility - let new_visible = application.visible(); - - if self.visible != new_visible { - window.set_visible(new_visible); - - self.visible = new_visible; - } } } diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index 0fa27413..b850a805 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -141,13 +141,21 @@ pub fn fullscreen( mode: Mode, ) -> Option { match mode { - Mode::Windowed => None, + Mode::Windowed | Mode::Hidden => None, Mode::Fullscreen => { Some(winit::window::Fullscreen::Borderless(monitor)) } } } +/// Converts a [`Mode`] to a visibility flag. +pub fn visible(mode: Mode) -> bool { + match mode { + Mode::Windowed | Mode::Fullscreen => true, + Mode::Hidden => false, + } +} + /// Converts a `MouseCursor` from [`iced_native`] to a [`winit`] cursor icon. /// /// [`winit`]: https://github.com/rust-windowing/winit diff --git a/winit/src/mode.rs b/winit/src/mode.rs index 37464711..fdce8e23 100644 --- a/winit/src/mode.rs +++ b/winit/src/mode.rs @@ -6,4 +6,7 @@ pub enum Mode { /// The application takes the whole screen of its current monitor. Fullscreen, + + /// The application is hidden + Hidden, } diff --git a/winit/src/settings.rs b/winit/src/settings.rs index 56be1e07..663fa07a 100644 --- a/winit/src/settings.rs +++ b/winit/src/settings.rs @@ -66,7 +66,6 @@ impl Window { self, title: &str, mode: Mode, - visible: bool, primary_monitor: Option, ) -> WindowBuilder { let mut window_builder = WindowBuilder::new(); @@ -82,7 +81,7 @@ impl Window { .with_window_icon(self.icon) .with_always_on_top(self.always_on_top) .with_fullscreen(conversion::fullscreen(primary_monitor, mode)) - .with_visible(visible); + .with_visible(conversion::visible(mode)); if let Some((width, height)) = self.min_size { window_builder = window_builder -- cgit From 3840b75beaa3925f3438a7b40f01aaac221b8206 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Date: Wed, 5 May 2021 14:33:03 +0700 Subject: Provide `compatible_surface` in `iced_wgpu::Compositor` --- winit/src/application.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'winit') diff --git a/winit/src/application.rs b/winit/src/application.rs index 106d5218..b1d5f418 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -118,8 +118,6 @@ where let mut debug = Debug::new(); debug.startup_started(); - let (compositor, renderer) = C::new(compositor_settings)?; - let event_loop = EventLoop::with_user_event(); let mut runtime = { @@ -150,6 +148,8 @@ where .build(&event_loop) .map_err(Error::WindowCreationFailed)?; + let (compositor, renderer) = C::new(compositor_settings, Some(&window))?; + let (mut sender, receiver) = mpsc::unbounded(); let mut instance = Box::pin(run_instance::( -- cgit From f04bc94b80942857f8cc1e0d39658bd1eb633a06 Mon Sep 17 00:00:00 2001 From: Quentin Boyer Date: Thu, 27 May 2021 14:22:11 +0200 Subject: allow disabling drag and drop on windows --- winit/src/settings.rs | 2 ++ winit/src/settings/windows.rs | 13 ++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) (limited to 'winit') diff --git a/winit/src/settings.rs b/winit/src/settings.rs index 663fa07a..941d88ce 100644 --- a/winit/src/settings.rs +++ b/winit/src/settings.rs @@ -100,6 +100,8 @@ impl Window { if let Some(parent) = self.platform_specific.parent { window_builder = window_builder.with_parent_window(parent); } + window_builder = window_builder + .with_drag_and_drop(self.platform_specific.drag_and_drop); } window_builder diff --git a/winit/src/settings/windows.rs b/winit/src/settings/windows.rs index 76b8d067..68dadefd 100644 --- a/winit/src/settings/windows.rs +++ b/winit/src/settings/windows.rs @@ -2,8 +2,19 @@ //! Platform specific settings for Windows. /// The platform specific window settings of an application. -#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct PlatformSpecific { /// Parent Window pub parent: Option, + /// Drap and Drop support + pub drag_and_drop: bool, +} + +impl Default for PlatformSpecific { + fn default() -> Self { + Self { + parent: None, + drag_and_drop: true, + } + } } -- cgit From 903570846e51df260925c81851d733dce5955b41 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Date: Tue, 1 Jun 2021 18:57:36 +0700 Subject: Fix documentation of `PlatformSpecific` settings --- winit/src/settings/windows.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'winit') diff --git a/winit/src/settings/windows.rs b/winit/src/settings/windows.rs index 68dadefd..fc26acd7 100644 --- a/winit/src/settings/windows.rs +++ b/winit/src/settings/windows.rs @@ -4,9 +4,10 @@ /// The platform specific window settings of an application. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct PlatformSpecific { - /// Parent Window + /// Parent window pub parent: Option, - /// Drap and Drop support + + /// Drag and drop support pub drag_and_drop: bool, } -- cgit From ba51661a2a7aa136d9c9d83f87df52ea60f7b51b Mon Sep 17 00:00:00 2001 From: Imbris Date: Tue, 15 Jun 2021 02:47:54 -0400 Subject: Bump winit to 0.25 --- winit/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'winit') diff --git a/winit/Cargo.toml b/winit/Cargo.toml index 7f117fa1..4bb46029 100644 --- a/winit/Cargo.toml +++ b/winit/Cargo.toml @@ -14,7 +14,7 @@ categories = ["gui"] debug = ["iced_native/debug"] [dependencies] -winit = "0.24" +winit = "0.25" window_clipboard = "0.2" log = "0.4" thiserror = "1.0" -- cgit From 9ae22b58d843d9a39212028478598c19a49bc2e6 Mon Sep 17 00:00:00 2001 From: Richard Date: Wed, 21 Apr 2021 17:52:31 -0300 Subject: Added events for url handling and create example --- winit/src/application.rs | 3 +++ 1 file changed, 3 insertions(+) (limited to 'winit') diff --git a/winit/src/application.rs b/winit/src/application.rs index b1d5f418..9f51ae68 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -310,6 +310,9 @@ async fn run_instance( window.request_redraw(); } + event::Event::ReceivedUrl(url) => { + events.push(iced_native::Event::UrlReceived(url)); + } event::Event::UserEvent(message) => { messages.push(message); } -- cgit From 96a462d2f2cb608ad14c93cc55896108a2dccb2b Mon Sep 17 00:00:00 2001 From: Richard Date: Wed, 9 Jun 2021 15:00:01 -0300 Subject: Use new enum variant and new winit repo --- winit/src/application.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'winit') diff --git a/winit/src/application.rs b/winit/src/application.rs index 9f51ae68..ce57bd1d 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -310,8 +310,9 @@ async fn run_instance( window.request_redraw(); } - event::Event::ReceivedUrl(url) => { - events.push(iced_native::Event::UrlReceived(url)); + event::Event::PlatformSpecific(event::PlatformSpecific::MacOS(event::MacOS::ReceivedUrl(url))) => { + use iced_native::event; + events.push(iced_native::Event::PlatformSpecific(event::PlatformSpecific::MacOS(event::MacOS::ReceivedUrl(url)))); } event::Event::UserEvent(message) => { messages.push(message); -- cgit From 612585109ffc9a14a507c3c8423c6aa790c35cbf Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 14 Jun 2021 17:21:55 -0300 Subject: Use `winit` and `glutin` forks in `iced-rs` org --- winit/Cargo.toml | 6 +++++- winit/src/application.rs | 10 ++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) (limited to 'winit') diff --git a/winit/Cargo.toml b/winit/Cargo.toml index 4bb46029..b926a9c5 100644 --- a/winit/Cargo.toml +++ b/winit/Cargo.toml @@ -14,11 +14,15 @@ categories = ["gui"] debug = ["iced_native/debug"] [dependencies] -winit = "0.25" window_clipboard = "0.2" log = "0.4" thiserror = "1.0" +[dependencies.winit] +version = "0.25" +git = "https://github.com/iced-rs/winit" +rev = "9c358959ed99736566d50a511b03d2fed3aac2ae" + [dependencies.iced_native] version = "0.4" path = "../native" diff --git a/winit/src/application.rs b/winit/src/application.rs index ce57bd1d..49f2f513 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -310,9 +310,15 @@ async fn run_instance( window.request_redraw(); } - event::Event::PlatformSpecific(event::PlatformSpecific::MacOS(event::MacOS::ReceivedUrl(url))) => { + event::Event::PlatformSpecific(event::PlatformSpecific::MacOS( + event::MacOS::ReceivedUrl(url), + )) => { use iced_native::event; - events.push(iced_native::Event::PlatformSpecific(event::PlatformSpecific::MacOS(event::MacOS::ReceivedUrl(url)))); + events.push(iced_native::Event::PlatformSpecific( + event::PlatformSpecific::MacOS(event::MacOS::ReceivedUrl( + url, + )), + )); } event::Event::UserEvent(message) => { messages.push(message); -- cgit From 5c45d36d1a8cfd92cd1a454a7f4deedcd4d13fe7 Mon Sep 17 00:00:00 2001 From: TimUntersberger Date: Fri, 25 Jun 2021 17:16:44 +0200 Subject: wip --- winit/src/settings.rs | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'winit') diff --git a/winit/src/settings.rs b/winit/src/settings.rs index 941d88ce..0508cd9b 100644 --- a/winit/src/settings.rs +++ b/winit/src/settings.rs @@ -35,6 +35,9 @@ pub struct Window { /// The size of the window. pub size: (u32, u32), + /// The position of the window. + pub position: (u32, u32), + /// The minimum size of the window. pub min_size: Option<(u32, u32)>, @@ -75,6 +78,7 @@ impl Window { window_builder = window_builder .with_title(title) .with_inner_size(winit::dpi::LogicalSize { width, height }) + .with_outer_position(self.position) .with_resizable(self.resizable) .with_decorations(self.decorations) .with_transparent(self.transparent) -- cgit From de79a01b88e1610d374ed06077ac78f3e10b9c3d Mon Sep 17 00:00:00 2001 From: TimUntersberger Date: Fri, 25 Jun 2021 18:03:18 +0200 Subject: done --- winit/src/settings.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'winit') diff --git a/winit/src/settings.rs b/winit/src/settings.rs index 0508cd9b..1769c676 100644 --- a/winit/src/settings.rs +++ b/winit/src/settings.rs @@ -36,7 +36,7 @@ pub struct Window { pub size: (u32, u32), /// The position of the window. - pub position: (u32, u32), + pub position: (i32, i32), /// The minimum size of the window. pub min_size: Option<(u32, u32)>, @@ -78,7 +78,7 @@ impl Window { window_builder = window_builder .with_title(title) .with_inner_size(winit::dpi::LogicalSize { width, height }) - .with_outer_position(self.position) + .with_position(winit::dpi::LogicalPosition { x: self.position.0, y: self.position.1 }) .with_resizable(self.resizable) .with_decorations(self.decorations) .with_transparent(self.transparent) @@ -116,6 +116,7 @@ impl Default for Window { fn default() -> Window { Window { size: (1024, 768), + position: (100, 100), min_size: None, max_size: None, resizable: true, -- cgit From 4994d34abab3222f9a8fd7a9a3e63f969ca97ffc Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 30 Jun 2021 23:44:51 +0200 Subject: Update `winit` and `glutin` to latest `master` --- winit/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'winit') diff --git a/winit/Cargo.toml b/winit/Cargo.toml index b926a9c5..c5c6ef70 100644 --- a/winit/Cargo.toml +++ b/winit/Cargo.toml @@ -21,7 +21,7 @@ thiserror = "1.0" [dependencies.winit] version = "0.25" git = "https://github.com/iced-rs/winit" -rev = "9c358959ed99736566d50a511b03d2fed3aac2ae" +rev = "44a9a6fc442fcfa3fa0dfc2d5a2f86fdf4aba10c" [dependencies.iced_native] version = "0.4" -- cgit From 9fc5ad23edca93553137100d167de7b69e88f785 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 5 Jul 2021 16:23:44 -0300 Subject: Initial menu implementation --- winit/Cargo.toml | 4 +- winit/src/application.rs | 6 ++ winit/src/application/state.rs | 2 + winit/src/conversion.rs | 224 ++++++++++++++++++++++++++++++++++++++++- 4 files changed, 233 insertions(+), 3 deletions(-) (limited to 'winit') diff --git a/winit/Cargo.toml b/winit/Cargo.toml index c5c6ef70..87fd23d5 100644 --- a/winit/Cargo.toml +++ b/winit/Cargo.toml @@ -20,8 +20,8 @@ thiserror = "1.0" [dependencies.winit] version = "0.25" -git = "https://github.com/iced-rs/winit" -rev = "44a9a6fc442fcfa3fa0dfc2d5a2f86fdf4aba10c" +# git = "https://github.com/iced-rs/winit" +# rev = "e351421a32bf01b428325dde44dea39ee2656153" [dependencies.iced_native] version = "0.4" diff --git a/winit/src/application.rs b/winit/src/application.rs index 49f2f513..108c6d64 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -14,6 +14,7 @@ use iced_futures::futures; use iced_futures::futures::channel::mpsc; use iced_graphics::window; use iced_native::program::Program; +use iced_native::Menu; use iced_native::{Cache, UserInterface}; use std::mem::ManuallyDrop; @@ -98,6 +99,11 @@ pub trait Application: Program { fn should_exit(&self) -> bool { false } + + /// TODO + fn menu(&self) -> Menu { + Menu::new() + } } /// Runs an [`Application`] with an executor, compositor, and the provided diff --git a/winit/src/application/state.rs b/winit/src/application/state.rs index b54d3aed..2994080c 100644 --- a/winit/src/application/state.rs +++ b/winit/src/application/state.rs @@ -36,6 +36,8 @@ impl State { ) }; + window.set_menu(Some(conversion::menu(application.menu()))); + Self { title, mode, diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index b850a805..da09ac9d 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -6,7 +6,7 @@ use crate::keyboard; use crate::mouse; use crate::touch; use crate::window; -use crate::{Event, Mode, Point}; +use crate::{Event, Menu, MenuEntry, Mode, Point}; /// Converts a winit window event into an iced event. pub fn window_event( @@ -156,6 +156,51 @@ pub fn visible(mode: Mode) -> bool { } } +/// Converts a `Hotkey` from [`iced_native`] to a [`winit`] Hotkey. +/// +/// [`winit`]: https://github.com/rust-windowing/winit +/// [`iced_native`]: https://github.com/hecrj/iced/tree/master/native +fn hotkey(hotkey: keyboard::Hotkey) -> winit::window::Hotkey { + use winit::event::ModifiersState; + + let mut modifiers = ModifiersState::empty(); + modifiers.set(ModifiersState::CTRL, hotkey.modifiers.control); + modifiers.set(ModifiersState::SHIFT, hotkey.modifiers.shift); + modifiers.set(ModifiersState::ALT, hotkey.modifiers.alt); + modifiers.set(ModifiersState::LOGO, hotkey.modifiers.logo); + + winit::window::Hotkey::new(modifiers, to_virtual_keycode(hotkey.key)) +} + +/// Converts a `Menu` from [`iced_native`] to a [`winit`] menu. +/// +/// [`winit`]: https://github.com/rust-windowing/winit +/// [`iced_native`]: https://github.com/hecrj/iced/tree/master/native +pub fn menu(menu: Menu) -> winit::window::Menu { + let mut converted = winit::window::Menu::new(); + + for item in menu.iter() { + match item { + MenuEntry::Item { + content, hotkey, .. + } => { + let hotkey: Option<&keyboard::Hotkey> = hotkey.as_ref().into(); + converted.add_item( + 0, + content, + hotkey.map(|h| self::hotkey(*h)), + ); + } + MenuEntry::Dropdown { content, submenu } => { + converted.add_dropdown(content, self::menu(submenu)); + } + MenuEntry::Separator => converted.add_separator(), + } + } + + converted +} + /// Converts a `MouseCursor` from [`iced_native`] to a [`winit`] cursor icon. /// /// [`winit`]: https://github.com/rust-windowing/winit @@ -252,6 +297,183 @@ pub fn touch_event( } } +/// Converts a `KeyCode` from [`iced_native`] to an [`winit`] key code. +/// +/// [`winit`]: https://github.com/rust-windowing/winit +/// [`iced_native`]: https://github.com/hecrj/iced/tree/master/native +fn to_virtual_keycode( + keycode: keyboard::KeyCode, +) -> winit::event::VirtualKeyCode { + use keyboard::KeyCode; + use winit::event::VirtualKeyCode; + + match keycode { + KeyCode::Key1 => VirtualKeyCode::Key1, + KeyCode::Key2 => VirtualKeyCode::Key2, + KeyCode::Key3 => VirtualKeyCode::Key3, + KeyCode::Key4 => VirtualKeyCode::Key4, + KeyCode::Key5 => VirtualKeyCode::Key5, + KeyCode::Key6 => VirtualKeyCode::Key6, + KeyCode::Key7 => VirtualKeyCode::Key7, + KeyCode::Key8 => VirtualKeyCode::Key8, + KeyCode::Key9 => VirtualKeyCode::Key9, + KeyCode::Key0 => VirtualKeyCode::Key0, + KeyCode::A => VirtualKeyCode::A, + KeyCode::B => VirtualKeyCode::B, + KeyCode::C => VirtualKeyCode::C, + KeyCode::D => VirtualKeyCode::D, + KeyCode::E => VirtualKeyCode::E, + KeyCode::F => VirtualKeyCode::F, + KeyCode::G => VirtualKeyCode::G, + KeyCode::H => VirtualKeyCode::H, + KeyCode::I => VirtualKeyCode::I, + KeyCode::J => VirtualKeyCode::J, + KeyCode::K => VirtualKeyCode::K, + KeyCode::L => VirtualKeyCode::L, + KeyCode::M => VirtualKeyCode::M, + KeyCode::N => VirtualKeyCode::N, + KeyCode::O => VirtualKeyCode::O, + KeyCode::P => VirtualKeyCode::P, + KeyCode::Q => VirtualKeyCode::Q, + KeyCode::R => VirtualKeyCode::R, + KeyCode::S => VirtualKeyCode::S, + KeyCode::T => VirtualKeyCode::T, + KeyCode::U => VirtualKeyCode::U, + KeyCode::V => VirtualKeyCode::V, + KeyCode::W => VirtualKeyCode::W, + KeyCode::X => VirtualKeyCode::X, + KeyCode::Y => VirtualKeyCode::Y, + KeyCode::Z => VirtualKeyCode::Z, + KeyCode::Escape => VirtualKeyCode::Escape, + KeyCode::F1 => VirtualKeyCode::F1, + KeyCode::F2 => VirtualKeyCode::F2, + KeyCode::F3 => VirtualKeyCode::F3, + KeyCode::F4 => VirtualKeyCode::F4, + KeyCode::F5 => VirtualKeyCode::F5, + KeyCode::F6 => VirtualKeyCode::F6, + KeyCode::F7 => VirtualKeyCode::F7, + KeyCode::F8 => VirtualKeyCode::F8, + KeyCode::F9 => VirtualKeyCode::F9, + KeyCode::F10 => VirtualKeyCode::F10, + KeyCode::F11 => VirtualKeyCode::F11, + KeyCode::F12 => VirtualKeyCode::F12, + KeyCode::F13 => VirtualKeyCode::F13, + KeyCode::F14 => VirtualKeyCode::F14, + KeyCode::F15 => VirtualKeyCode::F15, + KeyCode::F16 => VirtualKeyCode::F16, + KeyCode::F17 => VirtualKeyCode::F17, + KeyCode::F18 => VirtualKeyCode::F18, + KeyCode::F19 => VirtualKeyCode::F19, + KeyCode::F20 => VirtualKeyCode::F20, + KeyCode::F21 => VirtualKeyCode::F21, + KeyCode::F22 => VirtualKeyCode::F22, + KeyCode::F23 => VirtualKeyCode::F23, + KeyCode::F24 => VirtualKeyCode::F24, + KeyCode::Snapshot => VirtualKeyCode::Snapshot, + KeyCode::Scroll => VirtualKeyCode::Scroll, + KeyCode::Pause => VirtualKeyCode::Pause, + KeyCode::Insert => VirtualKeyCode::Insert, + KeyCode::Home => VirtualKeyCode::Home, + KeyCode::Delete => VirtualKeyCode::Delete, + KeyCode::End => VirtualKeyCode::End, + KeyCode::PageDown => VirtualKeyCode::PageDown, + KeyCode::PageUp => VirtualKeyCode::PageUp, + KeyCode::Left => VirtualKeyCode::Left, + KeyCode::Up => VirtualKeyCode::Up, + KeyCode::Right => VirtualKeyCode::Right, + KeyCode::Down => VirtualKeyCode::Down, + KeyCode::Backspace => VirtualKeyCode::Back, + KeyCode::Enter => VirtualKeyCode::Return, + KeyCode::Space => VirtualKeyCode::Space, + KeyCode::Compose => VirtualKeyCode::Compose, + KeyCode::Caret => VirtualKeyCode::Caret, + KeyCode::Numlock => VirtualKeyCode::Numlock, + KeyCode::Numpad0 => VirtualKeyCode::Numpad0, + KeyCode::Numpad1 => VirtualKeyCode::Numpad1, + KeyCode::Numpad2 => VirtualKeyCode::Numpad2, + KeyCode::Numpad3 => VirtualKeyCode::Numpad3, + KeyCode::Numpad4 => VirtualKeyCode::Numpad4, + KeyCode::Numpad5 => VirtualKeyCode::Numpad5, + KeyCode::Numpad6 => VirtualKeyCode::Numpad6, + KeyCode::Numpad7 => VirtualKeyCode::Numpad7, + KeyCode::Numpad8 => VirtualKeyCode::Numpad8, + KeyCode::Numpad9 => VirtualKeyCode::Numpad9, + KeyCode::AbntC1 => VirtualKeyCode::AbntC1, + KeyCode::AbntC2 => VirtualKeyCode::AbntC2, + KeyCode::NumpadAdd => VirtualKeyCode::NumpadAdd, + KeyCode::Plus => VirtualKeyCode::Plus, + KeyCode::Apostrophe => VirtualKeyCode::Apostrophe, + KeyCode::Apps => VirtualKeyCode::Apps, + KeyCode::At => VirtualKeyCode::At, + KeyCode::Ax => VirtualKeyCode::Ax, + KeyCode::Backslash => VirtualKeyCode::Backslash, + KeyCode::Calculator => VirtualKeyCode::Calculator, + KeyCode::Capital => VirtualKeyCode::Capital, + KeyCode::Colon => VirtualKeyCode::Colon, + KeyCode::Comma => VirtualKeyCode::Comma, + KeyCode::Convert => VirtualKeyCode::Convert, + KeyCode::NumpadDecimal => VirtualKeyCode::NumpadDecimal, + KeyCode::NumpadDivide => VirtualKeyCode::NumpadDivide, + KeyCode::Equals => VirtualKeyCode::Equals, + KeyCode::Grave => VirtualKeyCode::Grave, + KeyCode::Kana => VirtualKeyCode::Kana, + KeyCode::Kanji => VirtualKeyCode::Kanji, + KeyCode::LAlt => VirtualKeyCode::LAlt, + KeyCode::LBracket => VirtualKeyCode::LBracket, + KeyCode::LControl => VirtualKeyCode::LControl, + KeyCode::LShift => VirtualKeyCode::LShift, + KeyCode::LWin => VirtualKeyCode::LWin, + KeyCode::Mail => VirtualKeyCode::Mail, + KeyCode::MediaSelect => VirtualKeyCode::MediaSelect, + KeyCode::MediaStop => VirtualKeyCode::MediaStop, + KeyCode::Minus => VirtualKeyCode::Minus, + KeyCode::NumpadMultiply => VirtualKeyCode::NumpadMultiply, + KeyCode::Mute => VirtualKeyCode::Mute, + KeyCode::MyComputer => VirtualKeyCode::MyComputer, + KeyCode::NavigateForward => VirtualKeyCode::NavigateForward, + KeyCode::NavigateBackward => VirtualKeyCode::NavigateBackward, + KeyCode::NextTrack => VirtualKeyCode::NextTrack, + KeyCode::NoConvert => VirtualKeyCode::NoConvert, + KeyCode::NumpadComma => VirtualKeyCode::NumpadComma, + KeyCode::NumpadEnter => VirtualKeyCode::NumpadEnter, + KeyCode::NumpadEquals => VirtualKeyCode::NumpadEquals, + KeyCode::OEM102 => VirtualKeyCode::OEM102, + KeyCode::Period => VirtualKeyCode::Period, + KeyCode::PlayPause => VirtualKeyCode::PlayPause, + KeyCode::Power => VirtualKeyCode::Power, + KeyCode::PrevTrack => VirtualKeyCode::PrevTrack, + KeyCode::RAlt => VirtualKeyCode::RAlt, + KeyCode::RBracket => VirtualKeyCode::RBracket, + KeyCode::RControl => VirtualKeyCode::RControl, + KeyCode::RShift => VirtualKeyCode::RShift, + KeyCode::RWin => VirtualKeyCode::RWin, + KeyCode::Semicolon => VirtualKeyCode::Semicolon, + KeyCode::Slash => VirtualKeyCode::Slash, + KeyCode::Sleep => VirtualKeyCode::Sleep, + KeyCode::Stop => VirtualKeyCode::Stop, + KeyCode::NumpadSubtract => VirtualKeyCode::NumpadSubtract, + KeyCode::Sysrq => VirtualKeyCode::Sysrq, + KeyCode::Tab => VirtualKeyCode::Tab, + KeyCode::Underline => VirtualKeyCode::Underline, + KeyCode::Unlabeled => VirtualKeyCode::Unlabeled, + KeyCode::VolumeDown => VirtualKeyCode::VolumeDown, + KeyCode::VolumeUp => VirtualKeyCode::VolumeUp, + KeyCode::Wake => VirtualKeyCode::Wake, + KeyCode::WebBack => VirtualKeyCode::WebBack, + KeyCode::WebFavorites => VirtualKeyCode::WebFavorites, + KeyCode::WebForward => VirtualKeyCode::WebForward, + KeyCode::WebHome => VirtualKeyCode::WebHome, + KeyCode::WebRefresh => VirtualKeyCode::WebRefresh, + KeyCode::WebSearch => VirtualKeyCode::WebSearch, + KeyCode::WebStop => VirtualKeyCode::WebStop, + KeyCode::Yen => VirtualKeyCode::Yen, + KeyCode::Copy => VirtualKeyCode::Copy, + KeyCode::Paste => VirtualKeyCode::Paste, + KeyCode::Cut => VirtualKeyCode::Cut, + KeyCode::Asterisk => VirtualKeyCode::Asterisk, + } +} + /// Converts a `VirtualKeyCode` from [`winit`] to an [`iced_native`] key code. /// /// [`winit`]: https://github.com/rust-windowing/winit -- cgit From 1428e9180ae9f4edbf22514bb74c5c7e9df9c712 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 12 Jul 2021 21:38:54 +0200 Subject: Make `Menu` API a bit more functional --- winit/src/conversion.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'winit') diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index da09ac9d..02c21c59 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -3,10 +3,11 @@ //! [`winit`]: https://github.com/rust-windowing/winit //! [`iced_native`]: https://github.com/hecrj/iced/tree/master/native use crate::keyboard; +use crate::menu::{self, Menu}; use crate::mouse; use crate::touch; use crate::window; -use crate::{Event, Menu, MenuEntry, Mode, Point}; +use crate::{Event, Mode, Point}; /// Converts a winit window event into an iced event. pub fn window_event( @@ -181,7 +182,7 @@ pub fn menu(menu: Menu) -> winit::window::Menu { for item in menu.iter() { match item { - MenuEntry::Item { + menu::Entry::Item { content, hotkey, .. } => { let hotkey: Option<&keyboard::Hotkey> = hotkey.as_ref().into(); @@ -191,10 +192,10 @@ pub fn menu(menu: Menu) -> winit::window::Menu { hotkey.map(|h| self::hotkey(*h)), ); } - MenuEntry::Dropdown { content, submenu } => { + menu::Entry::Dropdown { content, submenu } => { converted.add_dropdown(content, self::menu(submenu)); } - MenuEntry::Separator => converted.add_separator(), + menu::Entry::Separator => converted.add_separator(), } } -- cgit From b57d567981bb7ef5f9ff397c210778f211d2de5b Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 12 Jul 2021 22:01:57 +0200 Subject: Use `bitflags` for `keyboard::Modifiers` --- winit/src/conversion.rs | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) (limited to 'winit') diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index 02c21c59..3aa88c5c 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -165,10 +165,10 @@ fn hotkey(hotkey: keyboard::Hotkey) -> winit::window::Hotkey { use winit::event::ModifiersState; let mut modifiers = ModifiersState::empty(); - modifiers.set(ModifiersState::CTRL, hotkey.modifiers.control); - modifiers.set(ModifiersState::SHIFT, hotkey.modifiers.shift); - modifiers.set(ModifiersState::ALT, hotkey.modifiers.alt); - modifiers.set(ModifiersState::LOGO, hotkey.modifiers.logo); + modifiers.set(ModifiersState::CTRL, hotkey.modifiers.control()); + modifiers.set(ModifiersState::SHIFT, hotkey.modifiers.shift()); + modifiers.set(ModifiersState::ALT, hotkey.modifiers.alt()); + modifiers.set(ModifiersState::LOGO, hotkey.modifiers.logo()); winit::window::Hotkey::new(modifiers, to_virtual_keycode(hotkey.key)) } @@ -249,12 +249,14 @@ pub fn mouse_button(mouse_button: winit::event::MouseButton) -> mouse::Button { pub fn modifiers( modifiers: winit::event::ModifiersState, ) -> keyboard::Modifiers { - keyboard::Modifiers { - shift: modifiers.shift(), - control: modifiers.ctrl(), - alt: modifiers.alt(), - logo: modifiers.logo(), - } + 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 } /// Converts a physical cursor position to a logical `Point`. -- cgit From b3ff522c182590ffcd03113b5147fa4599559059 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 12 Jul 2021 22:11:06 +0200 Subject: Simplify `Hotkey` conversion in `conversion::menu` --- winit/src/conversion.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) (limited to 'winit') diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index 3aa88c5c..4dc8bbf5 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -185,12 +185,7 @@ pub fn menu(menu: Menu) -> winit::window::Menu { menu::Entry::Item { content, hotkey, .. } => { - let hotkey: Option<&keyboard::Hotkey> = hotkey.as_ref().into(); - converted.add_item( - 0, - content, - hotkey.map(|h| self::hotkey(*h)), - ); + converted.add_item(0, content, hotkey.map(self::hotkey)); } menu::Entry::Dropdown { content, submenu } => { converted.add_dropdown(content, self::menu(submenu)); -- cgit From 31997d255f263a0f47f5af0d8560f3d5bd37f077 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 12 Jul 2021 22:28:18 +0200 Subject: Store and synchronize `Menu` in `application::State` --- winit/src/application.rs | 1 + winit/src/application/state.rs | 21 ++++++++++++++++++--- winit/src/conversion.rs | 2 +- 3 files changed, 20 insertions(+), 4 deletions(-) (limited to 'winit') diff --git a/winit/src/application.rs b/winit/src/application.rs index 108c6d64..bf4b2489 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -151,6 +151,7 @@ where application.mode(), event_loop.primary_monitor(), ) + .with_menu(Some(conversion::menu(&application.menu()))) .build(&event_loop) .map_err(Error::WindowCreationFailed)?; diff --git a/winit/src/application/state.rs b/winit/src/application/state.rs index 2994080c..f60f09be 100644 --- a/winit/src/application/state.rs +++ b/winit/src/application/state.rs @@ -1,5 +1,5 @@ use crate::conversion; -use crate::{Application, Color, Debug, Mode, Point, Size, Viewport}; +use crate::{Application, Color, Debug, Menu, Mode, Point, Size, Viewport}; use std::marker::PhantomData; use winit::event::{Touch, WindowEvent}; @@ -9,6 +9,7 @@ use winit::window::Window; #[derive(Debug, Clone)] pub struct State { title: String, + menu: Menu, mode: Mode, background_color: Color, scale_factor: f64, @@ -23,6 +24,7 @@ impl State { /// Creates a new [`State`] for the provided [`Application`] and window. pub fn new(application: &A, window: &Window) -> Self { let title = application.title(); + let menu = application.menu(); let mode = application.mode(); let background_color = application.background_color(); let scale_factor = application.scale_factor(); @@ -36,10 +38,9 @@ impl State { ) }; - window.set_menu(Some(conversion::menu(application.menu()))); - Self { title, + menu, mode, background_color, scale_factor, @@ -52,6 +53,11 @@ impl State { } } + /// Returns the current [`Menu`] of the [`State`]. + pub fn menu(&self) -> &Menu { + &self.menu + } + /// Returns the current background [`Color`] of the [`State`]. pub fn background_color(&self) -> Color { self.background_color @@ -205,5 +211,14 @@ impl State { self.scale_factor = new_scale_factor; } + + // Update menu + let new_menu = application.menu(); + + if self.menu != new_menu { + window.set_menu(Some(conversion::menu(&new_menu))); + + self.menu = new_menu; + } } } diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index 4dc8bbf5..22118bf5 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -177,7 +177,7 @@ fn hotkey(hotkey: keyboard::Hotkey) -> winit::window::Hotkey { /// /// [`winit`]: https://github.com/rust-windowing/winit /// [`iced_native`]: https://github.com/hecrj/iced/tree/master/native -pub fn menu(menu: Menu) -> winit::window::Menu { +pub fn menu(menu: &Menu) -> winit::window::Menu { let mut converted = winit::window::Menu::new(); for item in menu.iter() { -- cgit From f3b056a6fc06248aa068549fc47ab6864829b875 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 13 Jul 2021 20:46:23 +0200 Subject: Generate unique identifiers for entries in `conversion::menu` --- winit/src/conversion.rs | 42 ++++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) (limited to 'winit') diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index 22118bf5..3c483086 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -178,22 +178,40 @@ fn hotkey(hotkey: keyboard::Hotkey) -> winit::window::Hotkey { /// [`winit`]: https://github.com/rust-windowing/winit /// [`iced_native`]: https://github.com/hecrj/iced/tree/master/native pub fn menu(menu: &Menu) -> winit::window::Menu { - let mut converted = winit::window::Menu::new(); - - for item in menu.iter() { - match item { - menu::Entry::Item { - content, hotkey, .. - } => { - converted.add_item(0, content, hotkey.map(self::hotkey)); - } - menu::Entry::Dropdown { content, submenu } => { - converted.add_dropdown(content, self::menu(submenu)); + fn menu_i( + starting_id: usize, + menu: &Menu, + ) -> (winit::window::Menu, usize) { + let mut id = starting_id; + let mut converted = winit::window::Menu::new(); + + for item in menu.iter() { + match item { + menu::Entry::Item { + content, hotkey, .. + } => { + converted.add_item(id, content, hotkey.map(self::hotkey)); + + id += 1; + } + menu::Entry::Dropdown { content, submenu } => { + let (submenu, n_children) = menu_i(id, submenu); + + converted.add_dropdown(content, submenu); + + id += n_children; + } + menu::Entry::Separator => { + converted.add_separator(); + } } - menu::Entry::Separator => converted.add_separator(), } + + (converted, id - starting_id) } + let (converted, _) = menu_i(0, menu); + converted } -- cgit From 6221adf2b1b1e8150931d4175e1e36870d45f6e5 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 13 Jul 2021 20:55:21 +0200 Subject: Draft `conversion::menu_message` in `iced_winit` ... and wire it up to the runtime loop --- winit/src/application.rs | 10 ++++++++++ winit/src/conversion.rs | 11 +++++++++++ 2 files changed, 21 insertions(+) (limited to 'winit') diff --git a/winit/src/application.rs b/winit/src/application.rs index bf4b2489..ada64dfc 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -386,6 +386,16 @@ async fn run_instance( // TODO: Handle animations! // Maybe we can use `ControlFlow::WaitUntil` for this. } + event::Event::WindowEvent { + event: event::WindowEvent::MenuEntryActivated(entry_id), + .. + } => { + if let Some(message) = + conversion::menu_message(state.menu(), entry_id) + { + messages.push(message); + } + } event::Event::WindowEvent { event: window_event, .. diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index 3c483086..51db615b 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -215,6 +215,17 @@ pub fn menu(menu: &Menu) -> winit::window::Menu { converted } +/// Given a [`Menu`] and an identifier of a [`menu::Entry`], it returns the +/// `Message` that should be produced when that entry is activated. +pub fn menu_message( + _menu: &Menu, + id: isize, +) -> Option { + println!("Menu entry activated: {}", id); + + None +} + /// Converts a `MouseCursor` from [`iced_native`] to a [`winit`] cursor icon. /// /// [`winit`]: https://github.com/rust-windowing/winit -- cgit From 4abaee8b2354a666a75e4eb5ec9cc9a744936813 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 13 Jul 2021 21:11:13 +0200 Subject: Use `Menu::default` for root level menu in `conversion::menu` --- winit/src/conversion.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'winit') diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index 51db615b..75419006 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -179,11 +179,11 @@ fn hotkey(hotkey: keyboard::Hotkey) -> winit::window::Hotkey { /// [`iced_native`]: https://github.com/hecrj/iced/tree/master/native pub fn menu(menu: &Menu) -> winit::window::Menu { fn menu_i( + converted: &mut winit::window::Menu, starting_id: usize, menu: &Menu, - ) -> (winit::window::Menu, usize) { + ) -> usize { let mut id = starting_id; - let mut converted = winit::window::Menu::new(); for item in menu.iter() { match item { @@ -195,9 +195,11 @@ pub fn menu(menu: &Menu) -> winit::window::Menu { id += 1; } menu::Entry::Dropdown { content, submenu } => { - let (submenu, n_children) = menu_i(id, submenu); + let mut converted_submenu = winit::window::Menu::new(); + let n_children = + menu_i(&mut converted_submenu, id, submenu); - converted.add_dropdown(content, submenu); + converted.add_dropdown(content, converted_submenu); id += n_children; } @@ -207,10 +209,11 @@ pub fn menu(menu: &Menu) -> winit::window::Menu { } } - (converted, id - starting_id) + id - starting_id } - let (converted, _) = menu_i(0, menu); + let mut converted = winit::window::Menu::default(); + let _ = menu_i(&mut converted, 0, menu); converted } -- cgit From 5df2a92f28be1d53d32d5b42a6645459b7d78efe Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 13 Jul 2021 21:15:07 +0200 Subject: Force `Application::Message` to implement `Clone` A `Message` should represent an application event (e.g. user interactions, command results, subscription results...). Therefore, it should always consist of pure, cloneable data. --- winit/src/conversion.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'winit') diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index 75419006..687a037e 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -223,7 +223,10 @@ pub fn menu(menu: &Menu) -> winit::window::Menu { pub fn menu_message( _menu: &Menu, id: isize, -) -> Option { +) -> Option +where + Message: Clone, +{ println!("Menu entry activated: {}", id); None -- cgit From 2e7eac7d2167b492149e064927e764eca67f98fe Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 13 Jul 2021 21:31:34 +0200 Subject: Implement `conversion::menu_message` --- winit/src/conversion.rs | 50 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 44 insertions(+), 6 deletions(-) (limited to 'winit') diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index 687a037e..dc7f90ad 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -220,16 +220,54 @@ pub fn menu(menu: &Menu) -> winit::window::Menu { /// Given a [`Menu`] and an identifier of a [`menu::Entry`], it returns the /// `Message` that should be produced when that entry is activated. -pub fn menu_message( - _menu: &Menu, - id: isize, -) -> Option +pub fn menu_message(menu: &Menu, id: isize) -> Option where Message: Clone, { - println!("Menu entry activated: {}", id); + use std::convert::TryFrom; - None + fn find_message( + target: usize, + starting_id: usize, + menu: &Menu, + ) -> Result + where + Message: Clone, + { + let mut id = starting_id; + + for entry in menu.iter() { + match entry { + menu::Entry::Item { on_activation, .. } => { + if id == target { + return Ok(on_activation.clone()); + } + + id += 1; + } + menu::Entry::Dropdown { submenu, .. } => { + match find_message(target, id, submenu) { + Ok(message) => { + return Ok(message); + } + Err(n_children) => { + id += n_children; + } + } + } + menu::Entry::Separator => {} + } + } + + Err(id - starting_id) + } + + // TODO: Does `winit` really need to provide an `isize`? + if let Ok(id) = usize::try_from(id) { + find_message(id, 0, menu).ok() + } else { + None + } } /// Converts a `MouseCursor` from [`iced_native`] to a [`winit`] cursor icon. -- cgit From a2f49a74d08a0cff2e892932b484a88a4034f627 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 19 Jul 2021 21:01:24 +0700 Subject: Replace `content` with `title` in `menu` module --- winit/src/conversion.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'winit') diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index dc7f90ad..e61611aa 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -187,19 +187,17 @@ pub fn menu(menu: &Menu) -> winit::window::Menu { for item in menu.iter() { match item { - menu::Entry::Item { - content, hotkey, .. - } => { - converted.add_item(id, content, hotkey.map(self::hotkey)); + menu::Entry::Item { title, hotkey, .. } => { + converted.add_item(id, title, hotkey.map(self::hotkey)); id += 1; } - menu::Entry::Dropdown { content, submenu } => { + menu::Entry::Dropdown { title, submenu } => { let mut converted_submenu = winit::window::Menu::new(); let n_children = menu_i(&mut converted_submenu, id, submenu); - converted.add_dropdown(content, converted_submenu); + converted.add_dropdown(title, converted_submenu); id += n_children; } -- cgit From c8ac77e4e99414746adedf38cf69ac8dcd1601a4 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 19 Jul 2021 21:05:16 +0700 Subject: Write documentation for `menu` method in `Application` --- winit/src/application.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'winit') diff --git a/winit/src/application.rs b/winit/src/application.rs index ada64dfc..5d1aabf9 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -100,7 +100,9 @@ pub trait Application: Program { false } - /// TODO + /// Returns the current system [`Menu`] of the [`Application`]. + /// + /// By default, it returns an empty [`Menu`]. fn menu(&self) -> Menu { Menu::new() } -- cgit From 82db3c78b6cfa2cc55ece6ffa46811bfb5195f60 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 20 Jul 2021 21:34:20 +0700 Subject: Update `winit` and `glutin` dependencies ... and remove crates.io patch --- winit/Cargo.toml | 4 ++-- winit/src/conversion.rs | 17 +++++------------ 2 files changed, 7 insertions(+), 14 deletions(-) (limited to 'winit') diff --git a/winit/Cargo.toml b/winit/Cargo.toml index 87fd23d5..b1192135 100644 --- a/winit/Cargo.toml +++ b/winit/Cargo.toml @@ -20,8 +20,8 @@ thiserror = "1.0" [dependencies.winit] version = "0.25" -# git = "https://github.com/iced-rs/winit" -# rev = "e351421a32bf01b428325dde44dea39ee2656153" +git = "https://github.com/iced-rs/winit" +rev = "844485272a7412cb35cdbfac3524decdf59475ca" [dependencies.iced_native] version = "0.4" diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index e61611aa..5a8b9fd8 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -218,17 +218,15 @@ pub fn menu(menu: &Menu) -> winit::window::Menu { /// Given a [`Menu`] and an identifier of a [`menu::Entry`], it returns the /// `Message` that should be produced when that entry is activated. -pub fn menu_message(menu: &Menu, id: isize) -> Option +pub fn menu_message(menu: &Menu, id: u32) -> Option where Message: Clone, { - use std::convert::TryFrom; - fn find_message( - target: usize, - starting_id: usize, + target: u32, + starting_id: u32, menu: &Menu, - ) -> Result + ) -> Result where Message: Clone, { @@ -260,12 +258,7 @@ where Err(id - starting_id) } - // TODO: Does `winit` really need to provide an `isize`? - if let Ok(id) = usize::try_from(id) { - find_message(id, 0, menu).ok() - } else { - None - } + find_message(id, 0, menu).ok() } /// Converts a `MouseCursor` from [`iced_native`] to a [`winit`] cursor icon. -- cgit From 72b3bf95de3f11812b1541fe2ffea76a515aee79 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 21 Jul 2021 18:59:24 +0700 Subject: Improve `window::Position` API --- winit/src/conversion.rs | 45 ++++++++++++++++++++++++++++++++++++++++++++- winit/src/lib.rs | 2 ++ winit/src/position.rs | 22 ++++++++++++++++++++++ winit/src/settings.rs | 19 ++++++++++++++----- 4 files changed, 82 insertions(+), 6 deletions(-) create mode 100644 winit/src/position.rs (limited to 'winit') diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index b850a805..f785fce6 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -6,7 +6,7 @@ use crate::keyboard; use crate::mouse; use crate::touch; use crate::window; -use crate::{Event, Mode, Point}; +use crate::{Event, Mode, Point, Position}; /// Converts a winit window event into an iced event. pub fn window_event( @@ -133,6 +133,49 @@ pub fn window_event( } } +/// Converts a [`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, +) -> Option { + match position { + Position::Default => None, + Position::Specific(x, y) => { + Some(winit::dpi::Position::Logical(winit::dpi::LogicalPosition { + x: f64::from(x), + y: f64::from(y), + })) + } + Position::Centered => { + if let Some(monitor) = monitor { + let start = monitor.position(); + + let resolution: winit::dpi::LogicalSize = + monitor.size().to_logical(monitor.scale_factor()); + + let centered: winit::dpi::PhysicalPosition = + winit::dpi::LogicalPosition { + x: (resolution.width - f64::from(width)) / 2.0, + y: (resolution.height - f64::from(height)) / 2.0, + } + .to_physical(monitor.scale_factor()); + + Some(winit::dpi::Position::Physical( + winit::dpi::PhysicalPosition { + x: start.x + centered.x, + y: start.y + centered.y, + }, + )) + } else { + None + } + } + } +} + /// Converts a [`Mode`] to a [`winit`] fullscreen mode. /// /// [`winit`]: https://github.com/rust-windowing/winit diff --git a/winit/src/lib.rs b/winit/src/lib.rs index c9f324dd..1707846a 100644 --- a/winit/src/lib.rs +++ b/winit/src/lib.rs @@ -31,12 +31,14 @@ pub mod settings; mod clipboard; mod error; mod mode; +mod position; mod proxy; pub use application::Application; pub use clipboard::Clipboard; pub use error::Error; pub use mode::Mode; +pub use position::Position; pub use proxy::Proxy; pub use settings::Settings; diff --git a/winit/src/position.rs b/winit/src/position.rs new file mode 100644 index 00000000..c260c29e --- /dev/null +++ b/winit/src/position.rs @@ -0,0 +1,22 @@ +/// 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 1769c676..743f79bc 100644 --- a/winit/src/settings.rs +++ b/winit/src/settings.rs @@ -9,7 +9,7 @@ mod platform; pub use platform::PlatformSpecific; use crate::conversion; -use crate::Mode; +use crate::{Mode, Position}; use winit::monitor::MonitorHandle; use winit::window::WindowBuilder; @@ -36,7 +36,7 @@ pub struct Window { pub size: (u32, u32), /// The position of the window. - pub position: (i32, i32), + pub position: Position, /// The minimum size of the window. pub min_size: Option<(u32, u32)>, @@ -78,15 +78,21 @@ impl Window { window_builder = window_builder .with_title(title) .with_inner_size(winit::dpi::LogicalSize { width, height }) - .with_position(winit::dpi::LogicalPosition { x: self.position.0, y: self.position.1 }) .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_fullscreen(conversion::fullscreen(primary_monitor, mode)) .with_visible(conversion::visible(mode)); + 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 }); @@ -108,6 +114,9 @@ impl Window { .with_drag_and_drop(self.platform_specific.drag_and_drop); } + window_builder = window_builder + .with_fullscreen(conversion::fullscreen(primary_monitor, mode)); + window_builder } } @@ -116,7 +125,7 @@ impl Default for Window { fn default() -> Window { Window { size: (1024, 768), - position: (100, 100), + position: Position::default(), min_size: None, max_size: None, resizable: true, -- cgit