2 * Copyright (C) 2024 Mikulas Patocka
4 * This file is part of Ajla.
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
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.
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/>.
19 private unit ui.widget.progress;
21 uses ui.widget.common;
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.[
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,
43 record progress_state [
44 progress_prop : 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,
57 fn progress_get_width(app : appstate, com : widget_common, st : progress_state, x : int) : int
62 fn progress_get_height(app : appstate, com : widget_common, st : progress_state, x : int) : int
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;
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;
81 property_set_attrib(property_get_attrib(st.color_scheme + "progress", #0000, #0000, #0000, #aaaa, #aaaa, #aaaa, 0, curses_invert));
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));
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.[