kas_core/widgets/
mark.rs

1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License in the LICENSE-APACHE file or at:
4//     https://www.apache.org/licenses/LICENSE-2.0
5
6//! Mark widget
7
8use kas::prelude::*;
9use kas::theme::MarkStyle;
10use std::fmt::Debug;
11
12#[impl_self]
13mod Mark {
14    /// A mark
15    ///
16    /// These are small theme-defined "glyphs"; see [`MarkStyle`]. They may be
17    /// used as icons or visual connectors. See also [`MarkButton`].
18    ///
19    /// TODO: expand or replace.
20    #[derive(Clone, Debug)]
21    #[widget]
22    pub struct Mark {
23        core: widget_core!(),
24        style: MarkStyle,
25        label: String,
26    }
27    impl Self {
28        /// Construct
29        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        /// Get mark style
38        #[inline]
39        pub fn mark(&self) -> MarkStyle {
40            self.style
41        }
42
43        /// Set mark style
44        #[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    /// A mark which is also a button
74    ///
75    /// A clickable button over a [`Mark`].
76    /// This button is not keyboard navigable; only mouse/touch interactive.
77    ///
78    /// Uses stretch policy [`Stretch::Low`].
79    ///
80    /// # Messages
81    ///
82    /// [`kas::messages::Activate`] may be used to trigger the button.
83    #[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        /// Construct
94        ///
95        /// A clone of `msg` is sent as a message on click.
96        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}