show and hide keyboard for qml windows

This commit is contained in:
Seth Alves 2016-09-09 16:48:46 -07:00
parent dd39d46883
commit c396196e0f
3 changed files with 37 additions and 8 deletions

View file

@ -18,7 +18,7 @@ import "hifi/toolbars"
import "controls-uit" as HifiControls
Window {
property bool keyboardRaised: true
property bool keyboardRaised: false
property bool punctuationMode: false
id: root
@ -70,6 +70,7 @@ Window {
AddressBarDialog {
id: addressBarDialog
objectName: "AddressBarDialogDialog"
implicitWidth: backgroundImage.width
implicitHeight: backgroundImage.height + (keyboardRaised ? 200 : 0)
// The buttons have their button state changed on hover, so we have to manually fix them up here
@ -126,6 +127,7 @@ Window {
}
Image {
id: backgroundImage
objectName: "AddressBarDialogImage"
source: "../images/address-bar.svg"
width: 576 * root.scale
height: 80 * root.scale
@ -175,6 +177,7 @@ Window {
TextInput {
id: addressLine
focus: true
objectName: "AddressBarDialogTextInput"
anchors {
top: parent.top
bottom: parent.bottom

View file

@ -814,15 +814,29 @@ QVariant OffscreenQmlSurface::returnFromUiThread(std::function<QVariant()> funct
return function();
}
void OffscreenQmlSurface::focusDestroyed(QObject *obj) {
_currentFocusItem = nullptr;
}
void OffscreenQmlSurface::onFocusObjectChanged(QObject* object) {
if (!object) {
QQuickItem* item = dynamic_cast<QQuickItem*>(object);
if (!item) {
setFocusText(false);
_currentFocusItem = nullptr;
return;
}
QInputMethodQueryEvent query(Qt::ImEnabled);
qApp->sendEvent(object, &query);
setFocusText(query.value(Qt::ImEnabled).toBool());
if (_currentFocusItem) {
disconnect(_currentFocusItem, &QObject::destroyed, this, 0);
setKeyboardRaised(_currentFocusItem, false);
}
setKeyboardRaised(item, item->hasActiveFocus());
_currentFocusItem = item;
connect(_currentFocusItem, &QObject::destroyed, this, &OffscreenQmlSurface::focusDestroyed);
}
void OffscreenQmlSurface::setFocusText(bool newFocusText) {
@ -880,15 +894,25 @@ void OffscreenQmlSurface::synthesizeKeyPress(QString key) {
QCoreApplication::postEvent(getEventHandler(), releaseEvent);
}
void OffscreenQmlSurface::setKeyboardRaised(bool raised) {
void OffscreenQmlSurface::setKeyboardRaised(QObject* object, bool raised) {
// raise the keyboard only while in HMD mode and it's being requested.
// XXX
// bool value = AbstractViewStateInterface::instance()->isHMDMode() && raised;
// getRootItem()->setProperty("keyboardRaised", QVariant(value));
getRootItem()->setProperty("keyboardRaised", QVariant(raised));
if (!object) {
return;
}
QQuickItem* item = dynamic_cast<QQuickItem*>(object);
while (item) {
if (item->property("keyboardRaised").isValid()) {
item->setProperty("keyboardRaised", QVariant(raised));
return;
}
item = dynamic_cast<QQuickItem*>(item->parentItem());
}
}
void OffscreenQmlSurface::emitScriptEvent(const QVariant& message) {
@ -905,9 +929,9 @@ void OffscreenQmlSurface::emitWebEvent(const QVariant& message) {
} else {
// special case to handle raising and lowering the virtual keyboard
if (message.type() == QVariant::String && message.toString() == "_RAISE_KEYBOARD") {
setKeyboardRaised(true);
setKeyboardRaised(getRootItem(), true);
} else if (message.type() == QVariant::String && message.toString() == "_LOWER_KEYBOARD") {
setKeyboardRaised(false);
setKeyboardRaised(getRootItem(), false);
} else {
emit webEventReceived(message);
}

View file

@ -68,7 +68,7 @@ public:
QPointF mapToVirtualScreen(const QPointF& originalPoint, QObject* originalWidget);
bool eventFilter(QObject* originalDestination, QEvent* event) override;
Q_INVOKABLE void setKeyboardRaised(bool raised);
void setKeyboardRaised(QObject* object, bool raised);
Q_INVOKABLE void synthesizeKeyPress(QString key);
@ -81,7 +81,7 @@ public slots:
void requestUpdate();
void requestRender();
void onAboutToQuit();
void focusDestroyed(QObject *obj);
// event bridge
public slots:
@ -119,6 +119,8 @@ private:
uint8_t _maxFps{ 60 };
MouseTranslator _mouseTranslator{ [](const QPointF& p) { return p.toPoint(); } };
QWindow* _proxyWindow { nullptr };
QQuickItem* _currentFocusItem { nullptr };
};
#endif