Keep center of visible area static on zoom when possible

This commit is contained in:
Killy 2020-04-28 21:43:01 +03:00
commit e6e2ed523a
2 changed files with 24 additions and 3 deletions

View file

@ -1890,7 +1890,7 @@ namespace Greenshot.Drawing
/// <summary>
/// Get the rectangle bounding the part of this Surface currently visible in the editor (in surface coordinate space).
/// </summary>
private Rectangle GetVisibleRectangle()
public Rectangle GetVisibleRectangle()
{
var bounds = Bounds;
var clientArea = Parent.ClientRectangle;

View file

@ -1572,8 +1572,24 @@ namespace Greenshot {
}
private void ZoomSetValue(int value) {
var surface = Surface as Surface;
var panel = surface?.Parent as Panel;
if (panel == null)
{
return;
}
// Store old scroll position
var rc = surface.GetVisibleRectangle();
var size = surface.Size;
var horizontalCenter = 1.0 * (rc.Left + rc.Width / 2) / size.Width;
var verticalCenter = 1.0 * (rc.Top + rc.Height / 2) / size.Height;
// Set the new zoom value
_zoomValue = value;
Surface.ZoomFactor = 1f * value / 100;
Size = GetOptimalWindowSize();
AlignCanvasPositionAfterResize();
// Update zoom controls
string valueString = value.ToString();
@ -1584,8 +1600,13 @@ namespace Greenshot {
}
}
Size = GetOptimalWindowSize();
AlignCanvasPositionAfterResize();
// Restore scroll position
rc = surface.GetVisibleRectangle();
size = surface.Size;
panel.AutoScrollPosition = new Point(
(int)(horizontalCenter * size.Width) - rc.Width / 2,
(int)(verticalCenter * size.Height) - rc.Height / 2
);
}
}
}