Profiler.js 51 KB

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