aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2022-02-24 15:56:01 +0800
committercrupest <crupest@outlook.com>2022-02-24 15:56:01 +0800
commit640401bf2f4b3da3708970a1408b27e159f93631 (patch)
treec9692f9c6e8a3efdadabc5929029837450efd6af /src
parentd9a3b8c14b9ab1bc591ca7c373daaf7141d2d098 (diff)
downloadcru-640401bf2f4b3da3708970a1408b27e159f93631.tar.gz
cru-640401bf2f4b3da3708970a1408b27e159f93631.tar.bz2
cru-640401bf2f4b3da3708970a1408b27e159f93631.zip
...
Diffstat (limited to 'src')
-rw-r--r--src/theme_builder/CMakeLists.txt1
-rw-r--r--src/theme_builder/components/properties/MeasureLengthPropertyEditor.cpp37
-rw-r--r--src/theme_builder/components/properties/MeasureLengthPropertyEditor.h35
-rw-r--r--src/ui/CMakeLists.txt1
-rw-r--r--src/ui/mapper/MapperRegistry.cpp2
-rw-r--r--src/ui/mapper/MeasureLengthMapper.cpp29
-rw-r--r--src/ui/mapper/style/PreferredSizeStylerMapper.cpp9
7 files changed, 112 insertions, 2 deletions
diff --git a/src/theme_builder/CMakeLists.txt b/src/theme_builder/CMakeLists.txt
index 6bdd44b2..0c3e1926 100644
--- a/src/theme_builder/CMakeLists.txt
+++ b/src/theme_builder/CMakeLists.txt
@@ -13,6 +13,7 @@ add_executable(cru_theme_builder
components/properties/CheckBoxPropertyEditor.cpp
components/properties/ColorPropertyEditor.cpp
components/properties/CornerRadiusPropertyEditor.cpp
+ components/properties/MeasureLengthPropertyEditor.cpp
components/properties/PointPropertyEditor.cpp
components/properties/SelectPropertyEditor.cpp
components/properties/TextPropertyEditor.cpp
diff --git a/src/theme_builder/components/properties/MeasureLengthPropertyEditor.cpp b/src/theme_builder/components/properties/MeasureLengthPropertyEditor.cpp
new file mode 100644
index 00000000..d1f4afce
--- /dev/null
+++ b/src/theme_builder/components/properties/MeasureLengthPropertyEditor.cpp
@@ -0,0 +1,37 @@
+#include "MeasureLengthPropertyEditor.h"
+#include "cru/common/Format.h"
+#include "cru/ui/mapper/MapperRegistry.h"
+#include "cru/ui/render/MeasureRequirement.h"
+
+namespace cru::theme_builder::components::properties {
+MeasureLengthPropertyEditor::MeasureLengthPropertyEditor() {
+ container_.AddChild(&label_);
+ container_.AddChild(&text_);
+
+ text_.TextChangeEvent()->AddHandler([this](std::nullptr_t) {
+ auto text = text_.GetTextView();
+ auto measure_length_mapper = ui::mapper::MapperRegistry::GetInstance()
+ ->GetMapper<ui::render::MeasureLength>();
+ try {
+ auto measure_length =
+ measure_length_mapper->MapFromString(text.ToString());
+ measure_length_ = measure_length;
+ is_text_valid_ = true;
+ RaiseChangeEvent();
+ } catch (const Exception&) {
+ is_text_valid_ = false;
+ // TODO: Show error!
+ }
+ });
+}
+
+MeasureLengthPropertyEditor::~MeasureLengthPropertyEditor() {}
+
+void MeasureLengthPropertyEditor::SetValue(
+ const ui::render::MeasureLength& value, bool trigger_change) {
+ if (!trigger_change) SuppressNextChangeEvent();
+ text_.SetText(measure_length_.IsNotSpecified()
+ ? u"unspecified"
+ : ToString(measure_length_.GetLengthOrUndefined()));
+}
+} // namespace cru::theme_builder::components::properties
diff --git a/src/theme_builder/components/properties/MeasureLengthPropertyEditor.h b/src/theme_builder/components/properties/MeasureLengthPropertyEditor.h
new file mode 100644
index 00000000..43e783c5
--- /dev/null
+++ b/src/theme_builder/components/properties/MeasureLengthPropertyEditor.h
@@ -0,0 +1,35 @@
+#pragma once
+#include "../Editor.h"
+#include "cru/platform/graphics/Base.h"
+#include "cru/ui/controls/Container.h"
+#include "cru/ui/controls/FlexLayout.h"
+#include "cru/ui/controls/TextBlock.h"
+#include "cru/ui/controls/TextBox.h"
+#include "cru/ui/render/MeasureRequirement.h"
+
+namespace cru::theme_builder::components::properties {
+class MeasureLengthPropertyEditor : public Editor {
+ public:
+ using PropertyType = ui::render::MeasureLength;
+
+ MeasureLengthPropertyEditor();
+ ~MeasureLengthPropertyEditor() override;
+
+ public:
+ ui::controls::Control* GetRootControl() override { return &container_; }
+
+ String GetLabel() const { return label_.GetText(); }
+ void SetLabel(String label) { label_.SetText(std::move(label)); }
+
+ PropertyType GetValue() const { return measure_length_; }
+ void SetValue(const PropertyType& value, bool trigger_change = true);
+
+ private:
+ PropertyType measure_length_;
+
+ ui::controls::FlexLayout container_;
+ ui::controls::TextBlock label_;
+ ui::controls::TextBox text_;
+ bool is_text_valid_;
+};
+} // namespace cru::theme_builder::components::properties
diff --git a/src/ui/CMakeLists.txt b/src/ui/CMakeLists.txt
index 2e3cd547..e02ad9da 100644
--- a/src/ui/CMakeLists.txt
+++ b/src/ui/CMakeLists.txt
@@ -34,6 +34,7 @@ add_library(cru_ui SHARED
mapper/FontMapper.cpp
mapper/Mapper.cpp
mapper/MapperRegistry.cpp
+ mapper/MeasureLengthMapper.cpp
mapper/PointMapper.cpp
mapper/SizeMapper.cpp
mapper/ThicknessMapper.cpp
diff --git a/src/ui/mapper/MapperRegistry.cpp b/src/ui/mapper/MapperRegistry.cpp
index b70418ef..9ae95a59 100644
--- a/src/ui/mapper/MapperRegistry.cpp
+++ b/src/ui/mapper/MapperRegistry.cpp
@@ -5,6 +5,7 @@
#include "cru/ui/mapper/CornerRadiusMapper.h"
#include "cru/ui/mapper/CursorMapper.h"
#include "cru/ui/mapper/FontMapper.h"
+#include "cru/ui/mapper/MeasureLengthMapper.h"
#include "cru/ui/mapper/PointMapper.h"
#include "cru/ui/mapper/SizeMapper.h"
#include "cru/ui/mapper/ThicknessMapper.h"
@@ -33,6 +34,7 @@ MapperRegistry::MapperRegistry() {
RegisterMapper(new BrushMapper());
RegisterMapper(new CornerRadiusMapper());
RegisterMapper(new FontMapper());
+ RegisterMapper(new MeasureLengthMapper());
RegisterMapper(new PointMapper());
RegisterMapper(new SizeMapper());
RegisterMapper(new ThicknessMapper());
diff --git a/src/ui/mapper/MeasureLengthMapper.cpp b/src/ui/mapper/MeasureLengthMapper.cpp
new file mode 100644
index 00000000..6a0f95e3
--- /dev/null
+++ b/src/ui/mapper/MeasureLengthMapper.cpp
@@ -0,0 +1,29 @@
+#include "cru/ui/mapper/MeasureLengthMapper.h"
+#include "cru/ui/render/MeasureRequirement.h"
+
+namespace cru::ui::mapper {
+bool MeasureLengthMapper::XmlElementIsOfThisType(xml::XmlElementNode* node) {
+ return node->GetTag().CaseInsensitiveEqual(u"MeasureLength");
+}
+
+render::MeasureLength MeasureLengthMapper::DoMapFromString(String str) {
+ if (str.CaseInsensitiveEqual(u"notspecified")) {
+ return render::MeasureLength::NotSpecified();
+ }
+ if (str.CaseInsensitiveEqual(u"unspecified")) {
+ return render::MeasureLength::NotSpecified();
+ }
+ auto value = str.ParseToFloat();
+ if (value < 0) {
+ return render::MeasureLength::NotSpecified();
+ }
+ return render::MeasureLength(value);
+}
+
+render::MeasureLength MeasureLengthMapper::DoMapFromXml(
+ xml::XmlElementNode* node) {
+ auto value_attr = node->GetOptionalAttributeCaseInsensitive(u"value");
+ if (!value_attr) return {};
+ return DoMapFromString(*value_attr);
+}
+} // namespace cru::ui::mapper
diff --git a/src/ui/mapper/style/PreferredSizeStylerMapper.cpp b/src/ui/mapper/style/PreferredSizeStylerMapper.cpp
index b4382c19..9e03dfeb 100644
--- a/src/ui/mapper/style/PreferredSizeStylerMapper.cpp
+++ b/src/ui/mapper/style/PreferredSizeStylerMapper.cpp
@@ -1,4 +1,5 @@
#include "cru/ui/mapper/style/PreferredSizeStylerMapper.h"
+#include "cru/ui/mapper/MapperRegistry.h"
#include "cru/ui/render/MeasureRequirement.h"
#include "cru/ui/style/Styler.h"
@@ -11,14 +12,18 @@ bool PreferredSizeStylerMapper::XmlElementIsOfThisType(
ClonablePtr<ui::style::PreferredSizeStyler>
PreferredSizeStylerMapper::DoMapFromXml(xml::XmlElementNode* node) {
render::MeasureSize size;
+
+ auto measure_length_mapper =
+ MapperRegistry::GetInstance()->GetMapper<render::MeasureLength>();
+
auto width_attribute = node->GetOptionalAttributeCaseInsensitive(u"width");
if (width_attribute) {
- size.width = width_attribute->ParseToFloat();
+ size.width = measure_length_mapper->MapFromString(*width_attribute);
}
auto height_attribute = node->GetOptionalAttributeCaseInsensitive(u"height");
if (height_attribute) {
- size.height = height_attribute->ParseToFloat();
+ size.height = measure_length_mapper->MapFromString(*height_attribute);
}
return ui::style::PreferredSizeStyler::Create(size);