From cebf2069cbec96745c151e5ab46695d3d996112e Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Fri, 2 Sep 2016 11:10:27 -0400 Subject: [PATCH] gui/playlists/system: Respond to favorite and hide buttons I also take this opportunity to rename these widgets and add accessor functions. Signed-off-by: Anna Schumaker --- gui/audio.c | 24 ++----------------- gui/playlists/system.c | 27 +++++++++++++++++++++ include/gui/playlists/system.h | 16 +++++++++++++ share/ocarina/ocarina.ui | 8 +++---- tests/gui/audio.c | 12 +++++----- tests/gui/playlists/system.c | 43 +++++++++++++++++++++++++++++++++- 6 files changed, 97 insertions(+), 33 deletions(-) diff --git a/gui/audio.c b/gui/audio.c index a666511d..478a5cbb 100644 --- a/gui/audio.c +++ b/gui/audio.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -36,11 +37,7 @@ static void __audio_load(struct track *track) __audio_set_label("o_album", "x-large", track->tr_album->al_name); __audio_set_time_label("o_duration", track->tr_length); - gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(gui_builder_widget("o_hide")), - playlist_has(PL_SYSTEM, "Hidden", track)); - gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(gui_builder_widget("o_favorite")), - playlist_has(PL_SYSTEM, "Favorites", track)); - + gui_pl_system_track_loaded(track); gui_treeview_scroll(); gui_artwork_set_cover(); gui_idle_enable(); @@ -79,23 +76,6 @@ void __audio_volume_changed(GtkScaleButton *button, gdouble value, gpointer data audio_set_volume((unsigned int)value); } -void __audio_favorite(GtkToggleButton *toggle, gpointer data) -{ - if (gtk_toggle_button_get_active(toggle)) - playlist_add(PL_SYSTEM, "Favorites", audio_cur_track()); - else - playlist_remove(PL_SYSTEM, "Favorites", audio_cur_track()); -} - -void __audio_hide(GtkToggleButton *toggle, gpointer data) -{ - if (gtk_toggle_button_get_active(toggle)) { - if (playlist_add(PL_SYSTEM, "Hidden", audio_cur_track())) - audio_next(); - } else - playlist_remove(PL_SYSTEM, "Hidden", audio_cur_track()); -} - static int __audio_timeout(gpointer data) { GtkAdjustment *progress = data; diff --git a/gui/playlists/system.c b/gui/playlists/system.c index 13fa3025..2078ac5d 100644 --- a/gui/playlists/system.c +++ b/gui/playlists/system.c @@ -1,9 +1,11 @@ /* * Copyright 2016 (c) Anna Schumaker. */ +#include #include #include #include +#include #include static bool __gui_pl_system_is_playlist(struct playlist *playlist) @@ -31,6 +33,23 @@ static bool __gui_pl_system_find_descend_header(GtkTreeIter *iter, return gui_sidebar_iter_down(&header, iter); } +void __gui_pl_system_favorite_toggled(GtkToggleButton *toggle, gpointer data) +{ + if (gtk_toggle_button_get_active(toggle)) + playlist_add(PL_SYSTEM, "Favorites", audio_cur_track()); + else + playlist_remove(PL_SYSTEM, "Favorites", audio_cur_track()); +} + +void __gui_pl_system_hide_toggled(GtkToggleButton *toggle, gpointer data) +{ + if (gtk_toggle_button_get_active(toggle)) { + if (playlist_add(PL_SYSTEM, "Hidden", audio_cur_track())) + audio_next(); + } else + playlist_remove(PL_SYSTEM, "Hidden", audio_cur_track()); +} + static bool __gui_pl_system_init_idle() { GtkTreeIter iter; @@ -86,3 +105,11 @@ void gui_pl_system_update(struct playlist *playlist) if (gui_sidebar_iter_find(&iter, playlist->pl_name, playlist->pl_type)) gui_sidebar_iter_update(&iter); } + +void gui_pl_system_track_loaded(struct track *track) +{ + gtk_toggle_button_set_active(gui_favorite_button(), + playlist_has(PL_SYSTEM, "Favorites", track)); + gtk_toggle_button_set_active(gui_hide_button(), + playlist_has(PL_SYSTEM, "Hidden", track)); +} diff --git a/include/gui/playlists/system.h b/include/gui/playlists/system.h index 71a1354c..86290f58 100644 --- a/include/gui/playlists/system.h +++ b/include/gui/playlists/system.h @@ -3,6 +3,7 @@ */ #ifndef OCARINA_GUI_PLAYLISTS_SYSTEM_H #define OCARINA_GUI_PLAYLISTS_SYSTEM_H +#include /* Called to initialize GUI system playlists. */ void gui_pl_system_init(); @@ -10,4 +11,19 @@ void gui_pl_system_init(); /* Called to update a system playlist. */ void gui_pl_system_update(struct playlist *); +/* Called to set favorites and hidden button states. */ +void gui_pl_system_track_loaded(struct track *); + +/* Called to get the favorite toggle button. */ +static inline GtkToggleButton *gui_favorite_button() +{ + return GTK_TOGGLE_BUTTON(gui_builder_widget("favorite_button")); +} + +/* Called to get the hidden toggle button. */ +static inline GtkToggleButton *gui_hide_button() +{ + return GTK_TOGGLE_BUTTON(gui_builder_widget("hide_button")); +} + #endif /* OCARINA_GUI_PLAYLISTS_SYSTEM_H */ diff --git a/share/ocarina/ocarina.ui b/share/ocarina/ocarina.ui index a07c1086..0fd5c721 100644 --- a/share/ocarina/ocarina.ui +++ b/share/ocarina/ocarina.ui @@ -682,14 +682,14 @@ audio-volume-medium 5 expand - + True False False True Add this track to Favorites playlist True - + True @@ -705,13 +705,13 @@ audio-volume-medium - + True False False True Hide this track from the Collection - + True diff --git a/tests/gui/audio.c b/tests/gui/audio.c index 6f16e0ed..11ca7f91 100644 --- a/tests/gui/audio.c +++ b/tests/gui/audio.c @@ -62,8 +62,8 @@ static void test_audio() g_assert_true(gtk_widget_is_visible(gui_builder_widget("o_play"))); g_assert_false(gtk_widget_is_visible(gui_builder_widget("o_pause"))); g_assert_cmpstr_free(test_get_pause_text(), ==, "(disabled)"); - g_assert_false(test_get_toggle_state("o_favorite")); - g_assert_false(test_get_toggle_state("o_hide")); + g_assert_false(test_get_toggle_state("favorite_button")); + g_assert_false(test_get_toggle_state("hide_button")); db_for_each(dbe, next, track_db_get()) { if (TRACK(dbe)->tr_track == 1) { @@ -84,8 +84,8 @@ static void test_audio() g_assert_cmpstr(test_get_label_text("o_artist"), ==, track->tr_album->al_artist->ar_name); g_assert_cmpstr(test_get_label_text("o_position"), ==, "0:00"); g_assert_cmpstr(test_get_label_text("o_duration"), ==, duration); - g_assert_true(test_get_toggle_state("o_favorite")); - g_assert_true(test_get_toggle_state("o_hide")); + g_assert_true(test_get_toggle_state("favorite_button")); + g_assert_true(test_get_toggle_state("hide_button")); g_free(duration); test_click_button("o_play"); @@ -103,8 +103,8 @@ static void test_audio() test_click_button("o_next"); g_assert_cmpuint(audio_cur_track()->tr_track, ==, 3); - g_assert_false(test_get_toggle_state("o_favorite")); - g_assert_false(test_get_toggle_state("o_hide")); + g_assert_false(test_get_toggle_state("favorite_button")); + g_assert_false(test_get_toggle_state("hide_button")); test_set_pause_after(1); g_assert_cmpstr_free(test_get_pause_text(), ==, "next track"); diff --git a/tests/gui/playlists/system.c b/tests/gui/playlists/system.c index ccfab4cf..f559183d 100644 --- a/tests/gui/playlists/system.c +++ b/tests/gui/playlists/system.c @@ -1,9 +1,9 @@ /* * Copyright 2016 (c) Anna Schumaker. */ +#include #include #include -#include #include #include #include @@ -11,8 +11,20 @@ #include #include +static void test_audio_load(struct track *track) + { gui_pl_system_track_loaded(track); } +static void test_change_state(GstState state) { } +static void test_config_pause(int n) { } + +static struct audio_ops test_audio_ops = { + .on_load = test_audio_load, + .on_state_change = test_change_state, + .on_config_pause = test_config_pause, +}; + struct core_init_data init_data = { .playlist_ops = &playlist_ops, + .audio_ops = &test_audio_ops, }; static const gchar *toplevel[3] = { "Queued Tracks", "Collection", "History" }; @@ -49,9 +61,37 @@ static void test_system() g_assert_cmpuint(gui_sidebar_iter_type(&child), ==, PL_SYSTEM); gui_sidebar_iter_next(&child); } +} +static void test_buttons() +{ g_assert_true(gui_pl_library_add("tests/Music/Hyrule Symphony")); while (idle_run_task()) {} + + g_assert_false(gtk_toggle_button_get_active(gui_favorite_button())); + g_assert_false(gtk_toggle_button_get_active(gui_hide_button())); + + playlist_add(PL_SYSTEM, "Favorites", track_get(0)); + audio_load(track_get(0)); + g_assert_true( gtk_toggle_button_get_active(gui_favorite_button())); + g_assert_false(gtk_toggle_button_get_active(gui_hide_button())); + + gtk_toggle_button_set_active(gui_favorite_button(), false); + g_assert_false(playlist_has(PL_SYSTEM, "Favorites", track_get(0))); + gtk_toggle_button_set_active(gui_favorite_button(), true); + g_assert_true(playlist_has(PL_SYSTEM, "Favorites", track_get(0))); + + playlist_add(PL_SYSTEM, "Hidden", track_get(1)); + audio_load(track_get(1)); + g_assert_false(gtk_toggle_button_get_active(gui_favorite_button())); + g_assert_true(gtk_toggle_button_get_active(gui_hide_button())); + + gtk_toggle_button_set_active(gui_hide_button(), false); + g_assert_false(playlist_has(PL_SYSTEM, "Hidden", track_get(1))); + g_assert(audio_cur_track() == track_get(1)); + gtk_toggle_button_set_active(gui_hide_button(), true); + g_assert_true(playlist_has(PL_SYSTEM, "Hidden", track_get(1))); + g_assert(audio_cur_track() != track_get(1)); } int main(int argc, char **argv) @@ -70,6 +110,7 @@ int main(int argc, char **argv) g_test_init(&argc, &argv, NULL); g_test_add_func("/Gui/Playlists/System", test_system); + g_test_add_func("/Gui/Playlists/System/Buttons", test_buttons); ret = g_test_run(); core_deinit();