flaccy 1.5.0

Lossless music player for Linux
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
use crate::events::AppEvent;
use crate::library::{format_time, Track};
use crate::ui::{context, Ui};
use adw::prelude::*;
use gtk::gdk;
use gtk::pango;
use std::cell::RefCell;
use std::rc::Rc;

pub fn build(ui: &Rc<Ui>) -> gtk::Widget {
    let list = gtk::ListBox::builder()
        .selection_mode(gtk::SelectionMode::None)
        .valign(gtk::Align::Start)
        .build();
    list.add_css_class("boxed-list");

    let ids: Rc<RefCell<Vec<i64>>> = Rc::new(RefCell::new(Vec::new()));

    {
        let ui = Rc::clone(ui);
        let ids = Rc::clone(&ids);
        list.connect_row_activated(move |_, row| {
            let id = ids.borrow().get(row.index().max(0) as usize).copied();
            if let Some(id) = id {
                push_playlist_detail(&ui, id);
            }
        });
    }

    let new_button = gtk::Button::builder().label("New Playlist").build();
    new_button.add_css_class("pill");
    new_button.set_halign(gtk::Align::Start);
    {
        let ui = Rc::clone(ui);
        new_button.connect_clicked(move |_| prompt_new_playlist(&ui, None));
    }

    let content = gtk::Box::builder()
        .orientation(gtk::Orientation::Vertical)
        .spacing(18)
        .margin_top(18)
        .margin_bottom(18)
        .margin_start(18)
        .margin_end(18)
        .build();
    content.append(&new_button);
    content.append(&list);

    let empty = adw::StatusPage::builder()
        .icon_name("view-list-symbolic")
        .title("No Playlists")
        .description("Create a playlist, then add songs from any track's context menu.")
        .build();
    let empty_button = gtk::Button::with_label("New Playlist");
    empty_button.add_css_class("pill");
    empty_button.add_css_class("suggested-action");
    empty_button.set_halign(gtk::Align::Center);
    {
        let ui = Rc::clone(ui);
        empty_button.connect_clicked(move |_| prompt_new_playlist(&ui, None));
    }
    empty.set_child(Some(&empty_button));

    let scroll = gtk::ScrolledWindow::builder()
        .hscrollbar_policy(gtk::PolicyType::Never)
        .child(&adw::Clamp::builder().maximum_size(760).child(&content).build())
        .build();

    let stack = gtk::Stack::new();
    stack.add_named(&empty, Some("empty"));
    stack.add_named(&scroll, Some("list"));

    let rebuild = {
        let list = list.clone();
        let stack = stack.clone();
        let ids = Rc::clone(&ids);
        let ui = Rc::clone(ui);
        move || {
            while let Some(child) = list.first_child() {
                list.remove(&child);
            }
            let playlists = ui.core.db.fetch_playlists();
            let mut collected = Vec::new();
            for playlist in &playlists {
                collected.push(playlist.id);
                let row_box = gtk::Box::builder()
                    .orientation(gtk::Orientation::Horizontal)
                    .spacing(14)
                    .margin_top(10)
                    .margin_bottom(10)
                    .margin_start(10)
                    .margin_end(10)
                    .build();
                row_box.append(&gtk::Image::from_icon_name("view-list-symbolic"));
                let name = gtk::Label::builder()
                    .label(&playlist.name)
                    .xalign(0.0)
                    .hexpand(true)
                    .ellipsize(pango::EllipsizeMode::End)
                    .build();
                name.add_css_class("album-title");
                row_box.append(&name);
                let count = gtk::Label::new(Some(&format!(
                    "{} track{}",
                    playlist.track_count,
                    if playlist.track_count == 1 { "" } else { "s" }
                )));
                count.add_css_class("dim");
                count.add_css_class("caption");
                row_box.append(&count);
                let list_row = gtk::ListBoxRow::builder().child(&row_box).build();
                attach_playlist_list_menu(&list_row, playlist.id);
                list.append(&list_row);
            }
            *ids.borrow_mut() = collected;
            stack.set_visible_child_name(if playlists.is_empty() { "empty" } else { "list" });
        }
    };
    rebuild();

    {
        let rebuild = rebuild.clone();
        ui.core.hub.subscribe_widget(&stack, move |_, event| {
            if let AppEvent::LibraryReloaded = event {
                rebuild();
            }
        });
    }

    stack.upcast()
}

