From 8de5772eb3acd5ba6e4380b2bcb7b5e829b3f9f4 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Sat, 13 Jul 2024 14:43:05 +0200 Subject: app/ui: add basic participant list row --- app/CMakeLists.txt | 1 + .../turns/app/widgets/participant_list_row.hpp | 44 ++++++++++++ app/include/turns/app/widgets/template_widget.hpp | 63 +++++++++++++++++ app/include/turns/app/windows/main.hpp | 9 +++ app/src/widgets/participant_list_row.cpp | 38 ++++++++++ app/src/windows/main.cpp | 25 ++++++- res/CMakeLists.txt | 1 + res/widgets/participant_list_row.ui | 81 ++++++++++++++++++++++ res/widgets/widgets.cmb | 71 +++++++++++++++++++ res/windows/main_window.ui | 25 ++++++- res/windows/windows.cmb | 34 +++++++-- 11 files changed, 383 insertions(+), 9 deletions(-) create mode 100644 app/include/turns/app/widgets/participant_list_row.hpp create mode 100644 app/include/turns/app/widgets/template_widget.hpp create mode 100644 app/src/widgets/participant_list_row.cpp create mode 100644 res/widgets/participant_list_row.ui create mode 100644 res/widgets/widgets.cmb diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index 3dddc65..aa65a86 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -2,6 +2,7 @@ add_library("app" "src/application.cpp" + "src/widgets/participant_list_row.cpp" "src/windows/main.cpp" ) diff --git a/app/include/turns/app/widgets/participant_list_row.hpp b/app/include/turns/app/widgets/participant_list_row.hpp new file mode 100644 index 0000000..b9efbd6 --- /dev/null +++ b/app/include/turns/app/widgets/participant_list_row.hpp @@ -0,0 +1,44 @@ +#ifndef TURNS_APP_WIDGETS_PARTICIPANT_LIST_ROW_HPP +#define TURNS_APP_WIDGETS_PARTICIPANT_LIST_ROW_HPP + +#include "turns/app/widgets/template_widget.hpp" + +#include + +#include +#include +#include +#include +#include + +namespace turns::app::widgets +{ + struct participant_list_row : template_widget + { + auto constexpr inline static children = std::array{ + "header", + "prefixes", + "image", + "title_box", + "title", + "subtitle", + "suffixes", + }; + + participant_list_row(); + + auto set_subtitle(Glib::ustring const & value) -> void; + auto set_title(Glib::ustring const & value) -> void; + + private: + Gtk::Box * m_header; + Gtk::Image * m_image; + Gtk::Box * m_prefixes; + Gtk::Label * m_subtitle; + Gtk::Box * m_suffixes; + Gtk::Label * m_title; + Gtk::Box * m_title_box; + }; +} // namespace turns::app::widgets + +#endif \ No newline at end of file diff --git a/app/include/turns/app/widgets/template_widget.hpp b/app/include/turns/app/widgets/template_widget.hpp new file mode 100644 index 0000000..62b82e7 --- /dev/null +++ b/app/include/turns/app/widgets/template_widget.hpp @@ -0,0 +1,63 @@ +#ifndef TURNS_APP_WIDGETS_TEMPLATE_WIDGET_HPP +#define TURNS_APP_WIDGETS_TEMPLATE_WIDGET_HPP + +#include +#include + +#include +#include +#include +#include +#include + +namespace turns::app::widgets +{ + + template + struct template_widget : Glib::ExtraClassInit, + BaseWidgetType + { + template + template_widget(Glib::ustring && resource_path, BaseWidgetCtorArgTypes &&... base_widget_ctor_args) + : Glib::ExtraClassInit{class_init, &resource_path, instance_init} + , BaseWidgetType{std::forward(base_widget_ctor_args)...} + { + } + + protected: + template + auto get_widget(char const * name) -> WidgetType * + { + auto self = static_cast(this); + auto widget = GTK_WIDGET(self->gobj()); + auto type = G_OBJECT_TYPE(self->gobj()); + auto child = GTK_WIDGET(gtk_widget_get_template_child(widget, type, name)); + g_assert_nonnull(child); + return dynamic_cast(Glib::wrap(child)); + } + + private: + auto static class_init(void * g_class, void * g_class_data) -> void + { + g_return_if_fail(GTK_IS_WIDGET_CLASS(g_class)); + + auto resource_path = static_cast(g_class_data); + + gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS(g_class), resource_path->c_str()); + + std::ranges::for_each(CustomWidgetType::children, [g_class](auto const & child) { + gtk_widget_class_bind_template_child_full(GTK_WIDGET_CLASS(g_class), child, false, 0); + }); + } + + auto static instance_init(GTypeInstance * instance, void * /* type_class */) -> void + { + g_return_if_fail(GTK_IS_WIDGET(instance)); + + gtk_widget_init_template(GTK_WIDGET(instance)); + } + }; + +} // namespace turns::app::widgets + +#endif \ No newline at end of file diff --git a/app/include/turns/app/windows/main.hpp b/app/include/turns/app/windows/main.hpp index e27c1c7..fd55e72 100644 --- a/app/include/turns/app/windows/main.hpp +++ b/app/include/turns/app/windows/main.hpp @@ -2,11 +2,15 @@ #define TURNS_APP_WINDOWS_MAIN_WINDOW_HPP #include +#include #include #include #include #include +#include #include +#include +#include namespace turns::app::windows { @@ -16,10 +20,15 @@ namespace turns::app::windows main(BaseObjectType * base, Glib::RefPtr const builder); private: + auto handle_create_list_row(Glib::RefPtr const item) -> Gtk::Widget *; + Gtk::Button * m_add_participant; AdwApplicationWindow * m_adw; Gtk::MenuButton * m_open_main_menu; + Gtk::ListBox * m_participant_list; AdwWindowTitle * m_title; + + Glib::RefPtr m_tests; }; } // namespace turns::app::windows diff --git a/app/src/widgets/participant_list_row.cpp b/app/src/widgets/participant_list_row.cpp new file mode 100644 index 0000000..bc3108e --- /dev/null +++ b/app/src/widgets/participant_list_row.cpp @@ -0,0 +1,38 @@ +#include "turns/app/widgets/participant_list_row.hpp" + +#include + +namespace turns::app::widgets +{ + namespace + { + auto constexpr static TYPE_NAME = "participant_list_row"; + auto constexpr static TEMPLATE = "/turns/widgets/participant_list_row.ui"; + } // namespace + + participant_list_row::participant_list_row() + : Glib::ObjectBase(TYPE_NAME) + , template_widget{TEMPLATE} + { + m_header = get_widget("header"); + m_image = get_widget("image"); + m_prefixes = get_widget("prefixes"); + m_subtitle = get_widget("subtitle"); + m_suffixes = get_widget("suffixes"); + m_title = get_widget("title"); + m_title_box = get_widget("title_box"); + } + + auto participant_list_row::set_subtitle(Glib::ustring const & value) -> void + { + m_subtitle->set_text(value); + m_subtitle->set_visible(!value.empty()); + } + + auto participant_list_row::set_title(Glib::ustring const & value) -> void + { + m_title->set_text(value); + m_title->set_visible(!value.empty()); + } + +} // namespace turns::app::widgets \ No newline at end of file diff --git a/app/src/windows/main.cpp b/app/src/windows/main.cpp index 2ad9117..20d0649 100644 --- a/app/src/windows/main.cpp +++ b/app/src/windows/main.cpp @@ -1,9 +1,14 @@ #include "turns/app/windows/main.hpp" +#include "turns/app/widgets/participant_list_row.hpp" #include "turns/lang/messages.hpp" +#include +#include + #include #include +#include namespace turns::app::windows { @@ -15,11 +20,29 @@ namespace turns::app::windows , m_add_participant{builder->get_widget("add_participant")} , m_adw{ADW_APPLICATION_WINDOW(gobj())} , m_open_main_menu{builder->get_widget("open_main_menu")} + , m_participant_list{builder->get_widget("participant_list")} , m_title(ADW_WINDOW_TITLE(builder->get_widget("title")->gobj())) + , m_tests{Gtk::StringList::create()} { m_add_participant->set_tooltip_text(_(tooltips::add_a_participant)); m_open_main_menu->set_tooltip_text(_(tooltips::main_menu)); adw_window_title_set_subtitle(m_title, _(labels::no_active_turn_order)); + + for (auto n : std::views::iota(0, 32)) + { + m_tests->append(std::format("Participant #{}", n)); + } + + m_participant_list->bind_model(m_tests, sigc::mem_fun(*this, &main::handle_create_list_row)); + } + + auto main::handle_create_list_row(Glib::RefPtr const item) -> Gtk::Widget * + { + auto data = std::dynamic_pointer_cast(item); + auto widget = Gtk::make_managed(); + widget->set_title(data->get_string()); + widget->set_subtitle("Details for " + data->get_string()); + return widget; } -} // namespace turns::app::windows \ No newline at end of file +} // namespace turns::app::windows diff --git a/res/CMakeLists.txt b/res/CMakeLists.txt index f823bec..b2b723a 100644 --- a/res/CMakeLists.txt +++ b/res/CMakeLists.txt @@ -5,6 +5,7 @@ add_library("res") target_add_glib_resources("res" PREFIX "turns" UI_FILES + "widgets/participant_list_row.ui" "windows/main_window.ui" ) diff --git a/res/widgets/participant_list_row.ui b/res/widgets/participant_list_row.ui new file mode 100644 index 0000000..66506c8 --- /dev/null +++ b/res/widgets/participant_list_row.ui @@ -0,0 +1,81 @@ + + + + + + + + diff --git a/res/widgets/widgets.cmb b/res/widgets/widgets.cmb new file mode 100644 index 0000000..ce44dd0 --- /dev/null +++ b/res/widgets/widgets.cmb @@ -0,0 +1,71 @@ + + + + + (1,1,"participant_list_item.ui","participant_list_row.ui",None,None,None,None,None,"Adapted from: https://gitlab.gnome.org/GNOME/libadwaita",None) + + + (1,"gtk","4.14",None) + + + (1,1,"GtkListBoxRow","gtkmm__CustomObject_participant_list_row",None,None,None,None,-1,None,None), + (1,2,"GtkBox","header",1,None,None,None,-1,None,None), + (1,3,"GtkBox","prefixes",2,None,None,None,-1,None,None), + (1,4,"GtkImage","image",2,None,None,None,-1,None,None), + (1,5,"GtkBox","title_box",2,None,None,None,-1,None,None), + (1,6,"GtkLabel","title",5,None,None,None,-1,None,None), + (1,7,"GtkLabel","subtitle",5,None,None,None,-1,None,None), + (1,8,"GtkBox","suffixes",2,None,None,None,-1,None,None), + (1,9,"GtkSwitch",None,8,None,None,None,-1,None,None) + + + (1,1,"GtkListBoxRow","child",None,None,None,None,None,2,None,None,None,None), + (1,1,"GtkWidget","valign","center",None,None,None,None,None,None,None,None,None), + (1,2,"GtkWidget","css-classes","header",None,None,None,None,None,None,None,None,None), + (1,3,"GtkWidget","visible","False",None,None,None,None,None,None,None,None,None), + (1,4,"GtkImage","icon-name","face-smile",None,None,None,None,None,None,None,None,None), + (1,4,"GtkWidget","valign","center",None,None,None,None,None,None,None,None,None), + (1,4,"GtkWidget","visible","False",None,None,None,None,None,None,None,None,None), + (1,5,"GtkOrientable","orientation","vertical",None,None,None,None,None,None,None,None,None), + (1,5,"GtkWidget","hexpand","True",None,None,None,None,None,None,None,None,None), + (1,5,"GtkWidget","valign","center",None,None,None,None,None,None,None,None,None), + (1,6,"GtkLabel","lines","0",None,None,None,None,None,None,None,None,None), + (1,6,"GtkLabel","wrap","True",None,None,None,None,None,None,None,None,None), + (1,6,"GtkLabel","wrap-mode","word-char",None,None,None,None,None,None,None,None,None), + (1,6,"GtkLabel","xalign","0.0",None,None,None,None,None,None,None,None,None), + (1,6,"GtkWidget","visible","False",None,None,None,None,None,None,None,None,None), + (1,7,"GtkLabel","lines","0",None,None,None,None,None,None,None,None,None), + (1,7,"GtkLabel","wrap","True",None,None,None,None,None,None,None,None,None), + (1,7,"GtkLabel","wrap-mode","word-char",None,None,None,None,None,None,None,None,None), + (1,7,"GtkLabel","xalign","0.0",None,None,None,None,None,None,None,None,None), + (1,7,"GtkWidget","visible","False",None,None,None,None,None,None,None,None,None), + (1,8,"GtkWidget","valign","center",None,None,None,None,None,None,None,None,None) + + + (1,6,"GtkWidget",1,1,None,None,None,None,None,None), + (1,6,"GtkWidget",2,2,None,1,None,None,None,None), + (1,5,"GtkWidget",1,1,None,None,None,None,None,None), + (1,5,"GtkWidget",2,2,None,1,None,None,None,None), + (1,4,"GtkWidget",1,1,None,None,None,None,None,None), + (1,4,"GtkWidget",2,2,None,1,None,None,None,None), + (1,3,"GtkWidget",1,1,None,None,None,None,None,None), + (1,3,"GtkWidget",2,2,None,1,None,None,None,None), + (1,7,"GtkWidget",1,1,None,None,None,None,None,None), + (1,7,"GtkWidget",2,2,None,1,None,None,None,None), + (1,8,"GtkWidget",1,1,None,None,None,None,None,None), + (1,8,"GtkWidget",2,2,None,1,None,None,None,None), + (1,1,"GtkWidget",2,2,None,1,None,None,None,None), + (1,1,"GtkWidget",1,3,None,None,None,None,None,None), + (1,1,"GtkWidget",2,4,None,3,None,None,None,None) + + + (1,6,"GtkWidget",2,2,"name","title"), + (1,5,"GtkWidget",2,2,"name","title"), + (1,4,"GtkWidget",2,2,"name","icon"), + (1,3,"GtkWidget",2,2,"name","prefixes"), + (1,7,"GtkWidget",2,2,"name","subtitle"), + (1,8,"GtkWidget",2,2,"name","suffixes"), + (1,1,"GtkWidget",2,2,"name","property"), + (1,1,"GtkWidget",2,4,"name","view") + + diff --git a/res/windows/main_window.ui b/res/windows/main_window.ui index 5dc17d8..4712455 100644 --- a/res/windows/main_window.ui +++ b/res/windows/main_window.ui @@ -10,8 +10,23 @@ + True - + + + + 18 + 12 + 12 + 18 + none + start + + + + @@ -33,12 +48,16 @@ contact-new +