From 9fc5ad23edca93553137100d167de7b69e88f785 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 5 Jul 2021 16:23:44 -0300 Subject: Initial menu implementation --- Cargo.toml | 4 + core/src/keyboard.rs | 2 + core/src/keyboard/hotkey.rs | 18 ++++ examples/menu/Cargo.toml | 10 ++ examples/menu/src/main.rs | 123 ++++++++++++++++++++++ glutin/Cargo.toml | 4 +- native/src/lib.rs | 2 + native/src/menu.rs | 81 +++++++++++++++ src/application.rs | 11 +- src/lib.rs | 4 +- winit/Cargo.toml | 4 +- winit/src/application.rs | 6 ++ winit/src/application/state.rs | 2 + winit/src/conversion.rs | 224 ++++++++++++++++++++++++++++++++++++++++- 14 files changed, 487 insertions(+), 8 deletions(-) create mode 100644 core/src/keyboard/hotkey.rs create mode 100644 examples/menu/Cargo.toml create mode 100644 examples/menu/src/main.rs create mode 100644 native/src/menu.rs diff --git a/Cargo.toml b/Cargo.toml index 329877c8..075682e4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -85,6 +85,7 @@ members = [ "examples/tour", "examples/tooltip", "examples/url_handler", + "examples/menu", ] [dependencies] @@ -92,6 +93,9 @@ iced_core = { version = "0.4", path = "core" } iced_futures = { version = "0.3", path = "futures" } thiserror = "1.0" +[patch.crates-io] +winit = { git = "https://github.com/iced-rs/winit", rev = "e351421a32bf01b428325dde44dea39ee2656153"} + [target.'cfg(not(target_arch = "wasm32"))'.dependencies] iced_winit = { version = "0.3", path = "winit" } iced_glutin = { version = "0.2", path = "glutin", optional = true } diff --git a/core/src/keyboard.rs b/core/src/keyboard.rs index 61e017ad..cb64701a 100644 --- a/core/src/keyboard.rs +++ b/core/src/keyboard.rs @@ -1,8 +1,10 @@ //! Reuse basic keyboard types. mod event; +mod hotkey; mod key_code; mod modifiers; pub use event::Event; +pub use hotkey::Hotkey; pub use key_code::KeyCode; pub use modifiers::Modifiers; diff --git a/core/src/keyboard/hotkey.rs b/core/src/keyboard/hotkey.rs new file mode 100644 index 00000000..310ef286 --- /dev/null +++ b/core/src/keyboard/hotkey.rs @@ -0,0 +1,18 @@ +use crate::keyboard::{KeyCode, Modifiers}; + +/// Representation of a hotkey, consists on the combination of a [`KeyCode`] and [`Modifiers`]. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Hotkey { + /// The key that represents this hotkey. + pub key: KeyCode, + + /// The list of modifiers that represents this hotkey. + pub modifiers: Modifiers, +} + +impl Hotkey { + /// Creates a new [`Hotkey`] with the given [`Modifiers`] and [`KeyCode`]. + pub fn new(modifiers: Modifiers, key: KeyCode) -> Self { + Self { modifiers, key } + } +} diff --git a/examples/menu/Cargo.toml b/examples/menu/Cargo.toml new file mode 100644 index 00000000..44597734 --- /dev/null +++ b/examples/menu/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "menu" +version = "0.1.0" +authors = ["Héctor Ramón Jiménez "] +edition = "2018" +publish = false + +[dependencies] +iced = { path = "../.." } +iced_native = { path = "../../native" } \ No newline at end of file diff --git a/examples/menu/src/main.rs b/examples/menu/src/main.rs new file mode 100644 index 00000000..f8f70903 --- /dev/null +++ b/examples/menu/src/main.rs @@ -0,0 +1,123 @@ +use iced::{ + executor, Application, Clipboard, Command, Container, Element, Length, + Menu, Settings, Text, +}; +use iced_native::keyboard::{Hotkey, KeyCode, Modifiers}; + +pub fn main() -> iced::Result { + App::run(Settings::default()) +} + +#[derive(Debug, Default)] +struct App { + selected: Option, +} + +#[derive(Debug, Clone)] +enum Entry { + One, + Two, + Three, + A, + B, + C, +} + +#[derive(Debug, Clone)] +enum Message { + MenuActivated(Entry), +} + +impl Application for App { + type Executor = executor::Default; + type Message = Message; + type Flags = (); + + fn new(_flags: ()) -> (App, Command) { + (App::default(), Command::none()) + } + + fn title(&self) -> String { + String::from("Menu - Iced") + } + + fn menu(&self) -> Menu { + let alt = Modifiers { + alt: true, + control: false, + logo: false, + shift: false, + }; + let ctrl_shift = Modifiers { + control: true, + shift: true, + logo: false, + alt: false, + }; + + Menu::new() + .dropdown( + "First", + Menu::new() + .item( + "One", + Hotkey::new(alt, KeyCode::F1), + Message::MenuActivated(Entry::One), + ) + .item( + "Two", + Hotkey::new(alt, KeyCode::F2), + Message::MenuActivated(Entry::Two), + ) + .separator() + .item( + "Three", + Hotkey::new(alt, KeyCode::F3), + Message::MenuActivated(Entry::Three), + ), + ) + .dropdown( + "Second", + Menu::new() + .item( + "A", + Hotkey::new(ctrl_shift, KeyCode::A), + Message::MenuActivated(Entry::A), + ) + .item( + "B", + Hotkey::new(ctrl_shift, KeyCode::B), + Message::MenuActivated(Entry::B), + ) + .separator() + .item( + "C", + Hotkey::new(ctrl_shift, KeyCode::C), + Message::MenuActivated(Entry::C), + ), + ) + } + + fn update( + &mut self, + message: Message, + _clipboard: &mut Clipboard, + ) -> Command { + match message { + Message::MenuActivated(entry) => self.selected = Some(entry), + } + + Command::none() + } + + fn view(&mut self) -> Element { + Container::new( + Text::new(format!("Selected {:?}", self.selected)).size(48), + ) + .width(Length::Fill) + .height(Length::Fill) + .center_x() + .center_y() + .into() + } +} diff --git a/glutin/Cargo.toml b/glutin/Cargo.toml index 78d5fe47..b42a6b36 100644 --- a/glutin/Cargo.toml +++ b/glutin/Cargo.toml @@ -15,8 +15,8 @@ debug = ["iced_winit/debug"] [dependencies.glutin] version = "0.27" -git = "https://github.com/iced-rs/glutin" -rev = "2564d0ab87cf2ad824a2a58733aebe40dd2f29bb" +# git = "https://github.com/iced-rs/glutin" +# rev = "2564d0ab87cf2ad824a2a58733aebe40dd2f29bb" [dependencies.iced_native] version = "0.4" diff --git a/native/src/lib.rs b/native/src/lib.rs index cd214e36..56a933f0 100644 --- a/native/src/lib.rs +++ b/native/src/lib.rs @@ -37,6 +37,7 @@ pub mod clipboard; pub mod event; pub mod keyboard; pub mod layout; +pub mod menu; pub mod mouse; pub mod overlay; pub mod program; @@ -75,6 +76,7 @@ pub use element::Element; pub use event::Event; pub use hasher::Hasher; pub use layout::Layout; +pub use menu::{Menu, MenuEntry}; pub use overlay::Overlay; pub use program::Program; pub use renderer::Renderer; diff --git a/native/src/menu.rs b/native/src/menu.rs new file mode 100644 index 00000000..6c73cb32 --- /dev/null +++ b/native/src/menu.rs @@ -0,0 +1,81 @@ +//! Build menus for your application. +use crate::keyboard::Hotkey; + +/// Menu representation. +/// +/// This can be used by `shell` implementations to create a menu. +#[derive(Debug, Clone, PartialEq)] +pub struct Menu { + items: Vec>, +} + +impl Menu { + /// Creates an empty [`Menu`]. + pub fn new() -> Self { + Menu { items: Vec::new() } + } + + /// Adds an item to the [`Menu`]. + pub fn item>( + mut self, + content: S, + hotkey: impl Into>, + on_activation: Message, + ) -> Self { + let content = content.into(); + let hotkey = hotkey.into(); + + self.items.push(MenuEntry::Item { + on_activation, + content, + hotkey, + }); + self + } + + /// Adds a separator to the [`Menu`]. + pub fn separator(mut self) -> Self { + self.items.push(MenuEntry::Separator); + self + } + + /// Adds a dropdown to the [`Menu`]. + pub fn dropdown>( + mut self, + content: S, + submenu: Menu, + ) -> Self { + let content = content.into(); + + self.items.push(MenuEntry::Dropdown { content, submenu }); + self + } + + /// Returns a [`MenuEntry`] iterator. + pub fn iter(self) -> std::vec::IntoIter> { + self.items.into_iter() + } +} + +/// Represents one of the possible entries used to build a [`Menu`]. +#[derive(Debug, Clone, PartialEq)] +pub enum MenuEntry { + /// Item for a [`Menu`] + Item { + /// The title of the item + content: String, + /// The [`Hotkey`] to activate the item, if any + hotkey: Option, + /// The message generated when the item is activated + on_activation: Message, + }, + /// Dropdown for a [`Menu`] + Dropdown { + /// Title of the dropdown + content: String, + /// The submenu of the dropdown + submenu: Menu, + }, + /// Separator for a [`Menu`] + Separator, +} diff --git a/src/application.rs b/src/application.rs index bda8558c..b3dae1b3 100644 --- a/src/application.rs +++ b/src/application.rs @@ -1,6 +1,6 @@ use crate::window; use crate::{ - Clipboard, Color, Command, Element, Executor, Settings, Subscription, + Clipboard, Color, Command, Element, Executor, Menu, Settings, Subscription, }; /// An interactive cross-platform application. @@ -191,6 +191,11 @@ pub trait Application: Sized { false } + /// TODO + fn menu(&self) -> Menu { + Menu::new() + } + /// Runs the [`Application`]. /// /// On native platforms, this method will take control of the current thread @@ -296,6 +301,10 @@ where fn should_exit(&self) -> bool { self.0.should_exit() } + + fn menu(&self) -> Menu { + self.0.menu() + } } #[cfg(target_arch = "wasm32")] diff --git a/src/lib.rs b/src/lib.rs index 6a029afd..50952777 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -246,6 +246,6 @@ pub use settings::Settings; pub use runtime::{ futures, Align, Background, Clipboard, Color, Command, Font, - HorizontalAlignment, Length, Point, Rectangle, Size, Subscription, Vector, - VerticalAlignment, + HorizontalAlignment, Length, Menu, Point, Rectangle, Size, Subscription, + Vector, VerticalAlignment, }; 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 c4552a72d43e5f79faa7c64634be539d81f995b9 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 12 Jul 2021 21:21:22 +0200 Subject: Update `winit` dependency in `iced-rs` --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 075682e4..1a4da1e3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -94,7 +94,7 @@ iced_futures = { version = "0.3", path = "futures" } thiserror = "1.0" [patch.crates-io] -winit = { git = "https://github.com/iced-rs/winit", rev = "e351421a32bf01b428325dde44dea39ee2656153"} +winit = { git = "https://github.com/iced-rs/winit", rev = "327c8756f90953a6a03f818113f8566176e6eb0d"} [target.'cfg(not(target_arch = "wasm32"))'.dependencies] iced_winit = { version = "0.3", path = "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 --- examples/menu/src/main.rs | 44 ++++++++++++++------------- native/src/lib.rs | 2 +- native/src/menu.rs | 75 ++++++++++++++++++++++++----------------------- src/lib.rs | 2 +- winit/src/conversion.rs | 9 +++--- 5 files changed, 70 insertions(+), 62 deletions(-) diff --git a/examples/menu/src/main.rs b/examples/menu/src/main.rs index f8f70903..9d4ed71b 100644 --- a/examples/menu/src/main.rs +++ b/examples/menu/src/main.rs @@ -1,6 +1,7 @@ +use iced::menu::{self, Menu}; use iced::{ executor, Application, Clipboard, Command, Container, Element, Length, - Menu, Settings, Text, + Settings, Text, }; use iced_native::keyboard::{Hotkey, KeyCode, Modifiers}; @@ -55,47 +56,50 @@ impl Application for App { alt: false, }; - Menu::new() - .dropdown( + Menu::with_entries(vec![ + menu::Entry::dropdown( "First", - Menu::new() - .item( + Menu::with_entries(vec![ + menu::Entry::item( "One", Hotkey::new(alt, KeyCode::F1), Message::MenuActivated(Entry::One), - ) - .item( + ), + menu::Entry::item( "Two", Hotkey::new(alt, KeyCode::F2), Message::MenuActivated(Entry::Two), - ) - .separator() - .item( + ), + menu::Entry::Separator, + menu::Entry::item( "Three", Hotkey::new(alt, KeyCode::F3), Message::MenuActivated(Entry::Three), ), - ) - .dropdown( + ]), + ), + menu::Entry::dropdown( "Second", - Menu::new() - .item( + Menu::with_entries(vec![ + menu::Entry::item( "A", Hotkey::new(ctrl_shift, KeyCode::A), Message::MenuActivated(Entry::A), - ) - .item( + ), + menu::Entry::item( "B", Hotkey::new(ctrl_shift, KeyCode::B), Message::MenuActivated(Entry::B), - ) - .separator() - .item( + ), + menu::Entry::Separator, + menu::Entry::item( "C", Hotkey::new(ctrl_shift, KeyCode::C), Message::MenuActivated(Entry::C), ), - ) + ]), + ), + ]) } fn update( diff --git a/native/src/lib.rs b/native/src/lib.rs index 56a933f0..564f2514 100644 --- a/native/src/lib.rs +++ b/native/src/lib.rs @@ -76,7 +76,7 @@ pub use element::Element; pub use event::Event; pub use hasher::Hasher; pub use layout::Layout; -pub use menu::{Menu, MenuEntry}; +pub use menu::Menu; pub use overlay::Overlay; pub use program::Program; pub use renderer::Renderer; diff --git a/native/src/menu.rs b/native/src/menu.rs index 6c73cb32..fe770216 100644 --- a/native/src/menu.rs +++ b/native/src/menu.rs @@ -6,60 +6,35 @@ use crate::keyboard::Hotkey; /// This can be used by `shell` implementations to create a menu. #[derive(Debug, Clone, PartialEq)] pub struct Menu { - items: Vec>, + entries: Vec>, } impl Menu { /// Creates an empty [`Menu`]. pub fn new() -> Self { - Menu { items: Vec::new() } + Self::with_entries(Vec::new()) } - /// Adds an item to the [`Menu`]. - pub fn item>( - mut self, - content: S, - hotkey: impl Into>, - on_activation: Message, - ) -> Self { - let content = content.into(); - let hotkey = hotkey.into(); - - self.items.push(MenuEntry::Item { - on_activation, - content, - hotkey, - }); - self + /// Creates a new [`Menu`] with the given entries. + pub fn with_entries(entries: Vec>) -> Self { + Self { entries } } - /// Adds a separator to the [`Menu`]. - pub fn separator(mut self) -> Self { - self.items.push(MenuEntry::Separator); - self - } - - /// Adds a dropdown to the [`Menu`]. - pub fn dropdown>( - mut self, - content: S, - submenu: Menu, - ) -> Self { - let content = content.into(); - - self.items.push(MenuEntry::Dropdown { content, submenu }); + /// Adds an [`Entry`] to the [`Menu`]. + pub fn push(mut self, entry: Entry) -> Self { + self.entries.push(entry); self } /// Returns a [`MenuEntry`] iterator. - pub fn iter(self) -> std::vec::IntoIter> { - self.items.into_iter() + pub fn iter(self) -> impl Iterator> { + self.entries.into_iter() } } /// Represents one of the possible entries used to build a [`Menu`]. #[derive(Debug, Clone, PartialEq)] -pub enum MenuEntry { +pub enum Entry { /// Item for a [`Menu`] Item { /// The title of the item @@ -79,3 +54,31 @@ pub enum MenuEntry { /// Separator for a [`Menu`] Separator, } + +impl Entry { + /// Creates an [`Entry::Item`]. + pub fn item>( + content: S, + hotkey: impl Into>, + on_activation: Message, + ) -> Self { + let content = content.into(); + let hotkey = hotkey.into(); + + Entry::Item { + content, + hotkey, + on_activation, + } + } + + /// Creates an [`Entry::Dropdown`]. + pub fn dropdown>( + content: S, + submenu: Menu, + ) -> Self { + let content = content.into(); + + Entry::Dropdown { content, submenu } + } +} diff --git a/src/lib.rs b/src/lib.rs index 50952777..8cd6aa70 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -245,7 +245,7 @@ pub use sandbox::Sandbox; pub use settings::Settings; pub use runtime::{ - futures, Align, Background, Clipboard, Color, Command, Font, + futures, menu, Align, Background, Clipboard, Color, Command, Font, HorizontalAlignment, Length, Menu, Point, Rectangle, Size, Subscription, Vector, VerticalAlignment, }; 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 735cfb790813c44852612400e31c0190b9c641a6 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 12 Jul 2021 21:44:01 +0200 Subject: Move `menu` module from `iced_native` to `iced_core` --- core/src/lib.rs | 2 ++ core/src/menu.rs | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ native/src/lib.rs | 6 ++-- native/src/menu.rs | 84 ------------------------------------------------------ web/src/lib.rs | 4 +-- 5 files changed, 90 insertions(+), 90 deletions(-) create mode 100644 core/src/menu.rs delete mode 100644 native/src/menu.rs diff --git a/core/src/lib.rs b/core/src/lib.rs index 6453d599..c4288158 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -15,6 +15,7 @@ #![forbid(unsafe_code)] #![forbid(rust_2018_idioms)] pub mod keyboard; +pub mod menu; pub mod mouse; mod align; @@ -33,6 +34,7 @@ pub use background::Background; pub use color::Color; pub use font::Font; pub use length::Length; +pub use menu::Menu; pub use padding::Padding; pub use point::Point; pub use rectangle::Rectangle; diff --git a/core/src/menu.rs b/core/src/menu.rs new file mode 100644 index 00000000..fe770216 --- /dev/null +++ b/core/src/menu.rs @@ -0,0 +1,84 @@ +//! Build menus for your application. +use crate::keyboard::Hotkey; + +/// Menu representation. +/// +/// This can be used by `shell` implementations to create a menu. +#[derive(Debug, Clone, PartialEq)] +pub struct Menu { + entries: Vec>, +} + +impl Menu { + /// Creates an empty [`Menu`]. + pub fn new() -> Self { + Self::with_entries(Vec::new()) + } + + /// Creates a new [`Menu`] with the given entries. + pub fn with_entries(entries: Vec>) -> Self { + Self { entries } + } + + /// Adds an [`Entry`] to the [`Menu`]. + pub fn push(mut self, entry: Entry) -> Self { + self.entries.push(entry); + self + } + + /// Returns a [`MenuEntry`] iterator. + pub fn iter(self) -> impl Iterator> { + self.entries.into_iter() + } +} + +/// Represents one of the possible entries used to build a [`Menu`]. +#[derive(Debug, Clone, PartialEq)] +pub enum Entry { + /// Item for a [`Menu`] + Item { + /// The title of the item + content: String, + /// The [`Hotkey`] to activate the item, if any + hotkey: Option, + /// The message generated when the item is activated + on_activation: Message, + }, + /// Dropdown for a [`Menu`] + Dropdown { + /// Title of the dropdown + content: String, + /// The submenu of the dropdown + submenu: Menu, + }, + /// Separator for a [`Menu`] + Separator, +} + +impl Entry { + /// Creates an [`Entry::Item`]. + pub fn item>( + content: S, + hotkey: impl Into>, + on_activation: Message, + ) -> Self { + let content = content.into(); + let hotkey = hotkey.into(); + + Entry::Item { + content, + hotkey, + on_activation, + } + } + + /// Creates an [`Entry::Dropdown`]. + pub fn dropdown>( + content: S, + submenu: Menu, + ) -> Self { + let content = content.into(); + + Entry::Dropdown { content, submenu } + } +} diff --git a/native/src/lib.rs b/native/src/lib.rs index 564f2514..cbb02506 100644 --- a/native/src/lib.rs +++ b/native/src/lib.rs @@ -37,7 +37,6 @@ pub mod clipboard; pub mod event; pub mod keyboard; pub mod layout; -pub mod menu; pub mod mouse; pub mod overlay; pub mod program; @@ -62,8 +61,8 @@ mod debug; mod debug; pub use iced_core::{ - Align, Background, Color, Font, HorizontalAlignment, Length, Padding, - Point, Rectangle, Size, Vector, VerticalAlignment, + menu, Align, Background, Color, Font, HorizontalAlignment, Length, Menu, + Padding, Point, Rectangle, Size, Vector, VerticalAlignment, }; pub use iced_futures::{executor, futures, Command}; @@ -76,7 +75,6 @@ pub use element::Element; pub use event::Event; pub use hasher::Hasher; pub use layout::Layout; -pub use menu::Menu; pub use overlay::Overlay; pub use program::Program; pub use renderer::Renderer; diff --git a/native/src/menu.rs b/native/src/menu.rs deleted file mode 100644 index fe770216..00000000 --- a/native/src/menu.rs +++ /dev/null @@ -1,84 +0,0 @@ -//! Build menus for your application. -use crate::keyboard::Hotkey; - -/// Menu representation. -/// -/// This can be used by `shell` implementations to create a menu. -#[derive(Debug, Clone, PartialEq)] -pub struct Menu { - entries: Vec>, -} - -impl Menu { - /// Creates an empty [`Menu`]. - pub fn new() -> Self { - Self::with_entries(Vec::new()) - } - - /// Creates a new [`Menu`] with the given entries. - pub fn with_entries(entries: Vec>) -> Self { - Self { entries } - } - - /// Adds an [`Entry`] to the [`Menu`]. - pub fn push(mut self, entry: Entry) -> Self { - self.entries.push(entry); - self - } - - /// Returns a [`MenuEntry`] iterator. - pub fn iter(self) -> impl Iterator> { - self.entries.into_iter() - } -} - -/// Represents one of the possible entries used to build a [`Menu`]. -#[derive(Debug, Clone, PartialEq)] -pub enum Entry { - /// Item for a [`Menu`] - Item { - /// The title of the item - content: String, - /// The [`Hotkey`] to activate the item, if any - hotkey: Option, - /// The message generated when the item is activated - on_activation: Message, - }, - /// Dropdown for a [`Menu`] - Dropdown { - /// Title of the dropdown - content: String, - /// The submenu of the dropdown - submenu: Menu, - }, - /// Separator for a [`Menu`] - Separator, -} - -impl Entry { - /// Creates an [`Entry::Item`]. - pub fn item>( - content: S, - hotkey: impl Into>, - on_activation: Message, - ) -> Self { - let content = content.into(); - let hotkey = hotkey.into(); - - Entry::Item { - content, - hotkey, - on_activation, - } - } - - /// Creates an [`Entry::Dropdown`]. - pub fn dropdown>( - content: S, - submenu: Menu, - ) -> Self { - let content = content.into(); - - Entry::Dropdown { content, submenu } - } -} diff --git a/web/src/lib.rs b/web/src/lib.rs index 158416b9..6b7d0115 100644 --- a/web/src/lib.rs +++ b/web/src/lib.rs @@ -74,8 +74,8 @@ pub use dodrio; pub use element::Element; pub use hasher::Hasher; pub use iced_core::{ - keyboard, mouse, Align, Background, Color, Font, HorizontalAlignment, - Length, Padding, Point, Rectangle, Size, Vector, VerticalAlignment, + keyboard, menu, mouse, Align, Background, Color, Font, HorizontalAlignment, + Length, Menu, Padding, Point, Rectangle, Size, Vector, VerticalAlignment, }; pub use iced_futures::{executor, futures, Command}; pub use subscription::Subscription; -- 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` --- core/Cargo.toml | 1 + core/src/keyboard/modifiers.rs | 76 ++++++++++++++++++++++++++--------------- examples/menu/src/main.rs | 14 ++------ examples/pane_grid/src/main.rs | 2 +- native/src/widget/text_input.rs | 35 +++++++------------ winit/src/conversion.rs | 22 ++++++------ 6 files changed, 78 insertions(+), 72 deletions(-) diff --git a/core/Cargo.toml b/core/Cargo.toml index 88f257b7..54d927af 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -8,6 +8,7 @@ license = "MIT" repository = "https://github.com/hecrj/iced" [dependencies] +bitflags = "1.2" [dependencies.palette] version = "0.5.0" diff --git a/core/src/keyboard/modifiers.rs b/core/src/keyboard/modifiers.rs index d2a0500e..383b9370 100644 --- a/core/src/keyboard/modifiers.rs +++ b/core/src/keyboard/modifiers.rs @@ -1,20 +1,53 @@ -/// The current state of the keyboard modifiers. -#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] -pub struct Modifiers { - /// Whether a shift key is pressed - pub shift: bool, +use bitflags::bitflags; - /// Whether a control key is pressed - pub control: bool, - - /// Whether an alt key is pressed - pub alt: bool, - - /// Whether a logo key is pressed (e.g. windows key, command key...) - pub logo: bool, +bitflags! { + /// The current state of the keyboard modifiers. + #[derive(Default)] + pub struct Modifiers: u32{ + /// The "shift" key. + const SHIFT = 0b100 << 0; + // const LSHIFT = 0b010 << 0; + // const RSHIFT = 0b001 << 0; + // + /// The "control" key. + const CTRL = 0b100 << 3; + // const LCTRL = 0b010 << 3; + // const RCTRL = 0b001 << 3; + // + /// The "alt" key. + const ALT = 0b100 << 6; + // const LALT = 0b010 << 6; + // const RALT = 0b001 << 6; + // + /// The "windows" key on Windows, "command" key on Mac, and + /// "super" key on Linux. + const LOGO = 0b100 << 9; + // const LLOGO = 0b010 << 9; + // const RLOGO = 0b001 << 9; + } } impl Modifiers { + /// Returns true if the [`SHIFT`] key is pressed in the [`Modifiers`]. + pub fn shift(self) -> bool { + self.contains(Self::SHIFT) + } + + /// Returns true if the [`CTRL`] key is pressed in the [`Modifiers`]. + pub fn control(self) -> bool { + self.contains(Self::CTRL) + } + + /// Returns true if the [`ALT`] key is pressed in the [`Modifiers`]. + pub fn alt(self) -> bool { + self.contains(Self::ALT) + } + + /// Returns true if the [`LOGO`] key is pressed in the [`Modifiers`]. + pub fn logo(self) -> bool { + self.contains(Self::LOGO) + } + /// Returns true if a "command key" is pressed in the [`Modifiers`]. /// /// The "command key" is the main modifier key used to issue commands in the @@ -22,24 +55,13 @@ impl Modifiers { /// /// - It is the `logo` or command key (⌘) on macOS /// - It is the `control` key on other platforms - pub fn is_command_pressed(self) -> bool { + pub fn command(self) -> bool { #[cfg(target_os = "macos")] - let is_pressed = self.logo; + let is_pressed = self.logo(); #[cfg(not(target_os = "macos"))] - let is_pressed = self.control; + let is_pressed = self.control(); is_pressed } - - /// Returns true if the current [`Modifiers`] have at least the same - /// keys pressed as the provided ones, and false otherwise. - pub fn matches(&self, modifiers: Self) -> bool { - let shift = !modifiers.shift || self.shift; - let control = !modifiers.control || self.control; - let alt = !modifiers.alt || self.alt; - let logo = !modifiers.logo || self.logo; - - shift && control && alt && logo - } } diff --git a/examples/menu/src/main.rs b/examples/menu/src/main.rs index 9d4ed71b..7403713c 100644 --- a/examples/menu/src/main.rs +++ b/examples/menu/src/main.rs @@ -43,18 +43,8 @@ impl Application for App { } fn menu(&self) -> Menu { - let alt = Modifiers { - alt: true, - control: false, - logo: false, - shift: false, - }; - let ctrl_shift = Modifiers { - control: true, - shift: true, - logo: false, - alt: false, - }; + let alt = Modifiers::ALT; + let ctrl_shift = Modifiers::CTRL | Modifiers::SHIFT; Menu::with_entries(vec![ menu::Entry::dropdown( diff --git a/examples/pane_grid/src/main.rs b/examples/pane_grid/src/main.rs index 81cf1770..3bd8aa25 100644 --- a/examples/pane_grid/src/main.rs +++ b/examples/pane_grid/src/main.rs @@ -146,7 +146,7 @@ impl Application for Example { Event::Keyboard(keyboard::Event::KeyPressed { modifiers, key_code, - }) if modifiers.is_command_pressed() => handle_hotkey(key_code), + }) if modifiers.command() => handle_hotkey(key_code), _ => None, } }) diff --git a/native/src/widget/text_input.rs b/native/src/widget/text_input.rs index 20117fa0..f06f057b 100644 --- a/native/src/widget/text_input.rs +++ b/native/src/widget/text_input.rs @@ -362,7 +362,7 @@ where Event::Keyboard(keyboard::Event::CharacterReceived(c)) if self.state.is_focused && self.state.is_pasting.is_none() - && !self.state.keyboard_modifiers.is_command_pressed() + && !self.state.keyboard_modifiers.command() && !c.is_control() => { let mut editor = @@ -450,7 +450,7 @@ where if platform::is_jump_modifier_pressed(modifiers) && !self.is_secure { - if modifiers.shift { + if modifiers.shift() { self.state .cursor .select_left_by_words(&self.value); @@ -459,7 +459,7 @@ where .cursor .move_left_by_words(&self.value); } - } else if modifiers.shift { + } else if modifiers.shift() { self.state.cursor.select_left(&self.value) } else { self.state.cursor.move_left(&self.value); @@ -469,7 +469,7 @@ where if platform::is_jump_modifier_pressed(modifiers) && !self.is_secure { - if modifiers.shift { + if modifiers.shift() { self.state .cursor .select_right_by_words(&self.value); @@ -478,14 +478,14 @@ where .cursor .move_right_by_words(&self.value); } - } else if modifiers.shift { + } else if modifiers.shift() { self.state.cursor.select_right(&self.value) } else { self.state.cursor.move_right(&self.value); } } keyboard::KeyCode::Home => { - if modifiers.shift { + if modifiers.shift() { self.state.cursor.select_range( self.state.cursor.start(&self.value), 0, @@ -495,7 +495,7 @@ where } } keyboard::KeyCode::End => { - if modifiers.shift { + if modifiers.shift() { self.state.cursor.select_range( self.state.cursor.start(&self.value), self.value.len(), @@ -505,10 +505,7 @@ where } } keyboard::KeyCode::C - if self - .state - .keyboard_modifiers - .is_command_pressed() => + if self.state.keyboard_modifiers.command() => { match self.state.cursor.selection(&self.value) { Some((start, end)) => { @@ -520,10 +517,7 @@ where } } keyboard::KeyCode::X - if self - .state - .keyboard_modifiers - .is_command_pressed() => + if self.state.keyboard_modifiers.command() => { match self.state.cursor.selection(&self.value) { Some((start, end)) => { @@ -545,7 +539,7 @@ where messages.push(message); } keyboard::KeyCode::V => { - if self.state.keyboard_modifiers.is_command_pressed() { + if self.state.keyboard_modifiers.command() { let content = match self.state.is_pasting.take() { Some(content) => content, None => { @@ -576,10 +570,7 @@ where } } keyboard::KeyCode::A - if self - .state - .keyboard_modifiers - .is_command_pressed() => + if self.state.keyboard_modifiers.command() => { self.state.cursor.select_all(&self.value); } @@ -858,9 +849,9 @@ mod platform { pub fn is_jump_modifier_pressed(modifiers: keyboard::Modifiers) -> bool { if cfg!(target_os = "macos") { - modifiers.alt + modifiers.alt() } else { - modifiers.control + modifiers.control() } } } 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(-) 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` --- core/src/menu.rs | 40 ++++++++++++++++++++++++++++++++++++---- glutin/src/application.rs | 13 ++++++++----- winit/src/application.rs | 1 + winit/src/application/state.rs | 21 ++++++++++++++++++--- winit/src/conversion.rs | 2 +- 5 files changed, 64 insertions(+), 13 deletions(-) diff --git a/core/src/menu.rs b/core/src/menu.rs index fe770216..52186140 100644 --- a/core/src/menu.rs +++ b/core/src/menu.rs @@ -4,11 +4,17 @@ use crate::keyboard::Hotkey; /// Menu representation. /// /// This can be used by `shell` implementations to create a menu. -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone)] pub struct Menu { entries: Vec>, } +impl PartialEq for Menu { + fn eq(&self, other: &Self) -> bool { + self.entries == other.entries + } +} + impl Menu { /// Creates an empty [`Menu`]. pub fn new() -> Self { @@ -27,13 +33,13 @@ impl Menu { } /// Returns a [`MenuEntry`] iterator. - pub fn iter(self) -> impl Iterator> { - self.entries.into_iter() + pub fn iter(&self) -> impl Iterator> { + self.entries.iter() } } /// Represents one of the possible entries used to build a [`Menu`]. -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone)] pub enum Entry { /// Item for a [`Menu`] Item { @@ -82,3 +88,29 @@ impl Entry { Entry::Dropdown { content, submenu } } } + +impl PartialEq for Entry { + fn eq(&self, other: &Self) -> bool { + match (self, other) { + ( + Entry::Item { + content, hotkey, .. + }, + Entry::Item { + content: other_content, + hotkey: other_hotkey, + .. + }, + ) => content == other_content && hotkey == other_hotkey, + ( + Entry::Dropdown { content, submenu }, + Entry::Dropdown { + content: other_content, + submenu: other_submenu, + }, + ) => content == other_content && submenu == other_submenu, + (Entry::Separator, Entry::Separator) => true, + _ => false, + } + } +} diff --git a/glutin/src/application.rs b/glutin/src/application.rs index a8e5dbf9..279b6c77 100644 --- a/glutin/src/application.rs +++ b/glutin/src/application.rs @@ -52,11 +52,14 @@ where runtime.track(subscription); let context = { - let builder = settings.window.into_builder( - &application.title(), - application.mode(), - event_loop.primary_monitor(), - ); + let builder = settings + .window + .into_builder( + &application.title(), + application.mode(), + event_loop.primary_monitor(), + ) + .with_menu(Some(conversion::menu(&application.menu()))); let context = ContextBuilder::new() .with_vsync(true) 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(-) 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 --- glutin/src/application.rs | 10 ++++++++++ winit/src/application.rs | 10 ++++++++++ winit/src/conversion.rs | 11 +++++++++++ 3 files changed, 31 insertions(+) diff --git a/glutin/src/application.rs b/glutin/src/application.rs index 279b6c77..991c8705 100644 --- a/glutin/src/application.rs +++ b/glutin/src/application.rs @@ -306,6 +306,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/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(-) 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. --- native/src/program.rs | 2 +- src/application.rs | 2 +- src/sandbox.rs | 2 +- winit/src/conversion.rs | 5 ++++- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/native/src/program.rs b/native/src/program.rs index 066c29d8..75fab094 100644 --- a/native/src/program.rs +++ b/native/src/program.rs @@ -11,7 +11,7 @@ pub trait Program: Sized { type Renderer: Renderer; /// The type of __messages__ your [`Program`] will produce. - type Message: std::fmt::Debug + Send; + type Message: std::fmt::Debug + Clone + Send; /// The type of [`Clipboard`] your [`Program`] will use. type Clipboard: Clipboard; diff --git a/src/application.rs b/src/application.rs index b3dae1b3..42c28f99 100644 --- a/src/application.rs +++ b/src/application.rs @@ -99,7 +99,7 @@ pub trait Application: Sized { type Executor: Executor; /// The type of __messages__ your [`Application`] will produce. - type Message: std::fmt::Debug + Send; + type Message: std::fmt::Debug + Clone + Send; /// The data needed to initialize your [`Application`]. type Flags; diff --git a/src/sandbox.rs b/src/sandbox.rs index 10b05a92..cb3cf624 100644 --- a/src/sandbox.rs +++ b/src/sandbox.rs @@ -88,7 +88,7 @@ use crate::{ /// ``` pub trait Sandbox { /// The type of __messages__ your [`Sandbox`] will produce. - type Message: std::fmt::Debug + Send; + type Message: std::fmt::Debug + Clone + Send; /// Initializes the [`Sandbox`]. /// 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(-) 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 3099f3610003f513a386125d3cb81bfbf0ffe887 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 19 Jul 2021 20:59:09 +0700 Subject: Implement `Menu::map` naively --- core/src/menu.rs | 44 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/core/src/menu.rs b/core/src/menu.rs index 52186140..e9d3d13a 100644 --- a/core/src/menu.rs +++ b/core/src/menu.rs @@ -26,15 +26,30 @@ impl Menu { Self { entries } } + /// Returns a [`MenuEntry`] iterator. + pub fn iter(&self) -> impl Iterator> { + self.entries.iter() + } + /// Adds an [`Entry`] to the [`Menu`]. pub fn push(mut self, entry: Entry) -> Self { self.entries.push(entry); self } - /// Returns a [`MenuEntry`] iterator. - pub fn iter(&self) -> impl Iterator> { - self.entries.iter() + /// Maps the `Message` of the [`Menu`] using the provided function. + /// + /// This is useful to compose menus and split them into different + /// abstraction levels. + pub fn map(self, f: &impl Fn(Message) -> B) -> Menu { + // TODO: Use a boxed trait to avoid reallocation of entries + Menu { + entries: self + .entries + .into_iter() + .map(|entry| entry.map(f)) + .collect(), + } } } @@ -71,7 +86,7 @@ impl Entry { let content = content.into(); let hotkey = hotkey.into(); - Entry::Item { + Self::Item { content, hotkey, on_activation, @@ -85,7 +100,26 @@ impl Entry { ) -> Self { let content = content.into(); - Entry::Dropdown { content, submenu } + Self::Dropdown { content, submenu } + } + + fn map(self, f: &impl Fn(Message) -> B) -> Entry { + match self { + Self::Item { + content, + hotkey, + on_activation, + } => Entry::Item { + content, + hotkey, + on_activation: f(on_activation), + }, + Self::Dropdown { content, submenu } => Entry::Dropdown { + content, + submenu: submenu.map(f), + }, + Self::Separator => Entry::Separator, + } } } -- 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 --- core/src/menu.rs | 41 ++++++++++++++++++----------------------- winit/src/conversion.rs | 10 ++++------ 2 files changed, 22 insertions(+), 29 deletions(-) diff --git a/core/src/menu.rs b/core/src/menu.rs index e9d3d13a..3ad7b7a2 100644 --- a/core/src/menu.rs +++ b/core/src/menu.rs @@ -59,7 +59,7 @@ pub enum Entry { /// Item for a [`Menu`] Item { /// The title of the item - content: String, + title: String, /// The [`Hotkey`] to activate the item, if any hotkey: Option, /// The message generated when the item is activated @@ -68,7 +68,7 @@ pub enum Entry { /// Dropdown for a [`Menu`] Dropdown { /// Title of the dropdown - content: String, + title: String, /// The submenu of the dropdown submenu: Menu, }, @@ -79,43 +79,40 @@ pub enum Entry { impl Entry { /// Creates an [`Entry::Item`]. pub fn item>( - content: S, + title: S, hotkey: impl Into>, on_activation: Message, ) -> Self { - let content = content.into(); + let title = title.into(); let hotkey = hotkey.into(); Self::Item { - content, + title, hotkey, on_activation, } } /// Creates an [`Entry::Dropdown`]. - pub fn dropdown>( - content: S, - submenu: Menu, - ) -> Self { - let content = content.into(); + pub fn dropdown>(title: S, submenu: Menu) -> Self { + let title = title.into(); - Self::Dropdown { content, submenu } + Self::Dropdown { title, submenu } } fn map(self, f: &impl Fn(Message) -> B) -> Entry { match self { Self::Item { - content, + title, hotkey, on_activation, } => Entry::Item { - content, + title, hotkey, on_activation: f(on_activation), }, - Self::Dropdown { content, submenu } => Entry::Dropdown { - content, + Self::Dropdown { title, submenu } => Entry::Dropdown { + title, submenu: submenu.map(f), }, Self::Separator => Entry::Separator, @@ -127,22 +124,20 @@ impl PartialEq for Entry { fn eq(&self, other: &Self) -> bool { match (self, other) { ( + Entry::Item { title, hotkey, .. }, Entry::Item { - content, hotkey, .. - }, - Entry::Item { - content: other_content, + title: other_title, hotkey: other_hotkey, .. }, - ) => content == other_content && hotkey == other_hotkey, + ) => title == other_title && hotkey == other_hotkey, ( - Entry::Dropdown { content, submenu }, + Entry::Dropdown { title, submenu }, Entry::Dropdown { - content: other_content, + title: other_title, submenu: other_submenu, }, - ) => content == other_content && submenu == other_submenu, + ) => title == other_title && submenu == other_submenu, (Entry::Separator, Entry::Separator) => true, _ => false, } 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` --- src/application.rs | 4 +++- winit/src/application.rs | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/application.rs b/src/application.rs index 42c28f99..ae85c841 100644 --- a/src/application.rs +++ b/src/application.rs @@ -191,7 +191,9 @@ pub trait Application: Sized { false } - /// TODO + /// Returns the current system [`Menu`] of the [`Application`]. + /// + /// By default, it returns an empty [`Menu`]. fn menu(&self) -> Menu { Menu::new() } 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 b97954a1ee3ec7bc85d1d41b397e994752ff1831 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 19 Jul 2021 21:18:54 +0700 Subject: Add a presets `Menu` to the `game_of_life` example --- core/src/menu.rs | 4 ++-- examples/game_of_life/src/main.rs | 8 ++++++++ examples/game_of_life/src/preset.rs | 13 +++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/core/src/menu.rs b/core/src/menu.rs index 3ad7b7a2..8a679085 100644 --- a/core/src/menu.rs +++ b/core/src/menu.rs @@ -41,7 +41,7 @@ impl Menu { /// /// This is useful to compose menus and split them into different /// abstraction levels. - pub fn map(self, f: &impl Fn(Message) -> B) -> Menu { + pub fn map(self, f: impl Fn(Message) -> B + Copy) -> Menu { // TODO: Use a boxed trait to avoid reallocation of entries Menu { entries: self @@ -100,7 +100,7 @@ impl Entry { Self::Dropdown { title, submenu } } - fn map(self, f: &impl Fn(Message) -> B) -> Entry { + fn map(self, f: impl Fn(Message) -> B + Copy) -> Entry { match self { Self::Item { title, diff --git a/examples/game_of_life/src/main.rs b/examples/game_of_life/src/main.rs index 64599163..877aa2d2 100644 --- a/examples/game_of_life/src/main.rs +++ b/examples/game_of_life/src/main.rs @@ -6,6 +6,7 @@ mod style; use grid::Grid; use iced::button::{self, Button}; use iced::executor; +use iced::menu::{self, Menu}; use iced::pick_list::{self, PickList}; use iced::slider::{self, Slider}; use iced::time; @@ -128,6 +129,13 @@ impl Application for GameOfLife { } } + fn menu(&self) -> Menu { + Menu::with_entries(vec![menu::Entry::dropdown( + "Presets", + Preset::menu().map(Message::PresetPicked), + )]) + } + fn view(&mut self) -> Element { let version = self.version; let selected_speed = self.next_speed.unwrap_or(self.speed); diff --git a/examples/game_of_life/src/preset.rs b/examples/game_of_life/src/preset.rs index 05157b6a..1c199a72 100644 --- a/examples/game_of_life/src/preset.rs +++ b/examples/game_of_life/src/preset.rs @@ -1,3 +1,5 @@ +use iced::menu::{self, Menu}; + #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Preset { Custom, @@ -26,6 +28,17 @@ pub static ALL: &[Preset] = &[ ]; impl Preset { + pub fn menu() -> Menu { + Menu::with_entries( + ALL.iter() + .copied() + .map(|preset| { + menu::Entry::item(preset.to_string(), None, preset) + }) + .collect(), + ) + } + pub fn life(self) -> Vec<(isize, isize)> { #[rustfmt::skip] let cells = match self { -- 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 --- Cargo.toml | 3 --- glutin/Cargo.toml | 4 ++-- winit/Cargo.toml | 4 ++-- winit/src/conversion.rs | 17 +++++------------ 4 files changed, 9 insertions(+), 19 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1a4da1e3..5b1cfb0e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -93,9 +93,6 @@ iced_core = { version = "0.4", path = "core" } iced_futures = { version = "0.3", path = "futures" } thiserror = "1.0" -[patch.crates-io] -winit = { git = "https://github.com/iced-rs/winit", rev = "327c8756f90953a6a03f818113f8566176e6eb0d"} - [target.'cfg(not(target_arch = "wasm32"))'.dependencies] iced_winit = { version = "0.3", path = "winit" } iced_glutin = { version = "0.2", path = "glutin", optional = true } diff --git a/glutin/Cargo.toml b/glutin/Cargo.toml index b42a6b36..b2a7f307 100644 --- a/glutin/Cargo.toml +++ b/glutin/Cargo.toml @@ -15,8 +15,8 @@ debug = ["iced_winit/debug"] [dependencies.glutin] version = "0.27" -# git = "https://github.com/iced-rs/glutin" -# rev = "2564d0ab87cf2ad824a2a58733aebe40dd2f29bb" +git = "https://github.com/iced-rs/glutin" +rev = "03437d8a1826d83c62017b2bb7bf18bfc9e352cc" [dependencies.iced_native] version = "0.4" 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