Skip to content

Commit

Permalink
Merge pull request #94 from dobey/cycle-languages
Browse files Browse the repository at this point in the history
Improve layout switching behavior
  • Loading branch information
dobey authored Feb 7, 2022
2 parents 698f23c + ff81ea7 commit fd8f95d
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 93 deletions.
5 changes: 0 additions & 5 deletions data/schemas/org.maliit.keyboard.maliit.gschema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
<description>Currently active language, selected by user in the keyboard language menu</description>
<default>'en'</default>
</key>
<key name="previous-language" type="s">
<summary>Previous language</summary>
<description>Language which was in use immediately prior to the current active language</description>
<default>''</default>
</key>
<key name="enabled-languages" type="as">
<summary>Enabled languages</summary>
<description>User-defined list of activatable languages.</description>
Expand Down
6 changes: 0 additions & 6 deletions qml/Keyboard.qml
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,6 @@ Item {
maliit_input_method.close();
canvas.hidingComplete = true;
reportKeyboardVisibleRect();
// Switch back to the previous layout if we're in
// in a layout like emoji that requests switchBack
if (keypad.switchBack && maliit_input_method.previousLanguage) {
keypad.switchBack = false;
maliit_input_method.activeLanguage = maliit_input_method.previousLanguage;
}

// Exit cursor swipe mode when the keyboard hides
fullScreenItem.exitSwipeMode();
Expand Down
1 change: 0 additions & 1 deletion qml/KeyboardContainer.qml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ Item {

property string activeKeypadState: "NORMAL"
property alias popoverEnabled: extendedKeysSelector.enabled
property bool switchBack: false // Switch back to the previous layout when changing fields
property bool hideKeyLabels: false // Hide key labels when in cursor movement mode

property Item lastKeyPressed // Used for determining double click validity in PressArea
Expand Down
5 changes: 2 additions & 3 deletions qml/keys/LanguageKey.qml
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,12 @@ ActionKey {
}

onReleased: {
panel.switchBack = false;
if (held) {
return;
}

if (maliit_input_method.previousLanguage && maliit_input_method.previousLanguage != maliit_input_method.activeLanguage) {
maliit_input_method.activeLanguage = maliit_input_method.previousLanguage
if (altLangs) {
Keyboard.selectNextLanguage();
} else {
keypad.state = "EMOJI"
}
Expand Down
66 changes: 35 additions & 31 deletions src/plugin/inputmethod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ InputMethod::InputMethod(MAbstractInputMethodHost *host)
d->registerAutoCapsSetting();
d->registerWordEngineSetting();
d->registerActiveLanguage();
d->registerPreviousLanguage();
d->registerEnabledLanguages();
d->registerDoubleSpaceFullStop();
d->registerStayHidden();
Expand Down Expand Up @@ -341,8 +340,14 @@ void InputMethod::onEnabledLanguageSettingsChanged()
{
Q_D(InputMethod);
d->enabledLanguages = d->m_settings.enabledLanguages();
if (!d->enabledLanguages.contains(d->previousLanguage)) {
setPreviousLanguage(QString());
// Reset the value if it gets unset
if (d->enabledLanguages.length() == 0) {
d->m_settings.resetEnabledLanguages();
}
// Switch to first language in enabled languages if the currently active
// language is no longer enabled
if (!d->enabledLanguages.contains(d->activeLanguage)) {
setActiveLanguage(d->enabledLanguages.front());
}
Q_EMIT enabledLanguagesChanged(d->enabledLanguages);
}
Expand Down Expand Up @@ -543,14 +548,6 @@ const QString &InputMethod::activeLanguage() const
return d->activeLanguage;
}

//! \brief InputMethod::previousLanguage returns the language that was used
//! immediately prior to the current activeLanguage
const QString &InputMethod::previousLanguage() const
{
Q_D(const InputMethod);
return d->previousLanguage;
}

//! \brief InputMethod::useAudioFeedback is true, when keys should play a audio
//! feedback when pressed
//! \return
Expand Down Expand Up @@ -596,30 +593,54 @@ const QString InputMethod::audioFeedbackSound() const
return d->m_settings.keyPressAudioFeedbackSound();
}

//! \brief InputMethod::selectNextLanguage
//! Sets the active language to the next language in the enaabled languages list
void InputMethod::selectNextLanguage()
{
auto const& langs = enabledLanguages();
if (activeLanguage() == langs.back()) {
setActiveLanguage(langs.front());
} else {
setActiveLanguage(langs[langs.indexOf(activeLanguage()) + 1]);
}
}

//! \brief InputMethod::setActiveLanguage
//! Sets the currently active/used language
//! \param newLanguage id of the new language. For example "en" or "es"
//! FIXME check if the language is supported - if not use "en" as fallback
void InputMethod::setActiveLanguage(const QString &newLanguage)
{
Q_D(InputMethod);

qDebug() << "in inputMethod.cpp setActiveLanguage() activeLanguage is:" << newLanguage;

QString newPluginPath;
foreach(QString pluginPath, d->languagesPaths) {
QDir testDir(pluginPath + QDir::separator() + newLanguage);
if (testDir.exists()) {
d->currentPluginPath = testDir.absolutePath();
newPluginPath = testDir.absolutePath();
break;
}
}
// The language plpugin was not found, so reset the active language
if (newPluginPath.isEmpty()) {
d->m_settings.resetActiveLanguage();
// If the plugin was not found, and was in enabledLanguages list,
// also remove it from there
auto enabled = enabledLanguages();
if (enabled.contains(newLanguage)) {
enabled.removeAll(newLanguage);
d->m_settings.setEnabledLanguages(enabled);
}
return;
}
d->currentPluginPath = newPluginPath;

if (d->activeLanguage == newLanguage)
return;

d->editor.commitPreedit();

setPreviousLanguage(d->activeLanguage);
d->activeLanguage = newLanguage;
d->host->setLanguage(newLanguage);
d->m_settings.setActiveLanguage(newLanguage);
Expand All @@ -628,23 +649,6 @@ void InputMethod::setActiveLanguage(const QString &newLanguage)
Q_EMIT activeLanguageChanged(d->activeLanguage);
}

//! \brief InputMethod::setPreviousLanguage
//! Set the language used immediately prior to the current active language.
//! \param prevLanguage id the previous language used. e.g. "en" or "emoji"
void InputMethod::setPreviousLanguage(const QString &prevLanguage)
{
Q_D(InputMethod);

if (d->previousLanguage == prevLanguage)
return;

d->previousLanguage = prevLanguage;
d->m_settings.setPreviousLanguage(prevLanguage);

Q_EMIT previousLanguageChanged(d->previousLanguage);
}


void InputMethod::onWordEnginePluginChanged()
{
reset();
Expand Down
6 changes: 1 addition & 5 deletions src/plugin/inputmethod.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ class InputMethod
Q_PROPERTY(TextContentType contentType READ contentType WRITE setContentType NOTIFY contentTypeChanged)
Q_PROPERTY(QStringList enabledLanguages READ enabledLanguages NOTIFY enabledLanguagesChanged)
Q_PROPERTY(QString activeLanguage READ activeLanguage WRITE setActiveLanguage NOTIFY activeLanguageChanged)
Q_PROPERTY(QString previousLanguage READ previousLanguage WRITE setPreviousLanguage NOTIFY previousLanguageChanged)
Q_PROPERTY(QObject* actionKeyOverride READ actionKeyOverride NOTIFY actionKeyOverrideChanged)
Q_PROPERTY(bool useHapticFeedback READ useHapticFeedback NOTIFY useHapticFeedbackChanged)
Q_PROPERTY(bool enableMagnifier READ enableMagnifier NOTIFY enableMagnifierChanged)
Expand Down Expand Up @@ -114,11 +113,9 @@ class InputMethod
const QStringList &enabledLanguages() const;

const QString &activeLanguage() const;
Q_INVOKABLE void selectNextLanguage();
Q_SLOT void setActiveLanguage(const QString& newLanguage);

const QString &previousLanguage() const;
Q_SLOT void setPreviousLanguage(const QString& prevLanguage);

Q_SLOT void onVisibleRectChanged();

bool useAudioFeedback() const;
Expand Down Expand Up @@ -162,7 +159,6 @@ class InputMethod
void deactivateAutocaps();
void enabledLanguagesChanged(QStringList languages);
void activeLanguageChanged(QString language);
void previousLanguageChanged(QString language);
void useAudioFeedbackChanged();
void audioFeedbackSoundChanged(QString sound);
void useHapticFeedbackChanged();
Expand Down
11 changes: 0 additions & 11 deletions src/plugin/inputmethod_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ class InputMethodPrivate
bool wordEngineEnabled;
InputMethod::TextContentType contentType;
QString activeLanguage;
QString previousLanguage;
QStringList enabledLanguages;
Qt::ScreenOrientation appsCurrentOrientation;
QString keyboardState;
Expand Down Expand Up @@ -120,7 +119,6 @@ class InputMethodPrivate
, wordEngineEnabled(false)
, contentType(InputMethod::FreeTextContentType)
, activeLanguage(QStringLiteral("en"))
, previousLanguage()
, enabledLanguages(activeLanguage)
, appsCurrentOrientation(qGuiApp->primaryScreen()->orientation())
, keyboardState(QStringLiteral("CHARACTERS"))
Expand Down Expand Up @@ -305,15 +303,6 @@ class InputMethodPrivate
q->setActiveLanguage(activeLanguage);
}

void registerPreviousLanguage()
{
QObject::connect(&m_settings, &MaliitKeyboard::KeyboardSettings::previousLanguageChanged,
q, &InputMethod::setPreviousLanguage);

previousLanguage = m_settings.previousLanguage();
q->setPreviousLanguage(previousLanguage);
}

void registerEnabledLanguages()
{
QObject::connect(&m_settings, &MaliitKeyboard::KeyboardSettings::enabledLanguagesChanged,
Expand Down
45 changes: 17 additions & 28 deletions src/plugin/keyboardsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
using namespace MaliitKeyboard;

const QLatin1String ACTIVE_LANGUAGE_KEY = QLatin1String("activeLanguage");
const QLatin1String PREVIOUS_LANGUAGE_KEY = QLatin1String("previousLanguage");
const QLatin1String ENABLED_LANGUAGES_KEY = QLatin1String("enabledLanguages");
const QLatin1String AUTO_CAPITALIZATION_KEY = QLatin1String("autoCapitalization");
const QLatin1String AUTO_COMPLETION_KEY = QLatin1String("autoCompletion");
Expand Down Expand Up @@ -73,21 +72,11 @@ KeyboardSettings::KeyboardSettings(QObject *parent) :
auto enabled = enabledLanguages();
if (enabled.contains(emoji)) {
enabled.removeAll(emoji);
if (enabled.isEmpty()) {
enabled << QLatin1String("en");
}
m_settings->set(ENABLED_LANGUAGES_KEY, enabled);
setEnabledLanguages(enabled);
}
if (activeLanguage() == emoji) {
setActiveLanguage(enabled[0]);
}
if (previousLanguage() == emoji) {
if (enabled.length() > 1) {
setPreviousLanguage(enabled.back());
} else {
setPreviousLanguage(QLatin1String(""));
}
}
}

/*!
Expand All @@ -105,20 +94,9 @@ void KeyboardSettings::setActiveLanguage(const QString& id)
m_settings->set(ACTIVE_LANGUAGE_KEY, QVariant(id));
}

/*!
* \brief KeyboardSettings::previousLanguage returns the language that was
* active immediately before the current one.
* \return previously language
*/

QString KeyboardSettings::previousLanguage() const
{
return m_settings->get(PREVIOUS_LANGUAGE_KEY).toString();
}

void KeyboardSettings::setPreviousLanguage(const QString& id)
void KeyboardSettings::resetActiveLanguage()
{
m_settings->set(PREVIOUS_LANGUAGE_KEY, QVariant(id));
m_settings->reset(ACTIVE_LANGUAGE_KEY);
}

/*!
Expand All @@ -131,6 +109,20 @@ QStringList KeyboardSettings::enabledLanguages() const
return m_settings->get(ENABLED_LANGUAGES_KEY).toStringList();
}

void KeyboardSettings::setEnabledLanguages(const QStringList& ids)
{
if (ids.isEmpty()) {
resetEnabledLanguages();
return;
}
m_settings->set(ENABLED_LANGUAGES_KEY, ids);
}

void KeyboardSettings::resetEnabledLanguages()
{
m_settings->reset(ENABLED_LANGUAGES_KEY);
}

/*!
* \brief KeyboardSettings::autoCapitalization returns true id the first letter
* of each sentence should be capitalized
Expand Down Expand Up @@ -271,9 +263,6 @@ void KeyboardSettings::settingUpdated(const QString &key)
if (key == ACTIVE_LANGUAGE_KEY) {
Q_EMIT activeLanguageChanged(activeLanguage());
return;
} else if (key == PREVIOUS_LANGUAGE_KEY) {
Q_EMIT previousLanguageChanged(previousLanguage());
return;
} else if (key == ENABLED_LANGUAGES_KEY) {
Q_EMIT enabledLanguagesChanged(enabledLanguages());
return;
Expand Down
6 changes: 3 additions & 3 deletions src/plugin/keyboardsettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ class KeyboardSettings : public QObject

QString activeLanguage() const;
void setActiveLanguage(const QString& id);
QString previousLanguage() const;
void setPreviousLanguage(const QString& id);
void resetActiveLanguage();
QStringList enabledLanguages() const;
void setEnabledLanguages(const QStringList& ids);
void resetEnabledLanguages();
bool autoCapitalization() const;
bool autoCompletion() const;
bool predictiveText() const;
Expand All @@ -66,7 +67,6 @@ class KeyboardSettings : public QObject

Q_SIGNALS:
void activeLanguageChanged(QString);
void previousLanguageChanged(QString);
void enabledLanguagesChanged(QStringList);
void autoCapitalizationChanged(bool);
void autoCompletionChanged(bool);
Expand Down

0 comments on commit fd8f95d

Please sign in to comment.