feat: add a 'title' parameter to openDevTools() (#39047)

* [Feature Request]: Add a parameter to openDevTools() that sets the DevTools window title bar

* all titles->title

* add GetDevToolsTitle(),update docs

* fix:lint error

* fix:lint error

* add setDevToolTitle

* lint errror

* lint errror

* ling errror (.md)

* build error

* build error in mac

* build error

* build error

* change docs

* std::string->std::u16string

* lint error

* build error

* build error
This commit is contained in:
wgsheng 2023-08-15 13:32:53 +08:00 committed by GitHub
parent 8e3dcc8b17
commit 127584dc37
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 90 additions and 5 deletions

View file

@ -50,6 +50,7 @@ using electron::InspectableWebContentsViewMac;
- (void)setContentsResizingStrategy:
(const DevToolsContentsResizingStrategy&)strategy;
- (void)setTitle:(NSString*)title;
- (NSString*)getTitle;
- (void)redispatchContextMenuEvent:(base::apple::OwnedNSEvent)theEvent;

View file

@ -244,6 +244,10 @@
[devtools_window_ setTitle:title];
}
- (NSString*)getTitle {
return [devtools_window_ title];
}
- (void)viewDidBecomeFirstResponder:(NSNotification*)notification {
auto* inspectable_web_contents =
inspectableWebContentsView_->inspectable_web_contents();

View file

@ -426,6 +426,11 @@ void InspectableWebContents::SetDockState(const std::string& state) {
}
}
void InspectableWebContents::SetDevToolsTitle(const std::u16string& title) {
devtools_title_ = title;
view_->SetTitle(devtools_title_);
}
void InspectableWebContents::SetDevToolsWebContents(
content::WebContents* devtools) {
if (!managed_devtools_web_contents_)
@ -481,6 +486,10 @@ bool InspectableWebContents::IsDevToolsViewShowing() {
return managed_devtools_web_contents_ && view_->IsDevToolsViewShowing();
}
std::u16string InspectableWebContents::GetDevToolsTitle() {
return view_->GetTitle();
}
void InspectableWebContents::AttachTo(
scoped_refptr<content::DevToolsAgentHost> host) {
Detach();
@ -565,6 +574,9 @@ void InspectableWebContents::LoadCompleted() {
// If the devtools can dock, "SetIsDocked" will be called by devtools itself.
if (!can_dock_) {
SetIsDocked(DispatchCallback(), false);
if (!devtools_title_.empty()) {
view_->SetTitle(devtools_title_);
}
} else {
if (dock_state_.empty()) {
const base::Value::Dict& prefs =
@ -635,9 +647,12 @@ void InspectableWebContents::SetInspectedPageBounds(const gfx::Rect& rect) {
void InspectableWebContents::InspectElementCompleted() {}
void InspectableWebContents::InspectedURLChanged(const std::string& url) {
if (managed_devtools_web_contents_)
view_->SetTitle(
base::UTF8ToUTF16(base::StringPrintf(kTitleFormat, url.c_str())));
if (managed_devtools_web_contents_) {
if (devtools_title_.empty()) {
view_->SetTitle(
base::UTF8ToUTF16(base::StringPrintf(kTitleFormat, url.c_str())));
}
}
}
void InspectableWebContents::LoadNetworkResource(DispatchCallback callback,

View file

@ -59,9 +59,11 @@ class InspectableWebContents
void ReleaseWebContents();
void SetDevToolsWebContents(content::WebContents* devtools);
void SetDockState(const std::string& state);
void SetDevToolsTitle(const std::u16string& title);
void ShowDevTools(bool activate);
void CloseDevTools();
bool IsDevToolsViewShowing();
std::u16string GetDevToolsTitle();
void AttachTo(scoped_refptr<content::DevToolsAgentHost>);
void Detach();
void CallClientFunction(
@ -207,6 +209,7 @@ class InspectableWebContents
gfx::Rect devtools_bounds_;
bool can_dock_ = true;
std::string dock_state_;
std::u16string devtools_title_;
bool activate_ = true;
raw_ptr<InspectableWebContentsDelegate> delegate_ =

View file

@ -57,6 +57,7 @@ class InspectableWebContentsView {
virtual void SetContentsResizingStrategy(
const DevToolsContentsResizingStrategy& strategy) = 0;
virtual void SetTitle(const std::u16string& title) = 0;
virtual const std::u16string GetTitle() = 0;
protected:
// Owns us.

View file

@ -34,6 +34,7 @@ class InspectableWebContentsViewMac : public InspectableWebContentsView {
void SetContentsResizingStrategy(
const DevToolsContentsResizingStrategy& strategy) override;
void SetTitle(const std::u16string& title) override;
const std::u16string GetTitle() override;
private:
ElectronInspectableWebContentsView* __strong view_;

View file

@ -61,4 +61,8 @@ void InspectableWebContentsViewMac::SetTitle(const std::u16string& title) {
[view_ setTitle:base::SysUTF16ToNSString(title)];
}
const std::u16string InspectableWebContentsViewMac::GetTitle() {
return base::SysNSStringToUTF16([view_ getTitle]);
}
} // namespace electron

View file

@ -209,6 +209,10 @@ void InspectableWebContentsViewViews::SetTitle(const std::u16string& title) {
}
}
const std::u16string InspectableWebContentsViewViews::GetTitle() {
return title_;
}
void InspectableWebContentsViewViews::Layout() {
if (!devtools_web_view_->GetVisible()) {
contents_web_view_->SetBoundsRect(GetContentsBounds());

View file

@ -39,12 +39,11 @@ class InspectableWebContentsViewViews : public InspectableWebContentsView,
void SetContentsResizingStrategy(
const DevToolsContentsResizingStrategy& strategy) override;
void SetTitle(const std::u16string& title) override;
const std::u16string GetTitle() override;
// views::View:
void Layout() override;
const std::u16string& GetTitle() const { return title_; }
private:
std::unique_ptr<views::Widget> devtools_window_;
raw_ptr<views::WebView> devtools_window_web_view_ = nullptr;