xilem 0.4.0

A next-generation cross-platform Rust UI framework.
Documentation
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
// Copyright 2025 the Xilem Authors
// SPDX-License-Identifier: Apache-2.0

use winit::dpi::{Position, Size};
use winit::window::{Cursor, Icon, Window, WindowAttributes, WindowButtons, WindowLevel};

// TODO: make this a type-state builder to force Xilem::new apps to define on_close?
/// Attributes and callbacks of a window.
///
/// When passed to [`Xilem::new_simple`](crate::Xilem::new_simple) these are used to create the window.
/// When returned from the app logic function passed to [`Xilem::new`](crate::Xilem::new)
/// they can also be used to update the attributes/callbacks in the running application,
/// except if the attribute name starts with `initial_`. Attempting to change an initial-only attribute
/// won't have any effect and will result in a warning being logged.
pub struct WindowOptions<State> {
    pub(crate) reactive: ReactiveWindowAttrs,
    pub(crate) initial: InitialAttrs,
    pub(crate) callbacks: WindowCallbacks<State>,
}

/// These are attributes the user cannot change, so we can make them reactive.
#[derive(Clone, Debug)]
pub(crate) struct ReactiveWindowAttrs {
    title: String,
    resizable: bool,
    enabled_buttons: WindowButtons,
    cursor: Cursor,
    min_inner_size: Option<Size>,
    max_inner_size: Option<Size>,
    window_level: WindowLevel,
    decorations: bool,
    platform_specific: PlatformSpecificReactiveWindowAttrs,
}

/// These are attributes the user can change, so we cannot make them reactive.
#[derive(Clone, Debug)]
pub(crate) struct InitialAttrs {
    inner_size: Option<Size>,
    position: Option<Position>,
    transparent: bool,
    // TODO: move window_icon to ReactiveWindowAttrs once the winit type implements PartialEq
    window_icon: Option<Icon>,
    platform_specific: PlatformSpecificInitialWindowAttrs,
}

pub(crate) struct WindowCallbacks<State> {
    pub(crate) on_close: Option<Box<dyn Fn(&mut State)>>,
}
impl<S> Default for WindowCallbacks<S> {
    fn default() -> Self {
        Self { on_close: None }
    }
}

impl<State> WindowOptions<State> {
    /// Initializes a new window attributes builder with the given window title.
    pub fn new(title: impl Into<String>) -> Self {
        Self {
            reactive: ReactiveWindowAttrs {
                title: title.into(),
                resizable: true,
                enabled_buttons: WindowButtons::all(),
                cursor: Cursor::default(),
                min_inner_size: None,
                max_inner_size: None,
                window_level: WindowLevel::default(),
                decorations: true,
                platform_specific: PlatformSpecificReactiveWindowAttrs::default(),
            },
            initial: InitialAttrs {
                inner_size: None,
                position: None,
                transparent: false,
                window_icon: None,
                platform_specific: PlatformSpecificInitialWindowAttrs::default(),
            },
            callbacks: WindowCallbacks::default(),
        }
    }

    /// Sets a callback to execute when the user has requested to close the window.
    pub fn on_close(mut self, callback: impl Fn(&mut State) + 'static) -> Self {
        self.callbacks.on_close = Some(Box::new(callback));
        self
    }

    /// Sets whether the window is resizable or not.
    ///
    /// The default is `true`.
    pub fn with_resizable(mut self, resizable: bool) -> Self {
        self.reactive.resizable = resizable;
        self
    }

    /// Sets the enabled window buttons.
    ///
    /// The default is [`WindowButtons::all`]
    pub fn with_enabled_buttons(mut self, enabled_buttons: WindowButtons) -> Self {
        self.reactive.enabled_buttons = enabled_buttons;
        self
    }

    /// Sets the cursor icon of the window.
    pub fn with_cursor(mut self, cursor: impl Into<Cursor>) -> Self {
        self.reactive.cursor = cursor.into();
        self
    }

    /// Sets the minimum dimensions the window can have.
    pub fn with_min_inner_size<S: Into<Size>>(mut self, min_size: S) -> Self {
        self.reactive.min_inner_size = Some(min_size.into());
        self
    }

    /// Sets the maximum dimensions the window can have.
    pub fn with_max_inner_size<S: Into<Size>>(mut self, max_size: S) -> Self {
        self.reactive.max_inner_size = Some(max_size.into());
        self
    }

