ming_wm_lib/components/
highlight_button.rs1use std::vec;
2use std::vec::Vec;
3
4use crate::components::Component;
5use crate::framebuffer_types::{ Dimensions, Point };
6use crate::themes::ThemeInfo;
7use crate::messages::WindowMessage;
8use crate::window_manager_types::DrawInstructions;
9
10pub struct HighlightButton<T> {
11 name_: String,
12 top_left: Point,
13 size: Dimensions,
14 pub text: String,
15 pub highlighted: bool,
16 click_return: T,
17 toggle_highlight_return: T, }
19
20impl<T: Clone> Component<T> for HighlightButton<T> {
21 fn handle_message(&mut self, message: WindowMessage) -> Option<T> {
22 match message {
23 WindowMessage::Focus | WindowMessage::Unfocus => {
24 self.highlighted = !self.highlighted;
25 Some(self.toggle_highlight_return.clone())
26 },
27 WindowMessage::FocusClick => {
28 Some(self.click_return.clone())
30 },
31 _ => None,
32 }
33 }
34
35 fn draw(&self, theme_info: &ThemeInfo) -> Vec<DrawInstructions> {
36 let font_height = 15;
37 if self.highlighted {
38 vec![
39 DrawInstructions::Rect(self.top_left, self.size, theme_info.top),
41 DrawInstructions::Text([self.top_left[0] + 4, self.top_left[1] + (self.size[1] - font_height) / 2], vec!["nimbus-roman".to_string()], self.text.clone(), theme_info.top_text, theme_info.top, None, None),
42 ]
43 } else {
44 vec![
45 DrawInstructions::Rect(self.top_left, self.size, theme_info.background),
46 DrawInstructions::Text([self.top_left[0] + 4, self.top_left[1] + (self.size[1] - font_height) / 2], vec!["nimbus-roman".to_string()], self.text.clone(), theme_info.text, theme_info.background, None, None),
47 ]
48 }
49 }
50
51 fn focusable(&self) -> bool {
53 true
54 }
55
56 fn clickable(&self) -> bool {
57 true
58 }
59
60 fn name(&self) -> &String {
61 &self.name_
62 }
63}
64
65impl<T> HighlightButton<T> {
66 pub fn new(name_: String, top_left: Point, size: Dimensions, text: String, click_return: T, toggle_highlight_return: T, highlighted: bool) -> Self {
67 Self {
68 name_,
69 top_left,
70 size,
71 text,
72 click_return,
73 toggle_highlight_return,
74 highlighted,
75 }
76 }
77}
78