Modernize code

This commit is contained in:
Pcornat 2026-01-22 21:36:22 +01:00
commit a67eb0d0b5
Signed by: Pcornat
GPG key ID: E0326CC678A00BDD
3 changed files with 24 additions and 23 deletions

View file

@ -79,8 +79,8 @@ add_executable(LearnGtk4 main.cpp
app_win_2_back.hpp app_win_2_back.hpp
) )
set_target_properties(LearnGtk4 assimp spdlog_header_only PROPERTIES set_target_properties(LearnGtk4 spdlog_header_only PROPERTIES
CXX_STANDARD 17 CXX_STANDARD 23
CXX_STANDARD_REQUIRED ON CXX_STANDARD_REQUIRED ON
CXX_EXTENSIONS OFF CXX_EXTENSIONS OFF
INTERPROCEDURAL_OPTIMIZATION ON INTERPROCEDURAL_OPTIMIZATION ON

View file

@ -1,10 +1,16 @@
#include "app_win_2_back.hpp" #include "app_win_2_back.hpp"
#include "hello_world.hpp" #include "hello_world.hpp"
#include <glibmm/miscutils.h> #include <glibmm/miscutils.h>
#include <gtkmm/switch.h> #include <gtkmm/switch.h>
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
#include <ranges>
namespace gui_to_app { namespace gui_to_app {
namespace ranges = std::ranges;
namespace views = std::views;
AppWin2Back::AppWin2Back() : Gtk::Application("org.billy_adventures.character_sheet", Flags::HANDLES_OPEN) { AppWin2Back::AppWin2Back() : Gtk::Application("org.billy_adventures.character_sheet", Flags::HANDLES_OPEN) {
Glib::set_application_name("Billy's character sheet"); Glib::set_application_name("Billy's character sheet");
} }
@ -36,7 +42,7 @@ namespace gui_to_app {
void AppWin2Back::on_activate() { void AppWin2Back::on_activate() {
Application::on_activate(); Application::on_activate();
app_builder = [this]() -> Glib::RefPtr<Gtk::Builder> { app_builder = [this] -> Glib::RefPtr<Gtk::Builder> {
try { try {
return Gtk::Builder::create_from_file("window_ui.ui"); return Gtk::Builder::create_from_file("window_ui.ui");
} catch (const std::exception &e) { } catch (const std::exception &e) {
@ -73,7 +79,7 @@ namespace gui_to_app {
rock_kit_switch = app_builder->get_widget<Gtk::Switch>("rock_kit_switch"); rock_kit_switch = app_builder->get_widget<Gtk::Switch>("rock_kit_switch");
sack_switch = app_builder->get_widget<Gtk::Switch>("sack_switch"); sack_switch = app_builder->get_widget<Gtk::Switch>("sack_switch");
const auto test_result = [this]() -> std::vector<const char *> { const auto test_result = [this] -> std::vector<const char *> {
const std::array test_ptrs{ const std::array test_ptrs{
std::pair{ "sword_switch", sword_switch == nullptr }, std::pair{ "sword_switch", sword_switch == nullptr },
std::pair{ "lance_switch", lance_switch == nullptr }, std::pair{ "lance_switch", lance_switch == nullptr },
@ -88,16 +94,12 @@ namespace gui_to_app {
std::pair{ "rock_kit_switch", rock_kit_switch == nullptr }, std::pair{ "rock_kit_switch", rock_kit_switch == nullptr },
std::pair{ "sack_switch", sack_switch == nullptr }, std::pair{ "sack_switch", sack_switch == nullptr },
}; };
std::vector<const char *> local_test_result;
local_test_result.reserve(test_ptrs.size()); return ranges::to<std::vector<const char *> >(
std::for_each(test_ptrs.cbegin(), test_ptrs
test_ptrs.cend(), | views::filter([](const auto &a) { return a.second; })
[&local_test_result](const auto &a) { | views::keys
if (a.second) { );
local_test_result.emplace_back(a.first);
}
});
return local_test_result;
}(); }();
if (!test_result.empty()) { if (!test_result.empty()) {
spdlog::critical("Error occurred, at least one switch is not available. See logs below"); spdlog::critical("Error occurred, at least one switch is not available. See logs below");
@ -107,16 +109,15 @@ namespace gui_to_app {
on_quit(); on_quit();
return; return;
} }
// TODO: interaction avec la CharacterSheet
} }
void AppWin2Back::on_quit() noexcept { void AppWin2Back::on_quit() noexcept {
auto windows = get_windows(); ranges::for_each(get_windows() | views::filter([](const auto windows) { return windows != nullptr; }),
for (auto *window: windows) { [](auto *window) {
if (window != nullptr) {
window->set_visible(false); window->set_visible(false);
delete window; delete window;
} });
}
quit(); quit();
} }
} // gui_to_app } // gui_to_app

View file

@ -1,6 +1,6 @@
#include "app_win_2_back.hpp" #include "app_win_2_back.hpp"
int main(int argc, char *argv[]) { int main(const int argc, char *argv[]) {
auto app = gui_to_app::AppWin2Back::create(); const auto app = gui_to_app::AppWin2Back::create();
return app->run(argc, argv); return app->run(argc, argv);
} }