@@ -27,6 +27,9 @@ let iconDragStartY = 0;
2727let iconDragThresholdMet = false ;
2828let hasMove = false ;
2929let selectedIcon : HTMLElement | null = null ;
30+ let activeEditInput : HTMLInputElement | null = null ;
31+ let activeEditLabel : HTMLElement | null = null ;
32+ let activeEditOriginalName : string = '' ;
3033const DRAG_THRESHOLD = 5 ; // pixels to move before drag starts
3134
3235// Selection rectangle
@@ -55,6 +58,9 @@ function initializeIcons() {
5558 const desktopIcons = document . querySelector ( '.desktop-icons' ) ;
5659 desktopIcons ?. addEventListener ( 'click' , ( e ) => {
5760 if ( ( e . target as HTMLElement ) . classList . contains ( 'desktop-icons' ) ) {
61+ // Exit edit mode if active
62+ exitEditMode ( ) ;
63+
5864 if ( selectedIcon ) {
5965 selectedIcon . classList . remove ( 'selected' ) ;
6066 selectedIcon = null ;
@@ -192,16 +198,27 @@ function initializeIcons() {
192198 // Immediately show selection on first click
193199 const wasAlreadySelected = selectedIcon === icon && icon . classList . contains ( 'selected' ) ;
194200
201+ // Exit edit mode if clicking on a different icon
195202 if ( selectedIcon && selectedIcon !== icon ) {
203+ exitEditMode ( ) ;
196204 selectedIcon . classList . remove ( 'selected' ) ;
197205 }
206+
198207 icon . classList . add ( 'selected' ) ;
199208 selectedIcon = icon as HTMLElement ;
200209
201- // Set timeout to check for label editing
202- clickTimeout = window . setTimeout ( ( ) => {
203- clickCount = 0 ;
204- } , 250 ) ;
210+ // If clicking on an already selected icon, enter edit mode after short delay
211+ if ( wasAlreadySelected ) {
212+ clickTimeout = window . setTimeout ( ( ) => {
213+ enterEditMode ( icon as HTMLElement ) ;
214+ clickCount = 0 ;
215+ } , 500 ) ;
216+ } else {
217+ // Set timeout to reset click count
218+ clickTimeout = window . setTimeout ( ( ) => {
219+ clickCount = 0 ;
220+ } , 250 ) ;
221+ }
205222 } ) ;
206223
207224 icon . addEventListener ( 'dblclick' , ( e ) => {
@@ -266,22 +283,26 @@ function initializeIcons() {
266283}
267284
268285// Call initialization when DOM is ready
286+ // window management
287+ let activewindow = null ;
288+ let windowZIndex = 100 ;
289+
269290if ( document . readyState === 'loading' ) {
270291 document . addEventListener ( 'DOMContentLoaded' , ( ) => {
271292 initializeIcons ( ) ;
293+ // Set the welcome window as active initially
294+ activewindow = document . getElementById ( 'window-welcome' ) ;
272295 // Update taskbar to show welcome window
273296 updateTaskbar ( ) ;
274297 } ) ;
275298} else {
276299 initializeIcons ( ) ;
300+ // Set the welcome window as active initially
301+ activewindow = document . getElementById ( 'window-welcome' ) ;
277302 // Update taskbar to show welcome window
278303 updateTaskbar ( ) ;
279304}
280305
281- // window management
282- let activewindow = null ;
283- let windowZIndex = 100 ;
284-
285306function openWindow ( windowId , fromTaskbarButton = null ) {
286307 console . log ( 'Opening window:' , windowId ) ;
287308 const windowEl = document . getElementById ( `window-${ windowId } ` ) ;
@@ -597,13 +618,14 @@ function updateTaskbar() {
597618}
598619
599620// Keyboard shortcuts for window management
600- document . addEventListener ( 'keydown' , ( e ) => {
601- // Cmd+W (Mac) or Ctrl+W (Windows/Linux) to close active window
602- if ( ( e . metaKey || e . ctrlKey ) && e . key === 'w ' ) {
603- e . preventDefault ( ) ; // Prevent browser from closing the tab
621+ document . addEventListener ( 'keydown' , ( e : KeyboardEvent ) => {
622+ // Escape key to close active window
623+ if ( e . key === 'Escape' || e . key === 'Esc ' ) {
624+ const openWindow = document . querySelector ( '.window.active:not(.minimized)' ) ;
604625
605- if ( activewindow ) {
606- const windowId = activewindow . id . replace ( 'window-' , '' ) ;
626+ if ( openWindow ) {
627+ e . preventDefault ( ) ;
628+ const windowId = openWindow . id . replace ( 'window-' , '' ) ;
607629 closeWindow ( windowId ) ;
608630 }
609631 }
@@ -1074,6 +1096,76 @@ function openIconFromMenu() {
10741096 }
10751097}
10761098
1099+ function exitEditMode ( save : boolean = true ) {
1100+ if ( ! activeEditInput || ! activeEditLabel ) return ;
1101+
1102+ if ( save ) {
1103+ const newName = activeEditInput . value . trim ( ) ;
1104+ if ( newName ) {
1105+ activeEditLabel . textContent = newName ;
1106+ } else {
1107+ activeEditLabel . textContent = activeEditOriginalName ;
1108+ }
1109+ } else {
1110+ activeEditLabel . textContent = activeEditOriginalName ;
1111+ }
1112+
1113+ activeEditInput = null ;
1114+ activeEditLabel = null ;
1115+ activeEditOriginalName = '' ;
1116+ }
1117+
1118+ function enterEditMode ( icon : HTMLElement ) {
1119+ // Exit any existing edit mode first
1120+ exitEditMode ( ) ;
1121+
1122+ const labelElement = icon . querySelector ( '.desktop-icon-label' ) as HTMLElement ;
1123+ if ( ! labelElement ) return ;
1124+
1125+ const currentName = labelElement . textContent || '' ;
1126+
1127+ // Store edit state
1128+ activeEditLabel = labelElement ;
1129+ activeEditOriginalName = currentName ;
1130+
1131+ // Create an input element
1132+ const input = document . createElement ( 'input' ) ;
1133+ input . type = 'text' ;
1134+ input . value = currentName ;
1135+ input . style . cssText = `
1136+ width: 100%;
1137+ background: white;
1138+ border: 1px solid #0066cc;
1139+ padding: 2px 4px;
1140+ font-family: 'Segoe UI', Tahoma, sans-serif;
1141+ font-size: 11px;
1142+ text-align: center;
1143+ outline: none;
1144+ ` ;
1145+
1146+ activeEditInput = input ;
1147+
1148+ // Replace label with input
1149+ labelElement . textContent = '' ;
1150+ labelElement . appendChild ( input ) ;
1151+ input . focus ( ) ;
1152+ input . select ( ) ;
1153+
1154+ // Handle finish editing
1155+ const finishEdit = ( ) => {
1156+ exitEditMode ( true ) ;
1157+ } ;
1158+
1159+ input . addEventListener ( 'blur' , finishEdit ) ;
1160+ input . addEventListener ( 'keydown' , ( e ) => {
1161+ if ( e . key === 'Enter' ) {
1162+ exitEditMode ( true ) ;
1163+ } else if ( e . key === 'Escape' ) {
1164+ exitEditMode ( false ) ;
1165+ }
1166+ } ) ;
1167+ }
1168+
10771169function renameIcon ( ) {
10781170 if ( ! contextMenuIcon ) return ;
10791171
0 commit comments