Skip to content
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

Fixes #274 and #230 #276

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
40 changes: 28 additions & 12 deletions ShiftIt/SIWindowManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -127,22 +127,40 @@ - (void) dealloc {
[super dealloc];
}

// credits to https://gist.github.com/ljos/3040846
- (NSDictionary *) getRunningAppFocusedWindowInfo {
for (NSRunningApplication *rapp in [[NSWorkspace sharedWorkspace] runningApplications]) {
if ([rapp isActive]) {
NSArray *allWindowsInfoList = (NSArray *) CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements, kCGNullWindowID);

NSArray *windowInfoList = [allWindowsInfoList filter:^BOOL(NSDictionary *item) {
CFStringRef cfStringOwnerName = (CFStringRef)[item objectForKey:(id)kCGWindowOwnerName];
// https://stackoverflow.com/questions/17227348/nsstring-to-cfstringref-and-cfstringref-to-nsstring-in-arc/17256947#17256947
NSString *ownerName = (__bridge NSString *)cfStringOwnerName;

return [ownerName isEqualToString:[rapp localizedName]];
}];

// get the first one - the front most window
if ([windowInfoList count] > 0) {
return windowInfoList[0];
}
}
}

return nil;
}

- (BOOL) getFocusedWindow:(id<SIWindow> *)window error:(NSError **)error {
// get all windows order front to back
NSArray *allWindowsInfoList = (NSArray *) CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly + kCGWindowListExcludeDesktopElements,
kCGNullWindowID);
// filter only real windows - layer 0
NSArray *windowInfoList = [allWindowsInfoList filter:^BOOL(NSDictionary *item) {
return [[item objectForKey:(id)kCGWindowLayer] integerValue] == 0;
}];
NSDictionary *cgWindowInfo = [self getRunningAppFocusedWindowInfo];

// get the first one - the front most window
if ([windowInfoList count] == 0) {
if (cgWindowInfo == nil) {
*error = SICreateError(kWindowManagerFailureErrorCode, @"Unable to find front window");
return NO;
}
SIWindowInfo *frontWindowInfo = [SIWindowInfo windowInfoFromCGWindowInfoDictionary:[windowInfoList objectAtIndex:0]];

SIWindowInfo *frontWindowInfo = [SIWindowInfo windowInfoFromCGWindowInfoDictionary:cgWindowInfo];

// extract properties

FMTLogDebug(@"Found front window: %@", [frontWindowInfo description]);
Expand All @@ -165,8 +183,6 @@ - (BOOL) getFocusedWindow:(id<SIWindow> *)window error:(NSError **)error {
FMTLogDebug(@"Driver mapped window: %@", [w description]);
}

[allWindowsInfoList release];

*window = w;
[windows_ addObject:[*window retain]];

Expand Down