@@ -9,32 +9,23 @@ use accessibility::{AXUIElement, AXUIElementActions, AXUIElementAttributes};
99use accessibility_sys:: {
1010 kAXErrorAttributeUnsupported, kAXErrorCannotComplete, kAXFocusedAttribute,
1111} ;
12- use core_foundation:: { base:: TCFType , boolean:: CFBoolean , number:: CFNumber } ;
12+ use core_foundation:: { base:: TCFType , boolean:: CFBoolean , number:: CFNumber , string :: CFString } ;
1313use log:: Level ;
14- use objc2_app_kit:: NSEvent ;
15- use rdev:: { Button , EventType , simulate} ;
16- use std:: time:: Duration ;
14+ use monio:: { Button , Event , ScrollDirection } ;
1715
1816impl AppEngine {
19- pub ( super ) fn simulate_event ( & self , event_type : & EventType ) {
20- match simulate ( event_type) {
21- Ok ( ( ) ) => ( ) ,
22- Err ( e) => {
23- log:: error!( "Failed to simulate event {event_type:?}: {e}" ) ;
24- }
25- }
26- }
27-
2817 /// Move the mouse to `(end_x, end_y)` with a fading triangle cursor trail animation.
2918 pub ( super ) fn move_mouse_with_trail ( & self , end_x : f64 , end_y : f64 ) {
3019 // Get current mouse position (Cocoa bottom-left origin)
31- let mouse_loc = NSEvent :: mouseLocation ( ) ;
20+ let mouse_loc = objc2_app_kit :: NSEvent :: mouseLocation ( ) ;
3221
3322 if self . config . theme . enable_animation {
3423 self . drawer
3524 . draw_trail ( mouse_loc. x , mouse_loc. y , end_x, end_y) ;
3625 }
37- self . simulate_event ( & EventType :: MouseMove { x : end_x, y : end_y } ) ;
26+ if let Err ( e) = monio:: mouse_move ( end_x, end_y) {
27+ log:: error!( "Failed to move mouse to ({end_x}, {end_y}): {e}" ) ;
28+ }
3829 }
3930
4031 pub ( super ) fn simulate_click ( & self , x : f64 , y : f64 , button : Button ) {
@@ -53,10 +44,10 @@ impl AppEngine {
5344 let color = frame_colors. get ( color_idx) . unwrap_or ( default_color) ;
5445 self . drawer . draw_ripple ( x, y, color) ;
5546 }
56- std:: thread:: sleep ( Duration :: from_millis ( 20 ) ) ;
57- self . simulate_event ( & EventType :: ButtonPress ( button) ) ;
58- std :: thread :: sleep ( Duration :: from_millis ( 20 ) ) ;
59- self . simulate_event ( & EventType :: ButtonRelease ( button ) ) ;
47+ std:: thread:: sleep ( std :: time :: Duration :: from_millis ( 20 ) ) ;
48+ if let Err ( e ) = monio :: mouse_click ( button) {
49+ log :: error! ( "Failed to click mouse button {button:?}: {e}" ) ;
50+ }
6051 }
6152
6253 pub ( super ) fn focus_on_element ( & self , element : & AXUIElement ) {
@@ -203,6 +194,17 @@ impl AppEngine {
203194 }
204195 }
205196
197+ fn simulate_scroll ( delta : f64 ) {
198+ let direction = if delta > 0.0 {
199+ ScrollDirection :: Up
200+ } else {
201+ ScrollDirection :: Down
202+ } ;
203+ if let Err ( e) = monio:: simulate ( & Event :: mouse_wheel ( 0.0 , 0.0 , direction, delta) ) {
204+ log:: error!( "Failed to simulate scroll {delta}: {e}" ) ;
205+ }
206+ }
207+
206208 pub ( super ) fn perform_scroll_action ( & mut self , sa : ScrollAction ) {
207209 let Some ( selected) = self . selected . as_ref ( ) else {
208210 return ;
@@ -248,19 +250,13 @@ impl AppEngine {
248250 }
249251 }
250252 } else {
251- let distance = ( frame. size ( ) . 1 * self . config . scroll_distance ) . max ( 1.0 ) as i64 ;
253+ let distance = ( frame. size ( ) . 1 * self . config . scroll_distance ) . max ( 1.0 ) ;
252254 match sa {
253255 ScrollAction :: DownRight => {
254- self . simulate_event ( & EventType :: Wheel {
255- delta_x : 0 ,
256- delta_y : -distance,
257- } ) ;
256+ Self :: simulate_scroll ( -distance) ;
258257 }
259258 ScrollAction :: UpLeft => {
260- self . simulate_event ( & EventType :: Wheel {
261- delta_x : 0 ,
262- delta_y : distance,
263- } ) ;
259+ Self :: simulate_scroll ( distance) ;
264260 }
265261 ScrollAction :: IncreaseDistance => {
266262 self . config . scroll_distance *= 1.5 ;
@@ -269,17 +265,11 @@ impl AppEngine {
269265 self . config . scroll_distance /= 1.5 ;
270266 }
271267 ScrollAction :: Top => {
272- self . simulate_event ( & EventType :: Wheel {
273- delta_x : 0 ,
274- delta_y : 999999 ,
275- } ) ;
268+ Self :: simulate_scroll ( 999999.0 ) ;
276269 self . draw_element_menu ( "" , RoleOfInterest :: ScrollBar , false ) ;
277270 }
278271 ScrollAction :: Bottom => {
279- self . simulate_event ( & EventType :: Wheel {
280- delta_x : 0 ,
281- delta_y : -999999 ,
282- } ) ;
272+ Self :: simulate_scroll ( -999999.0 ) ;
283273 self . draw_element_menu ( "" , RoleOfInterest :: ScrollBar , false ) ;
284274 }
285275 }
@@ -300,14 +290,11 @@ impl AppEngine {
300290 pub ( super ) fn update_editing_text ( & mut self , new_text : String ) {
301291 if let Some ( selected) = self . editing . as_ref ( )
302292 && let Some ( ele) = selected. element ( )
293+ && let Err ( e) = ele. set_value ( CFString :: new ( & new_text) . as_CFType ( ) )
303294 {
304- use accessibility:: AXUIElementAttributes ;
305- use core_foundation:: { base:: TCFType , string:: CFString } ;
306- if let Err ( e) = ele. set_value ( CFString :: new ( & new_text) . as_CFType ( ) ) {
307- log:: warn!( "Failed to set the text of focused element: {ele:?}\n Error: {e}" ) ;
308- // Reset editing upon failure
309- self . editing = None ;
310- }
295+ log:: warn!( "Failed to set the text of focused element: {ele:?}\n Error: {e}" ) ;
296+ // Reset editing upon failure
297+ self . editing = None ;
311298 }
312299 }
313300
0 commit comments