diff options
author | Teddy Wing | 2016-12-03 00:22:17 -0500 |
---|---|---|
committer | Teddy Wing | 2016-12-03 00:22:17 -0500 |
commit | 9d91a7c908dc2261f2c3335875323c7088938204 (patch) | |
tree | e7924a71ba7c0171c6991e1c624247f2d8323b99 | |
parent | 954071bcc651ed5876d8ac0c3e8c253d4470c416 (diff) | |
download | Low-Battery-Yup-9d91a7c908dc2261f2c3335875323c7088938204.tar.bz2 |
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.
-rw-r--r-- | Low Battery Yup d/FakeAlert.h | 18 | ||||
-rw-r--r-- | Low Battery Yup d/FakeAlert.m | 29 | ||||
-rw-r--r-- | Low Battery Yup d/Mouse.h | 4 | ||||
-rw-r--r-- | Low Battery Yup d/Mouse.m | 23 | ||||
-rw-r--r-- | Low Battery Yup.d.xcodeproj/project.pbxproj | 6 |
5 files changed, 78 insertions, 2 deletions
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 <Foundation/Foundation.h> + +#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 <Foundation/Foundation.h> +#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 = "<group>"; }; D15D90B51DF0465E001700CD /* ShortcutView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ShortcutView.h; sourceTree = "<group>"; }; D15D90B61DF0465E001700CD /* ShortcutView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ShortcutView.m; sourceTree = "<group>"; }; + D183789F1DF280E6005C5676 /* FakeAlert.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FakeAlert.h; sourceTree = "<group>"; }; + D18378A01DF280E6005C5676 /* FakeAlert.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FakeAlert.m; sourceTree = "<group>"; }; D1871D051DE094AB00B8030D /* DDHotKeyTextField.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = DDHotKeyTextField.h; path = DDHotKey/DDHotKeyTextField.h; sourceTree = "<group>"; }; D1871D061DE094AB00B8030D /* DDHotKeyTextField.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = DDHotKeyTextField.m; path = DDHotKey/DDHotKeyTextField.m; sourceTree = "<group>"; }; 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; }; |