Skip to content

Commit 85ef762

Browse files
authored
fix: guard against duplicate TouchBarItem IDs (electron#22272)
* feat: Add OtherItemsProxy touchbar item * review! * fix: guard against duplicate TouchBarItem IDs * add spec
1 parent 12c1d44 commit 85ef762

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

lib/browser/api/touch-bar.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class TouchBar extends EventEmitter {
5353
}
5454

5555
let hasOtherItemsProxy = false
56+
const idSet = new Set()
5657
items.forEach((item) => {
5758
if (!(item instanceof TouchBarItem)) {
5859
throw new Error('Each item must be an instance of TouchBarItem')
@@ -65,6 +66,12 @@ class TouchBar extends EventEmitter {
6566
throw new Error('Must only have one OtherItemsProxy per TouchBar')
6667
}
6768
}
69+
70+
if (!idSet.has(item.id)) {
71+
idSet.add(item.id)
72+
} else {
73+
throw new Error('Cannot add a single instance of TouchBarItem multiple times in a TouchBar')
74+
}
6875
})
6976

7077
// register in separate loop after all items are validated

spec-main/api-touch-bar-spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ describe('TouchBar module', () => {
3939
}).to.throw('Must only have one OtherItemsProxy per TouchBar')
4040
})
4141

42+
it('throws an error if the same TouchBarItem is added multiple times', () => {
43+
expect(() => {
44+
const item = new TouchBarLabel({ label: 'Label' })
45+
const touchBar = new TouchBar({ items: [item, item] })
46+
touchBar.toString()
47+
}).to.throw('Cannot add a single instance of TouchBarItem multiple times in a TouchBar')
48+
})
49+
4250
describe('BrowserWindow behavior', () => {
4351
let window: BrowserWindow
4452

0 commit comments

Comments
 (0)