2019-11-25 14:17:13 +01:00
|
|
|
//! Configure your application.
|
2020-05-21 00:37:47 +02:00
|
|
|
use crate::conversion;
|
2023-07-12 19:21:05 -07:00
|
|
|
use crate::core::window;
|
2023-02-23 09:31:48 -08:00
|
|
|
|
2020-05-21 00:37:47 +02:00
|
|
|
use winit::monitor::MonitorHandle;
|
2023-05-25 23:14:07 +02:00
|
|
|
use winit::window::WindowBuilder;
|
2020-05-21 00:37:47 +02:00
|
|
|
|
2019-11-25 14:17:13 +01:00
|
|
|
/// The settings of an application.
|
2020-07-01 06:09:39 +02:00
|
|
|
#[derive(Debug, Clone, Default)]
|
2020-03-30 18:00:15 +02:00
|
|
|
pub struct Settings<Flags> {
|
2021-08-11 19:23:05 +07:00
|
|
|
/// The identifier of the application.
|
|
|
|
|
///
|
|
|
|
|
/// If provided, this identifier may be used to identify the application or
|
|
|
|
|
/// communicate with it through the windowing system.
|
|
|
|
|
pub id: Option<String>,
|
|
|
|
|
|
2022-11-05 01:43:28 +01:00
|
|
|
/// The [`Window`] settings.
|
2023-07-12 19:21:05 -07:00
|
|
|
pub window: window::Settings,
|
2019-12-29 12:29:47 +01:00
|
|
|
|
2020-03-30 18:00:15 +02:00
|
|
|
/// The data needed to initialize an [`Application`].
|
|
|
|
|
///
|
2020-11-25 07:11:27 +01:00
|
|
|
/// [`Application`]: crate::Application
|
2020-03-30 18:00:15 +02:00
|
|
|
pub flags: Flags,
|
2021-03-30 21:44:19 +02:00
|
|
|
|
|
|
|
|
/// Whether the [`Application`] should exit when the user requests the
|
|
|
|
|
/// window to close (e.g. the user presses the close button).
|
2022-04-30 14:20:52 +02:00
|
|
|
///
|
2023-07-12 19:21:05 -07:00
|
|
|
/// With a [`multi_window::Application`] this will instead be used to determine whether the
|
|
|
|
|
/// application should exit when the "main"" window is closed, i.e. the first window created on
|
|
|
|
|
/// app launch.
|
|
|
|
|
///
|
2022-04-30 14:20:52 +02:00
|
|
|
/// [`Application`]: crate::Application
|
2021-03-30 21:44:19 +02:00
|
|
|
pub exit_on_close_request: bool,
|
2019-11-25 14:17:13 +01:00
|
|
|
}
|
|
|
|
|
|
2023-07-12 19:21:05 -07:00
|
|
|
/// Converts the window settings into a `WindowBuilder` from `winit`.
|
|
|
|
|
pub fn window_builder(
|
|
|
|
|
settings: window::Settings,
|
|
|
|
|
title: &str,
|
|
|
|
|
monitor: Option<MonitorHandle>,
|
|
|
|
|
_id: Option<String>,
|
|
|
|
|
) -> WindowBuilder {
|
|
|
|
|
let mut window_builder = WindowBuilder::new();
|
|
|
|
|
|
|
|
|
|
let (width, height) = settings.size;
|
|
|
|
|
|
|
|
|
|
window_builder = window_builder
|
|
|
|
|
.with_title(title)
|
|
|
|
|
.with_inner_size(winit::dpi::LogicalSize { width, height })
|
|
|
|
|
.with_resizable(settings.resizable)
|
|
|
|
|
.with_decorations(settings.decorations)
|
|
|
|
|
.with_transparent(settings.transparent)
|
|
|
|
|
.with_window_icon(settings.icon.and_then(conversion::icon))
|
|
|
|
|
.with_window_level(conversion::window_level(settings.level))
|
|
|
|
|
.with_visible(settings.visible);
|
|
|
|
|
|
|
|
|
|
if let Some(position) =
|
|
|
|
|
conversion::position(monitor.as_ref(), settings.size, settings.position)
|
|
|
|
|
{
|
|
|
|
|
window_builder = window_builder.with_position(position);
|
2023-02-23 09:31:48 -08:00
|
|
|
}
|
2020-05-21 00:37:47 +02:00
|
|
|
|
2023-07-12 19:21:05 -07:00
|
|
|
if let Some((width, height)) = settings.min_size {
|
2020-05-21 00:37:47 +02:00
|
|
|
window_builder = window_builder
|
2023-07-12 19:21:05 -07:00
|
|
|
.with_min_inner_size(winit::dpi::LogicalSize { width, height });
|
|
|
|
|
}
|
2020-06-25 00:32:41 +02:00
|
|
|
|
2023-07-12 19:21:05 -07:00
|
|
|
if let Some((width, height)) = settings.max_size {
|
|
|
|
|
window_builder = window_builder
|
|
|
|
|
.with_max_inner_size(winit::dpi::LogicalSize { width, height });
|
|
|
|
|
}
|
2020-06-25 00:32:41 +02:00
|
|
|
|
2023-07-12 19:21:05 -07:00
|
|
|
#[cfg(any(
|
|
|
|
|
target_os = "linux",
|
|
|
|
|
target_os = "dragonfly",
|
|
|
|
|
target_os = "freebsd",
|
|
|
|
|
target_os = "netbsd",
|
|
|
|
|
target_os = "openbsd"
|
|
|
|
|
))]
|
|
|
|
|
{
|
|
|
|
|
// `with_name` is available on both `WindowBuilderExtWayland` and `WindowBuilderExtX11` and they do
|
|
|
|
|
// exactly the same thing. We arbitrarily choose `WindowBuilderExtWayland` here.
|
|
|
|
|
use ::winit::platform::wayland::WindowBuilderExtWayland;
|
|
|
|
|
|
|
|
|
|
if let Some(id) = _id {
|
|
|
|
|
window_builder = window_builder.with_name(id.clone(), id);
|
2020-11-06 09:13:59 +01:00
|
|
|
}
|
2023-07-12 19:21:05 -07:00
|
|
|
}
|
2020-11-06 09:13:59 +01:00
|
|
|
|
2023-07-12 19:21:05 -07:00
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
|
{
|
|
|
|
|
use winit::platform::windows::WindowBuilderExtWindows;
|
|
|
|
|
#[allow(unsafe_code)]
|
|
|
|
|
unsafe {
|
2021-05-27 14:22:11 +02:00
|
|
|
window_builder = window_builder
|
2023-07-12 19:21:05 -07:00
|
|
|
.with_parent_window(settings.platform_specific.parent);
|
2020-08-18 05:56:15 +07:00
|
|
|
}
|
2023-07-12 19:21:05 -07:00
|
|
|
window_builder = window_builder
|
|
|
|
|
.with_drag_and_drop(settings.platform_specific.drag_and_drop);
|
|
|
|
|
}
|
2020-05-21 00:37:47 +02:00
|
|
|
|
2023-07-12 19:21:05 -07:00
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
{
|
|
|
|
|
use winit::platform::macos::WindowBuilderExtMacOS;
|
2020-12-06 11:13:53 +01:00
|
|
|
|
2023-07-12 19:21:05 -07:00
|
|
|
window_builder = window_builder
|
|
|
|
|
.with_title_hidden(settings.platform_specific.title_hidden)
|
|
|
|
|
.with_titlebar_transparent(
|
|
|
|
|
settings.platform_specific.titlebar_transparent,
|
|
|
|
|
)
|
|
|
|
|
.with_fullsize_content_view(
|
|
|
|
|
settings.platform_specific.fullsize_content_view,
|
|
|
|
|
);
|
2020-05-21 00:37:47 +02:00
|
|
|
}
|
|
|
|
|
|
2023-07-12 19:21:05 -07:00
|
|
|
window_builder
|
2019-11-25 14:17:13 +01:00
|
|
|
}
|