Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix select after rename; fix rename folder; disallow rename if unsaved edits #1386

Merged
merged 16 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions src/FolderManager/FileItem.vala
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,31 @@ namespace Scratch.FolderManager {

var rename_item = new Gtk.MenuItem.with_label (_("Rename"));
rename_item.activate.connect (() => {
view.ignore_next_select = true;
view.start_editing_item (this);
// Ensure item selected else rename does not work
view.select_path (file.path);
if (view.start_editing_item (this)) {
ulong once = 0;
once = this.edited.connect ((new_name) => {
this.disconnect (once);
var new_path = Path.get_dirname (file.path) + Path.DIR_SEPARATOR_S + new_name;
view.toplevel_action_group.activate_action (MainWindow.ACTION_CLOSE_TAB, new Variant.string (this.path));
view.select (new_path); // Select and open newly named file
});

// Handle cancelled rename (which does not produce signal)
Timeout.add (200, () => {
if (view.editing) {
return Source.CONTINUE;
} else {
// Avoid selected but unopened item if rename cancelled (they would not open if clicked on)
view.unselect_all ();
return Source.REMOVE;
}
});
}
});

rename_item.sensitive = view.rename_request (file);
var delete_item = new Gtk.MenuItem.with_label (_("Move to Trash"));
delete_item.activate.connect (trash);

Expand Down
8 changes: 7 additions & 1 deletion src/FolderManager/FileView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@
public class Scratch.FolderManager.FileView : Granite.Widgets.SourceList, Code.PaneSwitcher {
private GLib.Settings settings;
private Scratch.Services.GitManager git_manager;
private ActionGroup? toplevel_action_group = null;

public ActionGroup toplevel_action_group { get; private set; }
private Scratch.Services.PluginsManager plugins;

public signal void select (string file);
public signal bool rename_request (File file);

public bool ignore_next_select { get; set; default = false; }
public string icon_name { get; set; }
Expand Down Expand Up @@ -119,6 +121,10 @@ public class Scratch.FolderManager.FileView : Granite.Widgets.SourceList, Code.P
item_selected.connect (on_item_selected);
}

public void unselect_all () {
selected = null;
}

public void collapse_other_projects (string? keep_open_path = null) {
unowned string path;
if (keep_open_path == null) {
Expand Down
49 changes: 36 additions & 13 deletions src/FolderManager/FolderItem.vala
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,23 @@ namespace Scratch.FolderManager {

var rename_menu_item = new Gtk.MenuItem.with_label (_("Rename"));
rename_menu_item.activate.connect (() => {
view.ignore_next_select = true;
view.start_editing_item (this);
selectable = true;
if (view.start_editing_item (this)) {
// Need to poll view as no signal emited when editing cancelled and need to set
// selectable to false anyway.
Timeout.add (200, () => {
if (view.editing) {
return Source.CONTINUE;
} else {
selectable = false;
}

return Source.REMOVE;
});
} else {
debug ("Could not rename %s", file.path);
selectable = false;
}
});

var delete_item = new Gtk.MenuItem.with_label (_("Move to Trash"));
Expand Down Expand Up @@ -380,14 +395,12 @@ namespace Scratch.FolderManager {
add (rename_item);
/* Start editing after finishing signal handler */
GLib.Idle.add (() => {
view.start_editing_item (rename_item);
/* Need to poll view editing as no signal is generated when canceled (Granite bug) */
Timeout.add (200, () => {
if (view.editing) {
return Source.CONTINUE;
} else {
if (view.start_editing_item (rename_item)) {
ulong once = 0;
once = rename_item.edited.connect (() => {
rename_item.disconnect (once);
// A name was accepted so create the corresponding file
var new_name = rename_item.name;
view.ignore_next_select = true;
try {
var gfile = file.file.get_child_for_display_name (new_name);
if (is_folder) {
Expand All @@ -398,13 +411,23 @@ namespace Scratch.FolderManager {
}
} catch (Error e) {
warning (e.message);
} finally {
}
});

/* Need to remove rename item even when editing cancelled so cannot use "edited" signal */
Timeout.add (200, () => {
if (view.editing) {
return Source.CONTINUE;
} else {
remove (rename_item);
}
}

return Source.REMOVE;
});
return Source.REMOVE;
});
} else {
remove (rename_item);
}


return Source.REMOVE;
});
Expand Down
16 changes: 16 additions & 0 deletions src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,22 @@ namespace Scratch {

folder_manager_view.restore_saved_state ();

folder_manager_view.rename_request.connect ((file) => {
var allow = true;
foreach (var window in app.get_windows ()) {
var win = (MainWindow)window;
foreach (var doc in win.document_view.docs) {
if (doc.file.equal (file.file)) {
// Only allow sidebar to rename docs that are in sync with their file in
// all windows
allow = allow && !doc.locked && doc.saved;
}
}
}

return allow;
});

terminal = new Code.Terminal () {
no_show_all = true,
visible = false
Expand Down