Debian Codegraph @

src/scores/new_score_dialog.rs

raw symbols SCIP
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::ROWS_TO_DISPLAY;
21 use super::category_bar::category_bar;
22 use super::dialog::{ScoresDialog, new_scores_dialog};
23 use super::grid::{create_scores_grid, scores_grid_update};
24 use super::scores::{Category, Score, ScoreList};
25 use gettextrs::gettext;
26 use gtk::{glib, prelude::*};
27 use std::cell::RefCell;
28 use std::rc::Rc;
29
30 pub async fn new_score_dialog(
31 new_high_score: &Score,
32 current_cat: &Category,
33 score_list: &ScoreList,
34 window: &gtk::Window,
35 ) -> String {
36 let ScoresDialog {
37 dlg,
38 window_title,
39 headerbar,
40 vbox,
41 } = new_scores_dialog(window);
42
43 window_title.set_title(&gettext("Congratulations!"));
44 headerbar.set_show_end_title_buttons(false);
45
46 vbox.set_spacing(20);
47
48 let category_label = gtk::Label::builder()
49 .label(current_cat.name().unwrap())
50 .halign(gtk::Align::Center)
51 .valign(gtk::Align::Center)
52 .build();
53
54 vbox.append(&category_bar(&category_label));
55 vbox.append(
56 &gtk::Separator::builder()
57 .orientation(gtk::Orientation::Horizontal)
58 .build(),
59 );
60
61 let grid = create_scores_grid();
62 vbox.append(&grid);
63
64 let done = gtk::Button::builder()
65 .label(gettext("_Done"))
66 .use_underline(true)
67 .build();
68 done.add_css_class("suggested-action");
69 done.connect_clicked(glib::clone!(
70 #[weak]
71 dlg,
72 move |_| dlg.close()
73 ));
74
75 headerbar.set_show_start_title_buttons(false);
76 headerbar.set_show_end_title_buttons(false);
77 headerbar.pack_end(&done);
78
79 scores_grid_update(&grid, score_list);
80
81 let user = Rc::new(RefCell::new(new_high_score.user.clone()));
82
83 if let Some(position) = score_list
84 .scores
85 .iter()
86 .take(ROWS_TO_DISPLAY)
87 .position(|score| score == new_high_score)
88 .map(|p| p as i32)
89 {
90 let count = score_list.scores.len();
91 if count > 1 && position == 0 {
92 window_title.set_subtitle(&gettext("Your score is the best!"));
93 } else {
94 window_title.set_subtitle(&gettext("Your score has made the top ten."));
95 }
96
97 let entry = gtk::Entry::builder().text(&new_high_score.user).build();
98 entry.connect_text_notify({
99 let user = user.clone();
100 move |e| {
101 *user.borrow_mut() = e.text().into();
102 }
103 });
104 grid_replace(&grid, entry.upcast_ref(), 2, position + 1);
105 }
106
107 let (sender, receiver) = async_channel::unbounded();
108 dlg.connect_close_request(move |_| {
109 sender.send_blocking(()).unwrap();
110 glib::Propagation::Proceed
111 });
112 dlg.present();
113
114 receiver.recv().await.unwrap();
115
116 Rc::unwrap_or_clone(user).take()
117 }
118
119 fn grid_replace(grid: &gtk::Grid, child: &gtk::Widget, column: i32, row: i32) {
120 if let Some(prev_child) = grid.child_at(column, row) {
121 grid.remove(&prev_child);
122 }
123 grid.attach(child, column, row, 1, 1);
124 }