ergonomic-windows 0.1.0

Ergonomic, safe Rust wrappers for Windows APIs - handles, processes, registry, file system, UI controls, Direct2D graphics, and more
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
//! WebView2 - Chromium-based web view.
//!
//! Provides safe wrappers for embedding a Chromium-based web browser
//! in Windows applications using Microsoft Edge WebView2.
//!
//! This module requires the `webview2` feature to be enabled.
//!
//! # Prerequisites
//!
//! WebView2 requires the Microsoft Edge WebView2 Runtime to be installed.
//! It's pre-installed on Windows 10 (version 1803+) and Windows 11.
//!
//! # Example
//!
//! ```ignore
//! use ergonomic_windows::webview::{WebView, WebViewBuilder};
//!
//! let webview = WebViewBuilder::new()
//!     .with_url("https://www.rust-lang.org")
//!     .build(parent_hwnd)?;
//!
//! // Navigate to a URL
//! webview.navigate("https://docs.rs")?;
//!
//! // Execute JavaScript
//! webview.execute_script("document.body.style.background = 'red';")?;
//! ```

#[cfg(feature = "webview2")]
mod inner {
    use crate::error::{Error, Result};
    use crate::string::WideString;
    use std::sync::mpsc;
    use webview2_com::Microsoft::Web::WebView2::Win32::{
        CreateCoreWebView2EnvironmentWithOptions, ICoreWebView2, ICoreWebView2Controller,
    };
    use webview2_com::{
        CreateCoreWebView2ControllerCompletedHandler,
        CreateCoreWebView2EnvironmentCompletedHandler, ExecuteScriptCompletedHandler,
    };
    use windows::core::{PCWSTR, PWSTR};
    use windows::Win32::Foundation::{HWND, RECT};
    use windows::Win32::UI::WindowsAndMessaging::GetClientRect;

    /// A builder for creating WebView2 instances.
    pub struct WebViewBuilder {
        url: Option<String>,
        user_data_folder: Option<String>,
        enable_dev_tools: bool,
        enable_context_menu: bool,
        enable_zoom: bool,
    }

    impl WebViewBuilder {
        /// Creates a new WebView builder.
        pub fn new() -> Self {
            Self {
                url: None,
                user_data_folder: None,
                enable_dev_tools: false,
                enable_context_menu: true,
                enable_zoom: true,
            }
        }

        /// Sets the initial URL to navigate to.
        pub fn with_url(mut self, url: &str) -> Self {
            self.url = Some(url.to_string());
            self
        }

        /// Sets the user data folder for the browser profile.
        pub fn with_user_data_folder(mut self, path: &str) -> Self {
            self.user_data_folder = Some(path.to_string());
            self
        }

        /// Enables developer tools (F12).
        pub fn with_dev_tools(mut self, enable: bool) -> Self {
            self.enable_dev_tools = enable;
            self
        }

        /// Enables the context menu (right-click).
        pub fn with_context_menu(mut self, enable: bool) -> Self {
            self.enable_context_menu = enable;
            self
        }

        /// Enables zoom controls.
        pub fn with_zoom(mut self, enable: bool) -> Self {
            self.enable_zoom = enable;
            self
        }

