From 954071bcc651ed5876d8ac0c3e8c253d4470c416 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Fri, 2 Dec 2016 18:53:55 -0500 Subject: Mouse.m: Some calculations on window center Wanted to get a percentage that I could apply to the height to get to the right pixel position on the screen, but it doesn't seem like it's going to be that simple. The percentages are off for a big screen and a small screen, so I can't rely on a single percentage value. After some research, my current idea is to make a fake window, call NSWindow -center on it, and get the coordinates of that window to figure out where the mouse should be positioned. --- Low Battery Yup d/Mouse.m | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'Low Battery Yup d/Mouse.m') diff --git a/Low Battery Yup d/Mouse.m b/Low Battery Yup d/Mouse.m index 9bade59..d5bf766 100644 --- a/Low Battery Yup d/Mouse.m +++ b/Low Battery Yup d/Mouse.m @@ -34,7 +34,8 @@ size_t height = CGDisplayPixelsHigh(_current_display); point.x = width / 2 + 182; - point.y = height / 2 - 116; +// point.y = height / 2 - 116; // 1440x900 | 450 - 116 = 334 | 900 / 334 | 3340 / 900 = 3.7111 + point.y = height / 2 - 292; // 2560x1600 | 800 - 292 = 508 | 1600 / 508 | 5080 / 1600 = 3.175 [self moveToPoint:point]; } -- cgit v1.2.3 From 9d91a7c908dc2261f2c3335875323c7088938204 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sat, 3 Dec 2016 00:22:17 -0500 Subject: Click the right spot on all screen sizes Previously, the point set to be clicked for the low battery alert was only valid on a 13" 1440x900 screen. This change allows the click functionality to work on any screen size. I discovered in 954071bcc651ed5876d8ac0c3e8c253d4470c416 that I couldn't use a percentage to get the height of the "OK" button. But, the low battery alert uses `NSWindow`'s `-center` method, so I thought up a hack to make a fake window, center it, and get the window's frame dimensions. The fake window I create has the same (close enough) dimensions as the low battery alert (measured from a screenshot). I then use those dimensions to figure out where to click so that I'm clicking in the right place on the "OK" button. We also add a check to reinitialise the dimensions and reset the current window if the main window has changed from what we thought it was. This should allow the app to still work if you launch it from one display and later plug in another as your main display where the low battery warning will now appear. We need to subtract the frame's Y coordinate from the screen height because the frame origin uses standard bottom-left origin coordinates, while `CGDisplayMoveCursorToPoint` uses top-left origin coordinates. --- Low Battery Yup d/Mouse.m | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'Low Battery Yup d/Mouse.m') diff --git a/Low Battery Yup d/Mouse.m b/Low Battery Yup d/Mouse.m index d5bf766..b3139fb 100644 --- a/Low Battery Yup d/Mouse.m +++ b/Low Battery Yup d/Mouse.m @@ -16,10 +16,18 @@ if (self) { _current_display = CGMainDisplayID(); _cursor_position = CGPointMake(0, 0); + _fake_alert = [[FakeAlert alloc] init]; + _fake_alert_frame = [_fake_alert frame]; } return self; } +- (void)dealloc +{ + [_fake_alert release]; + [super dealloc]; +} + - (void)moveToPoint:(CGPoint)point { _cursor_position = point; @@ -28,14 +36,25 @@ - (void)moveToLowBatteryOK { + if (_current_display != CGMainDisplayID()) { + _current_display = CGMainDisplayID(); + _fake_alert_frame = [_fake_alert frame]; + } + CGPoint point; size_t width = CGDisplayPixelsWide(_current_display); size_t height = CGDisplayPixelsHigh(_current_display); point.x = width / 2 + 182; -// point.y = height / 2 - 116; // 1440x900 | 450 - 116 = 334 | 900 / 334 | 3340 / 900 = 3.7111 - point.y = height / 2 - 292; // 2560x1600 | 800 - 292 = 508 | 1600 / 508 | 5080 / 1600 = 3.175 + point.y = height / 2 - 116; // 1440x900 | 450 - 116 = 334 | 900 / 334 | 3340 / 900 = 3.7111 +// point.y = height / 2 - 292; // 2560x1600 | 800 - 292 = 508 | 1600 / 508 | 5080 / 1600 = 3.175 + + // x + 420 + // y + 30 + point.x = _fake_alert_frame.origin.x + 420; + point.y = height - _fake_alert_frame.origin.y - 30; + NSLog(@"%f, %f, %f, %f", _fake_alert_frame.origin.x, _fake_alert_frame.origin.y, _fake_alert_frame.size.width, _fake_alert_frame.size.height); [self moveToPoint:point]; } -- cgit v1.2.3 From 7ffa846dff745feaccdc34d0d4caf72850bcd55d Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sat, 3 Dec 2016 00:32:01 -0500 Subject: Clean up code from 9d91a7c908dc2261f2c3335875323c7088938204 * Remove the old point assignments * Remove the comments describing the pixel offsets from the bottom-left of the low battery alert window * Remove the NSLog for inspecting the frame rect values * Remove the `width` variable which is no longer used --- Low Battery Yup d/Mouse.m | 8 -------- 1 file changed, 8 deletions(-) (limited to 'Low Battery Yup d/Mouse.m') diff --git a/Low Battery Yup d/Mouse.m b/Low Battery Yup d/Mouse.m index b3139fb..103909d 100644 --- a/Low Battery Yup d/Mouse.m +++ b/Low Battery Yup d/Mouse.m @@ -43,18 +43,10 @@ CGPoint point; - size_t width = CGDisplayPixelsWide(_current_display); size_t height = CGDisplayPixelsHigh(_current_display); - point.x = width / 2 + 182; - point.y = height / 2 - 116; // 1440x900 | 450 - 116 = 334 | 900 / 334 | 3340 / 900 = 3.7111 -// point.y = height / 2 - 292; // 2560x1600 | 800 - 292 = 508 | 1600 / 508 | 5080 / 1600 = 3.175 - - // x + 420 - // y + 30 point.x = _fake_alert_frame.origin.x + 420; point.y = height - _fake_alert_frame.origin.y - 30; - NSLog(@"%f, %f, %f, %f", _fake_alert_frame.origin.x, _fake_alert_frame.origin.y, _fake_alert_frame.size.width, _fake_alert_frame.size.height); [self moveToPoint:point]; } -- cgit v1.2.3