rsciter 0.0.11

Unofficial Rust bindings for Sciter
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
use std::{collections::HashMap, pin::Pin};

use super::{HostNotifications, Window, WindowDelegate, WindowFlags, WindowHandle, WindowState};
use crate::{
    api::sapi, bindings::*, ArchiveData, DefaultEventHandler, DefaultHost, EventHandler, Result,
    XFunction, XFunctionProvider,
};

// Some rust black magic to disallow
// -  `with_html` and `with_file` simultaneous calls on the same builder object
// -  calling `with_archive`, etc after setting custom host (calling `with_custom_host`)

/// A builder type for creating new windows.
pub struct WindowBuilder<'b, const HOST: u8 = HOST_NONE, const INITIAL_PAGE: u8 = INITIAL_PAGE_NONE>
{
    common: Common,
    initial_page: InitialPage<'b>,
    host: Host,
}

impl Default for WindowBuilder<'_, HOST_NONE, INITIAL_PAGE_NONE> {
    fn default() -> Self {
        Self::new()
    }
}

impl WindowBuilder<'_, HOST_NONE, INITIAL_PAGE_NONE> {
    /// Creates a new `WindowBuilder`
    pub fn new() -> Self {
        let flags = WindowFlags::HAS_TITLEBAR | WindowFlags::HAS_CONTROLS | WindowFlags::RESIZEABLE;

        Self {
            common: Common {
                flags,
                frame: None,
                parent: None,
                window_delegate: None,
                event_handler: None,
            },
            initial_page: InitialPage::None,
            host: Host::None,
        }
    }
}

// common case
impl<'b, const ANY_HOST: u8, const ANY_INITIAL_PAGE: u8>
    WindowBuilder<'b, ANY_HOST, ANY_INITIAL_PAGE>
{
    /// Sets the window's initial [`WindowFlags`].
    ///
    /// Be careful, this call overwrites all default flags set in [`WindowBuilder::new`]
    pub fn with_flags(mut self, flags: WindowFlags) -> Self {
        self.common.flags = flags;
        self
    }

    pub fn with_flags_or(mut self, flags: WindowFlags) -> Self {
        self.common.flags |= flags;
        self
    }

    /// Set the window's initial size and position.
    pub fn with_frame(mut self, frame: RECT) -> Self {
        self.common.frame = Some(frame);
        self
    }

    pub fn with_window_delegate(mut self, delegate: impl WindowDelegate) -> Self {
        self.common.window_delegate = Some(Box::new(delegate));
        self
    }

    pub fn with_event_handler(mut self, handler: impl for<'s> EventHandler<'s>) -> Self {
        self.common.event_handler = Some(Box::new(handler));
        self
    }

    /// Attempt to construct the main Sciter window.
    /// Explicitly sets `WindowFlags::MAIN` flag
    pub fn build_main(mut self) -> Result<Window> {
        self.common.flags |= WindowFlags::MAIN;
        self.build()
    }

    /// Attempt to construct a secondary Sciter window.
    /// Explicitly removes `WindowFlags::MAIN` flag
    pub fn build_secondary(mut self) -> Result<Window> {
        self.common.flags.remove(WindowFlags::MAIN);
        self.build()
    }

    /// Attempt to construct a Sciter window using existing [WindowFlags]
    fn build(self) -> Result<Window> {
        let api = sapi()?;

        let has_window_delegate = self.common.window_delegate.is_some();
        let host_info = self.host.get()?;
        let event_handler = match (host_info.event_handler, self.common.event_handler) {
            (None, None) => None,
            (None, Some(user)) => Some(user),
            (Some(default), None) => {
                let handler: Box<dyn for<'s> EventHandler<'s>> = Box::new(default);
                Some(handler)
            }
            (Some(mut default), Some(user)) => {
                default.set_custom_event_handler(user);
                let handler: Box<dyn for<'s> EventHandler<'s>> = Box::new(default);
                Some(handler)
            }
        };

        let state = WindowState {
            delegate: self.common.window_delegate,
            host: host_info.host,
            event_handler,
        };
        let mut pinned = Box::pin(state);
        let state_ptr = unsafe { Pin::get_unchecked_mut(pinned.as_mut()) as *mut WindowState };

        let flags = self.common.flags.0 as UINT;
        let hwnd = if has_window_delegate {
            api.create_window(
                flags,
                self.common.frame,
                self.common.parent,
                Some(super::window_delegate_thunk),
                state_ptr as _,
            )?
        } else {
            api.create_window(flags, self.common.frame, self.common.parent, None, 0 as _)?
        };

        let window = Window {
            handle: WindowHandle::from(hwnd),
            state: pinned,
        };

        if window.state.host.is_some() {
            api.set_callback(
                window.handle().into(),
                Some(super::host_thunk),
                state_ptr as _,
            )?;
        }

        if window.state.event_handler.is_some() {
            api.window_attach_event_handler(
                window.handle.into(),
                Some(crate::element_proc_thunk),
                state_ptr as _,
                EVENT_GROUPS::HANDLE_ALL,
            )?;
        }

        match self.initial_page {
            InitialPage::None => Ok(window),
            InitialPage::Html { html, base_url } => {
                let _res = window.load_html(html, base_url)?;
                // TODO: error?
                Ok(window)
            }
            InitialPage::File(file) => {
                let _res = window.load_file(file)?;
                // TODO: error?
                Ok(window)
            }
        }
    }
}