pub fn prompt_new_playlist(ui: &Rc<Ui>, add_rel_path: Option<String>) {
    let dialog = adw::AlertDialog::builder()
        .heading("New Playlist")
        .body("Name your new playlist.")
        .close_response("cancel")
        .default_response("create")
        .build();
    dialog.add_response("cancel", "Cancel");
    dialog.add_response("create", "Create");
    dialog.set_response_appearance("create", adw::ResponseAppearance::Suggested);

    let entry = gtk::Entry::builder()
        .placeholder_text("Playlist name")
        .activates_default(true)
        .build();
    dialog.set_extra_child(Some(&entry));

    let window = ui.window.clone();
    let ui = Rc::clone(ui);
    dialog.connect_response(None, move |_, response| {
        if response != "create" {
            return;
        }
        let name = entry.text().trim().to_string();
        if name.is_empty() {
            return;
        }
        match ui.core.db.create_playlist(&name) {
            Ok(id) => {
                if let Some(rel) = &add_rel_path {
                    let _ = ui.core.db.add_track_to_playlist(id, rel);
                }
                crate::logger::info("database", &format!("created playlist '{name}'"));
                ui.core.hub.emit(&AppEvent::LibraryReloaded);
            }
            Err(err) => {
                crate::logger::error("database", &format!("create playlist failed: {err}"));
            }
        }
    });
    dialog.present(Some(&window));
}

