Skip to content

Commit

Permalink
app2 language switching
Browse files Browse the repository at this point in the history
  • Loading branch information
igagis committed Oct 4, 2024
1 parent 6ee8133 commit 3ef3399
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 14 deletions.
16 changes: 15 additions & 1 deletion src/ruis/widget/base/text_string_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,18 @@ void text_string_widget::on_text_change()
this->text_widget::on_text_change();
}

void text_string_widget::set_text(std::u32string text)
void text_string_widget::set_text(text_type text)
{
this->text_string = std::move(text);
this->invalidate_layout();
this->on_text_change();
}

void text_string_widget::set_text(std::u32string text)
{
this->set_text(text_type(text));
}

const std::u32string& text_string_widget::get_text_string() const noexcept
{
if (std::holds_alternative<std::u32string>(this->text_string)) {
Expand All @@ -102,3 +107,12 @@ std::u32string text_string_widget::get_text() const
{
return this->get_text_string();
}

void text_string_widget::on_reload()
{
if (std::holds_alternative<wording>(this->text_string)) {
const auto& w = std::get<wording>(this->text_string);
auto new_wording = this->context.get().localization.get(w.id());
this->set_text(text_type(new_wording));
}
}
4 changes: 4 additions & 0 deletions src/ruis/widget/base/text_string_widget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class text_string_widget : public text_widget

void recompute_bounding_box();

void set_text(text_type text);

public:
using text_widget::set_text;

Expand All @@ -73,6 +75,8 @@ class text_string_widget : public text_widget
}

void on_text_change() override;

void on_reload() override;
};

} // namespace ruis
6 changes: 3 additions & 3 deletions src/ruis/widget/button/selection_box.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class selection_box : virtual public widget
private:
std::shared_ptr<provider> item_provider;

std::size_t selected_index = 0;
size_t selected_index = 0;

public:
void set_provider(std::shared_ptr<provider> item_provider = nullptr);
Expand Down Expand Up @@ -137,12 +137,12 @@ class selection_box : virtual public widget
* @brief Get index of the selected item.
* @return Index of the selected item.
*/
std::size_t get_selection() const noexcept
size_t get_selection() const noexcept
{
return this->selected_index;
}

std::function<void(selection_box& dds)> selection_handler;
std::function<void(selection_box& sb)> selection_handler;

private:
void handle_data_set_changed();
Expand Down
43 changes: 33 additions & 10 deletions tests/app2/src/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <ruis/widget/proxy/resize_proxy.hpp>
#include <ruis/widget/slider/scroll_bar.hpp>
#include <ruis/widget/slider/slider.hpp>
#include <ruisapp/application.hpp>

using namespace std::string_literals;
using namespace std::string_view_literals;
Expand Down Expand Up @@ -245,6 +246,37 @@ utki::shared_ref<ruis::window> make_selection_box_window(
ruis::rect rect
)
{
// clang-format off
auto sel_box = m::selection_box(c,
{
.layout_params{
.dims = {ruis::dim::max, ruis::dim::min}
},
.selection_params{
.provider = std::make_shared<language_selection_provider>()
}
}
);
// clang-format on

sel_box.get().selection_handler = [](ruis::selection_box& sb) {
auto sel = sb.get_selection();

// std::cout << "localization selection changed = " << sel << std::endl;

ASSERT(sel < language_id_to_name_mapping.size())

sb.context.get().post_to_ui_thread([lng = language_id_to_name_mapping.at(sel).first]() {
auto& app = ruisapp::inst();

// std::cout << "new localization = " << lng << std::endl;

app.gui.context.get().localization =
ruis::localization(tml::read(*app.get_res_file(utki::cat("res/localization/", lng, ".tml"))));
app.gui.get_root().reload();
});
};

// clang-format off
return m::window(c,
{
Expand Down Expand Up @@ -278,16 +310,7 @@ utki::shared_ref<ruis::window> make_selection_box_window(
},
c.get().localization.get("language"sv)
),
m::selection_box(c,
{
.layout_params{
.dims = {ruis::dim::max, ruis::dim::min}
},
.selection_params{
.provider = std::make_shared<language_selection_provider>()
}
}
)
std::move(sel_box)
}
);
// clang-format on
Expand Down

0 comments on commit 3ef3399

Please sign in to comment.