-
Notifications
You must be signed in to change notification settings - Fork 2
/
decorate-window.ts
229 lines (192 loc) · 6.08 KB
/
decorate-window.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import * as path from "path";
import * as fs from "fs";
import { ipcMain, TouchBar, BrowserWindow, WebContents } from "electron";
import { ItemAction, IpcEvent, TouchBarItem, ItemType } from "./types";
import { debug } from "./util";
interface CreatePayload {
id: string;
items: TouchBarItem[];
registerOnly?: boolean;
}
interface DestroyPayload {
id: string;
prevId?: string;
}
interface UpdatePayload {
parent: string;
id: string;
props: Record<string, any>;
}
interface ItemObject {
id: string;
instance: ReturnType<typeof createItem>;
}
/**
* Holds all TouchBar definitions so we can update them & restore previous
* states.
*/
let touchBarInstances: Record<
string,
{
items: ItemObject[];
}
> = {};
/**
* Resolve icon path from the current script directory.
*/
function resolveIcon(id: string, icon?: string) {
if (!icon) return null;
const iconPath = path.normalize(`${__dirname}/${icon}`);
if (fs.existsSync(iconPath)) return iconPath;
debug(
`Tried to resolve icon path for item "${id}" but the path doesn’t exist (${iconPath})`
);
return null;
}
/**
* Some child items like (eg. Scrubber/Segmented Control) might have an `icon`.
* We need to resolve its path correctly.
*/
function resolveIconForItems<T extends { icon: string }>(
id: string,
items: T[]
) {
return items.map((item) => ({
...item,
icon: resolveIcon(id, item.icon),
}));
}
/**
* Create action handler for a given item.
*/
function bindItemAction(parent: WebContents, id: string, action: ItemAction) {
return (args: any) => {
debug(`User action "${action}" on item "${id}"`);
parent.send(IpcEvent.itemAction, { id, action, args });
};
}
/**
* Create a TouchBar item instance from item definition.
*/
function createItem(parent: WebContents, item: TouchBarItem) {
const isGroupLike = [
ItemType.TouchBarGroup,
ItemType.TouchBarPopover,
].includes(item.type);
// Bind events
const events = {
click: bindItemAction(parent, item.id, ItemAction.click),
highlight: bindItemAction(parent, item.id, ItemAction.click),
change: bindItemAction(parent, item.id, ItemAction.change),
select: bindItemAction(parent, item.id, ItemAction.change),
};
debug(
`Creating a new item of type "${item.type}" (id: "${item.id}") with props:`,
item.props
);
// Resolve any icon paths
if (item.props.icon) {
item.props.icon = resolveIcon(item.id, item.props.icon);
}
// Segments and items need to be mapped to correctly resolve the icon prop.
if (item.props.segments?.length) {
item.props.segments = resolveIconForItems(item.id, item.props.segments);
}
if (!isGroupLike && item.props.items?.length) {
item.props.items = resolveIconForItems(item.id, item.props.items);
}
// Popover and group items need to be inside of another TouchBar instance
if (isGroupLike) {
item.props.items = new TouchBar({
items: touchBarInstances[item.id].items.map(({ instance }) => instance),
});
}
return new TouchBar[item.type]({ ...item.props, ...events });
}
/**
* Create a TouchBar instance. Mutates `touchBarInstances`
*/
function createTouchBarInstance(parent: WebContents, payload: CreatePayload) {
// Map item definitions to native instances
const items = payload.items.map((item) => ({
id: item.id,
instance: createItem(parent, item),
}));
// Save for later so we can update the items
touchBarInstances[payload.id] = { items };
if (payload.registerOnly) return;
return new TouchBar({ items: items.map(({ instance }) => instance) });
}
/**
* Update props on a TouchBar item. Mutates the original item.
*/
function updateTouchBarItem(item: ItemObject, prop: string, value: any) {
switch (prop) {
case "icon":
// @ts-expect-error
item.instance[prop] = resolveIcon(item.id, value);
break;
case "items":
case "segments":
// @ts-expect-error
item.instance[prop] = resolveIconForItems(item.id, value);
break;
default:
// @ts-expect-error
item.instance[prop] = value;
}
}
/**
* Decorate BrowserWindow to correctly handle updates & events from and to renderer.
*/
export function decorateWindow(browserWindow: BrowserWindow) {
ipcMain.on(IpcEvent.create, (event, payload: CreatePayload) => {
debug("Received create event with payload", payload);
const touchBarInstance = createTouchBarInstance(event.sender, payload);
/**
* Child TouchBars (Groups/Popovers) are registered only, we don’t want to
* set them immediately, we just need to save their native representation so
* we can add them as items to the currently active TouchBar.
*/
if (!touchBarInstance || payload.registerOnly) return;
browserWindow.setTouchBar(touchBarInstance);
});
ipcMain.on(IpcEvent.destroy, (_event, payload: DestroyPayload) => {
debug("Received destroy event with payload", payload);
delete touchBarInstances[payload.id];
let prevItems: ItemObject[] = [];
if (payload.prevId) {
if (touchBarInstances[payload.prevId]) {
prevItems = touchBarInstances[payload.prevId].items ?? [];
} else {
debug(
`Tried restoring previous TouchBar instance with id "${payload.prevId}" but it doesn’t exist. Possible ids:`,
Object.keys(touchBarInstances)
);
}
}
browserWindow.setTouchBar(
new TouchBar({ items: prevItems.map(({ instance }) => instance) })
);
});
ipcMain.on(IpcEvent.updateitem, (_event, payload: UpdatePayload) => {
const item = touchBarInstances[payload.parent].items.find(
(item) => item.id === payload.id
);
if (!item) {
debug(
`Tried to update item with id "${payload.id}" but it does not exist!`
);
return;
}
debug(`Updating item with id "${payload.id}" with props:`, payload.props);
/**
* Go through the `keys` of the payload and set the value to the item
* instance. Instance props are reactive, so they’ll update the TouchBar
* automatically.
*/
Object.keys(payload.props).forEach((prop) =>
updateTouchBarItem(item, prop, payload.props[prop])
);
});
}