|
| 1 | +use std::borrow::{Borrow, BorrowMut}; |
1 | 2 | use std::env;
|
2 | 3 | use std::io::{self, BufRead, Write};
|
3 | 4 | use std::sync::mpsc;
|
4 | 5 |
|
5 | 6 | use schemars::JsonSchema;
|
6 | 7 | use serde::{Deserialize, Serialize};
|
7 | 8 | use serde_json;
|
| 9 | +use tao::window::Fullscreen; |
8 | 10 |
|
9 | 11 | #[derive(JsonSchema, Deserialize, Debug)]
|
10 | 12 | struct WebViewOptions {
|
11 | 13 | title: String,
|
12 | 14 | #[serde(flatten)]
|
13 | 15 | target: WebViewTarget,
|
| 16 | + /// Sets whether the window should be fullscreen. |
| 17 | + #[serde(default)] |
| 18 | + fullscreen: bool, |
| 19 | + /// Sets whether the window should have a border, a title bar, etc. |
| 20 | + #[serde(default = "default_true")] |
| 21 | + decorations: bool, |
| 22 | + #[serde(default)] |
| 23 | + transparent: bool, |
| 24 | + /// Sets whether all media can be played without user interaction. |
| 25 | + #[serde(default)] |
| 26 | + autoplay: bool, |
| 27 | + /// Enable or disable web inspector which is usually called devtools. |
| 28 | + /// |
| 29 | + /// Note this only enables devtools to the webview. To open it, you can call WebView::open_devtools, or right click the page and open it from the context menu. |
| 30 | + #[serde(default)] |
| 31 | + devtools: bool, |
| 32 | + /// Run the WebView with incognito mode. Note that WebContext will be ingored if incognito is enabled. |
| 33 | + /// |
| 34 | + /// Platform-specific: |
| 35 | + /// - Windows: Requires WebView2 Runtime version 101.0.1210.39 or higher, does nothing on older versions, see https://learn.microsoft.com/en-us/microsoft-edge/webview2/release-notes/archive?tabs=dotnetcsharp#10121039 |
| 36 | + #[serde(default)] |
| 37 | + incognito: bool, |
| 38 | + /// Enables clipboard access for the page rendered on Linux and Windows. |
| 39 | + /// |
| 40 | + /// macOS doesn’t provide such method and is always enabled by default. But your app will still need to add menu item accelerators to use the clipboard shortcuts. |
| 41 | + #[serde(default)] |
| 42 | + clipboard: bool, |
| 43 | + /// Sets whether the webview should be focused when created. Default is false. |
| 44 | + #[serde(default)] |
| 45 | + focused: bool, |
| 46 | + /// Sets whether clicking an inactive window also clicks through to the webview. Default is false. |
| 47 | + #[serde(default)] |
| 48 | + accept_first_mouse: bool, |
| 49 | +} |
| 50 | + |
| 51 | +fn default_true() -> bool { |
| 52 | + true |
14 | 53 | }
|
15 | 54 |
|
16 | 55 | #[derive(JsonSchema, Deserialize, Debug)]
|
@@ -57,16 +96,29 @@ fn main() -> wry::Result<()> {
|
57 | 96 | use wry::WebViewBuilder;
|
58 | 97 |
|
59 | 98 | let event_loop = EventLoop::new();
|
60 |
| - let window = WindowBuilder::new() |
| 99 | + let mut window_builder = WindowBuilder::new() |
61 | 100 | .with_title(webview_options.title)
|
62 |
| - .build(&event_loop) |
63 |
| - .unwrap(); |
| 101 | + .with_transparent(webview_options.transparent) |
| 102 | + .with_decorations(webview_options.decorations); |
| 103 | + if webview_options.fullscreen { |
| 104 | + window_builder = window_builder.with_fullscreen(Some(Fullscreen::Borderless(None))); |
| 105 | + } |
| 106 | + let window = window_builder.build(&event_loop).unwrap(); |
| 107 | + |
| 108 | + eprintln!("transparent: {:?}", webview_options.transparent); |
64 | 109 |
|
65 |
| - let webview = match webview_options.target { |
| 110 | + let webview_builder = match webview_options.target { |
66 | 111 | WebViewTarget::Url(url) => WebViewBuilder::new(&window).with_url(url),
|
67 | 112 | WebViewTarget::Html(html) => WebViewBuilder::new(&window).with_html(html),
|
68 | 113 | }
|
69 |
| - .build()?; |
| 114 | + .with_transparent(webview_options.transparent) |
| 115 | + .with_autoplay(webview_options.autoplay) |
| 116 | + .with_incognito(webview_options.incognito) |
| 117 | + .with_clipboard(webview_options.clipboard) |
| 118 | + .with_focused(webview_options.focused) |
| 119 | + .with_devtools(webview_options.devtools) |
| 120 | + .with_accept_first_mouse(webview_options.accept_first_mouse); |
| 121 | + let webview = webview_builder.build()?; |
70 | 122 |
|
71 | 123 | let (tx, to_deno) = mpsc::channel::<WebViewEvent>();
|
72 | 124 | let (from_deno, rx) = mpsc::channel::<ClientEvent>();
|
|
0 commit comments