dimension.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. """
  2. Layout dimensions are used to give the minimum, maximum and preferred
  3. dimensions for containers and controls.
  4. """
  5. from __future__ import unicode_literals
  6. __all__ = (
  7. 'LayoutDimension',
  8. 'sum_layout_dimensions',
  9. 'max_layout_dimensions',
  10. )
  11. class LayoutDimension(object):
  12. """
  13. Specified dimension (width/height) of a user control or window.
  14. The layout engine tries to honor the preferred size. If that is not
  15. possible, because the terminal is larger or smaller, it tries to keep in
  16. between min and max.
  17. :param min: Minimum size.
  18. :param max: Maximum size.
  19. :param weight: For a VSplit/HSplit, the actual size will be determined
  20. by taking the proportion of weights from all the children.
  21. E.g. When there are two children, one width a weight of 1,
  22. and the other with a weight of 2. The second will always be
  23. twice as big as the first, if the min/max values allow it.
  24. :param preferred: Preferred size.
  25. """
  26. def __init__(self, min=None, max=None, weight=1, preferred=None):
  27. assert isinstance(weight, int) and weight > 0 # Cannot be a float.
  28. self.min_specified = min is not None
  29. self.max_specified = max is not None
  30. self.preferred_specified = preferred is not None
  31. if min is None:
  32. min = 0 # Smallest possible value.
  33. if max is None: # 0-values are allowed, so use "is None"
  34. max = 1000 ** 10 # Something huge.
  35. if preferred is None:
  36. preferred = min
  37. self.min = min
  38. self.max = max
  39. self.preferred = preferred
  40. self.weight = weight
  41. # Make sure that the 'preferred' size is always in the min..max range.
  42. if self.preferred < self.min:
  43. self.preferred = self.min
  44. if self.preferred > self.max:
  45. self.preferred = self.max
  46. @classmethod
  47. def exact(cls, amount):
  48. """
  49. Return a :class:`.LayoutDimension` with an exact size. (min, max and
  50. preferred set to ``amount``).
  51. """
  52. return cls(min=amount, max=amount, preferred=amount)
  53. def __repr__(self):
  54. return 'LayoutDimension(min=%r, max=%r, preferred=%r, weight=%r)' % (
  55. self.min, self.max, self.preferred, self.weight)
  56. def __add__(self, other):
  57. return sum_layout_dimensions([self, other])
  58. def sum_layout_dimensions(dimensions):
  59. """
  60. Sum a list of :class:`.LayoutDimension` instances.
  61. """
  62. min = sum([d.min for d in dimensions if d.min is not None])
  63. max = sum([d.max for d in dimensions if d.max is not None])
  64. preferred = sum([d.preferred for d in dimensions])
  65. return LayoutDimension(min=min, max=max, preferred=preferred)
  66. def max_layout_dimensions(dimensions):
  67. """
  68. Take the maximum of a list of :class:`.LayoutDimension` instances.
  69. """
  70. min_ = max([d.min for d in dimensions if d.min is not None])
  71. max_ = max([d.max for d in dimensions if d.max is not None])
  72. preferred = max([d.preferred for d in dimensions])
  73. return LayoutDimension(min=min_, max=max_, preferred=preferred)