@netless/telebox-insider 0.3.0-beta.4 → 0.3.0-beta.6

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.
@@ -30,6 +30,7 @@ import {
30
30
  ValEnhancedResult,
31
31
  withValueEnhancer,
32
32
  } from "value-enhancer";
33
+ import { AppMenu } from "../AppMenu";
33
34
 
34
35
  export * from "./typings";
35
36
  export * from "./constants";
@@ -49,6 +50,18 @@ export interface TeleBoxManager extends ValEnhancedResult<ValConfig> {}
49
50
  export class TeleBoxManager {
50
51
  private boxesStatus$: Map<string, TeleBoxState>;
51
52
  private lastNotMinimizedBoxesStatus$: Map<string, NotMinimizedBoxState>;
53
+ private appMenu: AppMenu | undefined;
54
+ private useBoxesStatus: boolean;
55
+ private effectBoxesStatusTimer?: {
56
+ skipUpdate: boolean;
57
+ timer: number | undefined;
58
+ resolve?: (skipUpdate: boolean) => void;
59
+ }
60
+ private effectLastNotMinimizedBoxStatusTimer?: {
61
+ skipUpdate: boolean;
62
+ timer: number | undefined;
63
+ resolve?: (skipUpdate: boolean) => void;
64
+ }
52
65
 
53
66
  public constructor({
54
67
  root = document.body,
@@ -65,8 +78,10 @@ export class TeleBoxManager {
65
78
  collector,
66
79
  namespace = "telebox",
67
80
  readonly = false,
81
+ useBoxesStatus = false,
68
82
  }: TeleBoxManagerConfig = {}) {
69
83
  this._sideEffect = new SideEffectManager();
84
+ this.useBoxesStatus = useBoxesStatus;
70
85
  const { combine, createVal } = createSideEffectBinder(this._sideEffect);
71
86
 
72
87
  this.root = root;
@@ -75,12 +90,19 @@ export class TeleBoxManager {
75
90
  this.boxes$ = createVal<TeleBox[]>([]);
76
91
  this.topBox$ = this.boxes$.derive((boxes) => {
77
92
  if (boxes.length > 0) {
78
- const topBox = boxes.reduce((topBox, box) => {
93
+ console.log("topBox===>topBox$===>000");
94
+ let currentTopBox: TeleBox | undefined = undefined;
95
+ const topBox = boxes.reduce((topBox, box) => {
79
96
  if (box.boxStatus && box.boxStatus === TELE_BOX_STATE.Minimized) {
80
97
  return topBox;
81
98
  }
99
+ if (!topBox) {
100
+ topBox = box;
101
+ return topBox;
102
+ }
82
103
  return topBox.zIndex > box.zIndex ? topBox : box;
83
- });
104
+ }, currentTopBox as (TeleBox | undefined));
105
+ console.log("topBox===>topBox$===>end", topBox?.id);
84
106
  return topBox;
85
107
  }
86
108
  return;
@@ -181,7 +203,7 @@ export class TeleBoxManager {
181
203
  new TeleBoxCollector({
182
204
  visible: minimized,
183
205
  readonly: readonly,
184
- namespace,
206
+ namespace
185
207
  }).mount(root)
186
208
  );
187
209
  collector$.subscribe((collector) => {
@@ -190,20 +212,16 @@ export class TeleBoxManager {
190
212
  collector.setReadonly(readonly$.value);
191
213
  collector.setDarkMode(this._darkMode$.value);
192
214
  this._sideEffect.add(() => {
215
+ collector.getBoxesStatus = () => {
216
+ return this.boxesStatus$;
217
+ };
193
218
  collector.onClick = () => {
194
219
  if (!readonly$.value) {
195
- if (this.hasMinimizedBox()) {
196
- this.getAllMinimizedBoxes().forEach((box) => {
197
- const lastNotMinimizedBoxStatus = box.lastNotMinimizedBoxStatus;
198
- if (lastNotMinimizedBoxStatus) {
199
- this.setBoxStatus(box.id, lastNotMinimizedBoxStatus);
200
- } else {
201
- this.setBoxStatus(box.id, TELE_BOX_STATE.Normal);
202
- }
203
- });
204
- this.effectBoxStatusChange();
220
+ if (!this.useBoxesStatus) {
221
+ minimized$.setValue(false);
222
+ } else if (this.useBoxesStatus && this.appMenu) {
223
+ this.appMenu.containerClickHandler();
205
224
  }
206
- minimized$.setValue(false);
207
225
  }
208
226
  };
209
227
  return () => collector.destroy();
@@ -233,6 +251,14 @@ export class TeleBoxManager {
233
251
  return;
234
252
  };
235
253
 
254
+ this._setCollectorRect = (collectorRect: TeleBoxRect | undefined) => {
255
+ if (collector$.value?.$collector) {
256
+ collectorRect$.setValue(collectorRect);
257
+ } else if (import.meta.env.DEV) {
258
+ console.warn("No collector for minimized boxes.");
259
+ }
260
+ };
261
+
236
262
  const collectorRect$ = createVal(
237
263
  minimized$.value ? this._calcCollectorRect() : void 0
238
264
  );
@@ -246,11 +272,7 @@ export class TeleBoxManager {
246
272
  collector$.value?.setVisible(minimized);
247
273
 
248
274
  if (minimized) {
249
- if (collector$.value?.$collector) {
250
- collectorRect$.setValue(this._calcCollectorRect());
251
- } else if (import.meta.env.DEV) {
252
- console.warn("No collector for minimized boxes.");
253
- }
275
+ this._setCollectorRect(this._calcCollectorRect());
254
276
  }
255
277
 
256
278
  this.boxes.forEach((box) =>
@@ -286,10 +308,28 @@ export class TeleBoxManager {
286
308
  if (id) {
287
309
  const box = this.getBox(id);
288
310
  if (box) {
289
- this.focusBox(box);
290
- if ((box.boxStatus && box.boxStatus !== TELE_BOX_STATE.Maximized) || !box.boxStatus) {
311
+ if (!this.useBoxesStatus) {
291
312
  this.makeBoxTop(box);
292
- }
313
+ } else {
314
+ if (box.boxStatus && box.boxStatus === TELE_BOX_STATE.Normal) {
315
+ const maxNormalBoxZIndex = this.getMaxNormalBoxZIndex();
316
+ if (maxNormalBoxZIndex > box.zIndex) {
317
+ this.setBox(box, {
318
+ zIndex: maxNormalBoxZIndex + 1
319
+ }, false);
320
+ }
321
+ } else if(box.boxStatus && box.boxStatus === TELE_BOX_STATE.Maximized){
322
+ const maxMaximizedBoxZIndex = this.getMaxMaximizedBoxZIndex();
323
+ if (maxMaximizedBoxZIndex > box.zIndex) {
324
+ this.setBox(box, {
325
+ zIndex: maxMaximizedBoxZIndex + 1
326
+ }, false);
327
+ }
328
+ this.makeMaximizedTopBoxFocus();
329
+ }
330
+ }
331
+ console.log("checkFocusBox===>", box.id);
332
+ this.focusBox(box);
293
333
  return;
294
334
  }
295
335
  }
@@ -322,29 +362,79 @@ export class TeleBoxManager {
322
362
  onEvent: (event): void => {
323
363
  switch (event.type) {
324
364
  case TELE_BOX_DELEGATE_EVENT.Maximize: {
325
- if (this.hasBoxesStatus() && this.maxTitleBar.focusedBox?.id) {
326
- this.setBoxStatus(this.maxTitleBar.focusedBox.id, TELE_BOX_STATE.Normal);
327
- this.effectBoxStatusChange();
328
- this.makeMaximizedTopBoxFocus();
365
+ if (this.hasBoxesStatus()) {
366
+ let targetBox = this.maxTitleBar.focusedBox;
367
+ if (!targetBox) {
368
+ targetBox = this.getMaximizedTopBox();
369
+ }
370
+ if (targetBox?.id) {
371
+ const box = this.getBox(targetBox.id);
372
+ if (box) {
373
+ this.setBox(box, {
374
+ status: TELE_BOX_STATE.Normal,
375
+ zIndex: this.getMaxNormalBoxZIndex() + 1
376
+ }, false, ()=>{
377
+ this.makeMaximizedTopBoxFocus();
378
+ });
379
+ }
380
+ }
329
381
  } else {
330
382
  maximized$.setValue(!maximized$.value);
331
383
  }
332
384
  break;
333
385
  }
334
386
  case TELE_BOX_DELEGATE_EVENT.Minimize: {
335
- if (this.hasBoxesStatus() && this.maxTitleBar.focusedBox?.id) {
336
- this.setBoxStatus(this.maxTitleBar.focusedBox.id, TELE_BOX_STATE.Minimized);
337
- this.effectBoxStatusChange();
338
- this.makeMaximizedTopBoxFocus();
387
+ if (this.hasBoxesStatus()) {
388
+ let targetBox = this.maxTitleBar.focusedBox;
389
+ if (!targetBox) {
390
+ targetBox = this.getMaximizedTopBox();
391
+ }
392
+ if(targetBox?.id){
393
+ const box = this.getBox(targetBox.id);
394
+ if (box) {
395
+ let isFocusChanged = false;
396
+ if (this.topBox === box) {
397
+ this.blurBox(box);
398
+ isFocusChanged = true;
399
+ }
400
+ this.setBox(box, {
401
+ status: TELE_BOX_STATE.Minimized,
402
+ zIndex: -1
403
+ }, false);
404
+ if (isFocusChanged) {
405
+ this.focusTopBox();
406
+ } else {
407
+ this.makeMaximizedTopBoxFocus();
408
+ }
409
+ }
410
+ }
339
411
  } else {
340
412
  minimized$.setValue(true);
341
413
  }
342
414
  break;
343
415
  }
344
416
  case TELE_BOX_EVENT.Close: {
345
- if (this.hasBoxesStatus() && this.maxTitleBar.focusedBox?.id) {
346
- this.remove(this.maxTitleBar.focusedBox.id);
347
- this.makeMaximizedTopBoxFocus();
417
+ if (this.hasBoxesStatus()) {
418
+ let targetBox = this.maxTitleBar.focusedBox;
419
+ if (!targetBox) {
420
+ targetBox = this.getMaximizedTopBox();
421
+ }
422
+ if (targetBox?.id) {
423
+ const box = this.getBox(targetBox.id);
424
+ if (box) {
425
+ let isFocusChanged = false;
426
+ if (this.topBox === box) {
427
+ this.blurBox(box);
428
+ isFocusChanged = true;
429
+ }
430
+ this.remove(box, false);
431
+ if (isFocusChanged) {
432
+ this.focusTopBox();
433
+ } else {
434
+ this.makeMaximizedTopBoxFocus();
435
+ }
436
+ }
437
+ }
348
438
  } else {
349
439
  this.removeTopBox();
350
440
  this.focusTopBox();
@@ -383,6 +473,10 @@ export class TeleBoxManager {
383
473
  this._state$ = state$;
384
474
 
385
475
  this.root.appendChild(this.maxTitleBar.render());
476
+
477
+ if (useBoxesStatus) {
478
+ this.appMenu = this.createMinimizedAppMenu();
479
+ }
386
480
  }
387
481
 
388
482
  public get boxes(): ReadonlyArray<TeleBox> {
@@ -405,6 +499,8 @@ export class TeleBoxManager {
405
499
 
406
500
  public _calcCollectorRect: () => TeleBoxRect | undefined;
407
501
 
502
+ public _setCollectorRect: (collectorRect: TeleBoxRect | undefined) => void;
503
+
408
504
  public get darkMode(): boolean {
409
505
  return this._darkMode$.value;
410
506
  }
@@ -443,15 +539,12 @@ export class TeleBoxManager {
443
539
  this.boxesStatus$.set(boxId, boxStatus);
444
540
  if (boxStatus === TELE_BOX_STATE.Minimized && oldBoxStatus !== TELE_BOX_STATE.Minimized) {
445
541
  this.setLastNotMinimizedBoxStatus(boxId, oldBoxStatus, skipUpdate);
446
- this.effectLastNotMinimizedBoxStatusChange();
447
542
  } else if (oldBoxStatus === TELE_BOX_STATE.Minimized && boxStatus !== TELE_BOX_STATE.Minimized) {
448
543
  this.setLastNotMinimizedBoxStatus(boxId, undefined, skipUpdate);
449
- this.effectLastNotMinimizedBoxStatusChange();
450
544
  }
451
545
  } else {
452
546
  this.boxesStatus$.delete(boxId);
453
547
  this.setLastNotMinimizedBoxStatus(boxId, undefined, skipUpdate);
454
- this.effectLastNotMinimizedBoxStatusChange();
455
548
  }
456
549
  if (!skipUpdate) {
457
550
  this.events.emit(TELE_BOX_MANAGER_EVENT.BoxStatus, { id: boxId, boxStatus});
@@ -462,10 +555,6 @@ export class TeleBoxManager {
462
555
  return this.boxesStatus$.get(boxId);
463
556
  }
464
557
 
465
- public getLastNotMinimizedBoxStatus(boxId: string): NotMinimizedBoxState | undefined {
466
- return this.lastNotMinimizedBoxesStatus$.get(boxId);
467
- }
468
-
469
558
  public get boxesStatus(): ReadonlyMap<string, TeleBoxState> {
470
559
  return this.boxesStatus$;
471
560
  }
@@ -474,55 +563,164 @@ export class TeleBoxManager {
474
563
  return this.lastNotMinimizedBoxesStatus$;
475
564
  }
476
565
 
477
- public setBoxesStatus(boxesStatus: Map<string, TeleBoxState>, skipUpdate = false): void {
566
+ public async setBoxesStatus(boxesStatus: Map<string, TeleBoxState>, skipUpdate = false): Promise<void> {
478
567
  this.boxesStatus$ = boxesStatus;
479
- this.effectBoxStatusChange(skipUpdate);
568
+ this.effectBoxesStatusChange(skipUpdate);
569
+ }
570
+
571
+ private effectBoxStatusChange(){
572
+ if (!this.useBoxesStatus) {
573
+ return;
574
+ }
575
+ if(this.collector){
576
+ this.collector.updateBoxesStatus();
577
+ if(this.hasMinimizedBox()){
578
+ this._setCollectorRect(this._calcCollectorRect());
579
+ }
580
+ }
581
+ if (this.appMenu) {
582
+ this.appMenu.appMenuChange();
583
+ }
584
+ if (this.maxTitleBar) {
585
+ this.maxTitleBar.updateBoxesStatus();
586
+ }
480
587
  }
481
588
 
482
- private effectBoxStatusChange(skipUpdate = false){
483
- let topBox:TeleBox | undefined;
484
- const topBoxId = this.topBox?.id;
485
- let hasMinimizedBox = false;
589
+ private effectBoxesStatusTimerFunc(skipUpdate: boolean){
590
+ if (!this.useBoxesStatus) {
591
+ return;
592
+ }
593
+ let newTopBox:TeleBox | undefined;
594
+ const curTopBox = this.topBox;
486
595
  this.boxes.forEach((box) => {
487
596
  const boxStatus = this.boxesStatus$.get(box.id);
488
597
  if (boxStatus) {
489
- if (boxStatus !== TELE_BOX_STATE.Minimized && box.zIndex > (topBox?.zIndex ?? 0)) {
490
- topBox = box;
598
+ if (boxStatus !== TELE_BOX_STATE.Minimized && box.zIndex > (newTopBox?.zIndex ?? 0)) {
599
+ newTopBox = box;
491
600
  }
492
601
  box.setBoxStatus(boxStatus, skipUpdate);
493
- if (boxStatus === TELE_BOX_STATE.Minimized) {
494
- hasMinimizedBox = true;
495
- }
496
602
  }
497
603
  });
498
- if (hasMinimizedBox) {
499
- this.collector?.setVisible(true);
500
- if (this.collector?.$collector) {
501
- this.setCollectorRect(this._calcCollectorRect());
502
- } else if (import.meta.env.DEV) {
503
- console.warn("No collector for minimized boxes.");
604
+ this.effectBoxStatusChange();
605
+ if (newTopBox && curTopBox !== newTopBox) {
606
+ this.topBox$.setValue(newTopBox);
607
+ this.focusBox(newTopBox, skipUpdate);
608
+ }
609
+ if (!newTopBox && curTopBox) {
610
+ this.topBox$.setValue(undefined);
611
+ this.blurBox(curTopBox, skipUpdate);
612
+ }
613
+ }
614
+
615
+ public async effectBoxesStatusChange(skipUpdate = false): Promise<void> {
616
+ if (!this.useBoxesStatus) {
617
+ if (this.effectBoxesStatusTimer && this.effectBoxesStatusTimer.timer) {
618
+ clearTimeout(this.effectBoxesStatusTimer.timer);
619
+ if (this.effectBoxesStatusTimer.resolve) {
620
+ this.effectBoxesStatusTimer.resolve(!!this.effectBoxesStatusTimer?.skipUpdate);
621
+ }
504
622
  }
505
- } else {
506
- this.collector?.setVisible(false);
623
+ this.effectBoxesStatusTimer=undefined;
624
+ return;
507
625
  }
508
- if (topBox && topBoxId !== topBox.id) {
509
- this.makeBoxTop(topBox, skipUpdate);
510
- this.topBox$.setValue(topBox);
511
- this.focusTopBox();
626
+ if (this.effectBoxesStatusTimer && this.effectBoxesStatusTimer.timer) {
627
+ if (this.effectBoxesStatusTimer.skipUpdate !== skipUpdate) {
628
+ clearTimeout(this.effectBoxesStatusTimer.timer);
629
+ if (this.effectBoxesStatusTimer.resolve) {
630
+ console.log("effectBoxStatusTimerFunc===>1111", skipUpdate);
631
+ this.effectBoxesStatusTimer.resolve(!!this.effectBoxesStatusTimer?.skipUpdate);
632
+ }
633
+ this.effectBoxesStatusTimer.skipUpdate = skipUpdate;
634
+ console.log("effectBoxStatusTimerFunc===>2222", skipUpdate);
635
+ } else {
636
+ return;
637
+ }
512
638
  }
513
- if (this.maxTitleBar) {
514
- this.maxTitleBar.updateBoxesStatus();
639
+ if (!this.effectBoxesStatusTimer) {
640
+ this.effectBoxesStatusTimer = {
641
+ skipUpdate,
642
+ timer: undefined,
643
+ resolve: undefined,
644
+ } as {
645
+ skipUpdate: boolean;
646
+ timer: number | undefined;
647
+ }
648
+ }
649
+ if (this.effectBoxesStatusTimer) {
650
+ const _skipUpdate = await new Promise<boolean>((resolve)=>{
651
+ this.effectBoxesStatusTimer!.resolve = resolve;
652
+ this.effectBoxesStatusTimer!.timer = setTimeout(()=>{
653
+ if (this.effectBoxesStatusTimer && this.effectBoxesStatusTimer.resolve) {
654
+ console.log("effectBoxStatusTimerFunc===>000", skipUpdate, {id:this.topBox$.value?.id, zIndex:this.topBox$.value?.zIndex}, this.boxes$.value.map((b)=>({id:b.id,focus:b.focus, boxStatus:b.boxStatus,zIndex:b.zIndex})));
655
+ this.effectBoxesStatusTimer.resolve(this.effectBoxesStatusTimer!.skipUpdate);
656
+ }
657
+ }, 100) as unknown as number;
658
+ })
659
+ console.log("effectBoxStatusTimerFunc===>000==>00", skipUpdate, {id:this.topBox$.value?.id, zIndex:this.topBox$.value?.zIndex}, this.boxes$.value.map((b)=>({id:b.id,focus:b.focus, boxStatus:b.boxStatus,zIndex:b.zIndex})));
660
+ this.effectBoxesStatusTimerFunc(_skipUpdate)
661
+ this.effectBoxesStatusTimer = undefined;
662
+ console.log("effectBoxStatusTimerFunc===>000==>11", skipUpdate, {id:this.topBox$.value?.id, zIndex:this.topBox$.value?.zIndex}, this.boxes$.value.map((b)=>({id:b.id,focus:b.focus, boxStatus:b.boxStatus,zIndex:b.zIndex})));
515
663
  }
516
664
  }
517
665
 
518
666
  public hasBoxesStatus(): boolean {
667
+ if (!this.useBoxesStatus) {
668
+ return false;
669
+ }
519
670
  return this.boxesStatus$.size > 0;
520
671
  }
521
672
 
522
673
  public hasMinimizedBox(): boolean {
674
+ if (!this.useBoxesStatus) {
675
+ return false;
676
+ }
523
677
  return this.boxes.some((box) => box.boxStatus && box.boxStatus === TELE_BOX_STATE.Minimized);
524
678
  }
525
679
 
680
+ public getMinimizedBoxesStatus(): Map<string, TELE_BOX_STATE.Minimized> {
681
+ const minimizedBoxes = new Map<string, TELE_BOX_STATE.Minimized>();
682
+ this.boxesStatus$.forEach((boxStatus, boxId) => {
683
+ if (boxStatus === TELE_BOX_STATE.Minimized) {
684
+ minimizedBoxes.set(boxId, boxStatus as TELE_BOX_STATE.Minimized);
685
+ }
686
+ });
687
+ return minimizedBoxes;
688
+ }
689
+
690
+ public getNotMinimizedBoxesStatus(): Map<string, NotMinimizedBoxState> {
691
+ const notMinimizedBoxes = new Map<string, NotMinimizedBoxState>();
692
+ this.boxesStatus$.forEach((boxStatus, boxId) => {
693
+ if (boxStatus !== TELE_BOX_STATE.Minimized) {
694
+ notMinimizedBoxes.set(boxId, boxStatus as NotMinimizedBoxState);
695
+ }
696
+ });
697
+ return notMinimizedBoxes;
698
+ }
699
+
700
+ public getAllMaximizedBoxesStatus(): Map<string, TELE_BOX_STATE.Maximized> {
701
+ const maximizedBoxes = new Map<string, TELE_BOX_STATE.Maximized>();
702
+ this.boxesStatus$.forEach((boxStatus, boxId) => {
703
+ if (boxStatus === TELE_BOX_STATE.Maximized) {
704
+ maximizedBoxes.set(boxId, boxStatus as TELE_BOX_STATE.Maximized);
705
+ }
706
+ });
707
+ return maximizedBoxes;
708
+ }
709
+
710
+ public getAllNormalBoxesStatus(): Map<string, TELE_BOX_STATE.Normal> {
711
+ const normalBoxes = new Map<string, TELE_BOX_STATE.Normal>();
712
+ this.boxesStatus$.forEach((boxStatus, boxId) => {
713
+ if (boxStatus === TELE_BOX_STATE.Normal) {
714
+ normalBoxes.set(boxId, boxStatus as TELE_BOX_STATE.Normal);
715
+ }
716
+ });
717
+ return normalBoxes;
718
+ }
719
+
720
+ public getLastNotMinimizedBoxStatus(boxId: string): NotMinimizedBoxState | undefined {
721
+ return this.lastNotMinimizedBoxesStatus$.get(boxId);
722
+ }
723
+
526
724
  public setLastNotMinimizedBoxStatus(boxId: string, lastNotMinimizedBoxStatus?: NotMinimizedBoxState, skipUpdate = false): void {
527
725
  if (lastNotMinimizedBoxStatus) {
528
726
  this.lastNotMinimizedBoxesStatus$.set(boxId, lastNotMinimizedBoxStatus);
@@ -539,23 +737,117 @@ export class TeleBoxManager {
539
737
  this.effectLastNotMinimizedBoxStatusChange(skipUpdate);
540
738
  }
541
739
 
542
- private effectLastNotMinimizedBoxStatusChange(skipUpdate = false){
740
+ private async effectLastNotMinimizedBoxStatusChange(skipUpdate = false): Promise<void> {
741
+ if (!this.useBoxesStatus) {
742
+ if (this.effectLastNotMinimizedBoxStatusTimer && this.effectLastNotMinimizedBoxStatusTimer.timer) {
743
+ clearTimeout(this.effectLastNotMinimizedBoxStatusTimer.timer);
744
+ if (this.effectLastNotMinimizedBoxStatusTimer.resolve) {
745
+ this.effectLastNotMinimizedBoxStatusTimer.resolve(!!this.effectLastNotMinimizedBoxStatusTimer?.skipUpdate);
746
+ }
747
+ }
748
+ this.effectLastNotMinimizedBoxStatusTimer=undefined;
749
+ return;
750
+ }
751
+ if (this.effectLastNotMinimizedBoxStatusTimer && this.effectLastNotMinimizedBoxStatusTimer.timer) {
752
+ clearTimeout(this.effectLastNotMinimizedBoxStatusTimer.timer);
753
+ if (this.effectLastNotMinimizedBoxStatusTimer.skipUpdate !== skipUpdate) {
754
+ // this.effectLastNotMinimizedBoxStatusFunc(this.effectLastNotMinimizedBoxStatusTimer.skipUpdate)
755
+ if (this.effectLastNotMinimizedBoxStatusTimer.resolve) {
756
+ console.log("effectLastNotMinimizedBoxStatusFunc===>1111", skipUpdate);
757
+ this.effectLastNotMinimizedBoxStatusTimer.resolve(!!this.effectLastNotMinimizedBoxStatusTimer?.skipUpdate);
758
+ }
759
+ this.effectLastNotMinimizedBoxStatusTimer.skipUpdate = skipUpdate;
760
+ console.log("effectLastNotMinimizedBoxStatusFunc===>2222", skipUpdate);
761
+ } else {
762
+ return;
763
+ }
764
+ }
765
+ if (!this.effectLastNotMinimizedBoxStatusTimer) {
766
+ this.effectLastNotMinimizedBoxStatusTimer = {
767
+ skipUpdate,
768
+ timer: undefined,
769
+ resolve: undefined,
770
+ } as {
771
+ skipUpdate: boolean;
772
+ timer: number | undefined;
773
+ }
774
+ }
775
+ if (this.effectLastNotMinimizedBoxStatusTimer) {
776
+ const _skipUpdate = await new Promise<boolean>((resolve)=>{
777
+ this.effectLastNotMinimizedBoxStatusTimer!.resolve = resolve;
778
+ this.effectLastNotMinimizedBoxStatusTimer!.timer = setTimeout(()=>{
779
+ if (this.effectLastNotMinimizedBoxStatusTimer && this.effectLastNotMinimizedBoxStatusTimer.resolve) {
780
+ console.log("effectLastNotMinimizedBoxStatusTimer===>000", skipUpdate);
781
+ this.effectLastNotMinimizedBoxStatusTimer.resolve(this.effectLastNotMinimizedBoxStatusTimer!.skipUpdate);
782
+ }
783
+ }, 100) as unknown as number;
784
+ })
785
+ console.log("effectLastNotMinimizedBoxStatusFunc===>000==>00", skipUpdate, this.boxes$.value.map((b)=>({id:b.id,focus:b.focus, boxStatus:b.boxStatus,zIndex:b.zIndex, lastNotMinimizedBoxStatus:b._lastNotMinimizedBoxStatus$.value})));
786
+ this.effectLastNotMinimizedBoxStatusFunc(_skipUpdate)
787
+ this.effectLastNotMinimizedBoxStatusTimer = undefined;
788
+ console.log("effectLastNotMinimizedBoxStatusFunc===>000==>11", skipUpdate, this.boxes$.value.map((b)=>({id:b.id,focus:b.focus, boxStatus:b.boxStatus,zIndex:b.zIndex, lastNotMinimizedBoxStatus:b._lastNotMinimizedBoxStatus$.value})));
789
+ }
790
+ }
791
+
792
+ private effectLastNotMinimizedBoxStatusFunc(skipUpdate = false){
793
+ if (!this.useBoxesStatus) {
794
+ return;
795
+ }
796
+ console.log("effectLastNotMinimizedBoxStatusFunc===>end", skipUpdate);
543
797
  this.boxes.forEach((box) => {
544
798
  const boxStatus = this.lastNotMinimizedBoxesStatus$.get(box.id);
545
- box.setLastNotMinimizedBoxStatus(boxStatus, skipUpdate);
799
+ if(boxStatus){
800
+ box.setLastNotMinimizedBoxStatus(boxStatus, skipUpdate);
801
+ }
546
802
  });
547
803
  }
548
804
 
549
- private getAllMinimizedBoxes(): ReadonlyArray<TeleBox> {
550
- return this.boxes.filter((box) => box.boxStatus && box.boxStatus === TELE_BOX_STATE.Minimized);
805
+ private getUnabledBoxesStatusZIndex(): number {
806
+ return this.topBox ? this.topBox.zIndex : 99;
807
+ }
808
+
809
+ // private getTopBoxZIndex(): number {
810
+ // if (!this.useBoxesStatus) {
811
+ // return this.getUnabledBoxesStatusZIndex();
812
+ // }
813
+ // return [...this.boxes].filter((box) => box.boxStatus !== TELE_BOX_STATE.Minimized).reduce((maxZIndex, box) => {
814
+ // return Math.max(maxZIndex, box.zIndex);
815
+ // }, 100);
816
+ // }
817
+
818
+ /** Maximized box zIndex range: 100-299 */
819
+ public getMaxMaximizedBoxZIndex(): number {
820
+ if (!this.useBoxesStatus) {
821
+ return this.getUnabledBoxesStatusZIndex();
822
+ }
823
+ return this.boxes.reduce((maxZIndex, box) => {
824
+ if (box.boxStatus && box.boxStatus === TELE_BOX_STATE.Maximized) {
825
+ return Math.max(maxZIndex, box.zIndex);
826
+ }
827
+ return maxZIndex;
828
+ }, 99);
829
+ }
830
+
831
+ /** normal box zIndex range: 300-499 */
832
+ public getMaxNormalBoxZIndex(): number {
833
+ if (!this.useBoxesStatus) {
834
+ return this.getUnabledBoxesStatusZIndex();
835
+ }
836
+ return this.boxes.reduce((maxZIndex, box) => {
837
+ if (box.boxStatus && box.boxStatus === TELE_BOX_STATE.Normal) {
838
+ return Math.max(maxZIndex, box.zIndex);
839
+ }
840
+ return maxZIndex;
841
+ }, 299);
551
842
  }
552
843
 
553
844
  public create(
554
845
  config: TeleBoxManagerCreateConfig = {},
555
846
  smartPosition = true
556
847
  ): ReadonlyTeleBox {
848
+ console.log("create===>", config, this.topBox?.zIndex);
557
849
  const box = new TeleBox({
558
- zIndex: this.topBox ? this.topBox.zIndex + 1 : 100,
850
+ zIndex: this.getMaxNormalBoxZIndex() + 1,
559
851
  ...(smartPosition ? this.smartPosition(config) : config),
560
852
  darkMode: this.darkMode,
561
853
  prefersColorScheme: this.prefersColorScheme,
@@ -570,6 +862,15 @@ export class TeleBoxManager {
570
862
 
571
863
  box.mount(this.root);
572
864
 
865
+ this.boxes$.setValue([...this.boxes, box]);
866
+
867
+ if (box.boxStatus && this.useBoxesStatus) {
868
+ this.setBox(box, {
869
+ status: box.boxStatus,
870
+ lastNotMinimizedBoxStatus: box._lastNotMinimizedBoxStatus$.value
871
+ }, true);
872
+ }
873
+
573
874
  if (box.focus) {
574
875
  this.focusBox(box);
575
876
  if (smartPosition) {
@@ -577,44 +878,45 @@ export class TeleBoxManager {
577
878
  }
578
879
  }
579
880
 
580
- this.boxes$.setValue([...this.boxes, box]);
581
-
582
- if (box.boxStatus) {
583
- this.setBoxStatus(box.id, box.boxStatus, true);
584
- this.effectBoxStatusChange(true);
585
- }
586
-
587
- if (box._lastNotMinimizedBoxStatus$.value) {
588
- this.setLastNotMinimizedBoxStatus(box.id, box._lastNotMinimizedBoxStatus$.value, true);
589
- this.effectLastNotMinimizedBoxStatusChange();
590
- }
591
-
592
881
  box._delegateEvents.on(TELE_BOX_DELEGATE_EVENT.Maximize, () => {
593
- if (box.boxStatus) {
882
+ if (this.useBoxesStatus && box.boxStatus) {
594
883
  if (box.boxStatus === TELE_BOX_STATE.Maximized) {
595
- this.setBoxStatus(box.id, TELE_BOX_STATE.Normal);
884
+ this.setBox(box.id, {
885
+ status: TELE_BOX_STATE.Normal,
886
+ zIndex: this.getMaxNormalBoxZIndex() + 1
887
+ }, false);
596
888
  } else {
597
- this.boxes.forEach((box) => {
889
+ // 所有normal box都最大化
890
+ [...this.boxes].filter((box) => box.boxStatus === TELE_BOX_STATE.Normal).sort((a, b) => a.zIndex - b.zIndex).forEach((box) => {
598
891
  if (box.boxStatus === TELE_BOX_STATE.Normal) {
599
- this.setBoxStatus(box.id, TELE_BOX_STATE.Maximized);
892
+ console.log("maximized===>", box.id);
893
+ this.setBox(box.id, {
894
+ status: TELE_BOX_STATE.Maximized,
895
+ zIndex: this.getMaxMaximizedBoxZIndex() + 1
896
+ }, false);
600
897
  }
601
898
  });
899
+ this.makeMaximizedTopBoxFocus();
602
900
  }
603
- this.effectBoxStatusChange();
604
- this.makeMaximizedTopBoxFocus();
605
901
  } else {
606
902
  this.setMaximized(!this.maximized);
607
903
  }
608
904
 
609
905
  });
610
906
  box._delegateEvents.on(TELE_BOX_DELEGATE_EVENT.Minimize, () => {
611
- if (box.boxStatus) {
612
- const oldBoxStatus = box.boxStatus;
613
- this.setBoxStatus(box.id, TELE_BOX_STATE.Minimized);
614
- this.effectBoxStatusChange();
615
- if (oldBoxStatus === TELE_BOX_STATE.Maximized) {
616
- this.makeMaximizedTopBoxFocus();
907
+ if (box.boxStatus && this.useBoxesStatus) {
908
+ if (this.topBox === box) {
909
+ if (this.topBox?.focus) {
910
+ this.blurBox(this.topBox);
911
+ }
617
912
  }
913
+ this.setBox(box, {
914
+ status: TELE_BOX_STATE.Minimized,
915
+ zIndex: -1
916
+ }, false, ()=>{
917
+ this.focusTopBox();
918
+ });
919
+
618
920
  } else{
619
921
  this.setMinimized(true);
620
922
  }
@@ -650,12 +952,19 @@ export class TeleBoxManager {
650
952
  });
651
953
  box._zIndex$.reaction((_, __, skipUpdate) => {
652
954
  if (this.boxes.length > 0) {
955
+ console.log("topBox===>_zIndex", this.boxes.map((box) => ({id: box.id, zIndex: box.zIndex, boxStatus: box.boxStatus})));
956
+ let currentTopBox: TeleBox | undefined = undefined;
653
957
  const topBox = this.boxes.reduce((topBox, box) => {
654
958
  if (box.boxStatus && box.boxStatus === TELE_BOX_STATE.Minimized) {
655
959
  return topBox;
656
960
  }
961
+ if (!topBox) {
962
+ topBox = box;
963
+ return topBox;
964
+ }
657
965
  return topBox.zIndex > box.zIndex ? topBox : box;
658
- });
966
+ }, currentTopBox as (TeleBox | undefined));
967
+ console.log("topBox===>_zIndex===>end", topBox?.id);
659
968
  this.topBox$.setValue(topBox);
660
969
  }
661
970
  if (!skipUpdate) {
@@ -701,9 +1010,34 @@ export class TeleBoxManager {
701
1010
  });
702
1011
  }
703
1012
 
1013
+ public setBox(boxOrID: string | TeleBox, options: {
1014
+ status?: TeleBoxState;
1015
+ lastNotMinimizedBoxStatus?: NotMinimizedBoxState;
1016
+ zIndex?: number;
1017
+ }, skipUpdate = false, effectFunc?: () => void){
1018
+ const box = this.getBox(boxOrID)
1019
+ if (box) {
1020
+ const {status, zIndex, lastNotMinimizedBoxStatus} = options;
1021
+ if (status) {
1022
+ this.setBoxStatus(box.id, status, skipUpdate);
1023
+ box.setBoxStatus(status, skipUpdate);
1024
+ this.effectBoxStatusChange();
1025
+ }
1026
+ if (zIndex) {
1027
+ box.setZIndex(zIndex, skipUpdate);
1028
+ }
1029
+ if (lastNotMinimizedBoxStatus) {
1030
+ this.setLastNotMinimizedBoxStatus(box.id, lastNotMinimizedBoxStatus, skipUpdate);
1031
+ box.setLastNotMinimizedBoxStatus(lastNotMinimizedBoxStatus, skipUpdate);
1032
+ }
1033
+ effectFunc && effectFunc();
1034
+ }
1035
+ }
1036
+
704
1037
  public remove(
705
1038
  boxOrID: string | TeleBox,
706
- skipUpdate = false
1039
+ skipUpdate = false,
1040
+ effectFunc?: () => void
707
1041
  ): ReadonlyTeleBox | undefined {
708
1042
  const box = this.getBox(boxOrID);
709
1043
  const index = this.getBoxIndex(boxOrID);
@@ -712,13 +1046,11 @@ export class TeleBoxManager {
712
1046
  const deletedBoxes = boxes.splice(index, 1);
713
1047
  this.boxes$.setValue(boxes);
714
1048
  deletedBoxes.forEach((box) => box.destroy());
715
- if (box) {
1049
+ if (box && this.useBoxesStatus) {
716
1050
  this.setBoxStatus(box.id, undefined, skipUpdate);
717
- this.boxesStatus$.delete(box.id);
718
- this.effectBoxStatusChange(skipUpdate);
719
1051
  this.setLastNotMinimizedBoxStatus(box.id, undefined, skipUpdate);
720
- this.lastNotMinimizedBoxesStatus$.delete(box.id);
721
- this.effectLastNotMinimizedBoxStatusChange();
1052
+ this.effectBoxStatusChange();
1053
+ effectFunc && effectFunc();
722
1054
  }
723
1055
  if (!skipUpdate) {
724
1056
  if (this.boxes.length <= 0) {
@@ -792,10 +1124,10 @@ export class TeleBoxManager {
792
1124
  this.blurBox(box, skipUpdate);
793
1125
  }
794
1126
  });
795
- if (this.maxTitleBar.hasMaximizedBoxInStatus()) {
796
- this.makeMaximizedBoxFocus(targetBox);
797
- } else {
1127
+ if (!this.useBoxesStatus) {
798
1128
  this.maxTitleBar.focusBox(targetBox);
1129
+ } else if (this.useBoxesStatus && this.maxTitleBar.hasMaximizedBoxInStatus()) {
1130
+ this.makeMaximizedTopBoxFocus();
799
1131
  }
800
1132
  }
801
1133
  }
@@ -816,11 +1148,7 @@ export class TeleBoxManager {
816
1148
  }
817
1149
  }
818
1150
  if (this.maxTitleBar.focusedBox === targetBox) {
819
- if (this.maxTitleBar.hasMaximizedBoxInStatus()) {
820
- this.makeMaximizedTopBoxFocus();
821
- } else {
822
- this.maxTitleBar.focusBox();
823
- }
1151
+ this.maxTitleBar.focusBox();
824
1152
  }
825
1153
  }
826
1154
  }
@@ -835,11 +1163,7 @@ export class TeleBoxManager {
835
1163
  }
836
1164
  });
837
1165
  if (this.maxTitleBar.focusedBox) {
838
- if (this.maxTitleBar.hasMaximizedBoxInStatus()) {
839
- this.makeMaximizedBoxBlur();
840
- } else {
841
- this.maxTitleBar.focusBox();
842
- }
1166
+ this.maxTitleBar.focusBox();
843
1167
  }
844
1168
  }
845
1169
 
@@ -847,6 +1171,7 @@ export class TeleBoxManager {
847
1171
 
848
1172
  protected boxes$: Val<TeleBox[]>;
849
1173
  protected topBox$: Val<TeleBox | undefined>;
1174
+ // protected maximizedTopBox$: Val<MaxTitleBarTeleBox | undefined>;
850
1175
 
851
1176
  protected teleBoxMatcher(
852
1177
  config: TeleBoxManagerQueryConfig
@@ -908,13 +1233,11 @@ export class TeleBoxManager {
908
1233
  if (config.footer != null) {
909
1234
  box.mountFooter(config.footer);
910
1235
  }
911
- if (config.boxStatus) {
912
- this.setBoxStatus(box.id, config.boxStatus, skipUpdate);
913
- this.effectBoxStatusChange(skipUpdate);
914
- }
915
- if (config.lastNotMinimizedBoxStatus) {
916
- this.setLastNotMinimizedBoxStatus(box.id, config.lastNotMinimizedBoxStatus, skipUpdate);
917
- this.effectLastNotMinimizedBoxStatusChange();
1236
+ if (this.useBoxesStatus && (config.boxStatus || config.lastNotMinimizedBoxStatus)) {
1237
+ this.setBox(box, {
1238
+ status: config.boxStatus,
1239
+ lastNotMinimizedBoxStatus: config.lastNotMinimizedBoxStatus
1240
+ }, skipUpdate);
918
1241
  }
919
1242
  }
920
1243
 
@@ -926,13 +1249,17 @@ export class TeleBoxManager {
926
1249
  let vx = 20;
927
1250
  if (this.topBox) {
928
1251
  vx = this.topBox.intrinsicX * this.containerRect.width + 20;
929
-
930
1252
  if (
931
1253
  vx >
932
1254
  this.containerRect.width - width * this.containerRect.width
933
1255
  ) {
934
1256
  vx = 20;
935
1257
  }
1258
+ } else if (this.hasMinimizedBox()) {
1259
+ // 如果都最小化,则获取lastNotMinimizedBoxesStatus中normal box的最大x
1260
+ vx = [...this.boxes].filter((box) => box.boxStatus && box.boxStatus === TELE_BOX_STATE.Minimized && box.lastNotMinimizedBoxStatus && box.lastNotMinimizedBoxStatus === TELE_BOX_STATE.Normal).reduce((maxX, box) => {
1261
+ return Math.max(maxX, box.intrinsicX * this.containerRect.width + 20);
1262
+ }, vx);
936
1263
  }
937
1264
  x = vx / this.containerRect.width;
938
1265
  }
@@ -950,6 +1277,11 @@ export class TeleBoxManager {
950
1277
  ) {
951
1278
  vy = 20;
952
1279
  }
1280
+ } else if (this.hasMinimizedBox()) {
1281
+ // 如果都最小化,则获取lastNotMinimizedBoxesStatus中normal box的最大y
1282
+ vy = [...this.boxes].filter((box) => box.boxStatus && box.boxStatus === TELE_BOX_STATE.Minimized && box.lastNotMinimizedBoxStatus && box.lastNotMinimizedBoxStatus === TELE_BOX_STATE.Normal).reduce((maxY, box) => {
1283
+ return Math.max(maxY, box.intrinsicY * this.containerRect.height + 20);
1284
+ }, vy);
953
1285
  }
954
1286
 
955
1287
  y = vy / this.containerRect.height;
@@ -959,17 +1291,19 @@ export class TeleBoxManager {
959
1291
  }
960
1292
 
961
1293
  protected makeBoxTop(box: TeleBox, skipUpdate = false): void {
962
- if (this.topBox) {
1294
+ if (!this.useBoxesStatus && this.topBox) {
963
1295
  if (box !== this.topBox) {
964
- const currentBoxZIndex = box.zIndex;
965
- const addZIndex = this.topBox.zIndex + 1 - box.zIndex;
966
1296
  box.setZIndex(this.topBox.zIndex + 1, skipUpdate);
967
- this.makeTopBoxEffect(box.id, addZIndex, currentBoxZIndex, skipUpdate);
968
1297
  }
1298
+ } else if (this.useBoxesStatus) {
1299
+ // todo
969
1300
  }
970
1301
  }
971
1302
 
972
- protected makeMaximizedTopBoxFocus(): void {
1303
+ private getMaximizedTopBox(): MaxTitleBarTeleBox | undefined {
1304
+ if (!this.useBoxesStatus) {
1305
+ return undefined;
1306
+ }
973
1307
  let maximizedBox: MaxTitleBarTeleBox | undefined;
974
1308
  if (this.maxTitleBar.hasMaximizedBoxInStatus()) {
975
1309
  maximizedBox = this.maxTitleBar.MaximizedBoxes.reduce((topBox, currentBox) => {
@@ -978,48 +1312,18 @@ export class TeleBoxManager {
978
1312
  }
979
1313
  return topBox.zIndex > currentBox.zIndex ? topBox : currentBox;
980
1314
  }, );
981
- if (maximizedBox) {
982
- this.maxTitleBar.focusBox(maximizedBox);
983
- }
984
1315
  }
1316
+ return maximizedBox;
985
1317
  }
986
1318
 
987
- private makeTopBoxEffect(boxId: string, addZIndex: number, currentZIndex: number, skipUpdate = false): void {
988
- if (this.hasBoxesStatus() && this.hasMinimizedBox()) {
989
- this.boxes.forEach((b) => {
990
- if (b.boxStatus && b.id !== boxId && b.zIndex >= currentZIndex) {
991
- b.setZIndex(b.zIndex + addZIndex, skipUpdate);
992
- }
993
- });
994
- }
995
- }
996
-
997
- protected makeMaximizedBoxFocus(maximizedBox: MaxTitleBarTeleBox, skipUpdate = false): void {
998
- if (this.maxTitleBar.hasMaximizedBoxInStatus() && this.maxTitleBar.MaximizedBoxes.find((box) => box.id === maximizedBox.id)) {
999
- const currentZIndex = maximizedBox.zIndex;
1000
- const currentMaximizedTopBox = this.maxTitleBar.MaximizedBoxes.reduce((topBox, box) => {
1001
- if (!topBox) {
1002
- return box;
1003
- }
1004
- return topBox.zIndex > box.zIndex ? topBox : box;
1005
- });
1006
- if (currentMaximizedTopBox !== maximizedBox) {
1007
- const addZIndex = currentMaximizedTopBox.zIndex - currentZIndex + 1;
1008
- this.boxes.forEach((box) => {
1009
- if (this.maxTitleBar.MaximizedBoxes.find((b) => b.id === box.id && b.id !== maximizedBox.id)) {
1010
- return;
1011
- }
1012
- if (box.zIndex >= currentZIndex) {
1013
- box.setZIndex(box.zIndex + addZIndex, skipUpdate);
1014
- }
1015
- });
1016
- }
1017
- this.maxTitleBar.focusBox(maximizedBox);
1319
+ protected makeMaximizedTopBoxFocus(): void {
1320
+ if (!this.useBoxesStatus) {
1321
+ return;
1018
1322
  }
1019
- }
1020
-
1021
- protected makeMaximizedBoxBlur(): void {
1022
- this.maxTitleBar.focusBox();
1323
+ const maximizedBox: MaxTitleBarTeleBox | undefined = this.getMaximizedTopBox();
1324
+ console.log("makeMaximizedTopBoxFocus===>", maximizedBox?.id);
1325
+ this.maxTitleBar.focusBox(maximizedBox);
1326
+ console.log("makeMaximizedTopBoxFocus===>end", maximizedBox?.id);
1023
1327
  }
1024
1328
 
1025
1329
  protected getBoxIndex(boxOrID: TeleBox | string): number {
@@ -1028,9 +1332,23 @@ export class TeleBoxManager {
1028
1332
  : this.boxes.findIndex((box) => box === boxOrID);
1029
1333
  }
1030
1334
 
1031
- protected getBox(boxOrID: TeleBox | string): TeleBox | undefined {
1335
+ public getBox(boxOrID: TeleBox | string): TeleBox | undefined {
1032
1336
  return typeof boxOrID === "string"
1033
1337
  ? this.boxes.find((box) => box.id === boxOrID)
1034
1338
  : boxOrID;
1035
1339
  }
1340
+
1341
+ public createMinimizedAppMenu():AppMenu | undefined {
1342
+ if (this.collector?.$appMenuContainer) {
1343
+ return new AppMenu({
1344
+ manager: this,
1345
+ container: this.collector?.$appMenuContainer,
1346
+ theme: this.prefersColorScheme ? 'dark' : 'light',
1347
+ getBoxesStatus: () => {
1348
+ return this.boxesStatus$;
1349
+ }
1350
+ });
1351
+ }
1352
+ return;
1353
+ }
1036
1354
  }