| 1 | /* |
| 2 | * Copyright 2024 Andrey Kutejko <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 3 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 16 | * |
| 17 | * For more details see the file COPYING. |
| 18 | */ |
| 19 | |
| 20 | use super::category_bar::category_bar; |
| 21 | use super::dialog::{ScoresDialog, new_scores_dialog}; |
| 22 | use super::grid::{create_scores_grid, scores_grid_update}; |
| 23 | use super::no_scores::no_scores; |
| 24 | use super::scores::{Category, ScoreList, Scores}; |
| 25 | use gettextrs::gettext; |
| 26 | use gtk::{gio, glib, prelude::*}; |
| 27 | |
| 28 | struct Page { |
| 29 | category: Category, |
| 30 | score_list: ScoreList, |
| 31 | } |
| 32 | |
| 33 | pub fn scores_dialog( |
| 34 | scores: Scores, |
| 35 | current_cat: Option<&Category>, |
| 36 | window: >k::Window, |
| 37 | ) -> gtk::Window { |
| 38 | let ScoresDialog { |
| 39 | dlg, |
| 40 | window_title, |
| 41 | headerbar, |
| 42 | vbox, |
| 43 | } = new_scores_dialog(window); |
| 44 | |
| 45 | window_title.set_title(&gettext("High Scores")); |
| 46 | headerbar.set_show_end_title_buttons(true); |
| 47 | |
| 48 | if scores.scores.is_empty() { |
| 49 | vbox.append(&no_scores()); |
| 50 | return dlg.upcast(); |
| 51 | } |
| 52 | |
| 53 | vbox.set_spacing(20); |
| 54 | |
| 55 | let grid = create_scores_grid(); |
| 56 | |
| 57 | let category_widget: gtk::Widget = if scores.scores.len() == 1 { |
| 58 | let (category, score_list) = scores.scores.into_iter().next().unwrap(); |
| 59 | |
| 60 | scores_grid_update(&grid, &score_list); |
| 61 | |
| 62 | gtk::Label::builder() |
| 63 | .label(category.name().unwrap()) |
| 64 | .halign(gtk::Align::Center) |
| 65 | .valign(gtk::Align::Center) |
| 66 | .build() |
| 67 | .upcast() |
| 68 | } else { |
| 69 | let model = gio::ListStore::new::<glib::BoxedAnyObject>(); |
| 70 | let mut to_select = None; |
| 71 | for (index, (category, score_list)) in scores.scores.into_iter().enumerate() { |
| 72 | if current_cat == Some(&category) { |
| 73 | to_select = Some(index as u32); |
| 74 | } |
| 75 | let page = glib::BoxedAnyObject::new(Page { |
| 76 | category, |
| 77 | score_list, |
| 78 | }); |
| 79 | model.append(&page); |
| 80 | } |
| 81 | |
| 82 | let params: &[>k::Expression] = &[]; |
| 83 | |
| 84 | let combo = gtk::DropDown::builder() |
| 85 | .focus_on_click(false) |
| 86 | .model(&model) |
| 87 | .expression(gtk::ClosureExpression::with_callback::<String, _>( |
| 88 | params, |
| 89 | |values| { |
| 90 | values[0] |
| 91 | .get::<glib::BoxedAnyObject>() |
| 92 | .ok() |
| 93 | .and_then(|item| { |
| 94 | let page = item.borrow::<Page>(); |
| 95 | page.category.name() |
| 96 | }) |
| 97 | .unwrap_or_default() |
| 98 | }, |
| 99 | )) |
| 100 | .build(); |
| 101 | combo.connect_selected_item_notify(glib::clone!( |
| 102 | #[weak] |
| 103 | grid, |
| 104 | move |cb| { |
| 105 | if let Some(selected_category) = |
| 106 | cb.selected_item().and_downcast::<glib::BoxedAnyObject>() |
| 107 | { |
| 108 | scores_grid_update(&grid, &selected_category.borrow::<Page>().score_list); |
| 109 | } |
| 110 | } |
| 111 | )); |
| 112 | combo.set_selected(to_select.unwrap_or_default()); |
| 113 | if let Some(selected_category) = model |
| 114 | .item(to_select.unwrap_or_default()) |
| 115 | .and_downcast::<glib::BoxedAnyObject>() |
| 116 | { |
| 117 | scores_grid_update(&grid, &selected_category.borrow::<Page>().score_list); |
| 118 | } |
| 119 | |
| 120 | combo.upcast() |
| 121 | }; |
| 122 | |
| 123 | vbox.append(&category_bar(&category_widget)); |
| 124 | |
| 125 | vbox.append( |
| 126 | >k::Separator::builder() |
| 127 | .orientation(gtk::Orientation::Horizontal) |
| 128 | .build(), |
| 129 | ); |
| 130 | |
| 131 | vbox.append(&grid); |
| 132 | |
| 133 | dlg.upcast() |
| 134 | } |