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
use dioxus_core::DomEdit;
use wry::{
    application::{event_loop::EventLoop, window::WindowBuilder},
    webview::WebView,
};

pub struct DesktopConfig<'a> {
    pub window: WindowBuilder,
    pub(crate) manual_edits: Option<Vec<DomEdit<'a>>>,
    pub(crate) pre_rendered: Option<String>,
    pub(crate) event_handler: Option<Box<dyn Fn(&mut EventLoop<()>, &mut WebView)>>,
}

impl<'a> DesktopConfig<'a> {
    /// Initializes a new `WindowBuilder` with default values.
    #[inline]
    pub fn new() -> Self {
        let window = WindowBuilder::new().with_title("Dioxus app");
        Self {
            event_handler: None,
            window,
            pre_rendered: None,
            manual_edits: None,
        }
    }

    pub fn with_edits(&mut self, edits: Vec<DomEdit<'a>>) -> &mut Self {
        self.manual_edits = Some(edits);
        self
    }

    pub fn with_prerendered(&mut self, content: String) -> &mut Self {
        self.pre_rendered = Some(content);
        self
    }

    pub fn with_window(
        &mut self,
        configure: impl FnOnce(WindowBuilder) -> WindowBuilder,
    ) -> &mut Self {
        // gots to do a swap because the window builder only takes itself as muy self
        // I wish more people knew about returning &mut Self
        let mut builder = WindowBuilder::default().with_title("Dioxus App");
        std::mem::swap(&mut self.window, &mut builder);
        builder = configure(builder);
        std::mem::swap(&mut self.window, &mut builder);
        self
    }

    pub fn with_event_handler(
        &mut self,
        handler: impl Fn(&mut EventLoop<()>, &mut WebView) + 'static,
    ) -> &mut Self {
        self.event_handler = Some(Box::new(handler));
        self
    }
}