browser_window/window/
builder.rs

1use unsafe_send_sync::UnsafeSend;
2
3use crate::{application::*, core::prelude::*, window::*, HasHandle};
4
5
6/// Exposes functionality related to constructing a window.
7pub struct WindowBuilder {
8	pub(crate) borders: bool,
9	pub(crate) height: Option<u32>,
10	pub(crate) minimizable: bool,
11	pub(crate) parent: Option<UnsafeSend<WindowImpl>>,
12	pub(crate) resizable: bool,
13	pub(crate) title: Option<String>,
14	pub(crate) width: Option<u32>,
15}
16
17#[allow(dead_code)]
18pub type WindowOptions = cbw_WindowOptions;
19
20impl WindowBuilder {
21	/// Sets whether or not the window has borders.
22	/// Default is true.
23	pub fn borders(&mut self, value: bool) { self.borders = value; }
24
25	// TODO: Create a Window struct that can be created with this method.
26	#[allow(dead_code)]
27	fn build(self, app: ApplicationHandle) {
28		// Title
29		let title: &str = match self.title.as_ref() {
30			None => "",
31			Some(t) => t,
32		};
33
34		// Convert options to the FFI struct
35		let window_options = WindowOptions {
36			borders: self.borders,
37			minimizable: self.minimizable,
38			resizable: self.resizable,
39		};
40
41		// Unwrap the parent ffi handle
42		let parent_impl_handle = match self.parent {
43			None => WindowImpl::default(),
44			Some(parent) => (*parent).clone(),
45		};
46
47		let _impl_handle = WindowImpl::new(
48			app.inner,
49			parent_impl_handle,
50			title.into(),
51			self.width as _,
52			self.height as _,
53			&window_options,
54		);
55	}
56
57	/// Sets the height that the browser window will be created with initially
58	pub fn height(&mut self, height: u32) { self.height = Some(height); }
59
60	/// Sets whether or not the window has a minimize button on the title bar
61	/// Default is true
62	pub fn minimizable(&mut self, value: bool) { self.minimizable = value; }
63
64	/// Configure a parent window.
65	/// When a parent window closes, this browser window will close as well.
66	/// This could be a reference to a `Browser` or `BrowserThreaded` handle.
67	pub fn parent<W>(&mut self, bw: &W)
68	where
69		W: HasHandle<WindowHandle>,
70	{
71		self.parent = Some(UnsafeSend::new(bw.handle().0.clone()));
72	}
73
74	pub fn new() -> Self {
75		Self {
76			borders: true,
77			height: None,
78			minimizable: true,
79			parent: None,
80			resizable: true,
81			title: None,
82			width: None,
83		}
84	}
85
86	/// Sets the width and height of the browser window
87	pub fn size(&mut self, width: u32, height: u32) {
88		self.width = Some(width);
89		self.height = Some(height);
90	}
91
92	/// Sets the title of the window.
93	pub fn title<S: Into<String>>(&mut self, title: S) { self.title = Some(title.into()); }
94
95	/// Sets the width that the browser window will be created with initially.
96	pub fn width(&mut self, width: u32) { self.width = Some(width); }
97
98	/// Sets whether or not the window will be resizable.
99	/// Default is true.
100	pub fn resizable(&mut self, resizable: bool) { self.resizable = resizable; }
101}