@netless/window-manager 0.2.15 → 0.2.19-canary.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.
@@ -1,73 +1,55 @@
1
- import { callbacks, WindowManager } from "./index";
2
- import { debounce } from "lodash";
3
- import { SET_SCENEPATH_DELAY } from "./constants";
1
+ import { Base } from './Base';
2
+ import { callbacks, WindowManager } from './index';
3
+ import { reaction, ViewVisionMode } from 'white-web-sdk';
4
+ import { SET_SCENEPATH_DELAY } from './constants';
5
+ import { TELE_BOX_STATE } from '@netless/telebox-insider';
4
6
  import {
5
7
  notifyMainViewModeChange,
6
8
  setScenePath,
7
9
  setViewFocusScenePath,
8
10
  setViewMode,
9
11
  } from "./Utils/Common";
10
- import { TELE_BOX_STATE } from "@netless/telebox-insider";
11
- import { ViewVisionMode, reaction } from "white-web-sdk";
12
- import type { Camera, Displayer, Size, View } from "white-web-sdk";
12
+ import type { Displayer, View } from "white-web-sdk";
13
13
  import type { AppManager } from "./AppManager";
14
14
  import type { CameraStore } from "./Utils/CameraStore";
15
15
 
16
- export class ViewManager {
17
- public mainView: View;
16
+ export class ViewManager extends Base {
18
17
  private views: Map<string, View> = new Map();
19
- private mainViewIsAddListener = false;
20
- private delegate = this.manager.delegate;
21
18
  private timer?: number;
19
+ private mainViewProxy = this.manager.mainViewProxy;
22
20
 
23
21
  constructor(
24
22
  private displayer: Displayer,
25
- private manager: AppManager,
23
+ manager: AppManager,
26
24
  private cameraStore: CameraStore
27
25
  ) {
28
- this.mainView = this.createMainView();
29
- this.addMainViewCameraListener();
26
+ super(manager);
30
27
  setTimeout(() => { // 延迟初始化 focus 的 reaction
31
28
  this.manager.refresher?.add("focus", () => {
32
29
  return reaction(
33
- () => this.manager.delegate.focus,
30
+ () => this.store.focus,
34
31
  focus => {
35
32
  if (focus) {
36
33
  this.switchAppToWriter(focus);
37
34
  } else {
38
35
  this.switchMainViewToWriter();
36
+ this.manager.boxManager.blurFocusBox();
39
37
  }
40
38
  },
41
39
  { fireImmediately: true }
42
40
  )
43
41
  });
44
- }, 100)
42
+ }, 100);
45
43
  }
46
44
 
47
45
  public get currentScenePath(): string {
48
46
  return this.displayer.state.sceneState.scenePath;
49
47
  }
50
48
 
