-
-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathMainWindow.vala
More file actions
160 lines (132 loc) · 5.23 KB
/
MainWindow.vala
File metadata and controls
160 lines (132 loc) · 5.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* SPDX-License-Identifier: LGPL-3.0-or-later
* SPDX-FileCopyrightText: 2021 elementary, Inc. (https://elementary.io)
*/
public class Music.MainWindow : Gtk.ApplicationWindow {
public const string ACTION_PREFIX = "win.";
public const string ACTION_OPEN = "action-open";
public const string ACTION_OPEN_FOLDER = "action-open-folder";
private QueueView queue_view;
construct {
queue_view = new QueueView ();
var end_window_controls = new Gtk.WindowControls (Gtk.PackType.END);
var end_header = new Gtk.HeaderBar () {
show_title_buttons = false,
title_widget = new Gtk.Label ("")
};
end_header.add_css_class (Granite.STYLE_CLASS_FLAT);
end_header.add_css_class (Granite.STYLE_CLASS_DEFAULT_DECORATION);
end_header.pack_end (end_window_controls);
var now_playing_view = new NowPlayingView () {
margin_top = 12,
margin_end = 12,
margin_bottom = 24,
margin_start = 12,
vexpand = true
};
var now_playing = new Gtk.Box (VERTICAL, 0);
now_playing.append (end_header);
now_playing.append (now_playing_view);
var now_playing_handle = new Gtk.WindowHandle () {
child = now_playing
};
var paned = new Gtk.Paned (Gtk.Orientation.HORIZONTAL) {
start_child = queue_view,
end_child = now_playing_handle,
resize_end_child = false,
shrink_end_child = false,
shrink_start_child = false
};
child = paned;
// We need to hide the title area for the split headerbar
var null_title = new Gtk.Grid () {
visible = false
};
set_titlebar (null_title);
var settings = new Settings ("io.elementary.music");
settings.bind ("pane-position", paned, "position", SettingsBindFlags.DEFAULT);
var open_action = new SimpleAction (ACTION_OPEN, null);
open_action.activate.connect (open_files);
add_action (open_action);
var open_folder_action = new SimpleAction (ACTION_OPEN_FOLDER, null);
open_folder_action.activate.connect (open_folder);
add_action (open_folder_action);
unowned var app = ((Gtk.Application) GLib.Application.get_default ());
app.set_accels_for_action (ACTION_PREFIX + ACTION_OPEN, {"<Ctrl>O"});
app.set_accels_for_action (ACTION_PREFIX + ACTION_OPEN_FOLDER, {"<Ctrl><Shift>O"});
}
public void start_search () {
queue_view.start_search ();
}
private void open_files () {
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);
handle_selected_files (files);
} catch (Error e) {
handle_file_dialog_error (e);
}
});
}
/**
* Same as open_files, except there is not filter and it uses the Folder dialog :
* No folder/file is pre-selected when entering a directory and individual files cannot be selected
*/
private void open_folder () {
var file_dialog = new Gtk.FileDialog () {
accept_label = _("Open"),
modal = true,
title = _("Open folder(s) containing audio files")
};
file_dialog.select_multiple_folders.begin (this, null, (obj, res) => {
try {
var folders = file_dialog.select_multiple_folders.end (res);
handle_selected_files (folders);
} catch (Error e) {
handle_file_dialog_error (e);
}
});
}
private void handle_selected_files (GLib.ListModel files) {
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);
}
private void handle_file_dialog_error (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);
}
}