fold equal records into the same local type
[ajla.git] / stdlib / ui / widget / progress.ajla
blob7518d78aaf4272a4714d94ec7d2537892caa7c67
1 {*
2  * Copyright (C) 2024 Mikulas Patocka
3  *
4  * This file is part of Ajla.
5  *
6  * Ajla is free software: you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation, either version 3 of the License, or (at your option) any later
9  * version.
10  *
11  * Ajla is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13  * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * Ajla. If not, see <https://www.gnu.org/licenses/>.
17  *}
19 private unit ui.widget.progress;
21 uses ui.widget.common;
23 type progress_state;
25 fn progress_init(progress_prop : bytes, color_scheme : bytes, w : world, app : appstate, id : wid) : (world, appstate, progress_state);
26 fn progress_get_width(app : appstate, com : widget_common, st : progress_state, x : int) : int;
27 fn progress_get_height(app : appstate, com : widget_common, st : progress_state, x : int) : int;
28 fn progress_redraw(app : appstate, curs : curses, com : widget_common, st : progress_state) : curses;
29 fn progress_process_event(w : world, app : appstate, com : widget_common, st : progress_state, wev : wevent) : (world, appstate, widget_common, progress_state);
31 const progress_class~flat := widget_class.[
32         t : progress_state,
33         name : "progress",
34         is_selectable : false,
35         get_width : progress_get_width,
36         get_height : progress_get_height,
37         redraw : progress_redraw,
38         process_event : progress_process_event,
41 implementation
43 record progress_state [
44         progress_prop : bytes;
45         color_scheme : bytes;
48 fn progress_init(progress_prop : bytes, color_scheme : bytes, implicit w : world, implicit app : appstate, id : wid) : (world, appstate, progress_state)
50         property_observe(id, progress_prop);
51         return progress_state.[
52                 progress_prop : progress_prop,
53                 color_scheme : color_scheme,
54         ];
57 fn progress_get_width(app : appstate, com : widget_common, st : progress_state, x : int) : int
59         return max(x, 7);
62 fn progress_get_height(app : appstate, com : widget_common, st : progress_state, x : int) : int
64         return 1;
67 fn progress_redraw(implicit app : appstate, implicit curs : curses, com : widget_common, st : progress_state) : curses
69         var ratio := property_get(st.progress_prop).r;
70         if ratio.den = 0 then
71                 ratio.den := 1;
72         if ratio < 0 then
73                 ratio := 0;
74         if ratio > 1 then
75                 ratio := 1;
76         var number : int := ratio * 100;
77         var num_str := list_left_pad(ntos(number) + "%", 4, ' ');
78         var space := max(com.size_x - 2 - len(num_str), 1);
79         var space_filled : int := ratio * space;
80         curses_set_pos(0, 0);
81         property_set_attrib(property_get_attrib(st.color_scheme + "progress", #0000, #0000, #0000, #aaaa, #aaaa, #aaaa, 0, curses_invert));
82         curses_print(`[`);
83         property_set_attrib(property_get_attrib(st.color_scheme + "progress-bar", #aaaa, #aaaa, #aaaa, #0000, #0000, #0000, 0, 0));
84         curses_print(list_repeat(` `, space_filled));
85         property_set_attrib(property_get_attrib(st.color_scheme + "progress", #0000, #0000, #0000, #aaaa, #aaaa, #aaaa, 0, curses_invert));
86         curses_print(list_repeat(` `, space - space_filled));
87         curses_print(`]`);
88         curses_print(ascii_to_string(num_str));
91 fn progress_process_event(implicit w : world, implicit app : appstate, implicit com : widget_common, implicit st : progress_state, wev : wevent) : (world, appstate, widget_common, progress_state)
93         if wev is property_changed then [
94                 widget_enqueue_event(com.self, wevent.redraw.(event_redraw.[
95                         x1 : 0,
96                         x2 : com.size_x,
97                         y1 : 0,
98                         y2 : com.size_y,
99                 ]));
100         ]