Skip to main content

WinitWindowAccessor

Trait WinitWindowAccessor 

Source
pub trait WinitWindowAccessor: WinitWindowAccessorSealed {
    // Required methods
    fn has_winit_window(&self) -> bool;
    fn with_winit_window<T>(
        &self,
        callback: impl FnOnce(&Window) -> T,
    ) -> Option<T>;
    fn on_winit_window_event(
        &self,
        callback: impl FnMut(&Window, &WindowEvent) -> EventResult + 'static,
    );
    fn winit_window(
        &self,
    ) -> impl Future<Output = Result<Arc<Window>, PlatformError>>;
}
Expand description

This helper trait can be used to obtain access to the winit::window::Window for a given slint::Window.

Note that the association of a Slint window with a winit window relies on two factors:

  • The winit backend must be in use. You can ensure this programmatically by calling slint::BackendSelector::backend_name() with “winit” as argument.
  • The winit window must’ve been created. Windowing systems, and by extension winit, require that windows can only be properly created when certain conditions of the event loop are met. For example, on Android the application can’t be suspended. Therefore, functions like Self::has_winit_window() or Self::with_winit_window() will only succeed when the event loop is active. This is typically the case when callbacks are invoked from the event loop, such as through timers, user input events, or when window receives events (see also Self::on_winit_window_event()).

Required Methods§

Source

fn has_winit_window(&self) -> bool

Returns true if a winit::window::Window exists for this window. This is the case if the window is backed by this winit backend.

Source

fn with_winit_window<T>(&self, callback: impl FnOnce(&Window) -> T) -> Option<T>

Invokes the specified callback with a reference to the winit::window::Window that exists for this Slint window and returns Some(T); otherwise None.

Source

fn on_winit_window_event( &self, callback: impl FnMut(&Window, &WindowEvent) -> EventResult + 'static, )

Registers a window event filter callback for this Slint window.

The callback is invoked in the winit event loop whenever a window event is received with a reference to the slint::Window and the winit::event::WindowEvent. The return value of the callback specifies whether Slint should handle this event.

If this window is not backed by winit, this function is a no-op.

Source

fn winit_window( &self, ) -> impl Future<Output = Result<Arc<Window>, PlatformError>>

Returns a future that resolves to the winit::window::Window for this Slint window. When the future is ready, the output it resolves to is either Ok(Arc<winit::window::Window>) if the window exists, or an error if the window has been deleted in the meanwhile or isn’t backed by the winit backend.

// Bring winit and accessor traits into scope.
use slint::winit_030::{WinitWindowAccessor, winit};

slint::slint!{
    import { VerticalBox, Button } from "std-widgets.slint";
    export component HelloWorld inherits Window {
        callback clicked;
        VerticalBox {
            Text {
                text: "hello world";
                color: green;
            }
            Button {
                text: "Click me";
                clicked => { root.clicked(); }
            }
        }
    }
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Make sure the winit backed is selected:
   slint::BackendSelector::new()
       .backend_name("winit".into())
       .select()?;

    let app = HelloWorld::new()?;
    let app_weak = app.as_weak();

    slint::spawn_local(async move {
        let app = app_weak.unwrap();
        let winit_window = app.window().winit_window().await.unwrap();
        eprintln!("window id = {:#?}", winit_window.id());
    }).unwrap();
    app.run()?;
    Ok(())
}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl WinitWindowAccessor for Window

Source§

fn has_winit_window(&self) -> bool

Source§

fn with_winit_window<T>(&self, callback: impl FnOnce(&Window) -> T) -> Option<T>

Source§

fn winit_window( &self, ) -> impl Future<Output = Result<Arc<Window>, PlatformError>>

Source§

fn on_winit_window_event( &self, callback: impl FnMut(&Window, &WindowEvent) -> EventResult + 'static, )

Implementors§