DelayButton.qml 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright (C) 2024 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.impl
  7. import QtQuick.Controls.FluentWinUI3.impl
  8. T.DelayButton {
  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. spacing: __config.spacing || 0
  15. topPadding: __config.topPadding || 0
  16. bottomPadding: __config.bottomPadding || 0
  17. leftPadding: __config.leftPadding || 0
  18. rightPadding: __config.rightPadding || 0
  19. icon.width: __config.icon.width
  20. icon.height: __config.icon.height
  21. icon.color: __buttonText
  22. readonly property color __buttonText: {
  23. if (Application.styleHints.accessibility.contrastPreference === Qt.HighContrast) {
  24. return (control.enabled && ((control.flat && (control.down || control.hovered))
  25. || ((control.highlighted || control.checked) && !control.down)))
  26. ? control.palette.button
  27. : control.enabled && (control.hovered || control.down)
  28. ? control.palette.highlight
  29. : control.palette.buttonText
  30. }
  31. if (control.down) {
  32. return (control.checked)
  33. ? Application.styleHints.colorScheme == Qt.Light
  34. ? Color.transparent("white", 0.7) : Color.transparent("black", 0.5)
  35. : (Application.styleHints.colorScheme === Qt.Light
  36. ? Color.transparent(control.palette.buttonText, 0.62)
  37. : Color.transparent(control.palette.buttonText, 0.7725))
  38. } else if (control.checked) {
  39. return (Application.styleHints.colorScheme === Qt.Dark && !control.enabled)
  40. ? Color.transparent("white", 0.5302)
  41. : (Application.styleHints.colorScheme === Qt.Dark ? "black" : "white")
  42. } else {
  43. return control.palette.buttonText
  44. }
  45. }
  46. readonly property string __currentState: [
  47. control.checked && "checked",
  48. !control.enabled && "disabled",
  49. control.enabled && !control.down && control.hovered && "hovered",
  50. control.down && "pressed"
  51. ].filter(Boolean).join("_") || "normal"
  52. readonly property var __config: Config.controls.button[__currentState] || {}
  53. readonly property Item __focusFrameTarget: control
  54. transition: Transition {
  55. NumberAnimation {
  56. duration: control.delay * (control.pressed ? 1.0 - control.progress : 0.3 * control.progress)
  57. }
  58. }
  59. contentItem: ItemGroup {
  60. ClippedText {
  61. clip: control.progress > 0
  62. clipX: -control.leftPadding + control.progress * control.width
  63. clipWidth: (1.0 - control.progress) * control.width
  64. visible: control.progress < 1
  65. text: control.text
  66. font: control.font
  67. color: control.icon.color
  68. horizontalAlignment: Text.AlignHCenter
  69. verticalAlignment: Text.AlignVCenter
  70. elide: Text.ElideRight
  71. }
  72. ClippedText {
  73. clip: control.progress > 0
  74. clipX: -control.leftPadding
  75. clipWidth: control.progress * control.width
  76. visible: control.progress > 0
  77. text: control.text
  78. font: control.font
  79. color: control.icon.color
  80. horizontalAlignment: Text.AlignHCenter
  81. verticalAlignment: Text.AlignVCenter
  82. elide: Text.ElideRight
  83. }
  84. }
  85. background: ButtonBackground {
  86. control: control
  87. implicitHeight: control.__config.background.height
  88. implicitWidth: control.__config.background.width
  89. radius: control.__config.background.topOffset
  90. subtle: false
  91. Rectangle {
  92. width: control.progress * parent.width
  93. height: parent.height
  94. radius: parent.radius
  95. color: control.down ? control.palette.accent : "transparent"
  96. visible: !control.checked && control.enabled
  97. }
  98. }
  99. }