Skip to content

Commit

Permalink
Added Focus follows mouse option
Browse files Browse the repository at this point in the history
  • Loading branch information
fjolnir committed Oct 15, 2013
1 parent 1bbe7d6 commit a4b45ae
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 481 deletions.
2 changes: 2 additions & 0 deletions Afloat.h
Expand Up @@ -30,6 +30,8 @@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- (IBAction) makeMoreTransparent:(id) sender;
- (IBAction) makeLessTransparent:(id) sender;

- (IBAction) toggleFocusFollowsMouse:(id)sender;

- (IBAction) showAdjustEffectsPanel:(id) sender;

- (CGFloat) currentAlphaValueForWindow:(NSWindow*) w;
Expand Down
28 changes: 23 additions & 5 deletions Afloat.m
Expand Up @@ -141,6 +141,11 @@ - (void) install {

// Scripting support.
[self installScriptingSupport];

// Set up tracking rectangles
for(NSWindow *window in [NSApp windows]) {
[self beginTrackingWindow:window];
}
}

- (NSUInteger) indexForInstallingInMenu:(NSMenu*) m {
Expand Down Expand Up @@ -180,6 +185,8 @@ - (BOOL) validateMenuItem:(NSMenuItem*) item {
return c != nil;
} else if ([item action] == @selector(showWindowFileInFinder:))
return [[self currentWindow] representedURL]? [[[self currentWindow] representedURL] isFileURL] : NO;
else if ([item action] == @selector(toggleFocusFollowsMouse:))
[item setState:[[AfloatStorage globalValueForKey:@"FocusFollowsMouse"] boolValue] ? NSOnState : NSOffState];

return YES;
}
Expand Down Expand Up @@ -309,11 +316,6 @@ - (void) setAlphaValue:(CGFloat) f forWindow:(NSWindow*) window animated:(BOOL)

[AfloatStorage setSharedValue:[NSNumber numberWithFloat:f] window:window key:kAfloatLastAlphaValueKey];

if (f == 1.0)
[self endTrackingWindow:window];
else
[self beginTrackingWindow:window];

if (animate) {
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:0.3];
Expand Down Expand Up @@ -366,10 +368,18 @@ - (void) mouseEntered:(NSEvent*) e {
L0Log(@"%@", e);

[self animateFadeInForWindow:[e window]];

BOOL focusFollowsMouse = [[AfloatStorage globalValueForKey:@"FocusFollowsMouse"] boolValue];
BOOL commandDown = [NSEvent modifierFlags] & NSCommandKeyMask ? YES : NO;
if(((focusFollowsMouse && !commandDown) || (!focusFollowsMouse && commandDown)) && ![[e window] isKeyWindow]) {
[[e window] makeKeyAndOrderFront:nil];
[NSApp activateIgnoringOtherApps:YES];
}
}

- (void) windowDidBecomeMain:(NSNotification*) n {
[self animateFadeInForWindow:[n object]];
[self beginTrackingWindow:[n object]];
}

- (void) animateFadeInForWindow:(NSWindow*) w {
Expand All @@ -390,6 +400,7 @@ - (void) mouseExited:(NSEvent*) e {

- (void) windowDidResignMain:(NSNotification*) n {
[self animateFadeOutForWindow:[n object]];
[self beginTrackingWindow:[n object]];
}

- (void) animateFadeOutForWindow:(NSWindow*) w {
Expand All @@ -406,6 +417,13 @@ - (void) animateFadeOutForWindow:(NSWindow*) w {

}

- (IBAction) toggleFocusFollowsMouse:(id)sender {
BOOL enable = ![[AfloatStorage globalValueForKey:@"FocusFollowsMouse"] boolValue];

[AfloatStorage setGlobalValue:@(enable)
forKey:@"FocusFollowsMouse"];
}

- (IBAction) showAdjustEffectsPanel:(id) sender {
NSWindow* w = [self currentWindow];
if (!w) { NSBeep(); return; }
Expand Down
3 changes: 3 additions & 0 deletions AfloatStorage.h
Expand Up @@ -23,6 +23,9 @@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ (id) sharedValueForWindow:(NSWindow*) w key:(NSString*) k;
+ (void) setSharedValue:(id) v window:(NSWindow*) w key:(NSString*) k;

+ (id) globalValueForKey:(NSString *) k;
+ (void) setGlobalValue:(id) v forKey:(NSString *) k;

- (id) valueForWindow:(NSWindow*) w key:(NSString*) k;
- (void) setValue:(id) v forWindow:(NSWindow*) w key:(NSString*) k;

Expand Down
33 changes: 30 additions & 3 deletions AfloatStorage.m
Expand Up @@ -101,9 +101,6 @@ - (BOOL) canSaveWindow:(NSWindow*) w category:(NSString**) cat key:(NSString**)
if (key) *key = [w frameAutosaveName];
return YES;
}



return NO;
}

Expand Down Expand Up @@ -155,6 +152,36 @@ + (void) setSharedValue:(id) v window:(NSWindow*) w key:(NSString*) k {
[[self sharedStorage] setValue:v forWindow:w key:k];
}

+ (void)_withGlobalStorage:(BOOL (^)(NSMutableDictionary *storage)) block
{
NSString *bundleIdentifier = [[NSBundle bundleForClass:self] bundleIdentifier];
NSString *storagePath = [[NSString stringWithFormat:@"~/Library/Preferences/%@.plist", bundleIdentifier] stringByExpandingTildeInPath];
NSDistributedLock *lock = [NSDistributedLock lockWithPath:[storagePath stringByAppendingPathExtension:@"lock"]];

NSMutableDictionary *storage = [NSMutableDictionary dictionaryWithContentsOfFile:storagePath]
?: [NSMutableDictionary new];
if(block(storage))
[storage writeToFile:storagePath atomically:YES];

[lock unlock];
}

+ (id) globalValueForKey:(NSString *) k {
__block id value;
[self _withGlobalStorage:^(NSMutableDictionary *storage) {
value = [storage objectForKey:k];
return NO;
}];
return value;
}

+ (void) setGlobalValue:(id) v forKey:(NSString *) k {
[self _withGlobalStorage:^(NSMutableDictionary *storage) {
[storage setObject:v forKey:k];
return YES;
}];
}

@synthesize delegate = _delegate;

@end

0 comments on commit a4b45ae

Please sign in to comment.