FluToggleSwitch.qml 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import QtQuick
  2. import QtQuick.Controls
  3. import QtQuick.Controls.Basic
  4. import QtQuick.Layouts
  5. import FluentUI
  6. Button {
  7. property bool selected: false
  8. property var clickFunc
  9. id: control
  10. height: 20
  11. implicitHeight: height
  12. focusPolicy:Qt.TabFocus
  13. Keys.onSpacePressed: control.visualFocus&&clicked()
  14. onClicked: {
  15. if(clickFunc){
  16. clickFunc()
  17. return
  18. }
  19. selected = !selected
  20. }
  21. contentItem: Item{}
  22. background : RowLayout{
  23. spacing: 0
  24. Rectangle {
  25. id:control_backgound
  26. width: 40
  27. height: control.height
  28. radius: height / 2
  29. FluFocusRectangle{
  30. visible: control.visualFocus
  31. radius: 20
  32. }
  33. color: {
  34. if(FluTheme.dark){
  35. if(selected){
  36. return FluTheme.primaryColor.dark
  37. }
  38. if(hovered){
  39. return "#3E3E3C"
  40. }
  41. return "#323232"
  42. }else{
  43. if(selected){
  44. return FluTheme.primaryColor.dark
  45. }
  46. if(hovered){
  47. return "#F4F4F4"
  48. }
  49. return "#FFFFFF"
  50. }
  51. }
  52. border.width: 1
  53. border.color: selected ? Qt.lighter(FluTheme.primaryColor.dark,1.2) : "#666666"
  54. Rectangle {
  55. width: pressed ? 28 : 20
  56. anchors{
  57. left: selected ? undefined : parent.left
  58. leftMargin: selected ? 20 : 0
  59. right: selected ? parent.right : undefined
  60. rightMargin: selected ? 0: 20
  61. }
  62. height: 20
  63. radius: 10
  64. scale: hovered ? 7/10 : 6/10
  65. anchors.verticalCenter: parent.verticalCenter
  66. color: selected ? "#FFFFFF" : "#666666"
  67. Behavior on anchors.leftMargin {
  68. NumberAnimation {
  69. duration: 167
  70. easing.type: Easing.BezierSpline
  71. easing.bezierCurve: [ 1, 0, 0, 0 ]
  72. }
  73. }
  74. Behavior on anchors.rightMargin {
  75. NumberAnimation {
  76. duration: 167
  77. easing.type: Easing.BezierSpline
  78. easing.bezierCurve: [ 1, 0, 0, 0 ]
  79. }
  80. }
  81. Behavior on width {
  82. NumberAnimation {
  83. duration: 167
  84. easing.type: Easing.BezierSpline
  85. easing.bezierCurve: [ 1, 0, 0, 0 ]
  86. }
  87. }
  88. Behavior on scale {
  89. NumberAnimation {
  90. duration: 167
  91. easing.type: Easing.BezierSpline
  92. easing.bezierCurve: [ 1, 0, 0, 0 ]
  93. }
  94. }
  95. }
  96. }
  97. FluText{
  98. text: control.text
  99. Layout.leftMargin: 5
  100. visible: text !== ""
  101. }
  102. }
  103. }