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
#[cfg(feature = "opengl")]
use crate::gl::GlConfig;
use crate::platform;
use dpi::{LogicalSize, Size};
use raw_window_handle::HasWindowHandle;
/// Settings used when creating a new window
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct WindowSettings {
/// The window title.
pub title: String,
/// The size of the window, either in physical or logical coordinates.
pub size: Size,
/// If the window is to be embedded in a parent window, the handle to that window.
///
/// If `None`, the window will be standalone.
pub parent: Option<ParentWindowHandle>,
/// If the window expects to have a parent when first displayed.
///
/// Setting this will delay the actual creation of the window until the parent is set (unless
/// the window is shown first).
///
/// If the `parent` field is already set, this does nothing and is ignored.
pub wait_for_parent: bool,
/// Whether the window can be resized.
pub resizable: bool,
pub min_size: Option<Size>,
pub max_size: Option<Size>,
/// A fallback scale factor, if Baseview couldn't get one from the platform.
///
/// If the platform does already provide an accurate scaling factor, this doesn't do anything.
///
/// If the given fallback scale factor is actually useful and different from the current one
/// (1.0 by default), this will resize and redraw the window accordingly.
///
/// # Platform compatibility notes.
///
/// On Win32, this value is used if running on early versions of Windows 10 (or earlier).
///
/// On X11, this value is used if no `Xft.dpi`setting is set.
///
/// On macOS, this function is always a no-op.
pub fallback_scale_factor: Option<f64>,
/// If provided, then an OpenGL context will be created for this window. You'll be able to
/// access this context through [crate::WindowContext::gl_context].
///
/// By default, this is set to `None`.
#[cfg(feature = "opengl")]
pub gl_config: Option<GlConfig>,
}
impl WindowSettings {
/// Creates a new [`WindowSettings`] with all default values.
#[inline]
pub fn new() -> Self {
Self::default()
}
/// Sets [`title`](Self::title) to the given value.
#[inline]
pub fn with_title(mut self, title: impl Into<String>) -> Self {
self.title = title.into();
self
}
/// Sets [`size`](Self::size) to the given value.
#[inline]
pub fn with_size(mut self, size: impl Into<Size>) -> Self {
self.size = size.into();
self
}
/// Sets [`size`](Self::size) to the given value.
#[inline]
pub fn with_parent<'a, P: HasWindowHandle + 'a>(
mut self, parent: impl Into<Option<&'a P>>,
) -> Self {
self.parent = parent.into().map(ParentWindowHandle::from_window);
self
}
/// Sets [`wait_for_parent`](Self::wait_for_parent) to `true`.
#[inline]
pub fn wait_for_parent(mut self) -> Self {
self.wait_for_parent = true;
self
}
/// Sets [`wait_for_parent`](Self::wait_for_parent) to the given value.
pub fn with_wait_for_parent(mut self, wait_for_parent: bool) -> Self {
self.wait_for_parent = wait_for_parent;
self
}
/// Sets [`fallback_scale_factor`](Self::fallback_scale_factor) to the given value.
#[inline]
pub fn with_fallback_scale_factor(mut self, scale_factor: impl Into<Option<f64>>) -> Self {
self.fallback_scale_factor = scale_factor.into();
self
}
/// Sets [`resizable`](Self::resizable) to the given value.
#[inline]
pub fn with_resizable(mut self, resizable: bool) -> Self {
self.resizable = resizable;
self
}
#[inline]
pub fn with_min_size<S: Into<Size>>(mut self, min_size: impl Into<Option<S>>) -> Self {
self.min_size = min_size.into().map(S::into);
self
}
#[inline]
pub fn with_max_size<S: Into<Size>>(mut self, max_size: impl Into<Option<S>>) -> Self {
self.max_size = max_size.into().map(S::into);
self
}
/// Sets [`gl_config`](Self::gl_config) to the given value.
#[cfg(feature = "opengl")]
#[inline]
pub fn with_gl_config(mut self, gl_config: impl Into<Option<GlConfig>>) -> Self {
self.gl_config = gl_config.into();
self
}
}
impl Default for WindowSettings {
fn default() -> Self {
Self {
title: String::from("baseview window"),
size: LogicalSize { width: 500.0, height: 400.0 }.into(),
parent: None,
wait_for_parent: false,
fallback_scale_factor: None,
resizable: true,
min_size: None,
max_size: None,
#[cfg(feature = "opengl")]
gl_config: None,
}
}
}
/// An owned handle to a parent window.
///
/// This type holds just what's needed for baseview to create a child window into this window.
///
/// This can safely be constructed from only a temporary reference to any [`HasWindowHandle`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParentWindowHandle {
pub(crate) inner: platform::ParentWindowHandle,
}
// Assert this is Send+Sync
const _: () = {
const fn foo<T: Send + Sync>() {}
foo::<ParentWindowHandle>();
};
impl ParentWindowHandle {
/// Grabs a handle to the given `parent_window`, to later create a child window in it.
pub fn from_window(parent_window: &impl HasWindowHandle) -> Self {
let inner = match platform::ParentWindowHandle::extract(parent_window) {
Ok(parent) => parent,
Err(e) => {
panic!("Invalid parent window handle: {e}")
}
};
Self { inner }
}
}
impl<W: HasWindowHandle> From<&W> for ParentWindowHandle {
fn from(window: &W) -> Self {
Self::from_window(window)
}
}
impl From<platform::ParentWindowHandle> for ParentWindowHandle {
fn from(inner: platform::ParentWindowHandle) -> Self {
Self { inner }
}
}