11mod tray;
22
3- use enigo:: { Direction , Enigo , Key , Keyboard , Settings } ;
4- use tauri:: { AppHandle , Manager , Runtime } ;
3+ use enigo:: { Direction , Enigo , Key , Keyboard , Mouse , Settings } ;
4+ use tauri:: { AppHandle , Manager , Monitor , PhysicalPosition , Runtime } ;
55use tauri_plugin_autostart:: MacosLauncher ;
6+ use tauri_plugin_global_shortcut:: { GlobalShortcutExt , Shortcut } ;
67use tauri_plugin_store:: StoreExt ;
78use window_vibrancy:: { apply_acrylic, apply_mica} ;
89
910use serde_json:: json;
1011
12+ // 定义默认的Panel窗口大小
13+ const DEFAULT_PANEL_WIDTH : i32 = 400 ;
14+ const DEFAULT_PANEL_HEIGHT : i32 = 476 ;
15+
1116#[ tauri:: command]
1217async fn input_text ( text : & str ) -> Result < ( ) , String > {
1318 let mut enigo = Enigo :: new ( & Settings :: default ( ) ) . unwrap ( ) ;
@@ -27,7 +32,6 @@ async fn simulate_paste() -> Result<(), String> {
2732#[ tauri:: command]
2833async fn get_key_from_store < R : Runtime > (
2934 app : tauri:: AppHandle < R > ,
30- _window : tauri:: Window < R > ,
3135 key : String ,
3236 fallback : serde_json:: Value ,
3337) -> Result < serde_json:: Value , String > {
@@ -49,7 +53,6 @@ async fn get_key_from_store<R: Runtime>(
4953#[ tauri:: command]
5054async fn set_key_to_store < R : Runtime > (
5155 app : tauri:: AppHandle < R > ,
52- _window : tauri:: Window < R > ,
5356 key : String ,
5457 value : serde_json:: Value ,
5558) -> Result < ( ) , String > {
@@ -66,12 +69,150 @@ async fn set_key_to_store<R: Runtime>(
6669fn show_window ( app : & AppHandle ) {
6770 let windows = app. webview_windows ( ) ;
6871
69- windows
70- . values ( )
71- . next ( )
72- . expect ( "Sorry, no window found" )
73- . set_focus ( )
74- . expect ( "Can't Bring Window to Focus" ) ;
72+ let window = windows. values ( ) . next ( ) . expect ( "Sorry, no window found" ) ;
73+ window. show ( ) . expect ( "Can't Show Window" ) ;
74+ window. set_focus ( ) . expect ( "Can't Bring Window to Focus" ) ;
75+ }
76+
77+ fn show_window_with_name ( app : & AppHandle < tauri:: Wry > , name : & str ) {
78+ let windows = app. webview_windows ( ) ;
79+
80+ let window = windows. get ( name) . expect ( "Sorry, no window found" ) ;
81+ window. show ( ) . expect ( "Can't Show Window" ) ;
82+ window. set_focus ( ) . expect ( "Can't Bring Window to Focus" ) ;
83+ }
84+
85+ fn show_window_with_name_and_position ( app : & AppHandle < tauri:: Wry > , pos : PhysicalPosition < i32 > ) {
86+ let windows = app. webview_windows ( ) ;
87+ let window = match windows. get ( "context" ) {
88+ Some ( win) => win. clone ( ) ,
89+ None => return ,
90+ } ;
91+
92+ let monitors = match window. available_monitors ( ) {
93+ Ok ( m) => m,
94+ Err ( _) => {
95+ let _ = window. center ( ) ;
96+ let _ = window. show ( ) ;
97+ let _ = window. set_focus ( ) ;
98+ return ;
99+ }
100+ } ;
101+
102+ if monitors. is_empty ( ) {
103+ let _ = window. center ( ) ;
104+ let _ = window. show ( ) ;
105+ let _ = window. set_focus ( ) ;
106+ return ;
107+ }
108+
109+ tauri:: async_runtime:: spawn ( async move {
110+ let panel_width = DEFAULT_PANEL_WIDTH ;
111+ let panel_height = DEFAULT_PANEL_HEIGHT ;
112+
113+ let mut current_monitor: Option < Monitor > = None ;
114+ for monitor in monitors {
115+ let size = monitor. size ( ) ;
116+ let position: & PhysicalPosition < i32 > = monitor. position ( ) ;
117+
118+ let monitor_x = position. x ;
119+ let monitor_y = position. y ;
120+ let monitor_width = size. width as i32 ;
121+ let monitor_height = size. height as i32 ;
122+
123+ if pos. x >= monitor_x
124+ && pos. x < monitor_x + monitor_width
125+ && pos. y >= monitor_y
126+ && pos. y < monitor_y + monitor_height
127+ {
128+ current_monitor = Some ( monitor) ;
129+ break ;
130+ }
131+ }
132+
133+ if let Some ( monitor) = current_monitor {
134+ let size = monitor. size ( ) ;
135+ let position = monitor. position ( ) ;
136+
137+ let monitor_x = position. x ;
138+ let monitor_y = position. y ;
139+ let monitor_width = size. width as i32 ;
140+ let monitor_height = size. height as i32 ;
141+
142+ let mut x = pos. x ;
143+ let mut y = pos. y ;
144+
145+ if x + panel_width > monitor_x + monitor_width {
146+ x = x - panel_width;
147+ }
148+
149+ if y + panel_height > monitor_y + monitor_height {
150+ y = y - panel_height;
151+ }
152+
153+ x = std:: cmp:: max (
154+ monitor_x,
155+ std:: cmp:: min ( x, monitor_x + monitor_width - panel_width) ,
156+ ) ;
157+ y = std:: cmp:: max (
158+ monitor_y,
159+ std:: cmp:: min ( y, monitor_y + monitor_height - panel_height) ,
160+ ) ;
161+
162+ let adjusted_pos = PhysicalPosition :: new ( x, y) ;
163+
164+ let _ = window. set_position ( adjusted_pos) ;
165+ let _ = window. show ( ) ;
166+ let _ = window. set_always_on_top ( true ) ;
167+ let _ = window. set_focus ( ) ;
168+ return ;
169+ }
170+
171+ let _ = window. set_position ( pos) ;
172+ let _ = window. show ( ) ;
173+ let _ = window. set_always_on_top ( true ) ;
174+ let _ = window. set_focus ( ) ;
175+
176+ } ) ;
177+ }
178+
179+ #[ tauri:: command]
180+ async fn reregister_panel_shortcut ( app : tauri:: AppHandle < tauri:: Wry > ) -> Result < ( ) , String > {
181+ let _ = app. global_shortcut ( ) . unregister_all ( ) ;
182+ let mut shortcut_string = "CmdOrControl+Shift+V" . to_string ( ) ;
183+ let stores = app. store ( "store.bin" ) ;
184+ let _store = match stores {
185+ Ok ( store) => {
186+ if let Some ( value) = store. get ( "globalShortcut" ) {
187+ if let Some ( s) = value. as_str ( ) {
188+ shortcut_string = s. to_string ( ) ;
189+ }
190+ }
191+ }
192+ Err ( _) => return Err ( "Failed to read config" . to_string ( ) ) ,
193+ } ;
194+ let shortcut = match shortcut_string. parse :: < Shortcut > ( ) {
195+ Ok ( s) => s,
196+ Err ( _) => return Err ( "Failed to parse shortcut" . to_string ( ) ) ,
197+ } ;
198+
199+ let _ = app
200+ . global_shortcut ( )
201+ . on_shortcut ( shortcut, move |app_handle, _event, _shortcut| {
202+ let enigo = Enigo :: new ( & Settings :: default ( ) ) ;
203+ let mut location = PhysicalPosition :: new ( 0 , 0 ) ;
204+
205+ // 获取鼠标位置
206+ if let Ok ( enigo) = enigo {
207+ if let Ok ( pos) = enigo. location ( ) {
208+ location = PhysicalPosition :: new ( pos. 0 as i32 , pos. 1 as i32 ) ;
209+ }
210+ }
211+
212+ show_window_with_name_and_position ( app_handle, location) ;
213+ } ) ;
214+
215+ Ok ( ( ) )
75216}
76217
77218#[ cfg_attr( mobile, tauri:: mobile_entry_point) ]
@@ -82,7 +223,7 @@ pub fn run() {
82223 . plugin ( tauri_plugin_process:: init ( ) )
83224 . plugin ( tauri_plugin_store:: Builder :: new ( ) . build ( ) )
84225 . plugin ( tauri_plugin_single_instance:: init ( |app, _args, _cwd| {
85- let _ = show_window ( app) ;
226+ let _ = show_window_with_name ( app, "main" ) ;
86227 } ) )
87228 . plugin ( tauri_plugin_autostart:: init (
88229 MacosLauncher :: LaunchAgent ,
@@ -102,14 +243,26 @@ pub fn run() {
102243
103244 let handle = app. handle ( ) ;
104245 let _ = tray:: create_tray ( handle) ;
246+
247+ let args: Vec < String > = std:: env:: args ( ) . collect ( ) ;
248+ if !args. contains ( & "--autostart" . to_string ( ) ) && !args. contains ( & "--silent" . to_string ( ) )
249+ {
250+ show_window_with_name ( app. handle ( ) , "main" ) ;
251+ }
252+ let app_handle = app. handle ( ) . clone ( ) ;
253+ tauri:: async_runtime:: spawn ( async move {
254+ let _ = reregister_panel_shortcut ( app_handle) . await ;
255+ } ) ;
105256 Ok ( ( ) )
106257 } )
107258 . plugin ( tauri_plugin_opener:: init ( ) )
259+ . plugin ( tauri_plugin_cors_fetch:: init ( ) )
108260 . invoke_handler ( tauri:: generate_handler![
109261 input_text,
110262 simulate_paste,
111263 get_key_from_store,
112- set_key_to_store
264+ set_key_to_store,
265+ reregister_panel_shortcut
113266 ] )
114267 . run ( tauri:: generate_context!( ) )
115268 . expect ( "error while running tauri application" ) ;
0 commit comments