ToolButton.qml 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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.Controls.impl
  6. import QtQuick.Controls.FluentWinUI3.impl
  7. import QtQuick.Templates as T
  8. T.ToolButton {
  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. topInset: -__config.topInset || 0
  20. bottomInset: -__config.bottomInset || 0
  21. leftInset: -__config.leftInset || 0
  22. rightInset: -__config.rightInset || 0
  23. icon.width: __config.icon.width
  24. icon.height: __config.icon.height
  25. readonly property string __currentState: [
  26. control.checked && "checked",
  27. !control.enabled && "disabled",
  28. control.enabled && !control.down && control.hovered && "hovered",
  29. down && "pressed"
  30. ].filter(Boolean).join("_") || "normal"
  31. readonly property var __config: Config.controls.toolbutton[__currentState] || {}
  32. readonly property Item __focusFrameTarget: control
  33. contentItem: IconLabel {
  34. spacing: control.spacing
  35. mirrored: control.mirrored
  36. display: control.display
  37. icon: control.icon
  38. defaultIconColor: {
  39. if (Application.styleHints.accessibility.contrastPreference === Qt.HighContrast) {
  40. if (!control.enabled)
  41. return control.palette.buttonText
  42. else if (control.checked && (control.hovered || control.down))
  43. return control.palette.highlight
  44. else if (!control.checked && !(control.down || control.hovered))
  45. return control.palette.buttonText
  46. else
  47. return control.palette.button
  48. }
  49. if (control.down) {
  50. return (control.checked || control.highlighted)
  51. ? Application.styleHints.colorScheme == Qt.Light
  52. ? Qt.rgba(1, 1, 1, 0.7) : Qt.rgba(0, 0, 0, 0.5)
  53. : (Application.styleHints.colorScheme === Qt.Light
  54. ? Qt.rgba(control.palette.buttonText.r, control.palette.buttonText.g, control.palette.buttonText.b, 0.62)
  55. : Qt.rgba(control.palette.buttonText.r, control.palette.buttonText.g, control.palette.buttonText.b, 0.7725))
  56. } else if (control.checked || control.highlighted) {
  57. return (Application.styleHints.colorScheme === Qt.Dark && !control.enabled)
  58. ? Qt.rgba(1, 1, 1, 0.5302)
  59. : (Application.styleHints.colorScheme === Qt.Dark ? "black" : "white")
  60. } else {
  61. return control.palette.buttonText
  62. }
  63. }
  64. text: control.text
  65. font: control.font
  66. color: defaultIconColor
  67. }
  68. background: ButtonBackground {
  69. control: control
  70. implicitHeight: control.__config.background.height
  71. implicitWidth: implicitHeight
  72. radius: control.__config.background.topOffset
  73. subtle: !(control.checked || control.highlighted) || control.flat
  74. highContrastBackgroundColorFunc: function() {
  75. if (!control.enabled)
  76. return "transparent"
  77. else if (control.checked && control.hovered)
  78. return control.palette.highlightedText
  79. else if (control.checked || control.hovered)
  80. return control.palette.highlight
  81. else
  82. return control.palette.button
  83. }
  84. }
  85. }