@@ -101,12 +101,12 @@ define(function (require, exports, module) {
101101 // Create the tab element with the structure we need
102102 // tab name is written as a separate div because it may include directory info which we style differently
103103 const $tab = $ (
104- `<div class="tab ${ isActive ? 'active' : '' }
105- ${ isDirty ? 'dirty' : '' } "
104+ `<div class="tab ${ isActive ? 'active' : '' } "
106105 data-path="${ entry . path } "
107106 title="${ entry . path } ">
108107 <div class="tab-icon"></div>
109108 <div class="tab-name"></div>
109+ <div class="tab-close"><i class="fa-solid fa-xmark"></i></div>
110110 </div>` ) ;
111111
112112 // Add the file icon
@@ -211,14 +211,66 @@ define(function (require, exports, module) {
211211
212212
213213 /**
214- * handle click events on the tabs to open the file
214+ * Handle close button click on tabs
215+ * This function will remove the file from the working set
216+ *
217+ * @param {String } filePath - path of the file to close
218+ */
219+ function handleTabClose ( filePath ) {
220+ // Logic: First open the file we want to close, then close it and finally restore focus
221+ // Why? Because FILE_CLOSE removes the currently active file from the working set
222+
223+ // Get the current active editor to restore focus later
224+ const currentActiveEditor = EditorManager . getActiveEditor ( ) ;
225+ const currentActivePath = currentActiveEditor ? currentActiveEditor . document . file . fullPath : null ;
226+
227+ // Only need to open the file first if it's not the currently active one
228+ if ( currentActivePath !== filePath ) {
229+ // open the file we want to close
230+ CommandManager . execute ( Commands . FILE_OPEN , { fullPath : filePath } )
231+ . done ( function ( ) {
232+ // close it
233+ CommandManager . execute ( Commands . FILE_CLOSE )
234+ . done ( function ( ) {
235+ // If we had a different file active before, restore focus to it
236+ if ( currentActivePath && currentActivePath !== filePath ) {
237+ CommandManager . execute ( Commands . FILE_OPEN , { fullPath : currentActivePath } ) ;
238+ }
239+ } )
240+ . fail ( function ( error ) {
241+ console . error ( "Failed to close file:" , filePath , error ) ;
242+ } ) ;
243+ } )
244+ . fail ( function ( error ) {
245+ console . error ( "Failed to open file for closing:" , filePath , error ) ;
246+ } ) ;
247+ } else {
248+ // if it's already the active file, just close it
249+ CommandManager . execute ( Commands . FILE_CLOSE )
250+ . fail ( function ( error ) {
251+ console . error ( "Failed to close file:" , filePath , error ) ;
252+ } ) ;
253+ }
254+ }
255+
256+
257+ /**
258+ * handle click events on the tabs to open the file or close the tab
215259 */
216260 function handleTabClick ( ) {
217261
218262 // delegate event handling for both tab bars
219263 $ ( document ) . on ( "click" , ".tab" , function ( event ) {
220- // Skip if this is a click on a close button or other control
221- if ( $ ( event . target ) . hasClass ( 'tab-close' ) || $ ( event . target ) . closest ( '.tab-close' ) . length ) {
264+ // check if the clicked element is the close button
265+ if ( $ ( event . target ) . hasClass ( 'fa-xmark' ) || $ ( event . target ) . closest ( '.tab-close' ) . length ) {
266+ // Get the file path from the data-path attribute of the parent tab
267+ const filePath = $ ( this ) . attr ( "data-path" ) ;
268+ if ( filePath ) {
269+ handleTabClose ( filePath ) ;
270+ // Prevent default behavior
271+ event . preventDefault ( ) ;
272+ event . stopPropagation ( ) ;
273+ }
222274 return ;
223275 }
224276
@@ -297,3 +349,6 @@ define(function (require, exports, module) {
297349 handleTabClick ( ) ;
298350 } ) ;
299351} ) ;
352+
353+
354+ // TODO: Bug (when we have two panes and one pane gets empty by closing all files in it, the other pane tab bar also gets removed)
0 commit comments