diff options
author | crupest <crupest@outlook.com> | 2022-03-10 18:03:49 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2022-03-10 18:03:49 +0800 |
commit | 1cf1002f1d3cd81ddb920eb7196a067b566f11e2 (patch) | |
tree | 8a299d16f0f5074d876175a602190f477bd498ea /src | |
parent | 51f87e3ff980e62f9cb5ee656e5591412e2766eb (diff) | |
download | cru-1cf1002f1d3cd81ddb920eb7196a067b566f11e2.tar.gz cru-1cf1002f1d3cd81ddb920eb7196a067b566f11e2.tar.bz2 cru-1cf1002f1d3cd81ddb920eb7196a067b566f11e2.zip |
...
Diffstat (limited to 'src')
-rw-r--r-- | src/osx/graphics/quartz/Font.cpp | 3 | ||||
-rw-r--r-- | src/theme_builder/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/theme_builder/components/properties/FontPropertyEditor.cpp | 19 | ||||
-rw-r--r-- | src/theme_builder/components/properties/FontPropertyEditor.h | 3 | ||||
-rw-r--r-- | src/ui/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/ui/components/Input.cpp | 94 |
6 files changed, 115 insertions, 6 deletions
diff --git a/src/osx/graphics/quartz/Font.cpp b/src/osx/graphics/quartz/Font.cpp index 605a1453..a1b8bac2 100644 --- a/src/osx/graphics/quartz/Font.cpp +++ b/src/osx/graphics/quartz/Font.cpp @@ -9,7 +9,7 @@ using cru::platform::osx::Convert; OsxCTFont::OsxCTFont(IGraphicsFactory* graphics_factory, const String& name, float size) - : OsxQuartzResource(graphics_factory) { + : OsxQuartzResource(graphics_factory), name_(name) { CFStringRef n = Convert(name); if (name.empty()) { @@ -25,5 +25,6 @@ OsxCTFont::OsxCTFont(IGraphicsFactory* graphics_factory, const String& name, OsxCTFont::~OsxCTFont() { CFRelease(ct_font_); } +String OsxCTFont::GetFontName() { return name_; } float OsxCTFont::GetFontSize() { return CTFontGetSize(ct_font_); } } // namespace cru::platform::graphics::osx::quartz diff --git a/src/theme_builder/CMakeLists.txt b/src/theme_builder/CMakeLists.txt index 40d23c4a..d1f30d61 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/FontPropertyEditor.cpp components/properties/MeasureLengthPropertyEditor.cpp components/properties/PointPropertyEditor.cpp components/properties/SelectPropertyEditor.cpp diff --git a/src/theme_builder/components/properties/FontPropertyEditor.cpp b/src/theme_builder/components/properties/FontPropertyEditor.cpp index b4f5fa06..927ada7d 100644 --- a/src/theme_builder/components/properties/FontPropertyEditor.cpp +++ b/src/theme_builder/components/properties/FontPropertyEditor.cpp @@ -24,13 +24,14 @@ FontPropertyEditor::FontPropertyEditor() { font_size_container_.SetFlexDirection(FlexDirection::Horizontal); font_size_container_.AddChild(&font_size_label_); - font_size_container_.AddChild(&font_size_text_); + font_size_container_.AddChild(font_size_input_.GetRootControl()); font_size_label_.SetText(u"Font Size"); + font_size_input_.SetMin(0.0f); font_family_text_.TextChangeEvent()->AddSpyOnlyHandler( [this] { RaiseChangeEvent(); }); - font_size_text_.TextChangeEvent()->AddSpyOnlyHandler( + font_size_input_.ChangeEvent()->AddSpyOnlyHandler( [this] { RaiseChangeEvent(); }); } @@ -42,8 +43,18 @@ std::shared_ptr<platform::graphics::IFont> FontPropertyEditor::GetValue() const { return platform::gui::IUiApplication::GetInstance() ->GetGraphicsFactory() - ->CreateFont(font_family_text_.GetText(), - font_size_text_.GetText().ParseToFloat()); + ->CreateFont(font_family_text_.GetText(), font_size_input_.GetValue()); } +void FontPropertyEditor::SetValue( + std::shared_ptr<platform::graphics::IFont> value, bool trigger_change) { + SuppressNextChangeEvent(); + font_family_text_.SetText(value->GetFontName()); + SuppressNextChangeEvent(); + font_size_input_.SetValue(value->GetFontSize()); + + if (trigger_change) { + RaiseChangeEvent(); + } +} } // namespace cru::theme_builder::components::properties diff --git a/src/theme_builder/components/properties/FontPropertyEditor.h b/src/theme_builder/components/properties/FontPropertyEditor.h index f7dd0362..d349f1f2 100644 --- a/src/theme_builder/components/properties/FontPropertyEditor.h +++ b/src/theme_builder/components/properties/FontPropertyEditor.h @@ -1,6 +1,7 @@ #pragma once #include "../Editor.h" #include "cru/platform/graphics/Font.h" +#include "cru/ui/components/Input.h" #include "cru/ui/controls/Control.h" #include "cru/ui/controls/FlexLayout.h" #include "cru/ui/controls/TextBlock.h" @@ -32,6 +33,6 @@ class FontPropertyEditor : public Editor { ui::controls::TextBox font_family_text_; ui::controls::FlexLayout font_size_container_; ui::controls::TextBlock font_size_label_; - ui::controls::TextBox font_size_text_; + ui::components::FloatInput font_size_input_; }; } // namespace cru::theme_builder::components::properties diff --git a/src/ui/CMakeLists.txt b/src/ui/CMakeLists.txt index c9f0df8e..408c93d8 100644 --- a/src/ui/CMakeLists.txt +++ b/src/ui/CMakeLists.txt @@ -3,6 +3,7 @@ add_library(cru_ui SHARED ThemeManager.cpp ThemeResourceDictionary.cpp components/Component.cpp + components/Input.cpp components/Menu.cpp components/PopupButton.cpp components/Select.cpp diff --git a/src/ui/components/Input.cpp b/src/ui/components/Input.cpp new file mode 100644 index 00000000..daca68c4 --- /dev/null +++ b/src/ui/components/Input.cpp @@ -0,0 +1,94 @@ +#include "cru/ui/components/Input.h" +#include <cmath> +#include <optional> +#include "cru/common/StringToNumberConverter.h" +#include "cru/ui/controls/Control.h" + +namespace cru::ui::components { +Input::Input() : last_validate_result_{true, u"Good value"} { + text_box_.TextChangeEvent()->AddSpyOnlyHandler([this] { + auto text = text_box_.GetText(); + auto validate_result = Validate(); + this->change_event_.Raise({std::move(text), validate_result.valid, + std::move(validate_result.message)}); + }); +} + +Input::~Input() {} + +controls::Control* Input::GetRootControl() { return &text_box_; } + +String Input::GetText() const { return text_box_.GetText(); } + +void Input::SetText(String text) { text_box_.SetText(std::move(text)); } + +IInputValidator* Input::GetValidator() const { return validator_; } + +void Input::SetValidator(IInputValidator* validator) { + validator_ = validator; + Validate(); +} + +InputValidateResult Input::Validate() { + if (validator_) + last_validate_result_ = validator_->Validate(text_box_.GetTextView()); + else + last_validate_result_ = {true, u"Good value"}; + return last_validate_result_; +} + +InputValidateResult Input::GetLastValidateResult() const { + return last_validate_result_; +} + +InputValidateResult FloatInputValidator::Validate(StringView text) const { + auto result = + text.ParseToFloat(nullptr, StringToNumberFlags::kAllowLeadingSpaces & + StringToNumberFlags::kAllowTrailingSpaces); + if (std::isnan(result)) { + return InputValidateResult{false, u"Invalid number."}; + } + + if (min && result < *min) { + return InputValidateResult{false, u"Value is less than minimum."}; + } + + if (max && result > *max) { + return InputValidateResult{false, u"Value is greater than maximum."}; + } + + return InputValidateResult{true, u"Good number"}; +} + +FloatInput::FloatInput() { + SetValidator(&validator_); + + ChangeEvent()->AddHandler([this](const InputChangeEventArgs& args) { + if (args.valid) { + value_ = args.text.ParseToFloat( + nullptr, StringToNumberFlags::kAllowLeadingSpaces & + StringToNumberFlags::kAllowTrailingSpaces); + } + }); +} + +FloatInput::~FloatInput() {} + +float FloatInput::GetValue() const { return value_; } + +void FloatInput::SetValue(float value) { SetText(ToString(value)); } + +std::optional<float> FloatInput::GetMin() const { return validator_.min; } + +void FloatInput::SetMin(std::optional<float> min) { + validator_.min = std::move(min); + Validate(); +} + +std::optional<float> FloatInput::GetMax() const { return validator_.max; } + +void FloatInput::SetMax(std::optional<float> max) { + validator_.max = std::move(max); + Validate(); +} +} // namespace cru::ui::components |