forked from sindresorhus/electron-util
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.d.ts
766 lines (645 loc) · 17.9 KB
/
index.d.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
/// <reference lib="dom"/>
/// <reference types="electron"/>
/// <reference types="node"/>
import {AllElectron, Remote, BrowserWindow, Size, Rectangle, Session, MenuItemConstructorOptions, MenuItem} from 'electron';
import {Options as NewGithubIssueUrlOptions} from 'new-github-issue-url';
import {RequireAtLeastOne} from 'type-fest';
/**
Access the Electron APIs in both the main and renderer process without having to care about which one you're in.
@example
```
// In the renderer process
api.app.quit(); // The `app` API is usually only available in the main process.
```
*/
export const api: AllElectron | Remote;
/**
Check for various things.
*/
export const is: {
/**
Running on macOS.
*/
readonly macos: boolean,
/**
Running on Linux.
*/
readonly linux: boolean,
/**
Running on Windows.
*/
readonly windows: boolean,
/**
Running on the [main process](https://electronjs.org/docs/tutorial/quick-start/#main-process).
*/
readonly main: boolean,
/**
Running on the [renderer process](https://electronjs.org/docs/tutorial/quick-start/#renderer-process).
*/
readonly renderer: boolean,
/**
The app is using [ASAR](https://electronjs.org/docs/tutorial/application-packaging/).
*/
readonly usingAsar: boolean,
/**
Running in development, not in production.
*/
readonly development: boolean,
/**
The app is an Mac App Store build.
*/
readonly macAppStore: boolean,
/**
The app is a Windows Store AppX build.
*/
readonly windowsStore: boolean
};
/**
Electron version.
@example
```
'1.7.9'
```
*/
export const electronVersion: string;
/**
Chrome version in Electron.
@example
```
'62.0.3202'
```
*/
export const chromeVersion: string;
interface _Choices<Macos, Windows, Linux, Default> {
readonly macos?: Macos | (() => Macos);
readonly windows?: Windows | (() => Windows);
readonly linux?: Linux | (() => Linux);
readonly default?: Default | (() => Default);
}
export type Choices<Macos, Windows, Linux, Default> = RequireAtLeastOne<_Choices<Macos, Windows, Linux, Default>, 'macos' | 'windows' | 'linux'>
/**
Accepts an object with the keys as either `macos`, `windows`, `linux`, or `default`, and picks the appropriate key depending on the current platform.
If no platform key is matched, the `default` key is used if it exists.
If the value is a function, it will be executed, and the returned value will be used.
@example
```
init({
enableUnicorn: util.platform({
macos: true,
windows: false,
linux: () => false
})
});
```
*/
export function platform<Macos = never, Windows = never, Linux = never, Default = undefined>(choices: Choices<Macos, Windows, Linux, Default>): Macos | Windows | Linux | Default;
/**
Returns the active window.
*/
export function activeWindow(): BrowserWindow;
/**
Run some JavaScript in the active or given window.
@param code - JavaScript code to be executed.
@param window - Default: Current window
@returns A promise for the result of the executed code or a rejected promise if the result is a rejected promise.
*/
export function runJS(code: string, window?: BrowserWindow): Promise<unknown>;
/**
ASAR is great, but it has [limitations when it comes to executing binaries](https://electronjs.org/docs/tutorial/application-packaging/#executing-binaries-inside-asar-archive).
For example, [`child_process.spawn()` is not automatically handled](https://github.com/electron/electron/issues/9459).
So you would have to unpack the binary, for example, with the [`asarUnpack`](https://www.electron.build/configuration/configuration#configuration-asarUnpack) option in `electron-builder`.
This creates a problem as the path to the binary changes, but your `path.join(__dirname, 'binary')` is not changed.
To make it work you need to fix the path. That's the purpose of this method.
Before:
/Users/sindresorhus/Kap.app/Contents/Resources/app.asar/node_modules/foo/binary
After:
/Users/sindresorhus/Kap.app/Contents/Resources/app.asar.unpacked/node_modules/foo/binary
@param path - A path in your app.
@returns The fixed path.
*/
export function fixPathForAsarUnpack(path: string): string;
/**
On macOS, for [security reasons](https://github.com/potionfactory/LetsMove/issues/56), if an app is launched outside the Applications folder, it will run in a read-only disk image, which could cause subtle problems for your app.
Use this method to ensure the app lives in the Applications folder.
It must not be used until the `app.whenReady()` promise is resolved.
It will be a no-op during development and on other systems than macOS.
It will offer to automatically move the app for the user.
*/
export function enforceMacOSAppLocation(): void;
/**
@returns The height of the menu bar on macOS, or `0` if not macOS.
*/
export function menuBarHeight(): number;
export interface GetWindowBoundsCenteredOptions {
/**
The window to get the bounds of.
Default: Current window
*/
readonly window?: BrowserWindow;
/**
Set a new window size.
Default: Size of `window`
@example
```
{width: 600, height: 400}
```
*/
readonly size?: Size;
/**
Use the full display size when calculating the position.
By default, only the workable screen area is used, which excludes the Windows taskbar and macOS dock.
@default false
*/
readonly useFullBounds?: boolean;
}
/**
Get the [bounds](https://electronjs.org/docs/api/browser-window#wingetbounds) of a window as if it was centered on the screen.
@returns Bounds of a window.
*/
export function getWindowBoundsCentered(options?: GetWindowBoundsCenteredOptions): Rectangle;
export interface OptionalRectangle {
/**
The height of the rectangle (must be an integer).
*/
readonly height?: number;
/**
The width of the rectangle (must be an integer).
*/
readonly width?: number;
/**
The x coordinate of the origin of the rectangle (must be an integer).
*/
readonly x?: number;
/**
The y coordinate of the origin of the rectangle (must be an integer).
*/
readonly y?: number;
}
export interface CenterWindowOptions {
/**
The window to center.
Default: Current window
*/
readonly window?: BrowserWindow;
/**
Set a new window size.
Default: Size of `window`
@example
```
{width: 600, height: 400}
```
*/
readonly size?: Size;
/**
Animate the change.
@default false
*/
readonly animated?: boolean;
/**
Use the full display size when calculating the position.
By default, only the workable screen area is used, which excludes the Windows taskbar and macOS dock.
@default false
*/
readonly useFullBounds?: boolean;
}
/**
Center a window on the screen.
*/
export function centerWindow(options?: CenterWindowOptions): void;
/**
Disable zooming, usually caused by pinching the trackpad on macOS or Ctrl+ on Windows.
@param window - Default: Current window
*/
export function disableZoom(window?: BrowserWindow): void;
/**
A timestamp (`Date.now()`) of when your app launched.
*/
export const appLaunchTimestamp: number;
/**
It works by writing a file to `app.getPath('userData')` if it doesn't exist and checks that.
That means it will return true the first time you add this check to your app.
@returns A `boolean` of whether it's the first time your app is launched.
*/
export function isFirstAppLaunch(): boolean;
export interface DarkMode {
/**
Whether the macOS dark mode is enabled.
On Windows and Linux, it's `false`.
*/
readonly isEnabled: boolean;
/**
The `callback` function is called when the macOS dark mode is toggled.
@returns A function, that when called, unsubscribes the listener. Calling it on Window and Linux works, but it just returns a no-op function.
*/
readonly onChange: (callback: () => void) => () => void;
}
/**
@example
```
import {darkMode} from 'electron-util';
console.log(darkMode.isEnabled);
//=> false
darkMode.onChange(() => {
console.log(darkMode.isEnabled);
//=> true
});
```
*/
export const darkMode: DarkMode;
export interface SetContentSecurityPolicyOptions {
/**
The session to apply the policy to.
Default: [`electron.session.defaultSession`](https://electronjs.org/docs/api/session#sessiondefaultsession)
*/
readonly session?: Session;
}
/**
Set a [Content Security Policy](https://developers.google.com/web/fundamentals/security/csp/) for your app.
Don't forget to [validate the policy](https://csp-evaluator.withgoogle.com) after changes.
@param policy - You can put rules on separate lines, but lines must end in a semicolon.
@example
```
import setContentSecurityPolicy from 'electron-util';
setContentSecurityPolicy(`
default-src 'none';
script-src 'self';
img-src 'self' data:;
style-src 'self';
font-src 'self';
connect-src 'self' https://api.example.com;
base-uri 'none';
form-action 'none';
frame-ancestors 'none';
`);
*/
export function setContentSecurityPolicy(policy: string, options?: SetContentSecurityPolicyOptions): void;
/**
Opens the new issue view on the given GitHub repo in the browser.
Optionally, with some fields like title and body prefilled.
@param options - The options are passed to the [`new-github-issue-url`](https://github.com/sindresorhus/new-github-issue-url#options) package.
@example
```
import {openNewGitHubIssue} from 'electron-util';
openNewGitHubIssue({
user: 'sindresorhus',
repo: 'playground',
body: 'Hello'
});
*/
export function openNewGitHubIssue(options: NewGithubIssueUrlOptions): void;
export interface OpenUrlMenuItemOptions extends Readonly<MenuItemConstructorOptions> {
/**
URL to be opened when the menu item is clicked.
*/
readonly url: string;
}
/**
Accepts the same options as [`new MenuItem()`](https://electronjs.org/docs/api/menu-item) in addition to a `url` option.
If you specify the `click` option, its handler will be called before the URL is opened.
@returns A `MenuItemConstructorOptions` that creates a menu item, which opens the given URL in the browser when clicked.
@example
```
import {Menu} from 'electron';
import {openUrlMenuItem} from 'electron-util';
const menu = Menu.buildFromTemplate([
{
label: 'Help',
submenu: [
openUrlMenuItem({
label: 'Website',
url: 'https://sindresorhus.com'
})
]
}
]);
Menu.setApplicationMenu(menu);
*/
export function openUrlMenuItem(options?: OpenUrlMenuItemOptions): MenuItemConstructorOptions;
export interface ShowAboutWindowOptions {
/**
An absolute path to the app icon.
Only used on Linux and Windows.
*/
readonly icon?: string;
/**
The copyright text.
*/
readonly copyright?: string;
/**
Some additional text if needed. Shown below copyright info.
*/
readonly text?: string;
/**
The URL to the app's website.
Only used on Linux.
*/
readonly website?: string;
/**
Customizable for localization. Used in the menu item label.
The app name is automatically appended at runtime.
Only used on Linux and Windows.
@default 'About'
*/
readonly title?: string;
}
/**
Shows an 'About' window. On macOS and Linux, the native 'About' window is shown, and on Windows, a simple custom dialog is shown.
On macOS, the `icon`, `text`, `title`, and `website` options are ignored.
_It will show `Electron` as the app name until you build your app for production._
@param options
@example
```
import {showAboutWindow} from 'electron-util';
showAboutWindow({
icon: path.join(__dirname, 'static/Icon.png'),
copyright: 'Copyright © Sindre Sorhus',
text: 'Some more info.'
});
```
*/
export function showAboutWindow(options: ShowAboutWindowOptions): void;
export interface AboutMenuItemOptions extends ShowAboutWindowOptions {}
/**
Accepts the same options as `.showAboutWindow()`.
@returns A `MenuItemConstructorOptions` that creates a menu item, which shows the about dialog when clicked.
@example
```
import {Menu} from 'electron';
import {aboutMenuItem} from 'electron-util';
const menu = Menu.buildFromTemplate([
{
label: 'Help',
submenu: [
aboutMenuItem({
icon: path.join(__dirname, 'static/Icon.png'),
copyright: 'Copyright © Sindre Sorhus',
text: 'Some more info.'
})
]
}
]);
Menu.setApplicationMenu(menu);
*/
export function aboutMenuItem(options?: AboutMenuItemOptions): MenuItemConstructorOptions;
/**
For example, use this in the `body` option of the `.openNewGitHubIssue()` method.
@returns A string with debug info suitable for inclusion in bug reports.
@example
```
import {debugInfo} from 'electron-util';
console.log(debugInfo());
//=> 'AppName 2.21.0\nElectron 3.0.6\ndarwin 18.2.0\nLocale: en-US'
```
*/
export function debugInfo(): string;
/**
Creating the [app menu](https://developer.apple.com/design/human-interface-guidelines/macos/menus/menu-bar-menus/) (the first menu) on macOS requires [a lot of boilerplate](https://github.com/sindresorhus/caprine/blob/5361289d1058b9463946f274cbfef587e6ad24a3/menu.js#L381-L431).
This method includes the default boilerplate and lets you add additional menu items in the correct place.
@param menuItems - Menu items to add below the `About App Name` menu item. Usually, you would add at least a `Preferences…` menu item.
@returns All menu items for the app menu.
@example
```
import {Menu} from 'electron';
import {appMenu} from 'electron-util';
const menu = Menu.buildFromTemplate([
appMenu([
{
label: 'Preferences…',
accelerator: 'Command+,',
click() {}
}
])
]);
Menu.setApplicationMenu(menu);
```
*/
export function appMenu(menuItems?: readonly MenuItemConstructorOptions[]): MenuItemConstructorOptions;
export interface SystemPreferencesMacOsPanes {
universalaccess:
| 'Captioning'
| 'Hearing'
| 'Keyboard'
| 'Media_Descriptions'
| 'Mouse'
| 'Seeing_Display'
| 'Seeing_VoiceOver'
| 'Seeing_Zoom'
| 'SpeakableItems'
| 'Switch';
security:
| 'Advanced'
| 'FDE'
| 'Firewall'
| 'General'
| 'Privacy'
| 'Privacy_Accessibility'
| 'Privacy_Advertising'
/**
Full Disk Access.
*/
| 'Privacy_AllFiles'
| 'Privacy_Assistive'
| 'Privacy_Automation'
| 'Privacy_Calendars'
| 'Privacy_Camera'
| 'Privacy_Contacts'
| 'Privacy_DesktopFolder'
| 'Privacy_Diagnostics'
| 'Privacy_DocumentsFolder'
| 'Privacy_DownloadsFolder'
| 'Privacy_LocationServices'
| 'Privacy_Microphone'
| 'Privacy_Photos'
| 'Privacy_Reminders'
| 'Privacy_ScreenCapture';
speech:
| 'Dictation'
| 'TTS';
sharing:
| 'Internet'
| 'Services_ARDService'
| 'Services_BluetoothSharing'
| 'Services_PersonalFileSharing'
| 'Services_PrinterSharing'
| 'Services_RemoteAppleEvent'
| 'Services_RemoteLogin'
| 'Services_ScreenSharing';
}
export type SystemPreferencesWindowsPane =
/**
System
*/
'display'
| 'sound' // Build 17063+
| 'notifications'
| 'quiethours' // Build 17074+
| 'powersleep'
| 'batterysaver'
| 'storagesense'
| 'tabletmode'
| 'multitasking'
| 'project'
| 'crossdevice'
| 'clipboard' // Build 17666+
| 'remotedesktop'
| 'about'
/**
Devices
*/
| 'bluetooth'
| 'connecteddevices'
| 'printers'
| 'mousetouchpad'
| 'devices-touchpad'
| 'typing'
| 'wheel'
| 'pen'
| 'autoplay'
| 'usb'
| 'mobile-devices' // Build 16251+
/**
Network & Internet
*/
| 'network'
| 'network-status'
| 'network-cellular'
| 'network-wifi'
| 'network-wificalling'
| 'network-ethernet'
| 'network-dialup'
| 'network-vpn'
| 'network-airplanemode'
| 'network-mobilehotspot'
| 'nfctransactions'
| 'datausage'
| 'network-proxy'
/**
Personalization
*/
| 'personalization'
| 'personalization-background'
| 'personalization-colors'
| 'lockscreen'
| 'themes'
| 'fonts' // Build 17083+
| 'personalization-start'
| 'taskbar'
/**
Apps
*/
| 'appsfeatures'
| 'optionalfeatures'
| 'defaultapps'
| 'maps'
| 'appsforwebsites'
| 'videoplayback' // Build 16215+
| 'startupapps' // Build 17017+
/**
Accounts
*/
| 'yourinfo'
| 'emailandaccounts'
| 'signinoptions'
| 'workplace'
| 'otherusers'
| 'sync'
/**
Time & language
*/
| 'dateandtime'
| 'regionformatting'
| 'regionlanguage'
| 'speech'
/**
Gaming
*/
| 'gaming-gamebar'
| 'gaming-gamedvr'
| 'gaming-broadcasting'
| 'gaming-gamemode'
| 'gaming-xboxnetworking' // Build 16226+
/**
Ease of Access
*/
| 'easeofaccess-display' // Build 17025+
| 'easeofaccess-cursorandpointersize' // Build 17040+
| 'easeofaccess-cursor'
| 'easeofaccess-magnifier'
| 'easeofaccess-colorfilter' // Build 17025+
| 'easeofaccess-highcontrast'
| 'easeofaccess-narrator'
| 'easeofaccess-audio' // Build 17035+
| 'easeofaccess-closedcaptioning'
| 'easeofaccess-speechrecognition' // Build 17035+
| 'easeofaccess-keyboard'
| 'easeofaccess-mouse'
| 'easeofaccess-eyecontrol' // Build 17035+
/**
Search & Cortana
*/
| 'search-permissions' // Version 1903+
| 'cortana-windowssearch' // Version 1903+
| 'cortana' // Build 16188+
| 'cortana-talktocortana' // Build 16188+
| 'cortana-permissions' // Build 16188+
/**
Privacy
*/
| 'privacy'
| 'privacy-speech'
| 'privacy-speechtyping'
| 'privacy-feedback'
| 'privacy-activityhistory' // Build 17040+
| 'privacy-location'
| 'privacy-webcam'
| 'privacy-microphone'
| 'privacy-voiceactivation'
| 'privacy-notifications'
| 'privacy-accountinfo'
| 'privacy-contacts'
| 'privacy-calendar'
| 'privacy-phonecalls'
| 'privacy-callhistory'
| 'privacy-email'
| 'privacy-eyetracker'
| 'privacy-tasks'
| 'privacy-messaging'
| 'privacy-radios'
| 'privacy-customdevices'
| 'privacy-backgroundapps'
| 'privacy-appdiagnostics'
| 'privacy-automaticfiledownloads'
| 'privacy-documents'
| 'privacy-pictures'
| 'privacy-videos'
| 'privacy-broadfilesystemaccess'
/**
Update & security
*/
| 'windowsupdate'
| 'delivery-optimization'
| 'windowsdefender'
| 'backup'
| 'troubleshoot'
| 'recovery'
| 'activation'
| 'findmydevice'
| 'developers'
| 'windowsinsider';
/**
Open the System Preferences on macOS and Windows 10.
This method does nothing on Linux.
On macOS, optionally provide a pane and section.
@example
```
import {openSystemPreferences} from 'electron-util';
openSystemPreferences();
// or
openSystemPreferences('security', 'Firewall');
```
@param pane - The pane to open.
@param section - The section within that pane.
@returns A Promise that resolves when the preferences window is opened.
*/
export function openSystemPreferences(pane?: SystemPreferencesWindowsPane): Promise<void>;
export function openSystemPreferences<T extends keyof SystemPreferencesMacOsPanes>(pane?: T, section?: SystemPreferencesMacOsPanes[T]): Promise<void>;