        /// Builds the WebView2 instance.
        ///
        /// This is an asynchronous operation. The WebView will be created
        /// and attached to the parent window.
        pub fn build(self, parent: HWND) -> Result<WebView> {
            // Get parent window bounds
            let mut rect = RECT::default();
            unsafe {
                GetClientRect(parent, &mut rect).map_err(Error::from_win32)?;
            }

            // Create environment and controller synchronously using a channel
            let (tx, rx) = mpsc::channel();

            let url = self.url.clone();
            let enable_dev_tools = self.enable_dev_tools;
            let enable_context_menu = self.enable_context_menu;
            let enable_zoom = self.enable_zoom;

            // Prepare user data folder path
            let user_data_wide = self.user_data_folder.as_ref().map(|s| WideString::new(s));
            let user_data_pcwstr: PCWSTR = user_data_wide
                .as_ref()
                .map(|w| w.as_pcwstr())
                .unwrap_or(PCWSTR::null());

            // Create the environment
            let create_result = unsafe {
                CreateCoreWebView2EnvironmentWithOptions(
                    PCWSTR::null(),
                    user_data_pcwstr,
                    None,
                    &CreateCoreWebView2EnvironmentCompletedHandler::create(Box::new(
                        move |_err, env| {
                            if let Some(env) = env {
                                // Create controller
                                let _ = env.CreateCoreWebView2Controller(
                                    parent,
                                    &CreateCoreWebView2ControllerCompletedHandler::create(
                                        Box::new(move |_err, controller| {
                                            let _ = tx.send(controller);
                                            Ok(())
                                        }),
                                    ),
                                );
                            } else {
                                let _ = tx.send(None);
                            }
                            Ok(())
                        },
                    )),
                )
            };

            if create_result.is_err() {
                return Err(Error::custom(
                    "Failed to create WebView2 environment. Is WebView2 Runtime installed?",
                ));
            }

            // Wait for the controller to be created
            // Note: In a real application, you'd integrate this with your message loop
            let controller = rx
                .recv()
                .map_err(|_| Error::custom("WebView2 controller creation failed"))?
                .ok_or_else(|| Error::custom("WebView2 controller was not created"))?;

            // Get the webview
            let webview = unsafe { controller.CoreWebView2() }
                .map_err(|_| Error::custom("Failed to get CoreWebView2"))?;

            // Configure settings
            if let Ok(settings) = unsafe { webview.Settings() } {
                unsafe {
                    let _ = settings.SetAreDevToolsEnabled(enable_dev_tools);
                    let _ = settings.SetAreDefaultContextMenusEnabled(enable_context_menu);
                    let _ = settings.SetIsZoomControlEnabled(enable_zoom);
                }
            }

            // Set bounds
            unsafe {
                let _ = controller.SetBounds(rect);
            }

            // Navigate to initial URL
            if let Some(url) = url {
                let url_wide = WideString::new(&url);
                unsafe {
                    let _ = webview.Navigate(url_wide.as_pcwstr());
                }
            }

            Ok(WebView {
                controller,
                webview,
                parent,
            })
        }
    }

    impl Default for WebViewBuilder {
        fn default() -> Self {
            Self::new()
        }
    }

    /// A WebView2 browser control.
    pub struct WebView {
        controller: ICoreWebView2Controller,
        webview: ICoreWebView2,
        parent: HWND,
    }

    impl WebView {
        /// Navigates to a URL.
        pub fn navigate(&self, url: &str) -> Result<()> {
            let url_wide = WideString::new(url);
            unsafe {
                self.webview
                    .Navigate(url_wide.as_pcwstr())
                    .map_err(|_| Error::custom("Navigation failed"))?;
            }
            Ok(())
        }

        /// Navigates to a string of HTML content.
        pub fn navigate_to_string(&self, html: &str) -> Result<()> {
            let html_wide = WideString::new(html);
            unsafe {
                self.webview
                    .NavigateToString(html_wide.as_pcwstr())
                    .map_err(|_| Error::custom("NavigateToString failed"))?;
            }
            Ok(())
        }

        /// Executes JavaScript in the context of the current page.
        pub fn execute_script(&self, script: &str) -> Result<()> {
            let script_wide = WideString::new(script);
            unsafe {
                self.webview
                    .ExecuteScript(
                        script_wide.as_pcwstr(),
                        &ExecuteScriptCompletedHandler::create(Box::new(|_err, _result| Ok(()))),
                    )
                    .map_err(|_| Error::custom("ExecuteScript failed"))?;
            }
            Ok(())
        }

        /// Refreshes the current page.
        pub fn reload(&self) -> Result<()> {
            unsafe {
                self.webview
                    .Reload()
                    .map_err(|_| Error::custom("Reload failed"))?;
            }
            Ok(())
        }

