Skip to content
Snippets Groups Projects
Commit ddba7a9f authored by George Nachman's avatar George Nachman
Browse files

Restore previously active app when hiding hotkey window.

parent 00e408a0
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -36,7 +36,8 @@
#define OSX_TIGERORLATER (floor(NSAppKitVersionNumber) > 743)
#define OSX_LEOPARDORLATER (floor(NSAppKitVersionNumber) > 824)
 
BOOL IsLionOrLater();
BOOL IsLionOrLater(void);
BOOL IsLeopard(void);
 
#if MAC_OS_X_VERSION_MAX_ALLOWED < 1070
static const int NSWindowCollectionBehaviorFullScreenPrimary = 1 << 7;
Loading
Loading
Loading
Loading
@@ -56,6 +56,10 @@ BOOL IsLionOrLater(void);
int keyWindowIndexMemo_;
BOOL itermWasActiveWhenHotkeyOpened;
BOOL rollingIn_;
// For restoring previously active app when exiting hotkey window
NSNumber *previouslyActiveAppPID_;
id runningApplicationClass_;
}
 
+ (iTermController*)sharedInstance;
Loading
Loading
Loading
Loading
@@ -49,6 +49,7 @@
#import "iTerm/PseudoTerminal.h"
#import "iTermExpose.h"
#import "GTMCarbonEvent.h"
#import "iTerm.h"
 
#ifdef HOTKEY_WINDOW_VERBOSE_LOGGING
#define HKWLog NSLog
Loading
Loading
@@ -80,7 +81,7 @@ static NSInteger _compareEncodingByLocalizedName(id a, id b, void *unused)
return [sa caseInsensitiveCompare: sb];
}
 
