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:
parent
8e3dcc8b17
commit
127584dc37
13 changed files with 90 additions and 5 deletions
|
@ -1777,6 +1777,7 @@ app.whenReady().then(() => {
|
||||||
In `undocked` mode it's possible to dock back. In `detach` mode it's not.
|
In `undocked` mode it's possible to dock back. In `detach` mode it's not.
|
||||||
* `activate` boolean (optional) - Whether to bring the opened devtools window
|
* `activate` boolean (optional) - Whether to bring the opened devtools window
|
||||||
to the foreground. The default is `true`.
|
to the foreground. The default is `true`.
|
||||||
|
* `title` string (optional) - A title for the DevTools window (only in `undocked` or `detach` mode).
|
||||||
|
|
||||||
Opens the devtools.
|
Opens the devtools.
|
||||||
|
|
||||||
|
@ -1797,6 +1798,18 @@ Returns `boolean` - Whether the devtools is opened.
|
||||||
|
|
||||||
Returns `boolean` - Whether the devtools view is focused .
|
Returns `boolean` - Whether the devtools view is focused .
|
||||||
|
|
||||||
|
#### `contents.getDevToolsTitle()`
|
||||||
|
|
||||||
|
Returns `string` - the current title of the DevTools window. This will only be visible
|
||||||
|
if DevTools is opened in `undocked` or `detach` mode.
|
||||||
|
|
||||||
|
#### `contents.setDevToolsTitle(title)`
|
||||||
|
|
||||||
|
* `title` string
|
||||||
|
|
||||||
|
Changes the title of the DevTools window to `title`. This will only be visible if DevTools is
|
||||||
|
opened in `undocked` or `detach` mode.
|
||||||
|
|
||||||
#### `contents.toggleDevTools()`
|
#### `contents.toggleDevTools()`
|
||||||
|
|
||||||
Toggles the developer tools.
|
Toggles the developer tools.
|
||||||
|
|
|
@ -2649,16 +2649,19 @@ void WebContents::OpenDevTools(gin::Arguments* args) {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
bool activate = true;
|
bool activate = true;
|
||||||
|
std::string title;
|
||||||
if (args && args->Length() == 1) {
|
if (args && args->Length() == 1) {
|
||||||
gin_helper::Dictionary options;
|
gin_helper::Dictionary options;
|
||||||
if (args->GetNext(&options)) {
|
if (args->GetNext(&options)) {
|
||||||
options.Get("mode", &state);
|
options.Get("mode", &state);
|
||||||
options.Get("activate", &activate);
|
options.Get("activate", &activate);
|
||||||
|
options.Get("title", &title);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
DCHECK(inspectable_web_contents_);
|
DCHECK(inspectable_web_contents_);
|
||||||
inspectable_web_contents_->SetDockState(state);
|
inspectable_web_contents_->SetDockState(state);
|
||||||
|
inspectable_web_contents_->SetDevToolsTitle(base::UTF8ToUTF16(title));
|
||||||
inspectable_web_contents_->ShowDevTools(activate);
|
inspectable_web_contents_->ShowDevTools(activate);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2678,6 +2681,18 @@ bool WebContents::IsDevToolsOpened() {
|
||||||
return inspectable_web_contents_->IsDevToolsViewShowing();
|
return inspectable_web_contents_->IsDevToolsViewShowing();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::u16string WebContents::GetDevToolsTitle() {
|
||||||
|
if (type_ == Type::kRemote)
|
||||||
|
return std::u16string();
|
||||||
|
|
||||||
|
DCHECK(inspectable_web_contents_);
|
||||||
|
return inspectable_web_contents_->GetDevToolsTitle();
|
||||||
|
}
|
||||||
|
|
||||||
|
void WebContents::SetDevToolsTitle(const std::u16string& title) {
|
||||||
|
inspectable_web_contents_->SetDevToolsTitle(title);
|
||||||
|
}
|
||||||
|
|
||||||
bool WebContents::IsDevToolsFocused() {
|
bool WebContents::IsDevToolsFocused() {
|
||||||
if (type_ == Type::kRemote)
|
if (type_ == Type::kRemote)
|
||||||
return false;
|
return false;
|
||||||
|
@ -4214,6 +4229,8 @@ void WebContents::FillObjectTemplate(v8::Isolate* isolate,
|
||||||
.SetMethod("closeDevTools", &WebContents::CloseDevTools)
|
.SetMethod("closeDevTools", &WebContents::CloseDevTools)
|
||||||
.SetMethod("isDevToolsOpened", &WebContents::IsDevToolsOpened)
|
.SetMethod("isDevToolsOpened", &WebContents::IsDevToolsOpened)
|
||||||
.SetMethod("isDevToolsFocused", &WebContents::IsDevToolsFocused)
|
.SetMethod("isDevToolsFocused", &WebContents::IsDevToolsFocused)
|
||||||
|
.SetMethod("getDevToolsTitle", &WebContents::GetDevToolsTitle)
|
||||||
|
.SetMethod("setDevToolsTitle", &WebContents::SetDevToolsTitle)
|
||||||
.SetMethod("enableDeviceEmulation", &WebContents::EnableDeviceEmulation)
|
.SetMethod("enableDeviceEmulation", &WebContents::EnableDeviceEmulation)
|
||||||
.SetMethod("disableDeviceEmulation", &WebContents::DisableDeviceEmulation)
|
.SetMethod("disableDeviceEmulation", &WebContents::DisableDeviceEmulation)
|
||||||
.SetMethod("toggleDevTools", &WebContents::ToggleDevTools)
|
.SetMethod("toggleDevTools", &WebContents::ToggleDevTools)
|
||||||
|
|
|
@ -201,6 +201,8 @@ class WebContents : public ExclusiveAccessContext,
|
||||||
void CloseDevTools();
|
void CloseDevTools();
|
||||||
bool IsDevToolsOpened();
|
bool IsDevToolsOpened();
|
||||||
bool IsDevToolsFocused();
|
bool IsDevToolsFocused();
|
||||||
|
std::u16string GetDevToolsTitle();
|
||||||
|
void SetDevToolsTitle(const std::u16string& title);
|
||||||
void ToggleDevTools();
|
void ToggleDevTools();
|
||||||
void EnableDeviceEmulation(const blink::DeviceEmulationParams& params);
|
void EnableDeviceEmulation(const blink::DeviceEmulationParams& params);
|
||||||
void DisableDeviceEmulation();
|
void DisableDeviceEmulation();
|
||||||
|
|
|
@ -50,6 +50,7 @@ using electron::InspectableWebContentsViewMac;
|
||||||
- (void)setContentsResizingStrategy:
|
- (void)setContentsResizingStrategy:
|
||||||
(const DevToolsContentsResizingStrategy&)strategy;
|
(const DevToolsContentsResizingStrategy&)strategy;
|
||||||
- (void)setTitle:(NSString*)title;
|
- (void)setTitle:(NSString*)title;
|
||||||
|
- (NSString*)getTitle;
|
||||||
|
|
||||||
- (void)redispatchContextMenuEvent:(base::apple::OwnedNSEvent)theEvent;
|
- (void)redispatchContextMenuEvent:(base::apple::OwnedNSEvent)theEvent;
|
||||||
|
|
||||||
|
|
|
@ -244,6 +244,10 @@
|
||||||
[devtools_window_ setTitle:title];
|
[devtools_window_ setTitle:title];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (NSString*)getTitle {
|
||||||
|
return [devtools_window_ title];
|
||||||
|
}
|
||||||
|
|
||||||
- (void)viewDidBecomeFirstResponder:(NSNotification*)notification {
|
- (void)viewDidBecomeFirstResponder:(NSNotification*)notification {
|
||||||
auto* inspectable_web_contents =
|
auto* inspectable_web_contents =
|
||||||
inspectableWebContentsView_->inspectable_web_contents();
|
inspectableWebContentsView_->inspectable_web_contents();
|
||||||
|
|
|
@ -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(
|
void InspectableWebContents::SetDevToolsWebContents(
|
||||||
content::WebContents* devtools) {
|
content::WebContents* devtools) {
|
||||||
if (!managed_devtools_web_contents_)
|
if (!managed_devtools_web_contents_)
|
||||||
|
@ -481,6 +486,10 @@ bool InspectableWebContents::IsDevToolsViewShowing() {
|
||||||
return managed_devtools_web_contents_ && view_->IsDevToolsViewShowing();
|
return managed_devtools_web_contents_ && view_->IsDevToolsViewShowing();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::u16string InspectableWebContents::GetDevToolsTitle() {
|
||||||
|
return view_->GetTitle();
|
||||||
|
}
|
||||||
|
|
||||||
void InspectableWebContents::AttachTo(
|
void InspectableWebContents::AttachTo(
|
||||||
scoped_refptr<content::DevToolsAgentHost> host) {
|
scoped_refptr<content::DevToolsAgentHost> host) {
|
||||||
Detach();
|
Detach();
|
||||||
|
@ -565,6 +574,9 @@ void InspectableWebContents::LoadCompleted() {
|
||||||
// If the devtools can dock, "SetIsDocked" will be called by devtools itself.
|
// If the devtools can dock, "SetIsDocked" will be called by devtools itself.
|
||||||
if (!can_dock_) {
|
if (!can_dock_) {
|
||||||
SetIsDocked(DispatchCallback(), false);
|
SetIsDocked(DispatchCallback(), false);
|
||||||
|
if (!devtools_title_.empty()) {
|
||||||
|
view_->SetTitle(devtools_title_);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
if (dock_state_.empty()) {
|
if (dock_state_.empty()) {
|
||||||
const base::Value::Dict& prefs =
|
const base::Value::Dict& prefs =
|
||||||
|
@ -635,9 +647,12 @@ void InspectableWebContents::SetInspectedPageBounds(const gfx::Rect& rect) {
|
||||||
void InspectableWebContents::InspectElementCompleted() {}
|
void InspectableWebContents::InspectElementCompleted() {}
|
||||||
|
|
||||||
void InspectableWebContents::InspectedURLChanged(const std::string& url) {
|
void InspectableWebContents::InspectedURLChanged(const std::string& url) {
|
||||||
if (managed_devtools_web_contents_)
|
if (managed_devtools_web_contents_) {
|
||||||
view_->SetTitle(
|
if (devtools_title_.empty()) {
|
||||||
base::UTF8ToUTF16(base::StringPrintf(kTitleFormat, url.c_str())));
|
view_->SetTitle(
|
||||||
|
base::UTF8ToUTF16(base::StringPrintf(kTitleFormat, url.c_str())));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void InspectableWebContents::LoadNetworkResource(DispatchCallback callback,
|
void InspectableWebContents::LoadNetworkResource(DispatchCallback callback,
|
||||||
|
|
|
@ -59,9 +59,11 @@ class InspectableWebContents
|
||||||
void ReleaseWebContents();
|
void ReleaseWebContents();
|
||||||
void SetDevToolsWebContents(content::WebContents* devtools);
|
void SetDevToolsWebContents(content::WebContents* devtools);
|
||||||
void SetDockState(const std::string& state);
|
void SetDockState(const std::string& state);
|
||||||
|
void SetDevToolsTitle(const std::u16string& title);
|
||||||
void ShowDevTools(bool activate);
|
void ShowDevTools(bool activate);
|
||||||
void CloseDevTools();
|
void CloseDevTools();
|
||||||
bool IsDevToolsViewShowing();
|
bool IsDevToolsViewShowing();
|
||||||
|
std::u16string GetDevToolsTitle();
|
||||||
void AttachTo(scoped_refptr<content::DevToolsAgentHost>);
|
void AttachTo(scoped_refptr<content::DevToolsAgentHost>);
|
||||||
void Detach();
|
void Detach();
|
||||||
void CallClientFunction(
|
void CallClientFunction(
|
||||||
|
@ -207,6 +209,7 @@ class InspectableWebContents
|
||||||
gfx::Rect devtools_bounds_;
|
gfx::Rect devtools_bounds_;
|
||||||
bool can_dock_ = true;
|
bool can_dock_ = true;
|
||||||
std::string dock_state_;
|
std::string dock_state_;
|
||||||
|
std::u16string devtools_title_;
|
||||||
bool activate_ = true;
|
bool activate_ = true;
|
||||||
|
|
||||||
raw_ptr<InspectableWebContentsDelegate> delegate_ =
|
raw_ptr<InspectableWebContentsDelegate> delegate_ =
|
||||||
|
|
|
@ -57,6 +57,7 @@ class InspectableWebContentsView {
|
||||||
virtual void SetContentsResizingStrategy(
|
virtual void SetContentsResizingStrategy(
|
||||||
const DevToolsContentsResizingStrategy& strategy) = 0;
|
const DevToolsContentsResizingStrategy& strategy) = 0;
|
||||||
virtual void SetTitle(const std::u16string& title) = 0;
|
virtual void SetTitle(const std::u16string& title) = 0;
|
||||||
|
virtual const std::u16string GetTitle() = 0;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
// Owns us.
|
// Owns us.
|
||||||
|
|
|
@ -34,6 +34,7 @@ class InspectableWebContentsViewMac : public InspectableWebContentsView {
|
||||||
void SetContentsResizingStrategy(
|
void SetContentsResizingStrategy(
|
||||||
const DevToolsContentsResizingStrategy& strategy) override;
|
const DevToolsContentsResizingStrategy& strategy) override;
|
||||||
void SetTitle(const std::u16string& title) override;
|
void SetTitle(const std::u16string& title) override;
|
||||||
|
const std::u16string GetTitle() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ElectronInspectableWebContentsView* __strong view_;
|
ElectronInspectableWebContentsView* __strong view_;
|
||||||
|
|
|
@ -61,4 +61,8 @@ void InspectableWebContentsViewMac::SetTitle(const std::u16string& title) {
|
||||||
[view_ setTitle:base::SysUTF16ToNSString(title)];
|
[view_ setTitle:base::SysUTF16ToNSString(title)];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const std::u16string InspectableWebContentsViewMac::GetTitle() {
|
||||||
|
return base::SysNSStringToUTF16([view_ getTitle]);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace electron
|
} // namespace electron
|
||||||
|
|
|
@ -209,6 +209,10 @@ void InspectableWebContentsViewViews::SetTitle(const std::u16string& title) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const std::u16string InspectableWebContentsViewViews::GetTitle() {
|
||||||
|
return title_;
|
||||||
|
}
|
||||||
|
|
||||||
void InspectableWebContentsViewViews::Layout() {
|
void InspectableWebContentsViewViews::Layout() {
|
||||||
if (!devtools_web_view_->GetVisible()) {
|
if (!devtools_web_view_->GetVisible()) {
|
||||||
contents_web_view_->SetBoundsRect(GetContentsBounds());
|
contents_web_view_->SetBoundsRect(GetContentsBounds());
|
||||||
|
|
|
@ -39,12 +39,11 @@ class InspectableWebContentsViewViews : public InspectableWebContentsView,
|
||||||
void SetContentsResizingStrategy(
|
void SetContentsResizingStrategy(
|
||||||
const DevToolsContentsResizingStrategy& strategy) override;
|
const DevToolsContentsResizingStrategy& strategy) override;
|
||||||
void SetTitle(const std::u16string& title) override;
|
void SetTitle(const std::u16string& title) override;
|
||||||
|
const std::u16string GetTitle() override;
|
||||||
|
|
||||||
// views::View:
|
// views::View:
|
||||||
void Layout() override;
|
void Layout() override;
|
||||||
|
|
||||||
const std::u16string& GetTitle() const { return title_; }
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<views::Widget> devtools_window_;
|
std::unique_ptr<views::Widget> devtools_window_;
|
||||||
raw_ptr<views::WebView> devtools_window_web_view_ = nullptr;
|
raw_ptr<views::WebView> devtools_window_web_view_ = nullptr;
|
||||||
|
|
|
@ -608,6 +608,27 @@ describe('webContents module', () => {
|
||||||
await devtoolsOpened;
|
await devtoolsOpened;
|
||||||
expect(w.webContents.isDevToolsOpened()).to.be.true();
|
expect(w.webContents.isDevToolsOpened()).to.be.true();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('can show a DevTools window with custom title', async () => {
|
||||||
|
const w = new BrowserWindow({ show: false });
|
||||||
|
const devtoolsOpened = once(w.webContents, 'devtools-opened');
|
||||||
|
w.webContents.openDevTools({ mode: 'detach', activate: false, title: 'myTitle' });
|
||||||
|
await devtoolsOpened;
|
||||||
|
expect(w.webContents.getDevToolsTitle()).to.equal('myTitle');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('setDevToolsTitle() API', () => {
|
||||||
|
afterEach(closeAllWindows);
|
||||||
|
it('can set devtools title with function', async () => {
|
||||||
|
const w = new BrowserWindow({ show: false });
|
||||||
|
const devtoolsOpened = once(w.webContents, 'devtools-opened');
|
||||||
|
w.webContents.openDevTools({ mode: 'detach', activate: false });
|
||||||
|
await devtoolsOpened;
|
||||||
|
expect(w.webContents.isDevToolsOpened()).to.be.true();
|
||||||
|
w.webContents.setDevToolsTitle('newTitle');
|
||||||
|
expect(w.webContents.getDevToolsTitle()).to.equal('newTitle');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('before-input-event event', () => {
|
describe('before-input-event event', () => {
|
||||||
|
|
Loading…
Add table
Reference in a new issue