Profiler.js 47 KB

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