Skip to content

Automatically detect refresh rate #102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1530"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES"
buildArchitectures = "Automatic">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "6ED47B60183BF3E800859244"
BuildableName = "Easy Move+Resize.app"
BlueprintName = "easy-move-resize"
ReferencedContainer = "container:easy-move-resize.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
shouldAutocreateTestPlan = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "65CF01611F229C34002259F2"
BuildableName = "easy-move-resizeTests.xctest"
BlueprintName = "easy-move-resizeTests"
ReferencedContainer = "container:easy-move-resize.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "6ED47B60183BF3E800859244"
BuildableName = "Easy Move+Resize.app"
BlueprintName = "easy-move-resize"
ReferencedContainer = "container:easy-move-resize.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "6ED47B60183BF3E800859244"
BuildableName = "Easy Move+Resize.app"
BlueprintName = "easy-move-resize"
ReferencedContainer = "container:easy-move-resize.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
2 changes: 2 additions & 0 deletions easy-move-resize/EMRAppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ static const double kResizeFilterInterval = 0.04;
@property (weak) IBOutlet NSMenuItem *resizeOnlyMenu;
@property (weak) IBOutlet NSMenuItem *disabledAppsMenu;
@property (weak) IBOutlet NSMenuItem *lastAppMenu;

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

@end
47 changes: 32 additions & 15 deletions easy-move-resize/EMRAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,22 @@ - (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;
}

