Profiler.js 50 KB

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