diff options
| author | Felix Morgner <felix.morgner@gmail.com> | 2025-04-30 09:21:20 +0200 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@gmail.com> | 2025-04-30 09:21:20 +0200 |
| commit | 8181e59a6e1e76161e50ab545f4f62c2d05c8b80 (patch) | |
| tree | 374c294a6cfe6ba06bdf9a691cb0156e93cd8484 /ui/src | |
| parent | 64c2e6601c62d583f9ce06079bfaa6b052626f6b (diff) | |
| download | turns-8181e59a6e1e76161e50ab545f4f62c2d05c8b80.tar.xz turns-8181e59a6e1e76161e50ab545f4f62c2d05c8b80.zip | |
ui: convert tracker to template_widget
Diffstat (limited to 'ui/src')
| -rw-r--r-- | ui/src/init.cpp | 4 | ||||
| -rw-r--r-- | ui/src/tracker.cpp | 74 | ||||
| -rw-r--r-- | ui/src/tracker.ui | 4 | ||||
| -rw-r--r-- | ui/src/tracker/actions.cpp | 18 | ||||
| -rw-r--r-- | ui/src/tracker/event_handlers.cpp | 12 |
5 files changed, 64 insertions, 48 deletions
diff --git a/ui/src/init.cpp b/ui/src/init.cpp index 28e2a77..eeb808a 100644 --- a/ui/src/init.cpp +++ b/ui/src/init.cpp @@ -3,7 +3,10 @@ #include "turns/ui/participant_editor.hpp" #include "turns/ui/participant_row.hpp" #include "turns/ui/preferences.hpp" +#include "turns/ui/tracker.hpp" #include "turns/ui/turn_order_view.hpp" +#include <glibmm/refptr.h> +#include <adwaitamm/application.hpp> namespace turns::ui { @@ -13,6 +16,7 @@ namespace turns::ui static_cast<void>(ParticipantEditor{{}}); static_cast<void>(ParticipantRow{{}}); static_cast<void>(Preferences{{}}); + static_cast<void>(Tracker{{}, {}}); static_cast<void>(TurnOrderView{{}}); } 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 <sigc++/adaptors/bind.h> @@ -9,6 +10,7 @@ #include <glibmm/binding.h> #include <glibmm/i18n.h> +#include <glibmm/objectbase.h> #include <glibmm/propertyproxy.h> #include <glibmm/refptr.h> #include <glibmm/ustring.h> @@ -31,6 +33,7 @@ #include <gtkmm/styleprovider.h> #include <gtkmm/widget.h> +#include <adwaitamm/application.hpp> #include <adwaitamm/applicationwindow.hpp> #include <adwaitamm/toast.hpp> #include <adwaitamm/toastoverlay.hpp> @@ -47,28 +50,37 @@ namespace turns::ui { - - tracker::tracker(BaseObjectType * base, Glib::RefPtr<Gtk::Builder> const builder, Glib::RefPtr<Gio::Settings> settings) - : ApplicationWindow{base} - , m_controls{builder->get_widget<Gtk::Revealer>("controls")} - , m_empty{builder->get_widget<Gtk::Widget>("empty")} - , m_overlay{builder->get_widget<Adwaita::ToastOverlay>("overlay")} - , m_stack{builder->get_widget<Gtk::Stack>("stack")} - , m_start{builder->get_widget<Gtk::Button>("start")} - , m_title{builder->get_widget<Adwaita::WindowTitle>("title")} + namespace + { + auto constexpr static TYPE_NAME = "Tracker"; + auto constexpr static TEMPLATE = "/ch/arknet/Turns/tracker.ui"; + } // namespace + + Tracker::Tracker(Glib::RefPtr<Adwaita::Application> const & app, Glib::RefPtr<Gio::Settings> const & settings) + : Glib::ObjectBase{TYPE_NAME} + , template_widget{TEMPLATE, app} + , m_controls{get_widget<Gtk::Revealer>("controls")} + , m_empty{get_widget<Gtk::Widget>("empty")} + , m_overlay{get_widget<Adwaita::ToastOverlay>("overlay")} + , m_stack{get_widget<Gtk::Stack>("stack")} + , m_start{get_widget<Gtk::Button>("start")} + , m_title{get_widget<Adwaita::WindowTitle>("title")} , m_turn_order{core::turn_order::create()} , m_turn_order_view{Gtk::make_managed<TurnOrderView>(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<Gio::File> file) -> void + auto Tracker::load(Glib::RefPtr<Gio::File> 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 diff --git a/ui/src/tracker.ui b/ui/src/tracker.ui index b875d10..c4fe324 100644 --- a/ui/src/tracker.ui +++ b/ui/src/tracker.ui @@ -5,7 +5,7 @@ <requires lib="gio" version="2.0"/> <requires lib="gtk" version="4.18"/> <requires lib="libadwaita" version="1.7"/> - <object class="AdwApplicationWindow" id="tracker"> + <template class="gtkmm__CustomObject_Tracker" parent="AdwApplicationWindow"> <property name="content"> <object class="AdwToolbarView"> <property name="content"> @@ -119,7 +119,7 @@ <property name="default-width">360</property> <property name="height-request">480</property> <property name="width-request">360</property> - </object> + </template> <menu id="main_menu"> <item> <attribute name="action">win.clear</attribute> diff --git a/ui/src/tracker/actions.cpp b/ui/src/tracker/actions.cpp index 7367dae..796345a 100644 --- a/ui/src/tracker/actions.cpp +++ b/ui/src/tracker/actions.cpp @@ -46,20 +46,20 @@ namespace turns::ui } } // namespace - auto tracker::add_participant() -> void + auto Tracker::add_participant() -> void { auto dialog = Gtk::make_managed<ParticipantEditor>(nullptr); dialog->signal_finished().connect([this](auto n, auto p, auto d) { m_turn_order->add(n, p, d); }); dialog->present(this); } - auto tracker::delete_participant(Glib::VariantBase param) -> void + auto Tracker::delete_participant(Glib::VariantBase param) -> void { auto index = Glib::VariantBase::cast_dynamic<Glib::Variant<int>>(param); m_turn_order->remove(index.get()); } - auto tracker::edit_participant(Glib::VariantBase param) -> void + auto Tracker::edit_participant(Glib::VariantBase param) -> void { auto index = Glib::VariantBase::cast_dynamic<Glib::Variant<int>>(param); auto participant = m_turn_order->get_typed_object<core::participant>(index.get()); @@ -72,14 +72,14 @@ namespace turns::ui dialog->present(this); } - auto tracker::open() -> void + auto Tracker::open() -> void { auto dialog = Gtk::FileDialog::create(); dialog->set_filters(file_filters()); - dialog->open(sigc::bind(sigc::mem_fun(*this, &tracker::on_open_response), dialog)); + dialog->open(sigc::bind(sigc::mem_fun(*this, &Tracker::on_open_response), dialog)); } - auto tracker::preferences() -> void + auto Tracker::preferences() -> void { auto preferences = Gtk::make_managed<struct Preferences>(m_settings); auto dialog = Gtk::make_managed<Adwaita::PreferencesDialog>(); @@ -88,7 +88,7 @@ namespace turns::ui dialog->present(this); } - auto tracker::save(bool force_ask) -> void + auto Tracker::save(bool force_ask) -> void { if (m_file && !force_ask) { @@ -99,11 +99,11 @@ namespace turns::ui auto dialog = Gtk::FileDialog::create(); m_file ? dialog->set_initial_file(m_file) : dialog->set_initial_name(_(lang::new_turn_order_file_name)); dialog->set_filters(file_filters()); - dialog->save(*this, sigc::bind(sigc::mem_fun(*this, &tracker::on_save_response), dialog)); + dialog->save(*this, sigc::bind(sigc::mem_fun(*this, &Tracker::on_save_response), dialog)); } } - auto tracker::stop() -> void + auto Tracker::stop() -> void { auto dialog = Adwaita::AlertDialog::create(_(lang::stop_turn_order), _(lang::question_clear_turn_order)); dialog->add_response("stop", _(lang::stop)); diff --git a/ui/src/tracker/event_handlers.cpp b/ui/src/tracker/event_handlers.cpp index 80664e4..78eb82b 100644 --- a/ui/src/tracker/event_handlers.cpp +++ b/ui/src/tracker/event_handlers.cpp @@ -22,7 +22,7 @@ namespace turns::ui { - auto tracker::on_load_content_done(Glib::RefPtr<Gio::AsyncResult> result) -> void + auto Tracker::on_load_content_done(Glib::RefPtr<Gio::AsyncResult> result) -> void { set_sensitive(); char * data{}; @@ -49,7 +49,7 @@ namespace turns::ui set_title(std::format("{} - {}", _(lang::turns), name)); } - auto tracker::on_replace_content_done(Glib::RefPtr<Gio::AsyncResult> result) -> void + auto Tracker::on_replace_content_done(Glib::RefPtr<Gio::AsyncResult> result) -> void { set_sensitive(); @@ -67,7 +67,7 @@ namespace turns::ui set_title(std::format("{} - {}", _(lang::turns), name)); } - auto tracker::on_open_response(Glib::RefPtr<Gio::AsyncResult> result, Glib::RefPtr<Gtk::FileDialog> dialog) -> void + auto Tracker::on_open_response(Glib::RefPtr<Gio::AsyncResult> result, Glib::RefPtr<Gtk::FileDialog> dialog) -> void { try { @@ -78,11 +78,11 @@ namespace turns::ui return show_error(e); } - 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); } - auto tracker::on_save_response(Glib::RefPtr<Gio::AsyncResult> result, Glib::RefPtr<Gtk::FileDialog> dialog) -> void + auto Tracker::on_save_response(Glib::RefPtr<Gio::AsyncResult> result, Glib::RefPtr<Gtk::FileDialog> dialog) -> void { try { @@ -97,7 +97,7 @@ namespace turns::ui set_sensitive(false); } - auto tracker::on_settings_changed(Glib::ustring) -> void + auto Tracker::on_settings_changed(Glib::ustring) -> void { update_colors(); } |
