hs2-mock 0.3.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +22 -5
- package/index.js +24 -0
- package/lib/application.js +17 -1
- package/lib/audiodevice.js +2 -0
- package/lib/ax.js +17 -2
- package/lib/bluetooth.js +94 -0
- package/lib/chooser.js +58 -0
- package/lib/eventtap.js +44 -0
- package/lib/http.js +75 -0
- package/lib/image.js +8 -2
- package/lib/menubar.js +17 -1
- package/lib/power.js +11 -1
- package/lib/ui.js +11 -1
- package/lib/userdefaults.js +14 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -28,21 +28,27 @@ Reading a member the mock lacks throws `hs2-mock: hs.x.y is not mocked`. Tests c
|
|
|
28
28
|
| `hs.appinfo` | `pid` |
|
|
29
29
|
| `hs.application` | `frontmost`, `fromPID`, `launchOrFocus`, `matchingBundleID`, `on`, `off` |
|
|
30
30
|
| `hs.audiodevice` | `allInputDevices`, `allOutputDevices`, `defaultInputDevice`, `defaultOutputDevice`, `on`, `off` |
|
|
31
|
+
| `hs.bluetooth` | `isScanning`, `on`, `off`, `scan`, `stopScan` |
|
|
32
|
+
| `hs.chooser` | `create` (`setChoices`, `show`, `hide`, `onSelect`, `onHide`) |
|
|
31
33
|
| `hs.ax` | `applicationElement`, `windowElement` |
|
|
32
34
|
| `hs.distributednotifications` | `on`, `off` |
|
|
35
|
+
| `hs.eventtap` | `keyStroke`, `leftClick` |
|
|
33
36
|
| `hs.fs` (in memory) | `exists`, `homeDirectory`, `mkdir`, `read`, `urlFromPath`, `write` |
|
|
34
37
|
| `hs.hotkey` | `bind`, `create` |
|
|
35
|
-
| `hs.
|
|
38
|
+
| `hs.http` | `openWebSocket` |
|
|
39
|
+
| `hs.menubar` | `create` (`title`, `setIcon`, `setTooltip`, `setClickCallback`, `setMenu`, `destroy`) |
|
|
40
|
+
| `hs.mouse` | `absolutePosition`, `setAbsolutePosition` |
|
|
36
41
|
| `hs.notify` | `show`, `create` (`send`) |
|
|
37
|
-
| `hs.permissions` | `checkAccessibility`, `checkLocation`, `requestLocation`, `requestNotifications` |
|
|
38
|
-
| `hs.power` | `on`, `off`
|
|
42
|
+
| `hs.permissions` | `checkAccessibility`, `checkBluetooth`, `requestBluetooth`, `checkLocation`, `requestLocation`, `requestNotifications` |
|
|
43
|
+
| `hs.power` | `lockScreen`, `on`, `off` |
|
|
39
44
|
| `hs.screen` | `main` |
|
|
40
45
|
| `hs.task` | `create`, `runAsync` |
|
|
41
46
|
| `hs.timer` | `absoluteTime`, `doAfter`, `doEvery`, `secondsSinceEpoch` |
|
|
42
47
|
| `hs.ui` | `alert`, `dialog`, `webview`, `window` |
|
|
43
48
|
| `hs.urlevent` | `openURL` |
|
|
49
|
+
| `hs.userdefaults` | `get`, `set`, `clear` |
|
|
44
50
|
| `hs.wifi` | `currentNetwork`, `on`, `off` |
|
|
45
|
-
| `HSImage`, `HSSize` globals | `fromName`, `fromSymbol` |
|
|
51
|
+
| `HSImage`, `HSSize` globals | `fromAppBundle`, `fromName`, `fromSymbol` |
|
|
46
52
|
|
|
47
53
|
## Controls (`ctl`)
|
|
48
54
|
|
|
@@ -52,6 +58,17 @@ Reading a member the mock lacks throws `hs2-mock: hs.x.y is not mocked`. Tests c
|
|
|
52
58
|
| `flush()` | Settles pending promises |
|
|
53
59
|
| `addApp({ bundleID, name, allWindows, focusedWindow, mainWindow })` | Adds a running app and returns it |
|
|
54
60
|
| `activate(bundleID)` | Brings an app to the front and emits `didActivate`; `null` for one without a bundle ID |
|
|
61
|
+
| `launch(options)`, `quit(bundleID)` | Starts or ends an app, emitting `didLaunch` or `didTerminate` |
|
|
62
|
+
| `keystrokes`, `clicks`, `mouse` | What `hs.eventtap` sent, and where the mouse is |
|
|
63
|
+
| `respondToKeystrokes(handler)`, `respondToClicks(handler)` | Runs as each is sent |
|
|
64
|
+
| `websockets` | `url`, `sent`, `readyState`, `destroyed`, `open()`, `message(text)`, `remoteClose()`, `error(text)` |
|
|
65
|
+
| `defaults` | What `hs.userdefaults` holds |
|
|
66
|
+
| `shownAlerts` | Each alert's `message`, `duration` and whether it was `closed` |
|
|
67
|
+
| `scans`, `discover({ identifier, name, rssi })`, `setBluetooth(on)` | BLE scans, advertisements and Bluetooth power |
|
|
68
|
+
| `bluetoothPermission` | Whether Bluetooth access is granted |
|
|
69
|
+
| `choosers` | `choices`, `visible`, `choose(text)`, `dismiss()` |
|
|
70
|
+
| `locks` | Times the screen was locked |
|
|
71
|
+
| `missingIcons` | Add the apps `HSImage.fromAppBundle` cannot load |
|
|
55
72
|
| `killed`, `launched`, `activations`, `raised` | Apps killed, launched and activated, and windows raised |
|
|
56
73
|
| `frontmostRequests` | Times Hammerspoon asked to be frontmost |
|
|
57
74
|
| `element({ role, children, attributes })` | An accessibility element |
|
|
@@ -60,7 +77,7 @@ Reading a member the mock lacks throws `hs2-mock: hs.x.y is not mocked`. Tests c
|
|
|
60
77
|
| `hotkeys` | Created hotkeys that have not been destroyed |
|
|
61
78
|
| `tasks` | Started tasks: `launchPath`, `args`, `environment`, `running`, `terminated`, `inputClosed`, `finish({ stdout, exitCode, reason, error })` |
|
|
62
79
|
| `respondToTasks(handler)` | Answers each task as it starts with what `finish()` takes, or `undefined` to leave it running |
|
|
63
|
-
| `menubarItems`
|
|
80
|
+
| `menubarItems` | Menu bar items: `item`, `icon`, `tooltip`, `menu`, `click()`, `destroyed` |
|
|
64
81
|
| `notifications` | `title`, `body`; `click()` for `show()` ones, `options` for `create()` ones |
|
|
65
82
|
| `dialogs` | `message`, `informativeText`, `buttons`, `open`, `press(label)` |
|
|
66
83
|
| `windows` | `rect`, `title`, `view.html`, `view.post(name, body)`, `shown`, `userClose()` |
|
package/index.js
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
const { createApplication } = require("./lib/application.js");
|
|
2
2
|
const { createAudiodevice } = require("./lib/audiodevice.js");
|
|
3
3
|
const { createAx } = require("./lib/ax.js");
|
|
4
|
+
const { createBluetooth } = require("./lib/bluetooth.js");
|
|
5
|
+
const { createChooser } = require("./lib/chooser.js");
|
|
4
6
|
const { createDialog } = require("./lib/dialog.js");
|
|
5
7
|
const {
|
|
6
8
|
createDistributedNotifications,
|
|
7
9
|
} = require("./lib/distributednotifications.js");
|
|
10
|
+
const { createEventtap } = require("./lib/eventtap.js");
|
|
8
11
|
const { createFs } = require("./lib/fs.js");
|
|
9
12
|
const { createHotkey } = require("./lib/hotkey.js");
|
|
13
|
+
const { createHttp } = require("./lib/http.js");
|
|
10
14
|
const { createImage } = require("./lib/image.js");
|
|
11
15
|
const { loadSpoon } = require("./lib/load.js");
|
|
12
16
|
const { createMenubar } = require("./lib/menubar.js");
|
|
@@ -17,6 +21,7 @@ const { strict } = require("./lib/strict.js");
|
|
|
17
21
|
const { createTask } = require("./lib/task.js");
|
|
18
22
|
const { createTimer } = require("./lib/timer.js");
|
|
19
23
|
const { createUi } = require("./lib/ui.js");
|
|
24
|
+
const { createUserdefaults } = require("./lib/userdefaults.js");
|
|
20
25
|
const { createWifi } = require("./lib/wifi.js");
|
|
21
26
|
const { createWindow } = require("./lib/window.js");
|
|
22
27
|
|
|
@@ -49,11 +54,17 @@ function createHs() {
|
|
|
49
54
|
const ui = createUi();
|
|
50
55
|
const wifi = createWifi(() => ctl.ssid);
|
|
51
56
|
const window = createWindow();
|
|
57
|
+
const bluetooth = createBluetooth(timer.module);
|
|
58
|
+
const chooser = createChooser();
|
|
59
|
+
const eventtap = createEventtap();
|
|
60
|
+
const http = createHttp();
|
|
61
|
+
const userdefaults = createUserdefaults();
|
|
52
62
|
Object.assign(globalThis, image.globals);
|
|
53
63
|
|
|
54
64
|
const ctl = merge(
|
|
55
65
|
{
|
|
56
66
|
accessibility: true,
|
|
67
|
+
bluetoothPermission: true,
|
|
57
68
|
location: true,
|
|
58
69
|
locationRequests: 0,
|
|
59
70
|
notificationPermission: true,
|
|
@@ -77,17 +88,29 @@ function createHs() {
|
|
|
77
88
|
),
|
|
78
89
|
);
|
|
79
90
|
|
|
91
|
+
merge(
|
|
92
|
+
ctl,
|
|
93
|
+
...[bluetooth, chooser, eventtap, http, userdefaults].map((m) => m.ctl),
|
|
94
|
+
);
|
|
95
|
+
|
|
80
96
|
const modules = {
|
|
81
97
|
appinfo: application.appinfo,
|
|
82
98
|
application: application.module,
|
|
83
99
|
audiodevice: audiodevice.module,
|
|
84
100
|
ax: ax.module,
|
|
101
|
+
bluetooth: bluetooth.module,
|
|
102
|
+
chooser: chooser.module,
|
|
85
103
|
distributednotifications: distributed.module,
|
|
104
|
+
eventtap: eventtap.eventtap,
|
|
86
105
|
fs: fs.module,
|
|
87
106
|
hotkey: hotkey.module,
|
|
107
|
+
http: http.module,
|
|
88
108
|
menubar: menubar.module,
|
|
109
|
+
mouse: eventtap.mouse,
|
|
89
110
|
notify: notify.module,
|
|
90
111
|
permissions: {
|
|
112
|
+
checkBluetooth: () => ctl.bluetoothPermission,
|
|
113
|
+
requestBluetooth: async () => ctl.bluetoothPermission,
|
|
91
114
|
checkAccessibility: () => ctl.accessibility,
|
|
92
115
|
checkLocation: () => ctl.location,
|
|
93
116
|
requestLocation: async () => {
|
|
@@ -102,6 +125,7 @@ function createHs() {
|
|
|
102
125
|
timer: timer.module,
|
|
103
126
|
ui: { ...ui.ui, ...dialog.module, ...window.module },
|
|
104
127
|
urlevent: ui.urlevent,
|
|
128
|
+
userdefaults: userdefaults.module,
|
|
105
129
|
wifi: wifi.module,
|
|
106
130
|
};
|
|
107
131
|
for (const [name, module] of Object.entries(modules)) {
|
package/lib/application.js
CHANGED
|
@@ -37,8 +37,8 @@ function createApplication() {
|
|
|
37
37
|
allWindows = [],
|
|
38
38
|
focusedWindow = null,
|
|
39
39
|
mainWindow = null,
|
|
40
|
+
axElement = element({ role: "AXApplication" }),
|
|
40
41
|
}) {
|
|
41
|
-
const axElement = element({ role: "AXApplication" });
|
|
42
42
|
const app = strict("HSApplication", {
|
|
43
43
|
bundleID,
|
|
44
44
|
name,
|
|
@@ -63,6 +63,20 @@ function createApplication() {
|
|
|
63
63
|
return app;
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
+
function launch(options) {
|
|
67
|
+
const app = addApp(options);
|
|
68
|
+
events.emit("didLaunch", app);
|
|
69
|
+
return app;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function quit(bundleID) {
|
|
73
|
+
const app = apps.get(bundleID);
|
|
74
|
+
if (!app) throw new Error(`hs2-mock: ${bundleID} is not running`);
|
|
75
|
+
apps.delete(bundleID);
|
|
76
|
+
if (frontmost === app) frontmost = null;
|
|
77
|
+
events.emit("didTerminate", app);
|
|
78
|
+
}
|
|
79
|
+
|
|
66
80
|
function activate(bundleID) {
|
|
67
81
|
frontmost =
|
|
68
82
|
apps.get(bundleID) ?? addApp({ bundleID, name: bundleID ?? "unbundled" });
|
|
@@ -71,6 +85,8 @@ function createApplication() {
|
|
|
71
85
|
|
|
72
86
|
const ctl = {
|
|
73
87
|
addApp,
|
|
88
|
+
launch,
|
|
89
|
+
quit,
|
|
74
90
|
activate,
|
|
75
91
|
activations,
|
|
76
92
|
killed,
|
package/lib/audiodevice.js
CHANGED
|
@@ -49,6 +49,7 @@ function createAudiodevice() {
|
|
|
49
49
|
output = !input,
|
|
50
50
|
volume = 0.5,
|
|
51
51
|
muted = false,
|
|
52
|
+
isInUse = false,
|
|
52
53
|
}) {
|
|
53
54
|
const events = createListeners("HSAudioDevice", DEVICE_EVENTS);
|
|
54
55
|
const device = strict("HSAudioDevice", {
|
|
@@ -59,6 +60,7 @@ function createAudiodevice() {
|
|
|
59
60
|
isOutput: output,
|
|
60
61
|
volume,
|
|
61
62
|
muted,
|
|
63
|
+
isInUse,
|
|
62
64
|
setDefaultOutputDevice() {
|
|
63
65
|
defaults.output = device;
|
|
64
66
|
return true;
|
package/lib/ax.js
CHANGED
|
@@ -1,10 +1,22 @@
|
|
|
1
1
|
const { inspect } = require("node:util");
|
|
2
2
|
const { strict } = require("./strict.js");
|
|
3
3
|
|
|
4
|
-
function element({
|
|
4
|
+
function element({
|
|
5
|
+
role,
|
|
6
|
+
title = null,
|
|
7
|
+
elementDescription = null,
|
|
8
|
+
position = null,
|
|
9
|
+
size = null,
|
|
10
|
+
children = [],
|
|
11
|
+
attributes = {},
|
|
12
|
+
}) {
|
|
5
13
|
const values = { ...attributes };
|
|
6
14
|
return strict("HSAXElement", {
|
|
7
15
|
role,
|
|
16
|
+
title,
|
|
17
|
+
elementDescription,
|
|
18
|
+
position,
|
|
19
|
+
size,
|
|
8
20
|
children: () => children,
|
|
9
21
|
attributeValue: (name) => values[name] ?? null,
|
|
10
22
|
setAttributeValue(name, value) {
|
|
@@ -22,8 +34,11 @@ const url = (text) => ({
|
|
|
22
34
|
|
|
23
35
|
function createAx() {
|
|
24
36
|
const raised = [];
|
|
25
|
-
|
|
37
|
+
let nextID = 1;
|
|
38
|
+
const window = (axElement, { id = nextID++, isStandard = true } = {}) => {
|
|
26
39
|
const self = strict("HSWindow", {
|
|
40
|
+
id,
|
|
41
|
+
isStandard,
|
|
27
42
|
axElement: () => axElement,
|
|
28
43
|
raise() {
|
|
29
44
|
raised.push(self);
|
package/lib/bluetooth.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
const { createEmitter } = require("./emitter.js");
|
|
2
|
+
const { strict } = require("./strict.js");
|
|
3
|
+
|
|
4
|
+
const OFF = "hs.bluetooth.scan(): Bluetooth is turned off";
|
|
5
|
+
|
|
6
|
+
/** hs.bluetooth BLE scanning. A scan always ends: after `seconds`, 10 by default. */
|
|
7
|
+
function createBluetooth(timer) {
|
|
8
|
+
const events = createEmitter();
|
|
9
|
+
const peripherals = new Map();
|
|
10
|
+
const scans = [];
|
|
11
|
+
let powered = true;
|
|
12
|
+
let active = null;
|
|
13
|
+
|
|
14
|
+
function end(error) {
|
|
15
|
+
const scan = active;
|
|
16
|
+
if (!scan) return;
|
|
17
|
+
active = null;
|
|
18
|
+
scan.timer.stop();
|
|
19
|
+
scan.record.done = true;
|
|
20
|
+
if (error) scan.reject(new Error(error));
|
|
21
|
+
else scan.resolve([...scan.found.values()]);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const module = {
|
|
25
|
+
scan({ seconds = 10, allowDuplicates = false } = {}) {
|
|
26
|
+
if (active)
|
|
27
|
+
return Promise.reject(
|
|
28
|
+
new Error("hs.bluetooth.scan(): a scan is already running"),
|
|
29
|
+
);
|
|
30
|
+
if (!powered) return Promise.reject(new Error(OFF));
|
|
31
|
+
const record = { seconds, allowDuplicates, done: false };
|
|
32
|
+
scans.push(record);
|
|
33
|
+
return new Promise((resolve, reject) => {
|
|
34
|
+
active = { record, resolve, reject, found: new Map() };
|
|
35
|
+
active.timer = timer.doAfter(seconds, () => end());
|
|
36
|
+
});
|
|
37
|
+
},
|
|
38
|
+
stopScan: () => end(),
|
|
39
|
+
get isScanning() {
|
|
40
|
+
return active !== null;
|
|
41
|
+
},
|
|
42
|
+
on: events.on,
|
|
43
|
+
off: events.off,
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
function peripheral(identifier) {
|
|
47
|
+
let entry = peripherals.get(identifier);
|
|
48
|
+
if (!entry) {
|
|
49
|
+
const state = { name: null, rssi: null, advertisement: null };
|
|
50
|
+
entry = {
|
|
51
|
+
state,
|
|
52
|
+
object: strict("HSBluetoothPeripheral", {
|
|
53
|
+
identifier,
|
|
54
|
+
get name() {
|
|
55
|
+
return state.name;
|
|
56
|
+
},
|
|
57
|
+
get rssi() {
|
|
58
|
+
return state.rssi;
|
|
59
|
+
},
|
|
60
|
+
get advertisement() {
|
|
61
|
+
return state.advertisement;
|
|
62
|
+
},
|
|
63
|
+
}),
|
|
64
|
+
};
|
|
65
|
+
peripherals.set(identifier, entry);
|
|
66
|
+
}
|
|
67
|
+
return entry;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** An advertisement. Returns false when no scan was there to hear it. */
|
|
71
|
+
function discover({ identifier, name = null, localName = name, rssi }) {
|
|
72
|
+
if (!active) return false;
|
|
73
|
+
const { state, object } = peripheral(identifier);
|
|
74
|
+
state.name = name;
|
|
75
|
+
// 127 means CoreBluetooth has no reading: HS2 keeps the last one.
|
|
76
|
+
if (rssi !== 127) state.rssi = rssi;
|
|
77
|
+
state.advertisement = { localName };
|
|
78
|
+
const repeat = active.found.has(identifier);
|
|
79
|
+
active.found.set(identifier, object);
|
|
80
|
+
if (!repeat || active.record.allowDuplicates)
|
|
81
|
+
events.emit("discovered", object);
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function setBluetooth(on) {
|
|
86
|
+
powered = on;
|
|
87
|
+
if (!on) end(OFF);
|
|
88
|
+
events.emit("powerChanged", on);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return { module, ctl: { scans, discover, setBluetooth } };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
module.exports = { createBluetooth };
|
package/lib/chooser.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
const { strict } = require("./strict.js");
|
|
2
|
+
|
|
3
|
+
/** hs.chooser, without rendering. As in HS2, it hides before onSelect is called. */
|
|
4
|
+
function createChooser() {
|
|
5
|
+
const choosers = [];
|
|
6
|
+
|
|
7
|
+
function create() {
|
|
8
|
+
let visible = false;
|
|
9
|
+
const record = {
|
|
10
|
+
choices: [],
|
|
11
|
+
get visible() {
|
|
12
|
+
return visible;
|
|
13
|
+
},
|
|
14
|
+
choose(text) {
|
|
15
|
+
const item = record.choices.find((choice) => choice.text === text);
|
|
16
|
+
if (!item) throw new Error(`hs2-mock: no choice '${text}'`);
|
|
17
|
+
select(item);
|
|
18
|
+
},
|
|
19
|
+
dismiss: () => select(null),
|
|
20
|
+
};
|
|
21
|
+
function hide() {
|
|
22
|
+
if (!visible) return chooser;
|
|
23
|
+
visible = false;
|
|
24
|
+
chooser.onHide?.();
|
|
25
|
+
return chooser;
|
|
26
|
+
}
|
|
27
|
+
function select(item) {
|
|
28
|
+
if (!visible) throw new Error("hs2-mock: the chooser is not showing");
|
|
29
|
+
hide();
|
|
30
|
+
chooser.onSelect?.(item);
|
|
31
|
+
}
|
|
32
|
+
const chooser = strict("HSChooser", {
|
|
33
|
+
placeholder: "Search...",
|
|
34
|
+
searchSubText: false,
|
|
35
|
+
onSelect: null,
|
|
36
|
+
onHide: null,
|
|
37
|
+
get isVisible() {
|
|
38
|
+
return visible;
|
|
39
|
+
},
|
|
40
|
+
setChoices(choices) {
|
|
41
|
+
record.choices = choices;
|
|
42
|
+
return chooser;
|
|
43
|
+
},
|
|
44
|
+
show() {
|
|
45
|
+
visible = true;
|
|
46
|
+
return chooser;
|
|
47
|
+
},
|
|
48
|
+
hide,
|
|
49
|
+
});
|
|
50
|
+
record.chooser = chooser;
|
|
51
|
+
choosers.push(record);
|
|
52
|
+
return chooser;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return { module: { create }, ctl: { choosers } };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
module.exports = { createChooser };
|
package/lib/eventtap.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
function createEventtap() {
|
|
2
|
+
const keystrokes = [];
|
|
3
|
+
const clicks = [];
|
|
4
|
+
let mouse = { x: 0, y: 0 };
|
|
5
|
+
let onKeystroke;
|
|
6
|
+
let onClick;
|
|
7
|
+
|
|
8
|
+
const eventtap = {
|
|
9
|
+
keyStroke(mods, key, app = null) {
|
|
10
|
+
const keystroke = { mods, key, app };
|
|
11
|
+
keystrokes.push(keystroke);
|
|
12
|
+
onKeystroke?.(keystroke);
|
|
13
|
+
},
|
|
14
|
+
leftClick(x, y) {
|
|
15
|
+
clicks.push({ x, y });
|
|
16
|
+
onClick?.({ x, y });
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
const mouseModule = {
|
|
20
|
+
absolutePosition: () => ({ ...mouse }),
|
|
21
|
+
setAbsolutePosition(x, y) {
|
|
22
|
+
mouse = { x, y };
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
const ctl = {
|
|
26
|
+
keystrokes,
|
|
27
|
+
clicks,
|
|
28
|
+
get mouse() {
|
|
29
|
+
return { ...mouse };
|
|
30
|
+
},
|
|
31
|
+
set mouse(position) {
|
|
32
|
+
mouse = { ...position };
|
|
33
|
+
},
|
|
34
|
+
respondToKeystrokes(handler) {
|
|
35
|
+
onKeystroke = handler;
|
|
36
|
+
},
|
|
37
|
+
respondToClicks(handler) {
|
|
38
|
+
onClick = handler;
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
return { eventtap, mouse: mouseModule, ctl };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
module.exports = { createEventtap };
|
package/lib/http.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
const { strict } = require("./strict.js");
|
|
2
|
+
|
|
3
|
+
const CONNECTING = 0;
|
|
4
|
+
const OPEN = 1;
|
|
5
|
+
const CLOSED = 3;
|
|
6
|
+
|
|
7
|
+
/** hs.http websockets, as HS2 1.0.0-beta.4 calls their callbacks back. */
|
|
8
|
+
function createHttp() {
|
|
9
|
+
const websockets = [];
|
|
10
|
+
|
|
11
|
+
function openWebSocket(url) {
|
|
12
|
+
const callbacks = {};
|
|
13
|
+
let readyState = CONNECTING;
|
|
14
|
+
const record = {
|
|
15
|
+
url,
|
|
16
|
+
sent: [],
|
|
17
|
+
destroyed: false,
|
|
18
|
+
get readyState() {
|
|
19
|
+
return readyState;
|
|
20
|
+
},
|
|
21
|
+
open() {
|
|
22
|
+
if (readyState !== CONNECTING) return;
|
|
23
|
+
readyState = OPEN;
|
|
24
|
+
callbacks.open?.();
|
|
25
|
+
},
|
|
26
|
+
message(text) {
|
|
27
|
+
if (readyState === OPEN) callbacks.message?.(text);
|
|
28
|
+
},
|
|
29
|
+
remoteClose(code = 1001, reason = "") {
|
|
30
|
+
if (readyState !== OPEN) return;
|
|
31
|
+
readyState = CLOSED;
|
|
32
|
+
callbacks.close?.(code, reason);
|
|
33
|
+
},
|
|
34
|
+
error(text) {
|
|
35
|
+
if (readyState === CLOSED) return;
|
|
36
|
+
readyState = CLOSED;
|
|
37
|
+
callbacks.error?.(text);
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
const set = (name) => (callback) => {
|
|
41
|
+
callbacks[name] = callback;
|
|
42
|
+
return socket;
|
|
43
|
+
};
|
|
44
|
+
const socket = strict("HSWebSocket", {
|
|
45
|
+
get readyState() {
|
|
46
|
+
return readyState;
|
|
47
|
+
},
|
|
48
|
+
setOpenCallback: set("open"),
|
|
49
|
+
setMessageCallback: set("message"),
|
|
50
|
+
setCloseCallback: set("close"),
|
|
51
|
+
setErrorCallback: set("error"),
|
|
52
|
+
send(message) {
|
|
53
|
+
if (readyState === OPEN) record.sent.push(message);
|
|
54
|
+
return socket;
|
|
55
|
+
},
|
|
56
|
+
close() {
|
|
57
|
+
if (readyState === CLOSED) return;
|
|
58
|
+
const wasOpen = readyState === OPEN;
|
|
59
|
+
readyState = CLOSED;
|
|
60
|
+
if (wasOpen) callbacks.close?.(1000, "Normal closure");
|
|
61
|
+
},
|
|
62
|
+
destroy() {
|
|
63
|
+
readyState = CLOSED;
|
|
64
|
+
record.destroyed = true;
|
|
65
|
+
for (const name of Object.keys(callbacks)) delete callbacks[name];
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
websockets.push(record);
|
|
69
|
+
return socket;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return { module: { openWebSocket }, ctl: { websockets } };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
module.exports = { createHttp };
|
package/lib/image.js
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
const { strict } = require("./strict.js");
|
|
2
2
|
|
|
3
|
-
/**
|
|
3
|
+
/**
|
|
4
|
+
* The HSImage and HSSize globals. fromName() only loads names in `images`;
|
|
5
|
+
* fromAppBundle() loads any app not in `missingIcons`.
|
|
6
|
+
*/
|
|
4
7
|
function createImage() {
|
|
5
8
|
const images = new Set();
|
|
9
|
+
const missingIcons = new Set();
|
|
6
10
|
class HSSize {
|
|
7
11
|
constructor(w, h) {
|
|
8
12
|
this.w = w;
|
|
@@ -14,8 +18,10 @@ function createImage() {
|
|
|
14
18
|
const HSImage = strict("HSImage", {
|
|
15
19
|
fromName: (name) => (images.has(name) ? image(name) : null),
|
|
16
20
|
fromSymbol: image,
|
|
21
|
+
fromAppBundle: (bundleID) =>
|
|
22
|
+
missingIcons.has(bundleID) ? null : image(bundleID),
|
|
17
23
|
});
|
|
18
|
-
return { globals: { HSImage, HSSize }, ctl: { images } };
|
|
24
|
+
return { globals: { HSImage, HSSize }, ctl: { images, missingIcons } };
|
|
19
25
|
}
|
|
20
26
|
|
|
21
27
|
module.exports = { createImage };
|
package/lib/menubar.js
CHANGED
|
@@ -4,7 +4,17 @@ function createMenubar() {
|
|
|
4
4
|
const menubarItems = [];
|
|
5
5
|
const module = {
|
|
6
6
|
create() {
|
|
7
|
-
|
|
7
|
+
let onClick = null;
|
|
8
|
+
const record = {
|
|
9
|
+
icon: null,
|
|
10
|
+
menu: null,
|
|
11
|
+
tooltip: null,
|
|
12
|
+
destroyed: false,
|
|
13
|
+
click(button = "left") {
|
|
14
|
+
if (!onClick) throw new Error("hs2-mock: no click callback is set");
|
|
15
|
+
onClick(button, []);
|
|
16
|
+
},
|
|
17
|
+
};
|
|
8
18
|
let title = null;
|
|
9
19
|
record.item = strict("HSMenuBarItem", {
|
|
10
20
|
get title() {
|
|
@@ -17,6 +27,12 @@ function createMenubar() {
|
|
|
17
27
|
setIcon(icon) {
|
|
18
28
|
record.icon = icon;
|
|
19
29
|
},
|
|
30
|
+
setTooltip(tooltip) {
|
|
31
|
+
record.tooltip = tooltip;
|
|
32
|
+
},
|
|
33
|
+
setClickCallback(callback) {
|
|
34
|
+
onClick = callback;
|
|
35
|
+
},
|
|
20
36
|
setMenu(menu) {
|
|
21
37
|
record.menu = menu;
|
|
22
38
|
},
|
package/lib/power.js
CHANGED
|
@@ -23,11 +23,21 @@ function createPower() {
|
|
|
23
23
|
throw new Error(`hs2-mock: unknown hs.power event '${event}'`);
|
|
24
24
|
return event;
|
|
25
25
|
};
|
|
26
|
+
let locks = 0;
|
|
26
27
|
const module = {
|
|
28
|
+
lockScreen() {
|
|
29
|
+
locks++;
|
|
30
|
+
},
|
|
27
31
|
on: (event, listener) => events.on(known(event), listener),
|
|
28
32
|
off: events.off,
|
|
29
33
|
};
|
|
30
|
-
|
|
34
|
+
const ctl = {
|
|
35
|
+
power: (event) => events.emit(known(event)),
|
|
36
|
+
get locks() {
|
|
37
|
+
return locks;
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
return { module, ctl };
|
|
31
41
|
}
|
|
32
42
|
|
|
33
43
|
module.exports = { createPower };
|
package/lib/ui.js
CHANGED
|
@@ -2,14 +2,24 @@ const { strict } = require("./strict.js");
|
|
|
2
2
|
|
|
3
3
|
function createUi() {
|
|
4
4
|
const alerts = [];
|
|
5
|
+
const shownAlerts = [];
|
|
5
6
|
const openedURLs = [];
|
|
6
7
|
const ui = {
|
|
7
8
|
alert(message) {
|
|
9
|
+
const record = { message, duration: 5, closed: false };
|
|
8
10
|
const alert = strict("HSUIAlert", {
|
|
11
|
+
duration(seconds) {
|
|
12
|
+
record.duration = seconds;
|
|
13
|
+
return alert;
|
|
14
|
+
},
|
|
9
15
|
show() {
|
|
10
16
|
alerts.push(message);
|
|
17
|
+
shownAlerts.push(record);
|
|
11
18
|
return alert;
|
|
12
19
|
},
|
|
20
|
+
close() {
|
|
21
|
+
record.closed = true;
|
|
22
|
+
},
|
|
13
23
|
});
|
|
14
24
|
return alert;
|
|
15
25
|
},
|
|
@@ -20,7 +30,7 @@ function createUi() {
|
|
|
20
30
|
return true;
|
|
21
31
|
},
|
|
22
32
|
};
|
|
23
|
-
return { ui, urlevent, ctl: { alerts, openedURLs } };
|
|
33
|
+
return { ui, urlevent, ctl: { alerts, shownAlerts, openedURLs } };
|
|
24
34
|
}
|
|
25
35
|
|
|
26
36
|
module.exports = { createUi };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
function createUserdefaults() {
|
|
2
|
+
const defaults = new Map();
|
|
3
|
+
const module = {
|
|
4
|
+
get: (key) =>
|
|
5
|
+
defaults.has(key) ? structuredClone(defaults.get(key)) : null,
|
|
6
|
+
set(key, value) {
|
|
7
|
+
defaults.set(key, structuredClone(value));
|
|
8
|
+
},
|
|
9
|
+
clear: (key) => defaults.delete(key),
|
|
10
|
+
};
|
|
11
|
+
return { module, ctl: { defaults } };
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
module.exports = { createUserdefaults };
|