Skip to content

Commit

Permalink
Add music file[s] from within app (#766)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremypw authored May 14, 2024
1 parent 07d7485 commit 7ebb2bf
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 35 deletions.
17 changes: 17 additions & 0 deletions data/Application.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@
* SPDX-FileCopyrightText: 2021 elementary, Inc. (https://elementary.io)
*/

toolbarview revealer.bottom-bar.raised {
box-shadow: 0 -1px alpha(@highlight_color, 0.2);
background-color: alpha(black, 0.01);
background-image:
linear-gradient(
to bottom,
@borders 1px,
alpha(black, 0.07) 1px,
transparent 3px
);
color: mix(@text_color, @insensitive_bg_color, 0.15);
}

toolbarview revealer.bottom-bar actionbar {
padding: 6px;
}

album {
background-image:
linear-gradient(
Expand Down
4 changes: 2 additions & 2 deletions src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public class Music.Application : Gtk.Application {
settings.bind ("window-maximized", main_window, "maximized", SettingsBindFlags.SET);
}

private File[] list_directory (string directory) {
private static File[] list_directory (string directory) {
Dir dir;
try {
dir = Dir.open (directory, 0);
Expand All @@ -129,7 +129,7 @@ public class Music.Application : Gtk.Application {
return elements;
}

private File[] loop_through_files (File[] files) {
public static File[] loop_through_files (File[] files) {
// All of these will be returned later in bulk
File[] elements = {};

Expand Down
109 changes: 76 additions & 33 deletions src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,30 @@ public class Music.MainWindow : Gtk.ApplicationWindow {

var drop_target = new Gtk.DropTarget (typeof (Gdk.FileList), Gdk.DragAction.COPY);

var add_button_label = new Gtk.Label (_("Open Files…"));

var add_button_box = new Gtk.Box (HORIZONTAL, 0);
add_button_box.append (new Gtk.Image.from_icon_name ("document-open-symbolic"));
add_button_box.append (add_button_label);

var add_button = new Gtk.Button () {
child = add_button_box,
};
add_button.add_css_class (Granite.STYLE_CLASS_FLAT);

add_button_label.mnemonic_widget = add_button;

var queue_action_bar = new Gtk.ActionBar ();
queue_action_bar.pack_start (add_button);

var queue = new Adw.ToolbarView () {
bottom_bar_style = RAISED,
content = scrolled
};
queue.add_controller (drop_target);
queue.add_css_class (Granite.STYLE_CLASS_VIEW);
queue.add_top_bar (queue_header);
queue.add_bottom_bar (queue_action_bar);

var error_toast = new Granite.Toast ("");

Expand Down Expand Up @@ -115,23 +133,15 @@ public class Music.MainWindow : Gtk.ApplicationWindow {

drop_target.drop.connect ((target, value, x, y) => {
if (value.type () == typeof (Gdk.FileList)) {
File[] files;
SList<File> file_list = null;
foreach (unowned var file in (SList<File>) value.get_boxed ()) {
var file_type = file.query_file_type (FileQueryInfoFlags.NONE);
if (file_type == FileType.DIRECTORY) {
prepend_directory_files (file, ref file_list);
} else {
file_list.prepend (file);
}
}
var list = (Gdk.FileList)value;

file_list.reverse ();
foreach (unowned var file in file_list) {
files += file;
File[] file_array = {};
foreach (unowned var file in list.get_files ()) {
file_array += file;
}

playback_manager.queue_files (files);
var files_to_play = Application.loop_through_files (file_array);
PlaybackManager.get_default ().queue_files (files_to_play);

return true;
}
Expand All @@ -147,6 +157,8 @@ public class Music.MainWindow : Gtk.ApplicationWindow {
error_toast.send_notification ();
});

add_button.clicked.connect (action_open);

repeat_button.clicked.connect (() => {
var enum_step = settings.get_enum ("repeat-mode");
if (enum_step < 2) {
Expand All @@ -161,27 +173,58 @@ public class Music.MainWindow : Gtk.ApplicationWindow {
});
}

//Array concatenation not permitted for parameters so use a list instead
private void prepend_directory_files (GLib.File dir, ref SList<File> file_list) {
try {
var enumerator = dir.enumerate_children (
"standard::*",
FileQueryInfoFlags.NOFOLLOW_SYMLINKS,
null
);

FileInfo info = null;
while ((info = enumerator.next_file (null)) != null) {
var child = dir.resolve_relative_path (info.get_name ());
if (info.get_file_type () == FileType.DIRECTORY) {
prepend_directory_files (child, ref file_list);
} else {
file_list.prepend (child);
private void action_open () {
var all_files_filter = new Gtk.FileFilter () {
name = _("All files"),
};
all_files_filter.add_pattern ("*");

var music_files_filter = new Gtk.FileFilter () {
name = _("Music files"),
};
music_files_filter.add_mime_type ("audio/*");

var filter_model = new ListStore (typeof (Gtk.FileFilter));
filter_model.append (all_files_filter);
filter_model.append (music_files_filter);

var file_dialog = new Gtk.FileDialog () {
accept_label = _("Open"),
default_filter = music_files_filter,
filters = filter_model,
modal = true,
title = _("Open audio files")
};

file_dialog.open_multiple.begin (this, null, (obj, res) => {
try {
var files = file_dialog.open_multiple.end (res);

File[] file_array = {};
for (int i = 0; i < files.get_n_items (); i++) {
file_array += (File)(files.get_item (i));
}

var files_to_play = Application.loop_through_files (file_array);
PlaybackManager.get_default ().queue_files (files_to_play);
} catch (Error e) {
if (e.matches (Gtk.DialogError.quark (), Gtk.DialogError.DISMISSED)) {
return;
}

var dialog = new Granite.MessageDialog (
"Couldn't add audio files",
e.message,
new ThemedIcon ("document-open")
) {
badge_icon = new ThemedIcon ("dialog-error"),
modal = true,
transient_for = this
};
dialog.present ();
dialog.response.connect (dialog.destroy);
}
} catch (Error e) {
warning ("Error while enumerating children of %s: %s", dir.get_uri (), e.message);
}
});
}

private void update_repeat_button () {
Expand Down

0 comments on commit 7ebb2bf

Please sign in to comment.