refactor: use WidgetDelegate's title property (#46849)

* refactor: use WidgetDelegate::SetTitle()

* Make NativeWindow::SetTitle() and NativeWindow::GetTitle() non-virtual.
  Use WidgetDelegate for their implementation.

* Add NativeWindow::OnTitleChanged(), a new protected virtual method to update
  subclasses (e.g. NativeWindowMac needs to redraw the button proxy).

* In NativeWindowMac, replace SetTitle() and GetTitle() with OnTitleChanged().

* In NativeWindowViews, replace SetTitle() and GetTitle() with OnTitleChanged().

* test: enable BrowserWindow.title tests on Linux

* test: add a test to confirm win.title changes when document.title is set in the renderer
This commit is contained in:
Charles Kerr 2025-04-30 10:22:27 -05:00 committed by GitHub
commit 25d77fd1ce
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 60 additions and 59 deletions

View file

@ -3888,8 +3888,14 @@ describe('BrowserWindow module', () => {
});
it('works for window events', async () => {
const pageTitleUpdated = once(w, 'page-title-updated');
w.loadURL('data:text/html,<script>document.title = \'changed\'</script>');
const newTitle = 'changed';
w.loadURL(`data:text/html,<script>document.title = '${newTitle}'</script>`);
await pageTitleUpdated;
// w.title should update after 'page-title-updated'.
// It happens right *after* the event fires though,
// so we have to waitUntil it changes
waitUntil(() => w.title === newTitle);
});
it('works for stop events', async () => {
@ -5428,6 +5434,36 @@ describe('BrowserWindow module', () => {
});
});
});
describe('native window title', () => {
describe('with properties', () => {
it('can be set with title constructor option', () => {
const w = new BrowserWindow({ show: false, title: 'mYtItLe' });
expect(w.title).to.eql('mYtItLe');
});
it('can be changed', () => {
const w = new BrowserWindow({ show: false });
expect(w.title).to.eql('Electron Test Main');
w.title = 'NEW TITLE';
expect(w.title).to.eql('NEW TITLE');
});
});
describe('with functions', () => {
it('can be set with minimizable constructor option', () => {
const w = new BrowserWindow({ show: false, title: 'mYtItLe' });
expect(w.getTitle()).to.eql('mYtItLe');
});
it('can be changed', () => {
const w = new BrowserWindow({ show: false });
expect(w.getTitle()).to.eql('Electron Test Main');
w.setTitle('NEW TITLE');
expect(w.getTitle()).to.eql('NEW TITLE');
});
});
});
});
ifdescribe(process.platform !== 'linux')('window states (excluding Linux)', () => {
@ -5508,36 +5544,6 @@ describe('BrowserWindow module', () => {
});
});
describe('native window title', () => {
describe('with properties', () => {
it('can be set with title constructor option', () => {
const w = new BrowserWindow({ show: false, title: 'mYtItLe' });
expect(w.title).to.eql('mYtItLe');
});
it('can be changed', () => {
const w = new BrowserWindow({ show: false });
expect(w.title).to.eql('Electron Test Main');
w.title = 'NEW TITLE';
expect(w.title).to.eql('NEW TITLE');
});
});
describe('with functions', () => {
it('can be set with minimizable constructor option', () => {
const w = new BrowserWindow({ show: false, title: 'mYtItLe' });
expect(w.getTitle()).to.eql('mYtItLe');
});
it('can be changed', () => {
const w = new BrowserWindow({ show: false });
expect(w.getTitle()).to.eql('Electron Test Main');
w.setTitle('NEW TITLE');
expect(w.getTitle()).to.eql('NEW TITLE');
});
});
});
describe('minimizable state', () => {
describe('with properties', () => {
it('can be set with minimizable constructor option', () => {