Skip to content

Commit

Permalink
add language settings file
Browse files Browse the repository at this point in the history
  • Loading branch information
igagis committed Oct 6, 2024
1 parent bf347c6 commit e75a663
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 6 deletions.
3 changes: 1 addition & 2 deletions src/bedsidemon/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,7 @@ utki::shared_ref<ruis::widget> make_root_widget_structure(utki::shared_ref<ruis:
.text_params = {
.font_size = style::font_size_label
}
},
{}
}
)
}
),
Expand Down
49 changes: 48 additions & 1 deletion src/bedsidemon/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,37 @@ namespace {
constexpr const auto settings_filename = "settings.tml"sv;

constexpr const auto sweep_speed_key = "sweep_speed_um_per_sec"sv;
constexpr const auto language_key = "language"sv;
} // namespace

namespace {
size_t language_id_to_index(std::string_view id)
{
const auto& lang_mapping = settings::language_id_to_name_mapping;

auto i = std::find_if(
lang_mapping.begin(), //
lang_mapping.end(),
[&](const auto& a) {
return a.first == id;
}
);

if (i == lang_mapping.end()) {
return 0;
}

return std::distance(lang_mapping.begin(), i);
}

std::string_view language_index_to_id(size_t index)
{
const auto& lang_mapping = settings::language_id_to_name_mapping;

ASSERT(index < lang_mapping.size())

return lang_mapping[index].first;
}
} // namespace

settings_storage::settings_storage() :
Expand All @@ -57,10 +88,14 @@ settings settings_storage::read(std::string_view filename)
auto tml = tml::read(fi);

for (const auto& t : tml) {
if (t.value.to_string() == sweep_speed_key) {
if (t.value.string == sweep_speed_key) {
if (!t.children.empty()) {
ret.sweep_speed_um_per_sec = t.children.front().value.to_uint32();
}
} else if (t.value.string == language_key) {
if (!t.children.empty()) {
ret.cur_language_index = language_id_to_index(t.children.front().value.string);
}
}
}

Expand Down Expand Up @@ -88,8 +123,20 @@ void settings_storage::write()
add_setting(sweep_speed_key, tml::leaf(this->settings_v.sweep_speed_um_per_sec));
}

if (this->settings_v.cur_language_index != 0) {
add_setting(language_key, tml::leaf(language_index_to_id(this->settings_v.cur_language_index)));
}

std::filesystem::create_directories(ruisapp::application::inst().directory.config);

papki::fs_file fi(filename);
tml::write(tml, fi);
}

constexpr decltype(settings::language_id_to_name_mapping) settings::language_id_to_name_mapping{
{
{"en"sv, U"English"sv},
{"fi"sv, U"Suomi"sv},
{"ru"sv, U"Русский"sv},
}
};
3 changes: 3 additions & 0 deletions src/bedsidemon/settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ namespace bedsidemon {
struct settings {
constexpr static const auto default_sweep_speed_um_per_sec = 25000;
uint32_t sweep_speed_um_per_sec = default_sweep_speed_um_per_sec;

static const std::array<std::pair<std::string_view, std::u32string_view>, 3> language_id_to_name_mapping;
size_t cur_language_index = 0;
};

class settings_storage : public utki::singleton<settings_storage>
Expand Down
6 changes: 3 additions & 3 deletions src/bedsidemon/settings_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ constexpr const std::array<uint32_t, 3> sweep_speeds_um_per_sec = {
} // namespace

namespace {
class selection_box_provider : public ruis::selection_box::provider
class sweep_speed_selection_box_provider : public ruis::selection_box::provider
{
public:
selection_box_provider() = default;
sweep_speed_selection_box_provider() = default;

size_t count() const noexcept override
{
Expand Down Expand Up @@ -128,7 +128,7 @@ std::vector<utki::shared_ref<ruis::widget>> make_menu_contents(utki::shared_ref<
.id = "sweep_speed_selection_box"s
},
.selection_params = {
.provider = std::make_shared<selection_box_provider>()
.provider = std::make_shared<sweep_speed_selection_box_provider>()
}
}
)
Expand Down

0 comments on commit e75a663

Please sign in to comment.