diff options
| -rw-r--r-- | adw/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | adw/include/adwaitamm/alertdialog.hpp | 20 | ||||
| -rw-r--r-- | adw/include/adwaitamm/helpers/async_callback.hpp | 15 | ||||
| -rw-r--r-- | adw/src/alertdialog.cpp | 52 | ||||
| -rw-r--r-- | adw/src/helpers/async_callback.cpp | 28 | ||||
| -rw-r--r-- | ui/src/windows/tracker/actions.cpp | 52 |
6 files changed, 136 insertions, 32 deletions
diff --git a/adw/CMakeLists.txt b/adw/CMakeLists.txt index c6bbd3a..ad979ea 100644 --- a/adw/CMakeLists.txt +++ b/adw/CMakeLists.txt @@ -15,6 +15,7 @@ pkg_check_modules("giomm" IMPORTED_TARGET REQUIRED "giomm-2.68>=2.80") pkg_check_modules("glibmm" IMPORTED_TARGET REQUIRED "glibmm-2.68>=2.80") add_library("adwaitamm" + "src/helpers/async_callback.cpp" "src/actionrow.cpp" "src/alertdialog.cpp" "src/application.cpp" diff --git a/adw/include/adwaitamm/alertdialog.hpp b/adw/include/adwaitamm/alertdialog.hpp index 9da8488..8608cc6 100644 --- a/adw/include/adwaitamm/alertdialog.hpp +++ b/adw/include/adwaitamm/alertdialog.hpp @@ -10,6 +10,9 @@ #include <glibmm/refptr.h> #include <glibmm/ustring.h> +#include <giomm/asyncresult.h> +#include <giomm/cancellable.h> + #include <gtkmm/widget.h> #include <glib-object.h> @@ -19,8 +22,6 @@ using AdwAlertDialogClass = struct _AdwAlertDialogClass; namespace Adwaita { - struct Dialog_Class; - struct AlertDialog : Dialog, helpers::gobj_mixin<AlertDialog, AdwAlertDialog> { @@ -37,6 +38,13 @@ namespace Adwaita auto static wrap_new(GObject * object) -> Glib::ObjectBase *; }; + enum struct ResponseAppearance : int + { + _default, + suggested, + destructive, + }; + using BaseObjectType = Class::BaseObjectType; using BaseClassType = Class::BaseClassType; using CppObjectType = Class::CppObjectType; @@ -52,9 +60,17 @@ namespace Adwaita auto operator=(AlertDialog const & other) noexcept -> AlertDialog & = delete; auto operator=(AlertDialog && other) noexcept -> AlertDialog & = default; + auto static create(Glib::ustring heading, Glib::ustring body) -> Glib::RefPtr<AlertDialog>; auto static get_type() -> GType; auto static get_base_type() -> GType; + auto add_response(Glib::ustring id, Glib::ustring label) -> void; + auto choose(Gtk::Widget & parent, Glib::RefPtr<Gio::Cancellable> const & cancellable, Gio::SlotAsyncReady const & slot) -> void; + auto choose_finish(Glib::RefPtr<Gio::AsyncResult> const & result) -> Glib::ustring; + auto set_close_response(Glib::ustring id) -> void; + auto set_default_response(Glib::ustring id) -> void; + auto set_response_appearance(Glib::ustring id, ResponseAppearance value) -> void; + protected: explicit AlertDialog(Glib::ConstructParams const & params); explicit AlertDialog(BaseObjectType * gobj); diff --git a/adw/include/adwaitamm/helpers/async_callback.hpp b/adw/include/adwaitamm/helpers/async_callback.hpp new file mode 100644 index 0000000..e352359 --- /dev/null +++ b/adw/include/adwaitamm/helpers/async_callback.hpp @@ -0,0 +1,15 @@ +#ifndef LIBADWAITAMM_HELPERS_ASYNC_CALLBACK_HPP +#define LIBADWAITAMM_HELPERS_ASYNC_CALLBACK_HPP + +#include <glibmm/object.h> + +#include <giomm/asyncresult.h> + +namespace Adwaita::helpers +{ + + auto async_callback(GObject *, GAsyncResult * result, void * data) noexcept -> void; + +} // namespace Adwaita::helpers + +#endif
\ No newline at end of file diff --git a/adw/src/alertdialog.cpp b/adw/src/alertdialog.cpp index 83afaa1..ea9cdca 100644 --- a/adw/src/alertdialog.cpp +++ b/adw/src/alertdialog.cpp @@ -1,6 +1,9 @@ #include "adwaitamm/alertdialog.hpp" +#include "adwaitamm/dialog.hpp" + #include <glibmm/class.h> +#include <glibmm/exceptionhandler.h> #include <glibmm/object.h> #include <glibmm/objectbase.h> #include <glibmm/refptr.h> @@ -8,20 +11,28 @@ #include <glibmm/utility.h> #include <glibmm/wrap.h> +#include <giomm/asyncresult.h> +#include <giomm/cancellable.h> + #include <gtkmm/init.h> #include <gtkmm/object.h> #include <gtkmm/private/widget_p.h> #include <gtkmm/widget.h> -#include <adwaitamm/dialog.hpp> +#include <adwaitamm/helpers/async_callback.hpp> #include <adwaita.h> #include <glib-object.h> +#include <glib.h> #include <gtk/gtk.h> namespace Adwaita { + static_assert(static_cast<int>(AlertDialog::ResponseAppearance::_default) == ADW_RESPONSE_DEFAULT); + static_assert(static_cast<int>(AlertDialog::ResponseAppearance::suggested) == ADW_RESPONSE_SUGGESTED); + static_assert(static_cast<int>(AlertDialog::ResponseAppearance::destructive) == ADW_RESPONSE_DESTRUCTIVE); + namespace { auto constinit _class = AlertDialog::Class{}; @@ -48,6 +59,11 @@ namespace Adwaita return Gtk::manage(new AlertDialog(ADW_ALERT_DIALOG(object))); } + auto AlertDialog::create(Glib::ustring heading, Glib::ustring body) -> Glib::RefPtr<AlertDialog> + { + return Glib::RefPtr<AlertDialog>{new AlertDialog(heading, body)}; + } + auto AlertDialog::get_type() -> GType { return _class.init().get_type(); @@ -74,6 +90,40 @@ namespace Adwaita { } + auto AlertDialog::add_response(Glib::ustring id, Glib::ustring label) -> void + { + adw_alert_dialog_add_response(Glib::unwrap(this), id.c_str(), label.c_str()); + } + + auto AlertDialog::choose(Gtk::Widget & parent, Glib::RefPtr<Gio::Cancellable> const & cancellable, Gio::SlotAsyncReady const & slot) -> void + { + adw_alert_dialog_choose(unwrap(this), + parent.gobj(), + const_cast<GCancellable *>(unwrap(cancellable)), + &helpers::async_callback, + new Gio::SlotAsyncReady(slot)); + } + + auto AlertDialog::choose_finish(Glib::RefPtr<Gio::AsyncResult> const & result) -> Glib::ustring + { + return adw_alert_dialog_choose_finish(Glib::unwrap(this), Glib::unwrap(result)); + } + + auto AlertDialog::set_close_response(Glib::ustring id) -> void + { + adw_alert_dialog_set_close_response(unwrap(this), id.c_str()); + } + + auto AlertDialog::set_default_response(Glib::ustring id) -> void + { + adw_alert_dialog_set_default_response(unwrap(this), id.c_str()); + } + + auto AlertDialog::set_response_appearance(Glib::ustring id, ResponseAppearance value) -> void + { + adw_alert_dialog_set_response_appearance(unwrap(this), id.c_str(), static_cast<AdwResponseAppearance>(static_cast<int>(value))); + } + } // namespace Adwaita namespace Glib diff --git a/adw/src/helpers/async_callback.cpp b/adw/src/helpers/async_callback.cpp new file mode 100644 index 0000000..4f148f0 --- /dev/null +++ b/adw/src/helpers/async_callback.cpp @@ -0,0 +1,28 @@ +#include "adwaitamm/helpers/async_callback.hpp" + +#include <glibmm/exceptionhandler.h> +#include <glibmm/object.h> + +#include <giomm/asyncresult.h> + +#include <memory> + +namespace Adwaita::helpers +{ + + auto async_callback(GObject *, GAsyncResult * result, void * data) noexcept -> void + { + auto slot = std::unique_ptr<Gio::SlotAsyncReady>(static_cast<Gio::SlotAsyncReady *>(data)); + + try + { + auto actual_result = Glib::wrap(result, true); + (*slot)(actual_result); + } + catch (...) + { + Glib::exception_handlers_invoke(); + } + } + +} // namespace Adwaita::helpers
\ No newline at end of file diff --git a/ui/src/windows/tracker/actions.cpp b/ui/src/windows/tracker/actions.cpp index 3fcf448..5223a34 100644 --- a/ui/src/windows/tracker/actions.cpp +++ b/ui/src/windows/tracker/actions.cpp @@ -1,5 +1,4 @@ #include "turns/core/participant.hpp" -#include "turns/core/turn_order.hpp" #include "turns/lang/messages.hpp" #include "turns/ui/widgets/preferences.hpp" #include "turns/ui/windows/participant_editor.hpp" @@ -22,6 +21,8 @@ #include <gtkmm/filefilter.h> #include <gtkmm/object.h> +#include <adwaitamm/alertdialog.hpp> +#include <adwaitamm/dialog.hpp> #include <adwaitamm/preferencesdialog.hpp> #include <adwaita.h> @@ -40,20 +41,6 @@ namespace turns::ui::windows return std::pair{builder, Gtk::Builder::get_widget_derived<participant_editor>(builder, "participant_editor", participant)}; } - auto stop_dialog_callback(AdwAlertDialog * dialog, GAsyncResult * result, core::turn_order * order) - { - auto response = Glib::ustring{adw_alert_dialog_choose_finish(dialog, result)}; - if (response == "cancel") - { - return; - } - if (response == "clear") - { - order->clear(); - } - order->stop(); - } - auto file_filters() { auto filters = Gio::ListStore<Gtk::FileFilter>::create(); @@ -120,19 +107,26 @@ namespace turns::ui::windows auto tracker::stop() -> void { - auto dialog = ADW_ALERT_DIALOG(adw_alert_dialog_new(_(lang::stop_turn_order), _(lang::question_clear_turn_order))); - adw_alert_dialog_add_response(dialog, "stop", _(lang::stop)); - adw_alert_dialog_set_response_appearance(dialog, "stop", ADW_RESPONSE_SUGGESTED); - adw_alert_dialog_add_response(dialog, "clear", _(lang::stop_and_clear)); - adw_alert_dialog_set_response_appearance(dialog, "clear", ADW_RESPONSE_DESTRUCTIVE); - adw_alert_dialog_add_response(dialog, "cancel", _(lang::cancel)); - adw_alert_dialog_set_response_appearance(dialog, "cancel", ADW_RESPONSE_DEFAULT); - adw_alert_dialog_set_close_response(dialog, "cancel"); - adw_alert_dialog_set_default_response(dialog, "cancel"); - adw_alert_dialog_choose(dialog, - GTK_WIDGET(Glib::unwrap(this)), - nullptr, - reinterpret_cast<GAsyncReadyCallback>(stop_dialog_callback), - m_turn_order.get()); + 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::AlertDialog::ResponseAppearance::suggested); + dialog->add_response("clear", _(lang::stop_and_clear)); + dialog->set_response_appearance("clear", Adwaita::AlertDialog::ResponseAppearance::destructive); + dialog->add_response("cancel", _(lang::cancel)); + dialog->set_response_appearance("cancel", Adwaita::AlertDialog::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::windows
\ No newline at end of file |
