aboutsummaryrefslogtreecommitdiff
path: root/src/ui/controls/border_delegate.cpp
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2018-09-30 20:38:10 +0800
committercrupest <crupest@outlook.com>2018-09-30 20:38:10 +0800
commit88765aab936724cb01fa2ffd86d65181182a1cd2 (patch)
treecf8f400d221756add6c6a61ef001130812f57313 /src/ui/controls/border_delegate.cpp
parent20dc75e2ce6a9c38dd1888fdbf793fd8a3bc9cd3 (diff)
downloadcru-88765aab936724cb01fa2ffd86d65181182a1cd2.tar.gz
cru-88765aab936724cb01fa2ffd86d65181182a1cd2.tar.bz2
cru-88765aab936724cb01fa2ffd86d65181182a1cd2.zip
Create border delegate.
Diffstat (limited to 'src/ui/controls/border_delegate.cpp')
-rw-r--r--src/ui/controls/border_delegate.cpp85
1 files changed, 85 insertions, 0 deletions
diff --git a/src/ui/controls/border_delegate.cpp b/src/ui/controls/border_delegate.cpp
new file mode 100644
index 00000000..4e841a8c
--- /dev/null
+++ b/src/ui/controls/border_delegate.cpp
@@ -0,0 +1,85 @@
+#include "border_delegate.h"
+
+namespace cru::ui::controls
+{
+ void BorderProperty::SetBrush(Microsoft::WRL::ComPtr<ID2D1Brush> brush)
+ {
+ brush_ = std::move(brush);
+ RaisePropertyChangedEvent(brush_property_name);
+ }
+
+ void BorderProperty::SetWidth(const float width)
+ {
+ width_ = width;
+ RaisePropertyChangedEvent(width_property_name);
+ }
+
+ void BorderProperty::SetStrokeStyle(Microsoft::WRL::ComPtr<ID2D1StrokeStyle> stroke_style)
+ {
+ stroke_style_ = std::move(stroke_style);
+ RaisePropertyChangedEvent(stroke_style_property_name);
+ }
+
+ void BorderProperty::SetRadiusX(const float radius_x)
+ {
+ radius_x_ = radius_x;
+ RaisePropertyChangedEvent(radius_x_property_name);
+ }
+
+ void BorderProperty::SetRadiusY(const float radius_y)
+ {
+ radius_y_ = radius_y;
+ RaisePropertyChangedEvent(radius_y_property_name);
+ }
+
+ BorderDelegate::BorderDelegate(Control* control)
+ : BorderDelegate(control, std::make_shared<BorderProperty>())
+ {
+
+ }
+
+ BorderDelegate::BorderDelegate(Control* control, std::shared_ptr<BorderProperty> border_property)\
+ : control_(control),
+ border_property_changed_listener_(CreateFunctionPtr<void(String)>([this](String property_name)
+ {
+ if (property_name == BorderProperty::width_property_name)
+ control_->InvalidateLayout();
+ control_->Repaint();
+ }))
+ {
+ border_property_ = std::move(border_property);
+ border_property_->AddPropertyChangedListener(border_property_changed_listener_);
+ }
+
+ void BorderDelegate::SetBorderProperty(std::shared_ptr<BorderProperty> border_property)
+ {
+ border_property_->RemovePropertyChangedListener(border_property_changed_listener_);
+ border_property_ = std::move(border_property);
+ border_property_->AddPropertyChangedListener(border_property_changed_listener_);
+ }
+
+ void BorderDelegate::Draw(ID2D1DeviceContext* device_context, const Size& size) const
+ {
+ device_context->DrawRoundedRectangle(
+ D2D1::RoundedRect(D2D1::RectF(0.0f, 0.0f, size.width, size.height),
+ border_property_->GetRadiusX(),
+ border_property_->GetRadiusY()
+ ),
+ border_property_->GetBrush().Get(),
+ border_property_->GetWidth(),
+ border_property_->GetStrokeStyle().Get()
+ );
+ }
+
+ Size BorderDelegate::GetBorderSize() const
+ {
+ const auto width = border_property_->GetWidth();
+ return Size(width * 2, width * 2);
+ }
+
+ Rect BorderDelegate::CoerceLayoutRect(const Rect& rect) const
+ {
+ const auto width = border_property_->GetWidth();
+ return Rect(rect.left + width, rect.top + width, rect.width - width * 2, rect.height - width * 2);
+ }
+}