From d110c7abbcde35d5350319b5b295e9ea271a1d01 Mon Sep 17 00:00:00 2001 From: "anthropic-code-agent[bot]" <242468646+Claude@users.noreply.github.com> Date: Wed, 4 Mar 2026 21:50:41 +0000 Subject: [PATCH 01/20] Initial plan From f692e2879ce2ee94a0a1c210a676a044b26c7b9c Mon Sep 17 00:00:00 2001 From: "anthropic-code-agent[bot]" <242468646+Claude@users.noreply.github.com> Date: Wed, 4 Mar 2026 21:56:40 +0000 Subject: [PATCH 02/20] Implement theme-aware red text colors for dark theme legibility Co-authored-by: ashaduri <2302268+ashaduri@users.noreply.github.com> --- src/applib/warning_colors.h | 35 ++++++++++++++++++++++++++++++++--- src/gui/gsc_info_window.cpp | 5 ++++- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/applib/warning_colors.h b/src/applib/warning_colors.h index 1dd92f9..8ebc614 100644 --- a/src/applib/warning_colors.h +++ b/src/applib/warning_colors.h @@ -13,10 +13,37 @@ Copyright: #define WARNING_COLORS_H #include +#include #include "storage_property.h" +/// Check if a dark GTK theme is currently active +inline bool is_dark_theme_active() +{ + // Try to get the GTK settings to check for dark theme preference + Glib::RefPtr settings = Gtk::Settings::get_default(); + if (settings) { + // Check if the application prefers dark theme + bool prefer_dark = false; + settings->get_property("gtk-application-prefer-dark-theme", prefer_dark); + if (prefer_dark) { + return true; + } + + // Check theme name for common dark theme identifiers + Glib::ustring theme_name; + settings->get_property("gtk-theme-name", theme_name); + std::string theme_str = theme_name.lowercase(); + if (theme_str.find("dark") != std::string::npos || + theme_str.find("black") != std::string::npos) { + return true; + } + } + + return false; +} + /// Get colors for tree rows according to warning severity. /// \return true if the colors were changed. @@ -45,14 +72,16 @@ inline bool app_property_get_row_highlight_colors(WarningLevel warning, std::str /// \return true if the color was changed. inline bool app_property_get_label_highlight_color(WarningLevel warning, std::string& fg) { + bool dark_theme = is_dark_theme_active(); + if (warning == WarningLevel::Notice) { - fg = "#770000"; // very dark red + fg = dark_theme ? "#FF9999" : "#770000"; // lighter red for dark themes, very dark red for light themes } else if (warning == WarningLevel::Warning) { - fg = "#C00000"; // dark red + fg = dark_theme ? "#FF6666" : "#C00000"; // lighter red for dark themes, dark red for light themes } else if (warning == WarningLevel::Alert) { - fg = "#FF0000"; // red + fg = dark_theme ? "#FF4444" : "#FF0000"; // lighter/pink red for dark themes, bright red for light themes } return !(fg.empty()); diff --git a/src/gui/gsc_info_window.cpp b/src/gui/gsc_info_window.cpp index ae32000..d3d217b 100644 --- a/src/gui/gsc_info_window.cpp +++ b/src/gui/gsc_info_window.cpp @@ -2444,7 +2444,10 @@ gboolean GscInfoWindow::test_idle_callback(void* data) break; case SelfTestStatusSeverity::Error: if (!result_main_msg.empty()) { // Highlight in red - result_main_msg = ""s + result_main_msg + ""; + std::string alert_color; + // Use the same color as Alert level warnings for consistency + app_property_get_label_highlight_color(WarningLevel::Alert, alert_color); + result_main_msg = ""s + result_main_msg + ""; } result_details_msg += "\n"s + _("Check the Self-Test Log for more information."); break; From 525fcfe3afb6d3c8387c9f978357289d52d38d24 Mon Sep 17 00:00:00 2001 From: "anthropic-code-agent[bot]" <242468646+Claude@users.noreply.github.com> Date: Fri, 6 Mar 2026 14:36:54 +0000 Subject: [PATCH 03/20] Add GTK init guard and optimize theme detection - Guard theme detection with Gtk::Main::level() check to avoid crashes in non-GUI executables - Use narrow headers (gtkmm/settings.h, gtkmm/main.h) instead of umbrella gtkmm.h - Return early from app_property_get_label_highlight_color() for WarningLevel::None Co-authored-by: ashaduri <2302268+ashaduri@users.noreply.github.com> --- src/applib/warning_colors.h | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/applib/warning_colors.h b/src/applib/warning_colors.h index 8ebc614..cc13052 100644 --- a/src/applib/warning_colors.h +++ b/src/applib/warning_colors.h @@ -13,7 +13,8 @@ Copyright: #define WARNING_COLORS_H #include -#include +#include +#include #include "storage_property.h" @@ -21,6 +22,12 @@ Copyright: /// Check if a dark GTK theme is currently active inline bool is_dark_theme_active() { + // Only check GTK settings if GTK has been initialized + // This avoids crashes/warnings in non-GUI executables + if (Gtk::Main::level() == 0) { + return false; // Default to light theme if GTK not initialized + } + // Try to get the GTK settings to check for dark theme preference Glib::RefPtr settings = Gtk::Settings::get_default(); if (settings) { @@ -72,6 +79,11 @@ inline bool app_property_get_row_highlight_colors(WarningLevel warning, std::str /// \return true if the color was changed. inline bool app_property_get_label_highlight_color(WarningLevel warning, std::string& fg) { + // Return early for None to avoid unnecessary theme detection + if (warning == WarningLevel::None) { + return false; + } + bool dark_theme = is_dark_theme_active(); if (warning == WarningLevel::Notice) { From 258d06b755a095817188cde0f5cfcb8be00e0b4a Mon Sep 17 00:00:00 2001 From: "anthropic-code-agent[bot]" <242468646+Claude@users.noreply.github.com> Date: Fri, 6 Mar 2026 19:08:06 +0000 Subject: [PATCH 04/20] Revert to umbrella gtkmm.h include per maintainer request Co-authored-by: ashaduri <2302268+ashaduri@users.noreply.github.com> --- src/applib/warning_colors.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/applib/warning_colors.h b/src/applib/warning_colors.h index cc13052..3f5157f 100644 --- a/src/applib/warning_colors.h +++ b/src/applib/warning_colors.h @@ -13,8 +13,7 @@ Copyright: #define WARNING_COLORS_H #include -#include -#include +#include #include "storage_property.h" From 7e626460c041849c6aa81fffbf49dd4957672bf3 Mon Sep 17 00:00:00 2001 From: Alexander Shaduri Date: Sat, 7 Mar 2026 16:15:24 +0100 Subject: [PATCH 05/20] Update src/gui/gsc_info_window.cpp Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/gui/gsc_info_window.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/gui/gsc_info_window.cpp b/src/gui/gsc_info_window.cpp index d3d217b..8530a04 100644 --- a/src/gui/gsc_info_window.cpp +++ b/src/gui/gsc_info_window.cpp @@ -2446,8 +2446,9 @@ gboolean GscInfoWindow::test_idle_callback(void* data) if (!result_main_msg.empty()) { // Highlight in red std::string alert_color; // Use the same color as Alert level warnings for consistency - app_property_get_label_highlight_color(WarningLevel::Alert, alert_color); - result_main_msg = ""s + result_main_msg + ""; + if (app_property_get_label_highlight_color(WarningLevel::Alert, alert_color) && !alert_color.empty()) { + result_main_msg = ""s + result_main_msg + ""; + } } result_details_msg += "\n"s + _("Check the Self-Test Log for more information."); break; From 68032c4f3c791affebdb3d27b354a05807ba4412 Mon Sep 17 00:00:00 2001 From: Alexander Shaduri Date: Sat, 7 Mar 2026 17:39:35 +0100 Subject: [PATCH 06/20] Update src/applib/warning_colors.h Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/applib/warning_colors.h | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/applib/warning_colors.h b/src/applib/warning_colors.h index 3f5157f..3f75cea 100644 --- a/src/applib/warning_colors.h +++ b/src/applib/warning_colors.h @@ -21,13 +21,8 @@ Copyright: /// Check if a dark GTK theme is currently active inline bool is_dark_theme_active() { - // Only check GTK settings if GTK has been initialized - // This avoids crashes/warnings in non-GUI executables - if (Gtk::Main::level() == 0) { - return false; // Default to light theme if GTK not initialized - } - - // Try to get the GTK settings to check for dark theme preference + // Try to get the GTK settings to check for dark theme preference. + // If GTK is not available or not initialized, get_default() will return null. Glib::RefPtr settings = Gtk::Settings::get_default(); if (settings) { // Check if the application prefers dark theme From 083936e99b515a3f2854767b9e654450db22f74c Mon Sep 17 00:00:00 2001 From: Claude <242468646+Claude@users.noreply.github.com> Date: Sun, 8 Mar 2026 18:31:58 +0100 Subject: [PATCH 07/20] Add Windows dark mode detection and automatic GTK theme switching (#111) --- src/gui/gsc_init.cpp | 26 ++++++++++++++ src/gui/gsc_main_window_iconview.cpp | 8 ++++- src/hz/win32_tools.h | 51 ++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 1 deletion(-) diff --git a/src/gui/gsc_init.cpp b/src/gui/gsc_init.cpp index 0dff60c..de288ef 100644 --- a/src/gui/gsc_init.cpp +++ b/src/gui/gsc_init.cpp @@ -505,6 +505,32 @@ bool app_init_and_loop(int& argc, char**& argv) } */ + // Detect Windows dark mode and set GTK theme preference accordingly + if constexpr(BuildEnv::is_kernel_family_windows()) { + Glib::RefPtr gtk_settings = Gtk::Settings::get_default(); + if (gtk_settings) { + bool use_dark_theme = false; + #ifdef _WIN32 + // Check Windows registry for dark mode preference + // HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize + // AppsUseLightTheme = 0 means dark mode, 1 means light mode + DWORD apps_use_light_theme = 1; // Default to light mode + if (hz::win32_get_registry_value_dword(HKEY_CURRENT_USER, + R"(Software\Microsoft\Windows\CurrentVersion\Themes\Personalize)", + "AppsUseLightTheme", apps_use_light_theme)) { + use_dark_theme = (apps_use_light_theme == 0); + debug_out_dump("app", "Windows theme detected: " << (use_dark_theme ? "dark" : "light") << "\n"); + } else { + debug_out_dump("app", "Could not read Windows theme preference, defaulting to light mode.\n"); + } + #endif + // Apply the dark theme preference to GTK + gtk_settings->property_gtk_application_prefer_dark_theme().set_value(use_dark_theme); + debug_out_dump("app", "GTK dark theme preference set to: " << (use_dark_theme ? "dark" : "light") << "\n"); + } + } + + // The application is dpi-aware in Windows. // However, Gtk3 does not support fractional scaling, so at 250% scaling in system settings, the UI will use 200%. // diff --git a/src/gui/gsc_main_window_iconview.cpp b/src/gui/gsc_main_window_iconview.cpp index dcb1057..98e47cc 100644 --- a/src/gui/gsc_main_window_iconview.cpp +++ b/src/gui/gsc_main_window_iconview.cpp @@ -120,7 +120,7 @@ bool GscMainWindowIconView::on_draw(const Cairo::RefPtr& cr) return true; } if (empty_view_message_ != Message::None && this->num_icons_ == 0) { // no icons - Glib::RefPtr layout = this->create_pango_layout(""); + const Glib::RefPtr layout = this->create_pango_layout(""); layout->set_alignment(Pango::ALIGN_CENTER); layout->set_markup(get_message_string(empty_view_message_)); @@ -131,6 +131,12 @@ bool GscMainWindowIconView::on_draw(const Cairo::RefPtr& cr) const int pos_y = (get_allocation().get_height() - layout_h) / 2; cr->move_to(pos_x, pos_y); + // Use the foreground color from the widget's style context so + // the text is visible in both light and dark themes. + const auto style_context = get_style_context(); + const Gdk::RGBA fg_color = style_context->get_color(style_context->get_state()); + cr->set_source_rgba(fg_color.get_red(), fg_color.get_green(), fg_color.get_blue(), fg_color.get_alpha()); + layout->show_in_cairo_context(cr); return true; diff --git a/src/hz/win32_tools.h b/src/hz/win32_tools.h index 73beeae..b33d9db 100644 --- a/src/hz/win32_tools.h +++ b/src/hz/win32_tools.h @@ -79,6 +79,14 @@ inline bool win32_set_registry_value_string(HKEY base, const std::string& keydir, const std::string& key, const std::string& value); +/// Get registry value as a DWORD. +/// Base may be e.g. HKEY_CURRENT_USER. +/// Note that this works only with REG_DWORD types. +/// False is returned for all other types. +inline bool win32_get_registry_value_dword(HKEY base, + const std::string& keydir, const std::string& key, DWORD& put_here); + + /// Redirect stdout and stderr to console window (if open). Requires winxp (at compile-time). /// \param create_if_none if true, create a new console if none was found and attach to it. /// \return false if failed or unsupported. @@ -340,6 +348,49 @@ inline bool win32_set_registry_value_string(HKEY base, +// Get registry value as a DWORD. +// Note that this works only with REG_DWORD types. +inline bool win32_get_registry_value_dword(HKEY base, + const std::string& keydir, const std::string& key, DWORD& put_here) +{ + std::wstring wkeydir = win32_utf8_to_utf16(keydir); + if (wkeydir.empty()) + return false; + + HKEY reg_key = nullptr; + bool open_status = (RegOpenKeyExW(base, wkeydir.c_str(), 0, KEY_QUERY_VALUE, ®_key) == ERROR_SUCCESS); + + if (!open_status) + return false; + + bool ok = false; + std::wstring wkey = win32_utf8_to_utf16(key, &ok); + if (!ok) { // conversion error. Note that an empty string is not an error. + if (reg_key) + RegCloseKey(reg_key); + return false; + } + + DWORD type = 0; + DWORD value = 0; + DWORD nbytes = sizeof(DWORD); + bool status = (RegQueryValueExW(reg_key, wkey.c_str(), nullptr, &type, + reinterpret_cast(&value), &nbytes) == ERROR_SUCCESS); + + if (status && type == REG_DWORD) { + put_here = value; + } else { + status = false; + } + + if (reg_key) + RegCloseKey(reg_key); + + return status; +} + + + // Redirect stdout and stderr to console window (if open). inline bool win32_redirect_stdio_to_console(bool create_if_none) { From ef30450c5f8a34946b1cd353d46dd49f12709b2c Mon Sep 17 00:00:00 2001 From: "anthropic-code-agent[bot]" <242468646+Claude@users.noreply.github.com> Date: Wed, 4 Mar 2026 21:50:41 +0000 Subject: [PATCH 08/20] Initial plan From 6de9bb897d8d204355334fd2698efb35ace16d35 Mon Sep 17 00:00:00 2001 From: "anthropic-code-agent[bot]" <242468646+Claude@users.noreply.github.com> Date: Wed, 4 Mar 2026 21:56:40 +0000 Subject: [PATCH 09/20] Implement theme-aware red text colors for dark theme legibility Co-authored-by: ashaduri <2302268+ashaduri@users.noreply.github.com> --- src/applib/warning_colors.h | 35 ++++++++++++++++++++++++++++++++--- src/gui/gsc_info_window.cpp | 5 ++++- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/applib/warning_colors.h b/src/applib/warning_colors.h index 1dd92f9..8ebc614 100644 --- a/src/applib/warning_colors.h +++ b/src/applib/warning_colors.h @@ -13,10 +13,37 @@ Copyright: #define WARNING_COLORS_H #include +#include #include "storage_property.h" +/// Check if a dark GTK theme is currently active +inline bool is_dark_theme_active() +{ + // Try to get the GTK settings to check for dark theme preference + Glib::RefPtr settings = Gtk::Settings::get_default(); + if (settings) { + // Check if the application prefers dark theme + bool prefer_dark = false; + settings->get_property("gtk-application-prefer-dark-theme", prefer_dark); + if (prefer_dark) { + return true; + } + + // Check theme name for common dark theme identifiers + Glib::ustring theme_name; + settings->get_property("gtk-theme-name", theme_name); + std::string theme_str = theme_name.lowercase(); + if (theme_str.find("dark") != std::string::npos || + theme_str.find("black") != std::string::npos) { + return true; + } + } + + return false; +} + /// Get colors for tree rows according to warning severity. /// \return true if the colors were changed. @@ -45,14 +72,16 @@ inline bool app_property_get_row_highlight_colors(WarningLevel warning, std::str /// \return true if the color was changed. inline bool app_property_get_label_highlight_color(WarningLevel warning, std::string& fg) { + bool dark_theme = is_dark_theme_active(); + if (warning == WarningLevel::Notice) { - fg = "#770000"; // very dark red + fg = dark_theme ? "#FF9999" : "#770000"; // lighter red for dark themes, very dark red for light themes } else if (warning == WarningLevel::Warning) { - fg = "#C00000"; // dark red + fg = dark_theme ? "#FF6666" : "#C00000"; // lighter red for dark themes, dark red for light themes } else if (warning == WarningLevel::Alert) { - fg = "#FF0000"; // red + fg = dark_theme ? "#FF4444" : "#FF0000"; // lighter/pink red for dark themes, bright red for light themes } return !(fg.empty()); diff --git a/src/gui/gsc_info_window.cpp b/src/gui/gsc_info_window.cpp index ae32000..d3d217b 100644 --- a/src/gui/gsc_info_window.cpp +++ b/src/gui/gsc_info_window.cpp @@ -2444,7 +2444,10 @@ gboolean GscInfoWindow::test_idle_callback(void* data) break; case SelfTestStatusSeverity::Error: if (!result_main_msg.empty()) { // Highlight in red - result_main_msg = ""s + result_main_msg + ""; + std::string alert_color; + // Use the same color as Alert level warnings for consistency + app_property_get_label_highlight_color(WarningLevel::Alert, alert_color); + result_main_msg = ""s + result_main_msg + ""; } result_details_msg += "\n"s + _("Check the Self-Test Log for more information."); break; From a6e2dd0cc6427078f38f058c58898aeb403aa0d5 Mon Sep 17 00:00:00 2001 From: "anthropic-code-agent[bot]" <242468646+Claude@users.noreply.github.com> Date: Fri, 6 Mar 2026 14:36:54 +0000 Subject: [PATCH 10/20] Add GTK init guard and optimize theme detection - Guard theme detection with Gtk::Main::level() check to avoid crashes in non-GUI executables - Use narrow headers (gtkmm/settings.h, gtkmm/main.h) instead of umbrella gtkmm.h - Return early from app_property_get_label_highlight_color() for WarningLevel::None Co-authored-by: ashaduri <2302268+ashaduri@users.noreply.github.com> --- src/applib/warning_colors.h | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/applib/warning_colors.h b/src/applib/warning_colors.h index 8ebc614..cc13052 100644 --- a/src/applib/warning_colors.h +++ b/src/applib/warning_colors.h @@ -13,7 +13,8 @@ Copyright: #define WARNING_COLORS_H #include -#include +#include +#include #include "storage_property.h" @@ -21,6 +22,12 @@ Copyright: /// Check if a dark GTK theme is currently active inline bool is_dark_theme_active() { + // Only check GTK settings if GTK has been initialized + // This avoids crashes/warnings in non-GUI executables + if (Gtk::Main::level() == 0) { + return false; // Default to light theme if GTK not initialized + } + // Try to get the GTK settings to check for dark theme preference Glib::RefPtr settings = Gtk::Settings::get_default(); if (settings) { @@ -72,6 +79,11 @@ inline bool app_property_get_row_highlight_colors(WarningLevel warning, std::str /// \return true if the color was changed. inline bool app_property_get_label_highlight_color(WarningLevel warning, std::string& fg) { + // Return early for None to avoid unnecessary theme detection + if (warning == WarningLevel::None) { + return false; + } + bool dark_theme = is_dark_theme_active(); if (warning == WarningLevel::Notice) { From c322e967410b2231d6f490eb2d51eef52892bdab Mon Sep 17 00:00:00 2001 From: "anthropic-code-agent[bot]" <242468646+Claude@users.noreply.github.com> Date: Fri, 6 Mar 2026 19:08:06 +0000 Subject: [PATCH 11/20] Revert to umbrella gtkmm.h include per maintainer request Co-authored-by: ashaduri <2302268+ashaduri@users.noreply.github.com> --- src/applib/warning_colors.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/applib/warning_colors.h b/src/applib/warning_colors.h index cc13052..3f5157f 100644 --- a/src/applib/warning_colors.h +++ b/src/applib/warning_colors.h @@ -13,8 +13,7 @@ Copyright: #define WARNING_COLORS_H #include -#include -#include +#include #include "storage_property.h" From 06a0a753497a542c7723aa5388ea710e940681c5 Mon Sep 17 00:00:00 2001 From: Alexander Shaduri Date: Sat, 7 Mar 2026 16:15:24 +0100 Subject: [PATCH 12/20] Update src/gui/gsc_info_window.cpp Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/gui/gsc_info_window.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/gui/gsc_info_window.cpp b/src/gui/gsc_info_window.cpp index d3d217b..8530a04 100644 --- a/src/gui/gsc_info_window.cpp +++ b/src/gui/gsc_info_window.cpp @@ -2446,8 +2446,9 @@ gboolean GscInfoWindow::test_idle_callback(void* data) if (!result_main_msg.empty()) { // Highlight in red std::string alert_color; // Use the same color as Alert level warnings for consistency - app_property_get_label_highlight_color(WarningLevel::Alert, alert_color); - result_main_msg = ""s + result_main_msg + ""; + if (app_property_get_label_highlight_color(WarningLevel::Alert, alert_color) && !alert_color.empty()) { + result_main_msg = ""s + result_main_msg + ""; + } } result_details_msg += "\n"s + _("Check the Self-Test Log for more information."); break; From e4c9afeed0213aef83fda7b449bf56985e38355f Mon Sep 17 00:00:00 2001 From: Alexander Shaduri Date: Sat, 7 Mar 2026 17:39:35 +0100 Subject: [PATCH 13/20] Update src/applib/warning_colors.h Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/applib/warning_colors.h | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/applib/warning_colors.h b/src/applib/warning_colors.h index 3f5157f..3f75cea 100644 --- a/src/applib/warning_colors.h +++ b/src/applib/warning_colors.h @@ -21,13 +21,8 @@ Copyright: /// Check if a dark GTK theme is currently active inline bool is_dark_theme_active() { - // Only check GTK settings if GTK has been initialized - // This avoids crashes/warnings in non-GUI executables - if (Gtk::Main::level() == 0) { - return false; // Default to light theme if GTK not initialized - } - - // Try to get the GTK settings to check for dark theme preference + // Try to get the GTK settings to check for dark theme preference. + // If GTK is not available or not initialized, get_default() will return null. Glib::RefPtr settings = Gtk::Settings::get_default(); if (settings) { // Check if the application prefers dark theme From 3642afc46f2e1f575e4201eb6ec5883a28e63a68 Mon Sep 17 00:00:00 2001 From: Alexander Shaduri Date: Sun, 8 Mar 2026 18:51:24 +0100 Subject: [PATCH 14/20] Use readable text highlight colors in dark mode. --- src/applib/CMakeLists.txt | 1 + src/applib/gui_utils.cpp | 23 ++++++++ src/applib/gui_utils.h | 4 +- src/applib/warning_colors.cpp | 88 ++++++++++++++++++++++++++++ src/applib/warning_colors.h | 106 ++-------------------------------- src/gui/gsc_info_window.cpp | 12 ++-- src/gui/gsc_main_window.cpp | 2 +- 7 files changed, 128 insertions(+), 108 deletions(-) create mode 100644 src/applib/warning_colors.cpp diff --git a/src/applib/CMakeLists.txt b/src/applib/CMakeLists.txt index c70702b..ba8b558 100644 --- a/src/applib/CMakeLists.txt +++ b/src/applib/CMakeLists.txt @@ -72,6 +72,7 @@ target_sources(applib PRIVATE storage_property_repository.cpp storage_property_repository.h storage_settings.h + warning_colors.cpp warning_colors.h warning_level.h window_instance_manager.h diff --git a/src/applib/gui_utils.cpp b/src/applib/gui_utils.cpp index 0eee330..50a8364 100644 --- a/src/applib/gui_utils.cpp +++ b/src/applib/gui_utils.cpp @@ -165,6 +165,29 @@ bool gui_show_text_entry_dialog(const std::string& title, const std::string& mes +bool gui_is_dark_theme_active() +{ + // Try to get the GTK settings to check for dark theme preference. + // If GTK is not available or not initialized, get_default() will return null. + const Glib::RefPtr settings = Gtk::Settings::get_default(); + if (settings) { + // Check if the application prefers dark theme + if (settings->property_gtk_application_prefer_dark_theme().get_value()) { + return true; + } + + // Check theme name for common dark theme identifiers + Glib::ustring theme_name; + settings->get_property("gtk-theme-name", theme_name); + const std::string theme_str = theme_name.lowercase(); + if (theme_str.find("dark") != std::string::npos || + theme_str.find("black") != std::string::npos) { + return true; + } + } + + return false; +} diff --git a/src/applib/gui_utils.h b/src/applib/gui_utils.h index f7ee726..b6f06dc 100644 --- a/src/applib/gui_utils.h +++ b/src/applib/gui_utils.h @@ -58,8 +58,8 @@ bool gui_show_text_entry_dialog(const std::string& title, const std::string& mes std::string& result, const std::string& default_str, Gtk::Window* parent = nullptr, bool sec_msg_markup = false); - - +/// Check if a dark GTK theme is currently active +bool gui_is_dark_theme_active(); #endif diff --git a/src/applib/warning_colors.cpp b/src/applib/warning_colors.cpp new file mode 100644 index 0000000..b648472 --- /dev/null +++ b/src/applib/warning_colors.cpp @@ -0,0 +1,88 @@ +/****************************************************************************** +License: GNU General Public License v3.0 only +Copyright: + (C) 2008 - 2026 Alexander Shaduri +******************************************************************************/ +/// \file +/// \author Alexander Shaduri +/// \ingroup applib +/// \weakgroup applib +/// @{ + +#include + +#include "warning_colors.h" +#include "gui_utils.h" + + +bool app_property_get_row_highlight_colors(bool dark_mode, WarningLevel warning, std::string& fg, std::string& bg) +{ + // Note: we're setting both fg and bg, to avoid theme conflicts. + if (warning == WarningLevel::Notice) { + fg = dark_mode ? "#FFFFFF" : "#000000"; // white for dark themes, black for light themes + bg = dark_mode ? "#6B2050" : "#FFD5EE"; // dark pinkish for dark themes, pinkish for light themes + + } else if (warning == WarningLevel::Warning) { + fg = dark_mode ? "#FFFFFF" : "#000000"; // white for dark themes, black for light themes + bg = dark_mode ? "#802020" : "#FFA0A0"; // dark red for dark themes, light red for light themes + + } else if (warning == WarningLevel::Alert) { + fg = dark_mode ? "#FFFFFF" : "#000000"; // white for dark themes, black for light themes + bg = dark_mode ? "#AA0000" : "#FF0000"; // darker red for dark themes, bright red for light themes + } + + return !(fg.empty()); +} + + + +bool app_property_get_label_highlight_color(bool dark_mode, WarningLevel warning, std::string& fg) +{ + if (warning == WarningLevel::None) { + return false; + } + + if (warning == WarningLevel::Notice) { + fg = dark_mode ? "#FF9999" : "#770000"; // lighter red for dark themes, very dark red for light themes + + } else if (warning == WarningLevel::Warning) { + fg = dark_mode ? "#FF6666" : "#C00000"; // lighter red for dark themes, dark red for light themes + + } else if (warning == WarningLevel::Alert) { + fg = dark_mode ? "#FF4444" : "#FF0000"; // lighter/pink red for dark themes, bright red for light themes + } + + return !(fg.empty()); +} + + + +std::string storage_property_get_warning_reason(const StorageProperty& p) +{ + std::string fg, start = "", stop = ""; + if (app_property_get_label_highlight_color(gui_is_dark_theme_active(), p.warning_level, fg)) { + start += ""; + stop = "" + stop; + } + + switch (p.warning_level) { + case WarningLevel::None: + // nothing + break; + case WarningLevel::Notice: + /// Translators: %1 and %2 are HTML tags, %3 is a message. + return Glib::ustring::compose(_("%1Notice:%2 %3"), start, stop, Glib::Markup::escape_text(p.warning_reason)); + case WarningLevel::Warning: + /// Translators: %1 and %2 are HTML tags, %3 is a message. + return Glib::ustring::compose(_("%1Warning:%2 %3"), start, stop, Glib::Markup::escape_text(p.warning_reason)); + case WarningLevel::Alert: + /// Translators: %1 and %2 are HTML tags, %3 is a message. + return Glib::ustring::compose(_("%1ALERT:%2 %3"), start, stop, Glib::Markup::escape_text(p.warning_reason)); + } + + return {}; +} + + + +/// @} diff --git a/src/applib/warning_colors.h b/src/applib/warning_colors.h index 3f75cea..eea3e9d 100644 --- a/src/applib/warning_colors.h +++ b/src/applib/warning_colors.h @@ -1,7 +1,7 @@ /****************************************************************************** License: GNU General Public License v3.0 only Copyright: - (C) 2008 - 2021 Alexander Shaduri + (C) 2008 - 2026 Alexander Shaduri ******************************************************************************/ /// \file /// \author Alexander Shaduri @@ -12,118 +12,24 @@ Copyright: #ifndef WARNING_COLORS_H #define WARNING_COLORS_H -#include -#include +#include #include "storage_property.h" - - -/// Check if a dark GTK theme is currently active -inline bool is_dark_theme_active() -{ - // Try to get the GTK settings to check for dark theme preference. - // If GTK is not available or not initialized, get_default() will return null. - Glib::RefPtr settings = Gtk::Settings::get_default(); - if (settings) { - // Check if the application prefers dark theme - bool prefer_dark = false; - settings->get_property("gtk-application-prefer-dark-theme", prefer_dark); - if (prefer_dark) { - return true; - } - - // Check theme name for common dark theme identifiers - Glib::ustring theme_name; - settings->get_property("gtk-theme-name", theme_name); - std::string theme_str = theme_name.lowercase(); - if (theme_str.find("dark") != std::string::npos || - theme_str.find("black") != std::string::npos) { - return true; - } - } - - return false; -} +#include "warning_level.h" /// Get colors for tree rows according to warning severity. /// \return true if the colors were changed. -inline bool app_property_get_row_highlight_colors(WarningLevel warning, std::string& fg, std::string& bg) -{ - // Note: we're setting both fg and bg, to avoid theme conflicts. - if (warning == WarningLevel::Notice) { - fg = "#000000"; // black - bg = "#FFD5EE"; // pinkish - - } else if (warning == WarningLevel::Warning) { - fg = "#000000"; // black - bg = "#FFA0A0"; // even more pinkish - - } else if (warning == WarningLevel::Alert) { - fg = "#000000"; // black - bg = "#FF0000"; // red - } - - return !(fg.empty()); -} - +bool app_property_get_row_highlight_colors(bool dark_mode, WarningLevel warning, std::string& fg, std::string& bg); /// Get color for labels according to warning severity. /// \return true if the color was changed. -inline bool app_property_get_label_highlight_color(WarningLevel warning, std::string& fg) -{ - // Return early for None to avoid unnecessary theme detection - if (warning == WarningLevel::None) { - return false; - } - - bool dark_theme = is_dark_theme_active(); - - if (warning == WarningLevel::Notice) { - fg = dark_theme ? "#FF9999" : "#770000"; // lighter red for dark themes, very dark red for light themes - - } else if (warning == WarningLevel::Warning) { - fg = dark_theme ? "#FF6666" : "#C00000"; // lighter red for dark themes, dark red for light themes - - } else if (warning == WarningLevel::Alert) { - fg = dark_theme ? "#FF4444" : "#FF0000"; // lighter/pink red for dark themes, bright red for light themes - } - - return !(fg.empty()); -} - - +bool app_property_get_label_highlight_color(bool dark_mode, WarningLevel warning, std::string& fg); /// Format warning text, but without description -inline std::string storage_property_get_warning_reason(const StorageProperty& p) -{ - std::string fg, start = "", stop = ""; - if (app_property_get_label_highlight_color(p.warning_level, fg)) { - start += ""; - stop = "" + stop; - } - - switch (p.warning_level) { - case WarningLevel::None: - // nothing - break; - case WarningLevel::Notice: - /// Translators: %1 and %2 are HTML tags, %3 is a message. - return Glib::ustring::compose(_("%1Notice:%2 %3"), start, stop, Glib::Markup::escape_text(p.warning_reason)); - case WarningLevel::Warning: - /// Translators: %1 and %2 are HTML tags, %3 is a message. - return Glib::ustring::compose(_("%1Warning:%2 %3"), start, stop, Glib::Markup::escape_text(p.warning_reason)); - case WarningLevel::Alert: - /// Translators: %1 and %2 are HTML tags, %3 is a message. - return Glib::ustring::compose(_("%1ALERT:%2 %3"), start, stop, Glib::Markup::escape_text(p.warning_reason)); - } - - return {}; -} - - +std::string storage_property_get_warning_reason(const StorageProperty& p); diff --git a/src/gui/gsc_info_window.cpp b/src/gui/gsc_info_window.cpp index 8530a04..75d8007 100644 --- a/src/gui/gsc_info_window.cpp +++ b/src/gui/gsc_info_window.cpp @@ -81,6 +81,7 @@ namespace { // vbox->pack_start(*label, false, false); } else { + const bool dark_mode = gui_is_dark_theme_active(); // add one label per element for (const auto& label_string : label_strings) { @@ -95,7 +96,7 @@ namespace { label->set_can_focus(false); std::string fg; - if (app_property_get_label_highlight_color(label_string.property->warning_level, fg)) { + if (app_property_get_label_highlight_color(dark_mode, label_string.property->warning_level, fg)) { label->set_markup( std::string("") .append(label_text).append("") ); @@ -129,7 +130,7 @@ namespace { } std::string fg; - if (app_property_get_label_highlight_color(warning, fg)) + if (app_property_get_label_highlight_color(gui_is_dark_theme_active(), warning, fg)) label->set_markup_with_mnemonic("" + original_label + ""); } @@ -1038,6 +1039,7 @@ void GscInfoWindow::fill_ui_general(const StoragePropertyRepository& property_re identity_table->hide(); WarningLevel max_tab_warning = WarningLevel::None; + const bool dark_mode = gui_is_dark_theme_active(); int row = 0; for (auto&& p : general_props) { @@ -1070,7 +1072,7 @@ void GscInfoWindow::fill_ui_general(const StoragePropertyRepository& property_re value->set_markup(Glib::Markup::escape_text(p.format_value())); std::string fg; - if (app_property_get_label_highlight_color(p.warning_level, fg)) { + if (app_property_get_label_highlight_color(dark_mode, p.warning_level, fg)) { name->set_markup("" + name->get_label() + ""); value->set_markup("" + value->get_label() + ""); } @@ -2067,7 +2069,7 @@ WarningLevel GscInfoWindow::fill_ui_directory(const StoragePropertyRepository& p inline void cell_renderer_set_warning_fg_bg(Gtk::CellRendererText* crt, const StorageProperty& p) { std::string fg, bg; - if (app_property_get_row_highlight_colors(p.warning_level, fg, bg)) { + if (app_property_get_row_highlight_colors(gui_is_dark_theme_active(), p.warning_level, fg, bg)) { // Note: property_cell_background makes horizontal tree lines disappear around it, // but property_background doesn't play nice with sorted column color. crt->property_cell_background() = bg; @@ -2446,7 +2448,7 @@ gboolean GscInfoWindow::test_idle_callback(void* data) if (!result_main_msg.empty()) { // Highlight in red std::string alert_color; // Use the same color as Alert level warnings for consistency - if (app_property_get_label_highlight_color(WarningLevel::Alert, alert_color) && !alert_color.empty()) { + if (app_property_get_label_highlight_color(gui_is_dark_theme_active(), WarningLevel::Alert, alert_color) && !alert_color.empty()) { result_main_msg = ""s + result_main_msg + ""; } } diff --git a/src/gui/gsc_main_window.cpp b/src/gui/gsc_main_window.cpp index f784f39..b25873b 100644 --- a/src/gui/gsc_main_window.cpp +++ b/src/gui/gsc_main_window.cpp @@ -774,7 +774,7 @@ void GscMainWindow::update_status_widgets() if (health_prop.generic_name == "smart_status/passed") { health_label_->set_text(health_prop.format_value()); std::string fg; - if (app_property_get_label_highlight_color(health_prop.warning_level, fg)) { + if (app_property_get_label_highlight_color(gui_is_dark_theme_active(), health_prop.warning_level, fg)) { health_label_->set_markup(""+ Glib::Markup::escape_text(health_label_->get_text()) + ""); } // don't set description tooltip - we already have the basic one. From 57a16f697184d54b1ded3b7d09908f2e15690ba4 Mon Sep 17 00:00:00 2001 From: Alexander Shaduri Date: Sun, 8 Mar 2026 19:08:51 +0100 Subject: [PATCH 15/20] Fix compilation. --- src/gui/gsc_info_window.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gui/gsc_info_window.cpp b/src/gui/gsc_info_window.cpp index a5afc7c..75d8007 100644 --- a/src/gui/gsc_info_window.cpp +++ b/src/gui/gsc_info_window.cpp @@ -2448,7 +2448,7 @@ gboolean GscInfoWindow::test_idle_callback(void* data) if (!result_main_msg.empty()) { // Highlight in red std::string alert_color; // Use the same color as Alert level warnings for consistency - if (app_property_get_label_highlight_color(WarningLevel::Alert, alert_color) && !alert_color.empty()) { + if (app_property_get_label_highlight_color(gui_is_dark_theme_active(), WarningLevel::Alert, alert_color) && !alert_color.empty()) { result_main_msg = ""s + result_main_msg + ""; } } From 19c50fdd72d35d41ff08a1be63b8a3a25fd818b0 Mon Sep 17 00:00:00 2001 From: Alexander Shaduri Date: Sun, 31 May 2026 11:54:47 +0200 Subject: [PATCH 16/20] Add scam alert and official download links to README Added a scam alert about a phishing site and provided official download links. --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 2820400..d74848f 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,12 @@ --- +**SCAM ALERT:** gsmartcontrol[.]com is a phishing site! Please don't download anything from there! + +Please always download GSmartControl from its official website [gsmartcontrol.shaduri.dev](https://gsmartcontrol.shaduri.dev) or the official GitHub repository [github.com/ashaduri/gsmartcontrol](https://github.com/ashaduri/gsmartcontrol). + +--- + [GSmartControl](https://gsmartcontrol.shaduri.dev) is a graphical user interface for smartctl (from [smartmontools](https://www.smartmontools.org/) package), which is a tool for From cab21bd6dd5f6c1fdcb3ea0b9dca98f7ff271227 Mon Sep 17 00:00:00 2001 From: Alexander Shaduri Date: Sun, 31 May 2026 11:55:43 +0200 Subject: [PATCH 17/20] Add scam alert and download instructions to index.md Added a scam alert regarding a phishing site and provided guidance on downloading GSmartControl from official sources. --- docs/index.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docs/index.md b/docs/index.md index 571e774..3037801 100644 --- a/docs/index.md +++ b/docs/index.md @@ -13,6 +13,12 @@ nav_order: 1 --- +**SCAM ALERT:** gsmartcontrol[.]com is a phishing site! Please don't download anything from there! + +Please always download GSmartControl from its official website [gsmartcontrol.shaduri.dev](https://gsmartcontrol.shaduri.dev) or the official GitHub repository [github.com/ashaduri/gsmartcontrol](https://github.com/ashaduri/gsmartcontrol). + +--- + [GSmartControl](https://gsmartcontrol.shaduri.dev) is a graphical user interface for smartctl (from [smartmontools](https://www.smartmontools.org/) package), which is a tool for From fa31a696a704332eb9112437b2a08958ff156d28 Mon Sep 17 00:00:00 2001 From: OldMan2525 <81668787+OldMan2525@users.noreply.github.com> Date: Tue, 7 Jul 2026 02:13:11 -0700 Subject: [PATCH 18/20] Use bundled icons when theme icons are too small Some icon themes may return a lower-resolution icon than requested. For example, under XQuartz on macOS, Gtk::IconTheme::load_icon() returns a 16x16 drive-harddisk icon even when a 64x64 icon is requested. Because the theme lookup succeeds, the bundled higher-resolution icons are never used, resulting in tiny drive icons in the main window. Fall back to the bundled icon when the theme-provided icon is smaller than the requested size. --- src/gui/gsc_main_window_iconview.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/gui/gsc_main_window_iconview.cpp b/src/gui/gsc_main_window_iconview.cpp index 98e47cc..6e78c5a 100644 --- a/src/gui/gsc_main_window_iconview.cpp +++ b/src/gui/gsc_main_window_iconview.cpp @@ -554,10 +554,18 @@ Glib::RefPtr GscMainWindowIconView::load_icon_pixbuf(Glib::RefPtr icon; - // Try XDG version first + // Try the icon theme first; fall back to the bundled icon if needed. try { if (default_icon_theme && !xdg_icon_name.empty()) { icon = default_icon_theme->load_icon(xdg_icon_name, icon_size_, get_scale_factor(), Gtk::IconLookupFlags(0)); + + // Some icon themes may return an icon smaller than requested. + // In that case, fall back to the bundled icon. + if (icon && + (icon->get_width() < icon_size_ || + icon->get_height() < icon_size_)) { + icon.reset(); + } } } catch (...) { } // ignore exceptions From cb5b1284bef7f88f0ee296e435b3fc69dacaec19 Mon Sep 17 00:00:00 2001 From: Alexander Shaduri Date: Thu, 15 Oct 2026 22:43:13 +0200 Subject: [PATCH 19/20] Adjust default window sizes for fractional scaling on Windows GTK3 only supports integer scaling, so at e.g. 125% or 250% system scaling the UI is rendered at 1x / 2x. Store the full system scale percentage and scale the default (glade) window sizes by the remaining fractional ratio, and increase the font size by the fractional amount. Co-Authored-By: Claude Opus 5.5 --- src/applib/gui_utils.cpp | 57 ++++++ src/applib/gui_utils.h | 37 ++++ src/gui/CMakeLists.txt | 1 + src/gui/gsc_executor_log_window.cpp | 4 + src/gui/gsc_info_window.cpp | 4 +- src/gui/gsc_init.cpp | 14 +- src/gui/gsc_main_window.cpp | 11 +- src/gui/gsc_text_window.h | 4 + src/gui/tests/CMakeLists.txt | 21 +++ src/gui/tests/test_fractional_scaling.cpp | 216 ++++++++++++++++++++++ src/test_all/CMakeLists.txt | 1 + 11 files changed, 357 insertions(+), 13 deletions(-) create mode 100644 src/gui/tests/CMakeLists.txt create mode 100644 src/gui/tests/test_fractional_scaling.cpp diff --git a/src/applib/gui_utils.cpp b/src/applib/gui_utils.cpp index 50a8364..7955e39 100644 --- a/src/applib/gui_utils.cpp +++ b/src/applib/gui_utils.cpp @@ -11,6 +11,8 @@ Copyright: #include "gui_utils.h" +#include + namespace { @@ -191,4 +193,59 @@ bool gui_is_dark_theme_active() +namespace { + + /// Storage for Windows fractional scaling percentage (0 if no fractional scaling) + int g_windows_fractional_scaling_percent = 0; + +} + + + +int app_get_windows_fractional_scaling_percent() +{ + return g_windows_fractional_scaling_percent; +} + + + +void app_set_windows_fractional_scaling_percent(int percent) +{ + g_windows_fractional_scaling_percent = percent; +} + + + +void app_apply_fractional_scaling_to_default_size(Gtk::Window* window, int config_size_w, int config_size_h) +{ + if (!window) { + return; + } + + int size_w = config_size_w; + int size_h = config_size_h; + + // Apply fractional scaling adjustment on Windows if no custom size is configured + // This compensates for GTK3's lack of fractional scaling support + const int full_percent = g_windows_fractional_scaling_percent; + if (full_percent > 0 && size_w == 0 && size_h == 0) { + // Get the default size from glade and scale it + int glade_w = 0, glade_h = 0; + window->get_default_size(glade_w, glade_h); + if (glade_w > 0 && glade_h > 0) { + const double system_scale = static_cast(full_percent) / 100.0; + const int integer_ui_scale = full_percent / 100; // GTK3 uses floor (integer) scaling + const double correction = system_scale / static_cast(integer_ui_scale); + size_w = static_cast(std::lround(glade_w * correction)); + size_h = static_cast(std::lround(glade_h * correction)); + } + } + + if (size_w > 0 && size_h > 0) { + window->set_default_size(size_w, size_h); + } +} + + + /// @} diff --git a/src/applib/gui_utils.h b/src/applib/gui_utils.h index b6f06dc..0e951d4 100644 --- a/src/applib/gui_utils.h +++ b/src/applib/gui_utils.h @@ -61,6 +61,43 @@ bool gui_show_text_entry_dialog(const std::string& title, const std::string& mes /// Check if a dark GTK theme is currently active bool gui_is_dark_theme_active(); +/// Get the fractional scaling percentage detected on Windows (0 if not detected or integer scale). +/// For example, at 150% scaling, this returns 150; at 125% scaling, this returns 125; at 250% scaling, this returns 250. +/// Returns 0 for exact integer scales (100%, 200%, etc.). +int app_get_windows_fractional_scaling_percent(); + + +/// Set the fractional scaling percentage detected on Windows. +/// This should only be called once during application initialization. +void app_set_windows_fractional_scaling_percent(int percent); + + +/// Apply fractional scaling to default window size if fractional scaling is detected. +/// This compensates for GTK3's lack of fractional scaling support on Windows. +/// \param window The window to apply scaling to +/// \param config_size_w Configured width (0 if using glade default) +/// \param config_size_h Configured height (0 if using glade default) +void app_apply_fractional_scaling_to_default_size(Gtk::Window* window, int config_size_w, int config_size_h); + +/// Get the fractional scaling percentage detected on Windows (0 if not detected or integer scale). +/// For example, at 150% scaling, this returns 150; at 125% scaling, this returns 125; at 250% scaling, this returns 250. +/// Returns 0 for exact integer scales (100%, 200%, etc.). +int app_get_windows_fractional_scaling_percent(); + + +/// Set the fractional scaling percentage detected on Windows. +/// This should only be called once during application initialization. +void app_set_windows_fractional_scaling_percent(int percent); + + +/// Apply fractional scaling to default window size if fractional scaling is detected. +/// This compensates for GTK3's lack of fractional scaling support on Windows. +/// \param window The window to apply scaling to +/// \param config_size_w Configured width (0 if using glade default) +/// \param config_size_h Configured height (0 if using glade default) +void app_apply_fractional_scaling_to_default_size(Gtk::Window* window, int config_size_w, int config_size_h); + + #endif diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt index 63aa790..0b01d34 100644 --- a/src/gui/CMakeLists.txt +++ b/src/gui/CMakeLists.txt @@ -68,4 +68,5 @@ endif() add_subdirectory(ui) +add_subdirectory(tests) diff --git a/src/gui/gsc_executor_log_window.cpp b/src/gui/gsc_executor_log_window.cpp index 55f04d1..55a8a16 100644 --- a/src/gui/gsc_executor_log_window.cpp +++ b/src/gui/gsc_executor_log_window.cpp @@ -19,6 +19,7 @@ Copyright: #include #include "applib/app_gtkmm_tools.h" // app_gtkmm_create_tree_view_column +#include "applib/gui_utils.h" #include "hz/fs.h" #include "rconfig/rconfig.h" @@ -31,6 +32,9 @@ Copyright: GscExecutorLogWindow::GscExecutorLogWindow(BaseObjectType* gtkcobj, Glib::RefPtr ui) : AppBuilderWidget(gtkcobj, std::move(ui)) { + // Apply fractional scaling to default window size + app_apply_fractional_scaling_to_default_size(this, 0, 0); + // Connect callbacks Gtk::Button* window_close_button = nullptr; diff --git a/src/gui/gsc_info_window.cpp b/src/gui/gsc_info_window.cpp index 75d8007..4336fdf 100644 --- a/src/gui/gsc_info_window.cpp +++ b/src/gui/gsc_info_window.cpp @@ -167,9 +167,7 @@ GscInfoWindow::GscInfoWindow(BaseObjectType* gtkcobj, Glib::RefPtr { const int def_size_w = rconfig::get_data("gui/info_window/default_size_w"); const int def_size_h = rconfig::get_data("gui/info_window/default_size_h"); - if (def_size_w > 0 && def_size_h > 0) { - set_default_size(def_size_w, def_size_h); - } + app_apply_fractional_scaling_to_default_size(this, def_size_w, def_size_h); } // Create missing widgets diff --git a/src/gui/gsc_init.cpp b/src/gui/gsc_init.cpp index de288ef..a99dd2e 100644 --- a/src/gui/gsc_init.cpp +++ b/src/gui/gsc_init.cpp @@ -47,6 +47,7 @@ #include "applib/window_instance_manager.h" #include "applib/gsc_settings.h" +#include "applib/gui_utils.h" // app_set_windows_fractional_scaling_percent() #include "gsc_main_window.h" #include "gsc_executor_log_window.h" #include "gsc_init.h" @@ -80,6 +81,7 @@ namespace { return channel; } + } @@ -545,10 +547,14 @@ bool app_init_and_loop(int& argc, char**& argv) if (h_ppi > 0) { const double scale = h_ppi / 96.0; debug_out_info("app", "Windows system DPI: " << h_ppi << ", scale: " << scale << "\n"); - const int fraction_percent = static_cast(std::round(scale * 100)) % 100; - if (fraction_percent != 0) { // fractional scaling - // Increase the font size by fraction, but round down the size to match the Windows behavior (?) - debug_out_dump("app", "Fractional scaling detected, increasing font size by " << fraction_percent << "%.\n"); + const int full_scale_percent = static_cast(std::lround(scale * 100.0)); + const int integer_scale_percent = (full_scale_percent / 100) * 100; // 250 -> 200, 150 -> 100 + if (full_scale_percent != integer_scale_percent) { // fractional scaling detected + // Store the full scale percent for use in window sizing + app_set_windows_fractional_scaling_percent(full_scale_percent); + // Increase the font size by the fractional amount + const int fraction_percent = full_scale_percent - integer_scale_percent; + debug_out_dump("app", "Fractional scaling detected (" << full_scale_percent << "%), increasing font size by " << fraction_percent << "%.\n"); Gtk::Settings::get_default()->property_gtk_font_name() .set_value("Segoe UI " + hz::number_to_string_nolocale(static_cast(9 * (1. + fraction_percent/100.)))); } diff --git a/src/gui/gsc_main_window.cpp b/src/gui/gsc_main_window.cpp index b25873b..1db97cf 100644 --- a/src/gui/gsc_main_window.cpp +++ b/src/gui/gsc_main_window.cpp @@ -22,6 +22,7 @@ Copyright: #include "hz/launch_url.h" #include "hz/fs.h" #include "rconfig/rconfig.h" + #include "applib/storage_detector.h" #include "applib/gui_utils.h" // gui_show_error_dialog #include "applib/smartctl_executor.h" // get_smartctl_binary() @@ -30,6 +31,8 @@ Copyright: #include "applib/warning_colors.h" // app_property_get_label_highlight_color #include "applib/app_regex.h" #include "applib/smartctl_version_parser.h" +#include "applib/command_executor_factory.h" +#include "applib/storage_settings.h" #include "gsc_init.h" // app_quit() #include "gsc_about_dialog.h" @@ -37,14 +40,12 @@ Copyright: #include "gsc_preferences_window.h" #include "gsc_executor_log_window.h" #include "gsc_executor_error_dialog.h" // gsc_executor_error_dialog_show - #include "gsc_main_window_iconview.h" #include "gsc_main_window.h" #include "gsc_add_device_window.h" -#include "applib/command_executor_factory.h" #include "gsc_startup_settings.h" + #include "build_config.h" -#include "applib/storage_settings.h" using namespace std::literals; @@ -60,9 +61,7 @@ GscMainWindow::GscMainWindow(BaseObjectType* gtkcobj, Glib::RefPtr { const int def_size_w = rconfig::get_data("gui/main_window/default_size_w"); const int def_size_h = rconfig::get_data("gui/main_window/default_size_h"); - if (def_size_w > 0 && def_size_h > 0) { - set_default_size(def_size_w, def_size_h); - } + app_apply_fractional_scaling_to_default_size(this, def_size_w, def_size_h); } // show the window first, scan later diff --git a/src/gui/gsc_text_window.h b/src/gui/gsc_text_window.h index 3b1cf0e..4f39bb8 100644 --- a/src/gui/gsc_text_window.h +++ b/src/gui/gsc_text_window.h @@ -24,6 +24,7 @@ Copyright: #include "applib/app_builder_widget.h" #include "applib/app_gtkmm_tools.h" +#include "applib/gui_utils.h" // app_apply_fractional_scaling_to_default_size() @@ -49,6 +50,9 @@ class GscTextWindow : public AppBuilderWidget, Ins GscTextWindow(typename Gtk::Window::BaseObjectType* gtkcobj, Glib::RefPtr ui) : AppBuilderWidget, InstanceSwitch::multi_instance>(gtkcobj, std::move(ui)) { + // Apply fractional scaling to default window size + app_apply_fractional_scaling_to_default_size(this, 0, 0); + // Connect callbacks Gtk::Button* save_as_button = nullptr; diff --git a/src/gui/tests/CMakeLists.txt b/src/gui/tests/CMakeLists.txt new file mode 100644 index 0000000..3aefcff --- /dev/null +++ b/src/gui/tests/CMakeLists.txt @@ -0,0 +1,21 @@ +############################################################################### +# License: BSD Zero Clause License file +# Copyright: +# (C) 2026 Alexander Shaduri +############################################################################### + +if (NOT APP_BUILD_TESTS) + set_directory_properties(PROPERTIES EXCLUDE_FROM_ALL true) +else() + set_directory_properties(PROPERTIES EXCLUDE_FROM_ALL false) +endif() + + +# Use Object libraries to allow runtime test discovery +add_library(gui_tests OBJECT) +target_sources(gui_tests PRIVATE + test_fractional_scaling.cpp +) +target_link_libraries(gui_tests PRIVATE + Catch2 +) diff --git a/src/gui/tests/test_fractional_scaling.cpp b/src/gui/tests/test_fractional_scaling.cpp new file mode 100644 index 0000000..dfe8bee --- /dev/null +++ b/src/gui/tests/test_fractional_scaling.cpp @@ -0,0 +1,216 @@ +/****************************************************************************** +License: BSD Zero Clause License +Copyright: + (C) 2008 - 2026 Alexander Shaduri +******************************************************************************/ +/// \file +/// \author Alexander Shaduri +/// \ingroup gui_tests +/// \weakgroup gui_tests +/// @{ + +#include "catch2/catch.hpp" + +#include + + +/// Test the fractional scaling calculation logic +/// This tests the mathematical correctness of the scaling correction ratio +TEST_CASE("FractionalScalingCalculation", "[gui][scaling]") +{ + // Test the calculation logic that's used in app_apply_fractional_scaling_to_default_size() + // correction = system_scale / integer_ui_scale + + SECTION("125% scaling (fractional)") + { + const int full_percent = 125; + const double system_scale = static_cast(full_percent) / 100.0; // 1.25 + const int integer_ui_scale = full_percent / 100; // 1 + const double correction = system_scale / static_cast(integer_ui_scale); + + REQUIRE(system_scale == 1.25); + REQUIRE(integer_ui_scale == 1); + REQUIRE(correction == 1.25); + + // Test window scaling: 800x600 base should become 1000x750 + const int base_w = 800; + const int base_h = 600; + const int scaled_w = static_cast(std::lround(base_w * correction)); + const int scaled_h = static_cast(std::lround(base_h * correction)); + + REQUIRE(scaled_w == 1000); + REQUIRE(scaled_h == 750); + } + + SECTION("150% scaling (fractional)") + { + const int full_percent = 150; + const double system_scale = static_cast(full_percent) / 100.0; // 1.5 + const int integer_ui_scale = full_percent / 100; // 1 + const double correction = system_scale / static_cast(integer_ui_scale); + + REQUIRE(system_scale == 1.5); + REQUIRE(integer_ui_scale == 1); + REQUIRE(correction == 1.5); + + // Test window scaling: 800x600 base should become 1200x900 + const int base_w = 800; + const int base_h = 600; + const int scaled_w = static_cast(std::lround(base_w * correction)); + const int scaled_h = static_cast(std::lround(base_h * correction)); + + REQUIRE(scaled_w == 1200); + REQUIRE(scaled_h == 900); + } + + SECTION("175% scaling (fractional)") + { + const int full_percent = 175; + const double system_scale = static_cast(full_percent) / 100.0; // 1.75 + const int integer_ui_scale = full_percent / 100; // 1 + const double correction = system_scale / static_cast(integer_ui_scale); + + REQUIRE(system_scale == 1.75); + REQUIRE(integer_ui_scale == 1); + REQUIRE(correction == 1.75); + + // Test window scaling: 800x600 base should become 1400x1050 + const int base_w = 800; + const int base_h = 600; + const int scaled_w = static_cast(std::lround(base_w * correction)); + const int scaled_h = static_cast(std::lround(base_h * correction)); + + REQUIRE(scaled_w == 1400); + REQUIRE(scaled_h == 1050); + } + + SECTION("250% scaling (fractional) - Critical test case") + { + // This is the case that was broken before the fix + const int full_percent = 250; + const double system_scale = static_cast(full_percent) / 100.0; // 2.5 + const int integer_ui_scale = full_percent / 100; // 2 + const double correction = system_scale / static_cast(integer_ui_scale); + + REQUIRE(system_scale == 2.5); + REQUIRE(integer_ui_scale == 2); + REQUIRE(correction == 1.25); // NOT 1.5 (the old broken behavior) + + // Test window scaling: GTK already applies 2x, we need to apply 1.25x more + // For a base of 800x600, GTK makes it 1600x1200, we should scale to 2000x1500 + const int gtk_scaled_w = 1600; // After GTK's 2x integer scaling + const int gtk_scaled_h = 1200; + const int final_w = static_cast(std::lround(gtk_scaled_w * correction)); + const int final_h = static_cast(std::lround(gtk_scaled_h * correction)); + + REQUIRE(final_w == 2000); // 2.5x total = 800 * 2.5 + REQUIRE(final_h == 1500); // 2.5x total = 600 * 2.5 + } + + SECTION("225% scaling (fractional)") + { + const int full_percent = 225; + const double system_scale = static_cast(full_percent) / 100.0; // 2.25 + const int integer_ui_scale = full_percent / 100; // 2 + const double correction = system_scale / static_cast(integer_ui_scale); + + REQUIRE(system_scale == 2.25); + REQUIRE(integer_ui_scale == 2); + REQUIRE(correction == 1.125); + + // Test window scaling: 800x600 at 2x GTK = 1600x1200, with correction = 1800x1350 + const int gtk_scaled_w = 1600; + const int gtk_scaled_h = 1200; + const int final_w = static_cast(std::lround(gtk_scaled_w * correction)); + const int final_h = static_cast(std::lround(gtk_scaled_h * correction)); + + REQUIRE(final_w == 1800); + REQUIRE(final_h == 1350); + } + + SECTION("100% scaling (integer, no fractional)") + { + // Should not trigger fractional scaling at all (full_percent would be 0) + const int full_percent = 100; + const int integer_scale_percent = (full_percent / 100) * 100; + + REQUIRE(full_percent == integer_scale_percent); // No fractional component + } + + SECTION("200% scaling (integer, no fractional)") + { + // Should not trigger fractional scaling at all (full_percent would be 0) + const int full_percent = 200; + const int integer_scale_percent = (full_percent / 100) * 100; + + REQUIRE(full_percent == integer_scale_percent); // No fractional component + } +} + + +/// Test the DPI detection logic +TEST_CASE("DPIDetectionLogic", "[gui][scaling][dpi]") +{ + SECTION("Detect 125% scaling from DPI") + { + const int h_ppi = 120; // 120 DPI + const double scale = static_cast(h_ppi) / 96.0; + const int full_scale_percent = static_cast(std::lround(scale * 100.0)); + const int integer_scale_percent = (full_scale_percent / 100) * 100; + + REQUIRE(full_scale_percent == 125); + REQUIRE(integer_scale_percent == 100); + REQUIRE(full_scale_percent != integer_scale_percent); // Fractional scaling detected + } + + SECTION("Detect 150% scaling from DPI") + { + const int h_ppi = 144; // 144 DPI + const double scale = static_cast(h_ppi) / 96.0; + const int full_scale_percent = static_cast(std::lround(scale * 100.0)); + const int integer_scale_percent = (full_scale_percent / 100) * 100; + + REQUIRE(full_scale_percent == 150); + REQUIRE(integer_scale_percent == 100); + REQUIRE(full_scale_percent != integer_scale_percent); // Fractional scaling detected + } + + SECTION("Detect 250% scaling from DPI") + { + const int h_ppi = 240; // 240 DPI + const double scale = static_cast(h_ppi) / 96.0; + const int full_scale_percent = static_cast(std::lround(scale * 100.0)); + const int integer_scale_percent = (full_scale_percent / 100) * 100; + + REQUIRE(full_scale_percent == 250); + REQUIRE(integer_scale_percent == 200); + REQUIRE(full_scale_percent != integer_scale_percent); // Fractional scaling detected + } + + SECTION("No fractional scaling at 100% (96 DPI)") + { + const int h_ppi = 96; // 96 DPI (standard) + const double scale = static_cast(h_ppi) / 96.0; + const int full_scale_percent = static_cast(std::lround(scale * 100.0)); + const int integer_scale_percent = (full_scale_percent / 100) * 100; + + REQUIRE(full_scale_percent == 100); + REQUIRE(integer_scale_percent == 100); + REQUIRE(full_scale_percent == integer_scale_percent); // No fractional scaling + } + + SECTION("No fractional scaling at 200% (192 DPI)") + { + const int h_ppi = 192; // 192 DPI + const double scale = static_cast(h_ppi) / 96.0; + const int full_scale_percent = static_cast(std::lround(scale * 100.0)); + const int integer_scale_percent = (full_scale_percent / 100) * 100; + + REQUIRE(full_scale_percent == 200); + REQUIRE(integer_scale_percent == 200); + REQUIRE(full_scale_percent == integer_scale_percent); // No fractional scaling + } +} + + +/// @} diff --git a/src/test_all/CMakeLists.txt b/src/test_all/CMakeLists.txt index 7568e79..36b8a47 100644 --- a/src/test_all/CMakeLists.txt +++ b/src/test_all/CMakeLists.txt @@ -22,6 +22,7 @@ target_link_libraries(test_all PRIVATE libdebug applib_tests hz_tests + gui_tests Catch2 ) From 08d200af9405a4e84bc8c6b39acfa2754dbbb61b Mon Sep 17 00:00:00 2001 From: Alexander Shaduri Date: Thu, 15 Oct 2026 22:42:37 +0200 Subject: [PATCH 20/20] Scaling: Fix sub-100% scale handling, test real helpers - Treat scales below 100% as non-fractional, avoiding a division by zero in the correction ratio and a bogus font size increase. - Add app_get_fractional_scaling_part_percent() and use it in gsc_init. - Move scaling tests to applib_tests and test the actual helper functions instead of duplicated inline math; remove the gui_tests target. Co-Authored-By: Claude Opus 5.5 --- src/applib/gui_utils.cpp | 63 +++++- src/applib/gui_utils.h | 53 +++-- src/applib/tests/CMakeLists.txt | 1 + src/applib/tests/test_fractional_scaling.cpp | 138 ++++++++++++ src/gui/CMakeLists.txt | 1 - src/gui/gsc_init.cpp | 10 +- src/gui/tests/CMakeLists.txt | 21 -- src/gui/tests/test_fractional_scaling.cpp | 216 ------------------- src/test_all/CMakeLists.txt | 1 - 9 files changed, 233 insertions(+), 271 deletions(-) create mode 100644 src/applib/tests/test_fractional_scaling.cpp delete mode 100644 src/gui/tests/CMakeLists.txt delete mode 100644 src/gui/tests/test_fractional_scaling.cpp diff --git a/src/applib/gui_utils.cpp b/src/applib/gui_utils.cpp index 7955e39..8ce4b6a 100644 --- a/src/applib/gui_utils.cpp +++ b/src/applib/gui_utils.cpp @@ -12,6 +12,7 @@ Copyright: #include "gui_utils.h" #include +#include namespace { @@ -202,6 +203,59 @@ namespace { +bool app_is_fractional_scaling(int full_scale_percent) +{ + // Scales below 100% are not handled (GTK3 never scales below 1x). + return full_scale_percent > 100 && full_scale_percent % 100 != 0; +} + + + +int app_get_fractional_scaling_part_percent(int full_scale_percent) +{ + if (!app_is_fractional_scaling(full_scale_percent)) { + return 0; + } + return full_scale_percent % 100; // 250 -> 50, 125 -> 25 +} + + + +double app_compute_fractional_scaling_correction(int full_scale_percent) +{ + if (!app_is_fractional_scaling(full_scale_percent)) { + return 0.0; + } + const double system_scale = static_cast(full_scale_percent) / 100.0; + const int integer_ui_scale = full_scale_percent / 100; // GTK3 uses floor (integer) scaling; >= 1 here + return system_scale / static_cast(integer_ui_scale); +} + + + +std::pair app_apply_fractional_scaling_to_size(int base_w, int base_h, double correction) +{ + if (correction <= 0.0 || base_w <= 0 || base_h <= 0) { + return {base_w, base_h}; + } + return { + static_cast(std::lround(base_w * correction)), + static_cast(std::lround(base_h * correction)) + }; +} + + + +int app_compute_scaling_percent_from_dpi(double h_ppi) +{ + if (h_ppi <= 0.0) { + return 0; + } + return static_cast(std::lround(h_ppi / 96.0 * 100.0)); +} + + + int app_get_windows_fractional_scaling_percent() { return g_windows_fractional_scaling_percent; @@ -232,13 +286,8 @@ void app_apply_fractional_scaling_to_default_size(Gtk::Window* window, int confi // Get the default size from glade and scale it int glade_w = 0, glade_h = 0; window->get_default_size(glade_w, glade_h); - if (glade_w > 0 && glade_h > 0) { - const double system_scale = static_cast(full_percent) / 100.0; - const int integer_ui_scale = full_percent / 100; // GTK3 uses floor (integer) scaling - const double correction = system_scale / static_cast(integer_ui_scale); - size_w = static_cast(std::lround(glade_w * correction)); - size_h = static_cast(std::lround(glade_h * correction)); - } + const double correction = app_compute_fractional_scaling_correction(full_percent); + std::tie(size_w, size_h) = app_apply_fractional_scaling_to_size(glade_w, glade_h, correction); } if (size_w > 0 && size_h > 0) { diff --git a/src/applib/gui_utils.h b/src/applib/gui_utils.h index 0e951d4..6e4b6a2 100644 --- a/src/applib/gui_utils.h +++ b/src/applib/gui_utils.h @@ -15,9 +15,43 @@ Copyright: #include #include #include +#include +/// Determine whether a given full scale percentage represents fractional scaling. +/// Fractional scaling exists when the percentage is above 100 and not a multiple of 100. +/// Scales below 100% are never considered fractional (GTK3 does not scale below 1x). +/// \param full_scale_percent The full scale percentage (e.g., 125, 200, 250) +/// \return true if the scaling has a fractional component +bool app_is_fractional_scaling(int full_scale_percent); + + +/// Get the fractional part of a full scale percentage (e.g., 250 -> 50, 125 -> 25). +/// Returns 0 if the scaling is not fractional (see app_is_fractional_scaling()). +int app_get_fractional_scaling_part_percent(int full_scale_percent); + + +/// Compute the fractional scaling correction ratio from a full scale percentage. +/// For example, 125% -> correction = 1.25, 250% -> correction = 1.25 (since GTK uses 2x integer scaling). +/// Returns 0.0 if the scaling is not fractional (see app_is_fractional_scaling()). +double app_compute_fractional_scaling_correction(int full_scale_percent); + + +/// Apply the fractional scaling correction to a window size. +/// \param base_w Base width (before fractional correction) +/// \param base_h Base height (before fractional correction) +/// \param correction The correction ratio from app_compute_fractional_scaling_correction() +/// \return Scaled (width, height) pair, or the unchanged base size if correction or size is not positive. +std::pair app_apply_fractional_scaling_to_size(int base_w, int base_h, double correction); + + +/// Compute the full scale percentage from a DPI value. +/// \param h_ppi Horizontal pixels per inch +/// \return Full scale percentage (e.g., 96 DPI -> 100, 120 DPI -> 125, 240 DPI -> 250), or 0 if h_ppi <= 0. +int app_compute_scaling_percent_from_dpi(double h_ppi); + + // These functions won't return until the dialogs are closed. // Messages must not contain any markup. @@ -79,25 +113,6 @@ void app_set_windows_fractional_scaling_percent(int percent); /// \param config_size_h Configured height (0 if using glade default) void app_apply_fractional_scaling_to_default_size(Gtk::Window* window, int config_size_w, int config_size_h); -/// Get the fractional scaling percentage detected on Windows (0 if not detected or integer scale). -/// For example, at 150% scaling, this returns 150; at 125% scaling, this returns 125; at 250% scaling, this returns 250. -/// Returns 0 for exact integer scales (100%, 200%, etc.). -int app_get_windows_fractional_scaling_percent(); - - -/// Set the fractional scaling percentage detected on Windows. -/// This should only be called once during application initialization. -void app_set_windows_fractional_scaling_percent(int percent); - - -/// Apply fractional scaling to default window size if fractional scaling is detected. -/// This compensates for GTK3's lack of fractional scaling support on Windows. -/// \param window The window to apply scaling to -/// \param config_size_w Configured width (0 if using glade default) -/// \param config_size_h Configured height (0 if using glade default) -void app_apply_fractional_scaling_to_default_size(Gtk::Window* window, int config_size_w, int config_size_h); - - #endif diff --git a/src/applib/tests/CMakeLists.txt b/src/applib/tests/CMakeLists.txt index cbd0b36..4d55559 100644 --- a/src/applib/tests/CMakeLists.txt +++ b/src/applib/tests/CMakeLists.txt @@ -15,6 +15,7 @@ endif() add_library(applib_tests OBJECT) target_sources(applib_tests PRIVATE test_app_regex.cpp + test_fractional_scaling.cpp test_smartctl_parser.cpp test_smartctl_version_parser.cpp ) diff --git a/src/applib/tests/test_fractional_scaling.cpp b/src/applib/tests/test_fractional_scaling.cpp new file mode 100644 index 0000000..087b6a9 --- /dev/null +++ b/src/applib/tests/test_fractional_scaling.cpp @@ -0,0 +1,138 @@ +/****************************************************************************** +License: BSD Zero Clause License +Copyright: + (C) 2026 Alexander Shaduri +******************************************************************************/ +/// \file +/// \author Alexander Shaduri +/// \ingroup applib_tests +/// \weakgroup applib_tests +/// @{ + +#include "catch2/catch.hpp" + +#include "applib/gui_utils.h" +#include + + + +TEST_CASE("FractionalScalingDetection", "[app][scaling]") +{ + SECTION("Fractional scales") + { + REQUIRE(app_is_fractional_scaling(125)); + REQUIRE(app_is_fractional_scaling(150)); + REQUIRE(app_is_fractional_scaling(175)); + REQUIRE(app_is_fractional_scaling(225)); + REQUIRE(app_is_fractional_scaling(250)); + } + + SECTION("Integer scales") + { + REQUIRE_FALSE(app_is_fractional_scaling(100)); + REQUIRE_FALSE(app_is_fractional_scaling(200)); + REQUIRE_FALSE(app_is_fractional_scaling(300)); + } + + SECTION("Invalid and sub-100% scales") + { + REQUIRE_FALSE(app_is_fractional_scaling(0)); + REQUIRE_FALSE(app_is_fractional_scaling(-125)); + REQUIRE_FALSE(app_is_fractional_scaling(75)); + REQUIRE_FALSE(app_is_fractional_scaling(99)); + } +} + + + +TEST_CASE("FractionalScalingPart", "[app][scaling]") +{ + REQUIRE(app_get_fractional_scaling_part_percent(125) == 25); + REQUIRE(app_get_fractional_scaling_part_percent(150) == 50); + REQUIRE(app_get_fractional_scaling_part_percent(250) == 50); + REQUIRE(app_get_fractional_scaling_part_percent(225) == 25); + + REQUIRE(app_get_fractional_scaling_part_percent(100) == 0); + REQUIRE(app_get_fractional_scaling_part_percent(200) == 0); + REQUIRE(app_get_fractional_scaling_part_percent(75) == 0); + REQUIRE(app_get_fractional_scaling_part_percent(0) == 0); + REQUIRE(app_get_fractional_scaling_part_percent(-50) == 0); +} + + + +TEST_CASE("FractionalScalingCorrection", "[app][scaling]") +{ + SECTION("Fractional scales") + { + REQUIRE(app_compute_fractional_scaling_correction(125) == Approx(1.25)); + REQUIRE(app_compute_fractional_scaling_correction(150) == Approx(1.5)); + REQUIRE(app_compute_fractional_scaling_correction(175) == Approx(1.75)); + // GTK already applies 2x at these scales, so only the remainder is corrected + REQUIRE(app_compute_fractional_scaling_correction(225) == Approx(1.125)); + REQUIRE(app_compute_fractional_scaling_correction(250) == Approx(1.25)); // not 1.5 + } + + SECTION("No correction for integer, invalid or sub-100% scales") + { + REQUIRE(app_compute_fractional_scaling_correction(100) == 0.0); + REQUIRE(app_compute_fractional_scaling_correction(200) == 0.0); + REQUIRE(app_compute_fractional_scaling_correction(0) == 0.0); + REQUIRE(app_compute_fractional_scaling_correction(-125) == 0.0); + REQUIRE(app_compute_fractional_scaling_correction(75) == 0.0); // used to divide by zero + REQUIRE(app_compute_fractional_scaling_correction(50) == 0.0); + } +} + + + +TEST_CASE("FractionalScalingWindowSize", "[app][scaling]") +{ + SECTION("Scaled sizes") + { + REQUIRE(app_apply_fractional_scaling_to_size(800, 600, app_compute_fractional_scaling_correction(125)) + == std::pair(1000, 750)); + REQUIRE(app_apply_fractional_scaling_to_size(800, 600, app_compute_fractional_scaling_correction(150)) + == std::pair(1200, 900)); + REQUIRE(app_apply_fractional_scaling_to_size(800, 600, app_compute_fractional_scaling_correction(175)) + == std::pair(1400, 1050)); + REQUIRE(app_apply_fractional_scaling_to_size(800, 600, app_compute_fractional_scaling_correction(225)) + == std::pair(900, 675)); + REQUIRE(app_apply_fractional_scaling_to_size(800, 600, app_compute_fractional_scaling_correction(250)) + == std::pair(1000, 750)); + } + + SECTION("Rounding") + { + REQUIRE(app_apply_fractional_scaling_to_size(101, 3, 1.25) == std::pair(126, 4)); // 126.25, 3.75 + } + + SECTION("Unchanged when correction or size is not positive") + { + REQUIRE(app_apply_fractional_scaling_to_size(800, 600, 0.0) == std::pair(800, 600)); + REQUIRE(app_apply_fractional_scaling_to_size(800, 600, -1.0) == std::pair(800, 600)); + REQUIRE(app_apply_fractional_scaling_to_size(0, 600, 1.25) == std::pair(0, 600)); + REQUIRE(app_apply_fractional_scaling_to_size(800, -1, 1.25) == std::pair(800, -1)); + } +} + + + +TEST_CASE("ScalingPercentFromDpi", "[app][scaling][dpi]") +{ + REQUIRE(app_compute_scaling_percent_from_dpi(96) == 100); + REQUIRE(app_compute_scaling_percent_from_dpi(120) == 125); + REQUIRE(app_compute_scaling_percent_from_dpi(144) == 150); + REQUIRE(app_compute_scaling_percent_from_dpi(168) == 175); + REQUIRE(app_compute_scaling_percent_from_dpi(192) == 200); + REQUIRE(app_compute_scaling_percent_from_dpi(216) == 225); + REQUIRE(app_compute_scaling_percent_from_dpi(240) == 250); + REQUIRE(app_compute_scaling_percent_from_dpi(72) == 75); + + REQUIRE(app_compute_scaling_percent_from_dpi(0) == 0); + REQUIRE(app_compute_scaling_percent_from_dpi(-96) == 0); +} + + + +/// @} diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt index 0b01d34..63aa790 100644 --- a/src/gui/CMakeLists.txt +++ b/src/gui/CMakeLists.txt @@ -68,5 +68,4 @@ endif() add_subdirectory(ui) -add_subdirectory(tests) diff --git a/src/gui/gsc_init.cpp b/src/gui/gsc_init.cpp index a99dd2e..cedc0f4 100644 --- a/src/gui/gsc_init.cpp +++ b/src/gui/gsc_init.cpp @@ -545,15 +545,13 @@ bool app_init_and_loop(int& argc, char**& argv) ReleaseDC(nullptr, screen); #endif if (h_ppi > 0) { - const double scale = h_ppi / 96.0; - debug_out_info("app", "Windows system DPI: " << h_ppi << ", scale: " << scale << "\n"); - const int full_scale_percent = static_cast(std::lround(scale * 100.0)); - const int integer_scale_percent = (full_scale_percent / 100) * 100; // 250 -> 200, 150 -> 100 - if (full_scale_percent != integer_scale_percent) { // fractional scaling detected + const int full_scale_percent = app_compute_scaling_percent_from_dpi(h_ppi); + debug_out_info("app", "Windows system DPI: " << h_ppi << ", scale: " << full_scale_percent << "%\n"); + if (app_is_fractional_scaling(full_scale_percent)) { // Store the full scale percent for use in window sizing app_set_windows_fractional_scaling_percent(full_scale_percent); // Increase the font size by the fractional amount - const int fraction_percent = full_scale_percent - integer_scale_percent; + const int fraction_percent = app_get_fractional_scaling_part_percent(full_scale_percent); debug_out_dump("app", "Fractional scaling detected (" << full_scale_percent << "%), increasing font size by " << fraction_percent << "%.\n"); Gtk::Settings::get_default()->property_gtk_font_name() .set_value("Segoe UI " + hz::number_to_string_nolocale(static_cast(9 * (1. + fraction_percent/100.)))); diff --git a/src/gui/tests/CMakeLists.txt b/src/gui/tests/CMakeLists.txt deleted file mode 100644 index 3aefcff..0000000 --- a/src/gui/tests/CMakeLists.txt +++ /dev/null @@ -1,21 +0,0 @@ -############################################################################### -# License: BSD Zero Clause License file -# Copyright: -# (C) 2026 Alexander Shaduri -############################################################################### - -if (NOT APP_BUILD_TESTS) - set_directory_properties(PROPERTIES EXCLUDE_FROM_ALL true) -else() - set_directory_properties(PROPERTIES EXCLUDE_FROM_ALL false) -endif() - - -# Use Object libraries to allow runtime test discovery -add_library(gui_tests OBJECT) -target_sources(gui_tests PRIVATE - test_fractional_scaling.cpp -) -target_link_libraries(gui_tests PRIVATE - Catch2 -) diff --git a/src/gui/tests/test_fractional_scaling.cpp b/src/gui/tests/test_fractional_scaling.cpp deleted file mode 100644 index dfe8bee..0000000 --- a/src/gui/tests/test_fractional_scaling.cpp +++ /dev/null @@ -1,216 +0,0 @@ -/****************************************************************************** -License: BSD Zero Clause License -Copyright: - (C) 2008 - 2026 Alexander Shaduri -******************************************************************************/ -/// \file -/// \author Alexander Shaduri -/// \ingroup gui_tests -/// \weakgroup gui_tests -/// @{ - -#include "catch2/catch.hpp" - -#include - - -/// Test the fractional scaling calculation logic -/// This tests the mathematical correctness of the scaling correction ratio -TEST_CASE("FractionalScalingCalculation", "[gui][scaling]") -{ - // Test the calculation logic that's used in app_apply_fractional_scaling_to_default_size() - // correction = system_scale / integer_ui_scale - - SECTION("125% scaling (fractional)") - { - const int full_percent = 125; - const double system_scale = static_cast(full_percent) / 100.0; // 1.25 - const int integer_ui_scale = full_percent / 100; // 1 - const double correction = system_scale / static_cast(integer_ui_scale); - - REQUIRE(system_scale == 1.25); - REQUIRE(integer_ui_scale == 1); - REQUIRE(correction == 1.25); - - // Test window scaling: 800x600 base should become 1000x750 - const int base_w = 800; - const int base_h = 600; - const int scaled_w = static_cast(std::lround(base_w * correction)); - const int scaled_h = static_cast(std::lround(base_h * correction)); - - REQUIRE(scaled_w == 1000); - REQUIRE(scaled_h == 750); - } - - SECTION("150% scaling (fractional)") - { - const int full_percent = 150; - const double system_scale = static_cast(full_percent) / 100.0; // 1.5 - const int integer_ui_scale = full_percent / 100; // 1 - const double correction = system_scale / static_cast(integer_ui_scale); - - REQUIRE(system_scale == 1.5); - REQUIRE(integer_ui_scale == 1); - REQUIRE(correction == 1.5); - - // Test window scaling: 800x600 base should become 1200x900 - const int base_w = 800; - const int base_h = 600; - const int scaled_w = static_cast(std::lround(base_w * correction)); - const int scaled_h = static_cast(std::lround(base_h * correction)); - - REQUIRE(scaled_w == 1200); - REQUIRE(scaled_h == 900); - } - - SECTION("175% scaling (fractional)") - { - const int full_percent = 175; - const double system_scale = static_cast(full_percent) / 100.0; // 1.75 - const int integer_ui_scale = full_percent / 100; // 1 - const double correction = system_scale / static_cast(integer_ui_scale); - - REQUIRE(system_scale == 1.75); - REQUIRE(integer_ui_scale == 1); - REQUIRE(correction == 1.75); - - // Test window scaling: 800x600 base should become 1400x1050 - const int base_w = 800; - const int base_h = 600; - const int scaled_w = static_cast(std::lround(base_w * correction)); - const int scaled_h = static_cast(std::lround(base_h * correction)); - - REQUIRE(scaled_w == 1400); - REQUIRE(scaled_h == 1050); - } - - SECTION("250% scaling (fractional) - Critical test case") - { - // This is the case that was broken before the fix - const int full_percent = 250; - const double system_scale = static_cast(full_percent) / 100.0; // 2.5 - const int integer_ui_scale = full_percent / 100; // 2 - const double correction = system_scale / static_cast(integer_ui_scale); - - REQUIRE(system_scale == 2.5); - REQUIRE(integer_ui_scale == 2); - REQUIRE(correction == 1.25); // NOT 1.5 (the old broken behavior) - - // Test window scaling: GTK already applies 2x, we need to apply 1.25x more - // For a base of 800x600, GTK makes it 1600x1200, we should scale to 2000x1500 - const int gtk_scaled_w = 1600; // After GTK's 2x integer scaling - const int gtk_scaled_h = 1200; - const int final_w = static_cast(std::lround(gtk_scaled_w * correction)); - const int final_h = static_cast(std::lround(gtk_scaled_h * correction)); - - REQUIRE(final_w == 2000); // 2.5x total = 800 * 2.5 - REQUIRE(final_h == 1500); // 2.5x total = 600 * 2.5 - } - - SECTION("225% scaling (fractional)") - { - const int full_percent = 225; - const double system_scale = static_cast(full_percent) / 100.0; // 2.25 - const int integer_ui_scale = full_percent / 100; // 2 - const double correction = system_scale / static_cast(integer_ui_scale); - - REQUIRE(system_scale == 2.25); - REQUIRE(integer_ui_scale == 2); - REQUIRE(correction == 1.125); - - // Test window scaling: 800x600 at 2x GTK = 1600x1200, with correction = 1800x1350 - const int gtk_scaled_w = 1600; - const int gtk_scaled_h = 1200; - const int final_w = static_cast(std::lround(gtk_scaled_w * correction)); - const int final_h = static_cast(std::lround(gtk_scaled_h * correction)); - - REQUIRE(final_w == 1800); - REQUIRE(final_h == 1350); - } - - SECTION("100% scaling (integer, no fractional)") - { - // Should not trigger fractional scaling at all (full_percent would be 0) - const int full_percent = 100; - const int integer_scale_percent = (full_percent / 100) * 100; - - REQUIRE(full_percent == integer_scale_percent); // No fractional component - } - - SECTION("200% scaling (integer, no fractional)") - { - // Should not trigger fractional scaling at all (full_percent would be 0) - const int full_percent = 200; - const int integer_scale_percent = (full_percent / 100) * 100; - - REQUIRE(full_percent == integer_scale_percent); // No fractional component - } -} - - -/// Test the DPI detection logic -TEST_CASE("DPIDetectionLogic", "[gui][scaling][dpi]") -{ - SECTION("Detect 125% scaling from DPI") - { - const int h_ppi = 120; // 120 DPI - const double scale = static_cast(h_ppi) / 96.0; - const int full_scale_percent = static_cast(std::lround(scale * 100.0)); - const int integer_scale_percent = (full_scale_percent / 100) * 100; - - REQUIRE(full_scale_percent == 125); - REQUIRE(integer_scale_percent == 100); - REQUIRE(full_scale_percent != integer_scale_percent); // Fractional scaling detected - } - - SECTION("Detect 150% scaling from DPI") - { - const int h_ppi = 144; // 144 DPI - const double scale = static_cast(h_ppi) / 96.0; - const int full_scale_percent = static_cast(std::lround(scale * 100.0)); - const int integer_scale_percent = (full_scale_percent / 100) * 100; - - REQUIRE(full_scale_percent == 150); - REQUIRE(integer_scale_percent == 100); - REQUIRE(full_scale_percent != integer_scale_percent); // Fractional scaling detected - } - - SECTION("Detect 250% scaling from DPI") - { - const int h_ppi = 240; // 240 DPI - const double scale = static_cast(h_ppi) / 96.0; - const int full_scale_percent = static_cast(std::lround(scale * 100.0)); - const int integer_scale_percent = (full_scale_percent / 100) * 100; - - REQUIRE(full_scale_percent == 250); - REQUIRE(integer_scale_percent == 200); - REQUIRE(full_scale_percent != integer_scale_percent); // Fractional scaling detected - } - - SECTION("No fractional scaling at 100% (96 DPI)") - { - const int h_ppi = 96; // 96 DPI (standard) - const double scale = static_cast(h_ppi) / 96.0; - const int full_scale_percent = static_cast(std::lround(scale * 100.0)); - const int integer_scale_percent = (full_scale_percent / 100) * 100; - - REQUIRE(full_scale_percent == 100); - REQUIRE(integer_scale_percent == 100); - REQUIRE(full_scale_percent == integer_scale_percent); // No fractional scaling - } - - SECTION("No fractional scaling at 200% (192 DPI)") - { - const int h_ppi = 192; // 192 DPI - const double scale = static_cast(h_ppi) / 96.0; - const int full_scale_percent = static_cast(std::lround(scale * 100.0)); - const int integer_scale_percent = (full_scale_percent / 100) * 100; - - REQUIRE(full_scale_percent == 200); - REQUIRE(integer_scale_percent == 200); - REQUIRE(full_scale_percent == integer_scale_percent); // No fractional scaling - } -} - - -/// @} diff --git a/src/test_all/CMakeLists.txt b/src/test_all/CMakeLists.txt index 36b8a47..7568e79 100644 --- a/src/test_all/CMakeLists.txt +++ b/src/test_all/CMakeLists.txt @@ -22,7 +22,6 @@ target_link_libraries(test_all PRIVATE libdebug applib_tests hz_tests - gui_tests Catch2 )