Skip to content

Commit 86e96ef

Browse files
committed
Mac: implement 'focus' property of Window to set initial focus
For nwjs#424
1 parent 576259c commit 86e96ef

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

src/browser/native_window_mac.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ class NativeWindowCocoa : public NativeWindow {
7070
bool enabled) OVERRIDE;
7171
virtual void SetToolbarUrlEntry(const std::string& url) OVERRIDE;
7272
virtual void SetToolbarIsLoading(bool loading) OVERRIDE;
73+
virtual void SetInitialFocus(bool accept_focus) OVERRIDE;
74+
virtual bool InitialFocus() OVERRIDE;
7375

7476
// Called to handle a mouse event.
7577
void HandleMouseEvent(NSEvent* event);
@@ -128,6 +130,9 @@ class NativeWindowCocoa : public NativeWindow {
128130
// used in custom drag to compute the window movement.
129131
NSPoint last_mouse_location_;
130132

133+
bool initial_focus_;
134+
bool first_show_;
135+
131136
DISALLOW_COPY_AND_ASSIGN(NativeWindowCocoa);
132137
};
133138

src/browser/native_window_mac.mm

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -287,10 +287,16 @@ - (NSRect)contentRectForFrameRect:(NSRect)frameRect {
287287
is_fullscreen_(false),
288288
is_kiosk_(false),
289289
attention_request_id_(0),
290-
use_system_drag_(true) {
290+
use_system_drag_(true),
291+
initial_focus_(false), // the initial value is different from other
292+
// platforms since osx will focus the first
293+
// window and we want to distinguish the first
294+
// window opening and Window.open case. See also #497
295+
first_show_(true) {
291296
int width, height;
292297
manifest->GetInteger(switches::kmWidth, &width);
293298
manifest->GetInteger(switches::kmHeight, &height);
299+
manifest->GetBoolean(switches::kmInitialFocus, &initial_focus_);
294300

295301
NSRect main_screen_rect = [[[NSScreen screens] objectAtIndex:0] frame];
296302
NSRect cocoa_bounds = NSMakeRect(
@@ -412,15 +418,22 @@ - (NSRect)contentRectForFrameRect:(NSRect)frameRect {
412418
content::RenderWidgetHostView* rwhv =
413419
shell_->web_contents()->GetRenderWidgetHostView();
414420

415-
// orderFrontRegardless causes us to become the first responder. The usual
416-
// Chrome assumption is that becoming the first responder = you have focus
417-
// so we use this trick to refuse to become first responder during orderFrontRegardless
418-
419-
if (rwhv)
420-
rwhv->SetTakesFocusOnlyOnMouseDown(true);
421-
[window() orderFrontRegardless];
422-
if (rwhv)
423-
rwhv->SetTakesFocusOnlyOnMouseDown(false);
421+
if (first_show_ && initial_focus_) {
422+
[window() makeKeyAndOrderFront:nil];
423+
// FIXME: the new window through Window.open failed
424+
// the focus-on-load test
425+
} else {
426+
// orderFrontRegardless causes us to become the first responder. The usual
427+
// Chrome assumption is that becoming the first responder = you have focus
428+
// so we use this trick to refuse to become first responder during orderFrontRegardless
429+
430+
if (rwhv)
431+
rwhv->SetTakesFocusOnlyOnMouseDown(true);
432+
[window() orderFrontRegardless];
433+
if (rwhv)
434+
rwhv->SetTakesFocusOnlyOnMouseDown(false);
435+
}
436+
first_show_ = false;
424437
}
425438

426439
void NativeWindowCocoa::Hide() {
@@ -618,6 +631,14 @@ - (NSRect)contentRectForFrameRect:(NSRect)frameRect {
618631
standard_menus.BuildWindowMenu();
619632
}
620633

634+
void NativeWindowCocoa::SetInitialFocus(bool accept_focus) {
635+
initial_focus_ = accept_focus;
636+
}
637+
638+
bool NativeWindowCocoa::InitialFocus() {
639+
return initial_focus_;
640+
}
641+
621642
void NativeWindowCocoa::HandleMouseEvent(NSEvent* event) {
622643
if ([event type] == NSLeftMouseDown) {
623644
last_mouse_location_ =

0 commit comments

Comments
 (0)