layout_widget_construct.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. """
  2. Copyright (c) Contributors to the Open 3D Engine Project.
  3. For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. SPDX-License-Identifier: Apache-2.0 OR MIT
  5. """
  6. from aws_cdk import (
  7. aws_cloudwatch as cloudwatch
  8. )
  9. from typing import List
  10. from . import aws_metrics_constants
  11. class LayoutWidget(cloudwatch.Column):
  12. """
  13. Construct for creating a custom layout with a maximum width.
  14. The layout has a text widget containing a short description on the first row and
  15. all other widgets will be presented on the following rows based on their width values.
  16. """
  17. def __init__(
  18. self,
  19. widgets: List[cloudwatch.IWidget],
  20. layout_description: str,
  21. max_width: int = aws_metrics_constants.DASHBOARD_MAX_WIDGET_WIDTH) -> None:
  22. self._max_width = max_width
  23. self._Rows = []
  24. self._currentRowWidth = 0
  25. self._currentRowWidgets = []
  26. widgets.insert(
  27. 0,
  28. cloudwatch.TextWidget(
  29. markdown=layout_description,
  30. width=max_width
  31. )
  32. )
  33. # Add all the widgets to the layout.
  34. self.__add_widgets(widgets)
  35. super().__init__(*self._Rows)
  36. def __add_widgets(
  37. self,
  38. widgets: [cloudwatch.IWidget]
  39. ):
  40. for widget in widgets:
  41. if (self._currentRowWidth + widget.width) > self._max_width:
  42. # The current row is full. Add the new widget to the next row.
  43. self._Rows.append(cloudwatch.Row(*self._currentRowWidgets))
  44. self._currentRowWidgets.clear()
  45. self._currentRowWidth = 0
  46. self._currentRowWidgets.append(widget)
  47. self._currentRowWidth = self._currentRowWidth + widget.width
  48. if self._currentRowWidth > 0:
  49. # Add the rest widgets to the last row even if the row is not full.
  50. self._Rows.append(cloudwatch.Row(*self._currentRowWidgets))