Skip to content

Commit a05ae97

Browse files
authored
Merge pull request #655 from unoplatform/dev/ICSI/654-Fix-Zoom-Messing-Offset
Fix Zoom
2 parents abf8042 + 1fd20b9 commit a05ae97

File tree

2 files changed

+62
-39
lines changed

2 files changed

+62
-39
lines changed

UI/PhotoViewer/InteractionControls/ZoomContentControl.cs

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@
77

88
namespace InteractionControls;
99

10+
[TemplatePart(Name = "PART_Grid", Type = typeof(Grid))]
1011
[TemplatePart(Name = "PART_Presenter", Type = typeof(ContentPresenter))]
1112
[TemplatePart(Name = "PART_scrollV", Type = typeof(ScrollBar))]
1213
[TemplatePart(Name = "PART_scrollH", Type = typeof(ScrollBar))]
1314
public partial class ZoomContentControl : ContentControl
1415
{
16+
private Grid? _grid;
1517
private ContentPresenter? _presenter;
1618
private ScrollBar? _scrollV;
1719
private ScrollBar? _scrollH;
20+
private Canvas _canvas = new Canvas();
21+
1822
private bool IsAllowedToWork => (IsEnabled && IsActive && _presenter is not null);
1923

2024
public bool ResetWhenNotActive { get; set; } = true;
@@ -157,16 +161,19 @@ public double VerticalMaxScroll
157161
get => (double)GetValue(VerticalMaxScrollProperty);
158162
set => SetValue(VerticalMaxScrollProperty, value);
159163
}
164+
160165
public double VerticalMinScroll
161166
{
162167
get => (double)GetValue(VerticalMinScrollProperty);
163168
set => SetValue(VerticalMinScrollProperty, value);
164169
}
170+
165171
public double HorizontalMaxScroll
166172
{
167173
get => (double)GetValue(HorizontalMaxScrollProperty);
168174
set => SetValue(HorizontalMaxScrollProperty, value);
169175
}
176+
170177
public double HorizontalMinScroll
171178
{
172179
get => (double)GetValue(HorizontalMinScrollProperty);
@@ -190,6 +197,7 @@ public double ViewPortHeight
190197
get => (double)GetValue(ViewPortHeightProperty);
191198
set => SetValue(ViewPortHeightProperty, value);
192199
}
200+
193201
public double ViewPortWidth
194202
{
195203
get => (double)GetValue(ViewPortWidthProperty);
@@ -205,7 +213,7 @@ private void RegisterPropertyHandlers()
205213
RegisterPropertyChangedCallback(MinZoomLevelProperty, CoerceZoomLevel);
206214
RegisterPropertyChangedCallback(MaxZoomLevelProperty, CoerceZoomLevel);
207215

208-
RegisterPropertyChangedCallback(ZoomLevelProperty, (s, e) => { UpdateScrollLimits(); });
216+
RegisterPropertyChangedCallback(ZoomLevelProperty, (s, e) => UpdateScrollLimits());
209217

210218
RegisterPropertyChangedCallback(HorizontalOffsetProperty, UpdateVerticalScrollBarValue);
211219
RegisterPropertyChangedCallback(VerticalOffsetProperty, UpdateHorizontalScrollBarValue);
@@ -234,6 +242,26 @@ private void IsActiveChanged(DependencyObject sender, DependencyProperty dp)
234242
{
235243
_scrollV.Visibility = IsActive ? Visibility.Visible : Visibility.Collapsed;
236244
}
245+
246+
if (_grid is not null)
247+
{
248+
if (IsActive)
249+
{
250+
_grid.Children.Remove(_presenter);
251+
_grid.Children.Add(_canvas);
252+
_canvas.Children.Add(_presenter);
253+
Grid.SetRow(_canvas, 0);
254+
Grid.SetColumn(_canvas, 0);
255+
}
256+
else
257+
{
258+
_canvas.Children.Remove(_presenter);
259+
_grid.Children.Remove(_canvas);
260+
_grid.Children.Add(_presenter);
261+
Grid.SetRow(_presenter, 0);
262+
Grid.SetColumn(_presenter, 0);
263+
}
264+
}
237265
}
238266

239267
private void UpdateScrollLimits()
@@ -273,6 +301,7 @@ public ZoomContentControl()
273301

274302
protected override void OnApplyTemplate()
275303
{
304+
_grid = GetTemplateChild("PART_Grid") as Grid;
276305
_presenter = GetTemplateChild("PART_Presenter") as ContentPresenter;
277306
_scrollV = GetTemplateChild("PART_scrollV") as ScrollBar;
278307
_scrollH = GetTemplateChild("PART_scrollH") as ScrollBar;
@@ -284,31 +313,26 @@ protected override void OnApplyTemplate()
284313
RegisterPointerHandlers();
285314
}
286315
#region ScrollBars Events
316+
287317
private void RegisterToControlEvents()
288318
{
289319
//due to templatebinding there's no TwoWay mode. We need to manually update the values
290320
if (_scrollV is not null)
291321
{
292-
_scrollV.Scroll += _scrollV_Scroll;
322+
_scrollV.Scroll += ScrollV_Scroll;
293323
}
294324

295325
if (_scrollH is not null)
296326
{
297-
_scrollH.Scroll += _scrollH_Scroll;
327+
_scrollH.Scroll += ScrollH_Scroll;
298328
}
299329
}
300330

301-
private void _scrollV_Scroll(object sender, ScrollEventArgs e)
302-
{
303-
//TemplateBinding doesn't support TwoWay mode. We need to manually update the values
304-
VerticalOffset = -1 * e.NewValue;
305-
}
331+
//TemplateBinding doesn't support TwoWay mode. We need to manually update the values
332+
private void ScrollV_Scroll(object sender, ScrollEventArgs e) => VerticalOffset = -1 * e.NewValue;
333+
334+
private void ScrollH_Scroll(object sender, ScrollEventArgs e) => HorizontalOffset = -1 * e.NewValue;
306335

