diff options
53 files changed, 1134 insertions, 206 deletions
diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml index 4a3aad7c..8a891884 100644 --- a/.github/FUNDING.yml +++ b/.github/FUNDING.yml @@ -1 +1,2 @@ +github: hecrj ko_fi: hecrj_ diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 3b0bf033..a00e2a22 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -11,6 +11,11 @@ jobs: - name: Install cargo-deb run: cargo install cargo-deb - uses: actions/checkout@master + - name: Install dependencies + run: | + export DEBIAN_FRONTED=noninteractive + sudo apt-get -qq update + sudo apt-get install -y libxkbcommon-dev - name: Enable Link Time Optimizations run: | echo "[profile.release]" >> Cargo.toml @@ -67,6 +72,8 @@ jobs: env: MACOSX_DEPLOYMENT_TARGET: 10.14 run: cargo build --verbose --release --package todos + - name: Open binary via double-click + run: chmod +x target/release/todos - name: Archive todos binary uses: actions/upload-artifact@v1 with: diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index bc531abf..0450f13d 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -12,6 +12,12 @@ jobs: with: rust-version: ${{ matrix.rust }} - uses: actions/checkout@master + - name: Install dependencies + if: matrix.os == 'ubuntu-latest' + run: | + export DEBIAN_FRONTED=noninteractive + sudo apt-get -qq update + sudo apt-get install -y libxkbcommon-dev - name: Run tests run: | cargo test --verbose --all @@ -84,6 +84,8 @@ members = [ "examples/todos", "examples/tour", "examples/tooltip", + "examples/url_handler", + "examples/menu", ] [dependencies] 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.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/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/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..8a679085 --- /dev/null +++ b/core/src/menu.rs @@ -0,0 +1,145 @@ +//! 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)] +pub struct Menu<Message> { + entries: Vec<Entry<Message>>, +} + +impl<Message> PartialEq for Menu<Message> { + fn eq(&self, other: &Self) -> bool { + self.entries == other.entries + } +} + +impl<Message> Menu<Message> { + /// 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<Entry<Message>>) -> Self { + Self { entries } + } + + /// Returns a [`MenuEntry`] iterator. + pub fn iter(&self) -> impl Iterator<Item = &Entry<Message>> { + self.entries.iter() + } + + /// Adds an [`Entry`] to the [`Menu`]. + pub fn push(mut self, entry: Entry<Message>) -> Self { + self.entries.push(entry); + self + } + + /// 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<B>(self, f: impl Fn(Message) -> B + Copy) -> Menu<B> { + // TODO: Use a boxed trait to avoid reallocation of entries + Menu { + entries: self + .entries + .into_iter() + .map(|entry| entry.map(f)) + .collect(), + } + } +} + +/// Represents one of the possible entries used to build a [`Menu`]. +#[derive(Debug, Clone)] +pub enum Entry<Message> { + /// Item for a [`Menu`] + Item { + /// The title of the item + title: String, + /// The [`Hotkey`] to activate the item, if any + hotkey: Option<Hotkey>, + /// The message generated when the item is activated + on_activation: Message, + }, + /// Dropdown for a [`Menu`] + Dropdown { + /// Title of the dropdown + title: String, + /// The submenu of the dropdown + submenu: Menu<Message>, + }, + /// Separator for a [`Menu`] + Separator, +} + +impl<Message> Entry<Message> { + /// Creates an [`Entry::Item`]. + pub fn item<S: Into<String>>( + title: S, + hotkey: impl Into<Option<Hotkey>>, + on_activation: Message, + ) -> Self { + let title = title.into(); + let hotkey = hotkey.into(); + + Self::Item { + title, + hotkey, + on_activation, + } + } + + /// Creates an [`Entry::Dropdown`]. + pub fn dropdown<S: Into<String>>(title: S, submenu: Menu<Message>) -> Self { + let title = title.into(); + + Self::Dropdown { title, submenu } + } + + fn map<B>(self, f: impl Fn(Message) -> B + Copy) -> Entry<B> { + match self { + Self::Item { + title, + hotkey, + on_activation, + } => Entry::Item { + title, + hotkey, + on_activation: f(on_activation), + }, + Self::Dropdown { title, submenu } => Entry::Dropdown { + title, + submenu: submenu.map(f), + }, + Self::Separator => Entry::Separator, + } + } +} + +impl<Message> PartialEq for Entry<Message> { + fn eq(&self, other: &Self) -> bool { + match (self, other) { + ( + Entry::Item { title, hotkey, .. }, + Entry::Item { + title: other_title, + hotkey: other_hotkey, + .. + }, + ) => title == other_title && hotkey == other_hotkey, + ( + Entry::Dropdown { title, submenu }, + Entry::Dropdown { + title: other_title, + submenu: other_submenu, + }, + ) => title == other_title && submenu == other_submenu, + (Entry::Separator, Entry::Separator) => true, + _ => false, + } + } +} diff --git a/examples/game_of_life/src/main.rs b/examples/game_of_life/src/main.rs index 64599163..c3e16e8b 100644 --- a/examples/game_of_life/src/main.rs +++ b/examples/game_of_life/src/main.rs @@ -6,9 +6,11 @@ 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; +use iced::window; use iced::{ Align, Application, Checkbox, Clipboard, Column, Command, Container, Element, Length, Row, Settings, Subscription, Text, @@ -19,6 +21,10 @@ use std::time::{Duration, Instant}; pub fn main() -> iced::Result { GameOfLife::run(Settings { antialiasing: true, + window: window::Settings { + position: window::Position::Centered, + ..window::Settings::default() + }, ..Settings::default() }) } @@ -128,6 +134,13 @@ impl Application for GameOfLife { } } + fn menu(&self) -> Menu<Message> { + Menu::with_entries(vec![menu::Entry::dropdown( + "Presets", + Preset::menu().map(Message::PresetPicked), + )]) + } + fn view(&mut self) -> Element<Message> { 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<Self> { + 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 { 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 <hector0193@gmail.com>"] +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..7403713c --- /dev/null +++ b/examples/menu/src/main.rs @@ -0,0 +1,117 @@ +use iced::menu::{self, Menu}; +use iced::{ + executor, Application, Clipboard, Command, Container, Element, Length, + 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<Entry>, +} + +#[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<Message>) { + (App::default(), Command::none()) + } + + fn title(&self) -> String { + String::from("Menu - Iced") + } + + fn menu(&self) -> Menu<Message> { + let alt = Modifiers::ALT; + let ctrl_shift = Modifiers::CTRL | Modifiers::SHIFT; + + Menu::with_entries(vec![ + menu::Entry::dropdown( + "First", + Menu::with_entries(vec![ + menu::Entry::item( + "One", + Hotkey::new(alt, KeyCode::F1), + Message::MenuActivated(Entry::One), + ), + menu::Entry::item( + "Two", + Hotkey::new(alt, KeyCode::F2), + Message::MenuActivated(Entry::Two), + ), + menu::Entry::Separator, + menu::Entry::item( + "Three", + Hotkey::new(alt, KeyCode::F3), + Message::MenuActivated(Entry::Three), + ), + ]), + ), + menu::Entry::dropdown( + "Second", + Menu::with_entries(vec![ + menu::Entry::item( + "A", + Hotkey::new(ctrl_shift, KeyCode::A), + Message::MenuActivated(Entry::A), + ), + menu::Entry::item( + "B", + Hotkey::new(ctrl_shift, KeyCode::B), + Message::MenuActivated(Entry::B), + ), + menu::Entry::Separator, + menu::Entry::item( + "C", + Hotkey::new(ctrl_shift, KeyCode::C), + Message::MenuActivated(Entry::C), + ), + ]), + ), + ]) + } + + fn update( + &mut self, + message: Message, + _clipboard: &mut Clipboard, + ) -> Command<Message> { + match message { + Message::MenuActivated(entry) => self.selected = Some(entry), + } + + Command::none() + } + + fn view(&mut self) -> Element<Message> { + Container::new( + Text::new(format!("Selected {:?}", self.selected)).size(48), + ) + .width(Length::Fill) + .height(Length::Fill) + .center_x() + .center_y() + .into() + } +} 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/examples/url_handler/Cargo.toml b/examples/url_handler/Cargo.toml new file mode 100644 index 00000000..911b2f25 --- /dev/null +++ b/examples/url_handler/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "url_handler" +version = "0.1.0" +authors = ["Héctor Ramón Jiménez <hector0193@gmail.com>"] +edition = "2018" +publish = false + +[dependencies] +iced = { path = "../.." } +iced_native = { path = "../../native" }
\ No newline at end of file diff --git a/examples/url_handler/src/main.rs b/examples/url_handler/src/main.rs new file mode 100644 index 00000000..f14e5227 --- /dev/null +++ b/examples/url_handler/src/main.rs @@ -0,0 +1,73 @@ +use iced::{ + executor, Application, Clipboard, Command, Container, Element, Length, + Settings, Subscription, Text, +}; +use iced_native::{ + event::{MacOS, PlatformSpecific}, + Event, +}; + +pub fn main() -> iced::Result { + App::run(Settings::default()) +} + +#[derive(Debug, Default)] +struct App { + url: Option<String>, +} + +#[derive(Debug, Clone)] +enum Message { + EventOccurred(iced_native::Event), +} + +impl Application for App { + type Executor = executor::Default; + type Message = Message; + type Flags = (); + + fn new(_flags: ()) -> (App, Command<Message>) { + (App::default(), Command::none()) + } + + fn title(&self) -> String { + String::from("Url - Iced") + } + + fn update( + &mut self, + message: Message, + _clipboard: &mut Clipboard, + ) -> Command<Message> { + match message { + Message::EventOccurred(event) => { + if let Event::PlatformSpecific(PlatformSpecific::MacOS( + MacOS::ReceivedUrl(url), + )) = event + { + self.url = Some(url); + } + } + }; + + Command::none() + } + + fn subscription(&self) -> Subscription<Message> { + iced_native::subscription::events().map(Message::EventOccurred) + } + + fn view(&mut self) -> Element<Message> { + let content = match &self.url { + Some(url) => Text::new(format!("{}", url)), + None => Text::new("No URL received yet!"), + }; + + Container::new(content.size(48)) + .width(Length::Fill) + .height(Length::Fill) + .center_x() + .center_y() + .into() + } +} diff --git a/futures/src/subscription.rs b/futures/src/subscription.rs index 27d2d295..e60ad79a 100644 --- a/futures/src/subscription.rs +++ b/futures/src/subscription.rs @@ -125,9 +125,9 @@ impl<I, O, H> std::fmt::Debug for Subscription<I, O, H> { /// - [`stopwatch`], a watch with start/stop and reset buttons showcasing how /// to listen to time. /// -/// [examples]: https://github.com/hecrj/iced/tree/0.2/examples -/// [`download_progress`]: https://github.com/hecrj/iced/tree/0.2/examples/download_progress -/// [`stopwatch`]: https://github.com/hecrj/iced/tree/0.2/examples/stopwatch +/// [examples]: https://github.com/hecrj/iced/tree/0.3/examples +/// [`download_progress`]: https://github.com/hecrj/iced/tree/0.3/examples/download_progress +/// [`stopwatch`]: https://github.com/hecrj/iced/tree/0.3/examples/stopwatch pub trait Recipe<Hasher: std::hash::Hasher, Event> { /// The events that will be produced by a [`Subscription`] with this /// [`Recipe`]. diff --git a/glow/src/widget/pane_grid.rs b/glow/src/widget/pane_grid.rs index 44f9201c..fc36862c 100644 --- a/glow/src/widget/pane_grid.rs +++ b/glow/src/widget/pane_grid.rs @@ -6,7 +6,7 @@ //! The [`pane_grid` example] showcases how to use a [`PaneGrid`] with resizing, //! drag and drop, and hotkey support. //! -//! [`pane_grid` example]: https://github.com/hecrj/iced/tree/0.2/examples/pane_grid +//! [`pane_grid` example]: https://github.com/hecrj/iced/tree/0.3/examples/pane_grid use crate::Renderer; pub use iced_graphics::pane_grid::{ diff --git a/glutin/Cargo.toml b/glutin/Cargo.toml index 5d2c5be7..b2a7f307 100644 --- a/glutin/Cargo.toml +++ b/glutin/Cargo.toml @@ -13,8 +13,10 @@ categories = ["gui"] [features] debug = ["iced_winit/debug"] -[dependencies] -glutin = "0.26" +[dependencies.glutin] +version = "0.27" +git = "https://github.com/iced-rs/glutin" +rev = "03437d8a1826d83c62017b2bb7bf18bfc9e352cc" [dependencies.iced_native] version = "0.4" diff --git a/glutin/src/application.rs b/glutin/src/application.rs index 79fcf745..991c8705 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) @@ -237,6 +240,16 @@ async fn run_instance<A, E, C>( context.window().request_redraw(); } + 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); } @@ -294,6 +307,16 @@ async fn run_instance<A, E, C>( // 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/graphics/src/widget/canvas/program.rs b/graphics/src/widget/canvas/program.rs index d703caad..85a2f67b 100644 --- a/graphics/src/widget/canvas/program.rs +++ b/graphics/src/widget/canvas/program.rs @@ -34,7 +34,7 @@ pub trait Program<Message> { /// [`Geometry`] can be easily generated with a [`Frame`] or stored in a /// [`Cache`]. /// - /// [`Frame`]: crate::widget::canvas::Cache + /// [`Frame`]: crate::widget::canvas::Frame /// [`Cache`]: crate::widget::canvas::Cache fn draw(&self, bounds: Rectangle, cursor: Cursor) -> Vec<Geometry>; diff --git a/graphics/src/widget/pane_grid.rs b/graphics/src/widget/pane_grid.rs index 93b4b672..92cdbb77 100644 --- a/graphics/src/widget/pane_grid.rs +++ b/graphics/src/widget/pane_grid.rs @@ -6,7 +6,7 @@ //! The [`pane_grid` example] showcases how to use a [`PaneGrid`] with resizing, //! drag and drop, and hotkey support. //! -//! [`pane_grid` example]: https://github.com/hecrj/iced/tree/0.2/examples/pane_grid +//! [`pane_grid` example]: https://github.com/hecrj/iced/tree/0.3/examples/pane_grid use crate::defaults; use crate::{Backend, Color, Primitive, Renderer}; use iced_native::container; diff --git a/native/src/event.rs b/native/src/event.rs index 205bb797..1c26b5f2 100644 --- a/native/src/event.rs +++ b/native/src/event.rs @@ -23,6 +23,27 @@ pub enum Event { /// A touch event Touch(touch::Event), + + /// A platform specific event + PlatformSpecific(PlatformSpecific), +} + +/// A platform specific event +#[derive(Debug, Clone, PartialEq)] +pub enum PlatformSpecific { + /// A MacOS specific event + MacOS(MacOS), +} + +/// Describes an event specific to MacOS +#[derive(Debug, Clone, PartialEq)] +pub enum MacOS { + /// Triggered when the app receives an URL from the system + /// + /// _**Note:** For this event to be triggered, the executable needs to be properly [bundled]!_ + /// + /// [bundled]: https://developer.apple.com/library/archive/documentation/CoreFoundation/Conceptual/CFBundles/BundleTypes/BundleTypes.html#//apple_ref/doc/uid/10000123i-CH101-SW19 + ReceivedUrl(String), } /// The status of an [`Event`] after being processed. diff --git a/native/src/lib.rs b/native/src/lib.rs index cd214e36..cbb02506 100644 --- a/native/src/lib.rs +++ b/native/src/lib.rs @@ -61,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}; 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/native/src/user_interface.rs b/native/src/user_interface.rs index 475faf8d..8e0d7d1c 100644 --- a/native/src/user_interface.rs +++ b/native/src/user_interface.rs @@ -16,7 +16,7 @@ use std::hash::Hasher; /// The [`integration` example] uses a [`UserInterface`] to integrate Iced in /// an existing graphical application. /// -/// [`integration` example]: https://github.com/hecrj/iced/tree/0.2/examples/integration +/// [`integration` example]: https://github.com/hecrj/iced/tree/0.3/examples/integration #[allow(missing_debug_implementations)] pub struct UserInterface<'a, Message, Renderer> { root: Element<'a, Message, Renderer>, diff --git a/native/src/widget.rs b/native/src/widget.rs index 759fe71a..43c1b023 100644 --- a/native/src/widget.rs +++ b/native/src/widget.rs @@ -99,12 +99,12 @@ use crate::{Clipboard, Hasher, Layout, Length, Point, Rectangle}; /// - [`geometry`], a custom widget showcasing how to draw geometry with the /// `Mesh2D` primitive in [`iced_wgpu`]. /// -/// [examples]: https://github.com/hecrj/iced/tree/0.2/examples -/// [`bezier_tool`]: https://github.com/hecrj/iced/tree/0.2/examples/bezier_tool -/// [`custom_widget`]: https://github.com/hecrj/iced/tree/0.2/examples/custom_widget -/// [`geometry`]: https://github.com/hecrj/iced/tree/0.2/examples/geometry +/// [examples]: https://github.com/hecrj/iced/tree/0.3/examples +/// [`bezier_tool`]: https://github.com/hecrj/iced/tree/0.3/examples/bezier_tool +/// [`custom_widget`]: https://github.com/hecrj/iced/tree/0.3/examples/custom_widget +/// [`geometry`]: https://github.com/hecrj/iced/tree/0.3/examples/geometry /// [`lyon`]: https://github.com/nical/lyon -/// [`iced_wgpu`]: https://github.com/hecrj/iced/tree/0.2/wgpu +/// [`iced_wgpu`]: https://github.com/hecrj/iced/tree/0.3/wgpu pub trait Widget<Message, Renderer> where Renderer: crate::Renderer, diff --git a/native/src/widget/image/viewer.rs b/native/src/widget/image/viewer.rs index a006c0af..405daf00 100644 --- a/native/src/widget/image/viewer.rs +++ b/native/src/widget/image/viewer.rs @@ -132,19 +132,30 @@ where ) -> layout::Node { let (width, height) = renderer.dimensions(&self.handle); - let aspect_ratio = width as f32 / height as f32; - let mut size = limits .width(self.width) .height(self.height) .resolve(Size::new(width as f32, height as f32)); - let viewport_aspect_ratio = size.width / size.height; - - if viewport_aspect_ratio > aspect_ratio { - size.width = width as f32 * size.height / height as f32; + let expansion_size = if height > width { + self.width } else { - size.height = height as f32 * size.width / width as f32; + self.height + }; + + // Only calculate viewport sizes if the images are constrained to a limited space. + // If they are Fill|Portion let them expand within their alotted space. + match expansion_size { + Length::Shrink | Length::Units(_) => { + let aspect_ratio = width as f32 / height as f32; + let viewport_aspect_ratio = size.width / size.height; + if viewport_aspect_ratio > aspect_ratio { + size.width = width as f32 * size.height / height as f32; + } else { + size.height = height as f32 * size.width / width as f32; + } + } + Length::Fill | Length::FillPortion(_) => {} } layout::Node::new(size) diff --git a/native/src/widget/pane_grid.rs b/native/src/widget/pane_grid.rs index 44028f5e..b72172cc 100644 --- a/native/src/widget/pane_grid.rs +++ b/native/src/widget/pane_grid.rs @@ -6,7 +6,7 @@ //! The [`pane_grid` example] showcases how to use a [`PaneGrid`] with resizing, //! drag and drop, and hotkey support. //! -//! [`pane_grid` example]: https://github.com/hecrj/iced/tree/0.2/examples/pane_grid +//! [`pane_grid` example]: https://github.com/hecrj/iced/tree/0.3/examples/pane_grid mod axis; mod configuration; mod content; diff --git a/native/src/widget/pane_grid/content.rs b/native/src/widget/pane_grid/content.rs index f028ec25..b0110393 100644 --- a/native/src/widget/pane_grid/content.rs +++ b/native/src/widget/pane_grid/content.rs @@ -193,18 +193,17 @@ where &mut self, layout: Layout<'_>, ) -> Option<overlay::Element<'_, Message, Renderer>> { - let body_layout = if self.title_bar.is_some() { + if let Some(title_bar) = self.title_bar.as_mut() { let mut children = layout.children(); + let title_bar_layout = children.next()?; - // Overlays only allowed in the pane body, for now at least. - let _title_bar_layout = children.next(); - - children.next()? + match title_bar.overlay(title_bar_layout) { + Some(overlay) => Some(overlay), + None => self.body.overlay(children.next()?), + } } else { - layout - }; - - self.body.overlay(body_layout) + self.body.overlay(layout) + } } } diff --git a/native/src/widget/pane_grid/title_bar.rs b/native/src/widget/pane_grid/title_bar.rs index d9d85dbb..070010f8 100644 --- a/native/src/widget/pane_grid/title_bar.rs +++ b/native/src/widget/pane_grid/title_bar.rs @@ -1,6 +1,7 @@ use crate::container; use crate::event::{self, Event}; use crate::layout; +use crate::overlay; use crate::pane_grid; use crate::{ Clipboard, Element, Hasher, Layout, Padding, Point, Rectangle, Size, @@ -242,4 +243,27 @@ where control_status.merge(title_status) } + + pub(crate) fn overlay( + &mut self, + layout: Layout<'_>, + ) -> Option<overlay::Element<'_, Message, Renderer>> { + let mut children = layout.children(); + let padded = children.next()?; + + let mut children = padded.children(); + let title_layout = children.next()?; + + let Self { + content, controls, .. + } = self; + + content.overlay(title_layout).or_else(move || { + controls.as_mut().and_then(|controls| { + let controls_layout = children.next()?; + + controls.overlay(controls_layout) + }) + }) + } } 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/src/application.rs b/src/application.rs index 14bed2cb..78280e98 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. @@ -39,15 +39,15 @@ use crate::{ /// to listen to time. /// - [`todos`], a todos tracker inspired by [TodoMVC]. /// -/// [The repository has a bunch of examples]: https://github.com/hecrj/iced/tree/0.2/examples -/// [`clock`]: https://github.com/hecrj/iced/tree/0.2/examples/clock -/// [`download_progress`]: https://github.com/hecrj/iced/tree/0.2/examples/download_progress -/// [`events`]: https://github.com/hecrj/iced/tree/0.2/examples/events -/// [`game_of_life`]: https://github.com/hecrj/iced/tree/0.2/examples/game_of_life -/// [`pokedex`]: https://github.com/hecrj/iced/tree/0.2/examples/pokedex -/// [`solar_system`]: https://github.com/hecrj/iced/tree/0.2/examples/solar_system -/// [`stopwatch`]: https://github.com/hecrj/iced/tree/0.2/examples/stopwatch -/// [`todos`]: https://github.com/hecrj/iced/tree/0.2/examples/todos +/// [The repository has a bunch of examples]: https://github.com/hecrj/iced/tree/0.3/examples +/// [`clock`]: https://github.com/hecrj/iced/tree/0.3/examples/clock +/// [`download_progress`]: https://github.com/hecrj/iced/tree/0.3/examples/download_progress +/// [`events`]: https://github.com/hecrj/iced/tree/0.3/examples/events +/// [`game_of_life`]: https://github.com/hecrj/iced/tree/0.3/examples/game_of_life +/// [`pokedex`]: https://github.com/hecrj/iced/tree/0.3/examples/pokedex +/// [`solar_system`]: https://github.com/hecrj/iced/tree/0.3/examples/solar_system +/// [`stopwatch`]: https://github.com/hecrj/iced/tree/0.3/examples/stopwatch +/// [`todos`]: https://github.com/hecrj/iced/tree/0.3/examples/todos /// [`Sandbox`]: crate::Sandbox /// [`Canvas`]: crate::widget::Canvas /// [PokéAPI]: https://pokeapi.co/ @@ -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; @@ -191,6 +191,13 @@ pub trait Application: Sized { false } + /// Returns the current system [`Menu`] of the [`Application`]. + /// + /// By default, it returns an empty [`Menu`]. + fn menu(&self) -> Menu<Self::Message> { + Menu::new() + } + /// Runs the [`Application`]. /// /// On native platforms, this method will take control of the current thread @@ -297,6 +304,10 @@ where fn should_exit(&self) -> bool { self.0.should_exit() } + + fn menu(&self) -> Menu<Self::Message> { + self.0.menu() + } } #[cfg(target_arch = "wasm32")] @@ -245,7 +245,7 @@ pub use sandbox::Sandbox; pub use settings::Settings; pub use runtime::{ - futures, Align, Background, Clipboard, Color, Command, Font, - HorizontalAlignment, Length, Point, Rectangle, Size, Subscription, Vector, - VerticalAlignment, + futures, menu, Align, Background, Clipboard, Color, Command, Font, + HorizontalAlignment, Length, Menu, Point, Rectangle, Size, Subscription, + Vector, VerticalAlignment, }; diff --git a/src/sandbox.rs b/src/sandbox.rs index 9dd17b7e..cb3cf624 100644 --- a/src/sandbox.rs +++ b/src/sandbox.rs @@ -36,19 +36,19 @@ use crate::{ /// - [`tour`], a simple UI tour that can run both on native platforms and the /// web! /// -/// [The repository has a bunch of examples]: https://github.com/hecrj/iced/tree/0.2/examples -/// [`bezier_tool`]: https://github.com/hecrj/iced/tree/0.2/examples/bezier_tool -/// [`counter`]: https://github.com/hecrj/iced/tree/0.2/examples/counter -/// [`custom_widget`]: https://github.com/hecrj/iced/tree/0.2/examples/custom_widget -/// [`geometry`]: https://github.com/hecrj/iced/tree/0.2/examples/geometry -/// [`pane_grid`]: https://github.com/hecrj/iced/tree/0.2/examples/pane_grid -/// [`progress_bar`]: https://github.com/hecrj/iced/tree/0.2/examples/progress_bar -/// [`styling`]: https://github.com/hecrj/iced/tree/0.2/examples/styling -/// [`svg`]: https://github.com/hecrj/iced/tree/0.2/examples/svg -/// [`tour`]: https://github.com/hecrj/iced/tree/0.2/examples/tour +/// [The repository has a bunch of examples]: https://github.com/hecrj/iced/tree/0.3/examples +/// [`bezier_tool`]: https://github.com/hecrj/iced/tree/0.3/examples/bezier_tool +/// [`counter`]: https://github.com/hecrj/iced/tree/0.3/examples/counter +/// [`custom_widget`]: https://github.com/hecrj/iced/tree/0.3/examples/custom_widget +/// [`geometry`]: https://github.com/hecrj/iced/tree/0.3/examples/geometry +/// [`pane_grid`]: https://github.com/hecrj/iced/tree/0.3/examples/pane_grid +/// [`progress_bar`]: https://github.com/hecrj/iced/tree/0.3/examples/progress_bar +/// [`styling`]: https://github.com/hecrj/iced/tree/0.3/examples/styling +/// [`svg`]: https://github.com/hecrj/iced/tree/0.3/examples/svg +/// [`tour`]: https://github.com/hecrj/iced/tree/0.3/examples/tour /// [`Canvas widget`]: crate::widget::Canvas /// [the overview]: index.html#overview -/// [`iced_wgpu`]: https://github.com/hecrj/iced/tree/0.2/wgpu +/// [`iced_wgpu`]: https://github.com/hecrj/iced/tree/0.3/wgpu /// [`Svg` widget]: crate::widget::Svg /// [Ghostscript Tiger]: https://commons.wikimedia.org/wiki/File:Ghostscript_Tiger.svg /// @@ -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/src/window.rs b/src/window.rs index a2883b62..7d441062 100644 --- a/src/window.rs +++ b/src/window.rs @@ -1,9 +1,11 @@ //! Configure the window of your application in native platforms. mod mode; +mod position; mod settings; pub mod icon; pub use icon::Icon; pub use mode::Mode; +pub use position::Position; pub use settings::Settings; diff --git a/src/window/position.rs b/src/window/position.rs new file mode 100644 index 00000000..8535ef6a --- /dev/null +++ b/src/window/position.rs @@ -0,0 +1,33 @@ +/// 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 + } +} + +#[cfg(not(target_arch = "wasm32"))] +impl From<Position> for iced_winit::Position { + fn from(position: Position) -> Self { + match position { + Position::Default => Self::Default, + Position::Centered => Self::Centered, + Position::Specific(x, y) => Self::Specific(x, y), + } + } +} diff --git a/src/window/settings.rs b/src/window/settings.rs index 6b5d2985..ec6c3071 100644 --- a/src/window/settings.rs +++ b/src/window/settings.rs @@ -1,4 +1,4 @@ -use crate::window::Icon; +use crate::window::{Icon, Position}; /// The window settings of an application. #[derive(Debug, Clone)] @@ -6,6 +6,9 @@ pub struct Settings { /// The initial size of the window. pub size: (u32, u32), + /// The initial position of the window. + pub position: Position, + /// The minimum size of the window. pub min_size: Option<(u32, u32)>, @@ -32,6 +35,7 @@ impl Default for Settings { fn default() -> Settings { Settings { size: (1024, 768), + position: Position::default(), min_size: None, max_size: None, resizable: true, @@ -48,6 +52,7 @@ impl From<Settings> for iced_winit::settings::Window { fn from(settings: Settings) -> Self { Self { size: settings.size, + position: iced_winit::Position::from(settings.position), min_size: settings.min_size, max_size: settings.max_size, resizable: settings.resizable, diff --git a/web/src/lib.rs b/web/src/lib.rs index e4271d58..6b7d0115 100644 --- a/web/src/lib.rs +++ b/web/src/lib.rs @@ -49,7 +49,7 @@ //! //! [`wasm-pack`]: https://github.com/rustwasm/wasm-pack //! [`wasm-bindgen`]: https://github.com/rustwasm/wasm-bindgen -//! [`tour` example]: https://github.com/hecrj/iced/tree/0.2/examples/tour +//! [`tour` example]: https://github.com/hecrj/iced/tree/0.3/examples/tour #![deny(missing_docs)] #![deny(missing_debug_implementations)] #![deny(unused_results)] @@ -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; diff --git a/wgpu/Cargo.toml b/wgpu/Cargo.toml index 3eacdd0f..9675797d 100644 --- a/wgpu/Cargo.toml +++ b/wgpu/Cargo.toml @@ -26,8 +26,8 @@ qr_code = ["iced_graphics/qr_code"] default_system_font = ["iced_graphics/font-source"] [dependencies] -wgpu = "0.8" -wgpu_glyph = "0.12" +wgpu = "0.9" +wgpu_glyph = "0.13" glyph_brush = "0.7" raw-window-handle = "0.3" log = "0.4" diff --git a/wgpu/src/image.rs b/wgpu/src/image.rs index cd7b3d23..85663bf5 100644 --- a/wgpu/src/image.rs +++ b/wgpu/src/image.rs @@ -165,33 +165,13 @@ impl Pipeline { wgpu::VertexBufferLayout { array_stride: mem::size_of::<Instance>() as u64, step_mode: wgpu::InputStepMode::Instance, - attributes: &[ - wgpu::VertexAttribute { - shader_location: 1, - format: wgpu::VertexFormat::Float32x2, - offset: 0, - }, - wgpu::VertexAttribute { - shader_location: 2, - format: wgpu::VertexFormat::Float32x2, - offset: 4 * 2, - }, - wgpu::VertexAttribute { - shader_location: 3, - format: wgpu::VertexFormat::Float32x2, - offset: 4 * 4, - }, - wgpu::VertexAttribute { - shader_location: 4, - format: wgpu::VertexFormat::Float32x2, - offset: 4 * 6, - }, - wgpu::VertexAttribute { - shader_location: 5, - format: wgpu::VertexFormat::Sint32, - offset: 4 * 8, - }, - ], + attributes: &wgpu::vertex_attr_array!( + 1 => Float32x2, + 2 => Float32x2, + 3 => Float32x2, + 4 => Float32x2, + 5 => Sint32, + ), }, ], }, diff --git a/wgpu/src/image/vector.rs b/wgpu/src/image/vector.rs index 8c7de617..cd511a45 100644 --- a/wgpu/src/image/vector.rs +++ b/wgpu/src/image/vector.rs @@ -75,8 +75,8 @@ impl Cache { let id = handle.id(); let (width, height) = ( - (scale * width).round() as u32, - (scale * height).round() as u32, + (scale * width).ceil() as u32, + (scale * height).ceil() as u32, ); // TODO: Optimize! @@ -122,6 +122,7 @@ impl Cache { device, encoder, )?; + log::debug!("allocating {} {}x{}", id, width, height); let _ = self.svg_hits.insert(id); let _ = self.rasterized_hits.insert((id, width, height)); diff --git a/wgpu/src/quad.rs b/wgpu/src/quad.rs index 6f221ca3..93942fba 100644 --- a/wgpu/src/quad.rs +++ b/wgpu/src/quad.rs @@ -87,38 +87,14 @@ impl Pipeline { wgpu::VertexBufferLayout { array_stride: mem::size_of::<layer::Quad>() as u64, step_mode: wgpu::InputStepMode::Instance, - attributes: &[ - wgpu::VertexAttribute { - shader_location: 1, - format: wgpu::VertexFormat::Float32x2, - offset: 0, - }, - wgpu::VertexAttribute { - shader_location: 2, - format: wgpu::VertexFormat::Float32x2, - offset: 4 * 2, - }, - wgpu::VertexAttribute { - shader_location: 3, - format: wgpu::VertexFormat::Float32x4, - offset: 4 * (2 + 2), - }, - wgpu::VertexAttribute { - shader_location: 4, - format: wgpu::VertexFormat::Float32x4, - offset: 4 * (2 + 2 + 4), - }, - wgpu::VertexAttribute { - shader_location: 5, - format: wgpu::VertexFormat::Float32, - offset: 4 * (2 + 2 + 4 + 4), - }, - wgpu::VertexAttribute { - shader_location: 6, - format: wgpu::VertexFormat::Float32, - offset: 4 * (2 + 2 + 4 + 4 + 1), - }, - ], + attributes: &wgpu::vertex_attr_array!( + 1 => Float32x2, + 2 => Float32x2, + 3 => Float32x4, + 4 => Float32x4, + 5 => Float32, + 6 => Float32, + ), }, ], }, diff --git a/wgpu/src/triangle.rs b/wgpu/src/triangle.rs index 8636b331..65b2b7b0 100644 --- a/wgpu/src/triangle.rs +++ b/wgpu/src/triangle.rs @@ -150,20 +150,12 @@ impl Pipeline { buffers: &[wgpu::VertexBufferLayout { array_stride: mem::size_of::<Vertex2D>() as u64, step_mode: wgpu::InputStepMode::Vertex, - attributes: &[ + attributes: &wgpu::vertex_attr_array!( // Position - wgpu::VertexAttribute { - shader_location: 0, - format: wgpu::VertexFormat::Float32x2, - offset: 0, - }, + 0 => Float32x2, // Color - wgpu::VertexAttribute { - shader_location: 1, - format: wgpu::VertexFormat::Float32x4, - offset: 4 * 2, - }, - ], + 1 => Float32x4, + ), }], }, fragment: Some(wgpu::FragmentState { diff --git a/wgpu/src/widget/pane_grid.rs b/wgpu/src/widget/pane_grid.rs index 44f9201c..fc36862c 100644 --- a/wgpu/src/widget/pane_grid.rs +++ b/wgpu/src/widget/pane_grid.rs @@ -6,7 +6,7 @@ //! The [`pane_grid` example] showcases how to use a [`PaneGrid`] with resizing, //! drag and drop, and hotkey support. //! -//! [`pane_grid` example]: https://github.com/hecrj/iced/tree/0.2/examples/pane_grid +//! [`pane_grid` example]: https://github.com/hecrj/iced/tree/0.3/examples/pane_grid use crate::Renderer; pub use iced_graphics::pane_grid::{ diff --git a/winit/Cargo.toml b/winit/Cargo.toml index 7f117fa1..b1192135 100644 --- a/winit/Cargo.toml +++ b/winit/Cargo.toml @@ -14,11 +14,15 @@ categories = ["gui"] debug = ["iced_native/debug"] [dependencies] -winit = "0.24" window_clipboard = "0.2" log = "0.4" thiserror = "1.0" +[dependencies.winit] +version = "0.25" +git = "https://github.com/iced-rs/winit" +rev = "844485272a7412cb35cdbfac3524decdf59475ca" + [dependencies.iced_native] version = "0.4" path = "../native" diff --git a/winit/src/application.rs b/winit/src/application.rs index b1d5f418..5d1aabf9 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,13 @@ pub trait Application: Program<Clipboard = Clipboard> { fn should_exit(&self) -> bool { false } + + /// Returns the current system [`Menu`] of the [`Application`]. + /// + /// By default, it returns an empty [`Menu`]. + fn menu(&self) -> Menu<Self::Message> { + Menu::new() + } } /// Runs an [`Application`] with an executor, compositor, and the provided @@ -145,6 +153,7 @@ where application.mode(), event_loop.primary_monitor(), ) + .with_menu(Some(conversion::menu(&application.menu()))) .build(&event_loop) .map_err(Error::WindowCreationFailed)?; @@ -310,6 +319,16 @@ async fn run_instance<A, E, C>( window.request_redraw(); } + 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); } @@ -370,6 +389,16 @@ async fn run_instance<A, E, C>( // 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/state.rs b/winit/src/application/state.rs index b54d3aed..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<A: Application> { title: String, + menu: Menu<A::Message>, mode: Mode, background_color: Color, scale_factor: f64, @@ -23,6 +24,7 @@ impl<A: Application> State<A> { /// 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(); @@ -38,6 +40,7 @@ impl<A: Application> State<A> { Self { title, + menu, mode, background_color, scale_factor, @@ -50,6 +53,11 @@ impl<A: Application> State<A> { } } + /// Returns the current [`Menu`] of the [`State`]. + pub fn menu(&self) -> &Menu<A::Message> { + &self.menu + } + /// Returns the current background [`Color`] of the [`State`]. pub fn background_color(&self) -> Color { self.background_color @@ -203,5 +211,14 @@ impl<A: Application> State<A> { 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 b850a805..b3d05857 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, Mode, Point}; +use crate::{Event, Mode, Point, Position}; /// Converts a winit window event into an iced event. pub fn window_event( @@ -133,6 +134,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<winit::dpi::Position> { + 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<f64> = + monitor.size().to_logical(monitor.scale_factor()); + + let centered: winit::dpi::PhysicalPosition<i32> = + winit::dpi::LogicalPosition { + x: (resolution.width - f64::from(width)) / 2.0, + y: (resolution.height - f64::from(height)) / 2.0, + } + .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 @@ -156,6 +200,110 @@ 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<Message>(menu: &Menu<Message>) -> winit::window::Menu { + fn menu_i<Message>( + converted: &mut winit::window::Menu, + starting_id: usize, + menu: &Menu<Message>, + ) -> usize { + let mut id = starting_id; + + for item in menu.iter() { + match item { + menu::Entry::Item { title, hotkey, .. } => { + converted.add_item(id, title, hotkey.map(self::hotkey)); + + id += 1; + } + 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(title, converted_submenu); + + id += n_children; + } + menu::Entry::Separator => { + converted.add_separator(); + } + } + } + + id - starting_id + } + + let mut converted = winit::window::Menu::default(); + let _ = menu_i(&mut converted, 0, 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<Message>(menu: &Menu<Message>, id: u32) -> Option<Message> +where + Message: Clone, +{ + fn find_message<Message>( + target: u32, + starting_id: u32, + menu: &Menu<Message>, + ) -> Result<Message, u32> + 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) + } + + find_message(id, 0, menu).ok() +} + /// Converts a `MouseCursor` from [`iced_native`] to a [`winit`] cursor icon. /// /// [`winit`]: https://github.com/rust-windowing/winit @@ -203,12 +351,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`. @@ -252,6 +402,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 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 941d88ce..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; @@ -35,6 +35,9 @@ pub struct Window { /// The size of the window. pub size: (u32, u32), + /// The position of the window. + pub position: Position, + /// The minimum size of the window. pub min_size: Option<(u32, u32)>, @@ -80,9 +83,16 @@ 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_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 }); @@ -104,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 } } @@ -112,6 +125,7 @@ impl Default for Window { fn default() -> Window { Window { size: (1024, 768), + position: Position::default(), min_size: None, max_size: None, resizable: true, |