Skip to content

Commit

Permalink
Improve blurring mechanism by delaying another run
Browse files Browse the repository at this point in the history
  • Loading branch information
heyvito committed Oct 16, 2017
1 parent cc73239 commit 830f8f0
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 2 deletions.
8 changes: 8 additions & 0 deletions Bedim.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
DABEF7BB1F93F3930059A631 /* BDAboutViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = DABEF7B91F93F3930059A631 /* BDAboutViewController.xib */; };
DABEF7BD1F93F4980059A631 /* Licenses in Resources */ = {isa = PBXBuildFile; fileRef = DABEF7BC1F93F4980059A631 /* Licenses */; };
DABEF7C01F93FE1A0059A631 /* BDFileSystem.m in Sources */ = {isa = PBXBuildFile; fileRef = DABEF7BF1F93FE1A0059A631 /* BDFileSystem.m */; };
DAE460901F94CA8E0029D8A3 /* NSObject+BDDebouncer.m in Sources */ = {isa = PBXBuildFile; fileRef = DAE4608F1F94CA8E0029D8A3 /* NSObject+BDDebouncer.m */; };
DAEBDC991F9427720017DF80 /* LetsMove.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DAEBDC981F9427720017DF80 /* LetsMove.framework */; };
DAEBDC9A1F9427720017DF80 /* LetsMove.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = DAEBDC981F9427720017DF80 /* LetsMove.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
DAF1EB921F9427CC00063D79 /* Security.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DAF1EB911F9427CC00063D79 /* Security.framework */; };
Expand Down Expand Up @@ -101,6 +102,9 @@
DABEF7BC1F93F4980059A631 /* Licenses */ = {isa = PBXFileReference; lastKnownFileType = text; path = Licenses; sourceTree = "<group>"; };
DABEF7BE1F93FE1A0059A631 /* BDFileSystem.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BDFileSystem.h; sourceTree = "<group>"; };
DABEF7BF1F93FE1A0059A631 /* BDFileSystem.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = BDFileSystem.m; sourceTree = "<group>"; };
DAE4608A1F94C7260029D8A3 /* DebugLog.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DebugLog.h; sourceTree = "<group>"; };
DAE4608E1F94CA8E0029D8A3 /* NSObject+BDDebouncer.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "NSObject+BDDebouncer.h"; sourceTree = "<group>"; };
DAE4608F1F94CA8E0029D8A3 /* NSObject+BDDebouncer.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "NSObject+BDDebouncer.m"; sourceTree = "<group>"; };
DAEBDC981F9427720017DF80 /* LetsMove.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = LetsMove.framework; path = ../Carthage/Build/Mac/LetsMove.framework; sourceTree = "<group>"; };
DAF1EB911F9427CC00063D79 /* Security.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Security.framework; path = System/Library/Frameworks/Security.framework; sourceTree = SDKROOT; };
DAF1EB961F942AE900063D79 /* ServiceManagement.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = ServiceManagement.framework; path = System/Library/Frameworks/ServiceManagement.framework; sourceTree = SDKROOT; };
Expand Down Expand Up @@ -173,6 +177,8 @@
DA1448A01F9306F5008388FA /* BDImageProcessor.m */,
DA1448A21F9307D4008388FA /* BDUtility.h */,
DA1448A31F9307D4008388FA /* BDUtility.m */,
DAE4608E1F94CA8E0029D8A3 /* NSObject+BDDebouncer.h */,
DAE4608F1F94CA8E0029D8A3 /* NSObject+BDDebouncer.m */,
);
path = Utility;
sourceTree = "<group>";
Expand Down Expand Up @@ -217,6 +223,7 @@
DABA2B1F1F92AE720029EC09 /* AppDelegate.m */,
DA1448961F9301FF008388FA /* BDEventRouter.h */,
DA1448971F9301FF008388FA /* BDEventRouter.m */,
DAE4608A1F94C7260029D8A3 /* DebugLog.h */,
);
path = Bedim;
sourceTree = "<group>";
Expand Down Expand Up @@ -335,6 +342,7 @@
DABA2B371F92CE0D0029EC09 /* BDRunningApplication.m in Sources */,
DABEF7C01F93FE1A0059A631 /* BDFileSystem.m in Sources */,
DA1448A11F9306F5008388FA /* BDImageProcessor.m in Sources */,
DAE460901F94CA8E0029D8A3 /* NSObject+BDDebouncer.m in Sources */,
DA1448981F9301FF008388FA /* BDEventRouter.m in Sources */,
DABA2B341F92B41E0029EC09 /* BDWorkspace.m in Sources */,
DABA2B3A1F92CEA70029EC09 /* BDAXUIElement.m in Sources */,
Expand Down
7 changes: 5 additions & 2 deletions Bedim/AX/BDAXObserver.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
// Copyright © 2017 Victor Gama. All rights reserved.
//

#import <Cocoa/Cocoa.h>
#import "BDAXObserver.h"
#import "BDWindow.h"
#import <Cocoa/Cocoa.h>
#import "DebugLog.h"


static NSString * const BDAXObserverWindowKey = @"PHAXObserverWindow";

Expand All @@ -22,8 +24,9 @@ @implementation BDAXObserver

#pragma mark - AXObserverCallback

