Browse Source

LibGfx+LibGUI: Allow setting tab text alignment with set_property()

Andreas Kling 4 năm trước cách đây
mục cha
commit
e78cf6c590
2 tập tin đã thay đổi với 28 bổ sung0 xóa
  1. 8 0
      Libraries/LibGUI/TabWidget.cpp
  2. 20 0
      Libraries/LibGfx/TextAlignment.h

+ 8 - 0
Libraries/LibGUI/TabWidget.cpp

@@ -24,6 +24,7 @@
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+#include <AK/JsonObject.h>
 #include <AK/JsonValue.h>
 #include <LibGUI/BoxLayout.h>
 #include <LibGUI/Painter.h>
@@ -407,6 +408,13 @@ bool TabWidget::set_property(const StringView& name, const JsonValue& value)
         return true;
     }
 
+    if (name == "text_alignment") {
+        auto alignment = Gfx::text_alignment_from_string(value.to_string());
+        if (alignment.has_value())
+            set_text_alignment(alignment.value());
+        return true;
+    }
+
     return Widget::set_property(name, value);
 }
 

+ 20 - 0
Libraries/LibGfx/TextAlignment.h

@@ -26,6 +26,9 @@
 
 #pragma once
 
+#include <AK/Optional.h>
+#include <AK/StringView.h>
+
 namespace Gfx {
 
 enum class TextAlignment {
@@ -49,4 +52,21 @@ inline bool is_right_text_alignment(TextAlignment alignment)
     }
 }
 
+inline Optional<TextAlignment> text_alignment_from_string(const StringView& string)
+{
+    if (string == "TopLeft")
+        return TextAlignment::TopLeft;
+    if (string == "CenterLeft")
+        return TextAlignment::CenterLeft;
+    if (string == "Center")
+        return TextAlignment::Center;
+    if (string == "CenterRight")
+        return TextAlignment::CenterRight;
+    if (string == "TopRight")
+        return TextAlignment::TopRight;
+    if (string == "BottomRight")
+        return TextAlignment::BottomRight;
+    return {};
+}
+
 }