1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "nfd.h"
#include "nuklear.h"
#include "process_url.h"
#include "constants.h"
#include "icon.h"
#include "logger.h"
#include "ui.h"
#include "utils/size_info.h"
#include "utils/time_info.h"
#include "utils/utils.h"
static nk_size pct;
static bool show_app_about;
static nfdchar_t *outPath;
static struct nk_rect about_dialog_bounds = {128, -16, 768, 696};
void load_ui(struct ui_struct *ui) {
static char text[USHRT_MAX], box_buffer[UINT16_MAX], status_string[UCHAR_MAX];
float logGroup_height =
nk_window_get_height(ui->ctx) - 85 - 45 - 50 - 35 - 80;
ui->logger->extend_box =
(logGroup_height - 15) <
((ui->logger->box_lines + 1) * (ui->logger->font_height + 2));
poll_status(ui->stat);
if (ui->stat->total) {
pct = ((nk_size)ui->stat->cur) * (nk_size)100 / ((nk_size)ui->stat->total);
if (ui->stat->type == SIZE_BYTES) {
size_info_t info_cur = num2sizeinfo(ui->stat->cur);
size_info_t info_total = num2sizeinfo(ui->stat->total);
sprintf(status_string, "%.2Lf%s/%.2Lf%s, %hhu%%", info_cur.n,
info_cur.unit_str, info_total.n, info_total.unit_str,
(unsigned char)pct);
} else if (ui->stat->type == TIME_SECS) {
char *cur_ts = malloc(9), *total_ts = malloc(9);
sec2timestamp(ui->stat->cur, (char **)&cur_ts);
sec2timestamp(ui->stat->total, (char **)&total_ts);
sprintf(status_string, "%s/%s, %hhu%%", cur_ts, total_ts,
(unsigned char)pct);
FREE_AND_NULLIFY(cur_ts);
FREE_AND_NULLIFY(total_ts);
}
}
if (ui->stat->is_done) {
// (ui->stat->total) = 0; // To prevent nuklear further updating
// status_string
ui->stat->type = TIME_SECS; // Set for the callback stat
}
nk_menubar_begin(ui->ctx);
nk_layout_row_begin(ui->ctx, NK_STATIC, 35, 1);
nk_layout_row_push(ui->ctx, 100);
if (nk_menu_begin_label(ui->ctx, "MENU", NK_TEXT_CENTERED,
nk_vec2(100, 200))) {
nk_layout_row_dynamic(ui->ctx, 35, 1);
if (nk_menu_item_label(ui->ctx, "About", NK_TEXT_CENTERED))
show_app_about = nk_true;
nk_menu_end(ui->ctx);
}
nk_layout_row_end(ui->ctx);
nk_menubar_end(ui->ctx);
if (show_app_about) {
/* about popup */
if (nk_popup_begin(ui->ctx, NK_POPUP_DYNAMIC, "About",
NK_WINDOW_CLOSABLE | NK_WINDOW_MOVABLE |
NK_WINDOW_DYNAMIC,
about_dialog_bounds)) {
about_dialog_bounds = nk_window_get_bounds(ui->ctx);
/* Pure magic calculated from experiments.
* I'll guess that 6 is the x border size,
* but really have no idea what 85 is.
* What's worse, it's related w/ font size. Jeez.
*/
about_dialog_bounds.x -= 6;
about_dialog_bounds.y -= 45;
// DEBUG_PRINT("w: %f, h: %f\n", nk_window_get_bounds(ui->ctx).x,
// nk_window_get_bounds(ui->ctx).y);
nk_layout_row_dynamic(ui->ctx, 30, 1);
for (int i = 0; icon[i]; i++)
nk_label(ui->ctx, icon[i], NK_TEXT_CENTERED);
SPACER;
nk_label(ui->ctx, "Cross-platform multi-threaded downloader",
NK_TEXT_CENTERED);
nk_label(ui->ctx, "By Mole Shang <[email protected]>", NK_TEXT_CENTERED);
nk_label(ui->ctx, "<https://git.135e2.dev/135e2/hinata.git>",
NK_TEXT_CENTERED);
nk_popup_end(ui->ctx);
} else
show_app_about = nk_false;
}
nk_layout_row_dynamic(ui->ctx, 80, 1);
nk_label(ui->ctx, "Hinata", NK_TEXT_CENTERED);
nk_layout_row_template_begin(ui->ctx, 45);
nk_layout_row_template_push_static(ui->ctx, 5);
nk_layout_row_template_push_dynamic(ui->ctx);
nk_layout_row_template_push_static(ui->ctx, 5);
nk_layout_row_template_push_static(ui->ctx, 100);
nk_layout_row_template_push_static(ui->ctx, 5);
nk_layout_row_template_end(ui->ctx);
SPACER;
nk_edit_string_zero_terminated(ui->ctx, NK_EDIT_FIELD | NK_EDIT_AUTO_SELECT,
text, USHRT_MAX - 1, nk_filter_ascii);
SPACER;
if (nk_button_label(ui->ctx, "Down.")) {
// Clear logger text
clear_log();
if (NFD_PickFolder(&outPath, NULL) == NFD_OKAY && outPath) {
DEBUG_PRINT("nfd gets outPath: %s\n", (char *)outPath);
append_log("Got URL: %s\n", text);
add_url(text, (char *)outPath, NULL, NULL, NULL);
} else {
LOG("NFD", "Please specify a valid file PATH to write to!\n");
}
}
SPACER;
nk_layout_row_dynamic(ui->ctx, 50, 1);
nk_label(ui->ctx, status_string, NK_TEXT_CENTERED);
nk_layout_row_template_begin(ui->ctx, 35);
nk_layout_row_template_push_static(ui->ctx, 5);
nk_layout_row_template_push_dynamic(ui->ctx);
nk_layout_row_template_push_static(ui->ctx, 5);
nk_layout_row_template_end(ui->ctx);
SPACER;
if (nk_progress(ui->ctx, &pct, MAX_VALUE, !NK_MODIFIABLE)) {
// the value of the progress bar changed, the new value is stored in
// currentValue
}
SPACER;
nk_layout_row_dynamic(ui->ctx, logGroup_height, 1);
ui->ctx->style.window.group_padding = nk_vec2(0, 0);
if (nk_group_scrolled_begin(ui->ctx, ui->logger->scrollbar, "LogGroup", 0)) {
nk_layout_row_dynamic(
ui->ctx,
MAX(logGroup_height - 15,
(ui->logger->box_lines + 1) * (ui->logger->font_height + 2)),
1);
nk_edit_buffer(ui->ctx, NK_EDIT_BOX | NK_EDIT_READ_ONLY | NK_EDIT_MULTILINE,
ui->logger->text_edit, nk_filter_default);
nk_group_scrolled_end(ui->ctx);
}
}
|