static void BDAXObserverCallback(__unused AXObserverRef observer, __unused AXUIElementRef element, __unused CFStringRef notification, __unused void *data) {
static void BDAXObserverCallback(__unused AXObserverRef observer, AXUIElementRef element, CFStringRef notification, __unused void *data) {
@autoreleasepool {
Debug(@"[BDAXObserverCallback] Dispatching notification based on %@ (%@)", (__bridge NSString *)notification, (__bridge id)element);
[[NSNotificationCenter defaultCenter] postNotificationName:@"BDAX_NOTIFICATION"
object:nil
userInfo:nil];
Expand Down
2 changes: 2 additions & 0 deletions Bedim/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ - (void)applicationWillTerminate:(NSNotification *)aNotification {
}

- (void)applicationWillFinishLaunching:(NSNotification *)notification {
#ifndef DEBUG
PFMoveToApplicationsFolderIfNecessary();
#endif
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
Expand Down
2 changes: 2 additions & 0 deletions Bedim/BDEventRouter.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import <Cocoa/Cocoa.h>
#import "BDEventRouter.h"
#import "BDAXObserver.h"
#import "DebugLog.h"


@implementation BDEventRouter {
Expand Down Expand Up @@ -75,6 +76,7 @@ - (void)observeValueForKeyPath:(NSString *)keyPath


- (void)workspaceNotification:(NSNotification *)aNotification {
Debug(@"[BDEventRouter] Dispatching notification based on %@", aNotification.name);
if(self.delegate) { [self.delegate bdEventReceived]; }
}

Expand Down
17 changes: 17 additions & 0 deletions Bedim/DebugLog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// DebugLog.h
// Bedim
//
// Created by Victor Gama on 16/10/2017.
// Copyright © 2017 Victor Gama. All rights reserved.
//

#ifndef DebugLog_h
#define DebugLog_h

#ifdef DEBUG
#define Debug(...) NSLog(__VA_ARGS__);
#else
#define Debug(...) while(false){};
#endif
#endif /* DebugLog_h */
13 changes: 13 additions & 0 deletions Bedim/Integration/BDWorkspace.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#import "BDWindow.h"
#import "BDRunningApplication.h"
#import "NSScreen+BDUtilities.h"
#import "NSObject+BDDebouncer.h"
#import "BDImageProcessor.h"
#import "BDStorage.h"
#import "BDFileSystem.h"
Expand Down Expand Up @@ -48,6 +49,18 @@ - (void)removeBlurEffect {
}

- (void)applyBlurEffect {
@synchronized(self) {
[self debounce:@selector(handleBlurRequest) withObject:nil delaying:0.5];
/* FIXME: This is not the ideal approach to fix it, but it potentially
addresses the issue where macOS may emit events on "Hey,
there's an application launching", but its window is not yet
visible. We may be missing an AX event here, but hey.
*/
[self repeat:@selector(handleBlurRequest) after:1];
}
}

- (void)handleBlurRequest {
if(suspended) return;
NSArray<NSScreen *> *screens = [NSScreen screens];
NSSet<NSScreen *> *screensToBlur = [self screensToBlur];
Expand Down
16 changes: 16 additions & 0 deletions Bedim/Utility/NSObject+BDDebouncer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// NSObject+BDDebouncer.h
// Bedim
//
// Created by Victor Gama on 16/10/2017.
// Copyright © 2017 Victor Gama. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface NSObject (BDDebouncer)

- (void)debounce:(nonnull SEL)action withObject:(nullable id)obj delaying:(NSTimeInterval)interval;
- (void)repeat:(nonnull SEL)action after:(NSTimeInterval)interval;

@end
42 changes: 42 additions & 0 deletions Bedim/Utility/NSObject+BDDebouncer.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// NSObject+BDDebouncer.m
// Bedim
//
// Created by Victor Gama on 16/10/2017.
// Copyright © 2017 Victor Gama. All rights reserved.
//

#import "NSObject+BDDebouncer.h"
#import "DebugLog.h"

@implementation NSObject (BDDebouncer)

- (void)debounce:(SEL)action withObject:(id)obj delaying:(NSTimeInterval)interval {
__weak typeof(self) weakSelf = self;
[NSObject cancelPreviousPerformRequestsWithTarget:weakSelf selector:action object:obj];
if(![self respondsToSelector:action]) {
Debug(@"[BDDebouncer] Cancelling debounce: call since %@ does not respond to %@", self, NSStringFromSelector(action));
return;
}
[weakSelf performSelector:action withObject:obj afterDelay:interval];
}

- (void)repeat:(SEL)action after:(NSTimeInterval)interval {
[self debounce:@selector(redo:) withObject:NSStringFromSelector(action) delaying:interval];
}

- (void)redo:(NSString *)rawAction {
SEL action = NSSelectorFromString(rawAction);
Debug(@"[BDDebouncer] Performing redo:(%@) on %@", NSStringFromSelector(action), self);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
if(![self respondsToSelector:action]) {
NSLog(@"[BDDebouncer] Cancelling redo: call since %@ does not respond to %@", self, NSStringFromSelector(action));
return;
}
IMP imp = [self methodForSelector:action];
void (*func)(id, SEL) = (void *)imp;
func(self, action);
});
}

@end

0 comments on commit 830f8f0

Please sign in to comment.