verso/
builder.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
use dpi::{Position, Size};
use std::path::{Path, PathBuf};
use versoview_messages::{ConfigFromController, ProfilerSettings, UserScript};

use crate::VersoviewController;

/// A builder for configuring and creating a [`VersoviewController`] instance.
#[derive(Debug, Clone)]
pub struct VersoBuilder(ConfigFromController);

impl VersoBuilder {
    /// Creates a new [`VersoBuilder`] with default settings.
    pub fn new() -> Self {
        Self(ConfigFromController::default())
    }

    /// Sets whether the control panel should be included.
    pub fn with_panel(mut self, with_panel: bool) -> Self {
        self.0.with_panel = with_panel;
        self
    }

    /// Sets the initial window size.
    pub fn inner_size(mut self, size: impl Into<Size>) -> Self {
        self.0.inner_size = Some(size.into());
        self
    }

    /// Sets the initial window position.
    pub fn position(mut self, position: impl Into<Position>) -> Self {
        self.0.position = Some(position.into());
        self
    }

    /// Sets whether the window should start maximized.
    pub fn maximized(mut self, maximized: bool) -> Self {
        self.0.maximized = maximized;
        self
    }

    /// Sets whether the window should be visible initially.
    pub fn visible(mut self, visible: bool) -> Self {
        self.0.visible = visible;
        self
    }

    /// Sets whether the window should start in fullscreen mode.
    pub fn fullscreen(mut self, fullscreen: bool) -> Self {
        self.0.fullscreen = fullscreen;
        self
    }

    /// Sets whether the window will be initially focused or not.
    pub fn focused(mut self, focused: bool) -> Self {
        self.0.focused = focused;
        self
    }

    /// Sets whether the window will be initially decorated or not.
    pub fn decorated(mut self, decorated: bool) -> Self {
        self.0.decorated = decorated;
        self
    }

    /// Sets whether the window will be initially transparent or not.
    pub fn transparent(mut self, transparent: bool) -> Self {
        self.0.transparent = transparent;
        self
    }

    /// Sets the initial title of the window in the title bar.
    pub fn title(mut self, title: impl Into<String>) -> Self {
        self.0.title = Some(title.into());
        self
    }

    /// Port number to start a server to listen to remote Firefox devtools connections. 0 for random port.
    pub fn devtools_port(mut self, port: u16) -> Self {
        self.0.devtools_port = Some(port);
        self
    }

    /// Sets the profiler settings.
    pub fn profiler_settings(mut self, settings: ProfilerSettings) -> Self {
        self.0.profiler_settings = Some(settings);
        self
    }

    /// Overrides the user agent.
    pub fn user_agent(mut self, user_agent: impl Into<String>) -> Self {
        self.0.user_agent = Some(user_agent.into());
        self
    }

    /// Adds an user script to run when the document starts loading.
    pub fn user_script(mut self, script: impl Into<UserScript>) -> Self {
        self.0.user_scripts.push(script.into());
        self
    }

    /// Adds multiple user scripts to run when the document starts loading.
    pub fn user_scripts<I, S>(mut self, scripts: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<UserScript>,
    {
        for script in scripts {
            self = self.user_script(script)
        }
        self
    }

    /// Sets the initial zoom level of the webview.
    pub fn zoom_level(mut self, zoom: f32) -> Self {
        self.0.zoom_level = Some(zoom);
        self
    }

    /// Sets the resource directory path.
    pub fn resources_directory(mut self, path: impl Into<PathBuf>) -> Self {
        self.0.resources_directory = Some(path.into());
        self
    }

    /// Builds the [`VersoviewController`] with the configured settings.
    pub fn build(
        self,
        versoview_path: impl AsRef<Path>,
        initial_url: url::Url,
    ) -> VersoviewController {
        VersoviewController::create(versoview_path, initial_url, self.0)
    }
}