From 20dc75e2ce6a9c38dd1888fdbf793fd8a3bc9cd3 Mon Sep 17 00:00:00 2001 From: crupest Date: Sat, 29 Sep 2018 17:35:09 +0800 Subject: Add PropertyChangedNotifyObject and BorderProperty. --- src/base.cpp | 21 +++++++++++++ src/base.h | 29 ++++++++++++++++++ src/ui/controls/border_control.cpp | 34 +++++++++++++++++++++ src/ui/controls/border_control.h | 61 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 145 insertions(+) create mode 100644 src/ui/controls/border_control.cpp create mode 100644 src/ui/controls/border_control.h (limited to 'src') diff --git a/src/base.cpp b/src/base.cpp index f5868170..57a4848e 100644 --- a/src/base.cpp +++ b/src/base.cpp @@ -17,4 +17,25 @@ namespace cru throw Win32Error(::GetLastError(), "Failed to convert wide string to UTF-8."); return result; } + + void PropertyChangedNotifyObject::AddPropertyChangedListener(FunctionPtr listener) + { + listeners_.push_back(std::move(listener)); + } + + void PropertyChangedNotifyObject::RemovePropertyChangedListener(const FunctionPtr& listener) + { + for (auto i = listeners_.cbegin(); i != listeners_.cend(); ++i) + if (*i == listener) + { + listeners_.erase(i); + break; + } + } + + void PropertyChangedNotifyObject::RaisePropertyChangedEvent(String property_name) + { + for (const auto& listener : listeners_) + (*listener)(property_name); + } } diff --git a/src/base.h b/src/base.h index 7ef78014..ce7a781d 100644 --- a/src/base.h +++ b/src/base.h @@ -20,6 +20,7 @@ #include #include #include +#include namespace cru { @@ -60,6 +61,12 @@ namespace cru return std::make_shared(std::forward(args)...); } + template + Type CreateFunctionPtr(Function&& function) + { + return std::make_shared>(std::move(function)); + } + inline ActionPtr CreateActionPtr(Action&& action) { return std::make_shared(std::move(action)); @@ -104,4 +111,26 @@ namespace cru using CancelablePtr = std::shared_ptr; MultiByteString ToUtf8String(const StringView& string); + + + class PropertyChangedNotifyObject : public Object + { + public: + PropertyChangedNotifyObject() = default; + PropertyChangedNotifyObject(const PropertyChangedNotifyObject& other) = delete; + PropertyChangedNotifyObject(PropertyChangedNotifyObject&& other) = delete; + PropertyChangedNotifyObject& operator = (const PropertyChangedNotifyObject& other) = delete; + PropertyChangedNotifyObject& operator = (PropertyChangedNotifyObject&& other) = delete; + ~PropertyChangedNotifyObject() override = default; + + void AddPropertyChangedListener(FunctionPtr listener); + + void RemovePropertyChangedListener(const FunctionPtr& listener); + + protected: + void RaisePropertyChangedEvent(String property_name); + + private: + std::list> listeners_; + }; } diff --git a/src/ui/controls/border_control.cpp b/src/ui/controls/border_control.cpp new file mode 100644 index 00000000..9f33079c --- /dev/null +++ b/src/ui/controls/border_control.cpp @@ -0,0 +1,34 @@ +#include "border_control.h" + +namespace cru::ui::controls +{ + void BorderProperty::SetBrush(Microsoft::WRL::ComPtr brush) + { + brush_ = std::move(brush); + RaisePropertyChangedEvent(L"Brush"); + } + + void BorderProperty::SetWidth(const float width) + { + width_ = width; + RaisePropertyChangedEvent(L"Width"); + } + + void BorderProperty::SetStrokeStyle(Microsoft::WRL::ComPtr stroke_style) + { + stroke_style_ = std::move(stroke_style); + RaisePropertyChangedEvent(L"StrokeStyle"); + } + + void BorderProperty::SetRadiusX(const float radius_x) + { + radius_x_ = radius_x; + RaisePropertyChangedEvent(L"RadiusX"); + } + + void BorderProperty::SetRadiusY(const float radius_y) + { + radius_y_ = radius_y; + RaisePropertyChangedEvent(L"RadiusY"); + } +} diff --git a/src/ui/controls/border_control.h b/src/ui/controls/border_control.h new file mode 100644 index 00000000..582d2436 --- /dev/null +++ b/src/ui/controls/border_control.h @@ -0,0 +1,61 @@ +#pragma once + +#include "ui/control.h" + +namespace cru::ui::controls +{ + class BorderProperty : public PropertyChangedNotifyObject + { + public: + BorderProperty() = default; + BorderProperty(const BorderProperty& other) = delete; + BorderProperty(BorderProperty&& other) = delete; + BorderProperty& operator=(const BorderProperty& other) = delete; + BorderProperty& operator=(BorderProperty&& other) = delete; + ~BorderProperty() override = default; + + + Microsoft::WRL::ComPtr GetBrush() const + { + return brush_; + } + + float GetWidth() const + { + return width_; + } + + Microsoft::WRL::ComPtr GetStrokeStyle() const + { + return stroke_style_; + } + + float GetRadiusX() const + { + return radius_x_; + } + + float GetRadiusY() const + { + return radius_y_; + } + + void SetBrush(Microsoft::WRL::ComPtr brush); + void SetWidth(float width); + void SetStrokeStyle(Microsoft::WRL::ComPtr stroke_style); + void SetRadiusX(float radius_x); + void SetRadiusY(float radius_y); + + private: + Microsoft::WRL::ComPtr brush_ = nullptr; + float width_ = 1.0f; + Microsoft::WRL::ComPtr stroke_style_ = nullptr; + float radius_x_ = 0.0f; + float radius_y_ = 0.0f; + }; + + class BorderControl : public Control + { + + }; +} -- cgit v1.2.3