Skip to content

Commit

Permalink
Merge pull request #102 from murlokswarm/mac-share
Browse files Browse the repository at this point in the history
Share implementation on mac
  • Loading branch information
maxence-charriere committed Feb 4, 2018
2 parents 9c7c604 + 4ccaed6 commit 434e5f6
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 8 deletions.
2 changes: 1 addition & 1 deletion driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ type DriverWithShare interface {
Driver

// Share shares the value.
Share(v interface{})
Share(v interface{}) error
}

// DriverWithFilePanels is the interface that describes a driver able to open a
Expand Down
24 changes: 22 additions & 2 deletions drivers/mac/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,28 @@ func (d *Driver) Dock() app.DockTile {
}

// Share satisfies the app.DriverWithShare interface.
func (d *Driver) Share(v interface{}) {
panic("not implemented")
func (d *Driver) Share(v interface{}) error {
share := struct {
Value string `json:"value"`
Type string `json:"type"`
}{
Value: fmt.Sprint(v),
}

switch v.(type) {
case url.URL, *url.URL:
share.Type = "url"

default:
share.Type = "string"
}

_, err := d.macos.RequestWithAsyncResponse(
"/driver/share",
bridge.NewPayload(share),
)
return err

}

// NewFilePanel satisfies the app.DriverWithFilePanels interface.
Expand Down
1 change: 1 addition & 0 deletions drivers/mac/driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
- (bridge_result)setDockIcon:(NSURLComponents *)url payload:(NSString *)payload;
- (bridge_result)setDockBadge:(NSURLComponents *)url
payload:(NSString *)payload;
- (bridge_result)share:(NSURLComponents *)url payload:(NSString *)payload;
@end

#endif /* driver_h */
46 changes: 46 additions & 0 deletions drivers/mac/driver.m
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ - (instancetype)init {
handler:^(NSURLComponents *url, NSString *payload) {
return [self setDockBadge:url payload:payload];
}];
[self.objc handle:@"/driver/share"
handler:^(NSURLComponents *url, NSString *payload) {
return [self share:url payload:payload];
}];

// Window handlers.
[self.objc handle:@"/window/new"
Expand Down Expand Up @@ -251,6 +255,48 @@ - (bridge_result)setDockBadge:(NSURLComponents *)url
return make_bridge_result(nil, nil);
}

- (bridge_result)share:(NSURLComponents *)url payload:(NSString *)payload {
NSString *returnID = [url queryValue:@"return-id"];

NSDictionary *share = [JSONDecoder decodeObject:payload];
NSString *value = share[@"value"];
NSString *type = share[@"type"];

dispatch_async(dispatch_get_main_queue(), ^{
NSString *err = nil;

@try {
NSWindow *rawWindow = NSApp.keyWindow;
if (rawWindow == nil) {
@throw
[NSException exceptionWithName:@"NoKeyWindowExeption"
reason:@"no window to host the share menu"
userInfo:nil];
}

id valueToShare = value;
if ([type isEqual:@"url"]) {
valueToShare = [NSURL URLWithString:value];
}

Window *win = (Window *)rawWindow.windowController;
NSPoint pos = [win.window mouseLocationOutsideOfEventStream];
pos = [win.webview convertPoint:pos fromView:rawWindow.contentView];
NSRect rect = NSMakeRect(pos.x, pos.y, 1, 1);

NSSharingServicePicker *picker =
[[NSSharingServicePicker alloc] initWithItems:@[ valueToShare ]];
[picker showRelativeToRect:rect
ofView:win.webview
preferredEdge:NSMinYEdge];
} @catch (NSException *exception) {
err = exception.reason;
}
[self.objc asyncReturn:returnID result:make_bridge_result(nil, err)];
});
return make_bridge_result(nil, nil);
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
[self.golang request:@"/driver/run" payload:nil];
}
Expand Down
3 changes: 2 additions & 1 deletion drivers/test/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ func (d *Driver) Dock() app.DockTile {
}

// Share satisfies the app.DriverWithShare interface.
func (d *Driver) Share(v interface{}) {
func (d *Driver) Share(v interface{}) error {
return nil
}

// NewFilePanel satisfies the app.DriverWithFilePanels interface.
Expand Down
25 changes: 21 additions & 4 deletions examples/test/webview.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ func (c *Webview) Render() string {
<button onclick="OnChangeNumber">Render: change number</button>
<div>{{.Number}}</div>
</li>
<li><button onclick="OnShare">Share</button></li>
<li><button onclick="OnShareURL">Share URL</button></li>
<li>
<button {{if not .CanPrevious}}disabled{{end}} onclick="OnPrevious">Previous</button>
<button onclick="OnReload">Reload</button>
<button {{if not .CanNext}}disabled{{end}} onclick="OnNext">Next</button>
<button {{if not .CanPrevious}}disabled{{end}} onclick="OnPrevious">Previous</button>
<button onclick="OnReload">Reload</button>
<button {{if not .CanNext}}disabled{{end}} onclick="OnNext">Next</button>
</li>
</ul>
Expand Down Expand Up @@ -134,13 +136,28 @@ func (c *Webview) OnChangeSquareColor() {
app.Render(c)
}

// OnChangeNumber is the function to be called when the change number button is
// OnChangeNumber is the function that is called when the change number button is
// clicked.
func (c *Webview) OnChangeNumber() {
c.Number = rand.Int()
app.Render(c)
}

// OnShare is the function that is called when the Share button is clicked.
func (c *Webview) OnShare() {
app.Share("Hello world")
}

// OnShareURL is the function that is called when the Share URL button is
// clicked.
func (c *Webview) OnShareURL() {
u, err := url.Parse("https://github.com/murlokswarm/app")
if err != nil {
app.DefaultLogger.Log(err)
}
app.Share(u)
}

// OnPrevious is the function that is called when the previous button is
// clicked.
func (c *Webview) OnPrevious() {
Expand Down

0 comments on commit 434e5f6

Please sign in to comment.