Skip to main content

WinitPlatformExt

Trait WinitPlatformExt 

pub trait WinitPlatformExt {
    // Required methods
    fn launch_window(
        &self,
        window_config: WindowConfig,
    ) -> impl Future<Output = WindowId>;
    fn close_window(&self, window_id: WindowId);
    fn focus_window(&self, window_id: Option<WindowId>);
    fn with_window(
        &self,
        window_id: Option<WindowId>,
        callback: impl FnOnce(&mut Window) + 'static,
    );
}
Expand description

Extension trait that adds winit-specific window management capabilities to Platform.

Required Methods§

fn launch_window( &self, window_config: WindowConfig, ) -> impl Future<Output = WindowId>

Dynamically launch a new window at runtime with the given configuration.

This is meant to create windows on the fly after the application has started, as opposed to the initial windows registered via crate::config::LaunchConfig.

Returns the WindowId of the newly created window once it has been created.

§Example
use freya::prelude::*;

async fn open_new_window() {
    let window_id = Platform::get()
        .launch_window(WindowConfig::new(my_app).with_title("New Window"))
        .await;
}

fn close_window(&self, window_id: WindowId)

Close an existing window by its WindowId.

§Example
use freya::{
    prelude::*,
    winit::window::WindowId,
};

fn close_window(window_id: WindowId) {
    Platform::get().close_window(window_id);
}

fn focus_window(&self, window_id: Option<WindowId>)

Focus a window by its WindowId.

If window_id is None, the current window will be focused.

§Example
use freya::{
    prelude::*,
    winit::window::WindowId,
};

fn focus_specific_window(window_id: WindowId) {
    Platform::get().focus_window(Some(window_id));
}

fn focus_current_window() {
    Platform::get().focus_window(None);
}

fn with_window( &self, window_id: Option<WindowId>, callback: impl FnOnce(&mut Window) + 'static, )

Execute a callback with mutable access to a Window.

If window_id is None, the callback will be executed on the current window. This allows direct manipulation of the underlying winit Window for advanced use cases.

To create new windows dynamically, see WinitPlatformExt::launch_window().

§Example
use freya::{
    prelude::*,
    winit::window::WindowId,
};

fn set_window_title(window_id: Option<WindowId>, title: &'static str) {
    Platform::get().with_window(window_id, move |window| {
        window.set_title(title);
    });
}

fn minimize_current_window() {
    Platform::get().with_window(None, |window| {
        window.set_minimized(true);
    });
}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§