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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
use super::{Menu, SubItems};
use crate::{AccelLabel, CheckBox};
use kas::theme::{FrameStyle, TextClass};
use kas::{layout, prelude::*};
use std::fmt::Debug;
impl_scope! {
#[derive(Clone, Debug, Default)]
#[widget {
layout = self.label;
navigable = true;
}]
pub struct MenuEntry<M: Clone + Debug + 'static> {
core: widget_core!(),
#[widget]
label: AccelLabel,
msg: M,
}
impl Layout for Self {
fn find_id(&mut self, coord: Coord) -> Option<WidgetId> {
self.rect().contains(coord).then(|| self.id())
}
fn draw(&mut self, mut draw: DrawMgr) {
draw.frame(self.rect(), FrameStyle::MenuEntry, Default::default());
self.label.draw(draw);
}
}
impl Self {
pub fn new<S: Into<AccelString>>(label: S, msg: M) -> Self {
MenuEntry {
core: Default::default(),
label: AccelLabel::new(label).with_class(TextClass::MenuLabel),
msg,
}
}
pub fn set_msg(&mut self, msg: M) {
self.msg = msg;
}
}
impl HasStr for Self {
fn get_str(&self) -> &str {
self.label.get_str()
}
}
impl SetAccel for Self {
#[inline]
fn set_accel_string(&mut self, string: AccelString) -> TkAction {
self.label.set_accel_string(string)
}
}
impl Widget for Self {
fn configure(&mut self, mgr: &mut ConfigMgr) {
mgr.add_accel_keys(self.id_ref(), self.label.keys());
}
fn handle_event(&mut self, mgr: &mut EventMgr, event: Event) -> Response {
match event {
Event::Command(cmd) if cmd.is_activate() => {
mgr.push_msg(self.msg.clone());
Response::Used
}
_ => Response::Unused,
}
}
}
impl Menu for Self {
fn sub_items(&mut self) -> Option<SubItems> {
Some(SubItems {
label: Some(&mut self.label),
..Default::default()
})
}
}
}
impl_scope! {
#[autoimpl(Debug)]
#[autoimpl(HasBool using self.checkbox)]
#[derive(Clone, Default)]
#[widget {
layout = row: [self.checkbox, self.label];
}]
pub struct MenuToggle {
core: widget_core!(),
#[widget]
checkbox: CheckBox,
#[widget]
label: AccelLabel,
}
impl Layout for Self {
fn find_id(&mut self, coord: Coord) -> Option<WidgetId> {
self.rect().contains(coord).then(|| self.checkbox.id())
}
fn draw(&mut self, mut draw: DrawMgr) {
let mut draw = draw.re_id(self.checkbox.id());
draw.frame(self.rect(), FrameStyle::MenuEntry, Default::default());
<Self as layout::AutoLayout>::draw(self, draw);
}
}
impl Widget for Self {
fn configure(&mut self, mgr: &mut ConfigMgr) {
mgr.add_accel_keys(self.checkbox.id_ref(), self.label.keys());
}
}
impl Menu for Self {
fn sub_items(&mut self) -> Option<SubItems> {
Some(SubItems {
label: Some(&mut self.label),
toggle: Some(&mut self.checkbox),
..Default::default()
})
}
}
impl MenuToggle {
#[inline]
pub fn new<T: Into<AccelString>>(label: T) -> Self {
MenuToggle {
core: Default::default(),
checkbox: CheckBox::new(),
label: AccelLabel::new(label).with_class(TextClass::MenuLabel),
}
}
#[inline]
#[must_use]
pub fn on_toggle<F>(self, f: F) -> Self
where
F: Fn(&mut EventMgr, bool) + 'static,
{
MenuToggle {
core: self.core,
checkbox: self.checkbox.on_toggle(f),
label: self.label,
}
}
}
impl Self {
#[inline]
pub fn new_on<T: Into<AccelString>, F>(label: T, f: F) -> Self
where
F: Fn(&mut EventMgr, bool) + 'static,
{
MenuToggle::new(label).on_toggle(f)
}
#[inline]
#[must_use]
pub fn with_state(mut self, state: bool) -> Self {
self.checkbox = self.checkbox.with_state(state);
self
}
}
}