fltk_builder/extensions/builder/
widget.rs

1use fltk::{
2    enums::{CallbackTrigger, Color, Font, FrameType, LabelType},
3    prelude::{ImageExt, WidgetExt},
4};
5
6/// Adds builder pattern friendly versions of several setter functions
7pub trait WidgetBuilderExt {
8    /// Sets the widget's color
9    fn with_color(self, color: Color) -> Self;
10    /// Sets the widget's frame type
11    fn with_frame(self, frame: FrameType) -> Self;
12    /// Sets the widget label's size
13    fn with_label_size(self, size: i32) -> Self;
14    /// Sets the widget label's color
15    fn with_label_color(self, color: Color) -> Self;
16    /// Sets the widget label's type
17    fn with_label_type(self, typ: LabelType) -> Self;
18    /// Sets the selection color of the widget
19    fn with_selection_color(self, color: Color) -> Self;
20    /// Sets the widget label's font
21    fn with_label_font(self, font: Font) -> Self;
22    /// Sets the callback when the widget is triggered (clicks for example)
23    /// takes the widget as a closure argument
24    fn with_callback<F>(self, cb: F) -> Self
25    where
26        F: FnMut(&mut Self) + 'static;
27    /// Sets the default callback trigger for a widget, equivalent to `when()`
28    fn with_trigger(self, trigger: CallbackTrigger) -> Self;
29    /// Emits a message on callback using a sender
30    fn with_emit<T: 'static + Clone + Send + Sync>(
31        self,
32        sender: fltk::app::Sender<T>,
33        msg: T,
34    ) -> Self;
35    /// Sets the image of the widget
36    fn with_image<I: ImageExt>(self, image: Option<I>) -> Self;
37    /// Sets the image of the widget scaled to the widget's size
38    fn with_image_scaled<I: ImageExt>(self, image: Option<I>) -> Self
39    where
40        Self: Sized;
41    /// Sets the deactivated image of the widget
42    fn with_deimage<I: ImageExt>(self, image: Option<I>) -> Self
43    where
44        Self: Sized;
45    /// Sets the deactivated image of the widget scaled to the widget's size
46    fn with_deimage_scaled<I: ImageExt>(self, image: Option<I>) -> Self
47    where
48        Self: Sized;
49    /// Sets the tooltip text
50    fn with_tooltip(self, txt: &str) -> Self;
51}
52
53impl<W> WidgetBuilderExt for W
54where
55    W: WidgetExt,
56{
57    fn with_color(mut self, color: Color) -> Self {
58        self.set_color(color);
59        self
60    }
61
62    fn with_frame(mut self, frame: FrameType) -> Self {
63        self.set_frame(frame);
64        self
65    }
66
67    fn with_label_size(mut self, size: i32) -> Self {
68        self.set_label_size(size);
69        self
70    }
71
72    fn with_label_color(mut self, color: Color) -> Self {
73        self.set_label_color(color);
74        self
75    }
76
77    fn with_label_type(mut self, typ: LabelType) -> Self {
78        self.set_label_type(typ);
79        self
80    }
81
82    fn with_selection_color(mut self, color: Color) -> Self {
83        self.set_selection_color(color);
84        self
85    }
86
87    fn with_label_font(mut self, font: Font) -> Self {
88        self.set_label_font(font);
89        self
90    }
91
92    fn with_callback<F>(mut self, cb: F) -> Self
93    where
94        F: FnMut(&mut Self) + 'static,
95    {
96        self.set_callback(cb);
97        self
98    }
99
100    fn with_trigger(mut self, trigger: CallbackTrigger) -> Self {
101        self.set_trigger(trigger);
102        self
103    }
104
105    fn with_emit<T: 'static + Clone + Send + Sync>(
106        mut self,
107        sender: fltk::app::Sender<T>,
108        msg: T,
109    ) -> Self {
110        self.emit(sender, msg);
111        self
112    }
113
114    fn with_image<I: ImageExt>(mut self, image: Option<I>) -> Self {
115        self.set_image(image);
116        self
117    }
118
119    fn with_image_scaled<I: ImageExt>(mut self, image: Option<I>) -> Self
120    where
121        Self: Sized,
122    {
123        self.set_image_scaled(image);
124        self
125    }
126
127    fn with_deimage<I: ImageExt>(mut self, image: Option<I>) -> Self
128    where
129        Self: Sized,
130    {
131        self.set_deimage(image);
132        self
133    }
134
135    fn with_deimage_scaled<I: ImageExt>(mut self, image: Option<I>) -> Self
136    where
137        Self: Sized,
138    {
139        self.set_deimage_scaled(image);
140        self
141    }
142
143    fn with_tooltip(mut self, txt: &str) -> Self {
144        self.set_tooltip(txt);
145        self
146    }
147}