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

Make app.launch() faster by introducing an option #212

Merged
merged 1 commit into from
May 13, 2018
Merged
Show file tree
Hide file tree
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
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The description for launch(String appName) above should also be updated (sorry, GitHub doesn't let me comment there since it wasn't changed).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated.


- `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