| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139 |
- import { EventDispatcher } from 'three';
- import { Style } from './Style.js';
- import { Graph } from './Graph.js';
- import { getItem, setItem } from '../Inspector.js';
- export class Profiler extends EventDispatcher {
- constructor( inspector ) {
- super();
- this.inspector = inspector;
- this.tabs = {};
- this.activeTabId = null;
- this.isResizing = false;
- this.lastHeightBottom = 350; // Height for bottom position
- this.lastWidthRight = 450; // Width for right position
- this.position = 'bottom'; // 'bottom' or 'right'
- this.detachedWindows = []; // Array to store detached tab windows
- this.maxZIndex = 1002; // Track the highest z-index for detached windows (starts at base z-index from CSS)
- this.nextTabOriginalIndex = 0; // Track the original order of tabs as they are added
- this.setupShell();
- this.setupResizing();
- Style.init( this.domElement );
- // Setup window resize listener and update mobile status
- this.setupWindowResizeListener();
- // Setup orientation change listener for mobile devices
- this.setupOrientationListener();
- this.checkHeaderScroll();
- this.panel.addEventListener( 'transitionend', ( e ) => {
- if ( e.target === this.panel && ( e.propertyName === 'width' || e.propertyName === 'height' || e.propertyName === 'transform' ) ) {
- this.checkHeaderScroll();
- }
- } );
- }
- getSize() {
- if ( this.panel.classList.contains( 'visible' ) === false || this.panel.classList.contains( 'no-tabs' ) ) {
- return { width: 0, height: 0 };
- }
- if ( this.position === 'right' ) {
- return { width: this.panel.offsetWidth, height: 0 };
- } else {
- return { width: 0, height: this.panel.offsetHeight };
- }
- }
- get isMobile() {
- return this.detectMobile();
- }
- get isSmallScreen() {
- return window.innerWidth <= 768;
- }
- detectMobile() {
- // Check for mobile devices
- const userAgent = navigator.userAgent || navigator.vendor || window.opera;
- const isMobileUA = /android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini/i.test( userAgent );
- const isTouchDevice = ( 'ontouchstart' in window ) || ( navigator.maxTouchPoints > 0 );
- return isMobileUA || ( isTouchDevice && this.isSmallScreen );
- }
- setupOrientationListener() {
- const handleOrientationChange = () => {
- if ( ! this.isMobile ) return;
- // Check if device is in landscape or portrait mode
- const isLandscape = window.innerWidth > window.innerHeight;
- // In landscape mode, use right position (vertical panel)
- // In portrait mode, use bottom position (horizontal panel)
- const targetPosition = isLandscape ? 'right' : 'bottom';
- if ( this.position !== targetPosition ) {
- this.setPosition( targetPosition );
- }
- };
- // Initial check
- handleOrientationChange();
- // Listen for orientation changes
- window.addEventListener( 'orientationchange', handleOrientationChange );
- window.addEventListener( 'resize', handleOrientationChange );
- }
- setupWindowResizeListener() {
- const constrainDetachedWindows = () => {
- this.detachedWindows.forEach( detachedWindow => {
- this.constrainWindowToBounds( detachedWindow.panel );
- } );
- };
- const constrainMainPanel = () => {
- // Skip if panel is maximized (it should always fill the screen)
- if ( this.panel.classList.contains( 'maximized' ) ) return;
- const windowWidth = window.innerWidth;
- const windowHeight = window.innerHeight;
- if ( this.position === 'bottom' ) {
- const currentHeight = this.panel.offsetHeight;
- const maxHeight = windowHeight - 50; // Leave 50px margin
- if ( currentHeight > maxHeight ) {
- this.panel.style.height = `${ maxHeight }px`;
- this.lastHeightBottom = maxHeight;
- }
- } else if ( this.position === 'right' ) {
- const currentWidth = this.panel.offsetWidth;
- const maxWidth = windowWidth - 50; // Leave 50px margin
- if ( currentWidth > maxWidth ) {
- this.panel.style.width = `${ maxWidth }px`;
- this.lastWidthRight = maxWidth;
- }
- }
- };
- // Listen for window resize events
- window.addEventListener( 'resize', () => {
- if ( this.isSmallScreen ) {
- this.floatingBtn.style.display = 'none';
- this.panel.classList.add( 'hide-position-toggle' );
- } else {
- this.floatingBtn.style.display = '';
- this.panel.classList.remove( 'hide-position-toggle' );
- }
- if ( this.isMobile ) {
- this.panel.classList.add( 'is-mobile' );
- } else {
- this.panel.classList.remove( 'is-mobile' );
- }
- constrainDetachedWindows();
- constrainMainPanel();
- this.checkHeaderScroll();
- } );
- }
- constrainWindowToBounds( windowPanel ) {
- const windowWidth = window.innerWidth;
- const windowHeight = window.innerHeight;
- const panelWidth = windowPanel.offsetWidth;
- const panelHeight = windowPanel.offsetHeight;
- let left = parseFloat( windowPanel.style.left ) || windowPanel.offsetLeft || 0;
- let top = parseFloat( windowPanel.style.top ) || windowPanel.offsetTop || 0;
- // Allow window to extend half its width/height outside the screen
- const halfWidth = panelWidth / 2;
- const halfHeight = panelHeight / 2;
- // Constrain horizontal position (allow half width to extend beyond right edge)
- if ( left + panelWidth > windowWidth + halfWidth ) {
- left = windowWidth + halfWidth - panelWidth;
- }
- // Constrain horizontal position (allow half width to extend beyond left edge)
- if ( left < - halfWidth ) {
- left = - halfWidth;
- }
- // Constrain vertical position (allow half height to extend beyond bottom edge)
- if ( top + panelHeight > windowHeight + halfHeight ) {
- top = windowHeight + halfHeight - panelHeight;
- }
- // Constrain vertical position (allow half height to extend beyond top edge)
- if ( top < - halfHeight ) {
- top = - halfHeight;
- }
- // Apply constrained position
- windowPanel.style.left = `${ left }px`;
- windowPanel.style.top = `${ top }px`;
- }
- setupShell() {
- this.domElement = document.createElement( 'div' );
- this.domElement.classList.add( 'three-inspector' );
- this.toggleButton = document.createElement( 'button' );
- this.toggleButton.classList.add( 'profiler-toggle' );
- this.toggleButton.innerHTML = `
- <span class="builtin-tabs-container"></span>
- <span class="toggle-text">
- <span class="fps-counter">-</span>
- <span class="fps-label">FPS</span>
- </span>
- <span class="toggle-icon">
- <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="icon icon-tabler icons-tabler-outline icon-tabler-device-ipad-horizontal-search"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M11.5 20h-6.5a2 2 0 0 1 -2 -2v-12a2 2 0 0 1 2 -2h14a2 2 0 0 1 2 2v5.5" /><path d="M9 17h2" /><path d="M18 18m-3 0a3 3 0 1 0 6 0a3 3 0 1 0 -6 0" /><path d="M20.2 20.2l1.8 1.8" /></svg>
- <span class="console-badge-container">
- <span class="console-badge error" style="display: none;">0</span>
- <span class="console-badge warn" style="display: none;">0</span>
- </span>
- </span>
- `;
- this.toggleButton.onclick = () => this.togglePanel();
- this.builtinTabsContainer = this.toggleButton.querySelector( '.builtin-tabs-container' );
- // Create mini-panel for builtin tabs (shown when panel is hidden)
- this.miniPanel = document.createElement( 'div' );
- this.miniPanel.classList.add( 'profiler-mini-panel' );
- this.miniPanel.className = 'profiler-mini-panel';
- this.panel = document.createElement( 'div' );
- this.panel.classList.add( 'profiler-panel' );
- const header = document.createElement( 'div' );
- header.className = 'profiler-header';
- // Enable horizontal scrolling with vertical mouse wheel
- header.addEventListener( 'wheel', ( e ) => {
- if ( e.deltaY !== 0 ) {
- e.preventDefault();
- header.scrollLeft += e.deltaY * .25;
- }
- }, { passive: false } );
- this.tabsContainer = document.createElement( 'div' );
- this.tabsContainer.className = 'profiler-tabs';
- const controls = document.createElement( 'div' );
- controls.className = 'profiler-controls';
- this.floatingBtn = document.createElement( 'button' );
- this.floatingBtn.classList.add( 'floating-btn' );
- this.floatingBtn.title = 'Switch to Right Side';
- this.floatingBtn.innerHTML = '<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="3" width="18" height="18" rx="2" ry="2"></rect><line x1="15" y1="3" x2="15" y2="21"></line></svg>';
- this.floatingBtn.onclick = () => this.togglePosition();
- // Hide position toggle button on small screens
- if ( this.isSmallScreen ) {
- this.floatingBtn.style.display = 'none';
- this.panel.classList.add( 'hide-position-toggle' );
- }
- if ( this.isMobile ) {
- this.panel.classList.add( 'is-mobile' );
- }
- this.maximizeBtn = document.createElement( 'button' );
- this.maximizeBtn.classList.add( 'maximize-btn' );
- this.maximizeBtn.innerHTML = '<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M8 3H5a2 2 0 0 0-2 2v3m18 0V5a2 2 0 0 0-2-2h-3m0 18h3a2 2 0 0 0 2-2v-3M3 16v3a2 2 0 0 0 2 2h3"/></svg>';
- this.maximizeBtn.onclick = () => this.toggleMaximize();
- const hideBtn = document.createElement( 'button' );
- hideBtn.classList.add( 'hide-panel-btn' );
- hideBtn.textContent = '-';
- hideBtn.onclick = () => this.togglePanel();
- controls.append( this.floatingBtn, this.maximizeBtn, hideBtn );
- header.append( this.tabsContainer, controls );
- this.contentWrapper = document.createElement( 'div' );
- this.contentWrapper.className = 'profiler-content-wrapper';
- const resizer = document.createElement( 'div' );
- resizer.className = 'panel-resizer';
- this.panel.append( resizer, header, this.contentWrapper );
- this.domElement.append( this.toggleButton, this.miniPanel, this.panel );
- // Set initial position class
- this.panel.classList.add( `position-${this.position}` );
- if ( this.position === 'right' ) {
- this.toggleButton.classList.add( 'position-right' );
- this.miniPanel.classList.add( 'position-right' );
- }
- // Create a background performance graph for the toggle button
- this.toggleGraph = new Graph( 80 );
- this.toggleGraph.addLine( 'fps', '#4c4c6bff' );
- this.toggleGraph.domElement.className = 'profiler-toggle-graph';
- this.toggleButton.appendChild( this.toggleGraph.domElement );
- }
- setupResizing() {
- const resizer = this.panel.querySelector( '.panel-resizer' );
- const onStart = ( e ) => {
- this.isResizing = true;
- this.panel.classList.add( 'resizing' );
- resizer.setPointerCapture( e.pointerId );
- const startX = e.clientX;
- const startY = e.clientY;
- const startHeight = this.panel.offsetHeight;
- const startWidth = this.panel.offsetWidth;
- const onMove = ( moveEvent ) => {
- if ( ! this.isResizing ) return;
- moveEvent.preventDefault();
- const currentX = moveEvent.clientX;
- const currentY = moveEvent.clientY;
- if ( this.position === 'bottom' ) {
- // Vertical resize for bottom position
- const newHeight = startHeight - ( currentY - startY );
- if ( newHeight > 100 && newHeight < window.innerHeight - 50 ) {
- this.panel.style.height = `${ newHeight }px`;
- }
- } else if ( this.position === 'right' ) {
- // Horizontal resize for right position
- const newWidth = startWidth - ( currentX - startX );
- if ( newWidth > 200 && newWidth < window.innerWidth - 50 ) {
- this.panel.style.width = `${ newWidth }px`;
- }
- }
- this.dispatchEvent( { type: 'resize' } );
- this.checkHeaderScroll();
- };
- const onEnd = () => {
- this.isResizing = false;
- this.panel.classList.remove( 'resizing' );
- resizer.removeEventListener( 'pointermove', onMove );
- resizer.removeEventListener( 'pointerup', onEnd );
- resizer.removeEventListener( 'pointercancel', onEnd );
- if ( ! this.panel.classList.contains( 'maximized' ) ) {
- // Save dimensions based on current position
- if ( this.position === 'bottom' ) {
- this.lastHeightBottom = this.panel.offsetHeight;
- } else if ( this.position === 'right' ) {
- this.lastWidthRight = this.panel.offsetWidth;
- }
- // Save layout after resize
- this.saveLayout();
- }
- };
- resizer.addEventListener( 'pointermove', onMove );
- resizer.addEventListener( 'pointerup', onEnd );
- resizer.addEventListener( 'pointercancel', onEnd );
- };
- resizer.addEventListener( 'pointerdown', onStart );
- }
- toggleMaximize() {
- if ( this.panel.classList.contains( 'maximized' ) ) {
- this.panel.classList.remove( 'maximized' );
- this.domElement.classList.remove( 'maximized' );
- // Restore size based on current position
- if ( this.position === 'bottom' ) {
- this.panel.style.height = `${ this.lastHeightBottom }px`;
- this.panel.style.width = '100%';
- } else if ( this.position === 'right' ) {
- this.panel.style.height = '100%';
- this.panel.style.width = `${ this.lastWidthRight }px`;
- }
- this.maximizeBtn.innerHTML = '<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M8 3H5a2 2 0 0 0-2 2v3m18 0V5a2 2 0 0 0-2-2h-3m0 18h3a2 2 0 0 0 2-2v-3M3 16v3a2 2 0 0 0 2 2h3"/></svg>';
- } else {
- // Save current size before maximizing
- if ( this.position === 'bottom' ) {
- this.lastHeightBottom = this.panel.offsetHeight;
- } else if ( this.position === 'right' ) {
- this.lastWidthRight = this.panel.offsetWidth;
- }
- this.panel.classList.add( 'maximized' );
- this.domElement.classList.add( 'maximized' );
- // Maximize based on current position
- if ( this.position === 'bottom' ) {
- this.panel.style.height = '100vh';
- this.panel.style.width = '100%';
- } else if ( this.position === 'right' ) {
- this.panel.style.height = '100%';
- this.panel.style.width = '100vw';
- }
- this.maximizeBtn.innerHTML = '<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="8" y="8" width="12" height="12" rx="2" ry="2"></rect><path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2"></path></svg>';
- }
- this.dispatchEvent( { type: 'resize' } );
- }
- hide() {
- this.miniPanel.classList.remove( 'visible' );
- this.miniPanel.querySelectorAll( '.mini-panel-content' ).forEach( content => {
- content.style.display = 'none';
- } );
- this.builtinTabsContainer.querySelectorAll( '.builtin-tab-btn' ).forEach( btn => {
- btn.classList.remove( 'active' );
- } );
- }
- show( tab ) {
- this.hide();
- tab.builtinButton.classList.add( 'active' );
- if ( ! tab.miniContent.firstChild ) {
- while ( tab.content.firstChild ) {
- tab.miniContent.appendChild( tab.content.firstChild );
- }
- }
- tab.miniContent.style.display = 'block';
- this.miniPanel.classList.add( 'visible' );
- }
- addTab( tab ) {
- this.tabs[ tab.id ] = tab;
- // Assign a permanent original index to this tab
- tab.originalIndex = this.nextTabOriginalIndex ++;
- // Add visual indicator for tabs that cannot be detached
- if ( tab.allowDetach === false ) {
- tab.button.classList.add( 'no-detach' );
- }
- // Set visibility change callback
- tab.onVisibilityChange = () => this.updatePanelSize();
- this.setupTabDragAndDrop( tab );
- if ( ! tab.builtin ) {
- this.tabsContainer.appendChild( tab.button );
- }
- this.contentWrapper.appendChild( tab.content );
- // Apply the current visibility state to the DOM elements
- if ( ! tab.isVisible ) {
- tab.button.style.display = 'none';
- tab.content.style.display = 'none';
- }
- // If tab is builtin, add it to the profiler-toggle button
- if ( tab.builtin ) {
- this.addBuiltinTab( tab );
- }
- // Update panel size when tabs change
- this.updatePanelSize();
- // Set profiler reference
- tab.profiler = this;
- }
- addBuiltinTab( tab ) {
- // Create a button for the builtin tab in the profiler-toggle
- const builtinButton = document.createElement( 'button' );
- builtinButton.className = 'builtin-tab-btn';
- // Use icon if provided, otherwise use first letter
- if ( tab.icon ) {
- builtinButton.innerHTML = tab.icon;
- } else {
- builtinButton.textContent = tab.button.textContent.charAt( 0 ).toUpperCase();
- }
- builtinButton.title = tab.button.textContent;
- // Create mini-panel content container for this tab
- const miniContent = document.createElement( 'div' );
- miniContent.className = 'mini-panel-content';
- miniContent.style.display = 'none';
- // Store references in the tab object
- tab.builtinButton = builtinButton;
- tab.miniContent = miniContent;
- this.miniPanel.appendChild( miniContent );
- builtinButton.onclick = ( e ) => {
- e.stopPropagation(); // Prevent toggle panel from triggering
- // Toggle mini-panel for this tab
- const isCurrentlyActive = miniContent.style.display !== 'none' && miniContent.children.length > 0;
- if ( isCurrentlyActive ) {
- this.hide();
- } else {
- this.show( tab );
- }
- };
- this.builtinTabsContainer.appendChild( builtinButton );
- // Store references
- tab.builtinButton = builtinButton;
- tab.miniContent = miniContent;
- // If the tab was hidden before being added, hide the builtin button
- if ( ! tab.isVisible ) {
- builtinButton.style.display = 'none';
- miniContent.style.display = 'none';
- // Hide the builtin-tabs-container if all builtin buttons are hidden
- const hasVisibleBuiltinButtons = Array.from( this.builtinTabsContainer.querySelectorAll( '.builtin-tab-btn' ) )
- .some( btn => btn.style.display !== 'none' );
- if ( ! hasVisibleBuiltinButtons ) {
- this.builtinTabsContainer.style.display = 'none';
- }
- }
- }
- removeTab( tab ) {
- if ( ! tab || this.tabs[ tab.id ] === undefined ) return;
- delete this.tabs[ tab.id ];
- if ( tab.isDetached && tab.detachedWindow ) {
- if ( tab.detachedWindow.panel && tab.detachedWindow.panel.parentNode ) {
- tab.detachedWindow.panel.parentNode.removeChild( tab.detachedWindow.panel );
- }
- const index = this.detachedWindows.indexOf( tab.detachedWindow );
- if ( index !== - 1 ) {
- this.detachedWindows.splice( index, 1 );
- }
- }
- if ( ! tab.builtin ) {
- if ( tab.button && tab.button.parentNode ) {
- tab.button.parentNode.removeChild( tab.button );
- }
- } else {
- if ( tab.builtinButton && tab.builtinButton.parentNode ) {
- tab.builtinButton.parentNode.removeChild( tab.builtinButton );
- }
- if ( tab.miniContent && tab.miniContent.parentNode ) {
- tab.miniContent.parentNode.removeChild( tab.miniContent );
- }
- // Clean up builtin container if empty
- const hasVisibleBuiltinButtons = Array.from( this.builtinTabsContainer.querySelectorAll( '.builtin-tab-btn' ) )
- .some( btn => btn.style.display !== 'none' );
- if ( ! hasVisibleBuiltinButtons ) {
- this.builtinTabsContainer.style.display = 'none';
- }
- }
- if ( tab.content && tab.content.parentNode ) {
- tab.content.parentNode.removeChild( tab.content );
- }
- if ( this.activeTabId === tab.id ) {
- this.activeTabId = null;
- // Try to activate another tab
- const remainingTabs = Object.values( this.tabs ).filter( t => ! t.isDetached && t.isVisible );
- if ( remainingTabs.length > 0 ) {
- this.setActiveTab( remainingTabs[ 0 ].id );
- } else {
- this.updatePanelSize();
- }
- } else {
- this.updatePanelSize();
- }
- tab.onVisibilityChange = null;
- tab.profiler = null;
- }
- updatePanelSize() {
- // Check if there are any visible tabs in the panel
- const hasVisibleTabs = Object.values( this.tabs ).some( tab => ! tab.isDetached && tab.isVisible );
- // Add or remove CSS class to indicate no tabs state
- if ( ! hasVisibleTabs ) {
- this.panel.classList.add( 'no-tabs' );
- // If maximized and no tabs, restore to normal size
- if ( this.panel.classList.contains( 'maximized' ) ) {
- this.panel.classList.remove( 'maximized' );
- this.domElement.classList.remove( 'maximized' );
- this.maximizeBtn.innerHTML = '<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M8 3H5a2 2 0 0 0-2 2v3m18 0V5a2 2 0 0 0-2-2h-3m0 18h3a2 2 0 0 0 2-2v-3M3 16v3a2 2 0 0 0 2 2h3"/></svg>';
- }
- // No tabs visible - set to minimum size
- if ( this.position === 'bottom' ) {
- this.panel.style.height = '32px';
- } else if ( this.position === 'right' ) {
- // 45px = width of one button column
- this.panel.style.width = '45px';
- }
- } else {
- this.panel.classList.remove( 'no-tabs' );
- if ( Object.keys( this.tabs ).length > 0 ) {
- // Has tabs - restore to saved size only if we had set it to minimum before
- if ( this.position === 'bottom' ) {
- const currentHeight = parseInt( this.panel.style.height );
- if ( currentHeight === 32 || currentHeight === 38 ) {
- this.panel.style.height = `${ this.lastHeightBottom }px`;
- }
- } else if ( this.position === 'right' ) {
- const currentWidth = parseInt( this.panel.style.width );
- if ( currentWidth === 45 ) {
- this.panel.style.width = `${ this.lastWidthRight }px`;
- }
- }
- }
- }
- this.dispatchEvent( { type: 'resize' } );
- this.checkHeaderScroll();
- }
- checkHeaderScroll() {
- const header = this.panel.querySelector( '.profiler-header' );
- if ( header ) {
- const hasScroll = header.scrollWidth > header.clientWidth + 1;
- if ( hasScroll ) {
- this.panel.classList.add( 'has-horizontal-scroll' );
- } else {
- this.panel.classList.remove( 'has-horizontal-scroll' );
- }
- }
- }
- setupTabDragAndDrop( tab ) {
- // Always handle basic click
- tab.button.addEventListener( 'click', () => {
- if ( ! isDragging ) {
- this.setActiveTab( tab.id );
- }
- } );
- // Disable drag and drop if tab doesn't allow detach
- if ( tab.allowDetach === false ) {
- tab.button.style.cursor = 'default';
- return;
- }
- let isDragging = false;
- let startX, startY;
- let hasMoved = false;
- let previewWindow = null;
- const dragThreshold = 10; // pixels to move before starting drag
- const onDragStart = ( e ) => {
- startX = e.clientX;
- startY = e.clientY;
- isDragging = false;
- hasMoved = false;
- tab.button.setPointerCapture( e.pointerId );
- };
- const onDragMove = ( e ) => {
- const currentX = e.clientX;
- const currentY = e.clientY;
- const deltaX = Math.abs( currentX - startX );
- const deltaY = Math.abs( currentY - startY );
- if ( ! isDragging && ( deltaX > dragThreshold || deltaY > dragThreshold ) ) {
- isDragging = true;
- tab.button.style.cursor = 'grabbing';
- tab.button.style.opacity = '0.5';
- tab.button.style.transform = 'scale(1.05)';
- previewWindow = this.createPreviewWindow( tab, currentX, currentY );
- previewWindow.style.opacity = '0.8';
- }
- if ( isDragging && previewWindow ) {
- hasMoved = true;
- e.preventDefault();
- previewWindow.style.left = `${ currentX - 200 }px`;
- previewWindow.style.top = `${ currentY - 20 }px`;
- }
- };
- const onDragEnd = () => {
- if ( isDragging && hasMoved && previewWindow ) {
- const finalX = parseInt( previewWindow.style.left ) + 200;
- const finalY = parseInt( previewWindow.style.top ) + 20;
- if ( previewWindow.parentNode ) {
- previewWindow.parentNode.removeChild( previewWindow );
- }
- this.detachTab( tab, finalX, finalY );
- } else if ( ! hasMoved ) {
- this.setActiveTab( tab.id );
- if ( previewWindow && previewWindow.parentNode ) {
- previewWindow.parentNode.removeChild( previewWindow );
- }
- } else if ( previewWindow ) {
- if ( previewWindow.parentNode ) {
- previewWindow.parentNode.removeChild( previewWindow );
- }
- }
- tab.button.style.opacity = '';
- tab.button.style.transform = '';
- tab.button.style.cursor = '';
- isDragging = false;
- hasMoved = false;
- previewWindow = null;
- tab.button.removeEventListener( 'pointermove', onDragMove );
- tab.button.removeEventListener( 'pointerup', onDragEnd );
- tab.button.removeEventListener( 'pointercancel', onDragEnd );
- };
- tab.button.addEventListener( 'pointerdown', ( e ) => {
- if ( this.isMobile && e.pointerType !== 'mouse' ) return;
- onDragStart( e );
- tab.button.addEventListener( 'pointermove', onDragMove );
- tab.button.addEventListener( 'pointerup', onDragEnd );
- tab.button.addEventListener( 'pointercancel', onDragEnd );
- } );
- // Set cursor to grab for tabs that can be detached
- tab.button.style.cursor = 'grab';
- }
- createPreviewWindow( tab, x, y ) {
- const windowPanel = document.createElement( 'div' );
- windowPanel.className = 'detached-tab-panel';
- windowPanel.style.left = `${ x - 200 }px`;
- windowPanel.style.top = `${ y - 20 }px`;
- windowPanel.style.pointerEvents = 'none'; // Preview only
- // Set z-index for preview window to be on top
- this.maxZIndex ++;
- windowPanel.style.setProperty( 'z-index', this.maxZIndex, 'important' );
- const windowHeader = document.createElement( 'div' );
- windowHeader.className = 'detached-tab-header';
- const title = document.createElement( 'span' );
- title.textContent = tab.button.textContent.replace( '⇱', '' ).trim();
- windowHeader.appendChild( title );
- const headerControls = document.createElement( 'div' );
- headerControls.className = 'detached-header-controls';
- const reattachBtn = document.createElement( 'button' );
- reattachBtn.className = 'detached-reattach-btn';
- reattachBtn.innerHTML = '↩';
- headerControls.appendChild( reattachBtn );
- windowHeader.appendChild( headerControls );
- const windowContent = document.createElement( 'div' );
- windowContent.className = 'detached-tab-content';
- const resizer = document.createElement( 'div' );
- resizer.className = 'detached-tab-resizer';
- windowPanel.appendChild( resizer );
- windowPanel.appendChild( windowHeader );
- windowPanel.appendChild( windowContent );
- this.domElement.appendChild( windowPanel );
- return windowPanel;
- }
- detachTab( tab, x, y ) {
- if ( tab.isDetached ) return;
- // Check if tab allows detachment
- if ( tab.allowDetach === false ) return;
- const allButtons = Array.from( this.tabsContainer.children );
- const tabIdsInOrder = allButtons.map( btn => {
- return Object.keys( this.tabs ).find( id => this.tabs[ id ].button === btn );
- } ).filter( id => id !== undefined );
- const currentIndex = tabIdsInOrder.indexOf( tab.id );
- let newActiveTab = null;
- if ( this.activeTabId === tab.id ) {
- tab.setActive( false );
- const remainingTabs = tabIdsInOrder.filter( id =>
- id !== tab.id &&
- ! this.tabs[ id ].isDetached &&
- this.tabs[ id ].isVisible
- );
- if ( remainingTabs.length > 0 ) {
- for ( let i = currentIndex - 1; i >= 0; i -- ) {
- if ( remainingTabs.includes( tabIdsInOrder[ i ] ) ) {
- newActiveTab = tabIdsInOrder[ i ];
- break;
- }
- }
- if ( ! newActiveTab ) {
- for ( let i = currentIndex + 1; i < tabIdsInOrder.length; i ++ ) {
- if ( remainingTabs.includes( tabIdsInOrder[ i ] ) ) {
- newActiveTab = tabIdsInOrder[ i ];
- break;
- }
- }
- }
- if ( ! newActiveTab ) {
- newActiveTab = remainingTabs[ 0 ];
- }
- }
- }
- if ( tab.button.parentNode ) {
- tab.button.parentNode.removeChild( tab.button );
- }
- if ( tab.content.parentNode ) {
- tab.content.parentNode.removeChild( tab.content );
- }
- const detachedWindow = this.createDetachedWindow( tab, x, y );
- this.detachedWindows.push( detachedWindow );
- tab.isDetached = true;
- tab.detachedWindow = detachedWindow;
- if ( newActiveTab ) {
- this.setActiveTab( newActiveTab );
- } else if ( this.activeTabId === tab.id ) {
- this.activeTabId = null;
- }
- // Update panel size after detaching
- this.updatePanelSize();
- this.saveLayout();
- }
- createDetachedWindow( tab, x, y ) {
- // Constrain initial position to window bounds
- const windowWidth = window.innerWidth;
- const windowHeight = window.innerHeight;
- const estimatedWidth = 400; // Default detached window width
- const estimatedHeight = 300; // Default detached window height
- let constrainedX = x - 200;
- let constrainedY = y - 20;
- if ( constrainedX + estimatedWidth > windowWidth ) {
- constrainedX = windowWidth - estimatedWidth;
- }
- if ( constrainedX < 0 ) {
- constrainedX = 0;
- }
- if ( constrainedY + estimatedHeight > windowHeight ) {
- constrainedY = windowHeight - estimatedHeight;
- }
- if ( constrainedY < 0 ) {
- constrainedY = 0;
- }
- const windowPanel = document.createElement( 'div' );
- windowPanel.className = 'detached-tab-panel';
- windowPanel.style.left = `${ constrainedX }px`;
- windowPanel.style.top = `${ constrainedY }px`;
- if ( ! this.panel.classList.contains( 'visible' ) ) {
- windowPanel.style.opacity = '0';
- windowPanel.style.visibility = 'hidden';
- windowPanel.style.pointerEvents = 'none';
- }
- // Hide detached window if tab is not visible
- if ( ! tab.isVisible ) {
- windowPanel.style.display = 'none';
- }
- const windowHeader = document.createElement( 'div' );
- windowHeader.className = 'detached-tab-header';
- const title = document.createElement( 'span' );
- title.textContent = tab.button.textContent.replace( '⇱', '' ).trim();
- windowHeader.appendChild( title );
- const headerControls = document.createElement( 'div' );
- headerControls.className = 'detached-header-controls';
- const reattachBtn = document.createElement( 'button' );
- reattachBtn.className = 'detached-reattach-btn';
- reattachBtn.innerHTML = '↩';
- reattachBtn.title = 'Reattach to main panel';
- reattachBtn.onclick = () => this.reattachTab( tab );
- headerControls.appendChild( reattachBtn );
- windowHeader.appendChild( headerControls );
- const windowContent = document.createElement( 'div' );
- windowContent.className = 'detached-tab-content';
- windowContent.appendChild( tab.content );
- // Make sure content is visible
- tab.content.style.display = 'block';
- tab.content.classList.add( 'active' );
- // Create resize handles for all edges
- const resizerTop = document.createElement( 'div' );
- resizerTop.className = 'detached-tab-resizer-top';
- const resizerRight = document.createElement( 'div' );
- resizerRight.className = 'detached-tab-resizer-right';
- const resizerBottom = document.createElement( 'div' );
- resizerBottom.className = 'detached-tab-resizer-bottom';
- const resizerLeft = document.createElement( 'div' );
- resizerLeft.className = 'detached-tab-resizer-left';
- const resizerCorner = document.createElement( 'div' );
- resizerCorner.className = 'detached-tab-resizer';
- windowPanel.appendChild( resizerTop );
- windowPanel.appendChild( resizerRight );
- windowPanel.appendChild( resizerBottom );
- windowPanel.appendChild( resizerLeft );
- windowPanel.appendChild( resizerCorner );
- windowPanel.appendChild( windowHeader );
- windowPanel.appendChild( windowContent );
- this.domElement.appendChild( windowPanel );
- // Setup window dragging
- this.setupDetachedWindowDrag( windowPanel, windowHeader, tab );
- // Setup window resizing
- this.setupDetachedWindowResize( windowPanel, resizerTop, resizerRight, resizerBottom, resizerLeft, resizerCorner );
- // Use the same z-index that was set on the preview window
- windowPanel.style.setProperty( 'z-index', this.maxZIndex, 'important' );
- return { panel: windowPanel, tab: tab };
- }
- bringWindowToFront( windowPanel ) {
- // Increment the max z-index and apply it to the clicked window
- this.maxZIndex ++;
- windowPanel.style.setProperty( 'z-index', this.maxZIndex, 'important' );
- }
- setupDetachedWindowDrag( windowPanel, header, tab ) {
- let isDragging = false;
- let startX, startY, startLeft, startTop;
- // Bring window to front when clicking anywhere on it
- windowPanel.addEventListener( 'pointerdown', () => {
- this.bringWindowToFront( windowPanel );
- } );
- const onDragStart = ( e ) => {
- if ( e.target.classList.contains( 'detached-reattach-btn' ) ) {
- return;
- }
- // Bring window to front when starting to drag
- this.bringWindowToFront( windowPanel );
- isDragging = true;
- header.style.cursor = 'grabbing';
- header.setPointerCapture( e.pointerId );
- startX = e.clientX;
- startY = e.clientY;
- const rect = windowPanel.getBoundingClientRect();
- startLeft = rect.left;
- startTop = rect.top;
- };
- const onDragMove = ( e ) => {
- if ( ! isDragging ) return;
- e.preventDefault();
- const currentX = e.clientX;
- const currentY = e.clientY;
- const deltaX = currentX - startX;
- const deltaY = currentY - startY;
- let newLeft = startLeft + deltaX;
- let newTop = startTop + deltaY;
- // Constrain to window bounds (allow half width/height to extend outside)
- const windowWidth = window.innerWidth;
- const windowHeight = window.innerHeight;
- const panelWidth = windowPanel.offsetWidth;
- const panelHeight = windowPanel.offsetHeight;
- const halfWidth = panelWidth / 2;
- const halfHeight = panelHeight / 2;
- // Allow window to extend half its width beyond right edge
- if ( newLeft + panelWidth > windowWidth + halfWidth ) {
- newLeft = windowWidth + halfWidth - panelWidth;
- }
- // Allow window to extend half its width beyond left edge
- if ( newLeft < - halfWidth ) {
- newLeft = - halfWidth;
- }
- // Allow window to extend half its height beyond bottom edge
- if ( newTop + panelHeight > windowHeight + halfHeight ) {
- newTop = windowHeight + halfHeight - panelHeight;
- }
- // Allow window to extend half its height beyond top edge
- if ( newTop < - halfHeight ) {
- newTop = - halfHeight;
- }
- windowPanel.style.left = `${ newLeft }px`;
- windowPanel.style.top = `${ newTop }px`;
- // Check if cursor is over the inspector panel
- const panelRect = this.panel.getBoundingClientRect();
- const isOverPanel = currentX >= panelRect.left && currentX <= panelRect.right &&
- currentY >= panelRect.top && currentY <= panelRect.bottom;
- if ( isOverPanel ) {
- windowPanel.style.opacity = '0.5';
- this.panel.style.outline = '2px solid var(--accent-color)';
- } else {
- windowPanel.style.opacity = '';
- this.panel.style.outline = '';
- }
- };
- const onDragEnd = ( e ) => {
- if ( ! isDragging ) return;
- isDragging = false;
- header.style.cursor = '';
- windowPanel.style.opacity = '';
- this.panel.style.outline = '';
- // Check if dropped over the inspector panel
- const currentX = e.clientX;
- const currentY = e.clientY;
- if ( currentX !== undefined && currentY !== undefined ) {
- const panelRect = this.panel.getBoundingClientRect();
- const isOverPanel = currentX >= panelRect.left && currentX <= panelRect.right &&
- currentY >= panelRect.top && currentY <= panelRect.bottom;
- if ( isOverPanel && tab ) {
- // Reattach the tab
- this.reattachTab( tab );
- } else {
- // Save layout after moving detached window
- this.saveLayout();
- }
- }
- header.removeEventListener( 'pointermove', onDragMove );
- header.removeEventListener( 'pointerup', onDragEnd );
- header.removeEventListener( 'pointercancel', onDragEnd );
- };
- header.addEventListener( 'pointerdown', ( e ) => {
- onDragStart( e );
- header.addEventListener( 'pointermove', onDragMove );
- header.addEventListener( 'pointerup', onDragEnd );
- header.addEventListener( 'pointercancel', onDragEnd );
- } );
- header.style.cursor = 'grab';
- }
- setupDetachedWindowResize( windowPanel, resizerTop, resizerRight, resizerBottom, resizerLeft, resizerCorner ) {
- const minWidth = 250;
- const minHeight = 150;
- const setupResizer = ( resizer, direction ) => {
- let isResizing = false;
- let startX, startY, startWidth, startHeight, startLeft, startTop;
- const onResizeStart = ( e ) => {
- e.preventDefault();
- e.stopPropagation();
- isResizing = true;
- // Bring window to front when resizing
- this.bringWindowToFront( windowPanel );
- resizer.setPointerCapture( e.pointerId );
- startX = e.clientX;
- startY = e.clientY;
- startWidth = windowPanel.offsetWidth;
- startHeight = windowPanel.offsetHeight;
- startLeft = windowPanel.offsetLeft;
- startTop = windowPanel.offsetTop;
- };
- const onResizeMove = ( e ) => {
- if ( ! isResizing ) return;
- e.preventDefault();
- const currentX = e.clientX;
- const currentY = e.clientY;
- const deltaX = currentX - startX;
- const deltaY = currentY - startY;
- const windowWidth = window.innerWidth;
- const windowHeight = window.innerHeight;
- if ( direction === 'right' || direction === 'corner' ) {
- const newWidth = startWidth + deltaX;
- const maxWidth = windowWidth - startLeft;
- if ( newWidth >= minWidth && newWidth <= maxWidth ) {
- windowPanel.style.width = `${ newWidth }px`;
- }
- }
- if ( direction === 'bottom' || direction === 'corner' ) {
- const newHeight = startHeight + deltaY;
- const maxHeight = windowHeight - startTop;
- if ( newHeight >= minHeight && newHeight <= maxHeight ) {
- windowPanel.style.height = `${ newHeight }px`;
- }
- }
- if ( direction === 'left' ) {
- const newWidth = startWidth - deltaX;
- const maxLeft = startLeft + startWidth - minWidth;
- if ( newWidth >= minWidth ) {
- const newLeft = startLeft + deltaX;
- if ( newLeft >= 0 && newLeft <= maxLeft ) {
- windowPanel.style.width = `${ newWidth }px`;
- windowPanel.style.left = `${ newLeft }px`;
- }
- }
- }
- if ( direction === 'top' ) {
- const newHeight = startHeight - deltaY;
- const maxTop = startTop + startHeight - minHeight;
- if ( newHeight >= minHeight ) {
- const newTop = startTop + deltaY;
- if ( newTop >= 0 && newTop <= maxTop ) {
- windowPanel.style.height = `${ newHeight }px`;
- windowPanel.style.top = `${ newTop }px`;
- }
- }
- }
- };
- const onResizeEnd = () => {
- isResizing = false;
- resizer.removeEventListener( 'pointermove', onResizeMove );
- resizer.removeEventListener( 'pointerup', onResizeEnd );
- resizer.removeEventListener( 'pointercancel', onResizeEnd );
- // Save layout after resizing detached window
- this.saveLayout();
- };
- resizer.addEventListener( 'pointerdown', ( e ) => {
- onResizeStart( e );
- resizer.addEventListener( 'pointermove', onResizeMove );
- resizer.addEventListener( 'pointerup', onResizeEnd );
- resizer.addEventListener( 'pointercancel', onResizeEnd );
- } );
- };
- // Setup all resizers
- setupResizer( resizerTop, 'top' );
- setupResizer( resizerRight, 'right' );
- setupResizer( resizerBottom, 'bottom' );
- setupResizer( resizerLeft, 'left' );
- setupResizer( resizerCorner, 'corner' );
- }
- reattachTab( tab ) {
- if ( ! tab.isDetached ) return;
- if ( tab.detachedWindow ) {
- const index = this.detachedWindows.indexOf( tab.detachedWindow );
- if ( index > - 1 ) {
- this.detachedWindows.splice( index, 1 );
- }
- if ( tab.detachedWindow.panel.parentNode ) {
- tab.detachedWindow.panel.parentNode.removeChild( tab.detachedWindow.panel );
- }
- tab.detachedWindow = null;
- }
- tab.isDetached = false;
- // Get all tabs and sort by their original index to determine the correct order
- const allTabs = Object.values( this.tabs );
- const allTabsSorted = allTabs
- .filter( t => t.originalIndex !== undefined && t.isVisible )
- .sort( ( a, b ) => a.originalIndex - b.originalIndex );
- // Get currently attached tab buttons
- const currentButtons = Array.from( this.tabsContainer.children );
- // Find the correct position for this tab
- let insertIndex = 0;
- for ( const t of allTabsSorted ) {
- if ( t.id === tab.id ) {
- break;
- }
- // Count only non-detached, non-builtin tabs that come before this one
- if ( ! t.isDetached && ! t.builtin ) {
- insertIndex ++;
- }
- }
- // Insert the button at the correct position
- if ( insertIndex >= currentButtons.length || currentButtons.length === 0 ) {
- // If insert index is beyond current buttons, or no buttons exist, append at the end
- this.tabsContainer.appendChild( tab.button );
- } else {
- // Insert before the button at the insert index
- this.tabsContainer.insertBefore( tab.button, currentButtons[ insertIndex ] );
- }
- this.contentWrapper.appendChild( tab.content );
- this.setActiveTab( tab.id );
- // Update panel size after reattaching
- this.updatePanelSize();
- this.saveLayout();
- }
- setActiveTab( id ) {
- if ( this.activeTabId && this.tabs[ this.activeTabId ] && ! this.tabs[ this.activeTabId ].isDetached ) {
- this.tabs[ this.activeTabId ].setActive( false );
- }
- this.activeTabId = id;
- if ( this.tabs[ id ] ) {
- const tab = this.tabs[ id ];
- if ( ! tab.isVisible ) {
- tab.show();
- }
- tab.setActive( true );
- }
- this.saveLayout();
- this.checkHeaderScroll();
- }
- togglePanel() {
- this.panel.classList.toggle( 'visible' );
- this.toggleButton.classList.toggle( 'panel-open' );
- this.miniPanel.classList.toggle( 'panel-open' );
- const isVisible = this.panel.classList.contains( 'visible' );
- if ( isVisible && this.activeTabId && this.tabs[ this.activeTabId ] ) {
- this.tabs[ this.activeTabId ].setActive( true );
- }
- this.detachedWindows.forEach( detachedWindow => {
- if ( isVisible ) {
- detachedWindow.panel.style.opacity = '';
- detachedWindow.panel.style.visibility = '';
- detachedWindow.panel.style.pointerEvents = '';
- } else {
- detachedWindow.panel.style.opacity = '0';
- detachedWindow.panel.style.visibility = 'hidden';
- detachedWindow.panel.style.pointerEvents = 'none';
- }
- } );
- this.dispatchEvent( { type: 'resize' } );
- this.saveLayout();
- }
- togglePosition() {
- const newPosition = this.position === 'bottom' ? 'right' : 'bottom';
- this.setPosition( newPosition );
- }
- setPosition( targetPosition ) {
- if ( this.position === targetPosition ) return;
- this.panel.style.transition = 'none';
- // Check if panel is currently maximized
- const isMaximized = this.panel.classList.contains( 'maximized' );
- if ( targetPosition === 'right' ) {
- this.position = 'right';
- this.floatingBtn.classList.add( 'active' );
- this.floatingBtn.innerHTML = '<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="3" width="18" height="18" rx="2" ry="2"></rect><path d="M3 15h18"></path></svg>';
- this.floatingBtn.title = 'Switch to Bottom';
- // Apply right position styles
- this.panel.classList.remove( 'position-bottom' );
- this.panel.classList.add( 'position-right' );
- this.toggleButton.classList.add( 'position-right' );
- this.miniPanel.classList.add( 'position-right' );
- this.panel.style.bottom = '';
- this.panel.style.top = '0';
- this.panel.style.right = '0';
- this.panel.style.left = '';
- // Apply size based on maximized state
- if ( isMaximized ) {
- this.panel.style.width = '100vw';
- this.panel.style.height = '100%';
- } else {
- this.panel.style.width = `${ this.lastWidthRight }px`;
- this.panel.style.height = '100%';
- }
- } else {
- this.position = 'bottom';
- this.floatingBtn.classList.remove( 'active' );
- this.floatingBtn.innerHTML = '<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="3" width="18" height="18" rx="2" ry="2"></rect><line x1="15" y1="3" x2="15" y2="21"></line></svg>';
- this.floatingBtn.title = 'Switch to Right Side';
- // Apply bottom position styles
- this.panel.classList.remove( 'position-right' );
- this.panel.classList.add( 'position-bottom' );
- this.toggleButton.classList.remove( 'position-right' );
- this.miniPanel.classList.remove( 'position-right' );
- this.panel.style.top = '';
- this.panel.style.right = '';
- this.panel.style.bottom = '0';
- this.panel.style.left = '0';
- // Apply size based on maximized state
- if ( isMaximized ) {
- this.panel.style.width = '100%';
- this.panel.style.height = '100vh';
- } else {
- this.panel.style.width = '100%';
- this.panel.style.height = `${ this.lastHeightBottom }px`;
- }
- }
- // Re-enable transition after a brief delay
- setTimeout( () => {
- this.panel.style.transition = '';
- }, 50 );
- // Update panel size based on visible tabs
- this.updatePanelSize();
- // Save layout after position change
- this.saveLayout();
- }
- saveLayout() {
- if ( this.isLoadingLayout ) return;
- const layout = {
- position: this.position,
- lastHeightBottom: this.lastHeightBottom,
- lastWidthRight: this.lastWidthRight,
- activeTabId: this.activeTabId,
- detachedTabs: [],
- isVisible: this.panel.classList.contains( 'visible' )
- };
- // Save detached windows state
- this.detachedWindows.forEach( detachedWindow => {
- const tab = detachedWindow.tab;
- const panel = detachedWindow.panel;
- // Get position values, ensuring they're valid numbers
- const left = parseFloat( panel.style.left ) || panel.offsetLeft || 0;
- const top = parseFloat( panel.style.top ) || panel.offsetTop || 0;
- const width = panel.offsetWidth;
- const height = panel.offsetHeight;
- layout.detachedTabs.push( {
- tabId: tab.id,
- originalIndex: tab.originalIndex !== undefined ? tab.originalIndex : 0,
- left: left,
- top: top,
- width: width,
- height: height
- } );
- } );
- try {
- setItem( 'layout', layout );
- } catch ( e ) {
- console.warn( 'Failed to save profiler layout:', e );
- }
- }
- loadLayout() {
- this.isLoadingLayout = true;
- try {
- const layout = getItem( 'layout' );
- if ( Object.keys( layout ).length === 0 ) return;
- // Constrain detached tabs positions to current screen bounds
- if ( layout.detachedTabs && layout.detachedTabs.length > 0 ) {
- const windowWidth = window.innerWidth;
- const windowHeight = window.innerHeight;
- layout.detachedTabs = layout.detachedTabs.map( detachedTabData => {
- let { left, top, width, height } = detachedTabData;
- // Ensure width and height are within bounds
- if ( width > windowWidth ) {
- width = windowWidth - 100; // Leave some margin
- }
- if ( height > windowHeight ) {
- height = windowHeight - 100; // Leave some margin
- }
- // Allow window to extend half its width/height outside the screen
- const halfWidth = width / 2;
- const halfHeight = height / 2;
- // Constrain horizontal position (allow half width to extend beyond right edge)
- if ( left + width > windowWidth + halfWidth ) {
- left = windowWidth + halfWidth - width;
- }
- // Constrain horizontal position (allow half width to extend beyond left edge)
- if ( left < - halfWidth ) {
- left = - halfWidth;
- }
- // Constrain vertical position (allow half height to extend beyond bottom edge)
- if ( top + height > windowHeight + halfHeight ) {
- top = windowHeight + halfHeight - height;
- }
- // Constrain vertical position (allow half height to extend beyond top edge)
- if ( top < - halfHeight ) {
- top = - halfHeight;
- }
- return {
- ...detachedTabData,
- left,
- top,
- width,
- height
- };
- } );
- }
- // Restore position and dimensions
- if ( layout.position ) {
- this.position = layout.position;
- }
- if ( layout.lastHeightBottom ) {
- this.lastHeightBottom = layout.lastHeightBottom;
- }
- if ( layout.lastWidthRight ) {
- this.lastWidthRight = layout.lastWidthRight;
- }
- // Constrain saved dimensions to current screen bounds
- const windowWidth = window.innerWidth;
- const windowHeight = window.innerHeight;
- if ( this.lastHeightBottom > windowHeight - 50 ) {
- this.lastHeightBottom = windowHeight - 50;
- }
- if ( this.lastWidthRight > windowWidth - 50 ) {
- this.lastWidthRight = windowWidth - 50;
- }
- // Apply the saved position after shell is set up
- if ( this.position === 'right' ) {
- this.floatingBtn.classList.add( 'active' );
- this.floatingBtn.innerHTML = '<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="3" width="18" height="18" rx="2" ry="2"></rect><path d="M3 15h18"></path></svg>';
- this.floatingBtn.title = 'Switch to Bottom';
- this.panel.classList.remove( 'position-bottom' );
- this.panel.classList.add( 'position-right' );
- this.toggleButton.classList.add( 'position-right' );
- this.miniPanel.classList.add( 'position-right' );
- this.panel.style.bottom = '';
- this.panel.style.top = '0';
- this.panel.style.right = '0';
- this.panel.style.left = '';
- this.panel.style.width = `${ this.lastWidthRight }px`;
- this.panel.style.height = '100%';
- } else {
- this.panel.style.height = `${ this.lastHeightBottom }px`;
- }
- if ( layout.isVisible ) {
- this.panel.classList.add( 'visible' );
- this.toggleButton.classList.add( 'panel-open' );
- }
- if ( layout.activeTabId ) {
- this.setActiveTab( layout.activeTabId );
- }
- if ( layout.detachedTabs && layout.detachedTabs.length > 0 ) {
- this.pendingDetachedTabs = layout.detachedTabs;
- this.restoreDetachedTabs();
- }
- // Update panel size after loading layout
- this.updatePanelSize();
- // Ensure initial open state applies to mini panel as well
- if ( this.panel.classList.contains( 'visible' ) ) {
- this.miniPanel.classList.add( 'panel-open' );
- }
- } catch ( e ) {
- console.warn( 'Failed to load profiler layout:', e );
- } finally {
- this.isLoadingLayout = false;
- }
- }
- restoreDetachedTabs() {
- if ( ! this.pendingDetachedTabs || this.pendingDetachedTabs.length === 0 ) return;
- this.pendingDetachedTabs.forEach( detachedTabData => {
- const tab = this.tabs[ detachedTabData.tabId ];
- if ( ! tab || tab.isDetached ) return;
- // Restore originalIndex if saved
- if ( detachedTabData.originalIndex !== undefined ) {
- tab.originalIndex = detachedTabData.originalIndex;
- }
- if ( tab.button.parentNode ) {
- tab.button.parentNode.removeChild( tab.button );
- }
- if ( tab.content.parentNode ) {
- tab.content.parentNode.removeChild( tab.content );
- }
- const detachedWindow = this.createDetachedWindow( tab, 0, 0 );
- detachedWindow.panel.style.left = `${ detachedTabData.left }px`;
- detachedWindow.panel.style.top = `${ detachedTabData.top }px`;
- detachedWindow.panel.style.width = `${ detachedTabData.width }px`;
- detachedWindow.panel.style.height = `${ detachedTabData.height }px`;
- // Constrain window to bounds after restoring position and size
- this.constrainWindowToBounds( detachedWindow.panel );
- this.detachedWindows.push( detachedWindow );
- tab.isDetached = true;
- tab.detachedWindow = detachedWindow;
- } );
- this.pendingDetachedTabs = null;
- // Update maxZIndex to be higher than all existing windows
- this.detachedWindows.forEach( detachedWindow => {
- const currentZIndex = parseInt( getComputedStyle( detachedWindow.panel ).zIndex ) || 0;
- if ( currentZIndex > this.maxZIndex ) {
- this.maxZIndex = currentZIndex;
- }
- } );
- const needsNewActiveTab = ! this.activeTabId ||
- ! this.tabs[ this.activeTabId ] ||
- this.tabs[ this.activeTabId ].isDetached ||
- ! this.tabs[ this.activeTabId ].isVisible;
- if ( needsNewActiveTab ) {
- const tabIds = Object.keys( this.tabs );
- const availableTabs = tabIds.filter( id =>
- ! this.tabs[ id ].isDetached &&
- this.tabs[ id ].isVisible
- );
- if ( availableTabs.length > 0 ) {
- const buttons = Array.from( this.tabsContainer.children );
- const orderedTabIds = buttons.map( btn => {
- return Object.keys( this.tabs ).find( id => this.tabs[ id ].button === btn );
- } ).filter( id =>
- id !== undefined &&
- ! this.tabs[ id ].isDetached &&
- this.tabs[ id ].isVisible
- );
- this.setActiveTab( orderedTabIds[ 0 ] || availableTabs[ 0 ] );
- } else {
- this.activeTabId = null;
- }
- }
- // Update panel size after restoring detached tabs
- this.updatePanelSize();
- }
- }
|