summaryrefslogtreecommitdiff
path: root/ui/src/tracker
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@gmail.com>2025-04-29 16:33:32 +0200
committerFelix Morgner <felix.morgner@gmail.com>2025-04-29 16:33:32 +0200
commit3ff5bd46952144926d9bd9beedf50023a51913ee (patch)
tree2c579fe13bbb5cc90f8f50e7af35218c98e123d5 /ui/src/tracker
parent873bf396b904ce477a238f22d1891e1b03f24eff (diff)
downloadturns-3ff5bd46952144926d9bd9beedf50023a51913ee.tar.xz
turns-3ff5bd46952144926d9bd9beedf50023a51913ee.zip
ui: flatten namespace hierarchy
Diffstat (limited to 'ui/src/tracker')
-rw-r--r--ui/src/tracker/actions.cpp125
-rw-r--r--ui/src/tracker/event_handlers.cpp105
2 files changed, 230 insertions, 0 deletions
diff --git a/ui/src/tracker/actions.cpp b/ui/src/tracker/actions.cpp
new file mode 100644
index 0000000..51642f8
--- /dev/null
+++ b/ui/src/tracker/actions.cpp
@@ -0,0 +1,125 @@
+#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 <sigc++/adaptors/bind.h>
+#include <sigc++/functors/mem_fun.h>
+
+#include <glibmm/i18n.h>
+#include <glibmm/refptr.h>
+#include <glibmm/ustring.h>
+#include <glibmm/variant.h>
+#include <glibmm/wrap.h>
+
+#include <giomm/liststore.h>
+#include <giomm/settings.h>
+
+#include <gtkmm/builder.h>
+#include <gtkmm/filedialog.h>
+#include <gtkmm/filefilter.h>
+#include <gtkmm/object.h>
+
+#include <adwaitamm/alertdialog.hpp>
+#include <adwaitamm/dialog.hpp>
+#include <adwaitamm/enums.hpp>
+#include <adwaitamm/preferencesdialog.hpp>
+
+#include <gio/gio.h>
+#include <gtk/gtk.h>
+
+#include <utility>
+
+namespace turns::ui
+{
+ namespace
+ {
+ auto file_filters()
+ {
+ auto filters = Gio::ListStore<Gtk::FileFilter>::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<ParticipantEditor>(nullptr);
+ dialog->present(this);
+ dialog->signal_finished().connect([this](auto n, auto p, auto d) { m_turn_order->add(n, p, d); });
+ }
+
+ 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 index = Glib::VariantBase::cast_dynamic<Glib::Variant<int>>(param);
+ auto participant = m_turn_order->get_typed_object<core::participant>(index.get());
+ auto dialog = Gtk::make_managed<ParticipantEditor>(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<struct preferences>(m_settings);
+ auto dialog = Gtk::make_managed<Adwaita::PreferencesDialog>();
+ 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/ui/src/tracker/event_handlers.cpp b/ui/src/tracker/event_handlers.cpp
new file mode 100644
index 0000000..80664e4
--- /dev/null
+++ b/ui/src/tracker/event_handlers.cpp
@@ -0,0 +1,105 @@
+#include "turns/lang/messages.hpp"
+#include "turns/ui/tracker.hpp"
+
+#include <sigc++/functors/mem_fun.h>
+
+#include <glibmm/i18n.h>
+#include <glibmm/refptr.h>
+#include <glibmm/ustring.h>
+
+#include <giomm/asyncresult.h>
+#include <giomm/error.h>
+
+#include <gtkmm/filedialog.h>
+
+#include <nlohmann/json.hpp>
+
+#include <cstddef>
+#include <exception>
+#include <format>
+#include <string_view>
+
+namespace turns::ui
+{
+
+ auto tracker::on_load_content_done(Glib::RefPtr<Gio::AsyncResult> 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<Gio::AsyncResult> 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<Gio::AsyncResult> result, Glib::RefPtr<Gtk::FileDialog> 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<Gio::AsyncResult> result, Glib::RefPtr<Gtk::FileDialog> 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