1use kas::prelude::*;
9use kas::theme::MarkStyle;
10use std::fmt::Debug;
11
12#[impl_self]
13mod Mark {
14 #[derive(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, cx: &mut SizeCx, axis: AxisInfo) -> SizeRules {
51 cx.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(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, cx: &mut SizeCx, axis: AxisInfo) -> SizeRules {
108 cx.feature(self.style.into(), axis)
109 .with_stretch(Stretch::Low)
110 }
111
112 fn draw(&self, mut draw: DrawCx) {
113 draw.mark(self.rect(), self.style);
114 }
115 }
116
117 impl Tile for Self {
118 fn tooltip(&self) -> Option<&str> {
119 Some(&self.label)
120 }
121
122 fn role(&self, cx: &mut dyn RoleCx) -> Role<'_> {
123 cx.set_label(&self.label);
124 Role::Button
125 }
126 }
127
128 impl Events for Self {
129 const REDRAW_ON_MOUSE_OVER: bool = true;
130
131 type Data = ();
132
133 fn handle_event(&mut self, cx: &mut EventCx, _: &Self::Data, event: Event) -> IsUsed {
134 event.on_click(cx, self.id(), |cx| cx.push(self.msg.clone()))
135 }
136
137 fn handle_messages(&mut self, cx: &mut EventCx, _: &Self::Data) {
138 if let Some(kas::messages::Activate(code)) = cx.try_pop() {
139 cx.push(self.msg.clone());
140 cx.depress_with_key(&self, code);
141 }
142 }
143 }
144}