Length.h 975 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #pragma once
  2. #include <AK/String.h>
  3. class Length {
  4. public:
  5. enum class Type {
  6. Auto,
  7. Absolute,
  8. };
  9. Length() {}
  10. Length(int value, Type type)
  11. : m_type(type)
  12. , m_value(value)
  13. {
  14. }
  15. Length(float value, Type type)
  16. : m_type(type)
  17. , m_value(value)
  18. {
  19. }
  20. ~Length() {}
  21. bool is_auto() const { return m_type == Type::Auto; }
  22. bool is_absolute() const { return m_type == Type::Absolute; }
  23. float value() const { return m_value; }
  24. String to_string() const
  25. {
  26. if (is_auto())
  27. return "[Length/auto]";
  28. return String::format("%g [Length/px]", m_value);
  29. }
  30. float to_px() const
  31. {
  32. if (is_auto())
  33. return 0;
  34. return m_value;
  35. }
  36. private:
  37. Type m_type { Type::Auto };
  38. float m_value { 0 };
  39. };
  40. inline const LogStream& operator<<(const LogStream& stream, const Length& value)
  41. {
  42. return stream << value.to_string();
  43. }