3
3
windows_subsystem = "windows"
4
4
) ]
5
5
6
- mod clipboard;
7
- mod database;
8
- mod hotkeys;
9
- mod tray;
10
- mod updater;
6
+ mod api;
7
+ mod utils;
11
8
12
- use tauri:: Manager ;
13
- use tauri:: PhysicalPosition ;
9
+ use tauri:: { Manager , Listener } ;
14
10
use tauri_plugin_autostart:: MacosLauncher ;
15
11
use tauri_plugin_prevent_default:: Flags ;
16
12
17
- pub fn center_window_on_current_monitor ( window : & tauri:: WebviewWindow ) {
18
- if let Some ( monitor) = window. available_monitors ( ) . unwrap ( ) . iter ( ) . find ( |m| {
19
- let primary_monitor = window
20
- . primary_monitor ( )
21
- . unwrap ( )
22
- . expect ( "Failed to get primary monitor" ) ;
23
- let mouse_position = primary_monitor. position ( ) ;
24
- let monitor_position = m. position ( ) ;
25
- let monitor_size = m. size ( ) ;
26
- mouse_position. x >= monitor_position. x
27
- && mouse_position. x < monitor_position. x + monitor_size. width as i32
28
- && mouse_position. y >= monitor_position. y
29
- && mouse_position. y < monitor_position. y + monitor_size. height as i32
30
- } ) {
31
- let monitor_size = monitor. size ( ) ;
32
- let window_size = window. outer_size ( ) . unwrap ( ) ;
33
-
34
- let x = ( monitor_size. width as i32 - window_size. width as i32 ) / 2 ;
35
- let y = ( monitor_size. height as i32 - window_size. height as i32 ) / 2 ;
36
-
37
- window
38
- . set_position ( PhysicalPosition :: new (
39
- monitor. position ( ) . x + x,
40
- monitor. position ( ) . y + y,
41
- ) )
42
- . unwrap ( ) ;
43
- }
44
- }
45
-
46
13
fn main ( ) {
47
14
tauri:: Builder :: default ( )
48
15
. plugin ( tauri_plugin_clipboard:: init ( ) )
@@ -54,58 +21,55 @@ fn main() {
54
21
MacosLauncher :: LaunchAgent ,
55
22
Some ( vec ! [ ] ) ,
56
23
) )
24
+ . plugin ( api:: updater:: init ( ) )
25
+ . plugin ( api:: database:: init ( ) )
26
+ . plugin ( api:: tray:: init ( ) )
27
+ . plugin ( api:: hotkeys:: init ( ) )
28
+ . plugin ( api:: clipboard:: init ( ) )
57
29
. plugin (
58
30
tauri_plugin_prevent_default:: Builder :: new ( )
59
31
. with_flags ( Flags :: all ( ) . difference ( Flags :: CONTEXT_MENU ) )
60
32
. build ( ) ,
61
33
)
62
34
. setup ( |app| {
63
35
let app_handle = app. handle ( ) . clone ( ) ;
64
-
65
- hotkeys:: setup ( app_handle. clone ( ) ) ;
66
- tray:: setup ( app) ?;
67
- database:: setup ( app) ?;
68
- clipboard:: setup ( app. handle ( ) ) ;
69
- let _ = clipboard:: start_monitor ( app_handle. clone ( ) ) ;
70
-
71
- if let Some ( window) = app. get_webview_window ( "main" ) {
72
- center_window_on_current_monitor ( & window) ;
73
- window. hide ( ) . unwrap ( ) ;
74
- }
75
-
76
- // #[cfg(dev)]
77
- // {
78
- // let window = app.get_webview_window("main").unwrap();
79
- // window.open_devtools();
80
- // window.close_devtools();
81
- // }
82
-
36
+
83
37
let app_data_dir = app
84
38
. path ( )
85
39
. app_data_dir ( )
86
40
. expect ( "Failed to get app data directory" ) ;
87
- clipboard:: set_app_data_dir ( app_data_dir) ;
41
+ api:: clipboard:: set_app_data_dir ( app_data_dir) ;
42
+
43
+ if let Some ( window) = app. get_webview_window ( "main" ) {
44
+ utils:: commands:: center_window_on_current_monitor ( & window) ;
45
+ window. hide ( ) . unwrap ( ) ;
46
+ }
88
47
48
+ let update_handle = app_handle. clone ( ) ;
89
49
tauri:: async_runtime:: spawn ( async move {
90
- updater:: check_for_updates ( app_handle) . await ;
50
+ api:: updater:: check_for_updates ( update_handle) . await ;
51
+ } ) ;
52
+
53
+ let monitor_handle = app_handle. clone ( ) ;
54
+ app_handle. listen ( "database_initialized" , move |_| {
55
+ let _ = api:: clipboard:: start_monitor ( monitor_handle. clone ( ) ) ;
91
56
} ) ;
92
57
93
58
Ok ( ( ) )
94
59
} )
95
- . on_window_event ( |app, event| match event {
60
+ . on_window_event ( |app, event| {
96
61
#[ cfg( not( dev) ) ]
97
- tauri :: WindowEvent :: Focused ( false ) => {
62
+ if let WindowEvent :: Focused ( false ) = event {
98
63
if let Some ( window) = app. get_webview_window ( "main" ) {
99
- window. hide ( ) . unwrap ( ) ;
64
+ let _ = window. hide ( ) ;
100
65
}
101
66
}
102
- _ => { }
103
67
} )
104
68
. invoke_handler ( tauri:: generate_handler![
105
- clipboard:: simulate_paste,
106
- clipboard:: get_image_path,
107
- clipboard:: read_image
69
+ api :: clipboard:: simulate_paste,
70
+ api :: clipboard:: get_image_path,
71
+ api :: clipboard:: read_image
108
72
] )
109
73
. run ( tauri:: generate_context!( ) )
110
74
. expect ( "error while running tauri application" ) ;
111
- }
75
+ }
0 commit comments