diff options
| author | Felix Morgner <felix.morgner@gmail.com> | 2025-04-29 16:33:32 +0200 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@gmail.com> | 2025-04-29 16:33:32 +0200 |
| commit | 3ff5bd46952144926d9bd9beedf50023a51913ee (patch) | |
| tree | 2c579fe13bbb5cc90f8f50e7af35218c98e123d5 /ui/src/participant_row.cpp | |
| parent | 873bf396b904ce477a238f22d1891e1b03f24eff (diff) | |
| download | turns-3ff5bd46952144926d9bd9beedf50023a51913ee.tar.xz turns-3ff5bd46952144926d9bd9beedf50023a51913ee.zip | |
ui: flatten namespace hierarchy
Diffstat (limited to 'ui/src/participant_row.cpp')
| -rw-r--r-- | ui/src/participant_row.cpp | 149 |
1 files changed, 149 insertions, 0 deletions
diff --git a/ui/src/participant_row.cpp b/ui/src/participant_row.cpp new file mode 100644 index 0000000..b6d18a8 --- /dev/null +++ b/ui/src/participant_row.cpp @@ -0,0 +1,149 @@ +#include "turns/ui/participant_row.hpp" + +#include "turns/core/disposition.hpp" +#include "turns/core/participant.hpp" +#include "turns/lang/messages.hpp" +#include "turns/ui/template_widget.hpp" + +#include <sigc++/functors/mem_fun.h> + +#include <glibmm/binding.h> +#include <glibmm/i18n.h> +#include <glibmm/objectbase.h> +#include <glibmm/propertyproxy.h> +#include <glibmm/refptr.h> +#include <glibmm/ustring.h> +#include <glibmm/variant.h> + +#include <gtkmm/button.h> +#include <gtkmm/label.h> +#include <gtkmm/listboxrow.h> +#include <gtkmm/togglebutton.h> + +#include <algorithm> +#include <format> +#include <vector> + +namespace turns::ui +{ + namespace + { + auto constexpr static TYPE_NAME = "participant_row"; + auto constexpr static TEMPLATE = "/ch/arknet/Turns/participant_row.ui"; + + auto css_class_for(core::disposition value) -> Glib::ustring + { + switch (value) + { + case core::disposition::friendly: + return "disposition-friendly"; + case core::disposition::hostile: + return "disposition-hostile"; + case core::disposition::secret: + return "disposition-secret"; + default: + return ""; + } + } + } // namespace + + participant_row::participant_row(Glib::RefPtr<core::participant> participant) + : Glib::ObjectBase(TYPE_NAME) + , template_widget<participant_row, Gtk::ListBoxRow>{TEMPLATE} + , m_delete{get_widget<Gtk::Button>("delete")} + , m_edit{get_widget<Gtk::Button>("edit")} + , m_subtitle{get_widget<Gtk::Label>("subtitle")} + , m_title{get_widget<Gtk::Label>("title")} + , m_toggle_defeated{get_widget<Gtk::ToggleButton>("toggle_defeated")} + , m_delete_enabled{*this, "delete-enabled", true} + , m_edit_enabled{*this, "edit-enabled", true} + + { + m_delete->signal_clicked().connect(sigc::mem_fun(*this, &participant_row::handle_delete)); + m_edit->signal_clicked().connect(sigc::mem_fun(*this, &participant_row::handle_edit)); + + Glib::Binding::bind_property(m_subtitle->property_label(), + m_subtitle->property_visible(), + Glib::Binding::Flags::DEFAULT, + sigc::mem_fun(&Glib::ustring::size)); + Glib::Binding::bind_property(m_title->property_label(), + m_title->property_visible(), + Glib::Binding::Flags::INVERT_BOOLEAN, + sigc::mem_fun(&Glib::ustring::size)); + Glib::Binding::bind_property(m_toggle_defeated->property_active(), + m_toggle_defeated->property_icon_name(), + Glib::Binding::Flags::SYNC_CREATE, + [](auto active) { return active ? "face-sick-symbolic" : "face-smile-symbolic"; }); + + // clang-format off + Glib::Binding::bind_property(delete_enabled(), + m_delete->property_sensitive(), + Glib::Binding::Flags::SYNC_CREATE); + Glib::Binding::bind_property(edit_enabled(), + m_edit->property_sensitive(), + Glib::Binding::Flags::SYNC_CREATE); + // clang-format on + + if (participant) + { + Glib::Binding::bind_property(participant->property_name(), m_title->property_label(), Glib::Binding::Flags::SYNC_CREATE); + + Glib::Binding::bind_property(participant->property_priority(), + m_subtitle->property_label(), + Glib::Binding::Flags::SYNC_CREATE, + [](auto n) { return std::vformat(_(lang::priority_number), std::make_format_args(n)); }); + + Glib::Binding::bind_property(participant->property_disposition(), + m_toggle_defeated->property_css_classes(), + Glib::Binding::Flags::SYNC_CREATE, + [this](auto value) { + auto classes = m_toggle_defeated->get_css_classes(); + auto removed = std::ranges::remove_if(classes, [](auto cls) { + return (cls == "disposition-friendly") | (cls == "disposition-hostile") || (cls == "disposition-secret"); + }); + classes.erase(removed.begin(), removed.end()); + classes.push_back(css_class_for(value)); + return classes; + }); + + Glib::Binding::bind_property(participant->property_is_active(), + property_css_classes(), + Glib::Binding::Flags::SYNC_CREATE, + [this](auto value) { + auto classes = get_css_classes(); + if (!value) + { + std::erase(classes, "active-participant"); + } + else + { + classes.push_back("active-participant"); + } + return classes; + }); + } + } + + auto participant_row::delete_enabled() -> Glib::PropertyProxy<bool> + { + return m_delete_enabled.get_proxy(); + } + + auto participant_row::edit_enabled() -> Glib::PropertyProxy<bool> + { + return m_edit_enabled.get_proxy(); + } + + auto participant_row::handle_delete() -> void + { + auto index = Glib::Variant<int>::create(get_index()); + activate_action("win.delete", index); + } + + auto participant_row::handle_edit() -> void + { + auto index = Glib::Variant<int>::create(get_index()); + activate_action("win.edit", index); + } + +} // namespace turns::ui
\ No newline at end of file |
