From 72ab1fb4c1bf363f46470816d8b914a78ac493c4 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Sat, 24 May 2025 11:58:39 +0200 Subject: gui: restructure file layout and apply licenses --- gui/src/main.cpp | 5 + gui/src/participant_editor.cpp | 167 ++++++++++++++++++++++++ gui/src/participant_row.cpp | 154 ++++++++++++++++++++++ gui/src/preferences.cpp | 86 ++++++++++++ gui/src/settings.cpp | 33 +++++ gui/src/tracker.cpp | 260 +++++++++++++++++++++++++++++++++++++ gui/src/tracker_actions.cpp | 130 +++++++++++++++++++ gui/src/tracker_event_handlers.cpp | 110 ++++++++++++++++ gui/src/turn_order_view.cpp | 69 ++++++++++ 9 files changed, 1014 insertions(+) create mode 100644 gui/src/participant_editor.cpp create mode 100644 gui/src/participant_row.cpp create mode 100644 gui/src/preferences.cpp create mode 100644 gui/src/settings.cpp create mode 100644 gui/src/tracker.cpp create mode 100644 gui/src/tracker_actions.cpp create mode 100644 gui/src/tracker_event_handlers.cpp create mode 100644 gui/src/turn_order_view.cpp (limited to 'gui/src') diff --git a/gui/src/main.cpp b/gui/src/main.cpp index 030aa0f..a1f9843 100644 --- a/gui/src/main.cpp +++ b/gui/src/main.cpp @@ -1,3 +1,8 @@ +/* + * SPDX-FileCopyrightText: 2025 Felix Morgner + * SPDX-License-Identifier: LGPL-2.1-only + */ + // #include "turns/core/init.hpp" // #include "turns/core/settings.hpp" // #include "turns/ui/init.hpp" diff --git a/gui/src/participant_editor.cpp b/gui/src/participant_editor.cpp new file mode 100644 index 0000000..998d566 --- /dev/null +++ b/gui/src/participant_editor.cpp @@ -0,0 +1,167 @@ +/* + * SPDX-FileCopyrightText: 2025 Felix Morgner + * SPDX-License-Identifier: LGPL-2.1-only + */ + +#include "turns/ui/participant_editor.hpp" + +#include "turns/core/disposition.hpp" +#include "turns/core/participant.hpp" +#include "turns/lang/messages.hpp" +#include "turns/ui/template_widget.hpp" + +#include + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include + +namespace turns::ui +{ + namespace + { + auto constexpr static TYPE_NAME = "ParticipantEditor"; + auto constexpr static TEMPLATE = "/ch/arknet/Turns/participant_editor.ui"; + } // namespace + + ParticipantEditor::ParticipantEditor(Glib::RefPtr participant) + : Glib::ObjectBase{TYPE_NAME} + , template_widget{TEMPLATE} + , m_disposition{get_widget("disposition")} + , m_finish{get_widget("finish")} + , m_name{get_widget("name")} + , m_priority{get_widget("priority")} + , m_disposition_factory{Gtk::SignalListItemFactory::create()} + , m_disposition_model{Gtk::StringList::create()} + , m_participant{*this, "participant", nullptr} + { + m_finish->signal_clicked().connect(sigc::mem_fun(*this, &ParticipantEditor::handle_finish_clicked)); + + for (auto n : std::views::iota(std::uint8_t{}, static_cast(core::Disposition::END))) + { + m_disposition_model->append(presentation_name_for(core::Disposition{n})); + } + + m_disposition_factory->signal_bind().connect(sigc::mem_fun(*this, &ParticipantEditor::handle_item_bind)); + m_disposition_factory->signal_setup().connect(sigc::mem_fun(*this, &ParticipantEditor::handle_item_setup)); + + m_disposition->set_factory(m_disposition_factory); + m_disposition->set_model(m_disposition_model); + + property_participant().signal_changed().connect(sigc::mem_fun(*this, &ParticipantEditor::handle_participant_changed)); + + set_participant(participant); + } + + auto ParticipantEditor::get_disposition() const -> core::Disposition + { + return static_cast(m_disposition->get_selected()); + } + + auto ParticipantEditor::get_name() const -> Glib::ustring + { + return m_name->get_text(); + } + + auto ParticipantEditor::get_participant() const -> Glib::RefPtr + { + return m_participant.get_value(); + } + + auto ParticipantEditor::get_priority() const -> double + { + return m_priority->get_value(); + } + + auto ParticipantEditor::set_disposition(core::Disposition value) -> void + { + m_disposition->set_selected(static_cast(value)); + } + + auto ParticipantEditor::set_name(Glib::ustring const & value) -> void + { + m_name->set_text(value); + } + + auto ParticipantEditor::set_participant(Glib::RefPtr const & value) -> void + { + m_participant.set_value(value); + } + + auto ParticipantEditor::set_priority(double value) -> void + { + m_priority->set_value(value); + } + + auto ParticipantEditor::property_participant() -> Glib::PropertyProxy> + { + return m_participant.get_proxy(); + } + + auto ParticipantEditor::property_participant() const -> Glib::PropertyProxy_ReadOnly> + { + return m_participant.get_proxy(); + } + + auto ParticipantEditor::signal_finished() -> SignalFinishedType + { + return m_signal_finished; + } + + auto ParticipantEditor::handle_finish_clicked() -> void + { + m_signal_finished.emit(m_name->get_text(), m_priority->get_value(), static_cast(m_disposition->get_selected())); + close(); + } + + auto ParticipantEditor::handle_item_bind(Glib::RefPtr item) -> void + { + auto value = std::dynamic_pointer_cast(item->get_item())->get_string(); + dynamic_cast(item->get_child())->set_label(value); + } + + auto ParticipantEditor::handle_item_setup(Glib::RefPtr item) -> void + { + item->set_child(*Gtk::make_managed()); + } + + auto ParticipantEditor::handle_participant_changed() -> void + { + auto value = m_participant.get_value(); + set_title(_(value ? lang::edit_participant : lang::add_participant)); + if (value) + { + Glib::Binding::bind_property(value->property_name(), + m_name->property_text(), + Glib::Binding::Flags::BIDIRECTIONAL | Glib::Binding::Flags::SYNC_CREATE); + Glib::Binding::bind_property(value->property_priority(), + m_priority->property_value(), + Glib::Binding::Flags::BIDIRECTIONAL | Glib::Binding::Flags::SYNC_CREATE); + Glib::Binding::bind_property(value->property_disposition(), + m_disposition->property_selected(), + Glib::Binding::Flags::BIDIRECTIONAL | Glib::Binding::Flags::SYNC_CREATE, + [](auto value) { return static_cast(value); }, + [](auto value) { return static_cast(value); }); + } + } + +} // namespace turns::ui \ No newline at end of file diff --git a/gui/src/participant_row.cpp b/gui/src/participant_row.cpp new file mode 100644 index 0000000..c0f53df --- /dev/null +++ b/gui/src/participant_row.cpp @@ -0,0 +1,154 @@ +/* + * SPDX-FileCopyrightText: 2025 Felix Morgner + * SPDX-License-Identifier: LGPL-2.1-only + */ + +#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 + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include + +namespace turns::ui +{ + namespace + { + auto constexpr static TYPE_NAME = "ParticipantRow"; + 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 + + ParticipantRow::ParticipantRow(Glib::RefPtr participant) + : Glib::ObjectBase(TYPE_NAME) + , template_widget{TEMPLATE} + , m_delete{get_widget("delete")} + , m_edit{get_widget("edit")} + , m_subtitle{get_widget("subtitle")} + , m_title{get_widget("title")} + , m_toggle_defeated{get_widget("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(_(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 ParticipantRow::delete_enabled() -> Glib::PropertyProxy + { + return m_delete_enabled.get_proxy(); + } + + auto ParticipantRow::edit_enabled() -> Glib::PropertyProxy + { + return m_edit_enabled.get_proxy(); + } + + auto ParticipantRow::handle_delete() -> void + { + auto index = Glib::Variant::create(get_index()); + activate_action("win.delete", index); + } + + auto ParticipantRow::handle_edit() -> void + { + auto index = Glib::Variant::create(get_index()); + activate_action("win.edit", index); + } + +} // namespace turns::ui \ No newline at end of file diff --git a/gui/src/preferences.cpp b/gui/src/preferences.cpp new file mode 100644 index 0000000..cd6cdbf --- /dev/null +++ b/gui/src/preferences.cpp @@ -0,0 +1,86 @@ +/* + * SPDX-FileCopyrightText: 2025 Felix Morgner + * SPDX-License-Identifier: LGPL-2.1-only + */ + +#include "turns/ui/preferences.hpp" + +#include "turns/core/settings.hpp" +#include "turns/ui/template_widget.hpp" + +#include +#include + +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include + +#include + +#include + +namespace turns::ui +{ + namespace + { + auto constexpr static TYPE_NAME = "Preferences"; + auto constexpr static TEMPLATE = "/ch/arknet/Turns/preferences.ui"; + } // namespace + + Preferences::Preferences(Glib::RefPtr settings) + : Glib::ObjectBase{TYPE_NAME} + , template_widget{TEMPLATE} + , m_settings{settings} + , m_friendly_reset_button{get_widget("friendly_reset_button")} + , m_hostile_reset_button{get_widget("hostile_reset_button")} + , m_secret_reset_button{get_widget("secret_reset_button")} + , m_friendly_color_button{get_widget("friendly_color_button")} + , m_hostile_color_button{get_widget("hostile_color_button")} + , m_secret_color_button{get_widget("secret_color_button")} + , m_skip_defeated{get_widget("skip_defeated")} + { + if (!m_settings) + { + return; + } + + bind_reset(core::settings::key::disposition_friendly_color, m_friendly_reset_button); + bind_setting(core::settings::key::disposition_friendly_color, m_friendly_color_button); + bind_reset(core::settings::key::disposition_hostile_color, m_hostile_reset_button); + bind_setting(core::settings::key::disposition_hostile_color, m_hostile_color_button); + bind_reset(core::settings::key::disposition_secret_color, m_secret_reset_button); + bind_setting(core::settings::key::disposition_secret_color, m_secret_color_button); + + m_settings->bind(core::settings::key::skip_defeated, m_skip_defeated->property_active()); + } + + auto Preferences::bind_reset(Glib::ustring const & key, Gtk::Button * button) -> void + { + m_settings->signal_changed(key).connect([=, this](auto) { update_sensitive(key, button); }); + update_sensitive(key, button); + button->signal_clicked().connect(sigc::bind(sigc::mem_fun(*m_settings, &Gio::Settings::reset), key)); + } + + auto Preferences::bind_setting(Glib::ustring const & key, Gtk::ColorDialogButton * button) -> void + { + m_settings->bind(key, button->property_rgba(), Gio::Settings::BindFlags::DEFAULT, [](auto value) { + return Gdk::RGBA{value}; + }, [](auto color) { return color.to_string(); }); + } + + auto Preferences::update_sensitive(Glib::ustring const & key, Gtk::Button * button) -> void + { + auto v = Glib::Variant{}; + button->set_sensitive(m_settings->get_user_value(key, v)); + } + +} // namespace turns::ui::widgets \ No newline at end of file diff --git a/gui/src/settings.cpp b/gui/src/settings.cpp new file mode 100644 index 0000000..161a709 --- /dev/null +++ b/gui/src/settings.cpp @@ -0,0 +1,33 @@ +/* + * SPDX-FileCopyrightText: 2025 Felix Morgner + * SPDX-License-Identifier: LGPL-2.1-only + */ + +#include "turns/core/settings.hpp" + +#include +#include + +#include +#include + +#include + +namespace turns::core +{ + + auto get_settings() -> Glib::RefPtr + { + auto constexpr schema_id = "ch.arknet.Turns"; + +#ifdef TURNS_SETTINGS_SCHEMA_DIR + auto source = Gio::SettingsSchemaSource::create(TURNS_SETTINGS_SCHEMA_DIR "/glib-2.0/schemas", true); + auto schema = source->lookup(schema_id, true); + auto settings = g_settings_new_full(Glib::unwrap(schema), nullptr, nullptr); + return Glib::wrap(settings); +#else + return Gio::Settings::create(schema_id); +#endif + } + +} // namespace turns::core diff --git a/gui/src/tracker.cpp b/gui/src/tracker.cpp new file mode 100644 index 0000000..dd926b5 --- /dev/null +++ b/gui/src/tracker.cpp @@ -0,0 +1,260 @@ +/* + * SPDX-FileCopyrightText: 2025 Felix Morgner + * SPDX-License-Identifier: LGPL-2.1-only + */ + +#include "turns/ui/tracker.hpp" + +#include "turns/core/settings.hpp" +#include "turns/core/turn_order_model.hpp" +#include "turns/lang/messages.hpp" +#include "turns/ui/template_widget.hpp" +#include "turns/ui/turn_order_view.hpp" + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include +#include + +#include +#include +#include +#include +#include + +namespace turns::ui +{ + namespace + { + auto constexpr static TYPE_NAME = "Tracker"; + auto constexpr static TEMPLATE = "/ch/arknet/Turns/tracker.ui"; + } // namespace + + Tracker::Tracker() + : Tracker{{}, core::get_settings()} + { + } + + 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::TurnOderModel::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()} + { + 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)); + update_subtitle(); + + // clang-format off + Glib::Binding::bind_property(m_turn_order->is_empty(), + m_stack->property_visible_child(), + Glib::Binding::Flags::SYNC_CREATE, + [this](auto empty) { return empty ? m_empty : m_turn_order_view; }); + + Glib::Binding::bind_property(m_turn_order->is_running(), + m_controls->property_reveal_child(), + Glib::Binding::Flags::SYNC_CREATE); + // clang-format on + + m_settings->bind("skip-defeated", m_turn_order->skip_defeated()); + } + + 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)); + + Glib::Binding::bind_property(m_turn_order->is_running(), + action->property_enabled(), + Glib::Binding::Flags::SYNC_CREATE | Glib::Binding::Flags::INVERT_BOOLEAN); + } + + // win.clear + // depends-on: turn_order:is_empty == false + { + auto action = add_action("clear", sigc::mem_fun(*m_turn_order, &core::TurnOderModel::clear)); + + Glib::Binding::bind_property(m_turn_order->is_empty(), + action->property_enabled(), + Glib::Binding::Flags::SYNC_CREATE | Glib::Binding::Flags::INVERT_BOOLEAN); + } + + // win.next + // depends-on: turn_order:state == running + { + auto action = add_action("next", sigc::mem_fun(*m_turn_order, &core::TurnOderModel::next)); + + Glib::Binding::bind_property(m_turn_order->is_running(), action->property_enabled(), Glib::Binding::Flags::SYNC_CREATE); + } + + // win.previous + // depends-on: turn_order:has_previous == true + { + auto action = add_action("previous", sigc::mem_fun(*m_turn_order, &core::TurnOderModel::previous)); + + Glib::Binding::bind_property(m_turn_order->has_previous(), action->property_enabled(), Glib::Binding::Flags::SYNC_CREATE); + } + + // win.start + // depends-on: turn_order:is_empty == false + { + auto action = add_action("start", sigc::mem_fun(*m_turn_order, &core::TurnOderModel::start)); + + Glib::Binding::bind_property(m_turn_order->is_empty(), + action->property_enabled(), + Glib::Binding::Flags::SYNC_CREATE | Glib::Binding::Flags::INVERT_BOOLEAN); + + Glib::Binding::bind_property(m_turn_order->is_running(), + m_start->property_visible(), + Glib::Binding::Flags::SYNC_CREATE | Glib::Binding::Flags::INVERT_BOOLEAN); + } + + // win.stop + // depends-on: turn_order:running == true + { + 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); + } + + // win.delete + // win.edit + // 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)); + } + + // win.save + // depends-on: turn_order:is_empty == 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(), + Glib::Binding::Flags::SYNC_CREATE | Glib::Binding::Flags::INVERT_BOOLEAN); + } + + // 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)); + + Glib::Binding::bind_property(m_turn_order->is_empty(), + action->property_enabled(), + Glib::Binding::Flags::SYNC_CREATE | Glib::Binding::Flags::INVERT_BOOLEAN); + } + } + + 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)); + update_colors(); + } + + 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); + } + + 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 + { + m_overlay->add_toast(*Adwaita::Toast::create(message)); + } + + 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"); + auto secret_color = m_settings->get_string("disposition-color-secret"); + m_css->load_from_string(std::format("@define-color friendly {};\n" + "@define-color hostile {};\n" + "@define-color secret {};\n", + friendly_color.c_str(), + hostile_color.c_str(), + secret_color.c_str())); + } + + auto Tracker::update_subtitle() -> void + { + if (m_turn_order->is_empty()) + { + m_subtitle = _(lang::no_active_turn_order); + } + else + { + auto round_number = m_turn_order->round_number() + 1; + m_subtitle = round_number == 0 ? "" : std::vformat(_(lang::round_number), std::make_format_args(round_number)); + } + } + + 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)); + set_sensitive(false); + } + } + +} // namespace turns::ui diff --git a/gui/src/tracker_actions.cpp b/gui/src/tracker_actions.cpp new file mode 100644 index 0000000..7149f0e --- /dev/null +++ b/gui/src/tracker_actions.cpp @@ -0,0 +1,130 @@ +/* + * SPDX-FileCopyrightText: 2025 Felix Morgner + * SPDX-License-Identifier: LGPL-2.1-only + */ + +#include "turns/core/participant.hpp" +#include "turns/lang/messages.hpp" +#include "turns/ui/participant_editor.hpp" +#include "turns/ui/preferences.hpp" +#include "turns/ui/tracker.hpp" + +#include +#include + +#include +#include +#include +#include +#include + +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include + +#include + +namespace turns::ui +{ + namespace + { + auto file_filters() + { + auto filters = Gio::ListStore::create(); + auto filter = Gtk::FileFilter::create(); + filter->set_name(_("Turns Files")); + filter->add_pattern("*.trns"); + filters->append(filter); + return filters; + } + } // namespace + + auto Tracker::add_participant() -> void + { + auto dialog = Gtk::make_managed(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 index = Glib::VariantBase::cast_dynamic>(param); + m_turn_order->remove(index.get()); + } + + auto Tracker::edit_participant(Glib::VariantBase param) -> void + { + auto index = Glib::VariantBase::cast_dynamic>(param); + auto participant = m_turn_order->get_typed_object(index.get()); + auto dialog = Gtk::make_managed(participant); + dialog->present(this); + } + + 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)); + } + + auto Tracker::preferences() -> void + { + auto preferences = Gtk::make_managed(m_settings); + auto dialog = Gtk::make_managed(); + dialog->add(*preferences); + dialog->set_visible_page(*preferences); + dialog->present(this); + } + + auto Tracker::save(bool force_ask) -> void + { + if (m_file && !force_ask) + { + start_replace_content(); + } + else + { + 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)); + } + } + + auto Tracker::stop() -> void + { + auto dialog = Adwaita::AlertDialog::create(_(lang::stop_turn_order), _(lang::question_clear_turn_order)); + dialog->add_response("stop", _(lang::stop)); + dialog->set_response_appearance("stop", Adwaita::ResponseAppearance::Suggested); + dialog->add_response("clear", _(lang::stop_and_clear)); + dialog->set_response_appearance("clear", Adwaita::ResponseAppearance::Destructive); + dialog->add_response("cancel", _(lang::cancel)); + dialog->set_response_appearance("cancel", Adwaita::ResponseAppearance::Default); + dialog->set_close_response("cancel"); + dialog->set_default_response("cancel"); + dialog->choose(*this, nullptr, [dialog = std::move(dialog), this](auto const & result) { + auto response = dialog->choose_finish(result); + if (response == "cancel") + { + return; + } + if (response == "clear") + { + m_turn_order->clear(); + } + m_turn_order->stop(); + }); + } +} // namespace turns::ui \ No newline at end of file diff --git a/gui/src/tracker_event_handlers.cpp b/gui/src/tracker_event_handlers.cpp new file mode 100644 index 0000000..ae0328c --- /dev/null +++ b/gui/src/tracker_event_handlers.cpp @@ -0,0 +1,110 @@ +/* + * SPDX-FileCopyrightText: 2025 Felix Morgner + * SPDX-License-Identifier: LGPL-2.1-only + */ + +#include "turns/lang/messages.hpp" +#include "turns/ui/tracker.hpp" + +#include + +#include +#include +#include + +#include +#include + +#include + +#include + +#include +#include +#include +#include + +namespace turns::ui +{ + + auto Tracker::on_load_content_done(Glib::RefPtr result) -> void + { + set_sensitive(); + char * data{}; + auto size = std::size_t{}; + + try + { + if (!m_file->load_contents_finish(result, data, size, m_file_etag)) + { + m_file.reset(); + m_file_etag.clear(); + return; + } + + m_turn_order->load(nlohmann::json::parse(std::string_view{data, size})); + } + catch (std::exception const & e) + { + return show_error(e); + } + + auto name = m_file->get_basename(); + show_toast(std::vformat(_(lang::successfully_opened_format), std::make_format_args(name))); + set_title(std::format("{} - {}", _(lang::turns), name)); + } + + auto Tracker::on_replace_content_done(Glib::RefPtr result) -> void + { + set_sensitive(); + + try + { + m_file->replace_contents_finish(result, m_file_etag); + } + catch (Gio::Error const & e) + { + return show_error(e); + } + + auto name = m_file->get_basename(); + show_toast(std::vformat(_(lang::successfully_saved_format), std::make_format_args(name))); + set_title(std::format("{} - {}", _(lang::turns), name)); + } + + auto Tracker::on_open_response(Glib::RefPtr result, Glib::RefPtr dialog) -> void + { + try + { + m_file = dialog->open_finish(result); + } + catch (std::exception const & e) + { + return show_error(e); + } + + m_file->load_contents_async(sigc::mem_fun(*this, &Tracker::on_load_content_done)); + set_sensitive(false); + } + + auto Tracker::on_save_response(Glib::RefPtr result, Glib::RefPtr dialog) -> void + { + try + { + m_file = dialog->save_finish(result); + } + catch (std::exception const & e) + { + show_error(e); + } + + start_replace_content(); + set_sensitive(false); + } + + auto Tracker::on_settings_changed(Glib::ustring) -> void + { + update_colors(); + } + +} // namespace turns::ui \ No newline at end of file diff --git a/gui/src/turn_order_view.cpp b/gui/src/turn_order_view.cpp new file mode 100644 index 0000000..5e5d550 --- /dev/null +++ b/gui/src/turn_order_view.cpp @@ -0,0 +1,69 @@ +/* + * SPDX-FileCopyrightText: 2025 Felix Morgner + * SPDX-License-Identifier: LGPL-2.1-only + */ + +#include "turns/ui/turn_order_view.hpp" + +#include "turns/core/participant.hpp" +#include "turns/ui/participant_row.hpp" +#include "turns/ui/template_widget.hpp" + +#include + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include + +namespace turns::ui +{ + namespace + { + auto constexpr static TYPE_NAME = "TurnOrderView"; + auto constexpr static TEMPLATE = "/ch/arknet/Turns/turn_order_view.ui"; + } // namespace + + TurnOrderView::TurnOrderView(Glib::RefPtr model) + : Glib::ObjectBase(TYPE_NAME) + , template_widget{TEMPLATE} + , m_model{model} + , m_progress{get_widget("progress")} + , m_view{get_widget("view")} + { + if (!model) + { + return; + } + + set_orientation(Gtk::Orientation::VERTICAL); + + m_view->bind_model(m_model, sigc::mem_fun(*this, &TurnOrderView::handle_create_row)); + Glib::Binding::bind_property(m_model->progress(), m_progress->property_fraction(), Glib::Binding::Flags::SYNC_CREATE); + } + + auto TurnOrderView::handle_create_row(Glib::RefPtr const item) -> Gtk::Widget * + { + auto participant = std::dynamic_pointer_cast(item); + auto row = Gtk::make_managed(participant); + + Glib::Binding::bind_property(m_model->is_running(), + row->delete_enabled(), + Glib::Binding::Flags::SYNC_CREATE | Glib::Binding::Flags::INVERT_BOOLEAN); + + Glib::Binding::bind_property(m_model->is_running(), + row->edit_enabled(), + Glib::Binding::Flags::SYNC_CREATE | Glib::Binding::Flags::INVERT_BOOLEAN); + + return row; + } + +} // namespace turns::ui::widgets \ No newline at end of file -- cgit v1.2.3