freya_core/
window_config.rs

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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
use std::sync::Arc;

use dioxus_core::{
    fc_to_builder,
    Element,
};
use dioxus_core_macro::rsx;
use dioxus_signals::{
    GlobalSignal,
    Readable,
};
use freya_engine::prelude::Color;
use winit::window::{
    Icon,
    Window,
    WindowAttributes,
};

use crate::{
    event_loop_messages::{
        EventLoopMessage,
        EventLoopMessageAction,
    },
    parsing::Parse,
};

pub type WindowCallback = Box<dyn FnOnce(&mut Window) + Send + Sync>;
pub type OnCloseCallback = Box<dyn FnOnce(&mut Window) -> OnCloseResponse + Send + Sync>;
pub type WindowBuilderHook = Box<dyn FnOnce(WindowAttributes) -> WindowAttributes + Send + Sync>;

impl From<accesskit_winit::Event> for EventLoopMessage {
    fn from(value: accesskit_winit::Event) -> Self {
        Self {
            window_id: Some(value.window_id),
            action: EventLoopMessageAction::Accessibility(value.window_event),
        }
    }
}

#[derive(PartialEq)]
pub enum OnCloseResponse {
    Close,
    NotClose,
}

/// Configuration for a Window.
pub struct WindowConfig {
    pub app: Arc<dyn Fn() -> Element + Send + Sync>,
    /// Size of the Window.
    pub size: (f64, f64),
    /// Minimum size of the Window.
    pub min_size: Option<(f64, f64)>,
    /// Maximum size of the Window.
    pub max_size: Option<(f64, f64)>,
    /// Enable Window decorations.
    pub decorations: bool,
    /// Title for the Window.
    pub title: &'static str,
    /// Make the Window transparent or not.
    pub transparent: bool,
    /// Background color of the Window.
    pub background: Color,
    /// Window visibility. Default to `true`.
    pub visible: bool,
    /// The Icon of the Window.
    pub icon: Option<Icon>,
    /// Setup callback.
    pub on_setup: Option<WindowCallback>,
    /// When window gets a close request.
    pub on_close: Option<OnCloseCallback>,
    /// Hook function called with the Window Attributes.
    pub window_attributes_hook: Option<WindowBuilderHook>,
    /// Max resource in bytes to be used by the GPU. Defaults to automatic.
    pub max_gpu_resources_bytes: Option<usize>,
}

impl WindowConfig {
    /// Create a window with the given app.
    pub fn new(app: fn() -> Element) -> Self {
        #[allow(non_snake_case)]
        let App = app;
        Self::new_with_defaults(Arc::new(move || rsx!(App {})))
    }

    /// Create a window with the given app and some props to pass to it.
    pub fn new_with_props<T: dioxus_core::Properties + Sync + Send>(
        app: fn(T) -> Element,
        props: T,
    ) -> Self {
        #[allow(non_snake_case)]
        let App = app;
        Self::new_with_defaults(Arc::new(move || rsx!(App { ..props.clone() })))
    }

    fn new_with_defaults(app: Arc<dyn Fn() -> Element + Send + Sync>) -> Self {
        Self {
            app,
            size: (700.0, 500.0),
            min_size: None,
            max_size: None,
            decorations: true,
            title: "Freya",
            transparent: false,
            background: Color::WHITE,
            visible: true,
            icon: None,
            on_setup: None,
            on_close: None,
            window_attributes_hook: None,
            max_gpu_resources_bytes: None,
        }
    }

    /// Specify a Window size.
    pub fn with_size(mut self, width: f64, height: f64) -> Self {
        self.size = (width, height);
        self
    }

    /// Specify a minimum Window size.
    pub fn with_min_size(mut self, min_width: f64, min_height: f64) -> Self {
        self.min_size = Some((min_width, min_height));
        self
    }

    /// Specify a maximum Window size.
    pub fn with_max_size(mut self, max_width: f64, max_height: f64) -> Self {
        self.max_size = Some((max_width, max_height));
        self
    }

    /// Whether the Window will have decorations or not.
    pub fn with_decorations(mut self, decorations: bool) -> Self {
        self.decorations = decorations;
        self
    }

    /// Specify the Window title.
    pub fn with_title(mut self, title: &'static str) -> Self {
        self.title = title;
        self
    }

    /// Make the Window transparent or not.
    pub fn with_transparency(mut self, transparency: bool) -> Self {
        self.transparent = transparency;
        self
    }
    /// Specify the max resources to be cached for the GPU, in bytes.
    pub fn with_max_gpu_resources_bytes(mut self, max_gpu_resources_bytes: usize) -> Self {
        self.max_gpu_resources_bytes = Some(max_gpu_resources_bytes);
        self
    }
    /// Specify the Window background color.
    pub fn with_background(mut self, background: &str) -> Self {
        self.background = Color::parse(background).unwrap_or(Color::WHITE);
        self
    }

    /// Specify the Window visibility at launch.
    pub fn with_visible(mut self, visible: bool) -> Self {
        self.visible = visible;
        self
    }
    /// Specify the Window icon.
    pub fn with_icon(mut self, icon: Icon) -> Self {
        self.icon = Some(icon);
        self
    }

    /// Register a callback that will be executed when the window is created.
    pub fn on_setup(mut self, callback: impl FnOnce(&mut Window) + 'static + Send + Sync) -> Self {
        self.on_setup = Some(Box::new(callback));
        self
    }

    /// Register a callback that will be executed when the window is closed.
    pub fn on_close(
        mut self,
        callback: impl FnOnce(&mut Window) -> OnCloseResponse + 'static + Send + Sync,
    ) -> Self {
        self.on_close = Some(Box::new(callback));
        self
    }

    /// Register a Window Attributes hook.
    pub fn with_window_attributes(
        mut self,
        window_attributes_hook: impl FnOnce(WindowAttributes) -> WindowAttributes
            + 'static
            + Send
            + Sync,
    ) -> Self {
        self.window_attributes_hook = Some(Box::new(window_attributes_hook));
        self
    }
}