Browse Source

chore: add more property types parsing so #350 backgrounds boot up

Almamu 1 năm trước cách đây
mục cha
commit
0a7306c231

+ 2 - 2
src/WallpaperEngine/Data/JSON.h

@@ -61,7 +61,7 @@ class JsonExtensions {
         const auto it = base.find (key);
 
         if (it == base.end ()) {
-            sLog.exception (message);
+            sLog.exception (message, ". Contents: ", base.dump ());
         }
 
         return *it;
@@ -72,7 +72,7 @@ class JsonExtensions {
         const auto it = base.find (key);
 
         if (it == base.end ()) {
-            sLog.exception (message);
+            sLog.exception (message, ". Contents: ", base.dump ());
         }
 
         return (*it);

+ 9 - 9
src/WallpaperEngine/Data/Model/DynamicValue.cpp

@@ -3,39 +3,39 @@
 using namespace WallpaperEngine::Data::Model;
 
 DynamicValue::DynamicValue (const glm::ivec4& value) {
-    this->update (value);
+    this->DynamicValue::update (value);
 }
 
 DynamicValue::DynamicValue (const glm::ivec3& value) {
-    this->update (value);
+    this->DynamicValue::update (value);
 }
 
 DynamicValue::DynamicValue (const glm::ivec2& value) {
-    this->update (value);
+    this->DynamicValue::update (value);
 }
 
 DynamicValue::DynamicValue (const glm::vec4& value) {
-    this->update (value);
+    this->DynamicValue::update (value);
 }
 
 DynamicValue::DynamicValue (const glm::vec3& value) {
-    this->update (value);
+    this->DynamicValue::update (value);
 }
 
 DynamicValue::DynamicValue (const glm::vec2& value) {
-    this->update (value);
+    this->DynamicValue::update (value);
 }
 
 DynamicValue::DynamicValue (float value) {
-    this->update (value);
+    this->DynamicValue::update (value);
 }
 
 DynamicValue::DynamicValue (int value) {
-    this->update (value);
+    this->DynamicValue::update (value);
 }
 
 DynamicValue::DynamicValue (bool value) {
-    this->update (value);
+    this->DynamicValue::update (value);
 }
 
 DynamicValue::~DynamicValue () {

+ 10 - 10
src/WallpaperEngine/Data/Model/DynamicValue.h

@@ -47,16 +47,16 @@ class DynamicValue {
     [[nodiscard]] UnderlyingType getType () const;
     [[nodiscard]] virtual std::string toString () const;
 
-    void update (float newValue);
-    void update (int newValue);
-    void update (bool newValue);
-    void update (const glm::vec2& newValue);
-    void update (const glm::vec3& newValue);
-    void update (const glm::vec4& newValue);
-    void update (const glm::ivec2& newValue);
-    void update (const glm::ivec3& newValue);
-    void update (const glm::ivec4& newValue);
-    void update (const DynamicValue& other);
+    virtual void update (float newValue);
+    virtual void update (int newValue);
+    virtual void update (bool newValue);
+    virtual void update (const glm::vec2& newValue);
+    virtual void update (const glm::vec3& newValue);
+    virtual void update (const glm::vec4& newValue);
+    virtual void update (const glm::ivec2& newValue);
+    virtual void update (const glm::ivec3& newValue);
+    virtual void update (const glm::ivec4& newValue);
+    virtual void update (const DynamicValue& other);
 
     /**
      * Registers the given callback to be called when the value changes

+ 40 - 3
src/WallpaperEngine/Data/Model/Property.h

@@ -36,7 +36,7 @@ class Property : public DynamicValue, public TypeCaster, public PropertyData {
 class PropertySlider final : public Property, SliderData {
   public:
     PropertySlider (PropertyData data, SliderData sliderData, const float value) : Property (std::move(data)), SliderData (std::move (sliderData)) {
-        this->update (value);
+        this->Property::update (value);
     }
 
     using Property::update;
@@ -48,7 +48,7 @@ class PropertySlider final : public Property, SliderData {
 class PropertyBoolean final : public Property {
   public:
     explicit PropertyBoolean (PropertyData data, const bool value) : Property (std::move(data)) {
-        this->update (value);
+        this->Property::update (value);
     }
 
     using Property::update;
@@ -118,8 +118,17 @@ class PropertyCombo final : public Property, ComboData {
 
     using Property::update;
     void update(const std::string& value) override {
-        this->update (std::stoi (value));
+        // search for the value in the combo options
+        if (this->values.contains (value)) {
+            this->m_value = this->values.at (value);
+        } else {
+            // default to a textual value if it's not found
+            this->m_value = value;
+        }
     }
+
+  private:
+    std::string m_value;
 };
 
 class PropertyText final : public Property {
@@ -149,4 +158,32 @@ class PropertySceneTexture final : public Property {
   private:
     std::string m_value;
 };
+
+class PropertyFile final : public Property {
+  public:
+    explicit PropertyFile (PropertyData data, const std::string& value) : Property (std::move(data)) {
+        this->PropertyFile::update (value);
+    }
+
+    void update(const std::string& value) override {
+        this->m_value = value;
+    }
+
+  private:
+    std::string m_value;
+};
+
+class PropertyTextInput final : public Property {
+  public:
+    explicit PropertyTextInput (PropertyData data, const std::string& value) : Property (std::move(data)) {
+        this->PropertyTextInput::update (value);
+    }
+
+    void update(const std::string& value) override {
+        this->m_value = value;
+    }
+
+  private:
+    std::string m_value;
+};
 }

+ 23 - 2
src/WallpaperEngine/Data/Parsers/PropertyParser.cpp

@@ -5,7 +5,8 @@ using namespace WallpaperEngine::Data::Parsers;
 using namespace WallpaperEngine::Data::Model;
 
 PropertySharedPtr PropertyParser::parse (const JSON& it, const std::string& name) {
-    const auto type = it.require <std::string> ("type", "Property type is required");
+    // type might not be included, in which case means the same as a group
+    const auto type = it.optional ("type");
 
     if (type == "color") {
         return parseColor (it, name);
@@ -25,8 +26,14 @@ PropertySharedPtr PropertyParser::parse (const JSON& it, const std::string& name
     if (type == "scenetexture") {
         return parseSceneTexture (it, name);
     }
+    if (type == "file") {
+        return parseFile (it, name);
+    }
+    if (type == "textinput") {
+        return parseTextInput (it, name);
+    }
 
-    if (type != "group") {
+    if (type.has_value () && type != "group") {
         // show the error and ignore this property
         sLog.error ("Unexpected type for property: ", type);
         sLog.error (it.dump ());
@@ -105,4 +112,18 @@ PropertySharedPtr PropertyParser::parseSceneTexture (const JSON& it, const std::
         .name = name,
         .text = it.optional <std::string> ("text", ""),
     }, it.require ("value", "Property must have a value"));
+}
+
+PropertySharedPtr PropertyParser::parseFile (const JSON& it, const std::string& name) {
+    return std::make_shared <PropertyFile> (PropertyData {
+        .name = name,
+        .text = it.optional <std::string> ("text", ""),
+    }, it.optional <std::string> ("value", ""));
+}
+
+PropertySharedPtr PropertyParser::parseTextInput (const JSON& it, const std::string& name) {
+    return std::make_shared <PropertyTextInput> (PropertyData {
+        .name = name,
+        .text = it.optional <std::string> ("text", ""),
+    }, it.require ("value", "Property must have a value").dump ());
 }

+ 2 - 0
src/WallpaperEngine/Data/Parsers/PropertyParser.h

@@ -17,5 +17,7 @@ class PropertyParser {
     static PropertySharedPtr parseSlider (const JSON& it, const std::string& name);
     static PropertySharedPtr parseText (const JSON& it, const std::string& name);
     static PropertySharedPtr parseSceneTexture (const JSON& it, const std::string& name);
+    static PropertySharedPtr parseFile (const JSON& it, const std::string& name);
+    static PropertySharedPtr parseTextInput (const JSON& it, const std::string& name);
 };
 }

+ 3 - 3
src/main.cpp

@@ -20,7 +20,7 @@ void initLogging () {
 }
 
 int main (int argc, char* argv[]) {
-    try {
+    //try {
         // if type parameter is specified, this is a subprocess, so no logging should be enabled from our side
         bool enableLogging = true;
         const std::string typeZygote = "--type=zygote";
@@ -66,8 +66,8 @@ int main (int argc, char* argv[]) {
         delete app;
 
         return 0;
-    } catch (const std::exception& e) {
+    /*} catch (const std::exception& e) {
         std::cerr << e.what () << std::endl;
         return 1;
-    }
+    }*/
 }