Profiler.js 48 KB

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