Add global font settings (Edit -> Preferences...)
Interface font + code/text-view font picked with GtkFontButton
widgets, persisted to ~/.config/fdesigner/settings.conf (font=,
code_font=, Pango descriptions; FDESIGNER_SETTINGS_CONF overrides
the path for tests), applied live at startup and on Apply via
screen-wide CSS providers (interface font) and a textview CSS rule
(code font, so Text/Memo views use it). Sizes clamp to 6-48 pt;
empty/invalid values fall back to defaults.
New: src/preferences.h/.cpp, tests/font_tests.{cpp,sh} (20-check
harness + self-test + Preferences dialog smoke/Apply test).
Wired: Edit menu + mi_preferences in ui/designer.ui, menu hook,
startup apply, FDESIGNER_OPEN_PREFERENCES test hook and a font
block in the self-test (src/main.cpp); Makefile build line;
DESIGN.md section.
This commit is contained in:
1 parent
7828ced3d6
commit
9d1e99ed88
8 files changed
+647
-2
No files matched your search
@@ -297,3 +297,16 @@ project holding every new widget, checks the markers, compiles the
|
||||
generated app with `-Wall -Wextra`, and runs it briefly. The generator
|
||||
matrix (`tests/run_matrix.sh` cases 02/03) also covers every new type
|
||||
automatically via `kTypes`.
|
||||
|
||||
## Global font settings (2026-11-26)
|
||||
|
||||
**Edit → Preferences…** opens a font dialog (interface font + code /
|
||||
text-view font, via GtkFontButton). Choices persist in
|
||||
`~/.config/fdesigner/settings.conf` (`font=`, `code_font=`, Pango
|
||||
descriptions; `FDESIGNER_SETTINGS_CONF` overrides the path for tests) and
|
||||
are applied live at startup and on Apply: the interface font via a
|
||||
screen-wide CSS provider, the code font via a `textview` CSS rule (so
|
||||
Text/Memo views and any other GtkTextView use it). Invalid sizes are
|
||||
clamped to 6–48 pt and empty values fall back to the defaults. Logic lives
|
||||
in `src/preferences.h/.cpp`; tests: a font block in the self-test plus
|
||||
`tests/font_tests.sh` (harness + self-test + Preferences dialog smoke test).
|
||||
@@ -3,8 +3,8 @@ CXXFLAGS ?= -O2 -Wall -Wextra -std=c++17
|
||||
GTKFLAGS := $(shell pkg-config --cflags gtk+-3.0)
|
||||
GTKLIBS := $(shell pkg-config --libs gtk+-3.0)
|
||||
|
||||
fdesigner: src/main.cpp src/generator.cpp src/project_io.cpp src/debugger.cpp src/debugger_ui.cpp src/model.h src/props.h src/generator.h src/project_io.h src/debugger.h src/debugger_ui.h src/indicators.h src/indicators_embedded.h src/drawings.h src/drawings_embedded.h ui/designer.ui
|
||||
$(CXX) $(CXXFLAGS) -o $@ src/main.cpp src/generator.cpp src/project_io.cpp src/debugger.cpp src/debugger_ui.cpp $(GTKFLAGS) $(GTKLIBS)
|
||||
fdesigner: src/main.cpp src/generator.cpp src/project_io.cpp src/debugger.cpp src/debugger_ui.cpp src/preferences.cpp src/model.h src/props.h src/generator.h src/project_io.h src/debugger.h src/debugger_ui.h src/preferences.h src/indicators.h src/indicators_embedded.h src/drawings.h src/drawings_embedded.h ui/designer.ui
|
||||
$(CXX) $(CXXFLAGS) -o $@ src/main.cpp src/generator.cpp src/project_io.cpp src/debugger.cpp src/debugger_ui.cpp src/preferences.cpp $(GTKFLAGS) $(GTKLIBS)
|
||||
|
||||
src/indicators_embedded.h: src/indicators.h tools/embed_indicators.sh
|
||||
sh tools/embed_indicators.sh src/indicators.h $@
|
||||
|
||||
@@ -37,6 +37,7 @@
|
||||
#include "project_io.h"
|
||||
#include "debugger.h"
|
||||
#include "debugger_ui.h"
|
||||
#include "preferences.h" // [font-settings]
|
||||
|
||||
// ---------------------------------------------------------------- state
|
||||
|
||||
@@ -3025,6 +3026,35 @@ static gboolean selftest_step(gpointer) {
|
||||
sess2.breakpoints()[0].location == "main");
|
||||
}
|
||||
|
||||
// [font-settings] Font settings: persistence round-trip,
|
||||
// sanitising/clamping, and live application to a text view.
|
||||
{
|
||||
FontSettings fs;
|
||||
fs.font = "DejaVu Sans 14";
|
||||
fs.code_font = "DejaVu Sans Mono 13";
|
||||
check(fs.save("/tmp/fdesigner_font.conf"));
|
||||
FontSettings fs2;
|
||||
check(fs2.load("/tmp/fdesigner_font.conf"));
|
||||
check(fs2.font == "DejaVu Sans 14" &&
|
||||
fs2.code_font == "DejaVu Sans Mono 13");
|
||||
check(FontSettings::sanitized("", "Sans 10") == "Sans 10");
|
||||
check(FontSettings::size_of(
|
||||
FontSettings::sanitized("Sans 99", "Sans 10")) == 48);
|
||||
check(FontSettings::size_of(
|
||||
FontSettings::sanitized("Sans 2", "Sans 10")) == 6);
|
||||
GtkWidget* tv = gtk_text_view_new();
|
||||
gtk_widget_show(tv);
|
||||
preferences_apply_to_text_view(tv, fs);
|
||||
while (gtk_events_pending()) gtk_main_iteration();
|
||||
std::string applied = preferences_applied_font(tv);
|
||||
check(FontSettings::family_of(applied) ==
|
||||
"DejaVu Sans Mono" &&
|
||||
FontSettings::size_of(applied) == 13);
|
||||
gtk_widget_destroy(tv);
|
||||
preferences_apply(fs); // screen-wide, must not crash
|
||||
preferences_apply(FontSettings()); // back to defaults
|
||||
}
|
||||
|
||||
std::printf("SELFTEST %s items=%zu\n", ok ? "PASS" : "FAIL",
|
||||
g_items.size());
|
||||
std::fflush(stdout);
|
||||
@@ -3038,6 +3068,10 @@ static gboolean selftest_step(gpointer) {
|
||||
int main(int argc, char** argv) {
|
||||
gtk_init(&argc, &argv);
|
||||
|
||||
// [font-settings] Apply saved global font settings before
|
||||
// building the UI, so the designer opens in the chosen font.
|
||||
preferences_apply(preferences_current());
|
||||
|
||||
// Selection outline for placed widgets.
|
||||
GtkCssProvider* css = gtk_css_provider_new();
|
||||
gtk_css_provider_load_from_data(
|
||||
@@ -3111,6 +3145,16 @@ int main(int argc, char** argv) {
|
||||
if (mi_save)
|
||||
g_signal_connect(mi_save, "activate", G_CALLBACK(on_save), nullptr);
|
||||
|
||||
// [font-settings] Edit -> Preferences... opens the font
|
||||
// settings dialog (src/preferences.cpp).
|
||||
GObject* mi_prefs = gtk_builder_get_object(builder, "mi_preferences");
|
||||
if (mi_prefs)
|
||||
g_signal_connect(mi_prefs, "activate",
|
||||
G_CALLBACK(+[](GtkMenuItem*, gpointer) {
|
||||
preferences_open(g_window);
|
||||
}),
|
||||
nullptr);
|
||||
|
||||
// Debug menu -> stepping debugger window / settings.
|
||||
GObject* mi_dbg = gtk_builder_get_object(builder, "mi_debug_open");
|
||||
if (mi_dbg)
|
||||
@@ -3191,6 +3235,14 @@ int main(int argc, char** argv) {
|
||||
return G_SOURCE_REMOVE;
|
||||
},
|
||||
nullptr);
|
||||
// [font-settings] Test hook (used by tests/font_tests.sh):
|
||||
// open the Preferences dialog at startup.
|
||||
if (std::getenv("FDESIGNER_OPEN_PREFERENCES") != nullptr)
|
||||
g_idle_add(+[](gpointer) -> gboolean {
|
||||
preferences_open(g_window);
|
||||
return G_SOURCE_REMOVE;
|
||||
},
|
||||
nullptr);
|
||||
|
||||
bool selftest = std::getenv("FDESIGNER_SELFTEST") != nullptr;
|
||||
if (selftest) g_idle_add(selftest_step, nullptr);
|
||||
|
||||
@@ -0,0 +1,283 @@
|
||||
// FDesigner global font settings — implementation. See preferences.h.
|
||||
#include "preferences.h"
|
||||
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <fstream>
|
||||
|
||||
namespace {
|
||||
|
||||
// Same persistence pattern as DebuggerSettings in debugger.cpp.
|
||||
std::string home_dir() {
|
||||
const char* h = std::getenv("HOME");
|
||||
return h ? h : "";
|
||||
}
|
||||
|
||||
bool ensure_parent_dir(const std::string& path) {
|
||||
size_t slash = path.rfind('/');
|
||||
if (slash == std::string::npos) return true;
|
||||
std::string dir = path.substr(0, slash);
|
||||
std::string cur;
|
||||
for (size_t i = 1; i <= dir.size(); ++i) {
|
||||
if (i == dir.size() || dir[i] == '/') {
|
||||
cur = dir.substr(0, i);
|
||||
if (!cur.empty()) ::mkdir(cur.c_str(), 0755);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string trim_s(const std::string& s) {
|
||||
size_t a = s.find_first_not_of(" \t\r\n");
|
||||
if (a == std::string::npos) return "";
|
||||
size_t b = s.find_last_not_of(" \t\r\n");
|
||||
return s.substr(a, b - a + 1);
|
||||
}
|
||||
|
||||
// Escape a font family for a quoted CSS string value.
|
||||
std::string css_quote(const std::string& family) {
|
||||
std::string out;
|
||||
for (char c : family) {
|
||||
if (c == '"' || c == '\\') out += '\\';
|
||||
out += c;
|
||||
}
|
||||
return "\"" + out + "\"";
|
||||
}
|
||||
|
||||
void load_css(GtkCssProvider* provider, const std::string& css) {
|
||||
GError* error = nullptr;
|
||||
gtk_css_provider_load_from_data(provider, css.c_str(), -1, &error);
|
||||
if (error) {
|
||||
std::fprintf(stderr, "fdesigner: font CSS failed: %s\n",
|
||||
error->message);
|
||||
g_error_free(error);
|
||||
}
|
||||
}
|
||||
|
||||
FontSettings g_current;
|
||||
bool g_current_loaded = false;
|
||||
|
||||
} // namespace
|
||||
|
||||
std::string FontSettings::default_path() {
|
||||
const char* e = std::getenv("FDESIGNER_SETTINGS_CONF");
|
||||
if (e && *e) return e;
|
||||
return home_dir() + "/.config/fdesigner/settings.conf";
|
||||
}
|
||||
|
||||
std::string FontSettings::sanitized(const std::string& desc,
|
||||
const std::string& fallback) {
|
||||
PangoFontDescription* d =
|
||||
pango_font_description_from_string(desc.c_str());
|
||||
const char* family =
|
||||
d ? pango_font_description_get_family(d) : nullptr;
|
||||
if (!d || !family || !*family) {
|
||||
if (d) pango_font_description_free(d);
|
||||
if (desc == fallback) return "Sans 10"; // fallback itself is bad
|
||||
return sanitized(fallback, fallback);
|
||||
}
|
||||
int size = pango_font_description_get_size(d); // Pango units, 0 = unset
|
||||
int fallback_size = size_of(fallback);
|
||||
if (size <= 0)
|
||||
size = (fallback_size > 0 ? fallback_size : 10) * PANGO_SCALE;
|
||||
else if (size < 6 * PANGO_SCALE)
|
||||
size = 6 * PANGO_SCALE;
|
||||
else if (size > 48 * PANGO_SCALE)
|
||||
size = 48 * PANGO_SCALE;
|
||||
pango_font_description_set_size(d, size);
|
||||
char* s = pango_font_description_to_string(d);
|
||||
std::string out = s ? s : fallback;
|
||||
g_free(s);
|
||||
pango_font_description_free(d);
|
||||
return out;
|
||||
}
|
||||
|
||||
std::string FontSettings::family_of(const std::string& desc) {
|
||||
PangoFontDescription* d =
|
||||
pango_font_description_from_string(desc.c_str());
|
||||
if (!d) return "";
|
||||
const char* family = pango_font_description_get_family(d);
|
||||
std::string out = family ? family : "";
|
||||
pango_font_description_free(d);
|
||||
return out;
|
||||
}
|
||||
|
||||
int FontSettings::size_of(const std::string& desc) {
|
||||
PangoFontDescription* d =
|
||||
pango_font_description_from_string(desc.c_str());
|
||||
if (!d) return 0;
|
||||
int size = pango_font_description_get_size(d);
|
||||
pango_font_description_free(d);
|
||||
return size > 0 ? size / PANGO_SCALE : 0;
|
||||
}
|
||||
|
||||
std::string FontSettings::ui_css() const {
|
||||
return "* { font-family: " + css_quote(family_of(font)) +
|
||||
"; font-size: " + std::to_string(size_of(font)) + "pt; }";
|
||||
}
|
||||
|
||||
std::string FontSettings::code_css() const {
|
||||
return "textview { font-family: " + css_quote(family_of(code_font)) +
|
||||
"; font-size: " + std::to_string(size_of(code_font)) + "pt; }";
|
||||
}
|
||||
|
||||
bool FontSettings::save(const std::string& path) const {
|
||||
std::string p = path.empty() ? default_path() : path;
|
||||
ensure_parent_dir(p);
|
||||
std::ofstream f(p);
|
||||
if (!f) return false;
|
||||
f << "# FDesigner settings\n";
|
||||
f << "font=" << sanitized(font, "Sans 10") << "\n";
|
||||
f << "code_font=" << sanitized(code_font, "Monospace 11") << "\n";
|
||||
return (bool)f;
|
||||
}
|
||||
|
||||
bool FontSettings::load(const std::string& path) {
|
||||
std::string p = path.empty() ? default_path() : path;
|
||||
std::ifstream f(p);
|
||||
if (!f) return false;
|
||||
std::string line;
|
||||
while (std::getline(f, line)) {
|
||||
std::string t = trim_s(line);
|
||||
if (t.empty() || t[0] == '#') continue;
|
||||
size_t eq = t.find('=');
|
||||
if (eq == std::string::npos) continue;
|
||||
std::string key = trim_s(t.substr(0, eq));
|
||||
std::string value = trim_s(t.substr(eq + 1));
|
||||
if (key == "font")
|
||||
font = sanitized(value, font);
|
||||
else if (key == "code_font")
|
||||
code_font = sanitized(value, code_font);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
FontSettings preferences_current() {
|
||||
if (!g_current_loaded) {
|
||||
g_current.load(); // keeps defaults when no file exists
|
||||
g_current_loaded = true;
|
||||
}
|
||||
return g_current;
|
||||
}
|
||||
|
||||
void preferences_apply(const FontSettings& s) {
|
||||
static GtkCssProvider* ui_provider = nullptr;
|
||||
static GtkCssProvider* code_provider = nullptr;
|
||||
static bool providers_added = false;
|
||||
if (!ui_provider) ui_provider = gtk_css_provider_new();
|
||||
if (!code_provider) code_provider = gtk_css_provider_new();
|
||||
load_css(ui_provider, s.ui_css());
|
||||
load_css(code_provider, s.code_css());
|
||||
if (!providers_added) {
|
||||
GdkScreen* screen = gdk_screen_get_default();
|
||||
if (screen) {
|
||||
gtk_style_context_add_provider_for_screen(
|
||||
screen, GTK_STYLE_PROVIDER(ui_provider),
|
||||
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
|
||||
gtk_style_context_add_provider_for_screen(
|
||||
screen, GTK_STYLE_PROVIDER(code_provider),
|
||||
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
|
||||
providers_added = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void preferences_apply_to_text_view(GtkWidget* view,
|
||||
const FontSettings& s) {
|
||||
if (!view) return;
|
||||
GtkCssProvider* provider = gtk_css_provider_new();
|
||||
load_css(provider,
|
||||
"* { font-family: " + css_quote(FontSettings::family_of(
|
||||
s.code_font)) +
|
||||
"; font-size: " +
|
||||
std::to_string(FontSettings::size_of(s.code_font)) +
|
||||
"pt; }");
|
||||
gtk_style_context_add_provider(
|
||||
gtk_widget_get_style_context(view), GTK_STYLE_PROVIDER(provider),
|
||||
GTK_STYLE_PROVIDER_PRIORITY_USER);
|
||||
g_object_unref(provider); // style context holds its own reference
|
||||
}
|
||||
|
||||
std::string preferences_applied_font(GtkWidget* widget) {
|
||||
if (!widget) return "";
|
||||
PangoContext* ctx = gtk_widget_get_pango_context(widget);
|
||||
if (!ctx) return "";
|
||||
const PangoFontDescription* d = pango_context_get_font_description(ctx);
|
||||
if (!d) return "";
|
||||
char* s = pango_font_description_to_string(d);
|
||||
std::string out = s ? s : "";
|
||||
g_free(s);
|
||||
return out;
|
||||
}
|
||||
|
||||
namespace {
|
||||
// Response id for the dialog's "Reset to Defaults" button.
|
||||
const int kResetResponse = 100;
|
||||
} // namespace
|
||||
|
||||
bool preferences_open(GtkWidget* parent) {
|
||||
FontSettings s = preferences_current();
|
||||
GtkWidget* d = gtk_dialog_new_with_buttons(
|
||||
"Preferences", GTK_WINDOW(parent), GTK_DIALOG_MODAL, "_Cancel",
|
||||
GTK_RESPONSE_CANCEL, "_Apply", GTK_RESPONSE_ACCEPT, nullptr);
|
||||
gtk_dialog_add_button(GTK_DIALOG(d), "_Reset to Defaults",
|
||||
kResetResponse);
|
||||
GtkWidget* content = gtk_dialog_get_content_area(GTK_DIALOG(d));
|
||||
GtkWidget* grid = gtk_grid_new();
|
||||
gtk_grid_set_row_spacing(GTK_GRID(grid), 6);
|
||||
gtk_grid_set_column_spacing(GTK_GRID(grid), 8);
|
||||
gtk_container_set_border_width(GTK_CONTAINER(grid), 10);
|
||||
gtk_container_add(GTK_CONTAINER(content), grid);
|
||||
|
||||
GtkWidget* ui_btn = gtk_font_button_new_with_font(s.font.c_str());
|
||||
gtk_font_button_set_use_font(GTK_FONT_BUTTON(ui_btn), TRUE);
|
||||
gtk_widget_set_hexpand(ui_btn, TRUE);
|
||||
gtk_grid_attach(GTK_GRID(grid), gtk_label_new("Interface font:"), 0, 0,
|
||||
1, 1);
|
||||
gtk_grid_attach(GTK_GRID(grid), ui_btn, 1, 0, 1, 1);
|
||||
|
||||
GtkWidget* code_btn =
|
||||
gtk_font_button_new_with_font(s.code_font.c_str());
|
||||
gtk_font_button_set_use_font(GTK_FONT_BUTTON(code_btn), TRUE);
|
||||
gtk_widget_set_hexpand(code_btn, TRUE);
|
||||
gtk_grid_attach(GTK_GRID(grid),
|
||||
gtk_label_new("Code / text-view font:"), 0, 1, 1, 1);
|
||||
gtk_grid_attach(GTK_GRID(grid), code_btn, 1, 1, 1, 1);
|
||||
|
||||
std::string note_text =
|
||||
"Fonts apply immediately to the designer interface and to\n"
|
||||
"code / text views, and are saved to " +
|
||||
FontSettings::default_path() + ".";
|
||||
GtkWidget* note = gtk_label_new(note_text.c_str());
|
||||
gtk_label_set_xalign(GTK_LABEL(note), 0.0);
|
||||
gtk_grid_attach(GTK_GRID(grid), note, 0, 2, 2, 1);
|
||||
|
||||
gtk_widget_show_all(d);
|
||||
int resp;
|
||||
while ((resp = gtk_dialog_run(GTK_DIALOG(d))) == kResetResponse) {
|
||||
gtk_font_chooser_set_font(GTK_FONT_CHOOSER(ui_btn), "Sans 10");
|
||||
gtk_font_chooser_set_font(GTK_FONT_CHOOSER(code_btn),
|
||||
"Monospace 11");
|
||||
}
|
||||
if (resp == GTK_RESPONSE_ACCEPT) {
|
||||
gchar* ui_font = gtk_font_chooser_get_font(GTK_FONT_CHOOSER(ui_btn));
|
||||
gchar* code_font =
|
||||
gtk_font_chooser_get_font(GTK_FONT_CHOOSER(code_btn));
|
||||
s.font = FontSettings::sanitized(ui_font ? ui_font : "",
|
||||
s.font);
|
||||
s.code_font = FontSettings::sanitized(code_font ? code_font : "",
|
||||
s.code_font);
|
||||
g_free(ui_font);
|
||||
g_free(code_font);
|
||||
s.save();
|
||||
g_current = s;
|
||||
g_current_loaded = true;
|
||||
preferences_apply(s);
|
||||
gtk_widget_destroy(d);
|
||||
return true;
|
||||
}
|
||||
gtk_widget_destroy(d);
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
// FDesigner global font settings — Edit -> Preferences...
|
||||
//
|
||||
// One interface font and one code/text-view font, chosen with GtkFontButton
|
||||
// widgets. Persisted in ~/.config/fdesigner/settings.conf (key=value, the
|
||||
// same pattern as the debugger's debugger.conf); FDESIGNER_SETTINGS_CONF
|
||||
// overrides the path (used by tests).
|
||||
//
|
||||
// Application is live and visible: the interface font is installed as a
|
||||
// screen-wide CSS provider ("* { font-family...; font-size... }") and the
|
||||
// code font as a "textview" CSS rule, so Text/Memo views (and any other
|
||||
// GtkTextView) pick it up, as does the rest of the designer UI. Sizes are
|
||||
// clamped to 6-48 pt; an empty or unparseable description falls back to
|
||||
// the default instead of breaking the UI.
|
||||
#pragma once
|
||||
|
||||
#include <gtk/gtk.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
struct FontSettings {
|
||||
std::string font = "Sans 10"; // interface font (Pango description)
|
||||
std::string code_font = "Monospace 11"; // code / text-view font
|
||||
|
||||
static std::string default_path(); // ~/.config/fdesigner/settings.conf
|
||||
bool save(const std::string& path = "") const;
|
||||
bool load(const std::string& path = "");
|
||||
|
||||
// CSS snippets that apply each font (used by preferences_apply()).
|
||||
std::string ui_css() const; // "* { font-family...; font-size... }"
|
||||
std::string code_css() const; // "textview { font-family...; ... }"
|
||||
|
||||
// Helpers, exposed for tests.
|
||||
// Parse desc as a Pango font description; fall back to `fallback`
|
||||
// when it is empty/unparseable, and clamp the size to 6-48 pt.
|
||||
static std::string sanitized(const std::string& desc,
|
||||
const std::string& fallback);
|
||||
static std::string family_of(const std::string& desc); // "" if none
|
||||
static int size_of(const std::string& desc); // pt, 0 if none
|
||||
};
|
||||
|
||||
// Settings currently in effect (loaded from disk on first call).
|
||||
FontSettings preferences_current();
|
||||
// Install the fonts as screen-wide CSS providers (re-appliable).
|
||||
void preferences_apply(const FontSettings& s);
|
||||
// Apply the code font to one text view (e.g. a code display).
|
||||
void preferences_apply_to_text_view(GtkWidget* view, const FontSettings& s);
|
||||
// Pango description of the font a widget is currently showing
|
||||
// ("" if it cannot be determined). Test helper.
|
||||
std::string preferences_applied_font(GtkWidget* widget);
|
||||
// Open the Preferences dialog; on Accept saves the choice, applies it
|
||||
// live, and returns true. On Cancel returns false.
|
||||
bool preferences_open(GtkWidget* parent);
|
||||
@@ -0,0 +1,136 @@
|
||||
// Font settings test harness — exercises src/preferences.cpp directly
|
||||
// (no main window needed). Run under Xvfb: xvfb-run -a ./font_tests
|
||||
// Prints one PASS/FAIL line per check; exit 0 only if all pass.
|
||||
#include <gtk/gtk.h>
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <string>
|
||||
|
||||
#include "preferences.h"
|
||||
|
||||
static int g_pass = 0, g_fail = 0;
|
||||
|
||||
static void check(bool cond, const char* name) {
|
||||
if (cond) {
|
||||
++g_pass;
|
||||
std::printf("PASS %s\n", name);
|
||||
} else {
|
||||
++g_fail;
|
||||
std::printf("FAIL %s\n", name);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
gtk_init(&argc, &argv);
|
||||
|
||||
// 1. Defaults.
|
||||
FontSettings def;
|
||||
check(def.font == "Sans 10", "defaults-ui-font");
|
||||
check(def.code_font == "Monospace 11", "defaults-code-font");
|
||||
|
||||
// 2. Save / load round-trip at an explicit path.
|
||||
FontSettings fs;
|
||||
fs.font = "DejaVu Sans 14";
|
||||
fs.code_font = "DejaVu Sans Mono 13";
|
||||
const char* path = "/tmp/fdesigner_font_harness.conf";
|
||||
check(fs.save(path), "save-roundtrip");
|
||||
FontSettings fs2;
|
||||
check(fs2.load(path), "load-roundtrip");
|
||||
check(fs2.font == "DejaVu Sans 14" &&
|
||||
fs2.code_font == "DejaVu Sans Mono 13",
|
||||
"roundtrip-values");
|
||||
|
||||
// 3. Missing file: load fails, defaults kept.
|
||||
FontSettings fs3;
|
||||
check(!fs3.load("/tmp/fdesigner_font_no_such_file.conf"),
|
||||
"load-missing-fails");
|
||||
check(fs3.font == "Sans 10", "load-missing-keeps-defaults");
|
||||
|
||||
// 4. Garbage in the file does not break loading.
|
||||
{
|
||||
FILE* f = std::fopen("/tmp/fdesigner_font_garbage.conf", "w");
|
||||
std::fprintf(f, "# comment\nnot-a-kv-line\nfont=\ncode_font=\n");
|
||||
std::fclose(f);
|
||||
}
|
||||
FontSettings fs4;
|
||||
check(fs4.load("/tmp/fdesigner_font_garbage.conf"),
|
||||
"load-garbage-file");
|
||||
check(fs4.font == "Sans 10" && fs4.code_font == "Monospace 11",
|
||||
"garbage-falls-back-to-defaults");
|
||||
|
||||
// 5. Sanitising: empty -> fallback, sizes clamped to 6-48 pt.
|
||||
check(FontSettings::sanitized("", "Sans 10") == "Sans 10",
|
||||
"sanitize-empty");
|
||||
check(FontSettings::size_of(FontSettings::sanitized("Sans 99",
|
||||
"Sans 10")) == 48,
|
||||
"sanitize-clamp-high");
|
||||
check(FontSettings::size_of(FontSettings::sanitized("Sans 2",
|
||||
"Sans 10")) == 6,
|
||||
"sanitize-clamp-low");
|
||||
check(FontSettings::family_of(
|
||||
FontSettings::sanitized("DejaVu Sans Mono 13",
|
||||
"Sans 10")) == "DejaVu Sans Mono",
|
||||
"sanitize-keeps-family");
|
||||
|
||||
// 6. Env override for the default path (mirrors debugger.conf).
|
||||
::setenv("FDESIGNER_SETTINGS_CONF", "/tmp/fdesigner_font_env.conf", 1);
|
||||
check(FontSettings::default_path() ==
|
||||
"/tmp/fdesigner_font_env.conf",
|
||||
"env-override-path");
|
||||
::unsetenv("FDESIGNER_SETTINGS_CONF");
|
||||
|
||||
// 7. CSS generation.
|
||||
check(fs.ui_css().find("DejaVu Sans") != std::string::npos &&
|
||||
fs.ui_css().find("14pt") != std::string::npos,
|
||||
"ui-css-content");
|
||||
check(fs.code_css().find("textview") != std::string::npos &&
|
||||
fs.code_css().find("DejaVu Sans Mono") != std::string::npos &&
|
||||
fs.code_css().find("13pt") != std::string::npos,
|
||||
"code-css-content");
|
||||
|
||||
// 8. Live apply to a text view: the widget really shows the font.
|
||||
GtkWidget* tv = gtk_text_view_new();
|
||||
gtk_widget_show(tv);
|
||||
preferences_apply_to_text_view(tv, fs);
|
||||
while (gtk_events_pending()) gtk_main_iteration();
|
||||
std::string applied = preferences_applied_font(tv);
|
||||
check(FontSettings::family_of(applied) == "DejaVu Sans Mono" &&
|
||||
FontSettings::size_of(applied) == 13,
|
||||
"apply-to-text-view");
|
||||
gtk_widget_destroy(tv);
|
||||
|
||||
// 9. Screen-wide apply: a fresh text view and a label pick it up.
|
||||
preferences_apply(fs);
|
||||
GtkWidget* tv2 = gtk_text_view_new();
|
||||
GtkWidget* lbl = gtk_label_new("probe");
|
||||
gtk_widget_show(tv2);
|
||||
gtk_widget_show(lbl);
|
||||
while (gtk_events_pending()) gtk_main_iteration();
|
||||
std::string applied2 = preferences_applied_font(tv2);
|
||||
check(FontSettings::family_of(applied2) == "DejaVu Sans Mono" &&
|
||||
FontSettings::size_of(applied2) == 13,
|
||||
"screen-apply-text-view");
|
||||
std::string applied_lbl = preferences_applied_font(lbl);
|
||||
check(FontSettings::family_of(applied_lbl) == "DejaVu Sans" &&
|
||||
FontSettings::size_of(applied_lbl) == 14,
|
||||
"screen-apply-label");
|
||||
gtk_widget_destroy(tv2);
|
||||
gtk_widget_destroy(lbl);
|
||||
|
||||
// 10. Re-applying defaults restores the defaults (no crash, no leak
|
||||
// of the old providers).
|
||||
preferences_apply(FontSettings());
|
||||
GtkWidget* tv3 = gtk_text_view_new();
|
||||
gtk_widget_show(tv3);
|
||||
while (gtk_events_pending()) gtk_main_iteration();
|
||||
std::string applied3 = preferences_applied_font(tv3);
|
||||
check(FontSettings::family_of(applied3) == "Monospace" &&
|
||||
FontSettings::size_of(applied3) == 11,
|
||||
"reapply-defaults");
|
||||
gtk_widget_destroy(tv3);
|
||||
|
||||
std::printf("FONT TESTS %s pass=%d fail=%d\n",
|
||||
g_fail == 0 ? "PASS" : "FAIL", g_pass, g_fail);
|
||||
return g_fail == 0 ? 0 : 1;
|
||||
}
|
||||
Executable
+92
@@ -0,0 +1,92 @@
|
||||
#!/bin/sh
|
||||
# FDesigner font settings tests.
|
||||
# tests/font_tests.sh
|
||||
# Builds + runs the preferences harness (tests/font_tests.cpp, needs Xvfb),
|
||||
# runs the fdesigner self-test (which has its own font block), and smoke
|
||||
# tests the real Preferences dialog (FDESIGNER_OPEN_PREFERENCES=1) with
|
||||
# xdotool if available. Prints a PASS/FAIL summary; exits non-zero on
|
||||
# any failure. Run from the repo root.
|
||||
set -u
|
||||
ROOT=$(cd "$(dirname "$0")/.." && pwd)
|
||||
cd "$ROOT"
|
||||
FAIL=0
|
||||
say() { printf '%s\n' "$*"; }
|
||||
|
||||
say "== building font harness =="
|
||||
g++ -O2 -Wall -Wextra -std=c++17 -I src -o /tmp/font_tests tests/font_tests.cpp \
|
||||
src/preferences.cpp $(pkg-config --cflags --libs gtk+-3.0) \
|
||||
2>/tmp/font_tests_build.log || {
|
||||
say "FAIL font harness build"; cat /tmp/font_tests_build.log; exit 1
|
||||
}
|
||||
say "== font harness (under Xvfb) =="
|
||||
xvfb-run -a /tmp/font_tests || FAIL=1
|
||||
|
||||
say "== building fdesigner =="
|
||||
make >/tmp/font_make.log 2>&1 || {
|
||||
say "FAIL fdesigner build"; cat /tmp/font_make.log; FAIL=1
|
||||
}
|
||||
if [ "$FAIL" = "0" ]; then
|
||||
say "== fdesigner self-test (includes font block) =="
|
||||
FDESIGNER_SELFTEST=1 xvfb-run -a ./fdesigner || FAIL=1
|
||||
fi
|
||||
|
||||
say "== Preferences dialog smoke test =="
|
||||
if ! command -v xdotool >/dev/null 2>&1; then
|
||||
say "SKIP dialog smoke test (xdotool not installed)"
|
||||
else
|
||||
# Launch under a fresh Xvfb; the env hook opens the dialog at startup.
|
||||
# The dialog is modal, so the app stays up until we close it with Esc.
|
||||
xvfb-run -a sh -c '
|
||||
FDESIGNER_OPEN_PREFERENCES=1 ./fdesigner >/tmp/font_dlg.log 2>&1 &
|
||||
APP=$!
|
||||
FOUND=""
|
||||
i=0
|
||||
while [ $i -lt 50 ]; do
|
||||
FOUND=$(xdotool search --name "^Preferences$" 2>/dev/null | head -1)
|
||||
[ -n "$FOUND" ] && break
|
||||
sleep 0.2; i=$((i+1))
|
||||
done
|
||||
if [ -n "$FOUND" ]; then
|
||||
echo "PASS dialog-opened (window $FOUND)"
|
||||
xdotool windowactivate "$FOUND" key Escape
|
||||
sleep 0.5
|
||||
else
|
||||
echo "FAIL dialog-did-not-open"
|
||||
xdotool search --name ".*" getwindowname %@ 2>/dev/null | head -5
|
||||
fi
|
||||
kill $APP 2>/dev/null
|
||||
wait $APP 2>/dev/null
|
||||
[ -n "$FOUND" ]
|
||||
' || FAIL=1
|
||||
# The Edit -> Preferences menu item must exist in the .ui and be wired.
|
||||
grep -q 'id="mi_preferences"' ui/designer.ui &&
|
||||
grep -q 'mi_preferences' src/main.cpp &&
|
||||
say "PASS menu-item-wired" ||
|
||||
{ say "FAIL menu-item-wired"; FAIL=1; }
|
||||
# Apply path: open the dialog with a scratch conf path, press Alt+A
|
||||
# (the _Apply mnemonic), and check the file was written.
|
||||
say "== Preferences dialog Apply test =="
|
||||
rm -f /tmp/font_gui_apply.conf
|
||||
xvfb-run -a sh -c '
|
||||
FDESIGNER_SETTINGS_CONF=/tmp/font_gui_apply.conf FDESIGNER_OPEN_PREFERENCES=1 ./fdesigner >/tmp/font_apply.log 2>&1 &
|
||||
APP=$!
|
||||
FOUND=""
|
||||
i=0
|
||||
while [ $i -lt 50 ]; do
|
||||
FOUND=$(xdotool search --name "^Preferences$" 2>/dev/null | head -1)
|
||||
[ -n "$FOUND" ] && break
|
||||
sleep 0.2; i=$((i+1))
|
||||
done
|
||||
[ -n "$FOUND" ] && xdotool windowfocus "$FOUND" key alt+a
|
||||
sleep 1
|
||||
kill $APP 2>/dev/null
|
||||
wait $APP 2>/dev/null
|
||||
'
|
||||
grep -q '^font=Sans 10$' /tmp/font_gui_apply.conf &&
|
||||
grep -q '^code_font=Monospace 11$' /tmp/font_gui_apply.conf &&
|
||||
say "PASS dialog-apply-saves" ||
|
||||
{ say "FAIL dialog-apply-saves"; cat /tmp/font_gui_apply.conf 2>/dev/null; FAIL=1; }
|
||||
fi
|
||||
|
||||
if [ "$FAIL" = "0" ]; then say "FONT TESTS: ALL PASS"; else say "FONT TESTS: FAILURES (see above)"; fi
|
||||
exit "$FAIL"
|
||||
@@ -64,6 +64,23 @@
|
||||
</child>
|
||||
</object>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkMenuItem" id="menu_edit">
|
||||
<property name="label">Edit</property>
|
||||
<property name="visible">True</property>
|
||||
<child type="submenu">
|
||||
<object class="GtkMenu" id="edit_menu">
|
||||
<property name="visible">True</property>
|
||||
<child>
|
||||
<object class="GtkMenuItem" id="mi_preferences">
|
||||
<property name="label">Preferences…</property>
|
||||
<property name="visible">True</property>
|
||||
</object>
|
||||
</child>
|
||||
</object>
|
||||
</child>
|
||||
</object>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkMenuItem" id="menu_debug">
|
||||
<property name="label">Debug</property>
|
||||
|
||||
Reference in new issue
Block a user