Spaces:
Running

File size: 4,129 Bytes
5e1643b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Native fullscreen and orientation lock are optional browser capabilities.
// The same landscape viewport also works when either request is unavailable.
export class PilotMobileScreen {
  constructor(stage,viewport,button){
    this.stage=stage;this.viewport=viewport;this.button=button;this.active=false;this.wasNative=false;
    this.touch=matchMedia('(pointer:coarse)').matches;
    this.enabled=true;
    document.body.dataset.mobile=String(this.touch);
    button.addEventListener('click',()=>this.active?this.leave():this.enter());
    window.addEventListener('resize',()=>this.layout());
    window.visualViewport?.addEventListener('resize',()=>this.layout());
    window.visualViewport?.addEventListener('scroll',()=>this.layout());
    for(const name of ['fullscreenchange','webkitfullscreenchange'])document.addEventListener(name,()=>{
      const native=this.native();
      if(this.wasNative&&!native)this.leave();
      else if(native&&!this.active)this.exitNative();
      this.wasNative=native;this.layout();
    });
    window.addEventListener('keydown',e=>{if(e.key==='Escape'&&this.active){e.preventDefault();this.leave();}});
    window.addEventListener('pagehide',()=>this.leave());
  }
  native(){return (document.fullscreenElement||document.webkitFullscreenElement)===this.stage;}
  async enter(){
    if(!this.enabled||this.active)return;
    this.active=true;this.scroll=[scrollX,scrollY];
    this.previousOverflow=[document.documentElement.style.overflow,document.body.style.overflow];
    document.documentElement.style.overflow='hidden';document.body.style.overflow='hidden';
    this.stage.classList.add('mobile-fullscreen');this.layout();
    try{
      const request=this.stage.requestFullscreen||this.stage.webkitRequestFullscreen;
      if(request)await request.call(this.stage,{navigationUI:'hide'});
    }catch(error){this.nativeError=error.name;}
    if(!this.active){this.exitNative();return;}
    if(this.touch&&this.native()&&screen.orientation?.lock){
      try{await screen.orientation.lock('landscape');this.orientationLocked=true;if(!this.active)this.unlock();}
      catch(error){this.orientationError=error.name;}
    }
    this.layout();
  }
  layout(){
    this.button.textContent=this.active?'Exit fullscreen':'Fullscreen';
    this.button.setAttribute('aria-pressed',String(this.active));
    this.button.setAttribute('aria-label',this.active?'Exit fullscreen and return to the page':this.touch?'Open scene fullscreen in landscape':'Open scene fullscreen');
    if(!this.active)return;
    const v=window.visualViewport,w=Math.round(v?.width||innerWidth),h=Math.round(v?.height||innerHeight),rotated=this.touch&&h>w;
    this.stage.style.setProperty('--screen-width',`${w}px`);this.stage.style.setProperty('--screen-height',`${h}px`);
    this.stage.style.setProperty('--screen-left',`${v?.offsetLeft||0}px`);this.stage.style.setProperty('--screen-top',`${v?.offsetTop||0}px`);
    this.stage.classList.toggle('landscape-rotated',rotated);
    this.stage.dataset.screenMode=rotated?'landscape-rotated':'landscape';
    const key=`${w}:${h}:${this.active}`;
    if(key!==this.lastLayout){this.lastLayout=key;this.resetInput();}
  }
  resetInput(){
    const child=this.stage.querySelector('iframe')?.contentWindow;
    child?.pilotControls?.resetInputs();child?.postMessage({type:'pilot-screen-change'},location.origin);
  }
  unlock(){try{screen.orientation?.unlock?.();}catch{}this.orientationLocked=false;}
  exitNative(){try{if(this.native())(document.exitFullscreen||document.webkitExitFullscreen)?.call(document)?.catch?.(()=>{});}catch{}}
  leave(){
    if(!this.active)return;
    this.active=false;this.resetInput();this.unlock();this.exitNative();
    this.stage.classList.remove('mobile-fullscreen','landscape-rotated');
    delete this.stage.dataset.screenMode;this.lastLayout=null;
    for(const name of ['--screen-width','--screen-height','--screen-left','--screen-top'])this.stage.style.removeProperty(name);
    [document.documentElement.style.overflow,document.body.style.overflow]=this.previousOverflow;
    this.layout();window.scrollTo(...this.scroll);
  }
}