MaterialBrandMenu.qml 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright (c) 2022 Ultimaker B.V.
  2. // Cura is released under the terms of the LGPLv3 or higher.
  3. import QtQuick 2.7
  4. import QtQuick.Controls 2.4
  5. import QtQuick.Layouts 2.7
  6. import UM 1.5 as UM
  7. import Cura 1.7 as Cura
  8. /* This element is a workaround for MacOS, where it can crash in Qt6 when nested menus are closed.
  9. Instead we'll use a pop-up which doesn't seem to have that problem. */
  10. Cura.MenuItem
  11. {
  12. id: materialBrandMenu
  13. overrideShowArrow: true
  14. property var materialTypesModel
  15. text: materialTypesModel.name
  16. contentItem: MouseArea
  17. {
  18. hoverEnabled: true
  19. RowLayout
  20. {
  21. spacing: 0
  22. opacity: materialBrandMenu.enabled ? 1 : 0.5
  23. Item
  24. {
  25. // Spacer
  26. width: UM.Theme.getSize("default_margin").width
  27. }
  28. UM.Label
  29. {
  30. text: replaceText(materialBrandMenu.text)
  31. Layout.fillWidth: true
  32. Layout.fillHeight:true
  33. elide: Label.ElideRight
  34. wrapMode: Text.NoWrap
  35. }
  36. Item
  37. {
  38. Layout.fillWidth: true
  39. }
  40. Item
  41. {
  42. // Right side margin
  43. width: UM.Theme.getSize("default_margin").width
  44. }
  45. }
  46. onEntered: showTimer.restartTimer()
  47. onExited: hideTimer.restartTimer()
  48. }
  49. Timer
  50. {
  51. id: showTimer
  52. interval: 100
  53. function restartTimer()
  54. {
  55. restart();
  56. running = Qt.binding(function() { return materialBrandMenu.enabled && materialBrandMenu.contentItem.containsMouse; });
  57. hideTimer.running = false;
  58. }
  59. onTriggered: menuPopup.open()
  60. }
  61. Timer
  62. {
  63. id: hideTimer
  64. interval: 250
  65. function restartTimer() //Restart but re-evaluate the running property then.
  66. {
  67. restart();
  68. running = Qt.binding(function() { return materialBrandMenu.enabled && !materialBrandMenu.contentItem.containsMouse && !menuPopup.itemHovered > 0; });
  69. showTimer.running = false;
  70. }
  71. onTriggered: menuPopup.close()
  72. }
  73. Popup
  74. {
  75. id: menuPopup
  76. x: parent.width
  77. y: 0
  78. width: materialTypesList.width + padding * 2
  79. height: materialTypesList.height + padding * 2
  80. padding: background.border.width
  81. // Nasty hack to ensure that we can keep track if the popup contains the mouse.
  82. // Since we also want a hover for the sub items (and these events are sent async)
  83. // We have to keep a count of itemHovered (instead of just a bool)
  84. property int itemHovered: 0
  85. MouseArea
  86. {
  87. id: submenuArea
  88. anchors.fill: parent
  89. hoverEnabled: true
  90. onEntered: hideTimer.restartTimer()
  91. }
  92. background: Rectangle
  93. {
  94. color: UM.Theme.getColor("main_background")
  95. border.color: UM.Theme.getColor("lining")
  96. border.width: UM.Theme.getSize("default_lining").width
  97. }
  98. Column
  99. {
  100. id: materialTypesList
  101. Repeater
  102. {
  103. model: materialTypesModel.material_types
  104. //Use a MouseArea and Rectangle, not a button, because the button grabs mouse events which makes the parent pop-up think it's no longer being hovered.
  105. //With a custom MouseArea, we can prevent the events from being accepted.
  106. delegate: Item
  107. {
  108. width: materialTypeLabel.width
  109. height: materialTypeLabel.height
  110. Rectangle
  111. {
  112. width: materialTypesList.width
  113. height: parent.height
  114. color: materialTypeButton.containsMouse ? UM.Theme.getColor("background_2") : UM.Theme.getColor("background_1")
  115. MouseArea
  116. {
  117. id: materialTypeButton
  118. anchors.fill: parent
  119. hoverEnabled: true
  120. onEntered: menuPopup.itemHovered += 1
  121. onExited: menuPopup.itemHovered -= 1
  122. }
  123. }
  124. UM.Label
  125. {
  126. id: materialTypeLabel
  127. text: model.name
  128. }
  129. }
  130. }
  131. }
  132. }
  133. }