From 8181e59a6e1e76161e50ab545f4f62c2d05c8b80 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Wed, 30 Apr 2025 09:21:20 +0200 Subject: ui: convert tracker to template_widget --- ui/src/tracker.cpp | 74 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 43 insertions(+), 31 deletions(-) (limited to 'ui/src/tracker.cpp') diff --git a/ui/src/tracker.cpp b/ui/src/tracker.cpp index 3e9ea43..3f70b16 100644 --- a/ui/src/tracker.cpp +++ b/ui/src/tracker.cpp @@ -2,6 +2,7 @@ #include "turns/core/turn_order.hpp" #include "turns/lang/messages.hpp" +#include "turns/ui/template_widget.hpp" #include "turns/ui/turn_order_view.hpp" #include @@ -9,6 +10,7 @@ #include #include +#include #include #include #include @@ -31,6 +33,7 @@ #include #include +#include #include #include #include @@ -47,28 +50,37 @@ namespace turns::ui { - - tracker::tracker(BaseObjectType * base, Glib::RefPtr const builder, Glib::RefPtr settings) - : ApplicationWindow{base} - , m_controls{builder->get_widget("controls")} - , m_empty{builder->get_widget("empty")} - , m_overlay{builder->get_widget("overlay")} - , m_stack{builder->get_widget("stack")} - , m_start{builder->get_widget("start")} - , m_title{builder->get_widget("title")} + namespace + { + auto constexpr static TYPE_NAME = "Tracker"; + auto constexpr static TEMPLATE = "/ch/arknet/Turns/tracker.ui"; + } // namespace + + Tracker::Tracker(Glib::RefPtr const & app, Glib::RefPtr const & settings) + : Glib::ObjectBase{TYPE_NAME} + , template_widget{TEMPLATE, app} + , m_controls{get_widget("controls")} + , m_empty{get_widget("empty")} + , m_overlay{get_widget("overlay")} + , m_stack{get_widget("stack")} + , m_start{get_widget("start")} + , m_title{get_widget("title")} , m_turn_order{core::turn_order::create()} , m_turn_order_view{Gtk::make_managed(m_turn_order)} , m_settings{std::move(settings)} , m_subtitle{m_title->property_subtitle()} , m_css{Gtk::CssProvider::create()} { + if(!app || !settings) + return; + setup_colors(); setup_actions(); m_stack->add(*m_turn_order_view); - m_turn_order->is_empty().signal_changed().connect(sigc::mem_fun(*this, &tracker::update_subtitle)); - m_turn_order->round_number().signal_changed().connect(sigc::mem_fun(*this, &tracker::update_subtitle)); + m_turn_order->is_empty().signal_changed().connect(sigc::mem_fun(*this, &Tracker::update_subtitle)); + m_turn_order->round_number().signal_changed().connect(sigc::mem_fun(*this, &Tracker::update_subtitle)); update_subtitle(); // clang-format off @@ -85,12 +97,12 @@ namespace turns::ui m_settings->bind("skip-defeated", m_turn_order->skip_defeated()); } - auto tracker::setup_actions() -> void + auto Tracker::setup_actions() -> void { // win.add_participant // depends-on: turn_order:state == stopped { - auto action = add_action("add_participant", sigc::mem_fun(*this, &tracker::add_participant)); + auto action = add_action("add_participant", sigc::mem_fun(*this, &Tracker::add_participant)); Glib::Binding::bind_property(m_turn_order->is_running(), action->property_enabled(), @@ -140,7 +152,7 @@ namespace turns::ui // win.stop // depends-on: turn_order:running == true { - auto action = add_action("stop", sigc::mem_fun(*this, &tracker::stop)); + auto action = add_action("stop", sigc::mem_fun(*this, &Tracker::stop)); Glib::Binding::bind_property(m_turn_order->is_running(), action->property_enabled(), Glib::Binding::Flags::SYNC_CREATE); } @@ -150,16 +162,16 @@ namespace turns::ui // win.open // win.preferences { - add_action_with_parameter("delete", Glib::VARIANT_TYPE_INT32, sigc::mem_fun(*this, &tracker::delete_participant)); - add_action_with_parameter("edit", Glib::VARIANT_TYPE_INT32, sigc::mem_fun(*this, &tracker::edit_participant)); - add_action("open", sigc::mem_fun(*this, &tracker::open)); - add_action("preferences", sigc::mem_fun(*this, &tracker::preferences)); + add_action_with_parameter("delete", Glib::VARIANT_TYPE_INT32, sigc::mem_fun(*this, &Tracker::delete_participant)); + add_action_with_parameter("edit", Glib::VARIANT_TYPE_INT32, sigc::mem_fun(*this, &Tracker::edit_participant)); + add_action("open", sigc::mem_fun(*this, &Tracker::open)); + add_action("preferences", sigc::mem_fun(*this, &Tracker::preferences)); } // win.save // depends-on: turn_order:is_empty == false { - auto action = add_action("save", sigc::bind(sigc::mem_fun(*this, &tracker::save), false)); + auto action = add_action("save", sigc::bind(sigc::mem_fun(*this, &Tracker::save), false)); Glib::Binding::bind_property(m_turn_order->is_empty(), action->property_enabled(), @@ -169,7 +181,7 @@ namespace turns::ui // win.save-as // depends-on: turn_order:is_empty == false { - auto action = add_action("save-as", sigc::bind(sigc::mem_fun(*this, &tracker::save), true)); + auto action = add_action("save-as", sigc::bind(sigc::mem_fun(*this, &Tracker::save), true)); Glib::Binding::bind_property(m_turn_order->is_empty(), action->property_enabled(), @@ -177,31 +189,31 @@ namespace turns::ui } } - auto tracker::setup_colors() -> void + auto Tracker::setup_colors() -> void { Gtk::CssProvider::add_provider_for_display(get_display(), m_css, GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); - m_settings->signal_changed().connect(sigc::mem_fun(*this, &tracker::on_settings_changed)); + m_settings->signal_changed().connect(sigc::mem_fun(*this, &Tracker::on_settings_changed)); update_colors(); } - auto tracker::start_replace_content() -> void + auto Tracker::start_replace_content() -> void { m_file_buffer = m_turn_order->serialize().dump(2); - m_file->replace_contents_async(sigc::mem_fun(*this, &tracker::on_replace_content_done), m_file_buffer, m_file_etag); + m_file->replace_contents_async(sigc::mem_fun(*this, &Tracker::on_replace_content_done), m_file_buffer, m_file_etag); } - auto tracker::show_error(std::exception const & e) -> void + auto Tracker::show_error(std::exception const & e) -> void { auto error = e.what(); show_toast(std::vformat(_(lang::saving_failed_format), std::make_format_args(error))); } - auto tracker::show_toast(std::string const & message) -> void + auto Tracker::show_toast(std::string const & message) -> void { m_overlay->add_toast(*Adwaita::Toast::create(message)); } - auto tracker::update_colors() -> void + auto Tracker::update_colors() -> void { auto friendly_color = m_settings->get_string("disposition-color-friendly"); auto hostile_color = m_settings->get_string("disposition-color-hostile"); @@ -214,7 +226,7 @@ namespace turns::ui secret_color.c_str())); } - auto tracker::update_subtitle() -> void + auto Tracker::update_subtitle() -> void { if (m_turn_order->is_empty()) { @@ -227,14 +239,14 @@ namespace turns::ui } } - auto tracker::load(Glib::RefPtr file) -> void + auto Tracker::load(Glib::RefPtr file) -> void { if (file->query_exists()) { m_file = file; - m_file->load_contents_async(sigc::mem_fun(*this, &tracker::on_load_content_done)); + m_file->load_contents_async(sigc::mem_fun(*this, &Tracker::on_load_content_done)); set_sensitive(false); } } -} // namespace turns::ui::windows +} // namespace turns::ui -- cgit v1.2.3