Profiler.js 42 KB

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