// initial page not set
impl<'b, const ANY_HOST: u8> WindowBuilder<'b, ANY_HOST, INITIAL_PAGE_NONE> {
    pub fn with_html(self, html: &'b [u8]) -> WindowBuilder<'b, ANY_HOST, INITIAL_PAGE_HTML> {
        WindowBuilder {
            common: self.common,
            initial_page: InitialPage::Html {
                html,
                base_url: None,
            },
            host: self.host,
        }
    }

    pub fn with_file(self, file: &'b str) -> WindowBuilder<'b, ANY_HOST, INITIAL_PAGE_FILE> {
        WindowBuilder {
            common: self.common,
            initial_page: InitialPage::File(file),
            host: self.host,
        }
    }
}

// initial page is HTML, we may set base url
impl<'b, const ANY_HOST: u8> WindowBuilder<'b, ANY_HOST, INITIAL_PAGE_HTML> {
    pub fn with_base_url(mut self, url: &'b str) -> Self {
        match &mut self.initial_page {
            InitialPage::Html { base_url, .. } => *base_url = Some(url),
            _ => unreachable!(),
        }
        self
    }
}

// host is unset
impl<'b, const ANY_INITIAL_PAGE: u8> WindowBuilder<'b, HOST_NONE, ANY_INITIAL_PAGE> {
    // inherent associated types are unstable
    // https://github.com/rust-lang/rust/issues/8995
    //
    // type WithDefaultHost = Test<'b, HOST_TYPE_DEFAULT, INITIAL_TYPE>;

    pub fn with_archive_static(
        self,
        data: &'static [u8],
    ) -> WindowBuilder<'b, HOST_DEFAULT, ANY_INITIAL_PAGE> {
        self.with_default_host().with_archive_static(data)
    }

    pub fn with_archive(self, data: Vec<u8>) -> WindowBuilder<'b, HOST_DEFAULT, ANY_INITIAL_PAGE> {
        self.with_default_host().with_archive(data)
    }

    pub fn with_host(
        self,
        host: impl HostNotifications,
    ) -> WindowBuilder<'b, HOST_CUSTOM, ANY_INITIAL_PAGE> {
        WindowBuilder {
            common: self.common,
            initial_page: self.initial_page,
            host: Host::Custom(Box::new(host)),
        }
    }

    pub fn with_default_host(self) -> WindowBuilder<'b, HOST_DEFAULT, ANY_INITIAL_PAGE> {
        WindowBuilder {
            common: self.common,
            initial_page: self.initial_page,
            host: Host::Default {
                archive_data: None,
                archive_uri: None,
                functions: Default::default(),
                modules: Default::default(),
            },
        }
    }

    pub fn with_xfunction(
        self,
        name: impl AsRef<str>,
        func: impl XFunction,
    ) -> WindowBuilder<'b, HOST_DEFAULT, ANY_INITIAL_PAGE> {
        self.with_default_host().with_xfunction(name, func)
    }

    pub fn with_xmodule(
        self,
        provider: impl XFunctionProvider,
    ) -> WindowBuilder<'b, HOST_DEFAULT, ANY_INITIAL_PAGE> {
        self.with_default_host().with_xmodule(provider)
    }
}

