fltk_builder/extensions/builder/
window.rs

1use fltk::{
2    draw::Region,
3    enums::Cursor,
4    image::RgbImage,
5    prelude::{ImageExt, WidgetExt, WindowExt},
6    window::RawHandle,
7};
8
9/// Adds builder pattern friendly versions of several setter functions
10pub trait WindowBuilderExt {
11    /// Makes a window modal, should be called before `show`
12    fn as_modal(self, val: bool) -> Self;
13    /// Makes a window fullscreen
14    fn as_fullscreen(self, val: bool) -> Self;
15    /// Sets the windows icon.
16    /// Supported formats are bmp, jpeg, png and rgb.
17    fn with_icon<T: ImageExt>(self, image: Option<T>) -> Self
18    where
19        Self: Sized;
20    /// Sets the cursor style within the window.
21    /// Needs to be called after the window is shown
22    fn with_cursor(self, cursor: Cursor) -> Self;
23    /// Sets whether the window has a border
24    fn with_border(self, flag: bool) -> Self;
25    #[doc(hidden)]
26    /// Set the window associated with a raw handle.
27    /// `RawHandle` is a void pointer to: (Windows: `HWND`, X11: `Xid` (`u64`), macOS: `NSWindow`)
28    /// # Safety
29    /// The data must be valid and is OS-dependent. The window must be shown.
30    unsafe fn with_raw_handle(self, handle: RawHandle) -> Self;
31    /// Set the graphical draw region of the window
32    /// # Safety
33    /// The data must be valid.
34    unsafe fn with_region(self, region: Region) -> Self;
35    /// Set the window's minimum width, minimum height, max width and max height.
36    /// You can pass 0 as max_w and max_h to allow unlimited upward resize of the window.
37    fn with_size_range(self, min_w: i32, min_h: i32, max_w: i32, max_h: i32) -> Self;
38    /// Set the hotspot widget of the window
39    fn with_hotspot<W: WidgetExt>(self, w: &W) -> Self
40    where
41        Self: Sized;
42    /// Set the shape of the window.
43    /// Supported image formats are BMP, RGB and Pixmap.
44    /// The window covers non-transparent/non-black shape of the image.
45    /// The image must not be scaled(resized) beforehand.
46    /// The size will be adapted to the window's size
47    fn with_shape<I: ImageExt>(self, image: Option<I>) -> Self
48    where
49        Self: Sized;
50    /// Set the cursor image
51    fn with_cursor_image(self, image: RgbImage, hot_x: i32, hot_y: i32) -> Self;
52    /// Set the window's default cursor
53    fn with_default_cursor(self, cursor: Cursor) -> Self;
54    /// Set the screen number
55    fn with_screen_num(self, n: i32) -> Self;
56    /// Set the window's opacity,
57    /// Ranges from 0.0 to 1.0, where 1.0 is fully opaque and 0.0 is fully transparent.
58    /// This should be called on a shown window.
59    /// On X11, opacity support depends on the window manager and can be queried:
60    /// ```ignore
61    /// $ xprop -root _NET_SUPPORTED | grep -o _NET_WM_WINDOW_OPACITY
62    /// ```
63    fn with_opacity(self, val: f64) -> Self;
64    /// Set the window's XA_WM_CLASS property.
65    /// This should be called before showing the window
66    fn with_xclass(self, s: &str) -> Self;
67    /// removes the window border and sets the window on top, by settings the NOBORDER and OVERRIDE flags
68    fn with_override(self) -> Self;
69    /// Forces the position of the window
70    fn with_force_position(self, flag: bool) -> Self;
71}
72
73impl<E> WindowBuilderExt for E
74where
75    E: WindowExt,
76{
77    fn as_modal(mut self, val: bool) -> Self {
78        self.make_modal(val);
79        self
80    }
81
82    fn as_fullscreen(mut self, val: bool) -> Self {
83        self.fullscreen(val);
84        self
85    }
86
87    fn with_icon<T: ImageExt>(mut self, image: Option<T>) -> Self
88    where
89        Self: Sized,
90    {
91        self.set_icon(image);
92        self
93    }
94
95    fn with_cursor(mut self, cursor: Cursor) -> Self {
96        self.set_cursor(cursor);
97        self
98    }
99
100    fn with_border(mut self, flag: bool) -> Self {
101        self.set_border(flag);
102        self
103    }
104
105    unsafe fn with_raw_handle(mut self, handle: RawHandle) -> Self {
106        self.set_raw_handle(handle);
107        self
108    }
109
110    unsafe fn with_region(mut self, region: Region) -> Self {
111        self.set_region(region);
112        self
113    }
114
115    fn with_size_range(mut self, min_w: i32, min_h: i32, max_w: i32, max_h: i32) -> Self {
116        self.size_range(min_w, min_h, max_w, max_h);
117        self
118    }
119
120    fn with_hotspot<W: WidgetExt>(mut self, w: &W) -> Self
121    where
122        Self: Sized,
123    {
124        self.hotspot(w);
125        self
126    }
127
128    fn with_shape<I: ImageExt>(mut self, image: Option<I>) -> Self
129    where
130        Self: Sized,
131    {
132        self.set_shape(image);
133        self
134    }
135
136    fn with_cursor_image(mut self, image: RgbImage, hot_x: i32, hot_y: i32) -> Self {
137        self.set_cursor_image(image, hot_x, hot_y);
138        self
139    }
140
141    fn with_default_cursor(mut self, cursor: Cursor) -> Self {
142        self.default_cursor(cursor);
143        self
144    }
145
146    fn with_screen_num(mut self, n: i32) -> Self {
147        self.set_screen_num(n);
148        self
149    }
150
151    fn with_opacity(mut self, val: f64) -> Self {
152        self.set_opacity(val);
153        self
154    }
155
156    fn with_xclass(mut self, s: &str) -> Self {
157        self.set_xclass(s);
158        self
159    }
160
161    fn with_override(mut self) -> Self {
162        self.set_override();
163        self
164    }
165
166    fn with_force_position(mut self, flag: bool) -> Self {
167        self.force_position(flag);
168        self
169    }
170}