Profiler.js 50 KB

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