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 <noreply@anthropic.com>
This commit is contained in:
Alexander ShaduriandClaude Opus 5.5 committed 2026-10-15 22:43:13 +02:00
1 parent cb5b1284be
commit 08d200af94
9 files changed
+233 -271

No files matched your search

+56 -7
View File
@@ -12,6 +12,7 @@ Copyright:
#include "gui_utils.h"
#include <cmath>
#include <tuple>
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<double>(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<double>(integer_ui_scale);
}
std::pair<int, int> 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<int>(std::lround(base_w * correction)),
static_cast<int>(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<int>(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<double>(full_percent) / 100.0;
const int integer_ui_scale = full_percent / 100; // GTK3 uses floor (integer) scaling
const double correction = system_scale / static_cast<double>(integer_ui_scale);
size_w = static_cast<int>(std::lround(glade_w * correction));
size_h = static_cast<int>(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) {
+34 -19
View File
@@ -15,9 +15,43 @@ Copyright:
#include <glibmm.h>
#include <gtkmm.h>
#include <string>
#include <utility>
/// 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<int, int> 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
+1
View File
@@ -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
)
@@ -0,0 +1,138 @@
/******************************************************************************
License: BSD Zero Clause License
Copyright:
(C) 2026 Alexander Shaduri <ashaduri@gmail.com>
******************************************************************************/
/// \file
/// \author Alexander Shaduri
/// \ingroup applib_tests
/// \weakgroup applib_tests
/// @{
#include "catch2/catch.hpp"
#include "applib/gui_utils.h"
#include <utility>
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);
}
/// @}
-1
View File
@@ -68,5 +68,4 @@ endif()
add_subdirectory(ui)
add_subdirectory(tests)
+4 -6
View File
@@ -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<int>(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<int>(9 * (1. + fraction_percent/100.))));
-21
View File
@@ -1,21 +0,0 @@
###############################################################################
# License: BSD Zero Clause License file
# Copyright:
# (C) 2026 Alexander Shaduri <ashaduri@gmail.com>
###############################################################################
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
)
-216
View File
@@ -1,216 +0,0 @@
/******************************************************************************
License: BSD Zero Clause License
Copyright:
(C) 2008 - 2026 Alexander Shaduri <ashaduri@gmail.com>
******************************************************************************/
/// \file
/// \author Alexander Shaduri
/// \ingroup gui_tests
/// \weakgroup gui_tests
/// @{
#include "catch2/catch.hpp"
#include <cmath>
/// 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<double>(full_percent) / 100.0; // 1.25
const int integer_ui_scale = full_percent / 100; // 1
const double correction = system_scale / static_cast<double>(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<int>(std::lround(base_w * correction));
const int scaled_h = static_cast<int>(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<double>(full_percent) / 100.0; // 1.5
const int integer_ui_scale = full_percent / 100; // 1
const double correction = system_scale / static_cast<double>(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<int>(std::lround(base_w * correction));
const int scaled_h = static_cast<int>(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<double>(full_percent) / 100.0; // 1.75
const int integer_ui_scale = full_percent / 100; // 1
const double correction = system_scale / static_cast<double>(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<int>(std::lround(base_w * correction));
const int scaled_h = static_cast<int>(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<double>(full_percent) / 100.0; // 2.5
const int integer_ui_scale = full_percent / 100; // 2
const double correction = system_scale / static_cast<double>(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<int>(std::lround(gtk_scaled_w * correction));
const int final_h = static_cast<int>(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<double>(full_percent) / 100.0; // 2.25
const int integer_ui_scale = full_percent / 100; // 2
const double correction = system_scale / static_cast<double>(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<int>(std::lround(gtk_scaled_w * correction));
const int final_h = static_cast<int>(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<double>(h_ppi) / 96.0;
const int full_scale_percent = static_cast<int>(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<double>(h_ppi) / 96.0;
const int full_scale_percent = static_cast<int>(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<double>(h_ppi) / 96.0;
const int full_scale_percent = static_cast<int>(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<double>(h_ppi) / 96.0;
const int full_scale_percent = static_cast<int>(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<double>(h_ppi) / 96.0;
const int full_scale_percent = static_cast<int>(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
}
}
/// @}
-1
View File
@@ -22,7 +22,6 @@ target_link_libraries(test_all PRIVATE
libdebug
applib_tests
hz_tests
gui_tests
Catch2
)