11use tauri:: { AppHandle , Emitter , Manager , WebviewUrl } ;
22use crate :: { sentinel, title, tab_label} ;
33
4+ /// Recherche un WebView enfant par son tab_id dans la fenêtre principale.
5+ /// `app.get_webview()` ne trouve PAS les WebViews ajoutés via Window::add_child —
6+ /// ils sont stockés dans la liste de la fenêtre parente, pas dans le registre global.
7+ fn find_tab_webview < R : tauri:: Runtime > ( app : & AppHandle < R > , tab_id : & str ) -> Option < tauri:: Webview < R > > {
8+ let label = tab_label ( tab_id) ;
9+ let win = app. get_window ( "main" ) ?;
10+ win. webviews ( ) . into_iter ( ) . find ( |w| w. label ( ) == label. as_str ( ) )
11+ }
12+
413#[ tauri:: command]
514pub fn tab_webview_create ( app : AppHandle , tab_id : String , url : String ) -> Result < ( ) , String > {
615 let label = tab_label ( & tab_id) ;
@@ -48,13 +57,13 @@ pub fn tab_webview_create(app: AppHandle, tab_id: String, url: String) -> Result
4857 }
4958 } ) ,
5059 tauri:: LogicalPosition :: new ( -9999.0 , -9999.0 ) ,
51- tauri:: LogicalSize :: new ( 1 .0, 1 .0) ,
60+ tauri:: LogicalSize :: new ( 1280 .0, 800 .0) ,
5261 ) . map ( |_| ( ) ) . map_err ( |e| e. to_string ( ) )
5362}
5463
5564#[ tauri:: command]
5665pub fn tab_webview_show ( app : AppHandle , tab_id : String , top : f64 , width : f64 , height : f64 ) -> Result < ( ) , String > {
57- let wv = app . get_webview ( & tab_label ( & tab_id) ) . ok_or ( "webview introuvable" ) ?;
66+ let wv = find_tab_webview ( & app , & tab_id) . ok_or_else ( || format ! ( "webview introuvable: {tab_id}" ) ) ?;
5867 wv. set_bounds ( tauri:: Rect {
5968 position : tauri:: Position :: Logical ( tauri:: LogicalPosition :: new ( 0.0 , top) ) ,
6069 size : tauri:: Size :: Logical ( tauri:: LogicalSize :: new ( width, height) ) ,
@@ -63,40 +72,40 @@ pub fn tab_webview_show(app: AppHandle, tab_id: String, top: f64, width: f64, he
6372
6473#[ tauri:: command]
6574pub fn tab_webview_hide ( app : AppHandle , tab_id : String ) -> Result < ( ) , String > {
66- if let Some ( wv) = app . get_webview ( & tab_label ( & tab_id) ) {
75+ if let Some ( wv) = find_tab_webview ( & app , & tab_id) {
6776 wv. set_bounds ( tauri:: Rect {
6877 position : tauri:: Position :: Logical ( tauri:: LogicalPosition :: new ( -9999.0 , -9999.0 ) ) ,
69- size : tauri:: Size :: Logical ( tauri:: LogicalSize :: new ( 1 .0, 1 .0) ) ,
78+ size : tauri:: Size :: Logical ( tauri:: LogicalSize :: new ( 1280 .0, 800 .0) ) ,
7079 } ) . map_err ( |e| e. to_string ( ) ) ?;
7180 }
7281 Ok ( ( ) )
7382}
7483
7584#[ tauri:: command]
7685pub fn tab_webview_close ( app : AppHandle , tab_id : String ) -> Result < ( ) , String > {
77- if let Some ( wv) = app . get_webview ( & tab_label ( & tab_id) ) {
86+ if let Some ( wv) = find_tab_webview ( & app , & tab_id) {
7887 wv. close ( ) . map_err ( |e| e. to_string ( ) ) ?;
7988 }
8089 Ok ( ( ) )
8190}
8291
8392#[ tauri:: command]
8493pub fn tab_webview_navigate ( app : AppHandle , tab_id : String , url : String ) -> Result < ( ) , String > {
85- let wv = app . get_webview ( & tab_label ( & tab_id) ) . ok_or ( "webview introuvable" ) ?;
94+ let wv = find_tab_webview ( & app , & tab_id) . ok_or_else ( || format ! ( "webview introuvable: {tab_id}" ) ) ?;
8695 wv. navigate ( tauri:: Url :: parse ( & url) . map_err ( |e| e. to_string ( ) ) ?) . map_err ( |e| e. to_string ( ) )
8796}
8897
8998#[ tauri:: command]
9099pub fn tab_webview_eval ( app : AppHandle , tab_id : String , js : String ) -> Result < ( ) , String > {
91- if let Some ( wv) = app . get_webview ( & tab_label ( & tab_id) ) {
100+ if let Some ( wv) = find_tab_webview ( & app , & tab_id) {
92101 wv. eval ( & js) . map_err ( |e| e. to_string ( ) ) ?;
93102 }
94103 Ok ( ( ) )
95104}
96105
97106#[ tauri:: command]
98107pub fn tab_webview_reload ( app : AppHandle , tab_id : String ) -> Result < ( ) , String > {
99- if let Some ( wv) = app . get_webview ( & tab_label ( & tab_id) ) {
108+ if let Some ( wv) = find_tab_webview ( & app , & tab_id) {
100109 wv. eval ( "window.location.reload()" ) . map_err ( |e| e. to_string ( ) ) ?;
101110 }
102111 Ok ( ( ) )
0 commit comments