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(-) 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/FakeAlert.h | 18 ++++++++++++++++++ Low Battery Yup d/FakeAlert.m | 29 +++++++++++++++++++++++++++++ Low Battery Yup d/Mouse.h | 4 ++++ Low Battery Yup d/Mouse.m | 23 +++++++++++++++++++++-- Low Battery Yup.d.xcodeproj/project.pbxproj | 6 ++++++ 5 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 Low Battery Yup d/FakeAlert.h create mode 100644 Low Battery Yup d/FakeAlert.m diff --git a/Low Battery Yup d/FakeAlert.h b/Low Battery Yup d/FakeAlert.h new file mode 100644 index 0000000..b51ee77 --- /dev/null +++ b/Low Battery Yup d/FakeAlert.h @@ -0,0 +1,18 @@ +// +// FakeAlert.h +// Low Battery Yup.d +// +// Created by TW on 12/2/16. +// Copyright (c) 2016 TW. All rights reserved. +// + +#import + +#define LOW_BATTERY_ALERT_WIDTH 475 +#define LOW_BATTERY_ALERT_HEIGHT 141 + +@interface FakeAlert : NSObject + +- (NSRect)frame; + +@end diff --git a/Low Battery Yup d/FakeAlert.m b/Low Battery Yup d/FakeAlert.m new file mode 100644 index 0000000..ef504a6 --- /dev/null +++ b/Low Battery Yup d/FakeAlert.m @@ -0,0 +1,29 @@ +// +// FakeAlert.m +// Low Battery Yup.d +// +// Created by TW on 12/2/16. +// Copyright (c) 2016 TW. All rights reserved. +// + +#import "FakeAlert.h" + +@implementation FakeAlert + +- (NSRect)frame +{ + NSWindow *window = [[NSWindow alloc] + initWithContentRect:NSMakeRect(0, 0, LOW_BATTERY_ALERT_WIDTH, LOW_BATTERY_ALERT_HEIGHT) + styleMask:NSTitledWindowMask + backing:NSBackingStoreBuffered + defer:YES]; + [window center]; + + NSRect frame = [window frame]; + + [window release]; + + return frame; +} + +@end diff --git a/Low Battery Yup d/Mouse.h b/Low Battery Yup d/Mouse.h index e15a212..f8a6732 100644 --- a/Low Battery Yup d/Mouse.h +++ b/Low Battery Yup d/Mouse.h @@ -7,10 +7,14 @@ // #import +#import "FakeAlert.h" @interface Mouse : NSObject { CGDirectDisplayID _current_display; CGPoint _cursor_position; + + FakeAlert *_fake_alert; + NSRect _fake_alert_frame; } - (void)moveToPoint:(CGPoint)point; 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]; } diff --git a/Low Battery Yup.d.xcodeproj/project.pbxproj b/Low Battery Yup.d.xcodeproj/project.pbxproj index da19448..1e54a44 100644 --- a/Low Battery Yup.d.xcodeproj/project.pbxproj +++ b/Low Battery Yup.d.xcodeproj/project.pbxproj @@ -11,6 +11,7 @@ D131EB381DF1E8AB00504A74 /* com.teddywing.Low-Battery-Yup.StartAtLogin.plist in Resources */ = {isa = PBXBuildFile; fileRef = D131EB371DF1E8AB00504A74 /* com.teddywing.Low-Battery-Yup.StartAtLogin.plist */; }; D131EB4B1DF203CD00504A74 /* Low Battery Yup.d.app in Resources */ = {isa = PBXBuildFile; fileRef = D18C949E1DDC33CF00E03F87 /* Low Battery Yup.d.app */; }; D15D90B71DF0465E001700CD /* ShortcutView.m in Sources */ = {isa = PBXBuildFile; fileRef = D15D90B61DF0465E001700CD /* ShortcutView.m */; }; + D18378A11DF280E6005C5676 /* FakeAlert.m in Sources */ = {isa = PBXBuildFile; fileRef = D18378A01DF280E6005C5676 /* FakeAlert.m */; }; D1871D071DE094AB00B8030D /* DDHotKeyTextField.m in Sources */ = {isa = PBXBuildFile; fileRef = D1871D061DE094AB00B8030D /* DDHotKeyTextField.m */; settings = {COMPILER_FLAGS = "-fobjc-arc"; }; }; D1871D081DE094C200B8030D /* DDHotKeyUtilities.m in Sources */ = {isa = PBXBuildFile; fileRef = D1A37AF01DE001770022434D /* DDHotKeyUtilities.m */; settings = {COMPILER_FLAGS = "-fobjc-arc"; }; }; D1871D091DE0955E00B8030D /* DDHotKeyCenter.m in Sources */ = {isa = PBXBuildFile; fileRef = D123F5A71DDF9D2400A27B7A /* DDHotKeyCenter.m */; settings = {COMPILER_FLAGS = "-fobjc-arc"; }; }; @@ -72,6 +73,8 @@ D131EB371DF1E8AB00504A74 /* com.teddywing.Low-Battery-Yup.StartAtLogin.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "com.teddywing.Low-Battery-Yup.StartAtLogin.plist"; sourceTree = ""; }; D15D90B51DF0465E001700CD /* ShortcutView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ShortcutView.h; sourceTree = ""; }; D15D90B61DF0465E001700CD /* ShortcutView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ShortcutView.m; sourceTree = ""; }; + D183789F1DF280E6005C5676 /* FakeAlert.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FakeAlert.h; sourceTree = ""; }; + D18378A01DF280E6005C5676 /* FakeAlert.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FakeAlert.m; sourceTree = ""; }; D1871D051DE094AB00B8030D /* DDHotKeyTextField.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = DDHotKeyTextField.h; path = DDHotKey/DDHotKeyTextField.h; sourceTree = ""; }; D1871D061DE094AB00B8030D /* DDHotKeyTextField.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = DDHotKeyTextField.m; path = DDHotKey/DDHotKeyTextField.m; sourceTree = ""; }; D18C949E1DDC33CF00E03F87 /* Low Battery Yup.d.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Low Battery Yup.d.app"; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -189,6 +192,8 @@ children = ( D18C94B41DDC33CF00E03F87 /* AppDelegate.h */, D18C94B51DDC33CF00E03F87 /* AppDelegate.m */, + D183789F1DF280E6005C5676 /* FakeAlert.h */, + D18378A01DF280E6005C5676 /* FakeAlert.m */, D18C94C31DDC355400E03F87 /* Mouse.h */, D18C94C41DDC355400E03F87 /* Mouse.m */, D18C94A91DDC33CF00E03F87 /* Supporting Files */, @@ -385,6 +390,7 @@ D18C94C51DDC355400E03F87 /* Mouse.m in Sources */, D123F5A81DDF9D2400A27B7A /* DDHotKeyCenter.m in Sources */, D1A37AF11DE001770022434D /* DDHotKeyUtilities.m in Sources */, + D18378A11DF280E6005C5676 /* FakeAlert.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; -- 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(-) 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