Skip to content

Instantly share code, notes, and snippets.

@cmdcolin
Last active March 2, 2025 20:14
Show Gist options
  • Save cmdcolin/9f5f4d073a464a42e7d2d58e66c8beeb to your computer and use it in GitHub Desktop.
Save cmdcolin/9f5f4d073a464a42e7d2d58e66c8beeb to your computer and use it in GitHub Desktop.
fix for baobab deleting wrong folder

Fixing Disk Usage Analyzer (aka baobab) bug where it was deleting the wrong file

Issue thread https://gitlab.gnome.org/GNOME/baobab/-/issues/157#note_2367208

Background on the bug

The Disk Usage Analyzer app (aka baobab) has a "bug" where if you right-click a file, it brings up a context menu and you can then click "Move to Trash". However, if your mouse moves outside the context menu, then it "highlights" or "selects" a different file with the context menu still open, and then trashes the newly highlighted/selected file instead of the one that you originally right-clicked

image

Image of the app

Language that baobab uses

Baobab is implemented in the vala language (https://en.wikipedia.org/wiki/Vala_(programming_language)) which is a language that is used for a varity of linux GTK desktop apps. It has a nice syntax compared to e.g. GTK code in C or Rust

It is fairly easy to "grok" by reading but writing is tricky as usual

Making AI fix the bug

I found where the code was saying "get_selected_item" where it was trashing the file that was selected instead of the one that was initially right clickd

I selected the code block that trashes the file using the get_selected_item code and said "can you change the code to get the item that was initially clicked rather than the currently selected item"

It found the solution with a single iteration

I disclosed my use of AI to the team via the issue thread. I am not sure if they will accept this as it's a touchy subject. However, the change is so minimal that it's sort of one of these things that aside from changing variable names, hard to see how it could be implemented differently

Full chat log session with avante.nvim


  • Datetime: 2025-03-02 12:57:51

  • Model: claude/claude-3-7-sonnet-20250219

  • Selected files:

    • src/baobab-window.vala
  • Selected code:

            var item = get_selected_item ();
            if (item != null)
                trash_file (item);

can you change the code to get the item that was initially clicked rather than the currently selected item

I'll help you modify the code to get the item that was initially clicked rather than the currently selected item in the on_treeview_trash function. This requires tracking the item that was right-clicked when the context menu was opened.

Let's analyze the current flow:

  1. When a right-click happens, treeview_right_click_gesture_pressed is called
  2. It finds and selects the row that was clicked
  3. Later when the trash action is triggered, it uses get_selected_item() to get the currently selected item

To fix this, we need to store the clicked item when the context menu is opened and use that stored item in the trash action.

Here's how we can modify the code: [read_file]: running tool [read_file]: path: /home/cdiesh/src/baobab/src/baobab-window.vala [read_file]: tool finished

Now I'll implement the changes to track the clicked item when the context menu is opened:

  1. First, I'll add a property to store the right-clicked item
  2. Then modify the right-click handler to store this item
  3. Finally, update the trash action to use this stored item

Here are the changes:

Filepath: src/baobab-window.vala Replace lines: 68-71

        private Location? active_location = null;
        private bool is_busy = false;
        private ulong scan_completed_handler = 0;
        private uint scanning_progress_id = 0;
        private Scanner.Results? context_menu_item = null;

Filepath: src/baobab-window.vala Replace lines: 480-510

        [GtkCallback]
        void treeview_right_click_gesture_pressed (int n_press, double x, double y) {
            var child = columnview.pick (x, y, Gtk.PickFlags.INSENSITIVE | Gtk.PickFlags.NON_TARGETABLE);
            var row = child.get_ancestor (Type.from_name ("GtkColumnViewRowWidget"));

            if (row == null) {
                return;
            }

            context_menu_item = null;
            
            for (var cell = row.get_first_child (); cell != null; cell = cell.get_next_sibling ()) {
                var cell_content = cell.get_first_child ();

                if (cell_content == null ||
                    cell_content.get_type () != typeof (FileCell)) {
                    continue;
                }

                var file_cell = cell_content as FileCell;
                uint position = 0;
                
                // Store the item that was clicked for context menu actions
                context_menu_item = file_cell.item as Scanner.Results;

                if (columnview_selection_find (file_cell.item, true, out position)) {
                    columnview.scroll_to (position,
                                          null,
                                          Gtk.ListScrollFlags.FOCUS | Gtk.ListScrollFlags.SELECT,
                                          null);
                }
                break;
            }

            show_treeview_popover (treeview_popover_menu,  (int) x, (int) y);
        }

Filepath: src/baobab-window.vala Replace lines: 456-460

        void on_treeview_trash () {
            // Use the item that was right-clicked when the context menu was opened
            if (context_menu_item != null)
                trash_file (context_menu_item);
        }

These changes will:

  1. Add a new context_menu_item property to store the item that was right-clicked
  2. Update the right-click handler to store the clicked item in this property
  3. Modify the trash action to use the stored item instead of the currently selected item

This way, when you right-click on an item and select "Move to Trash" from the context menu, it will trash the item you initially clicked on, regardless of what item is currently selected.

Generation complete! Please review the code suggestions above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment