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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use gtk::prelude::{BoxExt, ButtonExt, GtkWindowExt, LabelExt, MenuButtonExt, OrientableExt};
use nrelm::actions::{AccelsPlus, ActionablePlus, RelmAction, RelmActionGroup};
use nrelm::{ComponentParts, ComponentSender, RelmApp, RelmWidgetExt, SimpleComponent};
#[derive(Default)]
struct App {
counter: u8,
}
#[derive(Debug)]
enum Msg {
Increment,
Decrement,
}
#[nrelm::component]
impl SimpleComponent for App {
type Init = u8;
type Input = Msg;
type Output = ();
view! {
#[root]
main_window = gtk::Window {
set_title: "Menu example",
set_default_size: (300, 100),
gtk::Box {
set_orientation: gtk::Orientation::Vertical,
set_margin_all: 5,
set_spacing: 5,
gtk::Button {
set_label: "Increment",
connect_clicked => Msg::Increment,
ActionablePlus::set_action::<ExampleU8Action>: 1,
},
gtk::Button {
set_label: "Decrement",
connect_clicked => Msg::Decrement,
},
gtk::Label {
set_margin_all: 5,
#[watch]
set_label: &format!("Counter: {}", model.counter),
},
#[name(menu_button)]
gtk::MenuButton {
set_label: "Menu",
}
},
}
}
menu! {
main_menu: {
"Example" => ExampleAction,
"Example2" => ExampleAction,
"Example toggle" => ExampleU8Action(1_u8),
section! {
"Section example" => ExampleAction,
"Example toggle" => ExampleU8Action(1_u8),
},
section! {
"Example" => ExampleAction,
"Example2" => ExampleAction,
"Example Value" => ExampleU8Action(1_u8),
},
"submenu1" {
"Example" => ExampleAction,
"Example2" => ExampleAction,
"Example toggle" => ExampleU8Action(1_u8),
"submenu2" {
"Example" => ExampleAction,
"Example2" => ExampleAction,
"Example toggle" => ExampleU8Action(1_u8),
"submenu3" {
"Example" => ExampleAction,
"Example2" => ExampleAction,
"Example toggle" => ExampleU8Action(1_u8),
}
}
}
}
}
fn init(
counter: Self::Init,
root: Self::Root,
sender: ComponentSender<Self>,
) -> ComponentParts<Self> {
// ============================================================
//
// You can also use menu! outside of the widget macro.
// This is the manual equivalent to the the menu! macro above.
//
// ============================================================
//
// nrelm::menu! {
// main_menu: {
// custom: "my_widget",
// "Example" => ExampleAction,
// "Example2" => ExampleAction,
// "Example toggle" => ExampleU8Action(1_u8),
// section! {
// "Section example" => ExampleAction,
// "Example toggle" => ExampleU8Action(1_u8),
// },
// section! {
// "Example" => ExampleAction,
// "Example2" => ExampleAction,
// "Example Value" => ExampleU8Action(1_u8),
// }
// }
// };
let model = Self { counter };
let widgets = view_output!();
// GTK3 has no `GtkPopover::bind_model` rendering, so create a real
// `GtkMenu` from the model and attach it to the button. GtkMenu is the
// standard way to build menus in GTK3 (removed in GTK4).
let main_menu_widget = gtk::Menu::from_model(&main_menu);
widgets.menu_button.set_popup(Some(&main_menu_widget));
let app = nrelm::main_application();
app.set_accelerators_for_action::<ExampleAction>(&["<primary>W"]);
let action: RelmAction<ExampleAction> = {
RelmAction::new_stateless(move |_| {
println!("Statelesss action!");
sender.input(Msg::Increment);
})
};
let action2: RelmAction<ExampleU8Action> =
RelmAction::new_stateful_with_target_value(&0, |_, state, _value| {
*state ^= 1;
dbg!(state);
});
let mut group = RelmActionGroup::<WindowActionGroup>::new();
group.add_action(action);
group.add_action(action2);
group.register_for_widget(&widgets.main_window);
ComponentParts { model, widgets }
}
fn update(&mut self, msg: Self::Input, _sender: ComponentSender<Self>) {
match msg {
Msg::Increment => {
self.counter = self.counter.wrapping_add(1);
}
Msg::Decrement => {
self.counter = self.counter.wrapping_sub(1);
}
}
}
}
nrelm::new_action_group!(WindowActionGroup, "win");
nrelm::new_stateless_action!(ExampleAction, WindowActionGroup, "example");
nrelm::new_stateful_action!(ExampleU8Action, WindowActionGroup, "example2", u8, u8);
fn main() {
let app = RelmApp::new("nrelm.example.menu");
app.run::<App>(0);
}