Button.qml 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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.impl
  7. import QtQuick.Controls.Material
  8. import QtQuick.Controls.Material.impl
  9. T.Button {
  10. id: control
  11. implicitWidth: Math.max(implicitBackgroundWidth + leftInset + rightInset,
  12. implicitContentWidth + leftPadding + rightPadding)
  13. implicitHeight: Math.max(implicitBackgroundHeight + topInset + bottomInset,
  14. implicitContentHeight + topPadding + bottomPadding)
  15. topInset: 6
  16. bottomInset: 6
  17. verticalPadding: Material.buttonVerticalPadding
  18. leftPadding: Material.buttonLeftPadding(flat, hasIcon && (display !== AbstractButton.TextOnly))
  19. rightPadding: Material.buttonRightPadding(flat, hasIcon && (display !== AbstractButton.TextOnly),
  20. (text !== "") && (display !== AbstractButton.IconOnly))
  21. spacing: 8
  22. icon.width: 24
  23. icon.height: 24
  24. readonly property bool hasIcon: icon.name.length > 0 || icon.source.toString().length > 0
  25. Material.elevation: control.down ? 8 : 2
  26. Material.roundedScale: Material.FullScale
  27. contentItem: IconLabel {
  28. spacing: control.spacing
  29. mirrored: control.mirrored
  30. display: control.display
  31. icon: control.icon
  32. defaultIconColor: !control.enabled ? control.Material.hintTextColor
  33. : (control.flat && control.highlighted) || (control.checked && !control.highlighted)
  34. ? control.Material.accentColor : control.highlighted
  35. ? control.Material.primaryHighlightedTextColor : control.Material.foreground
  36. text: control.text
  37. font: control.font
  38. color: defaultIconColor
  39. }
  40. background: Rectangle {
  41. implicitWidth: 64
  42. implicitHeight: control.Material.buttonHeight
  43. radius: control.Material.roundedScale === Material.FullScale ? height / 2 : control.Material.roundedScale
  44. color: control.Material.buttonColor(control.Material.theme, control.Material.background,
  45. control.Material.accent, control.enabled, control.flat, control.highlighted, control.checked)
  46. // The layer is disabled when the button color is transparent so you can do
  47. // Material.background: "transparent" and get a proper flat button without needing
  48. // to set Material.elevation as well
  49. layer.enabled: control.enabled && color.a > 0 && !control.flat
  50. layer.effect: RoundedElevationEffect {
  51. elevation: control.Material.elevation
  52. roundedScale: control.background.radius
  53. }
  54. Ripple {
  55. clip: true
  56. clipRadius: parent.radius
  57. width: parent.width
  58. height: parent.height
  59. pressed: control.pressed
  60. anchor: control
  61. active: enabled && (control.down || control.visualFocus || control.hovered)
  62. color: control.flat && control.highlighted ? control.Material.highlightedRippleColor : control.Material.rippleColor
  63. }
  64. }
  65. }