-
Notifications
You must be signed in to change notification settings - Fork 409
Expand file tree
/
Copy pathwebkit_config.rs
More file actions
68 lines (55 loc) · 2.62 KB
/
Copy pathwebkit_config.rs
File metadata and controls
68 lines (55 loc) · 2.62 KB
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
//! WebKit configuration for macOS panel behavior.
//!
//! We keep JavaScript active while the panel is hidden and force the WKWebView
//! itself fully transparent so native liquid-glass can show through the app
//! container instead of only around it.
use tauri::Manager;
/// Returns true if the running macOS version is at least `major.minor`.
fn macos_at_least(major: u64, minor: u64) -> bool {
let info = objc2_foundation::NSProcessInfo::processInfo();
let version = info.operatingSystemVersion();
(version.majorVersion as u64, version.minorVersion as u64) >= (major, minor)
}
pub fn configure_webview(app_handle: &tauri::AppHandle) {
let Some(window) = app_handle.get_webview_window("main") else {
log::warn!("webkit_config: main window not found");
return;
};
let can_disable_inactive_scheduling = macos_at_least(14, 0);
if !can_disable_inactive_scheduling {
log::info!("WebKit inactiveSchedulingPolicy requires macOS 14.0+; skipping on this system");
}
if let Err(e) = window.with_webview(move |webview| unsafe {
use objc2::sel;
use objc2_app_kit::NSColor;
use objc2_foundation::{NSNumber, NSObjectNSKeyValueCoding, NSObjectProtocol, ns_string};
use objc2_web_kit::{WKInactiveSchedulingPolicy, WKWebView};
let wk_webview: &WKWebView = &*webview.inner().cast();
let clear = NSColor::clearColor();
let no = NSNumber::numberWithBool(false);
let config = wk_webview.configuration();
let prefs = config.preferences();
if can_disable_inactive_scheduling {
prefs.setInactiveSchedulingPolicy(WKInactiveSchedulingPolicy::None);
}
config.setValue_forKey(Some(&no), ns_string!("drawsBackground"));
wk_webview.setValue_forKey(Some(&no), ns_string!("drawsBackground"));
if wk_webview.respondsToSelector(sel!(setUnderPageBackgroundColor:)) {
wk_webview.setUnderPageBackgroundColor(Some(&clear));
}
if let Some(scroll_view) = wk_webview.enclosingScrollView() {
scroll_view.setDrawsBackground(false);
scroll_view.setBackgroundColor(&clear);
let clip_view = scroll_view.contentView();
clip_view.setDrawsBackground(false);
clip_view.setBackgroundColor(&clear);
}
if let Some(ns_window) = wk_webview.window() {
ns_window.setOpaque(false);
ns_window.setBackgroundColor(Some(&clear));
}
log::info!("Configured transparent WKWebView and disabled inactive scheduling");
}) {
log::warn!("Failed to configure WebKit scheduling: {e}");
}
}