Skip to content

Commit

Permalink
Detect the max refresh instead of choosing
Browse files Browse the repository at this point in the history
  • Loading branch information
cschep committed Apr 13, 2024
1 parent 59e7409 commit 1e71f1a
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 101 deletions.
6 changes: 1 addition & 5 deletions easy-move-resize/EMRAppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ static const double kResizeFilterInterval = 0.04;
@property (weak) IBOutlet NSMenuItem *disabledAppsMenu;
@property (weak) IBOutlet NSMenuItem *lastAppMenu;

@property (weak) IBOutlet NSMenuItem *oneFourtyFourHertz;
@property (weak) IBOutlet NSMenuItem *oneTwentyHertz;
@property (weak) IBOutlet NSMenuItem *oneHundredHertz;
@property (weak) IBOutlet NSMenuItem *sixtyHertz;

@property (nonatomic) BOOL sessionActive;
@property NSTimeInterval refreshInterval;

@end
56 changes: 14 additions & 42 deletions easy-move-resize/EMRAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ - (id) init {
if (self) {
NSUserDefaults *userDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"userPrefs"];
preferences = [[EMRPreferences alloc] initWithUserDefaults:userDefaults];

// Default to 60hz, but check each connected screen for a faster refresh and use the fastest one we find
self.refreshInterval = 0.0167;
for (NSScreen *screen in [NSScreen screens]) {
if (@available(macOS 12.0, *)) {
NSTimeInterval maxRefreshInterval = [screen maximumRefreshInterval];
if (maxRefreshInterval < self.refreshInterval) {
self.refreshInterval = maxRefreshInterval;
}
}
}
}
return self;
}
Expand All @@ -25,7 +36,7 @@ CGEventRef myCGEventCallback(CGEventTapProxy __unused proxy, CGEventType type, C
CGEventType resizeModifierUp = kCGEventRightMouseUp;
bool handled = NO;

double interval = [ourDelegate refreshInterval];
double refreshInterval = [ourDelegate refreshInterval];

if (![ourDelegate sessionActive]) {
return event;
Expand Down Expand Up @@ -140,7 +151,7 @@ CGEventRef myCGEventCallback(CGEventTapProxy __unused proxy, CGEventType type, C
CFTypeRef _position;

// actually applying the change is expensive, so only do it every kMoveFilterInterval seconds
if (CACurrentMediaTime() - [moveResize tracking] > interval) {
if (CACurrentMediaTime() - [moveResize tracking] > refreshInterval) {
_position = (CFTypeRef) (AXValueCreate(kAXValueCGPointType, (const void *) &thePoint));
AXUIElementSetAttributeValue(_clickedWindow, (__bridge CFStringRef) NSAccessibilityPositionAttribute, (CFTypeRef *) _position);
if (_position != NULL) CFRelease(_position);
Expand Down Expand Up @@ -238,7 +249,7 @@ CGEventRef myCGEventCallback(CGEventTapProxy __unused proxy, CGEventType type, C
[moveResize setWndSize:wndSize];

// actually applying the change is expensive, so only do it every kResizeFilterInterval events
if (CACurrentMediaTime() - [moveResize tracking] > interval) {
if (CACurrentMediaTime() - [moveResize tracking] > refreshInterval) {
// only make a call to update the position if we need to
if (resizeSection.xResizeDirection == left || resizeSection.yResizeDirection == bottom) {
CFTypeRef _position = (CFTypeRef)(AXValueCreate(kAXValueCGPointType, (const void *)&cTopLeft));
Expand Down Expand Up @@ -410,34 +421,6 @@ - (void)initMenuItems {
if ([flags containsObject:FN_KEY]) {
[_fnMenu setState:1];
}

[self updateRefreshRate];
}

- (void)updateRefreshRate {
[[self sixtyHertz] setState:0];
[[self oneHundredHertz] setState:0];
[[self oneTwentyHertz] setState:0];
[[self oneFourtyFourHertz] setState:0];

int refreshRate = [preferences getRefreshRate];
if (refreshRate == 0) {
refreshRate = 60;
[preferences setRefreshRate: 60];
}
if (refreshRate == 144) {
[[self oneFourtyFourHertz] setState: 1];
}
if (refreshRate == 120) {
[[self oneTwentyHertz] setState: 1];
}
if (refreshRate == 100) {
[[self oneHundredHertz] setState: 1];
}
if (refreshRate == 60) {
[[self sixtyHertz] setState: 1];
}

}

- (IBAction)modifierToggle:(id)sender {
Expand Down Expand Up @@ -509,13 +492,6 @@ - (IBAction)enableDisabledApp:(id)sender {
[self reconstructDisabledAppsSubmenu];
}

- (IBAction)toggleRefreshRate:(id)sender {
NSMenuItem *menu = (NSMenuItem*)sender;
int refreshRate = (int)[menu tag];
[preferences setRefreshRate:refreshRate];
[self updateRefreshRate];
}

- (int)modifierFlags {
return keyModifierFlags;
}
Expand All @@ -542,10 +518,6 @@ - (BOOL)resizeOnly {
return [preferences resizeOnly];
}

- (double)refreshInterval {
return [preferences refreshInterval];
}

- (void)setMenusEnabled:(BOOL)enabled {
[_altMenu setEnabled:enabled];
[_cmdMenu setEnabled:enabled];
Expand Down
10 changes: 0 additions & 10 deletions easy-move-resize/EMRPreferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#ifndef EMRPreferences_h
#define EMRPreferences_h

#define REFRESH_RATE @"RefreshRate"
#define SHOULD_BRING_WINDOW_TO_FRONT @"BringToFront"
#define SHOULD_MIDDLE_CLICK_RESIZE @"MiddleClickResize"
#define RESIZE_ONLY @"ResizeOnly"
Expand Down Expand Up @@ -43,15 +42,6 @@
// returns a dict of disabled apps
- (NSDictionary*)getDisabledApps;

// get current refresh rate
- (int)getRefreshRate;

// set refresh rate
- (void)setRefreshRate:(int)refreshRate;

// get current refresh rate
- (double)refreshInterval;

// add or remove an app from the disabled apps list
- (void)setDisabledForApp:(NSString*)bundleIdentifier withLocalizedName:(NSString*)localizedName disabled:(BOOL)disabled;

Expand Down
15 changes: 0 additions & 15 deletions easy-move-resize/EMRPreferences.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
@implementation EMRPreferences {
@private
NSUserDefaults *userDefaults;
double refreshInterval;
}

- (id)init {
Expand Down Expand Up @@ -100,7 +99,6 @@ - (void)setToDefaults {
[userDefaults setBool:NO forKey:SHOULD_MIDDLE_CLICK_RESIZE];
[userDefaults setBool:NO forKey:RESIZE_ONLY];
[userDefaults setObject:[NSDictionary dictionary] forKey:DISABLED_APPS_DEFAULTS_KEY];
[userDefaults setInteger:60 forKey:REFRESH_RATE];
}

- (NSMutableSet*)createSetFromFlagString:(NSString*)modifierFlagString {
Expand Down Expand Up @@ -166,17 +164,4 @@ - (void)setResizeOnly:(BOOL)resizeOnly {
[userDefaults setBool:resizeOnly forKey:RESIZE_ONLY];
}

- (int)getRefreshRate {
return (int)[userDefaults integerForKey:REFRESH_RATE];
}

- (void)setRefreshRate:(int)refreshRate {
[userDefaults setInteger:refreshRate forKey:REFRESH_RATE];
refreshInterval = (1.0 / refreshRate);
}

- (double)refreshInterval {
return refreshInterval;
}

@end
29 changes: 0 additions & 29 deletions easy-move-resize/en.lproj/MainMenu.xib
Original file line number Diff line number Diff line change
Expand Up @@ -720,31 +720,6 @@
<menu key="submenu" title="Re-enable for" id="yAl-5D-6Ba"/>
</menuItem>
<menuItem isSeparatorItem="YES" id="VPq-PN-NFG"/>
<menuItem title="144hz" tag="144" id="kWO-Tq-uKm">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="toggleRefreshRate:" target="494" id="ftY-I7-3v5"/>
</connections>
</menuItem>
<menuItem title="120hz" tag="120" id="3OS-Kn-Ydz">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="toggleRefreshRate:" target="494" id="xx6-K8-a3K"/>
</connections>
</menuItem>
<menuItem title="100hz" tag="100" id="N4d-Tv-TSa">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="toggleRefreshRate:" target="494" id="fhy-q2-m5F"/>
</connections>
</menuItem>
<menuItem title="60hz" tag="60" id="YgX-I2-TPy">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="toggleRefreshRate:" target="494" id="prR-1l-5w9"/>
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" id="AU4-37-Pfw"/>
<menuItem title="Reset to Defaults" id="emi-it-fz4">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
Expand Down Expand Up @@ -772,12 +747,8 @@
<outlet property="fnMenu" destination="3Up-j9-Hdf" id="ans-jZ-8mN"/>
<outlet property="lastAppMenu" destination="ZKn-HI-Yq4" id="aO7-eG-bLr"/>
<outlet property="middleClickResizeMenu" destination="S2R-bI-Azo" id="c8V-7R-1QY"/>
<outlet property="oneFourtyFourHertz" destination="kWO-Tq-uKm" id="NpD-do-SLj"/>
<outlet property="oneHundredHertz" destination="N4d-Tv-TSa" id="Eci-5r-0YB"/>
<outlet property="oneTwentyHertz" destination="3OS-Kn-Ydz" id="f5p-ua-UeR"/>
<outlet property="resizeOnlyMenu" destination="ErU-VJ-PWr" id="eXx-DE-rHT"/>
<outlet property="shiftMenu" destination="SZL-bI-dng" id="YZk-pM-c3k"/>
<outlet property="sixtyHertz" destination="YgX-I2-TPy" id="dSN-wt-Qwr"/>
<outlet property="statusMenu" destination="obP-gH-pam" id="YfI-Jt-Lpf"/>
</connections>
</customObject>
Expand Down

0 comments on commit 1e71f1a

Please sign in to comment.