Skip to content

Commit 9ced497

Browse files
authored
Merge pull request #652 from unoplatform/dev/ICSI/651-Fix-ScrollBars
Fix ScrollBars behavior
2 parents a50600e + e311a61 commit 9ced497

File tree

2 files changed

+52
-53
lines changed

2 files changed

+52
-53
lines changed

UI/PhotoViewer/InteractionControls/ZoomContentControl.cs

Lines changed: 48 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ public partial class ZoomContentControl : ContentControl
6868
public static readonly DependencyProperty HorizontalMinScrollProperty =
6969
DependencyProperty.Register(nameof(HorizontalMinScroll), typeof(double), typeof(ZoomContentControl), new PropertyMetadata(0.0d));
7070

71+
public static readonly DependencyProperty HorizontalScrollValueProperty =
72+
DependencyProperty.Register(nameof(HorizontalScrollValue), typeof(double), typeof(ZoomContentControl), new PropertyMetadata(0.0d));
73+
74+
public static readonly DependencyProperty VerticalScrollValueProperty =
75+
DependencyProperty.Register(nameof(VerticalScrollValue), typeof(double), typeof(ZoomContentControl), new PropertyMetadata(0.0d));
76+
7177
public static readonly DependencyProperty ViewPortWidthProperty =
7278
DependencyProperty.Register(nameof(ViewPortWidth), typeof(double), typeof(ZoomContentControl), new PropertyMetadata(0.0d));
7379

@@ -166,6 +172,19 @@ public double HorizontalMinScroll
166172
get => (double)GetValue(HorizontalMinScrollProperty);
167173
set => SetValue(HorizontalMinScrollProperty, value);
168174
}
175+
176+
public double HorizontalScrollValue
177+
{
178+
get => (double)GetValue(HorizontalScrollValueProperty);
179+
set => SetValue(HorizontalScrollValueProperty, value);
180+
}
181+
182+
public double VerticalScrollValue
183+
{
184+
get => (double)GetValue(VerticalScrollValueProperty);
185+
set => SetValue(VerticalScrollValueProperty, value);
186+
}
187+
169188
public double ViewPortHeight
170189
{
171190
get => (double)GetValue(ViewPortHeightProperty);
@@ -194,20 +213,11 @@ private void RegisterPropertyHandlers()
194213
RegisterPropertyChangedCallback(IsActiveProperty, IsActiveChanged);
195214
}
196215

197-
private void UpdateVerticalScrollBarValue(DependencyObject sender, DependencyProperty dp)
198-
{
199-
if (_scrollV is not null)
200-
{
201-
_scrollV.Value = VerticalOffset;
202-
}
203-
}
204-
private void UpdateHorizontalScrollBarValue(DependencyObject sender, DependencyProperty dp)
205-
{
206-
if (_scrollH is not null)
207-
{
208-
_scrollH.Value = HorizontalOffset;
209-
}
210-
}
216+
//Slide move is always on the opposite direction of the drag
217+
private void UpdateVerticalScrollBarValue(DependencyObject sender, DependencyProperty dp) => VerticalScrollValue = -1 * VerticalOffset;
218+
219+
//Slide move is always on the opposite direction of the drag
220+
private void UpdateHorizontalScrollBarValue(DependencyObject sender, DependencyProperty dp) => HorizontalScrollValue = -1 * HorizontalOffset;
211221

212222
private void IsActiveChanged(DependencyObject sender, DependencyProperty dp)
213223
{
@@ -216,16 +226,23 @@ private void IsActiveChanged(DependencyObject sender, DependencyProperty dp)
216226
ResetOffset();
217227
ResetZoom();
218228
}
229+
if (_scrollH is not null)
230+
{
231+
_scrollH.Visibility = IsActive ? Visibility.Visible : Visibility.Collapsed;
232+
}
233+
if (_scrollV is not null)
234+
{
235+
_scrollV.Visibility = IsActive ? Visibility.Visible : Visibility.Collapsed;
236+
}
219237
}
220238

221239
private void UpdateScrollLimits()
222240
{
223-
//min and max are inverted to slide on the opposite direction of the drag
224-
HorizontalMinScroll = this.ActualWidth * ZoomLevel;
225-
VerticalMinScroll = this.ActualHeight * ZoomLevel;
241+
HorizontalMaxScroll = this.ActualWidth * ZoomLevel;
242+
VerticalMaxScroll = this.ActualHeight * ZoomLevel;
226243

227-
HorizontalMaxScroll = -1 * HorizontalMinScroll;
228-
VerticalMaxScroll = -1 * VerticalMinScroll;
244+
HorizontalMinScroll = -1 * HorizontalMaxScroll;
245+
VerticalMinScroll = -1 * VerticalMaxScroll;
229246
}
230247

