Skip to content

Commit

Permalink
feat: zoom functions
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewBabbitt97 committed Jul 25, 2024
1 parent 7d08bc8 commit 1e7b99c
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/BlazorDesktop/Wpf/BlazorDesktopWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public partial class BlazorDesktopWindow : Window
private readonly IConfiguration _config;
private readonly IWebHostEnvironment _environment;
private readonly UISettings _uiSettings;
private readonly double[] _zoomSizes =
[5, 4, 3, 2.5, 2, 1.75, 1.5, 1.25, 1.1, 1, 0.9, 0.8, 0.75, 0.66, 0.5, 0.33, 0.25];
private const string DragScript =
@"
window.addEventListener('DOMContentLoaded', () => {
Expand Down Expand Up @@ -120,6 +122,54 @@ public void ToggleFullScreen()
}
}

/// <summary>
/// Resets the zoom level.
/// </summary>
public void ResetZoom()
{
WebView.WebView.ZoomFactor = 1;
}

/// <summary>
/// Zooms in.
/// </summary>
public void ZoomIn()
{
var closest = _zoomSizes.Select(z => new { Zoom = z, Distance = Math.Abs(z - WebView.WebView.ZoomFactor) })
.OrderBy(p => p.Distance)
.Select(p => p.Zoom)
.First();

var closestIndex = Array.IndexOf(_zoomSizes, closest);

if (closestIndex > 0)
{
var newZoomSize = _zoomSizes[closestIndex - 1];

WebView.WebView.ZoomFactor = newZoomSize;
}
}

/// <summary>
/// Zooms in.
/// </summary>
public void ZoomOut()
{
var closest = _zoomSizes.Select(z => new { Zoom = z, Distance = Math.Abs(z - WebView.WebView.ZoomFactor) })
.OrderBy(p => p.Distance)
.Select(p => p.Zoom)
.First();

var closestIndex = Array.IndexOf(_zoomSizes, closest);

if (closestIndex < _zoomSizes.Length - 1)
{
var newZoomSize = _zoomSizes[closestIndex + 1];

WebView.WebView.ZoomFactor = newZoomSize;
}
}

private void InitializeWindow()
{
var height = _config.GetValue<int?>(WindowDefaults.Height) ?? 768;
Expand Down

0 comments on commit 1e7b99c

Please sign in to comment.