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