    /// Sets the window level.
    ///
    /// This is just a hint to the OS, and the system could ignore it.
    ///
    /// The default is [`WindowLevel::Normal`].
    pub fn with_window_level(mut self, window_level: WindowLevel) -> Self {
        self.reactive.window_level = window_level;
        self
    }

    /// Sets whether the window should have a border, a title bar, etc.
    ///
    /// The default is `true`.
    pub fn with_decorations(mut self, decorations: bool) -> Self {
        self.reactive.decorations = decorations;
        self
    }

    /// Requests the window to be of specific dimensions.
    pub fn with_initial_inner_size<S: Into<Size>>(mut self, size: S) -> Self {
        self.initial.inner_size = Some(size.into());
        self
    }

    /// Sets a desired initial position for the window.
    pub fn with_initial_position<P: Into<Position>>(mut self, position: P) -> Self {
        self.initial.position = Some(position.into());
        self
    }

    /// Sets the window icon.
    ///
    /// The default is `None`.
    pub fn with_initial_window_icon(mut self, window_icon: Option<Icon>) -> Self {
        self.initial.window_icon = window_icon;
        self
    }

    /// Sets whether the background of the window should be transparent.
    ///
    /// If this is `true`, writing colors with alpha values different than
    /// `1.0` will produce a transparent window. On some platforms this
    /// is more of a hint for the system and you'd still have the alpha
    /// buffer.
    ///
    /// The default is `false`.
    pub fn with_transparent(mut self, transparent: bool) -> Self {
        self.initial.transparent = transparent;
        self
    }

    pub(crate) fn build_initial_attrs(&self) -> WindowAttributes {
        let mut attrs = WindowAttributes::default()
            .with_title(self.reactive.title.clone())
            .with_resizable(self.reactive.resizable)
            .with_enabled_buttons(self.reactive.enabled_buttons)
            .with_cursor(self.reactive.cursor.clone())
            .with_window_level(self.reactive.window_level)
            .with_decorations(self.reactive.decorations)
            .with_transparent(self.initial.transparent)
            .with_window_icon(self.initial.window_icon.clone());

        if let Some(min_inner_size) = self.reactive.min_inner_size {
            attrs = attrs.with_min_inner_size(min_inner_size);
        }
        if let Some(max_inner_size) = self.reactive.max_inner_size {
            attrs = attrs.with_max_inner_size(max_inner_size);
        }
        if let Some(inner_size) = self.initial.inner_size {
            attrs = attrs.with_inner_size(inner_size);
        }
        if let Some(position) = self.initial.position {
            attrs = attrs.with_position(position);
        }
        self.initial
            .platform_specific
            .build(self.reactive.platform_specific.build(attrs))
    }

    pub(crate) fn rebuild(&self, prev: &Self, window: &Window) {
        self.rebuild_reactive_window_attributes(prev, window);
        self.warn_for_changed_initial_attributes(prev);
    }

    fn rebuild_reactive_window_attributes(&self, prev: &Self, window: &Window) {
        let current = &self.reactive;
        let prev = &prev.reactive;

        if current.title != prev.title {
            window.set_title(&current.title);
        }
        if current.resizable != prev.resizable {
            window.set_resizable(current.resizable);
        }
        if current.enabled_buttons != prev.enabled_buttons {
            window.set_enabled_buttons(current.enabled_buttons);
        }
        if current.cursor != prev.cursor {
            window.set_cursor(current.cursor.clone());
        }
        if current.min_inner_size != prev.min_inner_size {
            window.set_min_inner_size(current.min_inner_size);
        }
        if current.max_inner_size != prev.max_inner_size {
            window.set_max_inner_size(current.max_inner_size);
        }
        if current.window_level != prev.window_level {
            window.set_window_level(current.window_level);
        }
        if current.decorations != prev.decorations {
            window.set_decorations(current.decorations);
        }

        current
            .platform_specific
            .rebuild(&prev.platform_specific, window);
    }

