From 7ebb2bf78d7502dd017faeb04a01cce5e6a79ec9 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Tue, 14 May 2024 17:48:05 +0000 Subject: [PATCH] Add music file[s] from within app (#766) --- data/Application.css | 17 +++++++ src/Application.vala | 4 +- src/MainWindow.vala | 109 ++++++++++++++++++++++++++++++------------- 3 files changed, 95 insertions(+), 35 deletions(-) diff --git a/data/Application.css b/data/Application.css index bccb159ea..847d30fa9 100644 --- a/data/Application.css +++ b/data/Application.css @@ -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( diff --git a/src/Application.vala b/src/Application.vala index f27503c75..ce0db16b8 100644 --- a/src/Application.vala +++ b/src/Application.vala @@ -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); @@ -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 = {}; diff --git a/src/MainWindow.vala b/src/MainWindow.vala index 08f8e0c9e..e542cdf63 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -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 (""); @@ -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_list = null; - foreach (unowned var file in (SList) 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; } @@ -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) { @@ -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_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 () {