Issue thread https://gitlab.gnome.org/GNOME/baobab/-/issues/157#note_2367208
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 of the app
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
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
-
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:
- When a right-click happens,
treeview_right_click_gesture_pressed
is called - It finds and selects the row that was clicked
- 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:
- First, I'll add a property to store the right-clicked item
- Then modify the right-click handler to store this item
- 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:
- Add a new
context_menu_item
property to store the item that was right-clicked - Update the right-click handler to store the clicked item in this property
- 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.