11use std:: sync:: { Arc , Mutex } ;
22
3- use tauri:: { App , AppHandle , Emitter , State } ;
4- use tauri_plugin_clipboard_manager:: ClipboardExt ;
5- use tokio:: time:: { self , Duration } ;
3+ use tauri:: { AppHandle , Emitter , State , WindowEvent } ;
64
75// use prisma::PrismaClient;
86
@@ -14,55 +12,58 @@ struct ClipboardState {
1412mod prisma;
1513mod shortcuts;
1614mod tray_menu;
15+ mod watcher;
1716
1817#[ tauri:: command]
1918fn get_recent_clipboard_entries (
2019 clipboard_state : State < ' _ , Arc < Mutex < ClipboardState > > > ,
2120) -> Vec < String > {
2221 let state = clipboard_state. lock ( ) . unwrap ( ) ;
23- println ! ( "Returning recent clipboard entries: {:?}" , state. recent) ;
2422 state. recent . clone ( )
2523}
2624
2725#[ cfg_attr( mobile, tauri:: mobile_entry_point) ]
28- pub async fn run ( ) -> anyhow :: Result < ( ) > {
26+ pub fn run ( ) {
2927 let clipboard_state = Arc :: new ( Mutex :: new ( ClipboardState :: default ( ) ) ) ;
30- let app = tauri:: Builder :: default ( )
28+ tauri:: Builder :: default ( )
3129 . manage ( clipboard_state. clone ( ) )
3230 . setup ( |app| {
33- let app_handle = app. handle ( ) . clone ( ) ;
31+ let app_handle = app. handle ( ) ;
32+ start_app ( & app_handle) ?;
33+
34+ let app_handle_clone = app_handle. clone ( ) ;
3435 tauri:: async_runtime:: spawn ( async move {
35- start_app ( & app_handle ) . await . unwrap ( ) ;
36+ watcher :: run ( app_handle_clone ) . await ;
3637 } ) ;
3738 Ok ( ( ) )
3839 } )
3940 . on_menu_event ( |app, event| tray_menu:: on_menu_event ( app, event) )
4041 . on_window_event ( |window, event| match event {
41- tauri:: WindowEvent :: Focused ( focused) => {
42+ WindowEvent :: CloseRequested { api, .. } => {
43+ if let Err ( e) = window. hide ( ) {
44+ eprintln ! ( "Failed to hide window: {:?}" , e) ;
45+ }
46+ api. prevent_close ( ) ;
47+ }
48+ WindowEvent :: Focused ( focused) => {
4249 if window. label ( ) == "Tab_Clip_Manager_main" {
4350 if * focused {
4451 window. emit ( "window_open" , ( ) ) . unwrap ( ) ;
4552 } else {
46- window. hide ( ) . unwrap ( ) ;
53+ if let Err ( e) = window. hide ( ) {
54+ eprintln ! ( "Failed to hide window: {:?}" , e) ;
55+ }
4756 }
4857 }
4958 }
5059 _ => { }
5160 } )
5261 . invoke_handler ( tauri:: generate_handler![ get_recent_clipboard_entries] )
53- . build ( tauri:: generate_context!( ) ) ?;
54-
55- app. run ( |_app_handle, event| match event {
56- tauri:: RunEvent :: ExitRequested { api, .. } => {
57- api. prevent_exit ( ) ;
58- }
59- _ => { }
60- } ) ;
61-
62- Ok ( ( ) )
62+ . run ( tauri:: generate_context!( ) )
63+ . expect ( "error while running tauri application" ) ;
6364}
6465
65- async fn start_app ( app : & AppHandle ) -> anyhow:: Result < ( ) > {
66+ fn start_app ( app : & AppHandle ) -> anyhow:: Result < ( ) > {
6667 tray_menu:: setup_tray_menu ( app) ?;
6768 shortcuts:: add_shortcuts ( app) ?;
6869
0 commit comments