mirror of
https://github.com/electron/electron.git
synced 2026-04-10 03:01:51 -04:00
fix: adjust initial webContents focus calculation (#29204)
* fix: adjust initial webContents focus calculation * fix: active window check on mac * fix: about:blank focus behavior * chore: add spec Co-authored-by: Raymond Zhao <raymondzhao@microsoft.com>
This commit is contained in:
@@ -289,6 +289,7 @@ void BrowserWindow::OnWindowIsKeyChanged(bool is_key) {
|
||||
auto* rwhv = web_contents()->GetRenderWidgetHostView();
|
||||
if (rwhv)
|
||||
rwhv->SetActive(is_key);
|
||||
window()->SetActive(is_key);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -701,8 +701,8 @@ WebContents::WebContents(v8::Isolate* isolate,
|
||||
// BrowserViews are not attached to a window initially so they should start
|
||||
// off as hidden. This is also important for compositor recycling. See:
|
||||
// https://github.com/electron/electron/pull/21372
|
||||
initially_shown_ = type_ != Type::kBrowserView;
|
||||
options.Get(options::kShow, &initially_shown_);
|
||||
bool initially_shown = type_ != Type::kBrowserView;
|
||||
options.Get(options::kShow, &initially_shown);
|
||||
|
||||
// Obtain the session.
|
||||
std::string partition;
|
||||
@@ -758,7 +758,7 @@ WebContents::WebContents(v8::Isolate* isolate,
|
||||
#endif
|
||||
} else {
|
||||
content::WebContents::CreateParams params(session->browser_context());
|
||||
params.initially_hidden = !initially_shown_;
|
||||
params.initially_hidden = !initially_shown;
|
||||
web_contents = content::WebContents::Create(params);
|
||||
}
|
||||
|
||||
@@ -1650,6 +1650,27 @@ void WebContents::DidRedirectNavigation(
|
||||
EmitNavigationEvent("did-redirect-navigation", navigation_handle);
|
||||
}
|
||||
|
||||
void WebContents::ReadyToCommitNavigation(
|
||||
content::NavigationHandle* navigation_handle) {
|
||||
// Don't focus content in an inactive window.
|
||||
if (!owner_window())
|
||||
return;
|
||||
#if defined(OS_MAC)
|
||||
if (!owner_window()->IsActive())
|
||||
return;
|
||||
#else
|
||||
if (!owner_window()->widget()->IsActive())
|
||||
return;
|
||||
#endif
|
||||
// Don't focus content after subframe navigations.
|
||||
if (!navigation_handle->IsInMainFrame())
|
||||
return;
|
||||
// Only focus for top-level contents.
|
||||
if (type_ != Type::kBrowserWindow)
|
||||
return;
|
||||
web_contents()->SetInitialFocus();
|
||||
}
|
||||
|
||||
void WebContents::DidFinishNavigation(
|
||||
content::NavigationHandle* navigation_handle) {
|
||||
if (!navigation_handle->HasCommitted())
|
||||
@@ -3110,10 +3131,6 @@ v8::Local<v8::Value> WebContents::Debugger(v8::Isolate* isolate) {
|
||||
return v8::Local<v8::Value>::New(isolate, debugger_);
|
||||
}
|
||||
|
||||
bool WebContents::WasInitiallyShown() {
|
||||
return initially_shown_;
|
||||
}
|
||||
|
||||
content::RenderFrameHost* WebContents::MainFrame() {
|
||||
return web_contents()->GetMainFrame();
|
||||
}
|
||||
@@ -3683,7 +3700,6 @@ v8::Local<v8::ObjectTemplate> WebContents::FillObjectTemplate(
|
||||
.SetProperty("hostWebContents", &WebContents::HostWebContents)
|
||||
.SetProperty("devToolsWebContents", &WebContents::DevToolsWebContents)
|
||||
.SetProperty("debugger", &WebContents::Debugger)
|
||||
.SetProperty("_initiallyShown", &WebContents::WasInitiallyShown)
|
||||
.SetProperty("mainFrame", &WebContents::MainFrame)
|
||||
.Build();
|
||||
}
|
||||
|
||||
@@ -324,7 +324,6 @@ class WebContents : public gin::Wrappable<WebContents>,
|
||||
content::WebContents* HostWebContents() const;
|
||||
v8::Local<v8::Value> DevToolsWebContents(v8::Isolate* isolate);
|
||||
v8::Local<v8::Value> Debugger(v8::Isolate* isolate);
|
||||
bool WasInitiallyShown();
|
||||
content::RenderFrameHost* MainFrame();
|
||||
|
||||
WebContentsZoomController* GetZoomController() { return zoom_controller_; }
|
||||
@@ -559,6 +558,8 @@ class WebContents : public gin::Wrappable<WebContents>,
|
||||
content::NavigationHandle* navigation_handle) override;
|
||||
void DidRedirectNavigation(
|
||||
content::NavigationHandle* navigation_handle) override;
|
||||
void ReadyToCommitNavigation(
|
||||
content::NavigationHandle* navigation_handle) override;
|
||||
void DidFinishNavigation(
|
||||
content::NavigationHandle* navigation_handle) override;
|
||||
bool OnMessageReceived(const IPC::Message& message) override;
|
||||
@@ -722,8 +723,6 @@ class WebContents : public gin::Wrappable<WebContents>,
|
||||
|
||||
v8::Global<v8::Value> pending_child_web_preferences_;
|
||||
|
||||
bool initially_shown_ = true;
|
||||
|
||||
// The window that this WebContents belongs to.
|
||||
base::WeakPtr<NativeWindow> owner_window_;
|
||||
|
||||
|
||||
@@ -135,6 +135,10 @@ class NativeWindow : public base::SupportsUserData,
|
||||
virtual void Invalidate() = 0;
|
||||
virtual void SetTitle(const std::string& title) = 0;
|
||||
virtual std::string GetTitle() = 0;
|
||||
#if defined(OS_MAC)
|
||||
virtual void SetActive(bool is_key) = 0;
|
||||
virtual bool IsActive() const = 0;
|
||||
#endif
|
||||
|
||||
// Ability to augment the window title for the screen readers.
|
||||
void SetAccessibleTitle(const std::string& title);
|
||||
|
||||
@@ -149,6 +149,8 @@ class NativeWindowMac : public NativeWindow,
|
||||
gfx::Rect WindowBoundsToContentBounds(const gfx::Rect& bounds) const override;
|
||||
void NotifyWindowEnterFullScreen() override;
|
||||
void NotifyWindowLeaveFullScreen() override;
|
||||
void SetActive(bool is_key) override;
|
||||
bool IsActive() const override;
|
||||
|
||||
void NotifyWindowWillEnterFullScreen();
|
||||
void NotifyWindowWillLeaveFullScreen();
|
||||
@@ -267,6 +269,7 @@ class NativeWindowMac : public NativeWindow,
|
||||
bool is_simple_fullscreen_ = false;
|
||||
bool was_maximizable_ = false;
|
||||
bool was_movable_ = false;
|
||||
bool is_active_ = false;
|
||||
NSRect original_frame_;
|
||||
NSInteger original_level_;
|
||||
NSUInteger simple_fullscreen_mask_;
|
||||
|
||||
@@ -1640,6 +1640,16 @@ void NativeWindowMac::NotifyWindowWillLeaveFullScreen() {
|
||||
UpdateVibrancyRadii(false);
|
||||
}
|
||||
|
||||
void NativeWindowMac::SetActive(bool is_key) {
|
||||
if (is_key)
|
||||
widget()->Activate();
|
||||
is_active_ = is_key;
|
||||
}
|
||||
|
||||
bool NativeWindowMac::IsActive() const {
|
||||
return is_active_;
|
||||
}
|
||||
|
||||
void NativeWindowMac::Cleanup() {
|
||||
DCHECK(!IsClosed());
|
||||
ui::NativeTheme::GetInstanceForNativeUi()->RemoveObserver(this);
|
||||
|
||||
Reference in New Issue
Block a user