aboutsummaryrefslogtreecommitdiff
path: root/src/ui/controls/border_delegate.h
blob: 12c5444a118cd30f6147a04f708a1008345beec6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#pragma once

#include "ui/control.h"

namespace cru::ui::controls
{
    class BorderProperty : public PropertyChangedNotifyObject
    {
    public:
        constexpr static auto brush_property_name = L"Brush";
        constexpr static auto width_property_name = L"Width";
        constexpr static auto stroke_style_property_name = L"StrokeStyle";
        constexpr static auto radius_x_property_name = L"RadiusX";
        constexpr static auto radius_y_property_name = L"RadiusY";

        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<ID2D1Brush> GetBrush() const
        {
            return brush_;
        }

        float GetWidth() const
        {
            return width_;
        }

        Microsoft::WRL::ComPtr<ID2D1StrokeStyle> GetStrokeStyle() const
        {
            return stroke_style_;
        }

        float GetRadiusX() const
        {
            return radius_x_;
        }

        float GetRadiusY() const
        {
            return radius_y_;
        }

        void SetBrush(Microsoft::WRL::ComPtr<ID2D1Brush> brush);
        void SetWidth(float width);
        void SetStrokeStyle(Microsoft::WRL::ComPtr<ID2D1StrokeStyle> stroke_style);
        void SetRadiusX(float radius_x);
        void SetRadiusY(float radius_y);

    private:
        Microsoft::WRL::ComPtr<ID2D1Brush> brush_ = nullptr;
        float width_ = 1.0f;
        Microsoft::WRL::ComPtr<ID2D1StrokeStyle> stroke_style_ = nullptr;
        float radius_x_ = 0.0f;
        float radius_y_ = 0.0f;
    };


    // BorderDelegate is a delegate for border painting and layout.
    // It must bind a control and not change the binding.
    // But multiple BorderDelegate may share a common BorderProperty.
    class BorderDelegate : public Object
    {
    public:
        explicit BorderDelegate(Control* control);
        BorderDelegate(Control* control, std::shared_ptr<BorderProperty> border_property);
        BorderDelegate(const BorderDelegate& other) = delete;
        BorderDelegate(BorderDelegate&& other) = delete;
        BorderDelegate& operator=(const BorderDelegate& other) = delete;
        BorderDelegate& operator=(BorderDelegate&& other) = delete;
        ~BorderDelegate() override;

        std::shared_ptr<BorderProperty> GetBorderProperty() const
        {
            return border_property_;
        }

        void SetBorderProperty(std::shared_ptr<BorderProperty> border_property);

        void Draw(ID2D1DeviceContext* device_context, const Size& size) const;

        Size GetBorderSize() const;
        Rect CoerceLayoutRect(const Rect& rect) const;

    private:
        Control* control_;
        std::shared_ptr<BorderProperty> border_property_;
        FunctionPtr<void(String)> border_property_changed_listener_;
    };
}