From 828f861d9a0abf57293e3e055aee4a0100626aab Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Fri, 26 Aug 2016 10:00:26 -0400 Subject: [PATCH] gui/sidebar: Handle random button clicks This makes the most sense here, and lets us remove gui/queue.c at some point in the future. Signed-off-by: Anna Schumaker --- gui/queue.c | 2 +- gui/sidebar.c | 22 +++++++++++++++++++++- include/gui/sidebar.h | 6 ++++++ share/ocarina/ocarina.ui | 6 +++--- tests/gui/queue.c | 9 +-------- tests/gui/sidebar.c | 29 ++++++++++++++++++++++++++--- 6 files changed, 58 insertions(+), 16 deletions(-) diff --git a/gui/queue.c b/gui/queue.c index 598dc729..53095303 100644 --- a/gui/queue.c +++ b/gui/queue.c @@ -66,7 +66,7 @@ void gui_queue_free(struct queue *queue) void gui_queue_show(struct gui_queue *queue) { - GtkButton *random = GTK_BUTTON(gui_builder_widget("o_random")); + GtkButton *random = GTK_BUTTON(gui_builder_widget("random_button")); bool has_random = false; gq_queue = queue; diff --git a/gui/sidebar.c b/gui/sidebar.c index 31653117..934efea4 100644 --- a/gui/sidebar.c +++ b/gui/sidebar.c @@ -3,6 +3,7 @@ */ #include #include +#include #include #include @@ -115,6 +116,7 @@ static gboolean __gui_sidebar_can_select(GtkTreeSelection *selection, void __gui_sidebar_selection_changed(GtkTreeSelection *selection, gpointer data) { GtkTreeModel *model = GTK_TREE_MODEL(gui_sidebar_filter()); + bool active = false, sensitive = false; struct playlist *playlist = NULL; enum playlist_type_t type; GtkTreeIter iter, child; @@ -125,10 +127,17 @@ void __gui_sidebar_selection_changed(GtkTreeSelection *selection, gpointer data) name = gui_sidebar_iter_name(&child); type = gui_sidebar_iter_type(&child); - playlist = playlist_get(type, name); + + playlist = playlist_get(type, name); + active = playlist_get_random(type, name); + sensitive = (type != PL_SYSTEM) || + !string_match(name, "History"); + g_free(name); } + gtk_toggle_button_set_active(gui_random_button(), active); + gtk_widget_set_sensitive(GTK_WIDGET(gui_random_button()), sensitive); gui_treeview_set_playlist(playlist); } @@ -137,6 +146,17 @@ void __gui_sidebar_resized(GtkPaned *pane, GParamSpec *pspec, gpointer data) settings_set(SIDEBAR_SETTING, gtk_paned_get_position(pane)); } +void __gui_sidebar_random_toggled(GtkToggleButton *button, gpointer data) +{ + struct playlist *playlist = gui_model_get_playlist(); + bool active = gtk_toggle_button_get_active(button); + + if (playlist) { + playlist_set_random(playlist->pl_type, + playlist->pl_name, active); + } +} + void gui_sidebar_init() { int pos = settings_get(SIDEBAR_SETTING); diff --git a/include/gui/sidebar.h b/include/gui/sidebar.h index 835c9ff6..fc230508 100644 --- a/include/gui/sidebar.h +++ b/include/gui/sidebar.h @@ -75,4 +75,10 @@ static inline GtkTreeView *gui_sidebar_treeview() return GTK_TREE_VIEW(gui_builder_widget("sidebar_treeview")); } +/* Called to get the random button. */ +static inline GtkToggleButton *gui_random_button() +{ + return GTK_TOGGLE_BUTTON(gui_builder_widget("random_button")); +} + #endif /* OCARINA_GUI_SIDEBAR_H */ diff --git a/share/ocarina/ocarina.ui b/share/ocarina/ocarina.ui index 67d87010..1cae4a0b 100644 --- a/share/ocarina/ocarina.ui +++ b/share/ocarina/ocarina.ui @@ -390,14 +390,14 @@ end end - + True True True Randomly pick tracks start center - + True @@ -1105,7 +1105,7 @@ audio-volume-medium - + diff --git a/tests/gui/queue.c b/tests/gui/queue.c index 4890e809..614a1651 100644 --- a/tests/gui/queue.c +++ b/tests/gui/queue.c @@ -47,7 +47,7 @@ static void test_queue() struct queue q; search = GTK_ENTRY(gui_filter_search()); - random = GTK_TOGGLE_BUTTON(gui_builder_widget("o_random")); + random = GTK_TOGGLE_BUTTON(gui_builder_widget("random_button")); /* Test initialization */ queue_init(&q, Q_ENABLED, &test_ops, NULL); @@ -65,13 +65,6 @@ static void test_queue() g_assert_false(gtk_toggle_button_get_active(random)); g_assert_cmpstr(gtk_entry_get_text(search), ==, ""); - /* Test clicking random button. */ - gtk_toggle_button_set_active(random, true); - g_assert_true(queue_has_flag(&q, Q_RANDOM)); - - gtk_toggle_button_set_active(random, false); - g_assert_false(queue_has_flag(&q, Q_RANDOM)); - /* Show a queue where random and repeat are enabled */ gq->gq_flags = GQ_CAN_RANDOM; q.q_flags = Q_RANDOM | Q_REPEAT | Q_ENABLED; diff --git a/tests/gui/sidebar.c b/tests/gui/sidebar.c index ff516915..f1a9d6f1 100644 --- a/tests/gui/sidebar.c +++ b/tests/gui/sidebar.c @@ -35,6 +35,9 @@ static void test_sidebar() g_assert_nonnull(gui_sidebar_treeview()); g_assert_true(GTK_IS_TREE_VIEW(gui_sidebar_treeview())); + g_assert_nonnull(gui_random_button()); + g_assert_true(GTK_IS_TOGGLE_BUTTON(gui_random_button())); + g_assert_true(gui_sidebar_iter_first(&iter)); gui_sidebar_iter_add(&iter, playlist_get(PL_SYSTEM, "Collection"), NULL); gui_sidebar_iter_add(&iter, playlist_get(PL_SYSTEM, "History"), NULL); @@ -98,6 +101,7 @@ static void test_sidebar() static void test_sidebar_selection() { GtkTreeSelection *selection; + GtkToggleButton *random; GtkTreeModel *filter; GtkTreePath *path; GtkTreeIter iter; @@ -105,6 +109,7 @@ static void test_sidebar_selection() selection = gtk_tree_view_get_selection(gui_sidebar_treeview()); filter = GTK_TREE_MODEL(gui_sidebar_filter()); + random = gui_random_button(); g_assert_cmpuint(gtk_tree_model_iter_n_children(filter, NULL), ==, 6); playlist_new(PL_LIBRARY, "tests/Music/Hyrule Symphony"); @@ -112,6 +117,7 @@ static void test_sidebar_selection() playlist_add(PL_SYSTEM, "History", track_get(0)); gtk_tree_model_filter_refilter(gui_sidebar_filter()); g_assert_cmpuint(gtk_tree_model_iter_n_children(filter, NULL), ==, 8); + playlist_set_random(PL_SYSTEM, "Collection", true); g_assert_true(gui_sidebar_iter_first(&iter)); path = gtk_tree_model_get_path(gui_sidebar_model(), &iter); @@ -119,14 +125,31 @@ static void test_sidebar_selection() gtk_tree_selection_select_path(selection, path); n = gtk_tree_selection_count_selected_rows(selection); g_assert_cmpuint(n, ==, (i < 2) ? 1 : 0); - if (i == 0) + if (i == 0) { g_assert(gui_model_get_playlist() == playlist_get(PL_SYSTEM, "Collection")); - else if (i == 1) + g_assert_true(gtk_toggle_button_get_active(random)); + g_assert_true(gtk_widget_get_sensitive( + GTK_WIDGET(random))); + + gtk_toggle_button_set_active(random, false); + g_assert_false( + playlist_get_random(PL_SYSTEM, "Collection")); + gtk_toggle_button_set_active(random, true); + g_assert_true( + playlist_get_random(PL_SYSTEM, "Collection")); + } else if (i == 1) { g_assert(gui_model_get_playlist() == playlist_get(PL_SYSTEM, "History")); - else + g_assert_false(gtk_toggle_button_get_active(random)); + g_assert_false(gtk_widget_get_sensitive( + GTK_WIDGET(random))); + } else { g_assert_null(gui_model_get_playlist()); + g_assert_false(gtk_toggle_button_get_active(random)); + g_assert_false(gtk_widget_get_sensitive( + GTK_WIDGET(random))); + } gtk_tree_selection_unselect_all(selection); gtk_tree_path_next(path); }