MaterialBrandSubMenu.qml 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright (c) 2022 UltiMaker
  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. Popup
  9. {
  10. id: materialBrandSubMenu
  11. // There is a bug where hovering the bottom half of the last element causes the popup to close.
  12. // Undo this commit if you find a fix.
  13. bottomPadding: -UM.Theme.getSize("thin_margin").height
  14. topPadding: UM.Theme.getSize("thin_margin").height
  15. implicitWidth: scrollViewContent.width + scrollbar.width + leftPadding + rightPadding
  16. implicitHeight: scrollViewContent.height + bottomPadding + topPadding + (2 * UM.Theme.getSize("thin_margin").height)
  17. // offset position relative to the parent
  18. property int implicitX: parent.width - UM.Theme.getSize("default_lining").width
  19. property int implicitY: -UM.Theme.getSize("thin_margin").height
  20. default property alias contents: scrollViewContent.children
  21. x: implicitX
  22. y: implicitY
  23. // needed for the `mapToItem` function to work; apparently a Popup is not an Item
  24. Item
  25. {
  26. id: materialBrandSubMenuItem
  27. anchors.fill: parent
  28. }
  29. onOpened:
  30. {
  31. // we want to make sure here that the popup never goes out side the window so we adjust the x and y position
  32. // based on the width/height of the mainWindow/popup. QML is a bit weird here though, as the globalPosition
  33. // is in absolute coordinates relative to the origin of the mainWindow while setting the x and y coordinates
  34. // of the popup only changes the position relative to the parent.
  35. // reset position, the remainder of the function asumes this position and size
  36. materialBrandSubMenu.x = implicitX;
  37. materialBrandSubMenu.y = implicitY;
  38. materialBrandSubMenu.width = implicitWidth;
  39. materialBrandSubMenu.height = implicitHeight;
  40. const globalPosition = materialBrandSubMenuItem.mapToItem(null, 0, 0);
  41. if (globalPosition.y > mainWindow.height - materialBrandSubMenu.height)
  42. {
  43. if (mainWindow.height > materialBrandSubMenu.height)
  44. {
  45. const targetY = mainWindow.height - materialBrandSubMenu.height;
  46. const deltaY = globalPosition.y - targetY;
  47. materialBrandSubMenu.y = implicitY - deltaY;
  48. }
  49. else
  50. {
  51. // if popup is taller then the the component, limit
  52. // the components height and set the position to
  53. // y = 0 (in absolute coordinates)
  54. materialBrandSubMenu.y = implicitY - globalPosition.y;
  55. materialBrandSubMenu.height = mainWindow.height;
  56. }
  57. }
  58. // Changing the height causes implicitWidth to change because of the scrollbar appearing/disappearing
  59. // Reassign it here to update the value
  60. materialBrandSubMenu.width = implicitWidth;
  61. if (globalPosition.x > mainWindow.width - materialBrandSubMenu.width)
  62. {
  63. if (mainWindow.width > materialBrandSubMenu.width)
  64. {
  65. const targetX = mainWindow.width - materialBrandSubMenu.width;
  66. const deltaX = globalPosition.x - targetX;
  67. materialBrandSubMenu.x = implicitX - deltaX;
  68. }
  69. else
  70. {
  71. materialBrandSubMenu.x = implicitX - globalPosition.x;
  72. materialBrandSubMenu.width = mainWindow.width;
  73. }
  74. }
  75. }
  76. padding: background.border.width
  77. background: Rectangle
  78. {
  79. color: UM.Theme.getColor("main_background")
  80. border.color: UM.Theme.getColor("lining")
  81. border.width: UM.Theme.getSize("default_lining").width
  82. }
  83. ScrollView
  84. {
  85. id: scrollView
  86. anchors.fill: parent
  87. contentHeight: scrollViewContent.height
  88. clip: true
  89. ScrollBar.vertical: UM.ScrollBar
  90. {
  91. id: scrollbar
  92. anchors.right: parent.right
  93. anchors.top: parent.top
  94. anchors.bottom: parent.bottom
  95. }
  96. Rectangle
  97. {
  98. id: scrollViewContent
  99. width: childrenRect.width
  100. height: childrenRect.height
  101. color: UM.Theme.getColor("main_background")
  102. }
  103. }
  104. }