1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
/*
* SPDX-FileCopyrightText: 2025 Felix Morgner <felix.morgner@gmail.com>
* SPDX-License-Identifier: LGPL-2.1-only
*/
#include "participant_row.hpp"
#include "messages.hpp"
#include "template_widget.hpp"
#include <turnsmm/participant.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::gui
{
namespace
{
auto constexpr static TYPE_NAME = "ParticipantRow";
auto constexpr static TEMPLATE = "/ch/arknet/Turns/participant_row.ui";
auto css_class_for(Participant::Disposition value) -> Glib::ustring
{
using Disposition = Participant::Disposition;
switch (value)
{
case Disposition::Friendly:
return "disposition-friendly";
case Disposition::Hostile:
return "disposition-hostile";
case Disposition::Secret:
return "disposition-secret";
default:
return "";
}
}
} // namespace
ParticipantRow::ParticipantRow(Glib::RefPtr<Participant> participant)
: Glib::ObjectBase(TYPE_NAME)
, template_widget{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, &ParticipantRow::handle_delete));
m_edit->signal_clicked().connect(sigc::mem_fun(*this, &ParticipantRow::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(_(messages::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_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 ParticipantRow::delete_enabled() -> Glib::PropertyProxy<bool>
{
return m_delete_enabled.get_proxy();
}
auto ParticipantRow::edit_enabled() -> Glib::PropertyProxy<bool>
{
return m_edit_enabled.get_proxy();
}
auto ParticipantRow::handle_delete() -> void
{
auto index = Glib::Variant<int>::create(get_index());
activate_action("win.delete", index);
}
auto ParticipantRow::handle_edit() -> void
{
auto index = Glib::Variant<int>::create(get_index());
activate_action("win.edit", index);
}
} // namespace Turns::gui
|