Profiler.js 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072
  1. import { EventDispatcher } from 'three';
  2. import { Style } from './Style.js';
  3. import { getItem, setItem } from '../Inspector.js';
  4. export class Profiler extends EventDispatcher {
  5. constructor( inspector ) {
  6. super();
  7. this.inspector = inspector;
  8. this.tabs = {};
  9. this.activeTabId = null;
  10. this.isResizing = false;
  11. this.lastHeightBottom = 350; // Height for bottom position
  12. this.lastWidthRight = 450; // Width for right position
  13. this.position = 'bottom'; // 'bottom' or 'right'
  14. this.detachedWindows = []; // Array to store detached tab windows
  15. this.maxZIndex = 1002; // Track the highest z-index for detached windows (starts at base z-index from CSS)
  16. this.nextTabOriginalIndex = 0; // Track the original order of tabs as they are added
  17. Style.init();
  18. this.setupShell();
  19. this.setupResizing();
  20. // Setup window resize listener and update mobile status
  21. this.setupWindowResizeListener();
  22. // Setup orientation change listener for mobile devices
  23. this.setupOrientationListener();
  24. }
  25. getSize() {
  26. if ( this.panel.classList.contains( 'visible' ) === false || this.panel.classList.contains( 'no-tabs' ) ) {
  27. return { width: 0, height: 0 };
  28. }
  29. if ( this.position === 'right' ) {
  30. return { width: this.panel.offsetWidth, height: 0 };
  31. } else {
  32. return { width: 0, height: this.panel.offsetHeight };
  33. }
  34. }
  35. get isMobile() {
  36. return this.detectMobile();
  37. }
  38. get isSmallScreen() {
  39. return window.innerWidth <= 768;
  40. }
  41. detectMobile() {
  42. // Check for mobile devices
  43. const userAgent = navigator.userAgent || navigator.vendor || window.opera;
  44. const isMobileUA = /android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini/i.test( userAgent );
  45. const isTouchDevice = ( 'ontouchstart' in window ) || ( navigator.maxTouchPoints > 0 );
  46. return isMobileUA || ( isTouchDevice && this.isSmallScreen );
  47. }
  48. setupOrientationListener() {
  49. const handleOrientationChange = () => {
  50. if ( ! this.isMobile ) return;
  51. // Check if device is in landscape or portrait mode
  52. const isLandscape = window.innerWidth > window.innerHeight;
  53. // In landscape mode, use right position (vertical panel)
  54. // In portrait mode, use bottom position (horizontal panel)
  55. const targetPosition = isLandscape ? 'right' : 'bottom';
  56. if ( this.position !== targetPosition ) {
  57. this.setPosition( targetPosition );
  58. }
  59. };
  60. // Initial check
  61. handleOrientationChange();
  62. // Listen for orientation changes
  63. window.addEventListener( 'orientationchange', handleOrientationChange );
  64. window.addEventListener( 'resize', handleOrientationChange );
  65. }
  66. setupWindowResizeListener() {
  67. const constrainDetachedWindows = () => {
  68. this.detachedWindows.forEach( detachedWindow => {
  69. this.constrainWindowToBounds( detachedWindow.panel );
  70. } );
  71. };
  72. const constrainMainPanel = () => {
  73. // Skip if panel is maximized (it should always fill the screen)
  74. if ( this.panel.classList.contains( 'maximized' ) ) return;
  75. const windowWidth = window.innerWidth;
  76. const windowHeight = window.innerHeight;
  77. if ( this.position === 'bottom' ) {
  78. const currentHeight = this.panel.offsetHeight;
  79. const maxHeight = windowHeight - 50; // Leave 50px margin
  80. if ( currentHeight > maxHeight ) {
  81. this.panel.style.height = `${ maxHeight }px`;
  82. this.lastHeightBottom = maxHeight;
  83. }
  84. } else if ( this.position === 'right' ) {
  85. const currentWidth = this.panel.offsetWidth;
  86. const maxWidth = windowWidth - 50; // Leave 50px margin
  87. if ( currentWidth > maxWidth ) {
  88. this.panel.style.width = `${ maxWidth }px`;
  89. this.lastWidthRight = maxWidth;
  90. }
  91. }
  92. };
  93. // Listen for window resize events
  94. window.addEventListener( 'resize', () => {
  95. if ( this.isSmallScreen ) {
  96. this.floatingBtn.style.display = 'none';
  97. this.panel.classList.add( 'hide-position-toggle' );
  98. } else {
  99. this.floatingBtn.style.display = '';
  100. this.panel.classList.remove( 'hide-position-toggle' );
  101. }
  102. if ( this.isMobile ) {
  103. this.panel.classList.add( 'is-mobile' );
  104. } else {
  105. this.panel.classList.remove( 'is-mobile' );
  106. }
  107. constrainDetachedWindows();
  108. constrainMainPanel();
  109. } );
  110. }
  111. constrainWindowToBounds( windowPanel ) {
  112. const windowWidth = window.innerWidth;
  113. const windowHeight = window.innerHeight;
  114. const panelWidth = windowPanel.offsetWidth;
  115. const panelHeight = windowPanel.offsetHeight;
  116. let left = parseFloat( windowPanel.style.left ) || windowPanel.offsetLeft || 0;
  117. let top = parseFloat( windowPanel.style.top ) || windowPanel.offsetTop || 0;
  118. // Allow window to extend half its width/height outside the screen
  119. const halfWidth = panelWidth / 2;
  120. const halfHeight = panelHeight / 2;
  121. // Constrain horizontal position (allow half width to extend beyond right edge)
  122. if ( left + panelWidth > windowWidth + halfWidth ) {
  123. left = windowWidth + halfWidth - panelWidth;
  124. }
  125. // Constrain horizontal position (allow half width to extend beyond left edge)
  126. if ( left < - halfWidth ) {
  127. left = - halfWidth;
  128. }
  129. // Constrain vertical position (allow half height to extend beyond bottom edge)
  130. if ( top + panelHeight > windowHeight + halfHeight ) {
  131. top = windowHeight + halfHeight - panelHeight;
  132. }
  133. // Constrain vertical position (allow half height to extend beyond top edge)
  134. if ( top < - halfHeight ) {
  135. top = - halfHeight;
  136. }
  137. // Apply constrained position
  138. windowPanel.style.left = `${ left }px`;
  139. windowPanel.style.top = `${ top }px`;
  140. }
  141. setupShell() {
  142. this.domElement = document.createElement( 'div' );
  143. this.domElement.id = 'profiler-shell';
  144. this.toggleButton = document.createElement( 'button' );
  145. this.toggleButton.id = 'profiler-toggle';
  146. this.toggleButton.innerHTML = `
  147. <span id="builtin-tabs-container"></span>
  148. <span id="toggle-text">
  149. <span id="fps-counter">-</span>
  150. <span class="fps-label">FPS</span>
  151. </span>
  152. <span id="toggle-icon">
  153. <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>
  154. </span>
  155. `;
  156. this.toggleButton.onclick = () => this.togglePanel();
  157. this.builtinTabsContainer = this.toggleButton.querySelector( '#builtin-tabs-container' );
  158. // Create mini-panel for builtin tabs (shown when panel is hidden)
  159. this.miniPanel = document.createElement( 'div' );
  160. this.miniPanel.id = 'profiler-mini-panel';
  161. this.miniPanel.className = 'profiler-mini-panel';
  162. this.panel = document.createElement( 'div' );
  163. this.panel.id = 'profiler-panel';
  164. const header = document.createElement( 'div' );
  165. header.className = 'profiler-header';
  166. // Enable horizontal scrolling with vertical mouse wheel
  167. header.addEventListener( 'wheel', ( e ) => {
  168. if ( e.deltaY !== 0 ) {
  169. e.preventDefault();
  170. header.scrollLeft += e.deltaY * .25;
  171. }
  172. }, { passive: false } );
  173. this.tabsContainer = document.createElement( 'div' );
  174. this.tabsContainer.className = 'profiler-tabs';
  175. const controls = document.createElement( 'div' );
  176. controls.className = 'profiler-controls';
  177. this.floatingBtn = document.createElement( 'button' );
  178. this.floatingBtn.id = 'floating-btn';
  179. this.floatingBtn.title = 'Switch to Right Side';
  180. 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>';
  181. this.floatingBtn.onclick = () => this.togglePosition();
  182. // Hide position toggle button on small screens
  183. if ( this.isSmallScreen ) {
  184. this.floatingBtn.style.display = 'none';
  185. this.panel.classList.add( 'hide-position-toggle' );
  186. }
  187. if ( this.isMobile ) {
  188. this.panel.classList.add( 'is-mobile' );
  189. }
  190. this.maximizeBtn = document.createElement( 'button' );
  191. this.maximizeBtn.id = 'maximize-btn';
  192. 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>';
  193. this.maximizeBtn.onclick = () => this.toggleMaximize();
  194. const hideBtn = document.createElement( 'button' );
  195. hideBtn.id = 'hide-panel-btn';
  196. hideBtn.textContent = '-';
  197. hideBtn.onclick = () => this.togglePanel();
  198. controls.append( this.floatingBtn, this.maximizeBtn, hideBtn );
  199. header.append( this.tabsContainer, controls );
  200. this.contentWrapper = document.createElement( 'div' );
  201. this.contentWrapper.className = 'profiler-content-wrapper';
  202. const resizer = document.createElement( 'div' );
  203. resizer.className = 'panel-resizer';
  204. this.panel.append( resizer, header, this.contentWrapper );
  205. this.domElement.append( this.toggleButton, this.miniPanel, this.panel );
  206. // Set initial position class
  207. this.panel.classList.add( `position-${this.position}` );
  208. if ( this.position === 'right' ) {
  209. this.toggleButton.classList.add( 'position-right' );
  210. this.miniPanel.classList.add( 'position-right' );
  211. }
  212. }
  213. setupResizing() {
  214. const resizer = this.panel.querySelector( '.panel-resizer' );
  215. const onStart = ( e ) => {
  216. this.isResizing = true;
  217. this.panel.classList.add( 'resizing' );
  218. resizer.setPointerCapture( e.pointerId );
  219. const startX = e.clientX;
  220. const startY = e.clientY;
  221. const startHeight = this.panel.offsetHeight;
  222. const startWidth = this.panel.offsetWidth;
  223. const onMove = ( moveEvent ) => {
  224. if ( ! this.isResizing ) return;
  225. moveEvent.preventDefault();
  226. const currentX = moveEvent.clientX;
  227. const currentY = moveEvent.clientY;
  228. if ( this.position === 'bottom' ) {
  229. // Vertical resize for bottom position
  230. const newHeight = startHeight - ( currentY - startY );
  231. if ( newHeight > 100 && newHeight < window.innerHeight - 50 ) {
  232. this.panel.style.height = `${ newHeight }px`;
  233. }
  234. } else if ( this.position === 'right' ) {
  235. // Horizontal resize for right position
  236. const newWidth = startWidth - ( currentX - startX );
  237. if ( newWidth > 200 && newWidth < window.innerWidth - 50 ) {
  238. this.panel.style.width = `${ newWidth }px`;
  239. }
  240. }
  241. this.dispatchEvent( { type: 'resize' } );
  242. };
  243. const onEnd = () => {
  244. this.isResizing = false;
  245. this.panel.classList.remove( 'resizing' );
  246. resizer.removeEventListener( 'pointermove', onMove );
  247. resizer.removeEventListener( 'pointerup', onEnd );
  248. resizer.removeEventListener( 'pointercancel', onEnd );
  249. if ( ! this.panel.classList.contains( 'maximized' ) ) {
  250. // Save dimensions based on current position
  251. if ( this.position === 'bottom' ) {
  252. this.lastHeightBottom = this.panel.offsetHeight;
  253. } else if ( this.position === 'right' ) {
  254. this.lastWidthRight = this.panel.offsetWidth;
  255. }
  256. // Save layout after resize
  257. this.saveLayout();
  258. }
  259. };
  260. resizer.addEventListener( 'pointermove', onMove );
  261. resizer.addEventListener( 'pointerup', onEnd );
  262. resizer.addEventListener( 'pointercancel', onEnd );
  263. };
  264. resizer.addEventListener( 'pointerdown', onStart );
  265. }
  266. toggleMaximize() {
  267. if ( this.panel.classList.contains( 'maximized' ) ) {
  268. this.panel.classList.remove( 'maximized' );
  269. // Restore size based on current position
  270. if ( this.position === 'bottom' ) {
  271. this.panel.style.height = `${ this.lastHeightBottom }px`;
  272. this.panel.style.width = '100%';
  273. } else if ( this.position === 'right' ) {
  274. this.panel.style.height = '100%';
  275. this.panel.style.width = `${ this.lastWidthRight }px`;
  276. }
  277. 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>';
  278. } else {
  279. // Save current size before maximizing
  280. if ( this.position === 'bottom' ) {
  281. this.lastHeightBottom = this.panel.offsetHeight;
  282. } else if ( this.position === 'right' ) {
  283. this.lastWidthRight = this.panel.offsetWidth;
  284. }
  285. this.panel.classList.add( 'maximized' );
  286. // Maximize based on current position
  287. if ( this.position === 'bottom' ) {
  288. this.panel.style.height = '100vh';
  289. this.panel.style.width = '100%';
  290. } else if ( this.position === 'right' ) {
  291. this.panel.style.height = '100%';
  292. this.panel.style.width = '100vw';
  293. }
  294. 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>';
  295. }
  296. this.dispatchEvent( { type: 'resize' } );
  297. }
  298. hide() {
  299. this.miniPanel.classList.remove( 'visible' );
  300. this.miniPanel.querySelectorAll( '.mini-panel-content' ).forEach( content => {
  301. content.style.display = 'none';
  302. } );
  303. this.builtinTabsContainer.querySelectorAll( '.builtin-tab-btn' ).forEach( btn => {
  304. btn.classList.remove( 'active' );
  305. } );
  306. }
  307. show( tab ) {
  308. this.hide();
  309. tab.builtinButton.classList.add( 'active' );
  310. if ( ! tab.miniContent.firstChild ) {
  311. while ( tab.content.firstChild ) {
  312. tab.miniContent.appendChild( tab.content.firstChild );
  313. }
  314. }
  315. tab.miniContent.style.display = 'block';
  316. this.miniPanel.classList.add( 'visible' );
  317. }
  318. addTab( tab ) {
  319. this.tabs[ tab.id ] = tab;
  320. // Assign a permanent original index to this tab
  321. tab.originalIndex = this.nextTabOriginalIndex ++;
  322. // Add visual indicator for tabs that cannot be detached
  323. if ( tab.allowDetach === false ) {
  324. tab.button.classList.add( 'no-detach' );
  325. }
  326. // Set visibility change callback
  327. tab.onVisibilityChange = () => this.updatePanelSize();
  328. this.setupTabDragAndDrop( tab );
  329. if ( ! tab.builtin ) {
  330. this.tabsContainer.appendChild( tab.button );
  331. }
  332. this.contentWrapper.appendChild( tab.content );
  333. // Apply the current visibility state to the DOM elements
  334. if ( ! tab.isVisible ) {
  335. tab.button.style.display = 'none';
  336. tab.content.style.display = 'none';
  337. }
  338. // If tab is builtin, add it to the profiler-toggle button
  339. if ( tab.builtin ) {
  340. this.addBuiltinTab( tab );
  341. }
  342. // Update panel size when tabs change
  343. this.updatePanelSize();
  344. // Set profiler reference
  345. tab.profiler = this;
  346. }
  347. addBuiltinTab( tab ) {
  348. // Create a button for the builtin tab in the profiler-toggle
  349. const builtinButton = document.createElement( 'button' );
  350. builtinButton.className = 'builtin-tab-btn';
  351. // Use icon if provided, otherwise use first letter
  352. if ( tab.icon ) {
  353. builtinButton.innerHTML = tab.icon;
  354. } else {
  355. builtinButton.textContent = tab.button.textContent.charAt( 0 ).toUpperCase();
  356. }
  357. builtinButton.title = tab.button.textContent;
  358. // Create mini-panel content container for this tab
  359. const miniContent = document.createElement( 'div' );
  360. miniContent.className = 'mini-panel-content';
  361. miniContent.style.display = 'none';
  362. // Store references in the tab object
  363. tab.builtinButton = builtinButton;
  364. tab.miniContent = miniContent;
  365. this.miniPanel.appendChild( miniContent );
  366. builtinButton.onclick = ( e ) => {
  367. e.stopPropagation(); // Prevent toggle panel from triggering
  368. // Toggle mini-panel for this tab
  369. const isCurrentlyActive = miniContent.style.display !== 'none' && miniContent.children.length > 0;
  370. if ( isCurrentlyActive ) {
  371. this.hide();
  372. } else {
  373. this.show( tab );
  374. }
  375. };
  376. this.builtinTabsContainer.appendChild( builtinButton );
  377. // Store references
  378. tab.builtinButton = builtinButton;
  379. tab.miniContent = miniContent;
  380. // If the tab was hidden before being added, hide the builtin button
  381. if ( ! tab.isVisible ) {
  382. builtinButton.style.display = 'none';
  383. miniContent.style.display = 'none';
  384. // Hide the builtin-tabs-container if all builtin buttons are hidden
  385. const hasVisibleBuiltinButtons = Array.from( this.builtinTabsContainer.querySelectorAll( '.builtin-tab-btn' ) )
  386. .some( btn => btn.style.display !== 'none' );
  387. if ( ! hasVisibleBuiltinButtons ) {
  388. this.builtinTabsContainer.style.display = 'none';
  389. }
  390. }
  391. }
  392. removeTab( tab ) {
  393. if ( ! tab || this.tabs[ tab.id ] === undefined ) return;
  394. delete this.tabs[ tab.id ];
  395. if ( tab.isDetached && tab.detachedWindow ) {
  396. if ( tab.detachedWindow.panel && tab.detachedWindow.panel.parentNode ) {
  397. tab.detachedWindow.panel.parentNode.removeChild( tab.detachedWindow.panel );
  398. }
  399. const index = this.detachedWindows.indexOf( tab.detachedWindow );
  400. if ( index !== - 1 ) {
  401. this.detachedWindows.splice( index, 1 );
  402. }
  403. }
  404. if ( ! tab.builtin ) {
  405. if ( tab.button && tab.button.parentNode ) {
  406. tab.button.parentNode.removeChild( tab.button );
  407. }
  408. } else {
  409. if ( tab.builtinButton && tab.builtinButton.parentNode ) {
  410. tab.builtinButton.parentNode.removeChild( tab.builtinButton );
  411. }
  412. if ( tab.miniContent && tab.miniContent.parentNode ) {
  413. tab.miniContent.parentNode.removeChild( tab.miniContent );
  414. }
  415. // Clean up builtin container if empty
  416. const hasVisibleBuiltinButtons = Array.from( this.builtinTabsContainer.querySelectorAll( '.builtin-tab-btn' ) )
  417. .some( btn => btn.style.display !== 'none' );
  418. if ( ! hasVisibleBuiltinButtons ) {
  419. this.builtinTabsContainer.style.display = 'none';
  420. }
  421. }
  422. if ( tab.content && tab.content.parentNode ) {
  423. tab.content.parentNode.removeChild( tab.content );
  424. }
  425. if ( this.activeTabId === tab.id ) {
  426. this.activeTabId = null;
  427. // Try to activate another tab
  428. const remainingTabs = Object.values( this.tabs ).filter( t => ! t.isDetached && t.isVisible );
  429. if ( remainingTabs.length > 0 ) {
  430. this.setActiveTab( remainingTabs[ 0 ].id );
  431. } else {
  432. this.updatePanelSize();
  433. }
  434. } else {
  435. this.updatePanelSize();
  436. }
  437. tab.onVisibilityChange = null;
  438. tab.profiler = null;
  439. }
  440. updatePanelSize() {
  441. // Check if there are any visible tabs in the panel
  442. const hasVisibleTabs = Object.values( this.tabs ).some( tab => ! tab.isDetached && tab.isVisible );
  443. // Add or remove CSS class to indicate no tabs state
  444. if ( ! hasVisibleTabs ) {
  445. this.panel.classList.add( 'no-tabs' );
  446. // If maximized and no tabs, restore to normal size
  447. if ( this.panel.classList.contains( 'maximized' ) ) {
  448. this.panel.classList.remove( 'maximized' );
  449. 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>';
  450. }
  451. // No tabs visible - set to minimum size
  452. if ( this.position === 'bottom' ) {
  453. this.panel.style.height = '38px';
  454. } else if ( this.position === 'right' ) {
  455. // 45px = width of one button column
  456. this.panel.style.width = '45px';
  457. }
  458. } else {
  459. this.panel.classList.remove( 'no-tabs' );
  460. if ( Object.keys( this.tabs ).length > 0 ) {
  461. // Has tabs - restore to saved size only if we had set it to minimum before
  462. if ( this.position === 'bottom' ) {
  463. const currentHeight = parseInt( this.panel.style.height );
  464. if ( currentHeight === 38 ) {
  465. this.panel.style.height = `${ this.lastHeightBottom }px`;
  466. }
  467. } else if ( this.position === 'right' ) {
  468. const currentWidth = parseInt( this.panel.style.width );
  469. if ( currentWidth === 45 ) {
  470. this.panel.style.width = `${ this.lastWidthRight }px`;
  471. }
  472. }
  473. }
  474. }
  475. this.dispatchEvent( { type: 'resize' } );
  476. }
  477. setupTabDragAndDrop( tab ) {
  478. // Always handle basic click
  479. tab.button.addEventListener( 'click', () => {
  480. if ( ! isDragging ) {
  481. this.setActiveTab( tab.id );
  482. }
  483. } );
  484. // Disable drag and drop if tab doesn't allow detach
  485. if ( tab.allowDetach === false ) {
  486. tab.button.style.cursor = 'default';
  487. return;
  488. }
  489. let isDragging = false;
  490. let startX, startY;
  491. let hasMoved = false;
  492. let previewWindow = null;
  493. const dragThreshold = 10; // pixels to move before starting drag
  494. const onDragStart = ( e ) => {
  495. startX = e.clientX;
  496. startY = e.clientY;
  497. isDragging = false;
  498. hasMoved = false;
  499. tab.button.setPointerCapture( e.pointerId );
  500. };
  501. const onDragMove = ( e ) => {
  502. const currentX = e.clientX;
  503. const currentY = e.clientY;
  504. const deltaX = Math.abs( currentX - startX );
  505. const deltaY = Math.abs( currentY - startY );
  506. if ( ! isDragging && ( deltaX > dragThreshold || deltaY > dragThreshold ) ) {
  507. isDragging = true;
  508. tab.button.style.cursor = 'grabbing';
  509. tab.button.style.opacity = '0.5';
  510. tab.button.style.transform = 'scale(1.05)';
  511. previewWindow = this.createPreviewWindow( tab, currentX, currentY );
  512. previewWindow.style.opacity = '0.8';
  513. }
  514. if ( isDragging && previewWindow ) {
  515. hasMoved = true;
  516. e.preventDefault();
  517. previewWindow.style.left = `${ currentX - 200 }px`;
  518. previewWindow.style.top = `${ currentY - 20 }px`;
  519. }
  520. };
  521. const onDragEnd = () => {
  522. if ( isDragging && hasMoved && previewWindow ) {
  523. if ( previewWindow.parentNode ) {
  524. previewWindow.parentNode.removeChild( previewWindow );
  525. }
  526. const finalX = parseInt( previewWindow.style.left ) + 200;
  527. const finalY = parseInt( previewWindow.style.top ) + 20;
  528. this.detachTab( tab, finalX, finalY );
  529. } else if ( ! hasMoved ) {
  530. this.setActiveTab( tab.id );
  531. if ( previewWindow && previewWindow.parentNode ) {
  532. previewWindow.parentNode.removeChild( previewWindow );
  533. }
  534. } else if ( previewWindow ) {
  535. if ( previewWindow.parentNode ) {
  536. previewWindow.parentNode.removeChild( previewWindow );
  537. }
  538. }
  539. tab.button.style.opacity = '';
  540. tab.button.style.transform = '';
  541. tab.button.style.cursor = '';
  542. isDragging = false;
  543. hasMoved = false;
  544. previewWindow = null;
  545. tab.button.removeEventListener( 'pointermove', onDragMove );
  546. tab.button.removeEventListener( 'pointerup', onDragEnd );
  547. tab.button.removeEventListener( 'pointercancel', onDragEnd );
  548. };
  549. tab.button.addEventListener( 'pointerdown', ( e ) => {
  550. if ( this.isMobile && e.pointerType !== 'mouse' ) return;
  551. onDragStart( e );
  552. tab.button.addEventListener( 'pointermove', onDragMove );
  553. tab.button.addEventListener( 'pointerup', onDragEnd );
  554. tab.button.addEventListener( 'pointercancel', onDragEnd );
  555. } );
  556. // Set cursor to grab for tabs that can be detached
  557. tab.button.style.cursor = 'grab';
  558. }
  559. createPreviewWindow( tab, x, y ) {
  560. const windowPanel = document.createElement( 'div' );
  561. windowPanel.className = 'detached-tab-panel';
  562. windowPanel.style.left = `${ x - 200 }px`;
  563. windowPanel.style.top = `${ y - 20 }px`;
  564. windowPanel.style.pointerEvents = 'none'; // Preview only
  565. // Set z-index for preview window to be on top
  566. this.maxZIndex ++;
  567. windowPanel.style.setProperty( 'z-index', this.maxZIndex, 'important' );
  568. const windowHeader = document.createElement( 'div' );
  569. windowHeader.className = 'detached-tab-header';
  570. const title = document.createElement( 'span' );
  571. title.textContent = tab.button.textContent.replace( '⇱', '' ).trim();
  572. windowHeader.appendChild( title );
  573. const headerControls = document.createElement( 'div' );
  574. headerControls.className = 'detached-header-controls';
  575. const reattachBtn = document.createElement( 'button' );
  576. reattachBtn.className = 'detached-reattach-btn';
  577. reattachBtn.innerHTML = '↩';
  578. headerControls.appendChild( reattachBtn );
  579. windowHeader.appendChild( headerControls );
  580. const windowContent = document.createElement( 'div' );
  581. windowContent.className = 'detached-tab-content';
  582. const resizer = document.createElement( 'div' );
  583. resizer.className = 'detached-tab-resizer';
  584. windowPanel.appendChild( resizer );
  585. windowPanel.appendChild( windowHeader );
  586. windowPanel.appendChild( windowContent );
  587. document.body.appendChild( windowPanel );
  588. return windowPanel;
  589. }
  590. detachTab( tab, x, y ) {
  591. if ( tab.isDetached ) return;
  592. // Check if tab allows detachment
  593. if ( tab.allowDetach === false ) return;
  594. const allButtons = Array.from( this.tabsContainer.children );
  595. const tabIdsInOrder = allButtons.map( btn => {
  596. return Object.keys( this.tabs ).find( id => this.tabs[ id ].button === btn );
  597. } ).filter( id => id !== undefined );
  598. const currentIndex = tabIdsInOrder.indexOf( tab.id );
  599. let newActiveTab = null;
  600. if ( this.activeTabId === tab.id ) {
  601. tab.setActive( false );
  602. const remainingTabs = tabIdsInOrder.filter( id =>
  603. id !== tab.id &&
  604. ! this.tabs[ id ].isDetached &&
  605. this.tabs[ id ].isVisible
  606. );
  607. if ( remainingTabs.length > 0 ) {
  608. for ( let i = currentIndex - 1; i >= 0; i -- ) {
  609. if ( remainingTabs.includes( tabIdsInOrder[ i ] ) ) {
  610. newActiveTab = tabIdsInOrder[ i ];
  611. break;
  612. }
  613. }
  614. if ( ! newActiveTab ) {
  615. for ( let i = currentIndex + 1; i < tabIdsInOrder.length; i ++ ) {
  616. if ( remainingTabs.includes( tabIdsInOrder[ i ] ) ) {
  617. newActiveTab = tabIdsInOrder[ i ];
  618. break;
  619. }
  620. }
  621. }
  622. if ( ! newActiveTab ) {
  623. newActiveTab = remainingTabs[ 0 ];
  624. }
  625. }
  626. }
  627. if ( tab.button.parentNode ) {
  628. tab.button.parentNode.removeChild( tab.button );
  629. }
  630. if ( tab.content.parentNode ) {
  631. tab.content.parentNode.removeChild( tab.content );
  632. }
  633. const detachedWindow = this.createDetachedWindow( tab, x, y );
  634. this.detachedWindows.push( detachedWindow );
  635. tab.isDetached = true;
  636. tab.detachedWindow = detachedWindow;
  637. if ( newActiveTab ) {
  638. this.setActiveTab( newActiveTab );
  639. } else if ( this.activeTabId === tab.id ) {
  640. this.activeTabId = null;
  641. }
  642. // Update panel size after detaching
  643. this.updatePanelSize();
  644. this.saveLayout();
  645. }
  646. createDetachedWindow( tab, x, y ) {
  647. // Constrain initial position to window bounds
  648. const windowWidth = window.innerWidth;
  649. const windowHeight = window.innerHeight;
  650. const estimatedWidth = 400; // Default detached window width
  651. const estimatedHeight = 300; // Default detached window height
  652. let constrainedX = x - 200;
  653. let constrainedY = y - 20;
  654. if ( constrainedX + estimatedWidth > windowWidth ) {
  655. constrainedX = windowWidth - estimatedWidth;
  656. }
  657. if ( constrainedX < 0 ) {
  658. constrainedX = 0;
  659. }
  660. if ( constrainedY + estimatedHeight > windowHeight ) {
  661. constrainedY = windowHeight - estimatedHeight;
  662. }
  663. if ( constrainedY < 0 ) {
  664. constrainedY = 0;
  665. }
  666. const windowPanel = document.createElement( 'div' );
  667. windowPanel.className = 'detached-tab-panel';
  668. windowPanel.style.left = `${ constrainedX }px`;
  669. windowPanel.style.top = `${ constrainedY }px`;
  670. if ( ! this.panel.classList.contains( 'visible' ) ) {
  671. windowPanel.style.opacity = '0';
  672. windowPanel.style.visibility = 'hidden';
  673. windowPanel.style.pointerEvents = 'none';
  674. }
  675. // Hide detached window if tab is not visible
  676. if ( ! tab.isVisible ) {
  677. windowPanel.style.display = 'none';
  678. }
  679. const windowHeader = document.createElement( 'div' );
  680. windowHeader.className = 'detached-tab-header';
  681. const title = document.createElement( 'span' );
  682. title.textContent = tab.button.textContent.replace( '⇱', '' ).trim();
  683. windowHeader.appendChild( title );
  684. const headerControls = document.createElement( 'div' );
  685. headerControls.className = 'detached-header-controls';
  686. const reattachBtn = document.createElement( 'button' );
  687. reattachBtn.className = 'detached-reattach-btn';
  688. reattachBtn.innerHTML = '↩';
  689. reattachBtn.title = 'Reattach to main panel';
  690. reattachBtn.onclick = () => this.reattachTab( tab );
  691. headerControls.appendChild( reattachBtn );
  692. windowHeader.appendChild( headerControls );
  693. const windowContent = document.createElement( 'div' );
  694. windowContent.className = 'detached-tab-content';
  695. windowContent.appendChild( tab.content );
  696. // Make sure content is visible
  697. tab.content.style.display = 'block';
  698. tab.content.classList.add( 'active' );
  699. // Create resize handles for all edges
  700. const resizerTop = document.createElement( 'div' );
  701. resizerTop.className = 'detached-tab-resizer-top';
  702. const resizerRight = document.createElement( 'div' );
  703. resizerRight.className = 'detached-tab-resizer-right';
  704. const resizerBottom = document.createElement( 'div' );
  705. resizerBottom.className = 'detached-tab-resizer-bottom';
  706. const resizerLeft = document.createElement( 'div' );
  707. resizerLeft.className = 'detached-tab-resizer-left';
  708. const resizerCorner = document.createElement( 'div' );
  709. resizerCorner.className = 'detached-tab-resizer';
  710. windowPanel.appendChild( resizerTop );
  711. windowPanel.appendChild( resizerRight );
  712. windowPanel.appendChild( resizerBottom );
  713. windowPanel.appendChild( resizerLeft );
  714. windowPanel.appendChild( resizerCorner );
  715. windowPanel.appendChild( windowHeader );
  716. windowPanel.appendChild( windowContent );
  717. document.body.appendChild( windowPanel );
  718. // Setup window dragging
  719. this.setupDetachedWindowDrag( windowPanel, windowHeader, tab );
  720. // Setup window resizing
  721. this.setupDetachedWindowResize( windowPanel, resizerTop, resizerRight, resizerBottom, resizerLeft, resizerCorner );
  722. // Use the same z-index that was set on the preview window
  723. windowPanel.style.setProperty( 'z-index', this.maxZIndex, 'important' );
  724. return { panel: windowPanel, tab: tab };
  725. }
  726. bringWindowToFront( windowPanel ) {
  727. // Increment the max z-index and apply it to the clicked window
  728. this.maxZIndex ++;
  729. windowPanel.style.setProperty( 'z-index', this.maxZIndex, 'important' );
  730. }
  731. setupDetachedWindowDrag( windowPanel, header, tab ) {
  732. let isDragging = false;
  733. let startX, startY, startLeft, startTop;
  734. // Bring window to front when clicking anywhere on it
  735. windowPanel.addEventListener( 'pointerdown', () => {
  736. this.bringWindowToFront( windowPanel );
  737. } );
  738. const onDragStart = ( e ) => {
  739. if ( e.target.classList.contains( 'detached-reattach-btn' ) ) {
  740. return;
  741. }
  742. // Bring window to front when starting to drag
  743. this.bringWindowToFront( windowPanel );
  744. isDragging = true;
  745. header.style.cursor = 'grabbing';
  746. header.setPointerCapture( e.pointerId );
  747. startX = e.clientX;
  748. startY = e.clientY;
  749. const rect = windowPanel.getBoundingClientRect();
  750. startLeft = rect.left;
  751. startTop = rect.top;
  752. };
  753. const onDragMove = ( e ) => {
  754. if ( ! isDragging ) return;
  755. e.preventDefault();
  756. const currentX = e.clientX;
  757. const currentY = e.clientY;
  758. const deltaX = currentX - startX;
  759. const deltaY = currentY - startY;
  760. let newLeft = startLeft + deltaX;
  761. let newTop = startTop + deltaY;
  762. // Constrain to window bounds (allow half width/height to extend outside)
  763. const windowWidth = window.innerWidth;
  764. const windowHeight = window.innerHeight;
  765. const panelWidth = windowPanel.offsetWidth;
  766. const panelHeight = windowPanel.offsetHeight;
  767. const halfWidth = panelWidth / 2;
  768. const halfHeight = panelHeight / 2;
  769. // Allow window to extend half its width beyond right edge
  770. if ( newLeft + panelWidth > windowWidth + halfWidth ) {
  771. newLeft = windowWidth + halfWidth - panelWidth;
  772. }
  773. // Allow window to extend half its width beyond left edge
  774. if ( newLeft < - halfWidth ) {
  775. newLeft = - halfWidth;
  776. }
  777. // Allow window to extend half its height beyond bottom edge
  778. if ( newTop + panelHeight > windowHeight + halfHeight ) {
  779. newTop = windowHeight + halfHeight - panelHeight;
  780. }
  781. // Allow window to extend half its height beyond top edge
  782. if ( newTop < - halfHeight ) {
  783. newTop = - halfHeight;
  784. }
  785. windowPanel.style.left = `${ newLeft }px`;
  786. windowPanel.style.top = `${ newTop }px`;
  787. // Check if cursor is over the inspector panel
  788. const panelRect = this.panel.getBoundingClientRect();
  789. const isOverPanel = currentX >= panelRect.left && currentX <= panelRect.right &&
  790. currentY >= panelRect.top && currentY <= panelRect.bottom;
  791. if ( isOverPanel ) {
  792. windowPanel.style.opacity = '0.5';
  793. this.panel.style.outline = '2px solid var(--accent-color)';
  794. } else {
  795. windowPanel.style.opacity = '';
  796. this.panel.style.outline = '';
  797. }
  798. };
  799. const onDragEnd = ( e ) => {
  800. if ( ! isDragging ) return;
  801. isDragging = false;
  802. header.style.cursor = '';
  803. windowPanel.style.opacity = '';
  804. this.panel.style.outline = '';
  805. // Check if dropped over the inspector panel
  806. const currentX = e.clientX;
  807. const currentY = e.clientY;
  808. if ( currentX !== undefined && currentY !== undefined ) {
  809. const panelRect = this.panel.getBoundingClientRect();
  810. const isOverPanel = currentX >= panelRect.left && currentX <= panelRect.right &&
  811. currentY >= panelRect.top && currentY <= panelRect.bottom;
  812. if ( isOverPanel && tab ) {
  813. // Reattach the tab
  814. this.reattachTab( tab );
  815. } else {
  816. // Save layout after moving detached window
  817. this.saveLayout();
  818. }
  819. }
  820. header.removeEventListener( 'pointermove', onDragMove );
  821. header.removeEventListener( 'pointerup', onDragEnd );
  822. header.removeEventListener( 'pointercancel', onDragEnd );
  823. };
  824. header.addEventListener( 'pointerdown', ( e ) => {
  825. onDragStart( e );
  826. header.addEventListener( 'pointermove', onDragMove );
  827. header.addEventListener( 'pointerup', onDragEnd );
  828. header.addEventListener( 'pointercancel', onDragEnd );
  829. } );
  830. header.style.cursor = 'grab';
  831. }
  832. setupDetachedWindowResize( windowPanel, resizerTop, resizerRight, resizerBottom, resizerLeft, resizerCorner ) {
  833. const minWidth = 250;
  834. const minHeight = 150;
  835. const setupResizer = ( resizer, direction ) => {
  836. let isResizing = false;
  837. let startX, startY, startWidth, startHeight, startLeft, startTop;
  838. const onResizeStart = ( e ) => {
  839. e.preventDefault();
  840. e.stopPropagation();
  841. isResizing = true;
  842. // Bring window to front when resizing
  843. this.bringWindowToFront( windowPanel );
  844. resizer.setPointerCapture( e.pointerId );
  845. startX = e.clientX;
  846. startY = e.clientY;
  847. startWidth = windowPanel.offsetWidth;
  848. startHeight = windowPanel.offsetHeight;
  849. startLeft = windowPanel.offsetLeft;
  850. startTop = windowPanel.offsetTop;
  851. };
  852. const onResizeMove = ( e ) => {
  853. if ( ! isResizing ) return;
  854. e.preventDefault();
  855. const currentX = e.clientX;
  856. const currentY = e.clientY;
  857. const deltaX = currentX - startX;
  858. const deltaY = currentY - startY;
  859. const windowWidth = window.innerWidth;
  860. const windowHeight = window.innerHeight;
  861. if ( direction === 'right' || direction === 'corner' ) {
  862. const newWidth = startWidth + deltaX;
  863. const maxWidth = windowWidth - startLeft;
  864. if ( newWidth >= minWidth && newWidth <= maxWidth ) {
  865. windowPanel.style.width = `${ newWidth }px`;
  866. }
  867. }
  868. if ( direction === 'bottom' || direction === 'corner' ) {
  869. const newHeight = startHeight + deltaY;
  870. const maxHeight = windowHeight - startTop;
  871. if ( newHeight >= minHeight && newHeight <= maxHeight ) {
  872. windowPanel.style.height = `${ newHeight }px`;
  873. }
  874. }
  875. if ( direction === 'left' ) {
  876. const newWidth = startWidth - deltaX;
  877. const maxLeft = startLeft + startWidth - minWidth;
  878. if ( newWidth >= minWidth ) {
  879. const newLeft = startLeft + deltaX;
  880. if ( newLeft >= 0 && newLeft <= maxLeft ) {
  881. windowPanel.style.width = `${ newWidth }px`;
  882. windowPanel.style.left = `${ newLeft }px`;
  883. }
  884. }
  885. }
  886. if ( direction === 'top' ) {
  887. const newHeight = startHeight - deltaY;
  888. const maxTop = startTop + startHeight - minHeight;
  889. if ( newHeight >= minHeight ) {
  890. const newTop = startTop + deltaY;
  891. if ( newTop >= 0 && newTop <= maxTop ) {
  892. windowPanel.style.height = `${ newHeight }px`;
  893. windowPanel.style.top = `${ newTop }px`;
  894. }
  895. }
  896. }
  897. };
  898. const onResizeEnd = () => {
  899. isResizing = false;
  900. resizer.removeEventListener( 'pointermove', onResizeMove );
  901. resizer.removeEventListener( 'pointerup', onResizeEnd );
  902. resizer.removeEventListener( 'pointercancel', onResizeEnd );
  903. // Save layout after resizing detached window
  904. this.saveLayout();
  905. };
  906. resizer.addEventListener( 'pointerdown', ( e ) => {
  907. onResizeStart( e );
  908. resizer.addEventListener( 'pointermove', onResizeMove );
  909. resizer.addEventListener( 'pointerup', onResizeEnd );
  910. resizer.addEventListener( 'pointercancel', onResizeEnd );
  911. } );
  912. };
  913. // Setup all resizers
  914. setupResizer( resizerTop, 'top' );
  915. setupResizer( resizerRight, 'right' );
  916. setupResizer( resizerBottom, 'bottom' );
  917. setupResizer( resizerLeft, 'left' );
  918. setupResizer( resizerCorner, 'corner' );
  919. }
  920. reattachTab( tab ) {
  921. if ( ! tab.isDetached ) return;
  922. if ( tab.detachedWindow ) {
  923. const index = this.detachedWindows.indexOf( tab.detachedWindow );
  924. if ( index > - 1 ) {
  925. this.detachedWindows.splice( index, 1 );
  926. }
  927. if ( tab.detachedWindow.panel.parentNode ) {
  928. tab.detachedWindow.panel.parentNode.removeChild( tab.detachedWindow.panel );
  929. }
  930. tab.detachedWindow = null;
  931. }
  932. tab.isDetached = false;
  933. // Get all tabs and sort by their original index to determine the correct order
  934. const allTabs = Object.values( this.tabs );
  935. const allTabsSorted = allTabs
  936. .filter( t => t.originalIndex !== undefined && t.isVisible )
  937. .sort( ( a, b ) => a.originalIndex - b.originalIndex );
  938. // Get currently attached tab buttons
  939. const currentButtons = Array.from( this.tabsContainer.children );
  940. // Find the correct position for this tab
  941. let insertIndex = 0;
  942. for ( const t of allTabsSorted ) {
  943. if ( t.id === tab.id ) {
  944. break;
  945. }
  946. // Count only non-detached tabs that come before this one
  947. if ( ! t.isDetached ) {
  948. insertIndex ++;
  949. }
  950. }
  951. // Insert the button at the correct position
  952. if ( insertIndex >= currentButtons.length || currentButtons.length === 0 ) {
  953. // If insert index is beyond current buttons, or no buttons exist, append at the end
  954. this.tabsContainer.appendChild( tab.button );
  955. } else {
  956. // Insert before the button at the insert index
  957. this.tabsContainer.insertBefore( tab.button, currentButtons[ insertIndex ] );
  958. }
  959. this.contentWrapper.appendChild( tab.content );
  960. this.setActiveTab( tab.id );
  961. // Update panel size after reattaching
  962. this.updatePanelSize();
  963. this.saveLayout();
  964. }
  965. setActiveTab( id ) {
  966. if ( this.activeTabId && this.tabs[ this.activeTabId ] && ! this.tabs[ this.activeTabId ].isDetached ) {
  967. this.tabs[ this.activeTabId ].setActive( false );
  968. }
  969. this.activeTabId = id;
  970. if ( this.tabs[ id ] ) {
  971. this.tabs[ id ].setActive( true );
  972. }
  973. this.saveLayout();
  974. }
  975. togglePanel() {
  976. this.panel.classList.toggle( 'visible' );
  977. this.toggleButton.classList.toggle( 'panel-open' );
  978. this.miniPanel.classList.toggle( 'panel-open' );
  979. const isVisible = this.panel.classList.contains( 'visible' );
  980. this.detachedWindows.forEach( detachedWindow => {
  981. if ( isVisible ) {
  982. detachedWindow.panel.style.opacity = '';
  983. detachedWindow.panel.style.visibility = '';
  984. detachedWindow.panel.style.pointerEvents = '';
  985. } else {
  986. detachedWindow.panel.style.opacity = '0';
  987. detachedWindow.panel.style.visibility = 'hidden';
  988. detachedWindow.panel.style.pointerEvents = 'none';
  989. }
  990. } );
  991. this.dispatchEvent( { type: 'resize' } );
  992. this.saveLayout();
  993. }
  994. togglePosition() {
  995. const newPosition = this.position === 'bottom' ? 'right' : 'bottom';
  996. this.setPosition( newPosition );
  997. }
  998. setPosition( targetPosition ) {
  999. if ( this.position === targetPosition ) return;
  1000. this.panel.style.transition = 'none';
  1001. // Check if panel is currently maximized
  1002. const isMaximized = this.panel.classList.contains( 'maximized' );
  1003. if ( targetPosition === 'right' ) {
  1004. this.position = 'right';
  1005. this.floatingBtn.classList.add( 'active' );
  1006. 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>';
  1007. this.floatingBtn.title = 'Switch to Bottom';
  1008. // Apply right position styles
  1009. this.panel.classList.remove( 'position-bottom' );
  1010. this.panel.classList.add( 'position-right' );
  1011. this.toggleButton.classList.add( 'position-right' );
  1012. this.miniPanel.classList.add( 'position-right' );
  1013. this.panel.style.bottom = '';
  1014. this.panel.style.top = '0';
  1015. this.panel.style.right = '0';
  1016. this.panel.style.left = '';
  1017. // Apply size based on maximized state
  1018. if ( isMaximized ) {
  1019. this.panel.style.width = '100vw';
  1020. this.panel.style.height = '100%';
  1021. } else {
  1022. this.panel.style.width = `${ this.lastWidthRight }px`;
  1023. this.panel.style.height = '100%';
  1024. }
  1025. } else {
  1026. this.position = 'bottom';
  1027. this.floatingBtn.classList.remove( 'active' );
  1028. 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>';
  1029. this.floatingBtn.title = 'Switch to Right Side';
  1030. // Apply bottom position styles
  1031. this.panel.classList.remove( 'position-right' );
  1032. this.panel.classList.add( 'position-bottom' );
  1033. this.toggleButton.classList.remove( 'position-right' );
  1034. this.miniPanel.classList.remove( 'position-right' );
  1035. this.panel.style.top = '';
  1036. this.panel.style.right = '';
  1037. this.panel.style.bottom = '0';
  1038. this.panel.style.left = '0';
  1039. // Apply size based on maximized state
  1040. if ( isMaximized ) {
  1041. this.panel.style.width = '100%';
  1042. this.panel.style.height = '100vh';
  1043. } else {
  1044. this.panel.style.width = '100%';
  1045. this.panel.style.height = `${ this.lastHeightBottom }px`;
  1046. }
  1047. }
  1048. // Re-enable transition after a brief delay
  1049. setTimeout( () => {
  1050. this.panel.style.transition = '';
  1051. }, 50 );
  1052. // Update panel size based on visible tabs
  1053. this.updatePanelSize();
  1054. // Save layout after position change
  1055. this.saveLayout();
  1056. }
  1057. saveLayout() {
  1058. if ( this.isLoadingLayout ) return;
  1059. const layout = {
  1060. position: this.position,
  1061. lastHeightBottom: this.lastHeightBottom,
  1062. lastWidthRight: this.lastWidthRight,
  1063. activeTabId: this.activeTabId,
  1064. detachedTabs: [],
  1065. isVisible: this.panel.classList.contains( 'visible' )
  1066. };
  1067. // Save detached windows state
  1068. this.detachedWindows.forEach( detachedWindow => {
  1069. const tab = detachedWindow.tab;
  1070. const panel = detachedWindow.panel;
  1071. // Get position values, ensuring they're valid numbers
  1072. const left = parseFloat( panel.style.left ) || panel.offsetLeft || 0;
  1073. const top = parseFloat( panel.style.top ) || panel.offsetTop || 0;
  1074. const width = panel.offsetWidth;
  1075. const height = panel.offsetHeight;
  1076. layout.detachedTabs.push( {
  1077. tabId: tab.id,
  1078. originalIndex: tab.originalIndex !== undefined ? tab.originalIndex : 0,
  1079. left: left,
  1080. top: top,
  1081. width: width,
  1082. height: height
  1083. } );
  1084. } );
  1085. try {
  1086. setItem( 'layout', layout );
  1087. } catch ( e ) {
  1088. console.warn( 'Failed to save profiler layout:', e );
  1089. }
  1090. }
  1091. loadLayout() {
  1092. this.isLoadingLayout = true;
  1093. try {
  1094. const layout = getItem( 'layout' );
  1095. if ( Object.keys( layout ).length === 0 ) return;
  1096. // Constrain detached tabs positions to current screen bounds
  1097. if ( layout.detachedTabs && layout.detachedTabs.length > 0 ) {
  1098. const windowWidth = window.innerWidth;
  1099. const windowHeight = window.innerHeight;
  1100. layout.detachedTabs = layout.detachedTabs.map( detachedTabData => {
  1101. let { left, top, width, height } = detachedTabData;
  1102. // Ensure width and height are within bounds
  1103. if ( width > windowWidth ) {
  1104. width = windowWidth - 100; // Leave some margin
  1105. }
  1106. if ( height > windowHeight ) {
  1107. height = windowHeight - 100; // Leave some margin
  1108. }
  1109. // Allow window to extend half its width/height outside the screen
  1110. const halfWidth = width / 2;
  1111. const halfHeight = height / 2;
  1112. // Constrain horizontal position (allow half width to extend beyond right edge)
  1113. if ( left + width > windowWidth + halfWidth ) {
  1114. left = windowWidth + halfWidth - width;
  1115. }
  1116. // Constrain horizontal position (allow half width to extend beyond left edge)
  1117. if ( left < - halfWidth ) {
  1118. left = - halfWidth;
  1119. }
  1120. // Constrain vertical position (allow half height to extend beyond bottom edge)
  1121. if ( top + height > windowHeight + halfHeight ) {
  1122. top = windowHeight + halfHeight - height;
  1123. }
  1124. // Constrain vertical position (allow half height to extend beyond top edge)
  1125. if ( top < - halfHeight ) {
  1126. top = - halfHeight;
  1127. }
  1128. return {
  1129. ...detachedTabData,
  1130. left,
  1131. top,
  1132. width,
  1133. height
  1134. };
  1135. } );
  1136. }
  1137. // Restore position and dimensions
  1138. if ( layout.position ) {
  1139. this.position = layout.position;
  1140. }
  1141. if ( layout.lastHeightBottom ) {
  1142. this.lastHeightBottom = layout.lastHeightBottom;
  1143. }
  1144. if ( layout.lastWidthRight ) {
  1145. this.lastWidthRight = layout.lastWidthRight;
  1146. }
  1147. // Constrain saved dimensions to current screen bounds
  1148. const windowWidth = window.innerWidth;
  1149. const windowHeight = window.innerHeight;
  1150. if ( this.lastHeightBottom > windowHeight - 50 ) {
  1151. this.lastHeightBottom = windowHeight - 50;
  1152. }
  1153. if ( this.lastWidthRight > windowWidth - 50 ) {
  1154. this.lastWidthRight = windowWidth - 50;
  1155. }
  1156. // Apply the saved position after shell is set up
  1157. if ( this.position === 'right' ) {
  1158. this.floatingBtn.classList.add( 'active' );
  1159. 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>';
  1160. this.floatingBtn.title = 'Switch to Bottom';
  1161. this.panel.classList.remove( 'position-bottom' );
  1162. this.panel.classList.add( 'position-right' );
  1163. this.toggleButton.classList.add( 'position-right' );
  1164. this.miniPanel.classList.add( 'position-right' );
  1165. this.panel.style.bottom = '';
  1166. this.panel.style.top = '0';
  1167. this.panel.style.right = '0';
  1168. this.panel.style.left = '';
  1169. this.panel.style.width = `${ this.lastWidthRight }px`;
  1170. this.panel.style.height = '100%';
  1171. } else {
  1172. this.panel.style.height = `${ this.lastHeightBottom }px`;
  1173. }
  1174. if ( layout.isVisible ) {
  1175. this.panel.classList.add( 'visible' );
  1176. this.toggleButton.classList.add( 'panel-open' );
  1177. }
  1178. if ( layout.activeTabId ) {
  1179. this.setActiveTab( layout.activeTabId );
  1180. }
  1181. if ( layout.detachedTabs && layout.detachedTabs.length > 0 ) {
  1182. this.pendingDetachedTabs = layout.detachedTabs;
  1183. this.restoreDetachedTabs();
  1184. }
  1185. // Update panel size after loading layout
  1186. this.updatePanelSize();
  1187. // Ensure initial open state applies to mini panel as well
  1188. if ( this.panel.classList.contains( 'visible' ) ) {
  1189. this.miniPanel.classList.add( 'panel-open' );
  1190. }
  1191. } catch ( e ) {
  1192. console.warn( 'Failed to load profiler layout:', e );
  1193. } finally {
  1194. this.isLoadingLayout = false;
  1195. }
  1196. }
  1197. restoreDetachedTabs() {
  1198. if ( ! this.pendingDetachedTabs || this.pendingDetachedTabs.length === 0 ) return;
  1199. this.pendingDetachedTabs.forEach( detachedTabData => {
  1200. const tab = this.tabs[ detachedTabData.tabId ];
  1201. if ( ! tab || tab.isDetached ) return;
  1202. // Restore originalIndex if saved
  1203. if ( detachedTabData.originalIndex !== undefined ) {
  1204. tab.originalIndex = detachedTabData.originalIndex;
  1205. }
  1206. if ( tab.button.parentNode ) {
  1207. tab.button.parentNode.removeChild( tab.button );
  1208. }
  1209. if ( tab.content.parentNode ) {
  1210. tab.content.parentNode.removeChild( tab.content );
  1211. }
  1212. const detachedWindow = this.createDetachedWindow( tab, 0, 0 );
  1213. detachedWindow.panel.style.left = `${ detachedTabData.left }px`;
  1214. detachedWindow.panel.style.top = `${ detachedTabData.top }px`;
  1215. detachedWindow.panel.style.width = `${ detachedTabData.width }px`;
  1216. detachedWindow.panel.style.height = `${ detachedTabData.height }px`;
  1217. // Constrain window to bounds after restoring position and size
  1218. this.constrainWindowToBounds( detachedWindow.panel );
  1219. this.detachedWindows.push( detachedWindow );
  1220. tab.isDetached = true;
  1221. tab.detachedWindow = detachedWindow;
  1222. } );
  1223. this.pendingDetachedTabs = null;
  1224. // Update maxZIndex to be higher than all existing windows
  1225. this.detachedWindows.forEach( detachedWindow => {
  1226. const currentZIndex = parseInt( getComputedStyle( detachedWindow.panel ).zIndex ) || 0;
  1227. if ( currentZIndex > this.maxZIndex ) {
  1228. this.maxZIndex = currentZIndex;
  1229. }
  1230. } );
  1231. const needsNewActiveTab = ! this.activeTabId ||
  1232. ! this.tabs[ this.activeTabId ] ||
  1233. this.tabs[ this.activeTabId ].isDetached ||
  1234. ! this.tabs[ this.activeTabId ].isVisible;
  1235. if ( needsNewActiveTab ) {
  1236. const tabIds = Object.keys( this.tabs );
  1237. const availableTabs = tabIds.filter( id =>
  1238. ! this.tabs[ id ].isDetached &&
  1239. this.tabs[ id ].isVisible
  1240. );
  1241. if ( availableTabs.length > 0 ) {
  1242. const buttons = Array.from( this.tabsContainer.children );
  1243. const orderedTabIds = buttons.map( btn => {
  1244. return Object.keys( this.tabs ).find( id => this.tabs[ id ].button === btn );
  1245. } ).filter( id =>
  1246. id !== undefined &&
  1247. ! this.tabs[ id ].isDetached &&
  1248. this.tabs[ id ].isVisible
  1249. );
  1250. this.setActiveTab( orderedTabIds[ 0 ] || availableTabs[ 0 ] );
  1251. } else {
  1252. this.activeTabId = null;
  1253. }
  1254. }
  1255. // Update panel size after restoring detached tabs
  1256. this.updatePanelSize();
  1257. }
  1258. }
粤ICP备19079148号