b3_core/
window.rs

1#[cfg(feature = "dh")]
2use b3_display_handler::{HasWindowHandler, WindowHandler};
3use dpi::{LogicalSize, PhysicalPosition, PhysicalSize, Position, Size};
4
5use crate::{
6    platform::{WindowApi, Wrapper},
7    platform_impl::WindowImpl,
8    ActiveApplication,
9    ContextOwner,
10};
11
12/// Window options.
13#[derive(Debug, Default, PartialEq, Eq, Clone, Copy)]
14pub struct WindowOptions {
15    /// Turn on/off a window title.
16    pub titled:      bool,
17    /// Allow a window to be minimized.
18    pub minimizable: bool,
19    /// Allow a window to be closed.
20    pub closable:    bool,
21    /// Allow a window to be resized.
22    pub resizable:   bool,
23    /// Allow a window to be dragged.
24    pub draggable:   bool,
25    /// Allow a window to be switched to a fullscreen mode.
26    pub fullscreen:  bool,
27    /// Show/hide a window borders.
28    pub borderless:  bool,
29}
30
31/// Initial mode
32#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
33pub enum InitMode {
34    /// A regular window with a specified frame size.
35    Default,
36    /// The window will be displayed in minimized mode.
37    Minimized,
38    /// The window will be displayed in maximized mode.
39    Maximized,
40    /// The window will be displayed in fullscreen mode.
41    Fullscreen,
42}
43
44impl Default for InitMode {
45    fn default() -> Self { Self::Default }
46}
47
48/// Window ID.
49pub type WindowId = usize;
50
51/// Applcation window.
52#[derive(Debug)]
53pub struct Window(WindowImpl);
54
55impl Window {
56    /// Returns a new builder instance.
57    pub fn builder() -> WindowBuilder { WindowBuilder::new() }
58
59    fn new(
60        ctx: &impl ContextOwner,
61        mode: InitMode,
62        options: Option<WindowOptions>,
63        size: Option<Size>,
64    ) -> Self {
65        Self(WindowImpl::new(ctx, mode, options, size))
66    }
67
68    /// Sets a window title.
69    ///
70    /// # Parameters:
71    /// * `title` - Window title.
72    pub fn set_title<S>(&mut self, title: S)
73    where
74        S: Into<String>,
75    {
76        self.0.set_title(title.into());
77    }
78
79    /// Returns a window title.
80    pub fn title(&self) -> String { self.0.title() }
81
82    /// Returns a window ID.
83    pub fn id(&self) -> WindowId { self.0.id() }
84
85    /// Sets window options.
86    ///
87    /// # Parameters:
88    /// * `options` - Window options.
89    pub fn set_options(&mut self, options: WindowOptions) { self.0.set_options(options); }
90
91    /// Retuns window options.
92    pub fn options(&self) -> WindowOptions { self.0.options() }
93
94    /// Makes a window visible.
95    ///
96    /// # Parameters:
97    /// * `app` - Active application.
98    pub fn show(&mut self, app: &ActiveApplication) { self.0.show(app); }
99
100    /// Makes a window visible.
101    ///
102    /// # Parameters:
103    /// * `app` - Active application.
104    pub fn show_modal(&mut self, app: &ActiveApplication) { self.0.show_modal(app); }
105
106    /// Toggles the fullscreen mode of the window.
107    pub fn toggle_fullscreen(&mut self) { self.0.toggle_fullscreen(); }
108
109    /// Returns if a window is in the fullscreen mode.
110    pub fn is_fullscreen(&self) -> bool { self.0.is_fullscreen() }
111
112    /// Sets a new frame size of the window.
113    ///
114    /// # Parameters:
115    /// * `size` - Window frame size.
116    pub fn set_frame_size(&mut self, size: Size) { self.0.set_frame_size(size); }
117
118    /// Returns a frame size of the window.
119    pub fn frame_size(&self) -> PhysicalSize<u32> { self.0.frame_size() }
120
121    /// Sets a window origin position.
122    ///
123    /// # Parameters:
124    /// * `position` - Origin position.
125    pub fn set_position(&mut self, position: Position) { self.0.set_position(position); }
126
127    /// Returns a window origin position.
128    pub fn position(&self) -> PhysicalPosition<i32> { self.0.position() }
129
130    /// Sets a minimal size of the window frame.
131    ///
132    /// # Parameters:
133    /// * `min_size` - Minimal window frame size.
134    pub fn set_min_size(&mut self, min_size: Size) { self.0.set_min_size(min_size); }
135
136    /// Returns a minimal size of the window frame.
137    pub fn min_size(&self) -> PhysicalSize<u32> { self.0.min_size() }
138
139    /// Sets a maximal size of the window frame.
140    ///
141    /// # Parameters:
142    /// * `max_size` - Maximal window frame size.
143    pub fn set_max_size(&mut self, max_size: Size) { self.0.set_max_size(max_size); }
144
145    /// Returns a maximal size of the window frame.
146    pub fn max_size(&self) -> PhysicalSize<u32> { self.0.max_size() }
147
148    /// Switches a window into the maximized mode.
149    pub fn maximize(&mut self) { self.0.maximize() }
150
151    /// Checks if a window is maximized.
152    pub fn is_maximized(&self) -> bool { self.0.is_maximized() }
153
154    /// Returns a content size (inner window size).
155    pub fn content_size(&self) -> PhysicalSize<u32> { self.0.content_size() }
156
157    /// Checks if a window is visible on the screen.
158    pub fn is_visible(&self) -> bool { self.0.is_visible() }
159
160    /// Closes a window.
161    pub fn close(&mut self) { self.0.close(); }
162
163    /// Switches a window into the minimized mode.
164    pub fn minimize(&mut self) { self.0.minimize(); }
165
166    /// Checks if a window is minimized.
167    pub fn is_minimized(&self) -> bool { self.0.is_minimized() }
168
169    /// De-minimizes window.
170    pub fn restore(&mut self) { self.0.restore(); }
171
172    /// Window backing scale factor.
173    pub fn scale_factor(&self) -> f64 { self.0.scale_factor() }
174}
175
176impl Wrapper<WindowImpl> for Window {
177    #[inline]
178    fn get_impl(&self) -> &WindowImpl { &self.0 }
179
180    #[inline]
181    fn get_impl_mut(&mut self) -> &mut WindowImpl { &mut self.0 }
182}
183
184#[cfg(feature = "dh")]
185impl HasWindowHandler for Window {
186    fn window_handler(&self) -> WindowHandler { self.0.window_handler() }
187}
188
189/// Window builder.
190#[derive(Default)]
191pub struct WindowBuilder {
192    title: Option<String>,
193    mode:  InitMode,
194    flags: Option<WindowOptions>,
195    size:  Option<Size>,
196}
197
198impl WindowBuilder {
199    #[inline]
200    fn new() -> Self {
201        Self {
202            ..Default::default()
203        }
204    }
205
206    /// Sets a title of the window under building.
207    ///
208    /// # Parameters:
209    /// * `title` - Window title.
210    pub fn with_title<S>(mut self, title: S) -> WindowBuilder
211    where
212        S: Into<String>,
213    {
214        self.title = Some(title.into());
215        self
216    }
217
218    /// Sets an initial mode of the window under building.
219    ///
220    /// # Parameters:
221    /// * `mode` - Initial mode.
222    pub fn with_init_mode(mut self, mode: InitMode) -> WindowBuilder {
223        self.mode = mode;
224        self
225    }
226
227    /// Sets a frame size of the window under building.
228    ///
229    /// # Parameters:
230    /// * `size` - Window frame size.
231    pub fn with_size(mut self, size: Size) -> WindowBuilder {
232        self.size = Some(size);
233        self
234    }
235
236    /// Sets a logical frame size of the window under building.
237    ///
238    /// # Parameters:
239    /// * `size` - Logical window frame size.
240    pub fn with_logical_size<S>(mut self, size: S) -> WindowBuilder
241    where
242        S: Into<LogicalSize<f64>>,
243    {
244        self.size = Some(Size::Logical(size.into()));
245        self
246    }
247
248    /// Sets a physical frame size of the window under building.
249    ///
250    /// # Parameters:
251    /// * `size` - Physical window frame size.
252    pub fn with_physical_size<S>(mut self, size: S) -> WindowBuilder
253    where
254        S: Into<PhysicalSize<u32>>,
255    {
256        self.size = Some(Size::Physical(size.into()));
257        self
258    }
259
260    /// Sets options of the window under building.
261    ///
262    /// # Parameters:
263    /// * `options` - Window options.
264    pub fn with_options(mut self, options: WindowOptions) -> WindowBuilder {
265        self.flags = Some(options);
266        self
267    }
268
269    /// Builds a new window instance with passed parameters.
270    ///
271    /// # Parameters:
272    /// * `ctx` - Context onwer.
273    pub fn build(self, ctx: &impl ContextOwner) -> Window {
274        let mut window = Window::new(ctx, self.mode, self.flags, self.size);
275
276        if let Some(title) = self.title {
277            window.set_title(title);
278        }
279
280        window
281    }
282}
283
284impl Drop for Window {
285    fn drop(&mut self) { self.close(); }
286}