@@ -11,6 +11,31 @@ use miaoclaw_lib::commands::*;
1111use miaoclaw_lib:: config:: ConfigManager ;
1212use miaoclaw_lib:: plugin:: PluginEngine ;
1313
14+ /// macOS: 递归遍历子视图,找到 WKWebView 并设置透明背景
15+ #[ cfg( target_os = "macos" ) ]
16+ unsafe fn set_webview_transparent ( view : cocoa:: base:: id ) {
17+ use cocoa:: appkit:: NSView ;
18+ use cocoa:: base:: { id, nil, NO } ;
19+ use objc:: { msg_send, sel, sel_impl, class} ;
20+
21+ // 检查是否是 WKWebView
22+ let wk_class = class ! ( WKWebView ) ;
23+ let is_wk: bool = msg_send ! [ view, isKindOfClass: wk_class] ;
24+ if is_wk {
25+ // WKWebView 支持 _setDrawsBackground:
26+ let _: ( ) = msg_send ! [ view, _setDrawsBackground: NO ] ;
27+ return ;
28+ }
29+
30+ // 递归子视图
31+ let subviews: id = view. subviews ( ) ;
32+ let count: usize = msg_send ! [ subviews, count] ;
33+ for i in 0 ..count {
34+ let subview: id = msg_send ! [ subviews, objectAtIndex: i] ;
35+ set_webview_transparent ( subview) ;
36+ }
37+ }
38+
1439fn main ( ) {
1540 tracing_subscriber:: fmt:: init ( ) ;
1641
@@ -192,9 +217,8 @@ fn main() {
192217 #[ cfg( target_os = "macos" ) ]
193218 {
194219 if let Some ( window) = app. get_webview_window ( "pet" ) {
195- use cocoa:: appkit:: { NSColor , NSWindow } ;
220+ use cocoa:: appkit:: { NSColor , NSWindow , NSView } ;
196221 use cocoa:: base:: { id, nil, NO } ;
197- use cocoa:: foundation:: NSString ;
198222 use objc:: { msg_send, sel, sel_impl} ;
199223
200224 let ns_window = window. ns_window ( ) . unwrap ( ) as id ;
@@ -204,9 +228,9 @@ fn main() {
204228 ns_window. setOpaque_ ( NO ) ;
205229 ns_window. setHasShadow_ ( NO ) ;
206230
207- let ns_view = window . ns_view ( ) . unwrap ( ) as id ;
208- let key = NSString :: alloc ( nil ) . init_str ( "drawsBackground" ) ;
209- let _ : ( ) = msg_send ! [ ns_view , setValue : NO forKey : key ] ;
231+ // 遍历子视图找到 WKWebView 并设置透明
232+ let content_view : id = ns_window . contentView ( ) ;
233+ set_webview_transparent ( content_view ) ;
210234 }
211235 }
212236 }
0 commit comments