summaryrefslogtreecommitdiffstats
path: root/winit/src/settings.rs
blob: c0b3b047c39377db8cb4be54c91a800a8b117891 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//! Configure your application.
use crate::core::window;
use crate::conversion;

use winit::monitor::MonitorHandle;
use winit::window::WindowBuilder;

/// The settings of an application.
#[derive(Debug, Clone, Default)]
pub struct Settings<Flags> {
    /// 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>,

    /// The [`Window`] settings.
    pub window: window::Settings,

    /// The data needed to initialize an [`Application`].
    ///
    /// [`Application`]: crate::Application
    pub flags: Flags,
}

/// 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);
    }

    if let Some((width, height)) = settings.min_size {
        window_builder = window_builder
            .with_min_inner_size(winit::dpi::LogicalSize { width, height });
    }

    if let Some((width, height)) = settings.max_size {
        window_builder = window_builder
            .with_max_inner_size(winit::dpi::LogicalSize { width, height });
    }

    #[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);
        }
    }

    #[cfg(target_os = "windows")]
    {
        use winit::platform::windows::WindowBuilderExtWindows;
        #[allow(unsafe_code)]
        unsafe {
            window_builder = window_builder
                .with_parent_window(settings.platform_specific.parent);
        }
        window_builder = window_builder
            .with_drag_and_drop(settings.platform_specific.drag_and_drop);
    }

    #[cfg(target_os = "macos")]
    {
        use winit::platform::macos::WindowBuilderExtMacOS;

        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,
            );
    }

    window_builder
}