231248
private void CoerceZoomLevel(DependencyObject sender, DependencyProperty dp)
@@ -261,11 +278,12 @@ protected override void OnApplyTemplate()
261278
_scrollH = GetTemplateChild("PART_scrollH") as ScrollBar;
262279

263280
RegisterToControlEvents();
281+
264282
ResetOffset();
265283
ResetZoom();
266284
RegisterPointerHandlers();
267285
}
268-
286+
#region ScrollBars Events
269287
private void RegisterToControlEvents()
270288
{
271289
//due to templatebinding there's no TwoWay mode. We need to manually update the values
@@ -282,31 +300,16 @@ private void RegisterToControlEvents()
282300

283301
private void _scrollV_Scroll(object sender, ScrollEventArgs e)
284302
{
285-
VerticalOffset = e.NewValue;
303+
//TemplateBinding doesn't support TwoWay mode. We need to manually update the values
304+
VerticalOffset = -1 * e.NewValue;
286305
}
287306

288307
private void _scrollH_Scroll(object sender, ScrollEventArgs e)
289308
{
290-
HorizontalOffset = e.NewValue;
309+
//TemplateBinding doesn't support TwoWay mode. We need to manually update the values
310+
HorizontalOffset = -1 * e.NewValue;
291311
}
292-
293-
//private void ApplyBindings()
294-
//{
295-
// if (_scrollV is not null)
296-
// {
297-
// var sVBinding = new Binding { Path = new PropertyPath("VerticalOffset"), Mode = BindingMode.TwoWay };
298-
// sVBinding.Source = this;
299-
// _scrollV.SetBinding(ScrollBar.ValueProperty, sVBinding);
300-
// }
301-
302-
// if (_scrollH is not null)
303-
// {
304-
// var sHBinding = new Binding { Path = new PropertyPath("HorizontalOffset"), Mode = BindingMode.TwoWay };
305-
// sHBinding.Source = this;
306-
// _scrollH.SetBinding(ScrollBar.ValueProperty, sHBinding);
307-
// }
308-
//}
309-
312+
#endregion
310313

311314
private uint _capturedPointerId;
312315
private Point _referencePosition;
@@ -438,20 +441,14 @@ private double GetPanDelta(PointerPointProperties pointerProperties)
438441

439442
public void ResetZoom()
440443
{
441-
if (IsAllowedToWork)
442-
{
443-
ZoomLevel = 1;
444-
HorizontalZoomCenter = 0;
445-
VerticalZoomCenter = 0;
446-
}
444+
ZoomLevel = 1;
445+
HorizontalZoomCenter = 0;
446+
VerticalZoomCenter = 0;
447447
}
448448

449449
public void ResetOffset()
450450
{
451-
if (IsAllowedToWork)
452-
{
453-
HorizontalOffset = 0;
454-
VerticalOffset = 0;
455-
}
451+
HorizontalOffset = 0;
452+
VerticalOffset = 0;
456453
}
457454
}

UI/PhotoViewer/InteractionControls/ZoomContentControl.xaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@
4848
Minimum="{TemplateBinding VerticalMinScroll}"
4949
Orientation="Vertical"
5050
SmallChange="1"
51-
ViewportSize="{TemplateBinding ViewPortHeight}" />
51+
ViewportSize="{TemplateBinding ViewPortHeight}"
52+
Value="{TemplateBinding VerticalScrollValue}" />
5253
<!-- Horizontal ScrollBar -->
5354
<ScrollBar
5455
x:Name="PART_scrollH"
@@ -62,7 +63,8 @@
6263
Minimum="{TemplateBinding HorizontalMinScroll}"
6364
Orientation="Horizontal"
6465
SmallChange="1"
65-
ViewportSize="{TemplateBinding ViewPortWidth}" />
66+
ViewportSize="{TemplateBinding ViewPortWidth}"
67+
Value="{TemplateBinding HorizontalScrollValue}" />
6668
</Grid>
6769
</ControlTemplate>
6870
</Setter.Value>

0 commit comments

Comments
 (0)