Skip to content

Commit

Permalink
Make app.launch() faster by introducing an option
Browse files Browse the repository at this point in the history
app.launch() now takes an extra options parameter. If `focus` is set to
true, then OC code will use NSWorkspaceLaunchDefault instead of
NSWorkspaceLaunchWithoutActivation to call launchApplicationAtURL. The
launched app will auto focus, and the speed will be noticeably faster.
  • Loading branch information
txchen committed May 1, 2018
1 parent f02761e commit c5324a9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Phoenix/PHApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#pragma mark - Apps

+ (instancetype) get:(NSString *)appName;
+ (instancetype) launch:(NSString *)appName;
+ (instancetype) launch:(NSString *)appName :(NSDictionary<NSString *, id> *)optionals;
+ (instancetype) focused;
+ (NSArray<PHApp *> *) all;

Expand Down
12 changes: 10 additions & 2 deletions Phoenix/PHApp.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ @interface PHApp ()
@implementation PHApp

static NSString * const PHAppForceOptionKey = @"force";
static NSString * const PHAppLaunchFocusOptionKey = @"focus";

#pragma mark - Initialising

Expand All @@ -41,7 +42,7 @@ + (instancetype) get:(NSString *)appName {
return nil;
}

+ (instancetype) launch:(NSString *)appName {
+ (instancetype) launch:(NSString *)appName :(NSDictionary<NSString *, id> *)optionals {

NSWorkspace *sharedWorkspace = [NSWorkspace sharedWorkspace];
NSString *appPath = [sharedWorkspace fullPathForApplication:appName];
Expand All @@ -51,9 +52,16 @@ + (instancetype) launch:(NSString *)appName {
return nil;
}

NSNumber *focusOption = optionals[PHAppLaunchFocusOptionKey];

NSWorkspaceLaunchOptions launchOption = NSWorkspaceLaunchWithoutActivation;
if (focusOption && focusOption.boolValue) {
launchOption = NSWorkspaceLaunchDefault;
}

NSError *error;
NSRunningApplication *app = [sharedWorkspace launchApplicationAtURL:[NSURL fileURLWithPath:appPath]
options:NSWorkspaceLaunchWithoutActivation
options:launchOption
configuration:@{}
error:&error];
if (error) {
Expand Down
8 changes: 6 additions & 2 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ Use the `App`-object to control apps. Beware that an app can get stale if you ke
class App implements Identifiable

static App get(String appName)
static App launch(String appName)
static App launch(String appName, Map<String, AnyObject> optionals)
static App focused()
static Array<App> all()

Expand All @@ -587,7 +587,7 @@ end
```

- `get(String appName)` returns the running app with the given name, returns `undefined` if the app is not currently running
- `launch(String appName)` launches to the background and returns the app with the given name, returns `undefined` if unsuccessful
- `launch(String appName, Map<String, AnyObject> optionals)` launches and returns the app with the given name, returns `undefined` if unsuccessful
- `focused()` returns the focused app
- `all()` returns all running apps
- `processIdentifier()` returns the process identifier (PID) for the app, returns `-1` if the app does not have a PID
Expand All @@ -613,6 +613,10 @@ end

- `force` (boolean): if set `true` force terminates the app

### Launch Optionals

- `focus` (boolean): if set `true`, the launched app will be automatically focused. You don't need to call app.focus() to bring it forward.

## 21. Window

Use the `Window`-object to control windows. Every screen (i.e. display) combines to form a large rectangle. Every window lives within this rectangle and their position can be altered by giving coordinates inside this rectangle. To position a window to a specific display, you need to calculate its position within the large rectangle. To figure out the coordinates for a given screen, use the functions in `Screen`. Beware that a window can get stale if you keep a reference to it and it is for instance closed while you do so.
Expand Down

0 comments on commit c5324a9

Please sign in to comment.