diff --git a/src/modules/keyboard/Config.cpp b/src/modules/keyboard/Config.cpp index b137927b4..b77282a18 100644 --- a/src/modules/keyboard/Config.cpp +++ b/src/modules/keyboard/Config.cpp @@ -16,6 +16,7 @@ #include "GlobalStorage.h" #include "JobQueue.h" #include "utils/Logger.h" +#include "utils/RAII.h" #include "utils/Retranslator.h" #include "utils/String.h" #include "utils/Variant.h" @@ -170,6 +171,12 @@ Config::Config( QObject* parent ) connect( m_keyboardVariantsModel, &KeyboardVariantsModel::currentIndexChanged, this, &Config::xkbChanged ); + // If the user picks something explicitly -- not a consequence of + // a guess -- then move to UserSelected state and stay there. + connect( m_keyboardModelsModel, &KeyboardModelsModel::currentIndexChanged, this, &Config::selectionChange ); + connect( m_keyboardLayoutsModel, &KeyboardLayoutModel::currentIndexChanged, this, &Config::selectionChange ); + connect( m_keyboardVariantsModel, &KeyboardVariantsModel::currentIndexChanged, this, &Config::selectionChange ); + m_selectedModel = m_keyboardModelsModel->key( m_keyboardModelsModel->currentIndex() ); m_selectedLayout = m_keyboardLayoutsModel->item( m_keyboardLayoutsModel->currentIndex() ).first; m_selectedVariant = m_keyboardVariantsModel->key( m_keyboardVariantsModel->currentIndex() ); @@ -264,6 +271,13 @@ findLayout( const KeyboardLayoutModel* klm, const QString& currentLayout ) void Config::detectCurrentKeyboardLayout() { + if ( m_state != State::Initial ) + { + return; + } + cPointerSetter returnToIntial( &m_state, State::Initial ); + m_state = State::Guessing; + //### Detect current keyboard layout and variant QString currentLayout; QString currentVariant; @@ -409,6 +423,13 @@ guessLayout( const QStringList& langParts, KeyboardLayoutModel* layouts, Keyboar void Config::guessLocaleKeyboardLayout() { + if ( m_state != State::Initial ) + { + return; + } + cPointerSetter returnToIntial( &m_state, State::Initial ); + m_state = State::Guessing; + /* Guessing a keyboard layout based on the locale means * mapping between language identifiers in _ * format to keyboard mappings, which are _ @@ -556,3 +577,12 @@ Config::retranslate() { retranslateKeyboardModels(); } + +void +Config::selectionChange() +{ + if ( m_state == State::Initial ) + { + m_state = State::UserSelected; + } +} diff --git a/src/modules/keyboard/Config.h b/src/modules/keyboard/Config.h index d4090bafc..436ead4b5 100644 --- a/src/modules/keyboard/Config.h +++ b/src/modules/keyboard/Config.h @@ -107,6 +107,24 @@ private: QString m_xOrgConfFileName; QString m_convertedKeymapPath; bool m_writeEtcDefaultKeyboard = true; + + // The state determines whether we guess settings or preserve them: + // - Initial -> Guessing + // - Initial -> UserSelected + // - Guessing -> Initial + enum class State + { + Initial, // after configuration, nothing special going on + Guessing, // on activation + UserSelected // explicit choice is made, preserve that + }; + State m_state = State::Initial; + + /** @brief Handles state change when selections in model, variant, layout + * + * This handles the Initial -> UserSelected transition in particular. + */ + void selectionChange(); };