    fn warn_for_changed_initial_attributes(&self, prev: &Self) {
        let current = &self.initial;
        let prev = &prev.initial;

        if current.inner_size != prev.inner_size {
            tracing::warn!(
                "attempted to change inner_size attribute after window creation, this is not supported"
            );
        }
        if current.position != prev.position {
            tracing::warn!(
                "attempted to change position attribute after window creation, this is not supported"
            );
        }
        if current.transparent != prev.transparent {
            tracing::warn!(
                "attempted to change transparent attribute after window creation, this is not supported"
            );
        }
        // winit::icon::Icon doesn't implement PartialEq, once it does it will be made reactive
        if current.window_icon.is_some() != prev.window_icon.is_some() {
            tracing::warn!(
                "attempted to change window_icon attribute after window creation, this is not supported"
            );
        }

        current.platform_specific.warn(&prev.platform_specific);
    }
}

#[cfg(windows)]
mod windows {
    use winit::platform::windows::{
        BackdropType, Color, CornerPreference, HMENU, HWND, WindowAttributesExtWindows,
        WindowExtWindows,
    };
    use winit::window::{Icon, Window, WindowAttributes};

    #[derive(Debug, Clone)]
    pub(crate) struct PlatformSpecificInitialWindowAttrs {
        owner: Option<HWND>,
        menu: Option<HMENU>,
        no_redirection_bitmap: bool,
        taskbar_icon: Option<Icon>,
        drag_and_drop: bool,
        class_name: String,
        clip_children: bool,
    }

    #[derive(Debug, Clone, Default)]
    pub(crate) struct PlatformSpecificReactiveWindowAttrs {
        skip_taskbar: bool,
        decoration_shadow: bool,
        backdrop_type: BackdropType,
        border_color: Option<Color>,
        title_background_color: Option<Color>,
        title_text_color: Option<Color>,
        corner_preference: Option<CornerPreference>,
    }

    impl PlatformSpecificInitialWindowAttrs {
        pub(crate) fn build(&self, attrs: WindowAttributes) -> WindowAttributes {
            let mut attrs = attrs
                .with_taskbar_icon(self.taskbar_icon.clone())
                .with_no_redirection_bitmap(self.no_redirection_bitmap)
                .with_drag_and_drop(self.drag_and_drop)
                .with_class_name(self.class_name.clone())
                .with_clip_children(self.clip_children);
            if let Some(owner) = self.owner {
                attrs = attrs.with_owner_window(owner);
            }
            if let Some(menu) = self.menu {
                attrs = attrs.with_menu(menu);
            }
            attrs
        }

        pub(crate) fn warn(&self, prev: &Self) {
            if self.owner != prev.owner {
                tracing::warn!(
                    "attempted to change owner attribute after window creation, this is not supported"
                );
            }
            if self.menu != prev.menu {
                tracing::warn!(
                    "attempted to change menu attribute after window creation, this is not supported"
                );
            }
            if self.no_redirection_bitmap != prev.no_redirection_bitmap {
                tracing::warn!(
                    "attempted to change no_redirection_bitmap attribute after window creation, this is not supported"
                );
            }
            if self.taskbar_icon.is_some() != prev.taskbar_icon.is_some() {
                tracing::warn!(
                    "attempted to change taskbar_icon attribute after window creation, this is not supported"
                );
            }
            if self.drag_and_drop != prev.drag_and_drop {
                tracing::warn!(
                    "attempted to change drag_and_drop attribute after window creation, this is not supported"
                );
            }
            if self.class_name != prev.class_name {
                tracing::warn!(
                    "attempted to change class_name attribute after window creation, this is not supported"
                );
            }
            if self.clip_children != prev.clip_children {
                tracing::warn!(
                    "attempted to change clip_children attribute after window creation, this is not supported"
                );
            }
        }
    }

    impl PlatformSpecificReactiveWindowAttrs {
        pub(crate) fn build(&self, attrs: WindowAttributes) -> WindowAttributes {
            let mut attrs = attrs
                .with_skip_taskbar(self.skip_taskbar)
                .with_undecorated_shadow(self.decoration_shadow)
                .with_system_backdrop(self.backdrop_type)
                .with_border_color(self.border_color)
                .with_title_background_color(self.title_background_color);
            if let Some(title_text_color) = self.title_text_color {
                attrs = attrs.with_title_text_color(title_text_color);
            }
            if let Some(corner_preference) = self.corner_preference {
                attrs = attrs.with_corner_preference(corner_preference);
            }
            attrs
        }

