Profiler.js 51 KB

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