diff options
author | Dirk Hohndel <dirk@hohndel.org> | 2019-10-13 14:43:38 -0700 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2019-10-14 13:39:45 -0700 |
commit | 01f1bea995722f6e9934f84d6ac375b30d2a51e4 (patch) | |
tree | f148bd24a959b90b27affbfc270dd611e8f2ae21 /mobile-widgets/qml/SsrfTextField.qml | |
parent | 85d810119b0716686348390d096af6f1d9c04e16 (diff) | |
download | subsurface-01f1bea995722f6e9934f84d6ac375b30d2a51e4.tar.gz |
Mobile: ensure input fields stay visible after keyboard opens
When the user taps on a TextField to enter text, usually the virtual
keyboard will pop up. This code tries to ensure that the keyboard
doesn't cover the entry field that the user was trying to work on.
In order to centralize these changes, this introduces a new
SsrfTextField type which we use to also remove a few redundant default
settings that we previously had for every field. The one TextArea for
the Notes field didn't seem worth creating yet another type for, so
there the changes are done directly in DiveDetailsEdit.
The awkward timer mechanism is necessary as the keyboard pops up
asynchronously and then triggers a change of height for the app, so we
need to wait a little bit before doing the adjustment.
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'mobile-widgets/qml/SsrfTextField.qml')
-rw-r--r-- | mobile-widgets/qml/SsrfTextField.qml | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/mobile-widgets/qml/SsrfTextField.qml b/mobile-widgets/qml/SsrfTextField.qml new file mode 100644 index 000000000..d4ec5098a --- /dev/null +++ b/mobile-widgets/qml/SsrfTextField.qml @@ -0,0 +1,57 @@ +// SPDX-License-Identifier: GPL-2.0 +import QtQuick 2.2 +import QtQuick.Controls 2.2 as Controls +import org.kde.kirigami 2.4 as Kirigami + +Controls.TextField { + /** + * set the flickable property to the Flickable this TextField is part of and + * when the user starts editing the text this should stay visible + */ + property var flickable + property bool firstTime: true + + id: stf + + // while we are at it, let's put some common settings here into the shared element + color: subsurfaceTheme.textColor + onEditingFinished: { + focus = false + firstTime = true + } + + // that's when a user taps on the field to start entering text + onPressed: { + if (flickable !== undefined) { + waitForKeyboard.start() + } else { + console.log("flickable is undefined") + } + } + + // give the OS enough time to actually resize the flickable + Timer { + id: waitForKeyboard + interval: 300 // 300ms seems like FOREVER, but even that sometimes isn't long enough on Android + + onTriggered: { + if (!Qt.inputMethod.visible) { + if (firstTime) { + firstTime = false + restart() + } + return + } + // make sure there's enough space for the input field above the keyboard and action button (and that it's not too far up, either) + if (stf.y + stf.height > flickable.contentY + flickable.height - 3 * Kirigami.Units.gridUnit || y < flickable.contentY) + ensureVisible(Math.max(0, 3 * Kirigami.Units.gridUnit + stf.y + stf.height - flickable.height)) + } + } + + // scroll the flickable to the desired position if the keyboard has shown up + // this didn't work when setting it from within the Timer, but calling this function works. + // go figure. + function ensureVisible(yDest) { + flickable.contentY = yDest + } +} |