        pub(crate) fn rebuild(&self, prev: &Self, window: &Window) {
            // TODO: move taskbar_icon to ReactiveWindowAttrs once the winit type implements PartialEq
            // if self.taskbar_icon != prev.taskbar_icon {
            //     window.set_taskbar_icon(self.taskbar_icon)
            // }
            if self.skip_taskbar != prev.skip_taskbar {
                window.set_skip_taskbar(self.skip_taskbar);
            }
            if self.decoration_shadow != prev.decoration_shadow {
                window.set_undecorated_shadow(self.decoration_shadow);
            }
            if self.backdrop_type != prev.backdrop_type {
                window.set_system_backdrop(self.backdrop_type);
            }
            if self.border_color != prev.border_color {
                window.set_border_color(self.border_color);
            }
            if self.title_background_color != prev.title_background_color {
                window.set_title_background_color(self.title_background_color);
            }
            if self.title_text_color != prev.title_text_color
                && let Some(c) = self.title_text_color
            {
                window.set_title_text_color(c);
            }
            if self.corner_preference != prev.corner_preference
                && let Some(c) = self.corner_preference
            {
                window.set_corner_preference(c);
            }
        }
    }

    impl Default for PlatformSpecificInitialWindowAttrs {
        fn default() -> Self {
            Self {
                owner: None,
                menu: None,
                taskbar_icon: None,
                no_redirection_bitmap: false,
                drag_and_drop: true,
                class_name: "Window Class".to_string(),
                clip_children: true,
            }
        }
    }

    /// Extension setters for Windows-specific window options.
    pub trait WindowOptionsExtWindows {
        /// Set an owner to the window to be created. Can be used to create a dialog box, for example.
        // / This only works when [`WindowAttributes::with_parent_window`] isn't called or set to `None`.
        // / Can be used in combination with
        // / [`WindowExtWindows::set_enable(false)`][WindowExtWindows::set_enable] on the owner
        // / window to create a modal dialog box.
        ///
        /// From MSDN:
        /// - An owned window is always above its owner in the z-order.
        /// - The system automatically destroys an owned window when its owner is destroyed.
        /// - An owned window is hidden when its owner is minimized.
        ///
        /// For more information, see <https://docs.microsoft.com/en-us/windows/win32/winmsg/window-features#owned-windows>
        fn with_owner_window(self, parent: HWND) -> Self;

        /// Sets a menu on the window to be created.
        ///
        /// Parent and menu are mutually exclusive; a child window cannot have a menu!
        ///
        /// The menu must have been manually created beforehand with `CreateMenu` or similar.
        ///
        /// Note: Dark mode cannot be supported for win32 menus, it's simply not possible to change how
        /// the menus look.
        // / If you use this, it is recommended that you combine it with
        // / `with_theme(Some(Theme::Light))` to avoid a jarring effect.
        fn with_menu(self, menu: HMENU) -> Self;

        /// This sets `ICON_BIG`. A good ceiling here is 256x256.
        fn with_taskbar_icon(self, taskbar_icon: Option<Icon>) -> Self;

        /// This sets `WS_EX_NOREDIRECTIONBITMAP`.
        fn with_no_redirection_bitmap(self, flag: bool) -> Self;

        /// Enables or disables drag and drop support (enabled by default). Will interfere with other
        /// crates that use multi-threaded COM API (`CoInitializeEx` with `COINIT_MULTITHREADED`
        /// instead of `COINIT_APARTMENTTHREADED`) on the same thread. Note that winit may still
        /// attempt to initialize COM API regardless of this option. Currently only fullscreen mode
        /// does that, but there may be more in the future. If you need COM API with
        /// `COINIT_MULTITHREADED` you must initialize it before calling any winit functions. See <https://docs.microsoft.com/en-us/windows/win32/api/objbase/nf-objbase-coinitialize#remarks> for more information.
        fn with_drag_and_drop(self, flag: bool) -> Self;

        /// Whether show or hide the window icon in the taskbar.
        fn with_skip_taskbar(self, skip: bool) -> Self;

        /// Customize the window class name.
        fn with_class_name<S: Into<String>>(self, class_name: S) -> Self;

        /// Shows or hides the background drop shadow for undecorated windows.
        ///
        /// The shadow is hidden by default.
        /// Enabling the shadow causes a thin 1px line to appear on the top of the window.
        fn with_undecorated_shadow(self, shadow: bool) -> Self;

        /// Sets system-drawn backdrop type.
        ///
        /// Requires Windows 11 build 22523+.
        fn with_system_backdrop(self, backdrop_type: BackdropType) -> Self;