fn push_playlist_detail(ui: &Rc<Ui>, playlist_id: i64) {
    let name = ui
        .core
        .db
        .playlist_name(playlist_id)
        .unwrap_or_else(|| "Playlist".to_string());

    let list = gtk::ListBox::builder()
        .selection_mode(gtk::SelectionMode::None)
        .valign(gtk::Align::Start)
        .build();
    list.add_css_class("boxed-list");

    let entries: Rc<RefCell<Vec<(i64, Track)>>> = Rc::new(RefCell::new(Vec::new()));

    let rebuild = {
        let list = list.clone();
        let entries = Rc::clone(&entries);
        let ui = Rc::clone(ui);
        Rc::new(move || {
            while let Some(child) = list.first_child() {
                list.remove(&child);
            }
            let library = ui.core.library.borrow().clone();
            let by_rel_path: std::collections::HashMap<&str, &Track> = library
                .tracks
                .iter()
                .map(|track| (track.rel_path.as_str(), track))
                .collect();
            let rows = ui.core.db.playlist_tracks(playlist_id);
            let mut collected = Vec::new();
            for row in rows {
                let Some(track) = by_rel_path.get(row.rel_path.as_str()).map(|t| (*t).clone()) else {
                    continue;
                };
                collected.push((row.row_id, track.clone()));
                let index = collected.len();
                let row_box = gtk::Box::builder()
                    .orientation(gtk::Orientation::Horizontal)
                    .spacing(12)
                    .margin_top(10)
                    .margin_bottom(10)
                    .margin_start(12)
                    .margin_end(12)
                    .build();
                let number = gtk::Label::builder()
                    .label(index.to_string())
                    .width_chars(3)
                    .xalign(1.0)
                    .build();
                number.add_css_class("track-number");
                row_box.append(&number);
                let text_box = gtk::Box::new(gtk::Orientation::Vertical, 2);
                let title = gtk::Label::builder()
                    .label(&track.title)
                    .xalign(0.0)
                    .ellipsize(pango::EllipsizeMode::End)
                    .build();
                text_box.append(&title);
                let artist = gtk::Label::builder()
                    .label(&track.artist)
                    .xalign(0.0)
                    .ellipsize(pango::EllipsizeMode::End)
                    .build();
                artist.add_css_class("dim");
                artist.add_css_class("caption");
                text_box.append(&artist);
                text_box.set_hexpand(true);
                row_box.append(&text_box);
                let duration = gtk::Label::new(Some(&format_time(track.duration)));
                duration.add_css_class("duration-label");
                row_box.append(&duration);

                let list_row = gtk::ListBoxRow::builder().child(&row_box).build();
                attach_playlist_row_menu(&ui, &list_row, playlist_id, row.row_id, &track);
                attach_reorder_dnd(&ui, &list_row, &list, playlist_id, &entries);
                list.append(&list_row);
            }
            *entries.borrow_mut() = collected;
        })
    };
    rebuild();

    {
        let ui = Rc::clone(ui);
        let entries = Rc::clone(&entries);
        list.connect_row_activated(move |_, row| {
            let tracks: Vec<Track> = entries.borrow().iter().map(|(_, t)| t.clone()).collect();
            let index = row.index().max(0) as usize;
            if index < tracks.len() {
                ui.core.play_tracks(tracks, index);
            }
        });
    }

    let header = gtk::Box::builder()
        .orientation(gtk::Orientation::Horizontal)
        .spacing(12)
        .build();
    let title = gtk::Label::builder().label(&name).xalign(0.0).hexpand(true).build();
    title.add_css_class("title-1");
    header.append(&title);

    let play = gtk::Button::builder().label("Play").build();
    play.add_css_class("pill");
    play.add_css_class("suggested-action");
    {
        let ui = Rc::clone(ui);
        let entries = Rc::clone(&entries);
        play.connect_clicked(move |_| {
            let tracks: Vec<Track> = entries.borrow().iter().map(|(_, t)| t.clone()).collect();
            if !tracks.is_empty() {
                ui.core.play_tracks(tracks, 0);
            }
        });
    }
    header.append(&play);

    let delete = gtk::Button::builder().label("Delete").build();
    delete.add_css_class("pill");
    delete.add_css_class("destructive-action");
    {
        let ui = Rc::clone(ui);
        delete.connect_clicked(move |_| {
            let dialog = adw::AlertDialog::builder()
                .heading("Delete Playlist?")
                .body("This removes the playlist. Your music files stay untouched.")
                .close_response("cancel")
                .build();
            dialog.add_response("cancel", "Cancel");
            dialog.add_response("delete", "Delete");
            dialog.set_response_appearance("delete", adw::ResponseAppearance::Destructive);
            let window = ui.window.clone();
            let ui = Rc::clone(&ui);
            dialog.connect_response(None, move |_, response| {
                if response == "delete" {
                    let _ = ui.core.db.delete_playlist(playlist_id);
                    ui.core.hub.emit(&AppEvent::LibraryReloaded);
                    ui.nav.pop();
                }
            });
            dialog.present(Some(&window));
        });
    }
    header.append(&delete);

    let content = gtk::Box::builder()
        .orientation(gtk::Orientation::Vertical)
        .spacing(18)
        .margin_top(24)
        .margin_bottom(24)
        .margin_start(24)
        .margin_end(24)
        .build();
    content.append(&header);
    content.append(&list);

    let hint = gtk::Label::new(Some("Drag rows to reorder · right-click to remove"));
    hint.add_css_class("dim-more");
    hint.add_css_class("caption");
    hint.set_xalign(0.0);
    content.append(&hint);

    let scroll = gtk::ScrolledWindow::builder()
        .hscrollbar_policy(gtk::PolicyType::Never)
        .child(&adw::Clamp::builder().maximum_size(760).child(&content).build())
        .build();

    {
        let rebuild = Rc::clone(&rebuild);
        ui.core.hub.subscribe_widget(&scroll, move |_, event| {
            if let AppEvent::LibraryReloaded = event {
                rebuild();
            }
        });
    }

    let page = adw::NavigationPage::builder().title(&name).child(&scroll).build();
    ui.nav.push(&page);
}