impl<'b, const ANY_INITIAL_PAGE: u8> WindowBuilder<'b, HOST_DEFAULT, ANY_INITIAL_PAGE> {
    pub fn with_archive_static(mut self, data: &'static [u8]) -> Self {
        match &mut self.host {
            Host::Default { archive_data, .. } => *archive_data = Some(ArchiveData::Static(data)),
            _ => unreachable!(),
        }
        self
    }

    pub fn with_archive(mut self, data: Vec<u8>) -> Self {
        match &mut self.host {
            Host::Default { archive_data, .. } => *archive_data = Some(ArchiveData::Heap(data)),
            _ => unreachable!(),
        }
        self
    }

    pub fn with_archive_uri(mut self, uri: String) -> Self {
        match &mut self.host {
            Host::Default { archive_uri, .. } => *archive_uri = Some(uri),
            _ => unreachable!(),
        }
        self
    }

    pub fn with_xfunction(mut self, name: impl AsRef<str>, func: impl XFunction) -> Self {
        match &mut self.host {
            Host::Default { functions, .. } => {
                functions.insert(name.as_ref().to_string(), Box::new(func));
            }
            _ => unreachable!(),
        }
        self
    }

    pub fn with_xmodule(mut self, provider: impl XFunctionProvider) -> Self {
        match &mut self.host {
            Host::Default { modules, .. } => {
                modules.push(Box::new(provider));
            }
            _ => unreachable!(),
        }
        self
    }
}

const HOST_NONE: u8 = 0;
const HOST_DEFAULT: u8 = 1;
const HOST_CUSTOM: u8 = 2;

const INITIAL_PAGE_NONE: u8 = 0;
const INITIAL_PAGE_HTML: u8 = 1;
const INITIAL_PAGE_FILE: u8 = 2;

struct Common {
    flags: WindowFlags,
    frame: Option<RECT>,
    parent: Option<HWND>,
    window_delegate: Option<Box<dyn WindowDelegate>>,
    event_handler: Option<Box<dyn for<'s> EventHandler<'s>>>,
}

enum InitialPage<'b> {
    None,
    Html {
        html: &'b [u8],
        base_url: Option<&'b str>,
    },
    File(&'b str),
}

enum Host {
    None,
    Default {
        archive_data: Option<ArchiveData>,
        archive_uri: Option<String>,
        functions: HashMap<String, Box<dyn XFunction>>,
        modules: Vec<Box<dyn XFunctionProvider>>,
    },
    Custom(Box<dyn HostNotifications>),
}

struct HostInfo {
    host: Option<Box<dyn HostNotifications>>,
    event_handler: Option<DefaultEventHandler>,
}

impl HostInfo {
    fn new(
        host: Box<dyn HostNotifications>,
        functions: HashMap<String, Box<dyn XFunction>>,
        modules: Vec<Box<dyn XFunctionProvider>>,
    ) -> Self {
        let event_handler = if !functions.is_empty() || !modules.is_empty() {
            Some(DefaultEventHandler::new(functions, modules))
        } else {
            None
        };

        Self {
            host: Some(host),
            event_handler,
        }
    }

    fn with_host(host: Box<dyn HostNotifications>) -> Self {
        Self {
            host: Some(host),
            event_handler: None,
        }
    }

    pub fn none() -> Self {
        Self {
            host: None,
            event_handler: None,
        }
    }
}

impl Host {
    fn get(self) -> Result<HostInfo> {
        match self {
            Host::None => Ok(HostInfo::none()),
            Host::Default {
                archive_data,
                archive_uri,
                functions,
                modules,
            } => {
                let mut host = archive_uri
                    .map(DefaultHost::with_archive_uri)
                    .unwrap_or_else(DefaultHost::new);

                if let Some(archive_data) = archive_data {
                    host.set_archive(archive_data)?;
                }

                Ok(HostInfo::new(Box::new(host), functions, modules))
            }
            Host::Custom(host) => Ok(HostInfo::with_host(host)),
        }
    }
}