        /// Goes back in history.
        pub fn go_back(&self) -> Result<()> {
            unsafe {
                self.webview
                    .GoBack()
                    .map_err(|_| Error::custom("GoBack failed"))?;
            }
            Ok(())
        }

        /// Goes forward in history.
        pub fn go_forward(&self) -> Result<()> {
            unsafe {
                self.webview
                    .GoForward()
                    .map_err(|_| Error::custom("GoForward failed"))?;
            }
            Ok(())
        }

        /// Stops loading the current page.
        pub fn stop(&self) -> Result<()> {
            unsafe {
                self.webview
                    .Stop()
                    .map_err(|_| Error::custom("Stop failed"))?;
            }
            Ok(())
        }

        /// Resizes the WebView to match the parent window.
        pub fn resize_to_parent(&self) -> Result<()> {
            let mut rect = RECT::default();
            unsafe {
                GetClientRect(self.parent, &mut rect).map_err(Error::from_win32)?;
                self.controller
                    .SetBounds(rect)
                    .map_err(|_| Error::custom("SetBounds failed"))?;
            }
            Ok(())
        }

        /// Sets the bounds of the WebView.
        pub fn set_bounds(&self, left: i32, top: i32, right: i32, bottom: i32) -> Result<()> {
            let rect = RECT {
                left,
                top,
                right,
                bottom,
            };
            unsafe {
                self.controller
                    .SetBounds(rect)
                    .map_err(|_| Error::custom("SetBounds failed"))?;
            }
            Ok(())
        }

        /// Shows the WebView.
        pub fn show(&self) {
            unsafe {
                let _ = self.controller.SetIsVisible(true);
            }
        }

        /// Hides the WebView.
        pub fn hide(&self) {
            unsafe {
                let _ = self.controller.SetIsVisible(false);
            }
        }

        /// Gets the current URL.
        pub fn url(&self) -> Result<String> {
            unsafe {
                let mut source: PWSTR = PWSTR::null();
                self.webview
                    .Source(&mut source)
                    .map_err(|_| Error::custom("Failed to get Source"))?;
                if source.is_null() {
                    return Ok(String::new());
                }
                Ok(source.to_string().unwrap_or_default())
            }
        }

        /// Posts a web message (JSON) to the page.
        pub fn post_web_message_as_json(&self, json: &str) -> Result<()> {
            let json_wide = WideString::new(json);
            unsafe {
                self.webview
                    .PostWebMessageAsJson(json_wide.as_pcwstr())
                    .map_err(|_| Error::custom("PostWebMessageAsJson failed"))?;
            }
            Ok(())
        }

        /// Posts a web message (string) to the page.
        pub fn post_web_message_as_string(&self, message: &str) -> Result<()> {
            let message_wide = WideString::new(message);
            unsafe {
                self.webview
                    .PostWebMessageAsString(message_wide.as_pcwstr())
                    .map_err(|_| Error::custom("PostWebMessageAsString failed"))?;
            }
            Ok(())
        }
    }
}

#[cfg(feature = "webview2")]
pub use inner::*;

/// Placeholder types when webview2 feature is not enabled.
#[cfg(not(feature = "webview2"))]
mod placeholder {
    use crate::error::{Error, Result};
    use windows::Win32::Foundation::HWND;

    /// WebView2 is not available without the `webview2` feature.
    #[derive(Default)]
    pub struct WebViewBuilder;

    impl WebViewBuilder {
        /// Creates a new WebView builder.
        ///
        /// **Note**: Enable the `webview2` feature to use WebView2.
        pub fn new() -> Self {
            Self
        }

        /// Builds the WebView2 instance.
        ///
        /// Always returns an error because the `webview2` feature is not enabled.
        pub fn build(self, _parent: HWND) -> Result<WebView> {
            Err(Error::custom(
                "WebView2 support requires the 'webview2' feature to be enabled",
            ))
        }
    }

    /// WebView2 placeholder when feature is disabled.
    pub struct WebView;
}

#[cfg(not(feature = "webview2"))]
pub use placeholder::*;