diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/game_of_life/src/main.rs | 13 | ||||
| -rw-r--r-- | examples/game_of_life/src/preset.rs | 13 | ||||
| -rw-r--r-- | examples/menu/Cargo.toml | 10 | ||||
| -rw-r--r-- | examples/menu/src/main.rs | 117 | ||||
| -rw-r--r-- | examples/pane_grid/src/main.rs | 2 | ||||
| -rw-r--r-- | examples/url_handler/Cargo.toml | 10 | ||||
| -rw-r--r-- | examples/url_handler/src/main.rs | 73 | 
7 files changed, 237 insertions, 1 deletions
| 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() +    } +} | 
