-
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathmain_impl.cpp
More file actions
232 lines (182 loc) · 8.98 KB
/
Copy pathmain_impl.cpp
File metadata and controls
232 lines (182 loc) · 8.98 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Copyright Take Vos 2022.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
#include "hikogui/hikogui.hpp"
#include "hikogui/crt.hpp"
#include <memory>
#include <stacktrace>
//import hikogui;
class my_preferences : public hi::preferences {
public:
hi::observer<std::string> audio_output_device_id;
hi::observer<bool> audio_output_exclusive;
hi::observer<double> audio_output_sample_rate;
hi::observer<hi::speaker_mapping> audio_output_speaker_mapping;
hi::observer<std::string> audio_input_device_id;
hi::observer<bool> audio_input_exclusive;
hi::observer<double> audio_input_sample_rate;
hi::observer<hi::speaker_mapping> audio_input_speaker_mapping;
hi::observer<int> tab_index = 1;
hi::observer<bool> toggle_value;
hi::observer<int> radio_value = 0;
hi::observer<std::vector<std::pair<std::string, hi::label>>> _audio_device_list;
hi::observer<std::string> selected_theme;
my_preferences(std::filesystem::path path) : hi::preferences(std::move(path))
{
add("audio_output_device_id", audio_output_device_id);
add("audio_output_exclusive", audio_output_exclusive);
add("audio_output_sample_rate", audio_output_sample_rate);
add("audio_input_device_id", audio_input_device_id);
add("audio_input_exclusive", audio_input_exclusive);
add("audio_input_sample_rate", audio_input_sample_rate);
add("tab_index", tab_index);
add("toggle_value", toggle_value);
add("radio_value", radio_value);
add("selected_theme", selected_theme);
}
};
hi::scoped_task<> init_audio_tab(hi::grid_widget& grid, my_preferences& preferences) noexcept
{
using namespace hi;
grid.emplace<label_widget>("A1", txt("Input audio device:"), alignment::top_right());
auto& input_config = grid.emplace<audio_device_widget>("B1");
input_config.direction = audio_direction::input;
input_config.device_id = preferences.audio_input_device_id;
grid.emplace<label_widget>("A2", txt("Output audio device:"), alignment::top_right());
auto& output_config = grid.emplace<audio_device_widget>("B2");
output_config.direction = audio_direction::output;
output_config.device_id = preferences.audio_output_device_id;
co_await std::suspend_always{};
}
hi::scoped_task<> init_theme_tab(hi::grid_widget& grid, my_preferences& preferences) noexcept
{
using namespace hi;
hi::observer<std::vector<std::pair<std::string, hi::label>>> theme_list;
{
auto proxy = theme_list.get();
for (auto const& name : theme_names()) {
proxy->emplace_back(name, txt(name));
}
}
grid.emplace<label_widget>("A1", txt("Theme:"), alignment::top_right());
grid.emplace<selection_widget>("B1", preferences.selected_theme, theme_list);
co_await std::suspend_always{};
}
hi::scoped_task<> init_license_tab(hi::grid_widget& grid, my_preferences& preferences) noexcept
{
using namespace hi;
grid.emplace<label_widget>(
"A1",
txt("This is a [he-IL]\xd7\x9c\xd6\xb0\xd7\x9e\xd6\xb7\xd7\xaa\xd6\xb5\xd7\x92[.].\nAnd another sentence. One more:"),
alignment::top_right());
grid.emplace<toggle_with_label_widget>("B1", preferences.toggle_value, txt("true"), txt("false"), txt("other"));
grid.emplace<label_widget>("A2", txt("These is a disabled checkbox:"), alignment::top_right());
auto& checkbox2 = grid.emplace<checkbox_with_label_widget>(
"B2", preferences.radio_value, 2, txt("Checkbox, with a pretty large label."), txt("off"), txt("other"));
//auto& checkbox2 = grid.emplace<checkbox_widget>("B2", preferences.radio_value, 2);
grid.emplace<label_widget>("A3", txt("These are radio buttons:"), alignment::top_right());
grid.emplace<radio_with_label_widget>("B3", preferences.radio_value, 0, txt("Radio 1"));
grid.emplace<radio_with_label_widget>("B4", preferences.radio_value, 1, txt("Radio 2 (on)"), txt("Radio 2 (off)"));
grid.emplace<radio_with_label_widget>("B5", preferences.radio_value, 2, txt("Radio 3"));
auto option_list = std::vector{
std::pair{0, label{txt("first")}},
std::pair{1, label{txt("second")}},
std::pair{2, label{txt("third")}},
std::pair{3, label{txt("four")}},
std::pair{4, label{txt("five")}},
std::pair{5, label{txt("six")}},
std::pair{6, label{txt("seven")}}};
grid.emplace<label_widget>("A6", txt("This is a selection box at the bottom:"), alignment::top_right());
auto& selection3 = grid.emplace<selection_widget>("B6", preferences.radio_value, option_list);
grid.emplace<label_widget>("A7", txt("Sample Rate:"), alignment::top_right());
grid.emplace<text_field_widget>("B7", preferences.audio_output_sample_rate);
auto toggle_value_cbt = preferences.toggle_value.subscribe(
[&](auto...) {
checkbox2.set_mode(*preferences.toggle_value ? widget_mode::enabled : widget_mode::disabled);
selection3.set_mode(*preferences.toggle_value ? widget_mode::enabled : widget_mode::disabled);
},
callback_flags::main);
grid.emplace<label_widget>("A8:B8", txt("This is large number locale formatted: {:L}", 1234.56));
co_await std::suspend_always{};
}
hi::task<> preferences_window(std::stop_token stop_token, my_preferences& preferences)
{
using namespace hi;
auto window_label = label{png::load(URL{"resource:hikogui_demo.png"}), txt("Preferences")};
auto top = std::make_unique<window_widget>(window_label);
top->toolbar().emplace<toolbar_tab_button_widget>(preferences.tab_index, 0, label{elusive_icon::Speaker, txt("Audio")});
top->toolbar().emplace<toolbar_tab_button_widget>(preferences.tab_index, 1, label{elusive_icon::Key, txt("License")});
top->toolbar().emplace<toolbar_tab_button_widget>(preferences.tab_index, 2, label{elusive_icon::Brush, txt("Theme")});
auto& tabs = top->content().emplace<tab_widget>("A1", preferences.tab_index);
auto& audio_tab_grid = tabs.emplace<grid_widget>(0);
auto& license_tab_grid = tabs.emplace<scroll_widget<axis::both>>(1).emplace<grid_widget>();
auto& theme_tab_grid = tabs.emplace<grid_widget>(2);
auto audio_tab = init_audio_tab(audio_tab_grid, preferences);
auto license_tab = init_license_tab(license_tab_grid, preferences);
auto theme_tab = init_theme_tab(theme_tab_grid, preferences);
auto window = gui_window{std::move(top)};
co_await when_any(window.closing, stop_token);
}
inline size_t target = 0;
hi::task<> main_window(my_preferences& preferences)
{
using namespace hi;
auto window_label = label{png::load(URL{"resource:hikogui_demo.png"}), txt("HikoGUI demo")};
auto top = std::make_unique<window_widget>(window_label);
auto preferences_label = label{elusive_icon::Wrench, txt("Preferences")};
auto& preferences_button = top->toolbar().emplace<hi::toolbar_button_widget>(preferences_label);
top->content().emplace_bottom<toggle_with_label_widget>(preferences.toggle_value);
top->content().emplace_bottom<async_widget>([] { hi_log_info("hello world"); }, txt("Hello world AV"));
auto const& vma_dump_button = top->content().emplace_bottom<momentary_button_widget>(txt("vma\ncalculate stats"));
auto const& abort_button = top->content().emplace_bottom<momentary_button_widget>(txt("abort"));
auto const& break_button = top->content().emplace_bottom<momentary_button_widget>(txt("break"));
auto window = gui_window{std::move(top)};
while (true) {
auto const result = co_await when_any(
preferences_button,
vma_dump_button,
abort_button,
break_button,
preferences.toggle_value,
window.closing);
switch (result.index()) {
case 0:
preferences_button.wait_for(preferences_window(preferences_button.get_stop_token(), preferences));
break;
case 1:
gfx_system::global().log_memory_usage();
break;
case 2:
//target = 1 / (result.index() - 3);
hi_assert_abort("my abort");
break;
case 3:
hi_debug_break();
break;
case 4:
hi_log_info("Toggle value {}", std::get<4>(result));
break;
case 5:
co_return;
default:
hi_no_default();
}
}
}
int hi_main(int argc, char *argv[])
{
using namespace hi;
set_application_name("HikoGUI Demo");
set_application_vendor("HikoGUI");
set_application_version({1, 0, 0});
// Start the logger system, so logging is done asynchronously.
log::start_subsystem(global_state_type::log_level_info);
start_render_doc();
auto preferences = my_preferences(get_path(data_dir(), "preferences.json"));
theme_book::global().selected_theme = preferences.selected_theme;
main_window(preferences);
return loop::main().resume();
}
// extern "C" const char *__asan_default_options() {
// return "help=1:log_path=asan.log";
//}