Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ulixee
GitHub Repository: ulixee/secret-agent
Path: blob/main/replay/backend/models/PlaybarView.ts
1030 views
1
import Window from '~backend/models/Window';
2
import Application from '~backend/Application';
3
import IRectangle from '~shared/interfaces/IRectangle';
4
import ReplayTabState from '~backend/api/ReplayTabState';
5
import ViewBackend from '~backend/models/ViewBackend';
6
7
export default class PlaybarView extends ViewBackend {
8
private readonly isReady: Promise<void>;
9
private tabState: ReplayTabState;
10
private isLoaded = false;
11
private playOnLoaded = false;
12
13
constructor(window: Window) {
14
super(window, {
15
sandbox: false,
16
nodeIntegration: true,
17
enableRemoteModule: true,
18
});
19
20
this.browserView.setAutoResize({
21
width: true,
22
height: false,
23
horizontal: false,
24
vertical: true,
25
});
26
27
const url = Application.instance.getPageUrl('playbar');
28
this.isReady = this.browserView.webContents.loadURL(url);
29
this.updateFrontendTicks = this.updateFrontendTicks.bind(this);
30
}
31
32
public async load(tabState: ReplayTabState) {
33
this.isLoaded = false;
34
this.attach();
35
36
// remove existing listeners
37
if (this.tabState) this.tabState.off('tick:changes', this.updateFrontendTicks);
38
39
this.tabState = tabState;
40
this.tabState.on('tick:changes', this.updateFrontendTicks);
41
42
await this.isReady;
43
44
this.browserView.webContents.send('ticks:load', this.tabState.getTickState());
45
this.isLoaded = true;
46
if (this.playOnLoaded) this.play();
47
}
48
49
public play() {
50
if (!this.isLoaded) {
51
this.playOnLoaded = true;
52
} else {
53
this.playOnLoaded = false;
54
this.browserView.webContents.send('start');
55
}
56
}
57
58
public pause() {
59
this.playOnLoaded = false;
60
this.browserView.webContents.send('pause');
61
}
62
63
public changeTickOffset(offset: number) {
64
this.browserView.webContents.send('ticks:change-offset', offset);
65
}
66
67
public onTickHover(rect: IRectangle, tickValue: number) {
68
if (!this.isAttached) return;
69
const tick = this.tabState.ticks.find(x => x.playbarOffsetPercent === tickValue);
70
if (!tick) return;
71
72
const bounds = this.browserView.getBounds();
73
// set rect y to the top of the playbar
74
rect.y = bounds.y;
75
const commandLabel = tick.label;
76
const commandResult =
77
tick.eventType === 'command'
78
? this.tabState.commandsById.get(tick.commandId)
79
: {
80
duration: 0,
81
};
82
Application.instance.overlayManager.show(
83
'command-overlay',
84
this.window.browserWindow,
85
rect,
86
commandLabel,
87
commandResult,
88
);
89
}
90
91
private updateFrontendTicks() {
92
this.browserView.webContents.send('ticks:updated', this.tabState.getTickState());
93
}
94
}
95
96