Skip to main content

Window

Struct Window 

Source
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 WindowHandler decided to close the window itself, e.g. by calling WindowContext::request_close from the user clicking an internal “close” button;
  • The WindowContext encountered 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

Source

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).

Source

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.

Source

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.

Source

pub fn size(&self) -> WindowSize

The current size of the window.

Source

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.

Source

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.

Source

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.

Source

pub fn is_open(&self) -> bool

Returns true if the window is still open, and returns false if the window was closed/dropped.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn show(&self) -> Result<(), Error>

Shows the window to the screen.

Source

pub fn hide(&self) -> Result<(), Error>

Hides the window from the screen.

The window will still exist, and it might still receive some events, but rendering will be paused and the user will not be able to see or interact with it.

Auto Trait Implementations§

§

impl !Freeze for Window

§

impl !RefUnwindSafe for Window

§

impl !Send for Window

§

impl !Sync for Window

§

impl !UnwindSafe for Window

§

impl Unpin for Window

§

impl UnsafeUnpin for Window

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more