Skip to content

Commit

Permalink
Drop folder onto sidebar to open as project (#1339)
Browse files Browse the repository at this point in the history
* Implement simple drop destination for sidebar

* Fix faulty revert
  • Loading branch information
jeremypw committed Dec 1, 2023
1 parent 15ea659 commit c02a406
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 15 deletions.
35 changes: 20 additions & 15 deletions src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ namespace Scratch {
{ ACTION_FIND_PREVIOUS, action_find_previous },
{ ACTION_FIND_GLOBAL, action_find_global, "s" },
{ ACTION_OPEN, action_open },
{ ACTION_OPEN_FOLDER, action_open_folder },
{ ACTION_OPEN_FOLDER, action_open_folder, "s" },
{ ACTION_COLLAPSE_ALL_FOLDERS, action_collapse_all_folders },
{ ACTION_ORDER_FOLDERS, action_order_folders },
{ ACTION_PREFERENCES, action_preferences },
Expand Down Expand Up @@ -913,23 +913,28 @@ namespace Scratch {
}
}

private void action_open_folder () {
var chooser = new Gtk.FileChooserNative (
"Select a folder.", this, Gtk.FileChooserAction.SELECT_FOLDER,
_("_Open"),
_("_Cancel")
);
private void action_open_folder (SimpleAction action, Variant? param) {
var path = param.get_string ();
if (path == "") {
var chooser = new Gtk.FileChooserNative (
"Select a folder.", this, Gtk.FileChooserAction.SELECT_FOLDER,
_("_Open"),
_("_Cancel")
);

chooser.select_multiple = true;
chooser.select_multiple = true;

if (chooser.run () == Gtk.ResponseType.ACCEPT) {
chooser.get_files ().foreach ((glib_file) => {
var foldermanager_file = new FolderManager.File (glib_file.get_path ());
folder_manager_view.open_folder (foldermanager_file);
});
}
if (chooser.run () == Gtk.ResponseType.ACCEPT) {
chooser.get_files ().foreach ((glib_file) => {
var foldermanager_file = new FolderManager.File (glib_file.get_path ());
folder_manager_view.open_folder (foldermanager_file);
});
}

chooser.destroy ();
chooser.destroy ();
} else {
folder_manager_view.open_folder (new FolderManager.File (path));
}
}

private void action_collapse_all_folders () {
Expand Down
41 changes: 41 additions & 0 deletions src/Widgets/Sidebar.vala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
*/

public class Code.Sidebar : Gtk.Grid {
public enum TargetType {
URI_LIST
}

public Gtk.Stack stack { get; private set; }
public Code.ChooseProjectButton choose_project_button { get; private set; }
public Hdy.HeaderBar headerbar { get; private set; }
Expand Down Expand Up @@ -53,6 +57,7 @@ public class Code.Sidebar : Gtk.Grid {

var add_folder_button = new Gtk.Button.from_icon_name ("folder-open-symbolic", Gtk.IconSize.SMALL_TOOLBAR) {
action_name = Scratch.MainWindow.ACTION_PREFIX + Scratch.MainWindow.ACTION_OPEN_FOLDER,
action_target = new Variant.string (""),
always_show_image = true,
label = _("Open Folder…")
};
Expand Down Expand Up @@ -105,6 +110,42 @@ public class Code.Sidebar : Gtk.Grid {
break;
}
});

Gtk.TargetEntry uris = {"text/uri-list", 0, TargetType.URI_LIST};
Gtk.drag_dest_set (this, Gtk.DestDefaults.ALL, {uris}, Gdk.DragAction.COPY);
drag_data_received.connect (drag_received);
}

private void drag_received (Gtk.Widget w,
Gdk.DragContext ctx,
int x,
int y,
Gtk.SelectionData sel,
uint info,
uint time) {

if (info == TargetType.URI_LIST) {
var uri_list = sel.get_uris ();
GLib.List<GLib.File> folder_list = null;
foreach (unowned var uri in uri_list) {
var file = GLib.File.new_for_uri (uri);
// Blocking but for simplicity omit cancellable for now
var ftype = file.query_file_type (FileQueryInfoFlags.NOFOLLOW_SYMLINKS);
if (ftype == GLib.FileType.DIRECTORY) {
folder_list.prepend (file);
}
}

foreach (var folder in folder_list) {
var win_group = get_action_group (Scratch.MainWindow.ACTION_GROUP);
win_group.activate_action (
Scratch.MainWindow.ACTION_OPEN_FOLDER,
new Variant.string (folder.get_path ())
);
}

Gtk.drag_finish (ctx, folder_list.length () > 0, false, time);
}
}

public void add_tab (Code.PaneSwitcher tab) {
Expand Down

0 comments on commit c02a406

Please sign in to comment.