1
1
use crate :: { app_state:: AppState , util:: Config , win_utils:: make_window_transparent} ;
2
2
3
3
use core:: time:: Duration ;
4
- use std:: { os:: raw:: c_void, sync:: Arc , thread :: sleep , time :: Instant } ;
4
+ use std:: { os:: raw:: c_void, sync:: Arc } ;
5
5
6
- use rustc_hash :: FxHashMap ;
6
+ use std :: collections :: HashMap ;
7
7
use windows:: Win32 :: { Foundation :: HWND , UI :: WindowsAndMessaging :: IsWindow } ;
8
-
9
8
#[ derive( Eq , PartialEq , Clone , Debug ) ]
10
9
struct WindowHandleState {
11
10
handle : isize ,
@@ -49,47 +48,40 @@ impl WindowHandleState {
49
48
*/
50
49
#[ inline( always) ]
51
50
pub async fn monitor_windows ( app_state : Arc < AppState > ) {
52
- let mut window_cache = FxHashMap :: with_capacity_and_hasher ( 8 , Default :: default ( ) ) ;
53
-
54
- let refresh_interval = Duration :: from_secs ( 1 ) ;
55
- let sleep_duration = Duration :: from_millis ( 250 ) ;
56
-
57
- let mut last_refresh = Instant :: now ( ) ;
58
- let mut last_state = app_state. is_enabled ( ) . await ;
51
+ let mut window_cache = HashMap :: with_capacity ( 8 ) ;
52
+ let refresh_interval = Duration :: from_millis ( 120 ) ;
59
53
let mut config = app_state. get_config ( ) . await ;
60
54
let mut is_enabled = app_state. is_enabled ( ) . await ;
61
-
62
55
let mut config_rx = app_state. subscribe_config_updates ( ) ;
63
56
let mut enabled_rx = app_state. subscribe_enabled_updates ( ) ;
64
57
65
58
loop {
66
- if let Ok ( new_config) = config_rx. try_recv ( ) {
67
- config = new_config;
68
- }
69
-
70
- if let Ok ( state) = enabled_rx. try_recv ( ) {
71
- is_enabled = state;
72
- }
73
-
74
- if is_enabled {
75
- let now = Instant :: now ( ) ;
76
- if now. duration_since ( last_refresh) > refresh_interval {
77
- refresh_window_cache ( & config, & mut window_cache) ;
78
- last_refresh = now;
59
+ tokio:: select! {
60
+ _ = app_state. shutdown. notified( ) => {
61
+ break ;
79
62
}
80
-
81
- update_windows ( & config, & mut window_cache) ;
82
- } else if last_state != is_enabled {
83
- reset_windows ( & mut window_cache) ;
63
+ Ok ( new_config) = config_rx. recv( ) => {
64
+ config = new_config;
65
+ }
66
+ Ok ( state) = enabled_rx. recv( ) => {
67
+ if state != is_enabled && is_enabled {
68
+ reset_windows( & mut window_cache) ;
69
+ }
70
+ is_enabled = state;
71
+ }
72
+ _ = tokio:: time:: sleep( refresh_interval) => {
73
+ if is_enabled {
74
+ refresh_window_cache( & config, & mut window_cache) ;
75
+ update_windows( & config, & mut window_cache) ;
76
+ }
77
+ }
78
+ else => break
84
79
}
85
-
86
- last_state = is_enabled;
87
- sleep ( sleep_duration) ;
88
80
}
89
81
}
90
82
91
83
#[ inline( always) ]
92
- fn refresh_window_cache ( config : & Config , cache : & mut FxHashMap < String , Vec < WindowHandleState > > ) {
84
+ fn refresh_window_cache ( config : & Config , cache : & mut HashMap < String , Vec < WindowHandleState > > ) {
93
85
for cfg in config. get_windows_non_mut ( ) . values ( ) {
94
86
let handles = cfg. get_window_hwnds ( ) ;
95
87
if handles. is_empty ( ) {
@@ -115,7 +107,7 @@ fn refresh_window_cache(config: &Config, cache: &mut FxHashMap<String, Vec<Windo
115
107
}
116
108
117
109
#[ inline( always) ]
118
- fn update_windows ( config : & Config , window_cache : & mut FxHashMap < String , Vec < WindowHandleState > > ) {
110
+ fn update_windows ( config : & Config , window_cache : & mut HashMap < String , Vec < WindowHandleState > > ) {
119
111
for window_config in config. get_windows_non_mut ( ) . values ( ) {
120
112
if let Some ( handle_states) = window_cache. get_mut ( & window_config. get_cache_key ( ) ) {
121
113
let mut new_transparency = window_config. get_transparency ( ) ;
@@ -138,7 +130,7 @@ fn update_windows(config: &Config, window_cache: &mut FxHashMap<String, Vec<Wind
138
130
}
139
131
140
132
#[ inline( always) ]
141
- fn reset_windows ( window_cache : & mut FxHashMap < String , Vec < WindowHandleState > > ) {
133
+ fn reset_windows ( window_cache : & mut HashMap < String , Vec < WindowHandleState > > ) {
142
134
window_cache
143
135
. values_mut ( )
144
136
. flat_map ( |handles| handles. iter_mut ( ) )
0 commit comments