Skip to content

Commit dc9329f

Browse files
committed
Add BrowserWindow.showDefinitionForSelection()
This API shows the system-provided pop-up dictionary. Some Mac apps including Chrome have "Look Up in in Dictionary" context menu item. This API can be used to implement it.
1 parent 47d7a35 commit dc9329f

File tree

7 files changed

+38
-0
lines changed

7 files changed

+38
-0
lines changed

atom/browser/api/atom_api_window.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,12 @@ bool Window::IsMenuBarVisible() {
422422
return window_->IsMenuBarVisible();
423423
}
424424

425+
#if defined(OS_MACOSX)
426+
void Window::ShowDefinitionForSelection() {
427+
window_->ShowDefinitionForSelection();
428+
}
429+
#endif
430+
425431
mate::Handle<WebContents> Window::GetWebContents(v8::Isolate* isolate) const {
426432
return WebContents::CreateFrom(isolate, window_->GetWebContents());
427433
}
@@ -491,6 +497,10 @@ void Window::BuildPrototype(v8::Isolate* isolate,
491497
.SetMethod("isMenuBarAutoHide", &Window::IsMenuBarAutoHide)
492498
.SetMethod("setMenuBarVisibility", &Window::SetMenuBarVisibility)
493499
.SetMethod("isMenuBarVisible", &Window::IsMenuBarVisible)
500+
#if defined(OS_MACOSX)
501+
.SetMethod(
502+
"showDefinitionForSelection", &Window::ShowDefinitionForSelection)
503+
#endif
494504
.SetMethod("_getWebContents", &Window::GetWebContents)
495505
.SetMethod("_getDevToolsWebContents", &Window::GetDevToolsWebContents);
496506
}

atom/browser/api/atom_api_window.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ class Window : public mate::EventEmitter,
123123
void SetMenuBarVisibility(bool visible);
124124
bool IsMenuBarVisible();
125125

126+
#if defined(OS_MACOSX)
127+
void ShowDefinitionForSelection();
128+
#endif
129+
126130
// APIs for WebContents.
127131
mate::Handle<WebContents> GetWebContents(v8::Isolate* isolate) const;
128132
mate::Handle<WebContents> GetDevToolsWebContents(v8::Isolate* isolate) const;

atom/browser/native_window.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,10 @@ void NativeWindow::Print(bool silent, bool print_background) {
247247
PrintNow(silent, print_background);
248248
}
249249

250+
void NativeWindow::ShowDefinitionForSelection() {
251+
NOTIMPLEMENTED();
252+
}
253+
250254
void NativeWindow::SetAutoHideMenuBar(bool auto_hide) {
251255
}
252256

atom/browser/native_window.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate,
161161
// Print current page.
162162
virtual void Print(bool silent, bool print_background);
163163

164+
// Show popup dictionary.
165+
virtual void ShowDefinitionForSelection();
166+
164167
// Toggle the menu bar.
165168
virtual void SetAutoHideMenuBar(bool auto_hide);
166169
virtual bool IsMenuBarAutoHide();

atom/browser/native_window_mac.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
#import <Cocoa/Cocoa.h>
99

10+
#include <string>
11+
#include <vector>
12+
1013
#include "base/mac/scoped_nsobject.h"
1114
#include "base/memory/scoped_ptr.h"
1215
#include "atom/browser/native_window.h"
@@ -68,6 +71,7 @@ class NativeWindowMac : public NativeWindow {
6871
virtual bool HasModalDialog() OVERRIDE;
6972
virtual gfx::NativeWindow GetNativeWindow() OVERRIDE;
7073
virtual void SetProgressBar(double progress) OVERRIDE;
74+
virtual void ShowDefinitionForSelection() OVERRIDE;
7175

7276
// Returns true if |point| in local Cocoa coordinate system falls within
7377
// the draggable region.

atom/browser/native_window_mac.mm

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,14 @@ - (void)drawRect:(NSRect)dirtyRect {
642642
[dock_tile display];
643643
}
644644

645+
void NativeWindowMac::ShowDefinitionForSelection() {
646+
content::WebContents* web_contents = GetWebContents();
647+
content::RenderWidgetHostView* rwhv = web_contents->GetRenderWidgetHostView();
648+
if (!rwhv)
649+
return;
650+
rwhv->ShowDefinitionForSelection();
651+
}
652+
645653
bool NativeWindowMac::IsWithinDraggableRegion(NSPoint point) const {
646654
if (!draggable_region_)
647655
return false;

docs/api/browser-window.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,11 @@ On Linux platform, only supports Unity desktop environment, you need to specify
530530
the `*.desktop` file name to `desktopName` field in `package.json`. By default,
531531
it will assume `app.getName().desktop`.
532532

533+
### BrowserWindow.showDefinitionForSelection()
534+
535+
Show pop-up dictionary that searches the selected word on the page.
536+
This API is available only on Mac OS.
537+
533538
### BrowserWindow.setAutoHideMenuBar(hide)
534539

535540
* `hide` Boolean

0 commit comments

Comments
 (0)