BOOL IsLionOrLater() {
BOOL IsLionOrLater(void) {
unsigned major;
unsigned minor;
if ([iTermController getSystemVersionMajor:&major minor:&minor bugFix:nil]) {
Loading
Loading
@@ -90,6 +91,16 @@ BOOL IsLionOrLater() {
}
}
 
BOOL IsLeopard(void) {
unsigned major;
unsigned minor;
if ([iTermController getSystemVersionMajor:&major minor:&minor bugFix:nil]) {
return (major == 10 && minor == 5);
} else {
return NO;
}
}
@implementation iTermController
 
static iTermController* shared = nil;
Loading
Loading
@@ -130,32 +141,35 @@ static BOOL IsSnowLeopardOrLater() {
#endif
self = [super init];
 
UKCrashReporterCheckForCrash();
if (self) {
UKCrashReporterCheckForCrash();
 
// create the iTerm directory if it does not exist
NSFileManager *fileManager = [NSFileManager defaultManager];
runningApplicationClass_ = NSClassFromString(@"NSRunningApplication"); // 10.6
// create the iTerm directory if it does not exist
NSFileManager *fileManager = [NSFileManager defaultManager];
 
// create the "~/Library/Application Support" directory if it does not exist
if([fileManager fileExistsAtPath: [APPLICATION_SUPPORT_DIRECTORY stringByExpandingTildeInPath]] == NO)
[fileManager createDirectoryAtPath: [APPLICATION_SUPPORT_DIRECTORY stringByExpandingTildeInPath] attributes: nil];
// create the "~/Library/Application Support" directory if it does not exist
if([fileManager fileExistsAtPath: [APPLICATION_SUPPORT_DIRECTORY stringByExpandingTildeInPath]] == NO)
[fileManager createDirectoryAtPath: [APPLICATION_SUPPORT_DIRECTORY stringByExpandingTildeInPath] attributes: nil];
 
if([fileManager fileExistsAtPath: [SUPPORT_DIRECTORY stringByExpandingTildeInPath]] == NO)
[fileManager createDirectoryAtPath: [SUPPORT_DIRECTORY stringByExpandingTildeInPath] attributes: nil];
if([fileManager fileExistsAtPath: [SUPPORT_DIRECTORY stringByExpandingTildeInPath]] == NO)
[fileManager createDirectoryAtPath: [SUPPORT_DIRECTORY stringByExpandingTildeInPath] attributes: nil];
 
terminalWindows = [[NSMutableArray alloc] init];
keyWindowIndexMemo_ = -1;
terminalWindows = [[NSMutableArray alloc] init];
keyWindowIndexMemo_ = -1;
 
// Activate Growl
/*
* Need to add routine in iTerm prefs for Growl support and
* PLIST check here.
*/
gd = [iTermGrowlDelegate sharedInstance];
// Activate Growl
/*
* Need to add routine in iTerm prefs for Growl support and
* PLIST check here.
*/
gd = [iTermGrowlDelegate sharedInstance];
}
 
return (self);
}
 
- (void) dealloc
- (void)dealloc
{
#if DEBUG_ALLOC
NSLog(@"%s(%d):-[iTermController dealloc]",
Loading
Loading
@@ -169,8 +183,10 @@ static BOOL IsSnowLeopardOrLater() {
[terminalWindows release];
 
// Release the GrowlDelegate
if(gd)
if (gd) {
[gd release];
}
[previouslyActiveAppPID_ release];
 
[super dealloc];
}
Loading
Loading
@@ -970,6 +986,66 @@ static BOOL IsSnowLeopardOrLater() {
return nil;
}
 
#pragma mark hotkey window
- (void)storePreviouslyActiveApp
{
if (IsLeopard()) {
// Visor has a 10.5 path, but it is very hacky and apparently has a crash. 10.5 is moribund
// so I'm going to omit it.
return;
} else {
// 10.6+ path
NSDictionary *activeAppDict = [[NSWorkspace sharedWorkspace] activeApplication];
[previouslyActiveAppPID_ release];
previouslyActiveAppPID_ = nil;
if ([[activeAppDict objectForKey:@"NSApplicationBundleIdentifier"] compare:@"com.googlecode.iterm2"]) {
previouslyActiveAppPID_ = [[activeAppDict objectForKey:@"NSApplicationProcessIdentifier"] copy];
}
}
}
- (void)restorePreviouslyActiveApp
{
if (IsLeopard()) {
// See note in storePreviouslyActiveApp.
return;
} else {
// 10.6+ path
if (!previouslyActiveAppPID_) {
return;
}
id app;
// NSInvocation hackery because we need to build against the 10.5 sdk and call a
// 10.6 function.
// app = [runningApplicationClass_ runningApplicationWithProcessIdentifier:[previouslyActiveAppPID_ intValue]];
NSMethodSignature *sig = [runningApplicationClass_->isa instanceMethodSignatureForSelector:@selector(runningApplicationWithProcessIdentifier:)];
NSInvocation *inv = [NSInvocation invocationWithMethodSignature:sig];
[inv setTarget:runningApplicationClass_];
[inv setSelector:@selector(runningApplicationWithProcessIdentifier:)];
int appId = [previouslyActiveAppPID_ intValue];
[inv setArgument:&appId atIndex:2];
[inv invoke];
[inv getReturnValue:&app];
if (app) {
//[app activateWithOptions:0];
sig = [[app class] instanceMethodSignatureForSelector:@selector(activateWithOptions:)];
assert(sig);
inv = [NSInvocation invocationWithMethodSignature:sig];
[inv setTarget:app];
[inv setSelector:@selector(activateWithOptions:)];
int opts = 0;
[inv setArgument:&opts atIndex:2];
[inv invoke];
}
[previouslyActiveAppPID_ release];
previouslyActiveAppPID_ = nil;
}
}
static PseudoTerminal* GetHotkeyWindow()
{
iTermController* cont = [iTermController sharedInstance];
Loading
Loading
@@ -1228,6 +1304,7 @@ static void RollOutHotkeyTerm(PseudoTerminal* term, BOOL itermWasActiveWhenHotke
 
- (void)showHotKeyWindow
{
[self storePreviouslyActiveApp];
itermWasActiveWhenHotkeyOpened = [NSApp isActive];
PseudoTerminal* hotkeyTerm = GetHotkeyWindow();
if (hotkeyTerm) {
Loading
Loading
@@ -1325,6 +1402,7 @@ static void RollOutHotkeyTerm(PseudoTerminal* term, BOOL itermWasActiveWhenHotke
- (void)hideHotKeyWindow:(PseudoTerminal*)hotkeyTerm
{
HKWLog(@"Hide visor.");
[self restorePreviouslyActiveApp];
RollOutHotkeyTerm(hotkeyTerm, itermWasActiveWhenHotkeyOpened);
}
 
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment