summaryrefslogtreecommitdiffstats
path: root/examples/menu/src/main.rs
blob: 810afa00a73e4b8e37377b41be248b8007230bcd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
use iced::menu::{self, Menu};
use iced::{
    executor, Application, 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) -> 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()
    }
}