MaterialBrandSubMenu.qml 3.8 KB

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