Переглянути джерело

LibGUI: Make it possible to wrap a Font in a GVariant

Andreas Kling 5 роки тому
батько
коміт
b89f64cb55
2 змінених файлів з 29 додано та 1 видалено
  1. 17 0
      Libraries/LibGUI/GVariant.cpp
  2. 12 1
      Libraries/LibGUI/GVariant.h

+ 17 - 0
Libraries/LibGUI/GVariant.cpp

@@ -16,6 +16,7 @@ const char* to_string(GVariant::Type type)
     case GVariant::Type::Point: return "Point";
     case GVariant::Type::Size: return "Size";
     case GVariant::Type::Rect: return "Rect";
+    case GVariant::Type::Font: return "Font";
     }
     ASSERT_NOT_REACHED();
 }
@@ -134,6 +135,13 @@ GVariant::GVariant(const GIcon& value)
     AK::ref_if_not_null(m_value.as_icon);
 }
 
+GVariant::GVariant(const Font& value)
+    : m_type(Type::Font)
+{
+    m_value.as_font = &const_cast<Font&>(value);
+    AK::ref_if_not_null(m_value.as_font);
+}
+
 GVariant::GVariant(Color color)
     : m_type(Type::Color)
 {
@@ -212,6 +220,10 @@ void GVariant::copy_from(const GVariant& other)
         m_value.as_icon = other.m_value.as_icon;
         AK::ref_if_not_null(m_value.as_icon);
         break;
+    case Type::Font:
+        m_value.as_font = other.m_value.as_font;
+        AK::ref_if_not_null(m_value.as_font);
+        break;
     case Type::Color:
         m_value.as_color = other.m_value.as_color;
         break;
@@ -256,6 +268,8 @@ bool GVariant::operator==(const GVariant& other) const
         return as_size() == other.as_size();
     case Type::Rect:
         return as_rect() == other.as_rect();
+    case Type::Font:
+        return &as_font() == &other.as_font();
     case Type::Invalid:
         return true;
     }
@@ -288,6 +302,7 @@ bool GVariant::operator<(const GVariant& other) const
     case Type::Point:
     case Type::Size:
     case Type::Rect:
+    case Type::Font:
         // FIXME: Figure out how to compare these.
         ASSERT_NOT_REACHED();
     case Type::Invalid:
@@ -321,6 +336,8 @@ String GVariant::to_string() const
         return as_size().to_string();
     case Type::Rect:
         return as_rect().to_string();
+    case Type::Font:
+        return String::format("[Font: %s]", as_font().name().characters());
     case Type::Invalid:
         return "[null]";
         break;

+ 12 - 1
Libraries/LibGUI/GVariant.h

@@ -1,8 +1,9 @@
 #pragma once
 
 #include <AK/String.h>
-#include <LibGUI/GIcon.h>
+#include <LibDraw/Font.h>
 #include <LibDraw/GraphicsBitmap.h>
+#include <LibGUI/GIcon.h>
 
 namespace AK {
 class JsonValue;
@@ -22,6 +23,7 @@ public:
     GVariant(const Point&);
     GVariant(const Size&);
     GVariant(const Rect&);
+    GVariant(const Font&);
     GVariant(const AK::JsonValue&);
     GVariant(Color);
 
@@ -47,6 +49,7 @@ public:
         Point,
         Size,
         Rect,
+        Font,
     };
 
     bool is_valid() const { return m_type != Type::Invalid; }
@@ -61,6 +64,7 @@ public:
     bool is_point() const { return m_type == Type::Point; }
     bool is_size() const { return m_type == Type::Size; }
     bool is_rect() const { return m_type == Type::Rect; }
+    bool is_font() const { return m_type == Type::Font; }
     Type type() const { return m_type; }
 
     bool as_bool() const
@@ -167,6 +171,12 @@ public:
         return Color::from_rgba(m_value.as_color);
     }
 
+    const Font& as_font() const
+    {
+        ASSERT(type() == Type::Font);
+        return *m_value.as_font;
+    }
+
     Color to_color(Color default_value = {}) const
     {
         if (type() == Type::Color)
@@ -206,6 +216,7 @@ private:
         StringImpl* as_string;
         GraphicsBitmap* as_bitmap;
         GIconImpl* as_icon;
+        Font* as_font;
         bool as_bool;
         int as_int;
         unsigned as_uint;