pub struct Window { /* private fields */ }Expand description
A handle to a Window created by baseview.
Unlike some other windowing libraries like winit, baseview Windows manage their own
lifecycle.
All of its events and internal operations (such as rendering) are handled in a separate
WindowHandler type, which is owned by the window itself.
Dropping this Window handle will always destroy the window, and drop its associated
WindowHandler and Host types.
§Window lifecycle and ownership
Owning this Window does not mean you fully own the window itself, per se.
While you control when the window is created, you do not have the sole control
over when it is destroyed.
The lifetime of this Window handle is going to be the longest possible lifetime for the
underlying platform window, but it can be destroyed earlier than this.
This is because while dropping this handle will always destroy the window, it can be destroyed from other factors, such as:
- The
WindowHandlerdecided to close the window itself, e.g. by callingWindowContext::request_closefrom the user clicking an internal “close” button; - The
WindowContextencountered a fatal error (e.g. during rendering) or panicked, and cannot operate anymore. - The underlying platform closed or destroyed the window directly.
- The connection to the display server (on e.g. X11) was lost.
This type makes enables to handle those cases safely: most methods will either return errors or
become no-ops. You can use the Window::is_open method to know if the window has been closed.
Implementations§
Source§impl Window
impl Window
Sourcepub fn create<H: WindowHandler>(
options: WindowSettings,
handler: impl FnOnce(WindowContext) -> Result<H, HandlerError> + Send + 'static,
) -> Result<Window, Error>
pub fn create<H: WindowHandler>( options: WindowSettings, handler: impl FnOnce(WindowContext) -> Result<H, HandlerError> + Send + 'static, ) -> Result<Window, Error>
Creates a new window, using the given WindowSettings and a builder closure to
create the associated WindowHandler.
This function creates the window but does not open or show it.
You must use the show method to actually open it
(unless you use run_until_closed, which does it automatically).
Sourcepub fn create_with_host<H: WindowHandler>(
settings: WindowSettings,
handler: impl FnOnce(WindowContext) -> Result<H, HandlerError> + Send + 'static,
host: impl Into<Option<Host>>,
) -> Result<Window, Error>
pub fn create_with_host<H: WindowHandler>( settings: WindowSettings, handler: impl FnOnce(WindowContext) -> Result<H, HandlerError> + Send + 'static, host: impl Into<Option<Host>>, ) -> Result<Window, Error>
Creates a new window, using the given WindowSettings and a builder closure to
create the associated WindowHandler, as well as an optional Host containing callbacks
to a potential system that is hosting the window (e.g. in a plug-in setting).
This function creates the window but does not open or show it.
You must use the show method to actually open it
(unless you use run_until_closed, which does it automatically).
Calling this function with None for the host value is equivalent to calling
create.
Sourcepub fn run_until_closed(self) -> Result<(), Error>
pub fn run_until_closed(self) -> Result<(), Error>
Blocks the thread and runs an event loop until the window is closed.
The window is shown automatically if it wasn’t already.
Sourcepub fn size(&self) -> WindowSize
pub fn size(&self) -> WindowSize
The current size of the window.
Sourcepub fn resize(&self, size: impl Into<Size>) -> Result<(), Error>
pub fn resize(&self, size: impl Into<Size>) -> Result<(), Error>
Resizes the window to the given Size.
The size can be provided in either physical or logical pixels.
Using this method does not trigger the HostCallbacks::request_resize callback.
Sourcepub fn suggest_fallback_scale_factor(
&self,
scale_factor: f64,
) -> Result<(), Error>
pub fn suggest_fallback_scale_factor( &self, scale_factor: f64, ) -> Result<(), Error>
Suggests 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.dpisetting is set.
On macOS, this function is always a no-op.
Sourcepub fn close(self)
pub fn close(self)
Closes and destroys the window.
This releases all resources the window uses.
It is guaranteed that no other objects (e.g. the parent window) are used by this window after this call.
Calling this method is more explicit, but otherwise identical to just dropping this Window.
Sourcepub fn is_open(&self) -> bool
pub fn is_open(&self) -> bool
Returns true if the window is still open, and returns false
if the window was closed/dropped.
Sourcepub fn is_resizable(&self) -> bool
pub fn is_resizable(&self) -> bool
Returns true if the window can be resized by the user, false otherwise.
This is set by the WindowSettings::resizable field.
Sourcepub fn min_size(&self) -> Option<Size>
pub fn min_size(&self) -> Option<Size>
Returns the minimum size of the window, if it has one.
This is set by the WindowSettings::min_size field.
Sourcepub fn max_size(&self) -> Option<Size>
pub fn max_size(&self) -> Option<Size>
Returns the minimum size of the window, if it has one.
This is set by the WindowSettings::max_size field.
Sourcepub fn host_main_thread_callback(&self)
pub fn host_main_thread_callback(&self)
Performs the work the window thread had scheduled for the main thread.
This must be called back on the main thread, as a response to HostMainThreadCaller::call_main_thread.
§Platform compatibility notes
Only the X11 platform has a separate window thread, so this is only needed to run host callbacks on X11.
On Windows and macOS, this is always a no-op.
Sourcepub fn set_parent(
&self,
parent: impl Into<ParentWindowHandle>,
) -> Result<(), Error>
pub fn set_parent( &self, parent: impl Into<ParentWindowHandle>, ) -> Result<(), Error>
Reparents this window using the given parent.
§Panics
This function can panic if the window did not have a parent, but was already created as a floating window.
This happens when the given WindowSettings had its parent set to None and
wait_for_parent set to false.