51
- public createMainView(): View {
52
- const mainView = this.displayer.views.createView();
53
- this.cameraStore.setCamera("mainView", mainView.camera);
54
- mainView.callbacks.on("onSizeUpdated", () => {
55
- this.manager.boxManager.updateManagerRect();
56
- });
57
- const mainViewScenePath = this.manager.delegate.getMainViewScenePath();
58
- if (mainViewScenePath) {
59
- setViewFocusScenePath(mainView, mainViewScenePath);
60
- }
61
- if (!this.delegate.focus) {
62
- this.switchMainViewModeToWriter();
63
- }
64
- return mainView;
49
+ public get mainView(): View {
50
+ return this.mainViewProxy.view;
65
51
  }
66
52
 
67
- public setMainViewSize = debounce(size => {
68
- this.manager.delegate.setMainViewSize({ ...size });
69
- }, 200);
70
-
71
53
  public createView(appId: string): View {
72
54
  const view = this.displayer.views.createView();
73
55
  this.cameraStore.setCamera(appId, view.camera);
@@ -94,76 +76,20 @@ export class ViewManager {
94
76
  return this.views.get(appId);
95
77
  }
96
78
 
97
- private addMainViewCameraListener() {
98
- this.mainView.callbacks.on("onCameraUpdated", this.mainViewCameraListener);
99
- }
100
-
101
- private removeMainViewCameraListener() {
102
- this.mainView.callbacks.off("onCameraUpdated", this.mainViewCameraListener);
103
- }
104
-
105
- public switchMainViewToFreedom(): void {
106
- this.manager.delegate.setMainViewFocusPath();
107
- notifyMainViewModeChange(callbacks, ViewVisionMode.Freedom);
108
- setViewMode(this.mainView, ViewVisionMode.Freedom);
109
- }
110
-
111
- public switchMainViewModeToWriter(): void {
112
- if (!this.manager.canOperate) return;
113
- if (this.mainView) {
114
- if (this.mainView.mode === ViewVisionMode.Writable) return;
115
- notifyMainViewModeChange(callbacks, ViewVisionMode.Writable);
116
- setViewMode(this.mainView, ViewVisionMode.Writable);
117
- }
118
- }
119
-
120
- public addMainViewListener(): void {
121
- if (this.mainViewIsAddListener) return;
122
- if (this.mainView.divElement) {
123
- this.mainView.divElement.addEventListener("click", this.mainViewClickListener);
124
- this.mainView.divElement.addEventListener("touchend", this.mainViewClickListener);
125
- this.mainViewIsAddListener = true;
126
- }
127
- }
128
-
129
- public removeMainViewListener(): void {
130
- if (this.mainView.divElement) {
131
- this.mainView.divElement.removeEventListener("click", this.mainViewClickListener);
132
- this.mainView.divElement.removeEventListener("touchend", this.mainViewClickListener);
133
- }
134
- }
135
-
136
- private mainViewClickListener = () => {
137
- this.mainViewClickHandler();
138
- };
139
-
140
- public async mainViewClickHandler(): Promise<void> {
141
- if (!this.manager.canOperate) return;
142
- if (this.mainView.mode === ViewVisionMode.Writable) return;
143
- this.manager.delegate.cleanFocus();
144
- this.manager.boxManager.blurFocusBox();
145
- }
146
-
147
- private mainViewCameraListener = (camera: Camera) => {
148
- this.cameraStore.setCamera("mainView", camera);
149
- };
150
-
151
79
  public switchMainViewToWriter(): Promise<boolean> | undefined {
152
80
  if (this.timer) {
153
81
  clearTimeout(this.timer);
154
82
  }
155
83
  if (this.mainView.mode === ViewVisionMode.Writable) return;
84
+ this.freedomAllViews();
156
85
  return new Promise((resolve, reject) => {
157
86
  this.timer = window.setTimeout(() => {
158
87
  try {
159
- const mainViewScenePath = this.manager.delegate.getMainViewScenePath();
88
+ const mainViewScenePath = this.store.getMainViewScenePath();
160
89
  if (mainViewScenePath) {
161
90
  this.freedomAllViews();
162
- this.removeMainViewCameraListener();
163
91
  setScenePath(this.manager.room, mainViewScenePath);
164
- this.switchMainViewModeToWriter();
165
- this.manager.cameraStore.recoverCamera("mainView", this.mainView);
166
- this.addMainViewCameraListener();
92
+ this.mainViewProxy.switchViewModeToWriter();
167
93
  }
168
94
  resolve(true);
169
95
  } catch (error) {
@@ -174,25 +100,18 @@ export class ViewManager {
174
100
  }
175
101
 
176
102
  public refreshViews(): void {
177
- const focus = this.manager.delegate.focus;
103
+ const focus = this.store.focus;
178
104
  this.setMainViewFocusScenePath();
179
105
  if (focus) {
180
106
  const appProxy = this.manager.appProxies.get(focus);
181
- if (appProxy) {
182
- if (appProxy.view?.mode === ViewVisionMode.Writable) return;
183
- appProxy.removeCameraListener();
184
- appProxy.switchToWritable();
185
- appProxy.recoverCamera();
186
- appProxy.addCameraListener();
187
- }
107
+ appProxy?.switchToWritable();
188
108
  } else {
189
- if (this.manager.mainView.mode === ViewVisionMode.Writable) return;
190
109
  this.switchMainViewToWriter();
191
110
  }
192
111
  }
193
112
 
194
113
  public setMainViewFocusScenePath() {
195
- const mainViewScenePath = this.manager.delegate.getMainViewScenePath();
114
+ const mainViewScenePath = this.store.getMainViewScenePath();
196
115
  if (mainViewScenePath) {
197
116
  setViewFocusScenePath(this.manager.mainView, mainViewScenePath);
198
117
  }
@@ -209,8 +128,8 @@ export class ViewManager {
209
128
  notifyMainViewModeChange(callbacks, ViewVisionMode.Freedom);
210
129
  this.mainView.mode = ViewVisionMode.Freedom;
211
130
  }
212
- if (!this.manager.viewManager.mainView.focusScenePath) {
213
- this.manager.delegate.setMainViewFocusPath();
131
+ if (!this.mainView.focusScenePath) {
132
+ this.store.setMainViewFocusPath();
214
133
  }
215
134
  }
216
135
 
@@ -220,22 +139,19 @@ export class ViewManager {
220
139
  setTimeout(() => {
221
140
  const appProxy = this.manager.appProxies.get(id);
222
141
  if (appProxy) {
223
- const boxState = this.manager.delegate.getBoxState();
142
+ const boxState = this.store.getBoxState();
224
143
  if (boxState && boxState === TELE_BOX_STATE.Minimized) {
225
144
  return;
226
145
  }
227
- appProxy.removeCameraListener();
228
146
  appProxy.setScenePath();
229
147
  appProxy.switchToWritable();
230
- appProxy.recoverCamera();
231
- appProxy.addCameraListener();
232
148
  appProxy.focusBox();
233
149
  }
234
150
  }, SET_SCENEPATH_DELAY);
235
151
  }
236
152
 
237
153
  public destroy(): void {
238
- this.removeMainViewListener();
154
+ this.mainViewProxy.removeMainViewListener();
239
155
  if (WindowManager.wrapper) {
240
156
  WindowManager.wrapper.parentNode?.removeChild(WindowManager.wrapper);
241
157
  WindowManager.wrapper = undefined;