-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResizer.cpp
More file actions
194 lines (165 loc) · 6.34 KB
/
Resizer.cpp
File metadata and controls
194 lines (165 loc) · 6.34 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "pch.h"
#include "Resizer.h"
#include "Resizer.g.cpp"
using namespace winrt;
using namespace Microsoft::UI::Xaml;
using namespace Microsoft::UI::Xaml::Controls;
using namespace Microsoft::UI::Xaml::Input;
using namespace Windows::Foundation;
namespace winrt::VisualWinUI3::implementation
{
// Static dependency property definitions
DependencyProperty Resizer::m_valueProperty =
DependencyProperty::Register(
L"Value",
xaml_typename<double>(),
xaml_typename<VisualWinUI3::Resizer>(),
PropertyMetadata{ box_value(0.0), PropertyChangedCallback{ &Resizer::OnValuePropertyChanged } }
);
DependencyProperty Resizer::m_minimumProperty =
DependencyProperty::Register(
L"Minimum",
xaml_typename<double>(),
xaml_typename<VisualWinUI3::Resizer>(),
PropertyMetadata{ box_value(0.0) }
);
DependencyProperty Resizer::m_maximumProperty =
DependencyProperty::Register(
L"Maximum",
xaml_typename<double>(),
xaml_typename<VisualWinUI3::Resizer>(),
PropertyMetadata{ box_value(1000.0) }
);
DependencyProperty Resizer::m_orientationProperty =
DependencyProperty::Register(
L"Orientation",
xaml_typename<VisualWinUI3::SplitOrientation>(),
xaml_typename<VisualWinUI3::Resizer>(),
PropertyMetadata{ box_value(VisualWinUI3::SplitOrientation::Vertical) }
);
Resizer::Resizer()
{
DefaultStyleKey(winrt::box_value(L"VisualWinUI3.Resizer"));
}
void Resizer::OnApplyTemplate()
{
__super::OnApplyTemplate();
// Set cursor to indicate draggable behavior
if (Orientation() == VisualWinUI3::SplitOrientation::Horizontal)
ProtectedCursor(winrt::Microsoft::UI::Input::InputSystemCursor::Create(winrt::Microsoft::UI::Input::InputSystemCursorShape::SizeNorthSouth));
else
ProtectedCursor(winrt::Microsoft::UI::Input::InputSystemCursor::Create(winrt::Microsoft::UI::Input::InputSystemCursorShape::SizeWestEast));
}
VisualWinUI3::SplitOrientation Resizer::Orientation() const
{
return winrt::unbox_value<VisualWinUI3::SplitOrientation>(GetValue(m_orientationProperty));
}
void Resizer::Orientation(VisualWinUI3::SplitOrientation const& value)
{
SetValue(m_orientationProperty, box_value(value));
}
DependencyProperty Resizer::OrientationProperty() { return m_orientationProperty; }
void Resizer::OnPointerPressed(winrt::Microsoft::UI::Xaml::Input::PointerRoutedEventArgs const& e)
{
__super::OnPointerPressed(e);
if (e.Pointer().PointerDeviceType() == Microsoft::UI::Input::PointerDeviceType::Mouse)
{
m_isDragging = true;
m_lastPointerPosition = e.GetCurrentPoint(nullptr).Position();
m_dragStartValue = Value();
bool success = CapturePointer(e.Pointer());
if (!success)
{
DebugBreak();
}
e.Handled(true);
}
}
void Resizer::OnPointerMoved(winrt::Microsoft::UI::Xaml::Input::PointerRoutedEventArgs const& e)
{
__super::OnPointerMoved(e);
if (Orientation() == VisualWinUI3::SplitOrientation::Horizontal)
{
if (m_isDragging)
{
auto currentPosition = e.GetCurrentPoint(nullptr).Position();
auto diff = currentPosition.Y - m_lastPointerPosition.Y;
m_dragStartValue += diff;
// clamp it to min/max
if (m_dragStartValue < Minimum())
{
auto diff2 = Minimum() - m_dragStartValue;
currentPosition.Y += (float)diff2;
m_dragStartValue = Minimum();
}
else if (m_dragStartValue > Maximum())
{
auto diff2 = m_dragStartValue - Maximum();
currentPosition.Y -= (float)diff2;
m_dragStartValue = Maximum();
}
Value(m_dragStartValue);
m_lastPointerPosition = currentPosition;
e.Handled(true);
}
}
else
{
if (m_isDragging)
{
auto currentPosition = e.GetCurrentPoint(nullptr).Position();
auto diff = currentPosition.X - m_lastPointerPosition.X;
m_dragStartValue += diff;
// clamp it to min/max
if (m_dragStartValue < Minimum())
{
auto diff2 = Minimum() - m_dragStartValue;
currentPosition.X += (float)diff2;
m_dragStartValue = Minimum();
}
else if (m_dragStartValue > Maximum())
{
auto diff2 = m_dragStartValue - Maximum();
currentPosition.X -= (float)diff2;
m_dragStartValue = Maximum();
}
Value(m_dragStartValue);
m_lastPointerPosition = currentPosition;
e.Handled(true);
}
}
}
void Resizer::OnPointerReleased(winrt::Microsoft::UI::Xaml::Input::PointerRoutedEventArgs const& e)
{
__super::OnPointerReleased(e);
if (m_isDragging)
{
m_isDragging = false;
ReleasePointerCapture(e.Pointer());
e.Handled(true);
}
}
void Resizer::OnPointerCaptureLost(winrt::Microsoft::UI::Xaml::Input::PointerRoutedEventArgs const& e)
{
__super::OnPointerCaptureLost(e);
m_isDragging = false;
}
void Resizer::OnValuePropertyChanged(DependencyObject const& d, DependencyPropertyChangedEventArgs const& )
{
if (auto control = d.try_as<VisualWinUI3::Resizer>())
{
if (auto impl = winrt::get_self<implementation::Resizer>(control))
{
impl->m_valueChangedEvent(control, nullptr);
}
}
}
winrt::event_token Resizer::ValueChanged(TypedEventHandler<VisualWinUI3::Resizer, IInspectable> const& handler)
{
return m_valueChangedEvent.add(handler);
}
void Resizer::ValueChanged(winrt::event_token const& token) noexcept
{
m_valueChangedEvent.remove(token);
}
}