fn attach_playlist_list_menu(row: &gtk::ListBoxRow, playlist_id: i64) {
    let gesture = gtk::GestureClick::builder()
        .button(gdk::BUTTON_SECONDARY)
        .build();
    let target = row.clone();
    gesture.connect_pressed(move |_, _, x, y| {
        context::popup_menu_at(&target, &playlist_list_menu(playlist_id), x, y);
    });
    row.add_controller(gesture);
}

fn playlist_list_menu(playlist_id: i64) -> gtk::gio::Menu {
    let menu = gtk::gio::Menu::new();
    let play_section = gtk::gio::Menu::new();
    play_section.append_item(&playlist_item("Play", "win.playlist-play", playlist_id));
    play_section.append_item(&playlist_item("Shuffle", "win.playlist-shuffle", playlist_id));
    menu.append_section(None, &play_section);
    let manage_section = gtk::gio::Menu::new();
    manage_section.append_item(&playlist_item("Rename…", "win.playlist-rename", playlist_id));
    manage_section.append_item(&playlist_item("Delete", "win.playlist-delete", playlist_id));
    menu.append_section(None, &manage_section);
    menu
}

fn playlist_item(label: &str, action: &str, playlist_id: i64) -> gtk::gio::MenuItem {
    let entry = gtk::gio::MenuItem::new(Some(label), None);
    entry.set_action_and_target_value(Some(action), Some(&playlist_id.to_variant()));
    entry
}

/// Resolves a playlist's stored track order against the live library, dropping
/// rows whose file has since left the library.
fn resolve_playlist_tracks(ui: &Rc<Ui>, playlist_id: i64) -> Vec<Track> {
    let library = ui.core.library.borrow().clone();
    let by_rel_path: std::collections::HashMap<&str, &Track> = library
        .tracks
        .iter()
        .map(|track| (track.rel_path.as_str(), track))
        .collect();
    ui.core
        .db
        .playlist_tracks(playlist_id)
        .into_iter()
        .filter_map(|row| by_rel_path.get(row.rel_path.as_str()).map(|track| (*track).clone()))
        .collect()
}

pub fn play_playlist(ui: &Rc<Ui>, playlist_id: i64, shuffle: bool) {
    let tracks = resolve_playlist_tracks(ui, playlist_id);
    if tracks.is_empty() {
        ui.core.toast("Playlist has no playable tracks");
        return;
    }
    if shuffle != ui.core.player.shuffle_enabled() {
        ui.core.player.toggle_shuffle();
    }
    ui.core.play_tracks(tracks, 0);
}

pub fn prompt_rename_playlist(ui: &Rc<Ui>, playlist_id: i64) {
    let current = ui.core.db.playlist_name(playlist_id).unwrap_or_default();
    let dialog = adw::AlertDialog::builder()
        .heading("Rename Playlist")
        .body("Choose a new name.")
        .close_response("cancel")
        .default_response("rename")
        .build();
    dialog.add_response("cancel", "Cancel");
    dialog.add_response("rename", "Rename");
    dialog.set_response_appearance("rename", adw::ResponseAppearance::Suggested);

    let entry = gtk::Entry::builder()
        .text(&current)
        .activates_default(true)
        .build();
    dialog.set_extra_child(Some(&entry));

    let window = ui.window.clone();
    let ui = Rc::clone(ui);
    dialog.connect_response(None, move |_, response| {
        if response != "rename" {
            return;
        }
        let name = entry.text().trim().to_string();
        if name.is_empty() {
            return;
        }
        match ui.core.db.rename_playlist(playlist_id, &name) {
            Ok(()) => {
                crate::logger::info("database", &format!("renamed playlist {playlist_id} to '{name}'"));
                ui.core.hub.emit(&AppEvent::LibraryReloaded);
            }
            Err(err) => {
                crate::logger::error("database", &format!("rename playlist failed: {err}"));
            }
        }
    });
    dialog.present(Some(&window));
}

