VisualBuilder: Let's have getters and setters for properties.

This commit is contained in:
Andreas Kling 2019-04-14 04:14:23 +02:00
parent 6dc9a6ef42
commit f1b58d8d8c
Notes: sideshowbarker 2024-07-19 14:43:31 +09:00
3 changed files with 24 additions and 3 deletions

View file

@ -6,6 +6,15 @@ VBProperty::VBProperty(const String& name, const GVariant& value)
{
}
VBProperty::VBProperty(const String& name, Function<GVariant(const GWidget&)>&& getter, Function<void(GWidget&, const GVariant&)>&& setter)
: m_name(name)
, m_getter(move(getter))
, m_setter(move(setter))
{
ASSERT(m_getter);
ASSERT(m_setter);
}
VBProperty::~VBProperty()
{
}

View file

@ -1,11 +1,15 @@
#pragma once
#include <AK/AKString.h>
#include <AK/Function.h>
#include <LibGUI/GVariant.h>
class GWidget;
class VBProperty {
public:
VBProperty(const String& name, const GVariant& value);
VBProperty(const String& name, Function<GVariant(const GWidget&)>&& getter, Function<void(GWidget&, const GVariant&)>&& setter);
~VBProperty();
String name() const { return m_name; }
@ -15,8 +19,12 @@ public:
bool is_readonly() const { return m_readonly; }
void set_readonly(bool b) { m_readonly = b; }
void sync();
private:
String m_name;
GVariant m_value;
Function<GVariant(const GWidget&)> m_getter;
Function<void(GWidget&, const GVariant&)> m_setter;
bool m_readonly { false };
};

View file

@ -76,11 +76,15 @@ static GWidget* build_gwidget(VBWidgetType type, GWidget* parent)
GWidget* VBWidgetRegistry::build_gwidget(VBWidgetType type, GWidget* parent, Vector<OwnPtr<VBProperty>>& properties)
{
auto* gwidget = ::build_gwidget(type, parent);
auto add_property = [&properties] (const String& name, const GVariant& value = { }, bool is_readonly = false) {
auto add_readonly_property = [&properties] (const String& name, const GVariant& value) {
auto property = make<VBProperty>(name, value);
property->set_readonly(is_readonly);
property->set_readonly(true);
properties.append(move(property));
};
add_property("class", to_class_name(type), true);
auto add_property = [&properties] (const String& name, Function<GVariant(const GWidget&)>&& getter, Function<void(GWidget&, const GVariant&)>&& setter) {
auto property = make<VBProperty>(name, move(getter), move(setter));
properties.append(move(property));
};
add_readonly_property("class", to_class_name(type));
return gwidget;
}