diff --git a/README.md b/README.md index 6d97222..76c3c59 100644 --- a/README.md +++ b/README.md @@ -225,6 +225,13 @@ partial_accept = true esc_dismisses = true blocklist_apps = ["konsole", "alacritty", "keepassxc", "1password"] +[ghost] +style = "inline" # "inline": in the app's text (the app draws it, usually underlined) + # "popup": gray text drawn by Fcitx5 at the caret +popup_font = "Sans 11" # popup only: font of the gray text +popup_theme = "xtype-ghost" # popup only: Fcitx5 theme used while popup is on +hyprland_inline = false # popup + Hyprland only: show the popup on the caret line + [learning] enabled = false # opt-in corpus_path = "~/.local/share/xtype/corpus.txt" @@ -250,6 +257,8 @@ mode = "Default" | `min_context_chars` | Minimum buffered characters before the engine asks for a suggestion. | | `context_window` | Sliding-window size (chars) handed to the model as prefill. | | `blocklist_apps` | Case-insensitive component match against `ic->program()`. | +| `ghost.style` | `"inline"` keeps the app's own (underlined) preedit. `"popup"` shows gray ghost text through Fcitx5's popup; XType switches Fcitx5's classic UI to `popup_theme`/`popup_font` and restores your own theme when you switch back. | +| `ghost.hyprland_inline` | Hyprland only. Loads the bundled `xtype-inline` plugin, which moves the popup from below the caret onto the caret line. **Not every app benefits:** it only affects native Wayland apps using the text-input protocol, and only lines up where the app reports its caret correctly. X11/XWayland apps and Qt apps using Fcitx5's Qt module (`QT_IM_MODULE=fcitx`) draw their popup elsewhere, so it stays below the caret; Electron/Chromium apps (VS Code, Teams, Edge, …) report a stale caret, so the popup lags behind the text. Use the inline style for those apps. The plugin is built against the installed Hyprland headers — rebuild and reinstall XType after Hyprland updates. | | `learning.voice_strength` | 0 = no corpus examples in prompt; 100 = all exemplars included; proportional. | | `learning.forget_after_days` | Prune corpus entries older than N days. 0 disables pruning. | | `user_prompt.description` | Free-text description of yourself injected into the system prompt (≤ 500 chars). | diff --git a/fcitx5-engine/CMakeLists.txt b/fcitx5-engine/CMakeLists.txt index 8326f18..c1f6460 100644 --- a/fcitx5-engine/CMakeLists.txt +++ b/fcitx5-engine/CMakeLists.txt @@ -44,6 +44,7 @@ set_target_properties(config_loader_obj PROPERTIES POSITION_INDEPENDENT_CODE ON) add_library(xtype-fcitx5 SHARED src/xtype.cpp src/inference_client.cpp + src/ghost_popup.cpp ) target_include_directories(xtype-fcitx5 PRIVATE src) target_link_libraries(xtype-fcitx5 PRIVATE @@ -67,6 +68,27 @@ install(TARGETS xtype-fcitx5 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/data/xtype.conf" DESTINATION "${FCITX_INSTALL_PKGDATADIR}/addon" ) +# Popup ghost style: gray-text Fcitx5 theme, selected by the engine while +# ghost.style = "popup" (see src/ghost_popup.h). +install(FILES data/themes/xtype-ghost/theme.conf + DESTINATION "${FCITX_INSTALL_PKGDATADIR}/themes/xtype-ghost" +) + +# Optional Hyprland plugin for ghost.hyprland_inline. Built when the Hyprland +# plugin headers are installed; must be rebuilt after every Hyprland update. +find_package(PkgConfig QUIET) +if(PkgConfig_FOUND) + pkg_check_modules(HYPRLAND_PLUGIN_DEPS QUIET IMPORTED_TARGET + hyprland pixman-1 libdrm libinput libudev wayland-server xkbcommon) +endif() +option(XTYPE_HYPRLAND_PLUGIN "Build the xtype-inline Hyprland plugin" ${HYPRLAND_PLUGIN_DEPS_FOUND}) +if(XTYPE_HYPRLAND_PLUGIN) + set(XTYPE_HYPRLAND_PLUGIN_DIR "${CMAKE_INSTALL_PREFIX}/lib/xtype") + add_subdirectory(hyprland-plugin) + target_compile_definitions(xtype-fcitx5 PRIVATE + XTYPE_HYPRLAND_PLUGIN_PATH="${XTYPE_HYPRLAND_PLUGIN_DIR}/xtype-inline.so") +endif() + install(FILES data/xtype-im.conf DESTINATION "${FCITX_INSTALL_PKGDATADIR}/inputmethod" RENAME xtype.conf diff --git a/fcitx5-engine/data/themes/xtype-ghost/theme.conf b/fcitx5-engine/data/themes/xtype-ghost/theme.conf new file mode 100644 index 0000000..c5011c3 --- /dev/null +++ b/fcitx5-engine/data/themes/xtype-ghost/theme.conf @@ -0,0 +1,84 @@ +# XType ghost theme. Fcitx5's input panel as bare gray text on a +# transparent background, so XType's popup ghost style reads as ghost text. +# Applies to every Fcitx5 popup while selected (Theme=xtype-ghost in +# ~/.config/fcitx5/conf/classicui.conf). +[Metadata] +Name=XType Ghost +Version=1 +Author=XType +Description=Gray ghost text on a transparent background (XType popup suggestions) +ScaleWithDPI=True + +[InputPanel] +NormalColor=#8c8c8c +HighlightCandidateColor=#8c8c8c +HighlightColor=#8c8c8c +HighlightBackgroundColor=#00000000 +EnableBlur=False + +[InputPanel/TextMargin] +Left=0 +Right=0 +Top=0 +Bottom=0 + +[InputPanel/ContentMargin] +Left=0 +Right=0 +Top=0 +Bottom=0 + +[InputPanel/Background] +Color=#00000000 +BorderColor=#00000000 +BorderWidth=0 + +[InputPanel/Background/Margin] +Left=0 +Right=0 +Top=0 +Bottom=0 + +[InputPanel/Highlight] +Color=#00000000 + +[InputPanel/Highlight/Margin] +Left=0 +Right=0 +Top=0 +Bottom=0 + +[Menu/Background] +Color=#202020 +BorderColor=#404040 +BorderWidth=1 + +[Menu/Background/Margin] +Left=2 +Right=2 +Top=2 +Bottom=2 + +[Menu/ContentMargin] +Left=2 +Right=2 +Top=2 +Bottom=2 + +[Menu/Highlight] +Color=#404040 + +[Menu/Highlight/Margin] +Left=5 +Right=5 +Top=5 +Bottom=5 + +[Menu/Separator] +Color=#404040 + +[Menu/TextMargin] +Left=5 +Right=5 +Top=5 +Bottom=5 diff --git a/fcitx5-engine/hyprland-plugin/CMakeLists.txt b/fcitx5-engine/hyprland-plugin/CMakeLists.txt new file mode 100644 index 0000000..28b47ee --- /dev/null +++ b/fcitx5-engine/hyprland-plugin/CMakeLists.txt @@ -0,0 +1,9 @@ +# xtype-inline: Hyprland plugin for ghost.hyprland_inline (see main.cpp). +# Plugins link against the running compositor's internals, so this must be +# built with the headers of the Hyprland version that loads it. +add_library(xtype-inline MODULE main.cpp) +set_target_properties(xtype-inline PROPERTIES PREFIX "" CXX_STANDARD 23) +target_compile_options(xtype-inline PRIVATE -Wno-c++11-narrowing + $<$:--no-gnu-unique>) +target_link_libraries(xtype-inline PRIVATE PkgConfig::HYPRLAND_PLUGIN_DEPS) +install(TARGETS xtype-inline LIBRARY DESTINATION "${XTYPE_HYPRLAND_PLUGIN_DIR}") diff --git a/fcitx5-engine/hyprland-plugin/main.cpp b/fcitx5-engine/hyprland-plugin/main.cpp new file mode 100644 index 0000000..fa5ca78 --- /dev/null +++ b/fcitx5-engine/hyprland-plugin/main.cpp @@ -0,0 +1,150 @@ +// xtype-inline — Hyprland plugin for XType's popup ghost style. +// +// Hyprland places input-method popups *below* the text caret (see +// CInputPopup::updateBox). With XType's popup ghost style that popup holds the +// suggestion, so moving it onto the caret's own line — starting right after the +// caret and vertically centred on it — makes it read as inline ghost text. +// +// Every consumer of the popup position (rendering, damage, pointer hit-tests) +// goes through CInputPopup::globalBox(), so hooking that one function is +// enough. isVecInPopup() is hooked too so the popup is click-through and never +// steals clicks from the text underneath it. +// +// The caret comes from the app (text-input cursor rectangle). When the app +// reports none, or one that can't be right (no height, outside its own +// surface), the popup is hidden instead of drawn somewhere unrelated. +// +// Debug: with ~/.local/share/xtype/.debug_verbose present when the plugin +// loads, every change of caret / surface / popup box is appended to +// ~/.local/share/xtype/hyprland-inline.log. +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +inline HANDLE PHANDLE = nullptr; +static CFunctionHook* g_globalBoxHook = nullptr; +static CFunctionHook* g_isVecInPopupHook = nullptr; + +using GlobalBoxFn = CBox (*)(CInputPopup*); +using IsVecInPopupFn = bool (*)(CInputPopup*, const Vector2D&); + +static FILE* g_debugLog = nullptr; // open only in debug mode +static std::string g_lastLogged; + +static void debugLog(const std::string& line) { + if (!g_debugLog || line == g_lastLogged) + return; // globalBox runs every frame; log changes only + g_lastLogged = line; + std::fprintf(g_debugLog, "%s\n", line.c_str()); + std::fflush(g_debugLog); +} + +static std::string str(const CBox& b) { + return std::format("{:.0f},{:.0f} {:.0f}x{:.0f}", b.x, b.y, b.w, b.h); +} + +// A box far outside every monitor: the popup is still mapped but not visible. +static CBox offscreen(CBox box) { + box.x = -100000; + box.y = -100000; + return box; +} + +static CBox hkGlobalBox(CInputPopup* self) { + CBox box = reinterpret_cast(g_globalBoxHook->m_original)(self); + + const auto TI = g_pInputManager->m_relay.getFocusedTextInput(); + if (!TI) + return box; // not attached to a text input: leave Hyprland's placement + if (!TI->hasCursorRectangle()) { + if (g_debugLog) debugLog("hide: app reports no caret"); + return offscreen(box); + } + + const auto SURF = Desktop::View::CWLSurface::fromResource(TI->focusedSurface()); + if (!SURF) + return box; + const auto PARENT = SURF->getSurfaceBoxGlobal(); + if (!PARENT) + return box; + + const CBox CARET = TI->cursorBox(); // surface-local + const bool plausible = CARET.h > 0 && CARET.x >= 0 && CARET.y >= 0 && + CARET.x <= PARENT->w && CARET.y + CARET.h <= PARENT->h; + if (!plausible) { + if (g_debugLog) debugLog("hide: caret " + str(CARET) + " outside surface " + str(*PARENT)); + return offscreen(box); + } + + box.x = PARENT->x + CARET.x + CARET.w; + box.y = PARENT->y + CARET.y + (CARET.h - box.h) / 2.0; + if (g_debugLog) debugLog("caret " + str(CARET) + " surface " + str(*PARENT) + " popup " + str(box)); + return box; +} + +static bool hkIsVecInPopup(CInputPopup*, const Vector2D&) { + return false; +} + +static void* findMember(const std::string& name, const std::string& qualified) { + for (const auto& m : HyprlandAPI::findFunctionsByName(PHANDLE, name)) + if (m.demangled.find(qualified) != std::string::npos) + return m.address; + return nullptr; +} + +APICALL EXPORT std::string PLUGIN_API_VERSION() { + return HYPRLAND_API_VERSION; +} + +APICALL EXPORT PLUGIN_DESCRIPTION_INFO PLUGIN_INIT(HANDLE handle) { + PHANDLE = handle; + + if (std::string(__hyprland_api_get_hash()) != __hyprland_api_get_client_hash()) { + HyprlandAPI::addNotification(PHANDLE, "[xtype-inline] Version mismatch: rebuild against the running Hyprland", + CHyprColor{1.0, 0.2, 0.2, 1.0}, 5000); + throw std::runtime_error("[xtype-inline] version mismatch"); + } + + void* globalBox = findMember("globalBox", "CInputPopup::globalBox"); + void* isVecInPopup = findMember("isVecInPopup", "CInputPopup::isVecInPopup"); + if (!globalBox || !isVecInPopup) { + HyprlandAPI::addNotification(PHANDLE, "[xtype-inline] CInputPopup functions not found; plugin inactive", + CHyprColor{1.0, 0.2, 0.2, 1.0}, 5000); + throw std::runtime_error("[xtype-inline] symbols not found"); + } + + g_globalBoxHook = HyprlandAPI::createFunctionHook(PHANDLE, globalBox, reinterpret_cast(&hkGlobalBox)); + g_isVecInPopupHook = HyprlandAPI::createFunctionHook(PHANDLE, isVecInPopup, reinterpret_cast(&hkIsVecInPopup)); + if (!g_globalBoxHook->hook() || !g_isVecInPopupHook->hook()) + throw std::runtime_error("[xtype-inline] failed to hook"); + + if (const char* home = std::getenv("HOME")) { + const std::filesystem::path dir = std::filesystem::path(home) / ".local/share/xtype"; + std::error_code ec; + if (std::filesystem::exists(dir / ".debug_verbose", ec)) + g_debugLog = std::fopen((dir / "hyprland-inline.log").c_str(), "a"); + } + + HyprlandAPI::addNotification(PHANDLE, "[xtype-inline] loaded: input-method popups now sit on the caret line", + CHyprColor{0.52, 0.44, 0.9, 1.0}, 3000); + return {"xtype-inline", "Places input-method popups on the caret line (XType popup suggestions)", "XType", "0.1"}; +} + +APICALL EXPORT void PLUGIN_EXIT() { + // Hooks created through HyprlandAPI are removed automatically on unload. + if (g_debugLog) { + std::fclose(g_debugLog); + g_debugLog = nullptr; + } +} diff --git a/fcitx5-engine/src/config.h b/fcitx5-engine/src/config.h index cc48d27..1914892 100644 --- a/fcitx5-engine/src/config.h +++ b/fcitx5-engine/src/config.h @@ -6,6 +6,11 @@ #include enum class TriggerMode { Pause, Manual }; +// How the suggestion is shown. Inline: as preedit in the app's own text field +// (the app styles it — usually underlined). Popup: drawn by Fcitx5's UI in a +// popup at the caret, so a Fcitx5 theme controls its look (gray text). +enum class GhostStyle { Inline, Popup }; + enum class AcceptKey { Tab, Enter, Right }; struct InferenceConfig { @@ -59,11 +64,26 @@ struct AppOverride { std::optional num_predict; std::optional mode; // Default/Code-aware/Email tone/Casual/Off std::optional prompt_addendum; + std::optional ghost_style; // overrides [ghost] style for this app +}; + +struct GhostConfig { + GhostStyle style = GhostStyle::Inline; + // Popup style only. While it is on, XType switches Fcitx5's classic UI to + // popup_theme / popup_font and restores the user's own settings after. + std::string popup_font = "Sans 11"; + std::string popup_theme = "xtype-ghost"; + // Popup style on Hyprland only: load the xtype-inline Hyprland plugin, + // which moves the popup from below the caret onto the caret's line. Only + // native Wayland text-input apps are affected (not X11, nor Qt apps using + // Fcitx5's Qt module), and Chromium-based apps report a stale caret. + bool hyprland_inline = false; }; 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..b091af2 100644 --- a/fcitx5-engine/src/config_loader.cpp +++ b/fcitx5-engine/src/config_loader.cpp @@ -80,6 +80,15 @@ XTypeConfig config_loader::load(const std::string& path) { } } + // [ghost] + if (auto* t = tbl["ghost"].as_table()) { + if (auto v = (*t)["style"].value()) + cfg.ghost.style = (*v == "popup") ? GhostStyle::Popup : GhostStyle::Inline; + if (auto v = (*t)["popup_font"].value()) cfg.ghost.popup_font = *v; + if (auto v = (*t)["popup_theme"].value()) cfg.ghost.popup_theme = *v; + if (auto v = (*t)["hyprland_inline"].value()) cfg.ghost.hyprland_inline = *v; + } + // [learning] if (auto* t = tbl["learning"].as_table()) { if (auto v = (*t)["enabled"].value()) cfg.learning.enabled = *v; @@ -116,6 +125,8 @@ XTypeConfig config_loader::load(const std::string& path) { if (auto v = (*at)["num_predict"].value()) ov.num_predict = (int)*v; if (auto v = (*at)["mode"].value()) ov.mode = *v; if (auto v = (*at)["prompt_addendum"].value()) ov.prompt_addendum = *v; + if (auto v = (*at)["ghost_style"].value()) + ov.ghost_style = (*v == "popup") ? GhostStyle::Popup : GhostStyle::Inline; cfg.apps[std::string(key.str())] = ov; } } @@ -123,3 +134,14 @@ XTypeConfig config_loader::load(const std::string& path) { return cfg; } + +GhostStyle config_loader::ghostStyleFor(const XTypeConfig& cfg, const AppOverride* ov) { + return (ov && ov->ghost_style) ? *ov->ghost_style : cfg.ghost.style; +} + +GhostConfig config_loader::ghostSetupConfig(const XTypeConfig& cfg) { + GhostConfig g = cfg.ghost; + for (const auto& [_, ov] : cfg.apps) + if (ov.ghost_style == GhostStyle::Popup) g.style = GhostStyle::Popup; + return g; +} diff --git a/fcitx5-engine/src/config_loader.h b/fcitx5-engine/src/config_loader.h index f1a1f6a..b3a5b9a 100644 --- a/fcitx5-engine/src/config_loader.h +++ b/fcitx5-engine/src/config_loader.h @@ -13,4 +13,14 @@ XTypeConfig load(const std::string& path = ""); // Expands ~ using $HOME; returns empty string on failure. std::string default_path(); +// The suggestion style for an app: its [apps.""] ghost_style if set, +// else [ghost] style. `ov` may be null (no override for the app). +GhostStyle ghostStyleFor(const XTypeConfig& cfg, const AppOverride* ov); + +// What the popup setup (classicui theme, Hyprland plugin) should apply: +// [ghost] with style = Popup when [ghost] or any app's override is popup. +// The setup stays on across app switches; each app's own style then picks +// popup or inline when a suggestion is shown. +GhostConfig ghostSetupConfig(const XTypeConfig& cfg); + } // namespace config_loader diff --git a/fcitx5-engine/src/ghost_popup.cpp b/fcitx5-engine/src/ghost_popup.cpp new file mode 100644 index 0000000..d50496c --- /dev/null +++ b/fcitx5-engine/src/ghost_popup.cpp @@ -0,0 +1,88 @@ +#include "ghost_popup.h" + +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "ghost_popup_state.h" + +#ifndef XTYPE_HYPRLAND_PLUGIN_PATH +#define XTYPE_HYPRLAND_PLUGIN_PATH "" +#endif + +GhostPopupSetup::GhostPopupSetup(fcitx::Instance *instance, std::string stateDir) + : _instance(instance), + _restorePath((std::filesystem::path(stateDir) / "classicui-restore.conf").string()) {} + +void GhostPopupSetup::apply(const GhostConfig &cfg) { + const bool popup = cfg.style == GhostStyle::Popup; + applyTheme(cfg, popup); + applyHyprland(popup && cfg.hyprland_inline); +} + +void GhostPopupSetup::applyTheme(const GhostConfig &cfg, bool popup) { + auto *ui = _instance->addonManager().addon("classicui", true); + if (!ui || !ui->getConfig()) { + if (popup) log("classicui not available — popup style needs Fcitx5's classic UI"); + return; + } + fcitx::RawConfig raw; + ui->getConfig()->save(raw); + + const auto saved = ghost_popup::loadRestore(_restorePath); + if (popup) { + // Save the user's values once, before the first change. A restore + // file that already exists means the values in classicui are ours. + if (!saved) { + ghost_popup::Values own; + for (const auto &[key, _] : ghost_popup::popupValues(cfg)) + if (const auto *v = raw.valueByPath(key)) own[key] = *v; + std::error_code ec; + std::filesystem::create_directories(std::filesystem::path(_restorePath).parent_path(), ec); + if (!ghost_popup::saveRestore(_restorePath, own)) { + log("could not save classicui settings; not changing the theme"); + return; + } + } + bool changed = false; + for (const auto &[key, value] : ghost_popup::popupValues(cfg)) { + const auto *cur = raw.valueByPath(key); + if (!cur || *cur != value) { raw.setValueByPath(key, value); changed = true; } + } + if (changed) { + ui->setConfig(raw); + log("classicui switched to theme '" + cfg.popup_theme + "' font '" + cfg.popup_font + "'"); + } + } else if (saved) { + for (const auto &[key, value] : *saved) raw.setValueByPath(key, value); + ui->setConfig(raw); + std::error_code ec; + std::filesystem::remove(_restorePath, ec); + log("classicui settings restored"); + } +} + +void GhostPopupSetup::applyHyprland(bool wanted) { + const std::string plugin = XTYPE_HYPRLAND_PLUGIN_PATH; + if (!std::getenv("HYPRLAND_INSTANCE_SIGNATURE")) { + if (wanted) log("hyprland_inline is on but Hyprland isn't running; ignoring"); + return; + } + if (plugin.empty() || !std::filesystem::exists(plugin)) { + if (wanted) log("hyprland_inline is on but the xtype-inline plugin isn't installed"); + return; + } + if (_hyprlandLoaded == wanted) return; + // Fire-and-forget: hyprctl talks to the compositor; nothing here waits on it. + // Loading an already-loaded plugin / unloading an absent one only errors. + fcitx::startProcess({"hyprctl", "plugin", wanted ? "load" : "unload", plugin}); + _hyprlandLoaded = wanted; + log(std::string("hyprctl plugin ") + (wanted ? "load " : "unload ") + plugin); +} diff --git a/fcitx5-engine/src/ghost_popup.h b/fcitx5-engine/src/ghost_popup.h new file mode 100644 index 0000000..f84ba01 --- /dev/null +++ b/fcitx5-engine/src/ghost_popup.h @@ -0,0 +1,35 @@ +#pragma once + +#include +#include +#include + +#include "config.h" + +namespace fcitx { class Instance; } + +// Puts Fcitx5 into the state the popup ghost style needs, and takes it back +// out again. Idempotent: call with the current config on load and every reload. +// +// - Classic UI: while popup is on, Theme/DarkTheme/Font point at +// ghost.popup_theme / ghost.popup_font. The user's own values are saved to +// /classicui-restore.conf first and restored when popup goes off. +// - Hyprland: loads the xtype-inline plugin while popup + hyprland_inline are +// on (and Hyprland is running), unloads it otherwise. +class GhostPopupSetup { +public: + GhostPopupSetup(fcitx::Instance *instance, std::string stateDir); + + void apply(const GhostConfig &cfg); + void setLogSink(std::function log) { _log = std::move(log); } + +private: + void applyTheme(const GhostConfig &cfg, bool popup); + void applyHyprland(bool wanted); + void log(const std::string &msg) const { if (_log) _log(msg); } + + fcitx::Instance *_instance; + std::string _restorePath; + std::optional _hyprlandLoaded; // unknown until first apply + std::function _log; +}; diff --git a/fcitx5-engine/src/ghost_popup_state.h b/fcitx5-engine/src/ghost_popup_state.h new file mode 100644 index 0000000..edd3988 --- /dev/null +++ b/fcitx5-engine/src/ghost_popup_state.h @@ -0,0 +1,48 @@ +#pragma once + +// Fcitx5-free helpers for the popup ghost style's classic-UI settings, so the +// save/restore logic can be unit-tested without a running Fcitx5. + +#include +#include +#include +#include + +#include "config.h" + +namespace ghost_popup { + +using Values = std::map; + +// The classicui options XType sets while the popup style is on. +inline Values popupValues(const GhostConfig &cfg) { + return { + {"Theme", cfg.popup_theme}, + {"DarkTheme", cfg.popup_theme}, + {"UseDarkTheme", "False"}, + {"UseAccentColor", "False"}, // accent colours would tint the transparent theme + {"Font", cfg.popup_font}, + }; +} + +// The user's own values, saved before XType first changes them. One +// "KeyValue" per line; absent keys were unset and are left alone on restore. +inline bool saveRestore(const std::string &path, const Values &values) { + std::ofstream out(path, std::ios::trunc); + for (const auto &[k, v] : values) out << k << '\t' << v << '\n'; + return static_cast(out); +} + +inline std::optional loadRestore(const std::string &path) { + std::ifstream in(path); + if (!in) return std::nullopt; + Values values; + std::string line; + while (std::getline(in, line)) { + auto tab = line.find('\t'); + if (tab != std::string::npos) values[line.substr(0, tab)] = line.substr(tab + 1); + } + return values; +} + +} // namespace ghost_popup diff --git a/fcitx5-engine/src/xtype.cpp b/fcitx5-engine/src/xtype.cpp index 3447540..cd67539 100644 --- a/fcitx5-engine/src/xtype.cpp +++ b/fcitx5-engine/src/xtype.cpp @@ -127,6 +127,14 @@ XTypeEngine::XTypeEngine(fcitx::AddonManager *manager) } _inference.update_config(_cfg.inference); + + // Popup ghost style: switch Fcitx5's classic UI theme / Hyprland plugin. + // Deferred so classicui is not loaded from inside our own constructor. + _ghostSetup = std::make_unique( + _instance, path_utils::expandTilde("~/.local/share/xtype").string()); + _ghostSetup->setLogSink([](const std::string &m) { dbg("[ghost] %s", m.c_str()); }); + _instance->eventDispatcher().schedule( + [this] { _ghostSetup->apply(config_loader::ghostSetupConfig(_cfg)); }); } XTypeEngine::~XTypeEngine() { @@ -163,6 +171,7 @@ void XTypeEngine::reloadConfig() { applyPrompt(); _inference.update_config(_cfg.inference); + _ghostSetup->apply(config_loader::ghostSetupConfig(_cfg)); dbg("[reload] done"); } @@ -473,7 +482,14 @@ void XTypeEngine::requestInference( void XTypeEngine::updatePreedit(fcitx::InputContext *ic) { auto &panel = ic->inputPanel(); - if (_ctx.hasSuggestion()) { + const bool popup = + config_loader::ghostStyleFor(_cfg, currentAppOverride()) == GhostStyle::Popup; + // Popup style: Fcitx5's UI draws the suggestion at the caret (styled by the + // Fcitx5 theme), and nothing is placed in the app's own text. + panel.setAuxUp(popup && _ctx.hasSuggestion() ? fcitx::Text(*_ctx.suggestion()) + : fcitx::Text{}); + ic->updateUserInterface(fcitx::UserInterfaceComponent::InputPanel); + if (_ctx.hasSuggestion() && !popup) { fcitx::Text t(*_ctx.suggestion(), fcitx::TextFormatFlags{fcitx::TextFormatFlag::Underline, fcitx::TextFormatFlag::DontCommit}); @@ -485,6 +501,8 @@ void XTypeEngine::updatePreedit(fcitx::InputContext *ic) { } void XTypeEngine::clearPreedit(fcitx::InputContext *ic) { + ic->inputPanel().setAuxUp(fcitx::Text{}); + ic->updateUserInterface(fcitx::UserInterfaceComponent::InputPanel); ic->inputPanel().setClientPreedit(fcitx::Text{}); ic->updatePreedit(); } diff --git a/fcitx5-engine/src/xtype.h b/fcitx5-engine/src/xtype.h index 8046036..98e8ce3 100644 --- a/fcitx5-engine/src/xtype.h +++ b/fcitx5-engine/src/xtype.h @@ -20,6 +20,7 @@ #include "context_buffer.h" #include "corpus_collector.h" #include "engine_metrics.h" +#include "ghost_popup.h" #include "inference_client.h" #include "phrase_blocklist.h" #include "recent_events.h" @@ -71,6 +72,7 @@ class XTypeEngine : public fcitx::InputMethodEngineV2 { RecentEventsRing _recent; PhraseBlocklist _phraseBlock; std::unique_ptr _corpus; + std::unique_ptr _ghostSetup; std::string _userTypedSinceLastTerminator; std::unique_ptr _debounceTimer; uint64_t _gen{0}; diff --git a/fcitx5-engine/tests/CMakeLists.txt b/fcitx5-engine/tests/CMakeLists.txt index 4d9d53f..6ae4839 100644 --- a/fcitx5-engine/tests/CMakeLists.txt +++ b/fcitx5-engine/tests/CMakeLists.txt @@ -47,6 +47,13 @@ catch_discover_tests(test_config_loader WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/.. ) +add_executable(test_ghost_popup_state test_ghost_popup_state.cpp) +target_include_directories(test_ghost_popup_state PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../src) +target_link_libraries(test_ghost_popup_state PRIVATE + Catch2::Catch2WithMain +) +catch_discover_tests(test_ghost_popup_state) + add_executable(test_phrase_blocklist test_phrase_blocklist.cpp) target_include_directories(test_phrase_blocklist PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../src) target_link_libraries(test_phrase_blocklist PRIVATE diff --git a/fcitx5-engine/tests/fixtures/config_full.toml b/fcitx5-engine/tests/fixtures/config_full.toml index eba0187..5954ab0 100644 --- a/fcitx5-engine/tests/fixtures/config_full.toml +++ b/fcitx5-engine/tests/fixtures/config_full.toml @@ -36,6 +36,7 @@ tone = "technical" avoid_phrases = ["synergy", "leverage"] [apps.kate] +ghost_style = "popup" enabled = false mode = "code-aware" num_predict = 20 diff --git a/fcitx5-engine/tests/fixtures/config_ghost.toml b/fcitx5-engine/tests/fixtures/config_ghost.toml new file mode 100644 index 0000000..0d96fe7 --- /dev/null +++ b/fcitx5-engine/tests/fixtures/config_ghost.toml @@ -0,0 +1,8 @@ +[ghost] +style = "popup" +popup_font = "Inter 12" +popup_theme = "my-theme" +hyprland_inline = true + +[apps.omawrite] +ghost_style = "inline" diff --git a/fcitx5-engine/tests/test_config_loader.cpp b/fcitx5-engine/tests/test_config_loader.cpp index e8d0a77..7097819 100644 --- a/fcitx5-engine/tests/test_config_loader.cpp +++ b/fcitx5-engine/tests/test_config_loader.cpp @@ -63,3 +63,41 @@ TEST_CASE("malformed TOML returns defaults without crash") { CHECK(cfg.inference.model == "qwen2.5:1.5b"); std::remove(path); } + +TEST_CASE("ghost: defaults are inline style with the xtype-ghost popup theme") { + auto cfg = config_loader::load("tests/fixtures/config_defaults.toml"); + CHECK(cfg.ghost.style == GhostStyle::Inline); + CHECK(cfg.ghost.popup_font == "Sans 11"); + CHECK(cfg.ghost.popup_theme == "xtype-ghost"); + CHECK(cfg.ghost.hyprland_inline == false); +} + +TEST_CASE("ghost: [ghost] section is read") { + auto cfg = config_loader::load("tests/fixtures/config_ghost.toml"); + CHECK(cfg.ghost.style == GhostStyle::Popup); + CHECK(cfg.ghost.popup_font == "Inter 12"); + CHECK(cfg.ghost.popup_theme == "my-theme"); + CHECK(cfg.ghost.hyprland_inline == true); +} + +TEST_CASE("ghost: per-app ghost_style overrides [ghost] style") { + auto full = config_loader::load("tests/fixtures/config_full.toml"); // [ghost] inline + CHECK(*full.apps.at("kate").ghost_style == GhostStyle::Popup); + CHECK_FALSE(full.apps.at("org.kde.konsole").ghost_style.has_value()); + CHECK(config_loader::ghostStyleFor(full, &full.apps.at("kate")) == GhostStyle::Popup); + CHECK(config_loader::ghostStyleFor(full, &full.apps.at("org.kde.konsole")) == GhostStyle::Inline); + CHECK(config_loader::ghostStyleFor(full, nullptr) == GhostStyle::Inline); + + auto ghost = config_loader::load("tests/fixtures/config_ghost.toml"); // [ghost] popup + CHECK(config_loader::ghostStyleFor(ghost, &ghost.apps.at("omawrite")) == GhostStyle::Inline); + CHECK(config_loader::ghostStyleFor(ghost, nullptr) == GhostStyle::Popup); +} + +TEST_CASE("ghost: popup setup is on when any app uses popup") { + auto defaults = config_loader::load("tests/fixtures/config_defaults.toml"); + CHECK(config_loader::ghostSetupConfig(defaults).style == GhostStyle::Inline); + + auto full = config_loader::load("tests/fixtures/config_full.toml"); // only kate is popup + CHECK(config_loader::ghostSetupConfig(full).style == GhostStyle::Popup); + CHECK(full.ghost.style == GhostStyle::Inline); // [ghost] itself untouched +} diff --git a/fcitx5-engine/tests/test_ghost_popup_state.cpp b/fcitx5-engine/tests/test_ghost_popup_state.cpp new file mode 100644 index 0000000..520327e --- /dev/null +++ b/fcitx5-engine/tests/test_ghost_popup_state.cpp @@ -0,0 +1,48 @@ +#include + +#include +#include + +#include "ghost_popup_state.h" + +namespace fs = std::filesystem; + +static fs::path tmpFile(const char *tag) { + return fs::temp_directory_path() / + ("xtype_ghost_" + std::string(tag) + "_" + std::to_string(::getpid()) + ".conf"); +} + +TEST_CASE("popupValues points theme, dark theme and font at the ghost config", "[ghost]") { + GhostConfig cfg; + cfg.popup_theme = "xtype-ghost"; + cfg.popup_font = "Inter 12"; + const auto v = ghost_popup::popupValues(cfg); + CHECK(v.at("Theme") == "xtype-ghost"); + CHECK(v.at("DarkTheme") == "xtype-ghost"); + CHECK(v.at("UseDarkTheme") == "False"); + CHECK(v.at("UseAccentColor") == "False"); + CHECK(v.at("Font") == "Inter 12"); +} + +TEST_CASE("restore file round-trips the user's values, including spaces", "[ghost]") { + auto p = tmpFile("rt"); + ghost_popup::Values own = {{"Theme", "default-dark"}, {"Font", "Noto Sans 10"}}; + REQUIRE(ghost_popup::saveRestore(p.string(), own)); + auto back = ghost_popup::loadRestore(p.string()); + REQUIRE(back.has_value()); + CHECK(*back == own); + fs::remove(p); +} + +TEST_CASE("missing restore file means nothing was changed", "[ghost]") { + CHECK_FALSE(ghost_popup::loadRestore(tmpFile("missing").string()).has_value()); +} + +TEST_CASE("empty restore file restores nothing (user had no classicui settings)", "[ghost]") { + auto p = tmpFile("empty"); + REQUIRE(ghost_popup::saveRestore(p.string(), {})); + auto back = ghost_popup::loadRestore(p.string()); + REQUIRE(back.has_value()); + CHECK(back->empty()); + fs::remove(p); +} diff --git a/settings/qt6-app/qml/pages/PageGeneral.qml b/settings/qt6-app/qml/pages/PageGeneral.qml index 0f4d4c8..4078d7d 100644 --- a/settings/qt6-app/qml/pages/PageGeneral.qml +++ b/settings/qt6-app/qml/pages/PageGeneral.qml @@ -3,8 +3,13 @@ import QtQuick.Controls.Basic import XType.Settings 1.0 Item { + id: page anchors.fill: parent + // Popup options matter when the default style or any app's override is popup. + readonly property bool popupInUse: Config.ghostStyle === "popup" + || Object.values(Config.apps).some(a => a && a.ghost_style === "popup") + Flickable { id: flick anchors.fill: parent @@ -93,6 +98,46 @@ Item { } } } + XRow { + label: "Suggestion style" + desc: "Inline: in the app's own text (usually underlined). Popup: gray text drawn by Fcitx5 at the caret." + width: parent.width + Row { + spacing: 6 + XPill { + label: "Inline" + on: Config.ghostStyle !== "popup" + onClicked: Config.ghostStyle = "inline" + } + XPill { + label: "Popup" + on: Config.ghostStyle === "popup" + onClicked: Config.ghostStyle = "popup" + } + } + } + XRow { + visible: page.popupInUse + label: "Popup font" + desc: "Font of the gray text, e.g. \"Sans 11\". Match your apps' text size." + width: parent.width + XInput { + width: 200 + placeholderText: "Sans 11" + text: Config.ghostPopupFont + onEditingFinished: Config.ghostPopupFont = text.trim().length > 0 ? text.trim() : "Sans 11" + } + } + XRow { + visible: page.popupInUse + label: "On the caret line (Hyprland)" + desc: "Moves the popup onto the line you type. Not every app: only native Wayland apps that report their caret; not X11, Electron/Chromium or Qt apps using Fcitx5's Qt module." + width: parent.width + XToggle { + on: Config.ghostHyprlandInline + onToggled: (v) => Config.ghostHyprlandInline = v + } + } XRow { label: "Partial accept (word-by-word)" desc: "Accept one word at a time" diff --git a/settings/qt6-app/qml/pages/PagePerApp.qml b/settings/qt6-app/qml/pages/PagePerApp.qml index dfdaacb..a886c82 100644 --- a/settings/qt6-app/qml/pages/PagePerApp.qml +++ b/settings/qt6-app/qml/pages/PagePerApp.qml @@ -143,6 +143,38 @@ Item { } } + // Suggestion style: overrides General → Suggestion style for this app + XRow { + label: "Suggestion style" + desc: "Default follows the General page." + width: parent.width + isLast: false + Row { + spacing: 6 + Repeater { + model: [ + { label: "Default", value: "" }, + { label: "Inline", value: "inline" }, + { label: "Popup", value: "popup" }, + ] + delegate: XPill { + required property var modelData + label: modelData.label + on: { + if (root.selectedId === "") return modelData.value === "" + const entry = Config.apps[AppsKnown.canonicalForId(root.selectedId)] + return ((entry && entry.ghost_style) || "") === modelData.value + } + onClicked: { + if (root.selectedId === "") return + Config.setApp(AppsKnown.canonicalForId(root.selectedId), "ghost_style", + modelData.value === "" ? null : modelData.value) + } + } + } + } + } + // Suggestion length (comingSoon) XRow { label: "Suggestion length" diff --git a/settings/qt6-app/src/config_store.cpp b/settings/qt6-app/src/config_store.cpp index 95dc487..935bccd 100644 --- a/settings/qt6-app/src/config_store.cpp +++ b/settings/qt6-app/src/config_store.cpp @@ -104,6 +104,14 @@ void ConfigStore::load() { } } + // --- [ghost] --- + if (auto *g = root["ghost"].as_table()) { + if (auto v = (*g)["style"].value()) _ghostStyle = QString::fromStdString(*v); + if (auto v = (*g)["popup_font"].value()) _ghostPopupFont = QString::fromStdString(*v); + if (auto v = (*g)["popup_theme"].value()) _ghostPopupTheme = QString::fromStdString(*v); + if (auto v = (*g)["hyprland_inline"].value()) _ghostHyprlandInline = *v; + } + // --- [learning] --- if (auto *lrn = root["learning"].as_table()) { if (auto v = (*lrn)["enabled"].value()) _learningEnabled = *v; @@ -136,6 +144,7 @@ void ConfigStore::load() { if (auto v = (*overTbl)["num_predict"].value()) entry["num_predict"] = static_cast(*v); if (auto v = (*overTbl)["mode"].value()) entry["mode"] = QString::fromStdString(*v); if (auto v = (*overTbl)["prompt_addendum"].value()) entry["prompt_addendum"] = QString::fromStdString(*v); + if (auto v = (*overTbl)["ghost_style"].value()) entry["ghost_style"] = QString::fromStdString(*v); _apps[QString::fromStdString(std::string(prog))] = entry; } } @@ -189,6 +198,14 @@ 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("style", _ghostStyle.toStdString()); + ghost.insert_or_assign("popup_font", _ghostPopupFont.toStdString()); + ghost.insert_or_assign("popup_theme", _ghostPopupTheme.toStdString()); + ghost.insert_or_assign("hyprland_inline", _ghostHyprlandInline); + root.insert_or_assign("ghost", std::move(ghost)); + // [learning] toml::table lrn; lrn.insert_or_assign("enabled", _learningEnabled); @@ -220,6 +237,7 @@ void ConfigStore::writeToml(const QString &path) { if (sub.contains("num_predict")) entry.insert_or_assign("num_predict", static_cast(sub["num_predict"].toInt())); if (sub.contains("mode")) entry.insert_or_assign("mode", sub["mode"].toString().toStdString()); if (sub.contains("prompt_addendum")) entry.insert_or_assign("prompt_addendum", sub["prompt_addendum"].toString().toStdString()); + if (sub.contains("ghost_style")) entry.insert_or_assign("ghost_style", sub["ghost_style"].toString().toStdString()); appsTbl.insert_or_assign(it.key().toStdString(), std::move(entry)); } root.insert_or_assign("apps", std::move(appsTbl)); @@ -263,6 +281,10 @@ void ConfigStore::resetAll() { _engineEnabled = true; _triggerMode = "pause"; _acceptKey = "tab"; + _ghostStyle = "inline"; + _ghostPopupFont = "Sans 11"; + _ghostPopupTheme = "xtype-ghost"; + _ghostHyprlandInline = false; _partialAccept = true; _escDismisses = true; _passThroughTerminals = true; @@ -306,6 +328,10 @@ void ConfigStore::resetAll() { emit engineEnabledChanged(); emit triggerModeChanged(); emit acceptKeyChanged(); + emit ghostStyleChanged(); + emit ghostPopupFontChanged(); + emit ghostPopupThemeChanged(); + emit ghostHyprlandInlineChanged(); emit partialAcceptChanged(); emit escDismissesChanged(); emit passThroughTerminalsChanged(); @@ -349,6 +375,10 @@ 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::setGhostStyle(const QString &v) { CS_SET(ghostStyle, ghostStyleChanged); } +void ConfigStore::setGhostPopupFont(const QString &v) { CS_SET(ghostPopupFont, ghostPopupFontChanged); } +void ConfigStore::setGhostPopupTheme(const QString &v) { CS_SET(ghostPopupTheme, ghostPopupThemeChanged); } +void ConfigStore::setGhostHyprlandInline(bool v) { CS_SET(ghostHyprlandInline, ghostHyprlandInlineChanged); } 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..a9f0769 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] style → "inline"|"popup". // std::optional round-trip: absent key ↔ invalid QVariant / empty optional. class ConfigStore : public QObject { @@ -24,6 +25,12 @@ class ConfigStore : public QObject { Q_PROPERTY(QStringList blocklistApps READ blocklistApps WRITE setBlocklistApps NOTIFY blocklistAppsChanged) Q_PROPERTY(QStringList blockedPhrases READ blockedPhrases WRITE setBlockedPhrases NOTIFY blockedPhrasesChanged) + // Ghost (how suggestions are shown) + Q_PROPERTY(QString ghostStyle READ ghostStyle WRITE setGhostStyle NOTIFY ghostStyleChanged) + Q_PROPERTY(QString ghostPopupFont READ ghostPopupFont WRITE setGhostPopupFont NOTIFY ghostPopupFontChanged) + Q_PROPERTY(QString ghostPopupTheme READ ghostPopupTheme WRITE setGhostPopupTheme NOTIFY ghostPopupThemeChanged) + Q_PROPERTY(bool ghostHyprlandInline READ ghostHyprlandInline WRITE setGhostHyprlandInline NOTIFY ghostHyprlandInlineChanged) + // Inference Q_PROPERTY(QString model READ model WRITE setModel NOTIFY modelChanged) Q_PROPERTY(QString ollamaHost READ ollamaHost WRITE setOllamaHost NOTIFY ollamaHostChanged) @@ -52,7 +59,8 @@ class ConfigStore : public QObject { Q_PROPERTY(QStringList userAvoidPhrases READ userAvoidPhrases WRITE setUserAvoidPhrases NOTIFY userAvoidPhrasesChanged) // Apps map: { "program": { "enabled": bool, "model": str, "debounce_ms": int, - // "num_predict": int, "mode": str, "prompt_addendum": str } } + // "num_predict": int, "mode": str, "prompt_addendum": str, + // "ghost_style": "inline"|"popup" } } // Missing keys in sub-map → absent optional in AppOverride. Q_PROPERTY(QVariantMap apps READ apps WRITE setApps NOTIFY appsChanged) @@ -73,6 +81,11 @@ class ConfigStore : public QObject { QStringList blocklistApps() const { return _blocklistApps; } QStringList blockedPhrases() const { return _blockedPhrases; } + QString ghostStyle() const { return _ghostStyle; } + QString ghostPopupFont() const { return _ghostPopupFont; } + QString ghostPopupTheme() const { return _ghostPopupTheme; } + bool ghostHyprlandInline() const { return _ghostHyprlandInline; } + QString model() const { return _model; } QString ollamaHost() const { return _ollamaHost; } int debounceMs() const { return _debounceMs; } @@ -109,6 +122,11 @@ class ConfigStore : public QObject { void setBlocklistApps(const QStringList &v); void setBlockedPhrases(const QStringList &v); + void setGhostStyle(const QString &v); + void setGhostPopupFont(const QString &v); + void setGhostPopupTheme(const QString &v); + void setGhostHyprlandInline(bool v); + void setModel(const QString &v); void setOllamaHost(const QString &v); void setDebounceMs(int v); @@ -151,12 +169,17 @@ public slots: void engineEnabledChanged(); void triggerModeChanged(); void acceptKeyChanged(); + void ghostStyleChanged(); void partialAcceptChanged(); void escDismissesChanged(); void passThroughTerminalsChanged(); void blocklistAppsChanged(); void blockedPhrasesChanged(); + void ghostPopupFontChanged(); + void ghostPopupThemeChanged(); + void ghostHyprlandInlineChanged(); + void modelChanged(); void ollamaHostChanged(); void debounceMsChanged(); @@ -200,6 +223,12 @@ public slots: QStringList _blocklistApps; QStringList _blockedPhrases; + // Ghost + QString _ghostStyle = "inline"; + QString _ghostPopupFont = "Sans 11"; + QString _ghostPopupTheme = "xtype-ghost"; + bool _ghostHyprlandInline = false; + // Inference QString _model = "qwen2.5:1.5b"; QString _ollamaHost = "http://localhost:11434"; diff --git a/settings/qt6-app/tests/test_config_store.cpp b/settings/qt6-app/tests/test_config_store.cpp index 1925c76..3fa1f51 100644 --- a/settings/qt6-app/tests/test_config_store.cpp +++ b/settings/qt6-app/tests/test_config_store.cpp @@ -167,6 +167,31 @@ TEST_CASE("apps: only present keys survive, absent optionals stay absent", "[con CHECK(!kate.contains("num_predict")); CHECK(!kate.contains("mode")); CHECK(!kate.contains("prompt_addendum")); + CHECK(!kate.contains("ghost_style")); +} + +TEST_CASE("apps: per-app ghost_style round-trips and clears with null", "[config_store]") { + QTemporaryDir tmp; REQUIRE(tmp.isValid()); + const QString dst = tmp.filePath("config.toml"); + + { + ConfigStore cs(dst); + cs.load(); + cs.setApp("omawrite", "ghost_style", "popup"); + cs.setApp("kate", "ghost_style", "inline"); + cs.saveNow(); + } + { + ConfigStore cs(dst); + cs.load(); + CHECK(cs.apps()["omawrite"].toMap()["ghost_style"].toString() == "popup"); + CHECK(cs.apps()["kate"].toMap()["ghost_style"].toString() == "inline"); + cs.setApp("omawrite", "ghost_style", QVariant::fromValue(nullptr)); // QML's null + cs.saveNow(); + } + ConfigStore cs(dst); + cs.load(); + CHECK(!cs.apps()["omawrite"].toMap().contains("ghost_style")); } // ------------------------------------------------------------------ 5 @@ -257,3 +282,28 @@ 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] round-trip and defaults", "[config_store]") { + QTemporaryDir tmp; REQUIRE(tmp.isValid()); + const QString dst = tmp.filePath("config.toml"); + + { + ConfigStore cs(dst); + cs.load(); + CHECK(cs.ghostStyle() == "inline"); + CHECK(cs.ghostPopupFont() == "Sans 11"); + CHECK(cs.ghostPopupTheme() == "xtype-ghost"); + CHECK(cs.ghostHyprlandInline() == false); + cs.setGhostStyle("popup"); + cs.setGhostPopupFont("Inter 13"); + cs.setGhostHyprlandInline(true); + cs.saveNow(); + } + + ConfigStore cs2(dst); + cs2.load(); + CHECK(cs2.ghostStyle() == "popup"); + CHECK(cs2.ghostPopupFont() == "Inter 13"); + CHECK(cs2.ghostPopupTheme() == "xtype-ghost"); + CHECK(cs2.ghostHyprlandInline() == true); +}