CGEventRef myCGEventCallback(CGEventTapProxy __unused proxy, CGEventType type, CGEventRef event, void *refcon) {

EMRAppDelegate *ourDelegate = (__bridge EMRAppDelegate*)refcon;
int keyModifierFlags = [ourDelegate modifierFlags];
bool shouldMiddleClickResize = [ourDelegate shouldMiddleClickResize];
Expand All @@ -26,6 +36,8 @@ CGEventRef myCGEventCallback(CGEventTapProxy __unused proxy, CGEventType type, C
CGEventType resizeModifierUp = kCGEventRightMouseUp;
bool handled = NO;

double refreshInterval = [ourDelegate refreshInterval];

if (![ourDelegate sessionActive]) {
return event;
}
Expand All @@ -35,7 +47,7 @@ CGEventRef myCGEventCallback(CGEventTapProxy __unused proxy, CGEventType type, C
return event;
}

if (shouldMiddleClickResize){
if (shouldMiddleClickResize) {
resizeModifierDown = kCGEventOtherMouseDown;
resizeModifierDragged = kCGEventOtherMouseDragged;
resizeModifierUp = kCGEventOtherMouseUp;
Expand Down Expand Up @@ -90,7 +102,7 @@ CGEventRef myCGEventCallback(CGEventTapProxy __unused proxy, CGEventType type, C

pid_t PID;
NSRunningApplication* app;
if(!AXUIElementGetPid(_clickedWindow, &PID)) {
if (!AXUIElementGetPid(_clickedWindow, &PID)) {
app = [NSRunningApplication runningApplicationWithProcessIdentifier:PID];
if ([[ourDelegate getDisabledApps] objectForKey:[app bundleIdentifier]] != nil) {
[moveResize setTracking:0];
Expand All @@ -99,7 +111,7 @@ CGEventRef myCGEventCallback(CGEventTapProxy __unused proxy, CGEventType type, C
[ourDelegate setMostRecentApp:app];
}

if([ourDelegate shouldBringWindowToFront]){
if ([ourDelegate shouldBringWindowToFront]) {
if (app != nil) {
[app activateWithOptions:NSApplicationActivateIgnoringOtherApps];
}
Expand Down Expand Up @@ -139,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] > kMoveFilterInterval) {
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 @@ -237,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] > kResizeFilterInterval) {
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 @@ -350,7 +362,7 @@ - (void)becameInactive:(NSNotification*) notification {
_sessionActive = false;
}

-(void)awakeFromNib{
- (void)awakeFromNib{
NSImage *icon = [NSImage imageNamed:@"MenuIcon"];
statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
[statusItem setMenu:statusMenu];
Expand Down Expand Up @@ -383,13 +395,13 @@ - (void)initMenuItems {
bool shouldMiddleClickResize = [preferences shouldMiddleClickResize];
bool resizeOnly = [preferences resizeOnly];

if(shouldBringWindowToFront){
if (shouldBringWindowToFront) {
[_bringWindowFrontMenu setState:1];
}
if(shouldMiddleClickResize){
if (shouldMiddleClickResize) {
[_middleClickResizeMenu setState:1];
}
if(resizeOnly){
if (resizeOnly) {
[_resizeOnlyMenu setState:1];
}

Expand Down Expand Up @@ -483,21 +495,26 @@ - (IBAction)enableDisabledApp:(id)sender {
- (int)modifierFlags {
return keyModifierFlags;
}
- (void) setMostRecentApp:(NSRunningApplication*)app {

- (void)setMostRecentApp:(NSRunningApplication*)app {
lastApp = app;
[_lastAppMenu setTitle:[NSString stringWithFormat:@"Disable for %@", [app localizedName]]];
[_lastAppMenu setEnabled:YES];
}
- (NSDictionary*) getDisabledApps {

- (NSDictionary*)getDisabledApps {
return [preferences getDisabledApps];
}
-(BOOL)shouldBringWindowToFront {

- (BOOL)shouldBringWindowToFront {
return [preferences shouldBringWindowToFront];
}
-(BOOL)shouldMiddleClickResize {

- (BOOL)shouldMiddleClickResize {
return [preferences shouldMiddleClickResize];
}
-(BOOL)resizeOnly {

- (BOOL)resizeOnly {
return [preferences resizeOnly];
}

Expand Down
2 changes: 1 addition & 1 deletion easy-move-resize/EMRMoveResize.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ - (void)setWindow:(AXUIElementRef)window {
_window = window;
}

- (CFRunLoopSourceRef) runLoopSource {
- (CFRunLoopSourceRef)runLoopSource {
return _runLoopSource;
}

Expand Down
11 changes: 5 additions & 6 deletions easy-move-resize/EMRPreferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#define FN_KEY @"FN"

@interface EMRPreferences : NSObject {

}

@property (nonatomic) BOOL shouldBringWindowToFront;
Expand All @@ -32,19 +31,19 @@
- (id)initWithUserDefaults:(NSUserDefaults *)defaults;

// Get the modifier flags from the standard preferences
- (int) modifierFlags;
- (int)modifierFlags;

// Set or unset the given modifier key in the preferences
- (void) setModifierKey:(NSString*)singleFlagString enabled:(BOOL)enabled;
- (void)setModifierKey:(NSString*)singleFlagString enabled:(BOOL)enabled;

// returns a set of the currently persisted key constants
- (NSSet*) getFlagStringSet;
- (NSSet*)getFlagStringSet;

// returns a dict of disabled apps
- (NSDictionary*) getDisabledApps;
- (NSDictionary*)getDisabledApps;

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

// reset preferences to the defaults
- (void)setToDefaults;
Expand Down
17 changes: 9 additions & 8 deletions easy-move-resize/EMRPreferences.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ - (void)setModifierFlagString:(NSString *)flagString {
[userDefaults setObject:flagString forKey:MODIFIER_FLAGS_DEFAULTS_KEY];
}


- (void)setModifierKey:(NSString *)singleFlagString enabled:(BOOL)enabled {
singleFlagString = [singleFlagString uppercaseString];
NSString *modifierFlagString = [userDefaults stringForKey:MODIFIER_FLAGS_DEFAULTS_KEY];
Expand Down Expand Up @@ -141,26 +140,28 @@ - (int)flagsFromFlagString:(NSString*)modifierFlagString {
return modifierFlags;
}

-(BOOL)shouldBringWindowToFront {
- (BOOL)shouldBringWindowToFront {
return [userDefaults boolForKey:SHOULD_BRING_WINDOW_TO_FRONT];
}
-(void)setShouldBringWindowToFront:(BOOL)bringToFront {

- (void)setShouldBringWindowToFront:(BOOL)bringToFront {
[userDefaults setBool:bringToFront forKey:SHOULD_BRING_WINDOW_TO_FRONT];
}

-(BOOL)shouldMiddleClickResize {
- (BOOL)shouldMiddleClickResize {
return [userDefaults boolForKey:SHOULD_MIDDLE_CLICK_RESIZE];
}
-(void)setShouldMiddleClickResize:(BOOL)middleClickResize {

- (void)setShouldMiddleClickResize:(BOOL)middleClickResize {
[userDefaults setBool:middleClickResize forKey:SHOULD_MIDDLE_CLICK_RESIZE];
}

-(BOOL)resizeOnly {
- (BOOL)resizeOnly {
return [userDefaults boolForKey:RESIZE_ONLY];
}
-(void)setResizeOnly:(BOOL)resizeOnly {

- (void)setResizeOnly:(BOOL)resizeOnly {
[userDefaults setBool:resizeOnly forKey:RESIZE_ONLY];
}

@end

4 changes: 2 additions & 2 deletions easy-move-resize/en.lproj/MainMenu.xib
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="21701" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="32700.99.1234" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
<dependencies>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="21701"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="22689"/>
</dependencies>
<objects>
<customObject id="-2" userLabel="File's Owner" customClass="NSApplication">
Expand Down