Skip to content

Commit 0807c31

Browse files
committed
implement menu example
1 parent 0ebb799 commit 0807c31

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed

druid/examples/menu.rs

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
// Copyright 2019 The Druid Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
//! This is a very small example of how to use menus.
16+
//! It does the almost bare minimum while still being useful.
17+
18+
// On Windows platform, don't show a console when opening the app.
19+
#![windows_subsystem = "windows"]
20+
21+
use druid::widget::prelude::*;
22+
use druid::widget::{Flex, Label};
23+
use druid::{AppDelegate, AppLauncher, Command, Data, DelegateCtx, Handled, Lens, Menu, MenuItem, Selector, Target, WidgetExt, WindowDesc};
24+
25+
const COMMAND: Selector = Selector::new("custom_Selector");
26+
27+
#[derive(Clone, Data, Lens)]
28+
struct AppState {
29+
option: bool,
30+
value: usize,
31+
}
32+
33+
pub fn main() {
34+
35+
36+
// describe the main window
37+
let main_window = WindowDesc::new(build_root_widget())
38+
.title("Hello World!")
39+
.window_size((400.0, 400.0))
40+
.menu(|_, _, _|build_menu());
41+
42+
// create the initial app state
43+
let initial_state: AppState = AppState {
44+
option: false,
45+
value: 0,
46+
};
47+
48+
// start the application. Here we pass in the application state.
49+
AppLauncher::with_window(main_window)
50+
.log_to_console()
51+
.delegate(Delegate)
52+
.launch(initial_state)
53+
.expect("Failed to launch application");
54+
}
55+
56+
fn build_root_widget() -> impl Widget<AppState> {
57+
Flex::column()
58+
.with_child(Label::new(|data: &AppState, _: &_|format!("Current value: {}", data.value)))
59+
.with_default_spacer()
60+
.with_child(Label::new(|data: &AppState, _: &_|format!("IS selected: {}", data.option)))
61+
.center()
62+
}
63+
64+
fn build_menu() -> Menu<AppState> {
65+
let menu = Menu::new("Druid Menu")
66+
.entry(
67+
MenuItem::new("Send Command")
68+
.command(COMMAND)
69+
)
70+
.separator()
71+
.entry(
72+
MenuItem::new("Change value")
73+
.on_activate(|_, data: &mut AppState, _|data.value = (data.value + 1) % 4)
74+
)
75+
.entry(
76+
MenuItem::new("1 Selected")
77+
.on_activate(|_, data: &mut AppState, _|if data.value == 1 {data.value = 0} else {data.value = 1})
78+
.selected_if(|data: &AppState, _|data.value == 1)
79+
)
80+
.entry(
81+
MenuItem::new("2 Selected")
82+
.on_activate(|_, data: &mut AppState, _|if data.value == 2 {data.value = 0} else {data.value = 2})
83+
.selected_if(|data: &AppState, _|data.value == 2)
84+
)
85+
.entry(
86+
MenuItem::new("3 Selected")
87+
.on_activate(|_, data: &mut AppState, _|if data.value == 3 {data.value = 0} else {data.value = 3})
88+
.selected_if(|data: &AppState, _|data.value == 3)
89+
)
90+
.separator()
91+
.entry(
92+
MenuItem::new("CheckBox")
93+
.on_activate(|_, data: &mut AppState, _|data.option = !data.option)
94+
.selected_if(|data: &AppState, _|data.option)
95+
)
96+
.entry(
97+
MenuItem::new("Disabled")
98+
.on_activate(|_, _, _|panic!("disabled Menu Item was activated!"))
99+
.enabled(false)
100+
101+
)
102+
.entry(
103+
MenuItem::new("Disabled Selectable")
104+
.on_activate(|_, _, _|panic!("disabled Menu Item was activated!"))
105+
.selected(false)
106+
.enabled(false)
107+
)
108+
//we dont add new menu items based on data!
109+
.rebuild_on(|_, _, _|false);
110+
111+
Menu::empty()
112+
.entry(menu)
113+
114+
}
115+
116+
struct Delegate;
117+
118+
impl AppDelegate<AppState> for Delegate {
119+
fn command(&mut self, _: &mut DelegateCtx, _: Target, cmd: &Command, _: &mut AppState, _: &Env) -> Handled {
120+
if cmd.is(COMMAND) {
121+
println!("Clicked \"Send Command\"!");
122+
Handled::Yes
123+
} else {
124+
Handled::No
125+
}
126+
}
127+
}

0 commit comments

Comments
 (0)