307-
private void _scrollH_Scroll(object sender, ScrollEventArgs e)
308-
{
309-
//TemplateBinding doesn't support TwoWay mode. We need to manually update the values
310-
HorizontalOffset = -1 * e.NewValue;
311-
}
312336
#endregion
313337

314338
private uint _capturedPointerId;
@@ -390,7 +414,7 @@ private void OnPointerWheelChanged(object sender, PointerRoutedEventArgs e)
390414
return; // Don't handle the event when the control is disabled.
391415
}
392416

393-
var pointerPoint = e.GetCurrentPoint(_presenter);
417+
var pointerPoint = e.GetCurrentPoint(this);
394418
var pointerProperties = pointerPoint.Properties;
395419
var pointerPosition = pointerPoint.Position;
396420

@@ -410,14 +434,7 @@ private void OnPointerWheelChanged(object sender, PointerRoutedEventArgs e)
410434
{
411435
e.Handled = true;
412436

413-
var hzc = HorizontalZoomCenter;
414-
var vzc = VerticalZoomCenter;
415-
var newPointerPosX = ((pointerPosition.X - hzc) * changeRatio) + hzc;
416-
var newPointerPosY = ((pointerPosition.Y - vzc) * changeRatio) + vzc;
417-
418437
ZoomLevel *= changeRatio;
419-
HorizontalOffset += newPointerPosX - pointerPosition.X;
420-
VerticalOffset += newPointerPosY - pointerPosition.Y;
421438
HorizontalZoomCenter = pointerPosition.X;
422439
VerticalZoomCenter = pointerPosition.Y;
423440
return;
@@ -451,4 +468,10 @@ public void ResetOffset()
451468
HorizontalOffset = 0;
452469
VerticalOffset = 0;
453470
}
471+
472+
public void Centralize()
473+
{
474+
HorizontalOffset = (ActualWidth / 2) - (ViewPortWidth / 2);
475+
VerticalOffset = (ActualHeight / 2) - (ViewPortHeight / 2);
476+
}
454477
}

UI/PhotoViewer/InteractionControls/ZoomContentControl.xaml

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<Setter Property="Template">
88
<Setter.Value>
99
<ControlTemplate TargetType="ctl:ZoomContentControl">
10-
<Grid>
10+
<Grid x:Name="PART_Grid">
1111
<Grid.RowDefinitions>
1212
<RowDefinition Height="*" />
1313
<RowDefinition Height="Auto" />
@@ -16,25 +16,23 @@
1616
<ColumnDefinition Width="*" />
1717
<ColumnDefinition Width="Auto" />
1818
</Grid.ColumnDefinitions>
19-
<Border
19+
<ContentPresenter
20+
x:Name="PART_Presenter"
2021
Grid.Row="0"
2122
Grid.Column="0"
22-
Background="{TemplateBinding Background}">
23-
<ContentPresenter
24-
x:Name="PART_Presenter"
25-
Margin="{TemplateBinding Padding}"
26-
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
27-
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
28-
Content="{TemplateBinding Content}"
29-
ContentTemplate="{TemplateBinding ContentTemplate}">
30-
<ContentPresenter.RenderTransform>
31-
<TransformGroup>
32-
<ScaleTransform CenterX="{TemplateBinding HorizontalZoomCenter}" CenterY="{TemplateBinding VerticalZoomCenter}" ScaleX="{TemplateBinding ZoomLevel}" ScaleY="{TemplateBinding ZoomLevel}" />
33-
<TranslateTransform X="{TemplateBinding HorizontalOffset}" Y="{TemplateBinding VerticalOffset}" />
34-
</TransformGroup>
35-
</ContentPresenter.RenderTransform>
36-
</ContentPresenter>
37-
</Border>
23+
Margin="0"
24+
Padding="0"
25+
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
26+
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
27+
Content="{TemplateBinding Content}"
28+
ContentTemplate="{TemplateBinding ContentTemplate}">
29+
<ContentPresenter.RenderTransform>
30+
<TransformGroup>
31+
<ScaleTransform CenterX="{TemplateBinding HorizontalZoomCenter}" CenterY="{TemplateBinding VerticalZoomCenter}" ScaleX="{TemplateBinding ZoomLevel}" ScaleY="{TemplateBinding ZoomLevel}" />
32+
<TranslateTransform X="{TemplateBinding HorizontalOffset}" Y="{TemplateBinding VerticalOffset}" />
33+
</TransformGroup>
34+
</ContentPresenter.RenderTransform>
35+
</ContentPresenter>
3836
<!-- Vertical ScrollBar -->
3937
<ScrollBar
4038
x:Name="PART_scrollV"
@@ -49,6 +47,7 @@
4947
Orientation="Vertical"
5048
SmallChange="1"
5149
ViewportSize="{TemplateBinding ViewPortHeight}"
50+
Visibility="{TemplateBinding IsActive}"
5251
Value="{TemplateBinding VerticalScrollValue}" />
5352
<!-- Horizontal ScrollBar -->
5453
<ScrollBar
@@ -64,6 +63,7 @@
6463
Orientation="Horizontal"
6564
SmallChange="1"
6665
ViewportSize="{TemplateBinding ViewPortWidth}"
66+
Visibility="{TemplateBinding IsActive}"
6767
Value="{TemplateBinding HorizontalScrollValue}" />
6868
</Grid>
6969
</ControlTemplate>

0 commit comments

Comments
 (0)