pub fn confirm_delete_playlist(ui: &Rc<Ui>, playlist_id: i64) {
    let dialog = adw::AlertDialog::builder()
        .heading("Delete Playlist?")
        .body("This removes the playlist. Your music files stay untouched.")
        .close_response("cancel")
        .build();
    dialog.add_response("cancel", "Cancel");
    dialog.add_response("delete", "Delete");
    dialog.set_response_appearance("delete", adw::ResponseAppearance::Destructive);
    let window = ui.window.clone();
    let ui = Rc::clone(ui);
    dialog.connect_response(None, move |_, response| {
        if response == "delete" {
            let _ = ui.core.db.delete_playlist(playlist_id);
            ui.core.hub.emit(&AppEvent::LibraryReloaded);
        }
    });
    dialog.present(Some(&window));
}

fn attach_playlist_row_menu(
    ui: &Rc<Ui>,
    row: &gtk::ListBoxRow,
    playlist_id: i64,
    row_id: i64,
    track: &Track,
) {
    let gesture = gtk::GestureClick::builder()
        .button(gdk::BUTTON_SECONDARY)
        .build();
    let ui = Rc::clone(ui);
    let target = row.clone();
    let rel_path = track.rel_path.clone();
    gesture.connect_pressed(move |_, _, x, y| {
        let loved = ui
            .core
            .library
            .borrow()
            .track_by_rel_path(&rel_path)
            .map(|t| t.loved)
            .unwrap_or(false);
        let menu = context::track_menu(&ui.core, &rel_path, loved);
        let remove_section = gtk::gio::Menu::new();
        let remove_item = gtk::gio::MenuItem::new(Some("Remove from Playlist"), None);
        remove_item.set_action_and_target_value(
            Some("win.playlist-remove-row"),
            Some(&(playlist_id, row_id).to_variant()),
        );
        remove_section.append_item(&remove_item);
        menu.append_section(None, &remove_section);
        context::popup_menu_at(&target, &menu, x, y);
    });
    row.add_controller(gesture);
}

fn attach_reorder_dnd(
    ui: &Rc<Ui>,
    row: &gtk::ListBoxRow,
    list: &gtk::ListBox,
    playlist_id: i64,
    entries: &Rc<RefCell<Vec<(i64, Track)>>>,
) {
    let drag = gtk::DragSource::builder().actions(gdk::DragAction::MOVE).build();
    {
        let row = row.clone();
        drag.connect_prepare(move |_, _, _| {
            Some(gdk::ContentProvider::for_value(&(row.index()).to_value()))
        });
    }
    row.add_controller(drag);

    let drop = gtk::DropTarget::new(i32::static_type(), gdk::DragAction::MOVE);
    {
        let ui = Rc::clone(ui);
        let list = list.clone();
        let entries = Rc::clone(entries);
        let row = row.clone();
        drop.connect_drop(move |_, value, _, _| {
            let Ok(source_index) = value.get::<i32>() else { return false };
            let target_index = row.index();
            if source_index == target_index || source_index < 0 || target_index < 0 {
                return false;
            }
            let mut order: Vec<i64> = entries.borrow().iter().map(|(id, _)| *id).collect();
            if source_index as usize >= order.len() {
                return false;
            }
            let moved = order.remove(source_index as usize);
            let insert_at = (target_index as usize).min(order.len());
            order.insert(insert_at, moved);
            if let Err(err) = ui.core.db.reorder_playlist(playlist_id, &order) {
                crate::logger::error("database", &format!("reorder failed: {err}"));
                return false;
            }
            let _ = &list;
            ui.core.hub.emit(&AppEvent::LibraryReloaded);
            true
        });
    }
    row.add_controller(drop);
}