1use kas::prelude::*;
9use kas::theme::MarkStyle;
10use std::fmt::Debug;
11
12#[impl_self]
13mod Mark {
14 #[derive(Clone, Debug)]
21 #[widget]
22 pub struct Mark {
23 core: widget_core!(),
24 style: MarkStyle,
25 label: String,
26 }
27 impl Self {
28 pub fn new(style: MarkStyle, label: impl ToString) -> Self {
30 Mark {
31 core: Default::default(),
32 style,
33 label: label.to_string(),
34 }
35 }
36
37 #[inline]
39 pub fn mark(&self) -> MarkStyle {
40 self.style
41 }
42
43 #[inline]
45 pub fn set_mark(&mut self, mark: MarkStyle) {
46 self.style = mark;
47 }
48 }
49 impl Layout for Self {
50 fn size_rules(&mut self, sizer: SizeCx, axis: AxisInfo) -> SizeRules {
51 sizer.feature(self.style.into(), axis)
52 }
53
54 fn draw(&self, mut draw: DrawCx) {
55 draw.mark(self.rect(), self.style);
56 }
57 }
58
59 impl Tile for Self {
60 fn tooltip(&self) -> Option<&str> {
61 Some(&self.label)
62 }
63
64 fn role(&self, cx: &mut dyn RoleCx) -> Role<'_> {
65 cx.set_label(&self.label);
66 Role::Indicator
67 }
68 }
69}
70
71#[impl_self]
72mod MarkButton {
73 #[derive(Clone, Debug)]
84 #[widget]
85 pub struct MarkButton<M: Clone + Debug + 'static> {
86 core: widget_core!(),
87 style: MarkStyle,
88 label: String,
89 msg: M,
90 }
91
92 impl Self {
93 pub fn new_msg(style: MarkStyle, label: impl ToString, msg: M) -> Self {
97 MarkButton {
98 core: Default::default(),
99 style,
100 label: label.to_string(),
101 msg,
102 }
103 }
104 }
105
106 impl Layout for Self {
107 fn size_rules(&mut self, sizer: SizeCx, axis: AxisInfo) -> SizeRules {
108 sizer.feature(self.style.into(), axis)
109 }
110
111 fn draw(&self, mut draw: DrawCx) {
112 draw.mark(self.rect(), self.style);
113 }
114 }
115
116 impl Tile for Self {
117 fn tooltip(&self) -> Option<&str> {
118 Some(&self.label)
119 }
120
121 fn role(&self, cx: &mut dyn RoleCx) -> Role<'_> {
122 cx.set_label(&self.label);
123 Role::Button
124 }
125 }
126
127 impl Events for Self {
128 const REDRAW_ON_MOUSE_OVER: bool = true;
129
130 type Data = ();
131
132 fn handle_event(&mut self, cx: &mut EventCx, _: &Self::Data, event: Event) -> IsUsed {
133 event.on_click(cx, self.id(), |cx| cx.push(self.msg.clone()))
134 }
135
136 fn handle_messages(&mut self, cx: &mut EventCx, _: &Self::Data) {
137 if let Some(kas::messages::Activate(code)) = cx.try_pop() {
138 cx.push(self.msg.clone());
139 cx.depress_with_key(&self, code);
140 }
141 }
142 }
143}