Switch.qml 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright (C) 2017 The Qt Company Ltd.
  2. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
  3. // Qt-Security score:significant reason:default
  4. import QtQuick
  5. import QtQuick.Templates as T
  6. import QtQuick.Controls.Imagine
  7. import QtQuick.Controls.Imagine.impl
  8. T.Switch {
  9. id: control
  10. implicitWidth: Math.max(implicitBackgroundWidth + leftInset + rightInset,
  11. implicitContentWidth + leftPadding + rightPadding)
  12. implicitHeight: Math.max(implicitBackgroundHeight + topInset + bottomInset,
  13. implicitContentHeight + topPadding + bottomPadding,
  14. implicitIndicatorHeight + topPadding + bottomPadding)
  15. spacing: 6 // ###
  16. topPadding: background ? background.topPadding : 0
  17. leftPadding: background ? background.leftPadding : 0
  18. rightPadding: background ? background.rightPadding : 0
  19. bottomPadding: background ? background.bottomPadding : 0
  20. topInset: background ? -background.topInset || 0 : 0
  21. leftInset: background ? -background.leftInset || 0 : 0
  22. rightInset: background ? -background.rightInset || 0 : 0
  23. bottomInset: background ? -background.bottomInset || 0 : 0
  24. indicator: NinePatchImage {
  25. x: control.text ? (control.mirrored ? control.width - width - control.rightPadding : control.leftPadding) : control.leftPadding + (control.availableWidth - width) / 2
  26. y: control.topPadding + (control.availableHeight - height) / 2
  27. width: Math.max(implicitWidth, handle.leftPadding && handle.rightPadding ? handle.implicitWidth : 2 * handle.implicitWidth)
  28. height: Math.max(implicitHeight, handle.implicitHeight)
  29. source: control.Imagine.url + "switch-indicator"
  30. NinePatchImageSelector on source {
  31. states: [
  32. {"disabled": !control.enabled},
  33. {"pressed": control.down},
  34. {"checked": control.checked},
  35. {"focused": control.visualFocus},
  36. {"mirrored": control.mirrored},
  37. {"hovered": control.enabled && control.hovered}
  38. ]
  39. }
  40. property NinePatchImage handle: NinePatchImage {
  41. readonly property real minPos: parent.leftPadding - leftPadding
  42. readonly property real maxPos: parent.width - width + rightPadding - parent.rightPadding
  43. readonly property real dragPos: control.visualPosition * parent.width - (width / 2)
  44. parent: control.indicator
  45. x: Math.max(minPos, Math.min(maxPos, control.visualPosition * parent.width - (width / 2)))
  46. y: (parent.height - height) / 2
  47. source: control.Imagine.url + "switch-handle"
  48. NinePatchImageSelector on source {
  49. states: [
  50. {"disabled": !control.enabled},
  51. {"pressed": control.down},
  52. {"checked": control.checked},
  53. {"focused": control.visualFocus},
  54. {"mirrored": control.mirrored},
  55. {"hovered": control.enabled && control.hovered}
  56. ]
  57. }
  58. Behavior on x {
  59. enabled: !control.down
  60. SmoothedAnimation { velocity: 200 }
  61. }
  62. }
  63. }
  64. contentItem: Text {
  65. leftPadding: control.indicator && !control.mirrored ? control.indicator.width + control.spacing : 0
  66. rightPadding: control.indicator && control.mirrored ? control.indicator.width + control.spacing : 0
  67. text: control.text
  68. font: control.font
  69. color: control.palette.windowText
  70. elide: Text.ElideRight
  71. verticalAlignment: Text.AlignVCenter
  72. }
  73. background: NinePatchImage {
  74. source: control.Imagine.url + "switch-background"
  75. NinePatchImageSelector on source {
  76. states: [
  77. {"disabled": !control.enabled},
  78. {"pressed": control.down},
  79. {"checked": control.checked},
  80. {"focused": control.visualFocus},
  81. {"mirrored": control.mirrored},
  82. {"hovered": control.enabled && control.hovered}
  83. ]
  84. }
  85. }
  86. }