        /// This sets or removes `WS_CLIPCHILDREN` style.
        fn with_clip_children(self, flag: bool) -> Self;

        /// Sets the color of the window border.
        ///
        /// Supported starting with Windows 11 Build 22000.
        fn with_border_color(self, color: Option<Color>) -> Self;

        /// Sets the background color of the title bar.
        ///
        /// Supported starting with Windows 11 Build 22000.
        fn with_title_background_color(self, color: Option<Color>) -> Self;

        /// Sets the color of the window title.
        ///
        /// Supported starting with Windows 11 Build 22000.
        fn with_title_text_color(self, color: Color) -> Self;

        /// Sets the preferred style of the window corners.
        ///
        /// Supported starting with Windows 11 Build 22000.
        fn with_corner_preference(self, corners: CornerPreference) -> Self;
    }

    impl<S> WindowOptionsExtWindows for super::WindowOptions<S> {
        #[inline]
        fn with_owner_window(mut self, parent: HWND) -> Self {
            self.initial.platform_specific.owner = Some(parent);
            self
        }

        #[inline]
        fn with_menu(mut self, menu: HMENU) -> Self {
            self.initial.platform_specific.menu = Some(menu);
            self
        }

        #[inline]
        fn with_taskbar_icon(mut self, taskbar_icon: Option<Icon>) -> Self {
            self.initial.platform_specific.taskbar_icon = taskbar_icon;
            self
        }

        #[inline]
        fn with_no_redirection_bitmap(mut self, flag: bool) -> Self {
            self.initial.platform_specific.no_redirection_bitmap = flag;
            self
        }

        #[inline]
        fn with_drag_and_drop(mut self, flag: bool) -> Self {
            self.initial.platform_specific.drag_and_drop = flag;
            self
        }

        #[inline]
        fn with_skip_taskbar(mut self, skip: bool) -> Self {
            self.reactive.platform_specific.skip_taskbar = skip;
            self
        }

        #[inline]
        fn with_class_name<C: Into<String>>(mut self, class_name: C) -> Self {
            self.initial.platform_specific.class_name = class_name.into();
            self
        }

        #[inline]
        fn with_undecorated_shadow(mut self, shadow: bool) -> Self {
            self.reactive.platform_specific.decoration_shadow = shadow;
            self
        }

        #[inline]
        fn with_system_backdrop(mut self, backdrop_type: BackdropType) -> Self {
            self.reactive.platform_specific.backdrop_type = backdrop_type;
            self
        }

        #[inline]
        fn with_clip_children(mut self, flag: bool) -> Self {
            self.initial.platform_specific.clip_children = flag;
            self
        }

        #[inline]
        fn with_border_color(mut self, color: Option<Color>) -> Self {
            self.reactive.platform_specific.border_color = color;
            self
        }

        #[inline]
        fn with_title_background_color(mut self, color: Option<Color>) -> Self {
            self.reactive.platform_specific.title_background_color = color;
            self
        }

        #[inline]
        fn with_title_text_color(mut self, color: Color) -> Self {
            self.reactive.platform_specific.title_text_color = Some(color);
            self
        }

        #[inline]
        fn with_corner_preference(mut self, corners: CornerPreference) -> Self {
            self.reactive.platform_specific.corner_preference = Some(corners);
            self
        }
    }
}

#[cfg(windows)]
pub use windows::*;

#[cfg(not(windows))]
mod dummy_platform {
    use winit::window::{Window, WindowAttributes};

    #[derive(Debug, Clone, Default)]
    pub(crate) struct PlatformSpecificInitialWindowAttrs {}

    #[derive(Debug, Clone, Default)]
    pub(crate) struct PlatformSpecificReactiveWindowAttrs {}

    impl PlatformSpecificInitialWindowAttrs {
        pub(crate) fn build(&self, attrs: WindowAttributes) -> WindowAttributes {
            attrs
        }

        pub(crate) fn warn(&self, _prev: &Self) {}
    }

    impl PlatformSpecificReactiveWindowAttrs {
        pub(crate) fn build(&self, attrs: WindowAttributes) -> WindowAttributes {
            attrs
        }

        pub(crate) fn rebuild(&self, _prev: &Self, _window: &Window) {}
    }
}

#[cfg(not(windows))]
pub(crate) use dummy_platform::*;