ShadowInputControl.qml 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright (C) 2017 The Qt Company Ltd.
  2. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
  3. import QtQuick
  4. // Deliberately imported after QtQuick to avoid missing restoreMode property in Binding. Fix in Qt 6.
  5. import QtQml
  6. import QtQuick.VirtualKeyboard
  7. import QtQuick.VirtualKeyboard.Settings
  8. Item {
  9. id: control
  10. property alias textEdit: shadowInput
  11. enabled: keyboard.active && VirtualKeyboardSettings.fullScreenMode
  12. MouseArea {
  13. anchors.fill: parent
  14. }
  15. onXChanged: InputContext.priv.shadow.updateSelectionProperties()
  16. onYChanged: InputContext.priv.shadow.updateSelectionProperties()
  17. Loader {
  18. sourceComponent: keyboard.style.fullScreenInputContainerBackground
  19. anchors.fill: parent
  20. Loader {
  21. id: fullScreenInputBackground
  22. sourceComponent: keyboard.style.fullScreenInputBackground
  23. anchors.fill: parent
  24. anchors.margins: keyboard.style.fullScreenInputMargins
  25. z: 1
  26. Flickable {
  27. id: flickable
  28. clip: true
  29. z: 2
  30. width: parent.width
  31. height: parent.height
  32. flickableDirection: Flickable.HorizontalFlick
  33. interactive: contentWidth > width
  34. contentWidth: shadowInput.width
  35. onContentXChanged: InputContext.priv.shadow.updateSelectionProperties()
  36. function ensureVisible(rectangle) {
  37. if (contentX >= rectangle.x)
  38. contentX = rectangle.x
  39. else if (contentX + width <= rectangle.x + rectangle.width)
  40. contentX = rectangle.x + rectangle.width - width;
  41. }
  42. TextInput {
  43. id: shadowInput
  44. objectName: "shadowInput"
  45. property bool blinkStatus: true
  46. width: Math.max(flickable.width, implicitWidth)
  47. height: implicitHeight
  48. anchors.verticalCenter: parent.verticalCenter
  49. leftPadding: keyboard.style.fullScreenInputPadding
  50. rightPadding: keyboard.style.fullScreenInputPadding
  51. activeFocusOnPress: false
  52. font: keyboard.style.fullScreenInputFont
  53. inputMethodHints: InputContext.inputMethodHints
  54. cursorDelegate: keyboard.style.fullScreenInputCursor
  55. passwordCharacter: keyboard.style.fullScreenInputPasswordCharacter
  56. color: keyboard.style.fullScreenInputColor
  57. selectionColor: keyboard.style.fullScreenInputSelectionColor
  58. selectedTextColor: keyboard.style.fullScreenInputSelectedTextColor
  59. echoMode: (InputContext.inputMethodHints & Qt.ImhHiddenText) ? TextInput.Password : TextInput.Normal
  60. selectByMouse: !!InputContext.inputItem && !!InputContext.inputItem.selectByMouse
  61. onCursorPositionChanged: {
  62. cursorSyncTimer.restart()
  63. blinkStatus = true
  64. if (cursorTimer.running)
  65. cursorTimer.restart()
  66. }
  67. onSelectionStartChanged: cursorSyncTimer.restart()
  68. onSelectionEndChanged: cursorSyncTimer.restart()
  69. onCursorRectangleChanged: flickable.ensureVisible(cursorRectangle)
  70. function getAnchorPosition() {
  71. if (selectionStart == selectionEnd)
  72. return cursorPosition
  73. else if (selectionStart == cursorPosition)
  74. return selectionEnd
  75. else
  76. return selectionStart
  77. }
  78. Timer {
  79. id: cursorSyncTimer
  80. interval: 0
  81. onTriggered: {
  82. var anchorPosition = shadowInput.getAnchorPosition()
  83. if (anchorPosition !== InputContext.anchorPosition || shadowInput.cursorPosition !== InputContext.cursorPosition)
  84. InputContext.priv.forceCursorPosition(anchorPosition, shadowInput.cursorPosition)
  85. }
  86. }
  87. Timer {
  88. id: cursorTimer
  89. interval: Qt.styleHints.cursorFlashTime / 2
  90. repeat: true
  91. running: control.visible
  92. onTriggered: shadowInput.blinkStatus = !shadowInput.blinkStatus
  93. }
  94. }
  95. }
  96. }
  97. }
  98. Component.onCompleted: {
  99. if (VirtualKeyboardSettings.fullScreenMode) {
  100. InputContext.priv.shadow.inputItem = shadowInput
  101. }
  102. }
  103. Connections {
  104. target: VirtualKeyboardSettings
  105. function onFullScreenModeChanged() {
  106. InputContext.priv.shadow.inputItem = VirtualKeyboardSettings.fullScreenMode ? shadowInput : null
  107. }
  108. }
  109. Connections {
  110. target: InputContext.priv.shadow
  111. function onInputItemChanged() {
  112. cursorSyncTimer.stop()
  113. if (!InputContext.priv.shadow.inputItem)
  114. shadowInput.clear()
  115. }
  116. }
  117. }