From 9ab54a79d969a501b0ce96fb9bcaf26c98fc8317 Mon Sep 17 00:00:00 2001 From: Ewerton Mattos Date: Thu, 15 Oct 2026 18:58:16 -0300 Subject: [PATCH] feat: choose how inline suggestions are marked ([ghost] inline_format) Inline suggestions were always underlined. [ghost] inline_format now picks the preedit format: underline (default), highlight, bold, italic or none, and the settings app's General page gets an "Inline look" choice. Only apps that talk to Fcitx5 through its Qt or GTK module apply the format; native Wayland text-input-v3 clients (Firefox, Electron, GTK without the module) get no styling and keep their own underline. Co-Authored-By: Claude Opus 5.5 --- fcitx5-engine/src/config.h | 11 +++++++++ fcitx5-engine/src/config_loader.cpp | 11 +++++++++ fcitx5-engine/src/xtype.cpp | 18 ++++++++++++--- fcitx5-engine/tests/fixtures/config_full.toml | 3 +++ fcitx5-engine/tests/test_config_loader.cpp | 2 ++ settings/qt6-app/qml/pages/PageGeneral.qml | 23 +++++++++++++++++++ settings/qt6-app/src/config_store.cpp | 13 +++++++++++ settings/qt6-app/src/config_store.h | 8 ++++++- settings/qt6-app/tests/test_config_store.cpp | 17 ++++++++++++++ 9 files changed, 102 insertions(+), 4 deletions(-) diff --git a/fcitx5-engine/src/config.h b/fcitx5-engine/src/config.h index cc48d27..65dacd0 100644 --- a/fcitx5-engine/src/config.h +++ b/fcitx5-engine/src/config.h @@ -7,6 +7,11 @@ enum class TriggerMode { Pause, Manual }; enum class AcceptKey { Tab, Enter, Right }; +// How inline suggestions are marked in the app's text. Only apps that talk to +// Fcitx5 through its Qt or GTK module apply it; native Wayland text-input-v3 +// clients (GTK without the module, Firefox, Electron) carry no styling and +// draw their own underline whatever is chosen. +enum class InlineFormat { Underline, Highlight, Bold, Italic, None }; struct InferenceConfig { std::string model = "qwen2.5:1.5b"; @@ -61,9 +66,15 @@ struct AppOverride { std::optional prompt_addendum; }; +// [ghost]: how suggestions are shown. +struct GhostConfig { + InlineFormat inline_format = InlineFormat::Underline; +}; + struct XTypeConfig { InferenceConfig inference; BehaviourConfig behaviour; + GhostConfig ghost; LearningConfig learning; UserPromptConfig user_prompt; std::map apps; diff --git a/fcitx5-engine/src/config_loader.cpp b/fcitx5-engine/src/config_loader.cpp index 0796c11..9a30fb0 100644 --- a/fcitx5-engine/src/config_loader.cpp +++ b/fcitx5-engine/src/config_loader.cpp @@ -80,6 +80,17 @@ XTypeConfig config_loader::load(const std::string& path) { } } + // [ghost] + if (auto* t = tbl["ghost"].as_table()) { + if (auto v = (*t)["inline_format"].value()) { + if (*v == "highlight") cfg.ghost.inline_format = InlineFormat::Highlight; + else if (*v == "bold") cfg.ghost.inline_format = InlineFormat::Bold; + else if (*v == "italic") cfg.ghost.inline_format = InlineFormat::Italic; + else if (*v == "none") cfg.ghost.inline_format = InlineFormat::None; + else cfg.ghost.inline_format = InlineFormat::Underline; + } + } + // [learning] if (auto* t = tbl["learning"].as_table()) { if (auto v = (*t)["enabled"].value()) cfg.learning.enabled = *v; diff --git a/fcitx5-engine/src/xtype.cpp b/fcitx5-engine/src/xtype.cpp index 3447540..eb61d79 100644 --- a/fcitx5-engine/src/xtype.cpp +++ b/fcitx5-engine/src/xtype.cpp @@ -471,12 +471,24 @@ void XTypeEngine::requestInference( // ── Preedit ─────────────────────────────────────────────────────────────────── +// Preedit flags for an inline suggestion. DontCommit keeps the app from +// committing the ghost text itself (e.g. when focus moves away). +static fcitx::TextFormatFlags inlineFlags(InlineFormat f) { + fcitx::TextFormatFlags flags{fcitx::TextFormatFlag::DontCommit}; + switch (f) { + case InlineFormat::Underline: flags |= fcitx::TextFormatFlag::Underline; break; + case InlineFormat::Highlight: flags |= fcitx::TextFormatFlag::HighLight; break; + case InlineFormat::Bold: flags |= fcitx::TextFormatFlag::Bold; break; + case InlineFormat::Italic: flags |= fcitx::TextFormatFlag::Italic; break; + case InlineFormat::None: break; + } + return flags; +} + void XTypeEngine::updatePreedit(fcitx::InputContext *ic) { auto &panel = ic->inputPanel(); if (_ctx.hasSuggestion()) { - fcitx::Text t(*_ctx.suggestion(), - fcitx::TextFormatFlags{fcitx::TextFormatFlag::Underline, - fcitx::TextFormatFlag::DontCommit}); + fcitx::Text t(*_ctx.suggestion(), inlineFlags(_cfg.ghost.inline_format)); panel.setClientPreedit(t); } else { panel.setClientPreedit(fcitx::Text{}); diff --git a/fcitx5-engine/tests/fixtures/config_full.toml b/fcitx5-engine/tests/fixtures/config_full.toml index eba0187..f119ea0 100644 --- a/fcitx5-engine/tests/fixtures/config_full.toml +++ b/fcitx5-engine/tests/fixtures/config_full.toml @@ -20,6 +20,9 @@ passthrough_terminals = false blocklist_apps = ["kate", "subl"] blocked_phrases = ["pursuant to", "hereinafter"] +[ghost] +inline_format = "italic" + [learning] enabled = true corpus_path = "~/.local/share/xtype/corpus2.txt" diff --git a/fcitx5-engine/tests/test_config_loader.cpp b/fcitx5-engine/tests/test_config_loader.cpp index e8d0a77..6fbff22 100644 --- a/fcitx5-engine/tests/test_config_loader.cpp +++ b/fcitx5-engine/tests/test_config_loader.cpp @@ -9,6 +9,7 @@ TEST_CASE("empty file returns all defaults") { CHECK(cfg.inference.temperature == Catch::Approx(0.3f).margin(0.001)); CHECK(cfg.behaviour.engine_enabled == true); CHECK(cfg.behaviour.accept_full_key == AcceptKey::Tab); + CHECK(cfg.ghost.inline_format == InlineFormat::Underline); CHECK(cfg.learning.enabled == false); CHECK(cfg.user_prompt.description.empty()); CHECK(cfg.apps.empty()); @@ -29,6 +30,7 @@ TEST_CASE("full config round-trips every field") { CHECK(cfg.behaviour.partial_accept == false); CHECK(cfg.behaviour.blocklist_apps == std::vector{"kate", "subl"}); CHECK(cfg.behaviour.blocked_phrases == std::vector{"pursuant to", "hereinafter"}); + CHECK(cfg.ghost.inline_format == InlineFormat::Italic); CHECK(cfg.learning.enabled == true); CHECK(cfg.learning.max_corpus_mb == 25); CHECK(cfg.learning.voice_strength == 80); diff --git a/settings/qt6-app/qml/pages/PageGeneral.qml b/settings/qt6-app/qml/pages/PageGeneral.qml index 0f4d4c8..2f0f9af 100644 --- a/settings/qt6-app/qml/pages/PageGeneral.qml +++ b/settings/qt6-app/qml/pages/PageGeneral.qml @@ -93,6 +93,29 @@ Item { } } } + XRow { + label: "Inline look" + desc: "Qt apps only; Firefox, Electron and most GTK apps always underline." + width: parent.width + Row { + spacing: 6 + Repeater { + model: [ + { label: "Underline", value: "underline" }, + { label: "Highlight", value: "highlight" }, + { label: "Bold", value: "bold" }, + { label: "Italic", value: "italic" }, + { label: "None", value: "none" }, + ] + delegate: XPill { + required property var modelData + label: modelData.label + on: Config.ghostInlineFormat === modelData.value + onClicked: Config.ghostInlineFormat = modelData.value + } + } + } + } XRow { label: "Partial accept (word-by-word)" desc: "Accept one word at a time" diff --git a/settings/qt6-app/src/config_store.cpp b/settings/qt6-app/src/config_store.cpp index 95dc487..650c239 100644 --- a/settings/qt6-app/src/config_store.cpp +++ b/settings/qt6-app/src/config_store.cpp @@ -104,6 +104,11 @@ void ConfigStore::load() { } } + // --- [ghost] --- + if (auto *g = root["ghost"].as_table()) { + if (auto v = (*g)["inline_format"].value()) _ghostInlineFormat = QString::fromStdString(*v); + } + // --- [learning] --- if (auto *lrn = root["learning"].as_table()) { if (auto v = (*lrn)["enabled"].value()) _learningEnabled = *v; @@ -189,6 +194,11 @@ void ConfigStore::writeToml(const QString &path) { writeStringArray(beh, "blocked_phrases", _blockedPhrases); root.insert_or_assign("behaviour", std::move(beh)); + // [ghost] + toml::table ghost; + ghost.insert_or_assign("inline_format", _ghostInlineFormat.toStdString()); + root.insert_or_assign("ghost", std::move(ghost)); + // [learning] toml::table lrn; lrn.insert_or_assign("enabled", _learningEnabled); @@ -263,6 +273,7 @@ void ConfigStore::resetAll() { _engineEnabled = true; _triggerMode = "pause"; _acceptKey = "tab"; + _ghostInlineFormat = "underline"; _partialAccept = true; _escDismisses = true; _passThroughTerminals = true; @@ -306,6 +317,7 @@ void ConfigStore::resetAll() { emit engineEnabledChanged(); emit triggerModeChanged(); emit acceptKeyChanged(); + emit ghostInlineFormatChanged(); emit partialAcceptChanged(); emit escDismissesChanged(); emit passThroughTerminalsChanged(); @@ -349,6 +361,7 @@ void ConfigStore::setEscDismisses(bool v) { CS_SET(escDismisses, void ConfigStore::setPassThroughTerminals(bool v) { CS_SET(passThroughTerminals, passThroughTerminalsChanged); } void ConfigStore::setTriggerMode(const QString &v) { CS_SET(triggerMode, triggerModeChanged); } void ConfigStore::setAcceptKey(const QString &v) { CS_SET(acceptKey, acceptKeyChanged); } +void ConfigStore::setGhostInlineFormat(const QString &v){ CS_SET(ghostInlineFormat, ghostInlineFormatChanged); } void ConfigStore::setBlocklistApps(const QStringList &v){ CS_SET(blocklistApps, blocklistAppsChanged); } void ConfigStore::setBlockedPhrases(const QStringList &v){ CS_SET(blockedPhrases, blockedPhrasesChanged); } diff --git a/settings/qt6-app/src/config_store.h b/settings/qt6-app/src/config_store.h index bcc64d3..89b36b7 100644 --- a/settings/qt6-app/src/config_store.h +++ b/settings/qt6-app/src/config_store.h @@ -8,7 +8,8 @@ #include // TOML key names mirror XTypeConfig struct fields exactly (snake_case). -// Enum strings: TriggerMode → "pause"|"manual"; AcceptKey → "tab"|"enter"|"right". +// Enum strings: TriggerMode → "pause"|"manual"; AcceptKey → "tab"|"enter"|"right"; +// [ghost] inline_format → "underline"|"highlight"|"bold"|"italic"|"none". // std::optional round-trip: absent key ↔ invalid QVariant / empty optional. class ConfigStore : public QObject { @@ -18,6 +19,7 @@ class ConfigStore : public QObject { Q_PROPERTY(bool engineEnabled READ engineEnabled WRITE setEngineEnabled NOTIFY engineEnabledChanged) Q_PROPERTY(QString triggerMode READ triggerMode WRITE setTriggerMode NOTIFY triggerModeChanged) Q_PROPERTY(QString acceptKey READ acceptKey WRITE setAcceptKey NOTIFY acceptKeyChanged) + Q_PROPERTY(QString ghostInlineFormat READ ghostInlineFormat WRITE setGhostInlineFormat NOTIFY ghostInlineFormatChanged) Q_PROPERTY(bool partialAccept READ partialAccept WRITE setPartialAccept NOTIFY partialAcceptChanged) Q_PROPERTY(bool escDismisses READ escDismisses WRITE setEscDismisses NOTIFY escDismissesChanged) Q_PROPERTY(bool passThroughTerminals READ passThroughTerminals WRITE setPassThroughTerminals NOTIFY passThroughTerminalsChanged) @@ -67,6 +69,7 @@ class ConfigStore : public QObject { bool engineEnabled() const { return _engineEnabled; } QString triggerMode() const { return _triggerMode; } QString acceptKey() const { return _acceptKey; } + QString ghostInlineFormat() const { return _ghostInlineFormat; } bool partialAccept() const { return _partialAccept; } bool escDismisses() const { return _escDismisses; } bool passThroughTerminals() const { return _passThroughTerminals; } @@ -103,6 +106,7 @@ class ConfigStore : public QObject { void setEngineEnabled(bool v); void setTriggerMode(const QString &v); void setAcceptKey(const QString &v); + void setGhostInlineFormat(const QString &v); void setPartialAccept(bool v); void setEscDismisses(bool v); void setPassThroughTerminals(bool v); @@ -151,6 +155,7 @@ public slots: void engineEnabledChanged(); void triggerModeChanged(); void acceptKeyChanged(); + void ghostInlineFormatChanged(); void partialAcceptChanged(); void escDismissesChanged(); void passThroughTerminalsChanged(); @@ -194,6 +199,7 @@ public slots: bool _engineEnabled = true; QString _triggerMode = "pause"; QString _acceptKey = "tab"; + QString _ghostInlineFormat = "underline"; bool _partialAccept = true; bool _escDismisses = true; bool _passThroughTerminals = true; diff --git a/settings/qt6-app/tests/test_config_store.cpp b/settings/qt6-app/tests/test_config_store.cpp index 1925c76..9a31bc8 100644 --- a/settings/qt6-app/tests/test_config_store.cpp +++ b/settings/qt6-app/tests/test_config_store.cpp @@ -257,3 +257,20 @@ TEST_CASE("resetAll: backup file exists, main file reverts to defaults", "[confi CHECK(cs2.model() == "qwen2.5:1.5b"); CHECK(cs2.debounceMs() == 220); } + +TEST_CASE("[ghost] inline_format round-trip and default", "[config_store]") { + QTemporaryDir tmp; REQUIRE(tmp.isValid()); + const QString dst = tmp.filePath("config.toml"); + + { + ConfigStore cs(dst); + cs.load(); + CHECK(cs.ghostInlineFormat() == "underline"); + cs.setGhostInlineFormat("italic"); + cs.saveNow(); + } + + ConfigStore cs2(dst); + cs2.load(); + CHECK(cs2.ghostInlineFormat() == "italic"); +}