Repository navigation
Expand file tree
/
Copy pathsettings-debug-panel-launcher.component.ts
More file actions
212 lines (202 loc) · 7.66 KB
/
Copy pathsettings-debug-panel-launcher.component.ts
File metadata and controls
212 lines (202 loc) · 7.66 KB
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
import { DOCUMENT } from '@angular/common';
import {
Component,
computed,
effect,
inject,
Injector,
input,
OnInit,
signal,
untracked,
viewChild,
} from '@angular/core';
import { MatMenuModule, MatMenuTrigger } from '@angular/material/menu';
import { AsyncHandler } from 'libs/common/src/lib/async-handler.class';
import { HotkeysService } from 'libs/common/src/lib/hotkeys.service';
import { RemoteLoggingService } from 'libs/common/src/lib/remote-logging.service';
import { SettingsService } from 'libs/common/src/lib/settings.service';
import { type HashMap } from 'libs/common/src/lib/types';
import { user_group_names } from 'libs/common/src/lib/user-state';
import { installBindingDebugHooks } from './binding-debug-hooks';
import { BindingDebugPanelComponent } from './binding-debug-panel.component';
import { DebugConsoleComponent } from './debug-console.component';
import { IconComponent } from './icon.component';
import { SettingsDebugPanelComponent } from './settings-debug-panel.component';
type DebugPanel = 'settings' | 'bindings' | 'console';
@Component({
selector: 'settings-debug-panel-launcher',
host: { '(document:contextmenu)': 'onContextMenu($event)' },
imports: [
MatMenuModule,
SettingsDebugPanelComponent,
BindingDebugPanelComponent,
DebugConsoleComponent,
IconComponent,
],
template: `
@if (allowed()) {
<button
type="button"
class="absolute bottom-0 left-0 z-999 h-px w-px"
aria-label="Open debugging tools"
[matMenuTriggerFor]="debug_menu"
#menu_trigger="matMenuTrigger"
(contextmenu)="openMenu($event, menu_trigger)"
></button>
<mat-menu #debug_menu="matMenu" yPosition="above">
<div
class="flex w-64 items-center justify-center pb-2 text-sm opacity-60"
>
Debugging Panels
</div>
<button mat-menu-item (click)="openPanel('settings')">
<div class="flex items-center gap-2">
<icon>discover_tune</icon>
<div>Settings</div>
</div>
</button>
<button mat-menu-item (click)="openPanel('bindings')">
<div class="flex items-center gap-2">
<icon>linked_services</icon>
<div>Driver bindings</div>
</div>
</button>
<button mat-menu-item (click)="openPanel('console')">
<div class="flex items-center gap-2">
<icon>terminal_2</icon>
<div>Console</div>
</div>
</button>
</mat-menu>
@if (override_count()) {
<button
type="button"
class="bg-warning fixed bottom-1 left-1 z-999 rounded-sm px-2 py-0.5 text-xs text-black shadow"
(click)="openPanel('settings')"
>
{{ override_count() }} setting
{{ override_count() === 1 ? 'override' : 'overrides' }}
active
</button>
}
@defer (when panel() === 'settings') {
@if (panel() === 'settings') {
<settings-debug-panel
[show]="true"
(showChange)="panel.set(null)"
[schema]="schema()"
/>
}
}
@defer (when panel() === 'bindings') {
@if (panel() === 'bindings') {
<binding-debug-panel
[show]="true"
(showChange)="panel.set(null)"
[hotkeysEnabled]="false"
/>
}
}
@defer (when panel() === 'console') {
@if (panel() === 'console') {
<debug-console
[show]="true"
(showChange)="panel.set(null)"
[hotkeysEnabled]="false"
/>
}
}
}
`,
styles: [``],
})
export class SettingsDebugPanelLauncherComponent
extends AsyncHandler
implements OnInit
{
private readonly _hotkey = inject(HotkeysService);
private readonly _document = inject(DOCUMENT);
private readonly _injector = inject(Injector);
private readonly _menu_trigger = viewChild(MatMenuTrigger);
private readonly _settings = signal<SettingsService | null>(null);
private _schema_request?: Promise<void>;
public readonly loadSchema = input<() => Promise<HashMap>>();
public readonly schema = signal<HashMap | null>(null);
public readonly panel = signal<DebugPanel | null>(null);
/** Debug tools are only for PlaceOS admin and support users. */
public readonly allowed = computed(() => {
const groups = user_group_names();
return (
groups.includes('placeos_admin') ||
groups.includes('placeos_support')
);
});
/** Local setting overrides. They stay in this browser for later users. */
public readonly override_count = computed(
() => Object.keys(this._settings()?.debug_overrides() ?? {}).length,
);
constructor() {
super();
// Start capture when the user may open the panels, so the panels
// also show activity from before they first open. For other users
// the launcher starts no capture and keeps no data.
effect(() => {
if (!this.allowed()) return;
untracked(() => {
installBindingDebugHooks();
this._injector.get(RemoteLoggingService);
this._settings.set(this._injector.get(SettingsService));
});
});
}
public ngOnInit(): void {
const shortcuts: [DebugPanel, string[]][] = [
['settings', ['Control', 'Alt', 'Shift', 'KeyS']],
['bindings', ['Control', 'Alt', 'Shift', 'KeyB']],
['console', ['Control', 'Backquote']],
];
for (const [panel, keys] of shortcuts) {
this.subscription(
panel,
this._hotkey.listen(keys, () => {
if (!this.allowed()) return;
if (this.panel() === panel) this.panel.set(null);
else this.openPanel(panel);
}),
);
}
}
/** Observe the corner without placing a pointer target over app controls. */
public onContextMenu(event: MouseEvent): void {
if (!this.allowed()) return;
const height = this._document.documentElement.clientHeight;
if (
event.clientX < 0 ||
event.clientX > 32 ||
event.clientY < height - 32 ||
event.clientY > height
)
return;
const trigger = this._menu_trigger();
if (trigger) this.openMenu(event, trigger);
}
public openMenu(event: MouseEvent, trigger: MatMenuTrigger): void {
event.preventDefault();
trigger.openMenu();
}
public openPanel(panel: DebugPanel): void {
if (!this.allowed()) return;
this.panel.set(panel);
if (panel === 'settings') {
this._schema_request ??= this.loadSettingsSchema();
}
}
private async loadSettingsSchema(): Promise<void> {
try {
this.schema.set((await this.loadSchema()?.()) ?? null);
} catch {
this.schema.set(null);
}
}
}