Skip to content

Commit

Permalink
Merge pull request #81 from cwensley/add-zoom-options
Browse files Browse the repository at this point in the history
Add more zoom levels and zoom in/out/reset commands
  • Loading branch information
cwensley authored Feb 26, 2023
2 parents 8ff7364 + 7153f7e commit 7c6529f
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 41 deletions.
15 changes: 10 additions & 5 deletions Source/Pablo/Actions/AllowGrow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

namespace Pablo.Actions
{
public class AllowGrow : CheckCommand
public class AllowGrow : CheckCommand, IUpdatableCommand
{
public const string ActionID = "zoomAllowGrow";

public ViewerPane ViewerPane { get; private set; }
public AllowGrow (ViewerPane viewerPane)

public AllowGrow(ViewerPane viewerPane)
{
this.ViewerPane = viewerPane;
this.ID = ActionID;
Expand All @@ -22,7 +22,12 @@ protected override void OnExecuted(EventArgs e)
{
base.OnExecuted(e);
ViewerPane.ZoomInfo.AllowGrow = this.Checked;
ViewerPane.UpdateSizes ();
ViewerPane.UpdateSizes();
}

public void UpdateState()
{
Checked = ViewerPane.ZoomInfo.AllowGrow;
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion Source/Pablo/Actions/FitHeight.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Pablo.Actions
{
public class FitHeight : CheckCommand
public class FitHeight : CheckCommand, IUpdatableCommand
{
public const string ActionID = "zoomFitHeight";

Expand All @@ -24,6 +24,11 @@ protected override void OnExecuted(EventArgs e)
ViewerPane.ZoomInfo.FitHeight = this.Checked;
ViewerPane.UpdateSizes ();
}

public void UpdateState()
{
Checked = ViewerPane.ZoomInfo.FitHeight;
}
}
}

7 changes: 6 additions & 1 deletion Source/Pablo/Actions/FitWidth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Pablo.Actions
{
public class FitWidth : CheckCommand
public class FitWidth : CheckCommand, IUpdatableCommand
{
public const string ActionID = "zoomFitWidth";

Expand All @@ -24,6 +24,11 @@ protected override void OnExecuted(EventArgs e)
ViewerPane.ZoomInfo.FitWidth = this.Checked;
ViewerPane.UpdateSizes ();
}

public void UpdateState()
{
Checked = ViewerPane.ZoomInfo.FitWidth;
}
}
}

35 changes: 35 additions & 0 deletions Source/Pablo/Actions/ZoomIn.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Linq;
using Eto.Forms;

namespace Pablo.Actions
{
public class ZoomIn : Command
{
public const string ActionID = "zoomIn";

public ViewerPane ViewerPane { get; private set; }

public ZoomIn(ViewerPane viewerPane)
{
this.ViewerPane = viewerPane;
this.ID = ActionID;
this.MenuText = "Zoom &In";
this.ToolTip = "Make things bigger";
this.Shortcut = Application.Instance.CommonModifier | Keys.Equal;
}

protected override void OnExecuted(EventArgs e)
{
base.OnExecuted(e);
var zoomInfo = ViewerPane.ZoomInfo;
var nextZoom = Pablo.ViewerPane.ZOOM_LEVELS.Where(r => r > zoomInfo.Zoom).OrderBy(r => r).FirstOrDefault();
if (nextZoom == 0)
nextZoom = ViewerPane.Zoom + 0.5f;
zoomInfo.Zoom = nextZoom;
ViewerPane.UpdateMenuItems();
ViewerPane.UpdateSizes();
}
}
}

33 changes: 33 additions & 0 deletions Source/Pablo/Actions/ZoomLevel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Linq;
using Eto.Forms;

namespace Pablo.Actions
{
public class ZoomLevel : RadioCommand, IUpdatableCommand
{
public ZoomLevel(RadioCommand controller, ViewerPane viewerPane, float zoomLevel)
{
ViewerPane = viewerPane;
Zoom = zoomLevel;
Controller = controller;
ID = "zoom" + zoomLevel;
MenuText = string.Format("{0}%", zoomLevel * 100);
}

public ViewerPane ViewerPane { get; set; }
public float Zoom { get; set; }
public void UpdateState()
{
Checked = Math.Abs(ViewerPane.ZoomInfo.Zoom - Zoom) < 0.0001f;
}

protected override void OnExecuted(EventArgs e)
{
base.OnExecuted(e);
ViewerPane.ZoomInfo.Zoom = Zoom;
ViewerPane.UpdateSizes();
}
}
}

38 changes: 38 additions & 0 deletions Source/Pablo/Actions/ZoomOut.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Linq;
using Eto.Forms;

namespace Pablo.Actions
{
public class ZoomOut : Command
{
public const string ActionID = "zoomOut";

public ViewerPane ViewerPane { get; private set; }

public ZoomOut(ViewerPane viewerPane)
{
this.ViewerPane = viewerPane;
this.ID = ActionID;
this.MenuText = "Zoom &Out";
this.ToolTip = "Make things smaller";
this.Shortcut = Application.Instance.CommonModifier | Keys.Minus;
}

protected override void OnExecuted(EventArgs e)
{
base.OnExecuted(e);
var zoomInfo = ViewerPane.ZoomInfo;
var nextZoom = Pablo.ViewerPane.ZOOM_LEVELS.Where(r => r < zoomInfo.Zoom).OrderByDescending(r => r).FirstOrDefault();
if (nextZoom == 0)
nextZoom = ViewerPane.Zoom * 0.5f;
if (nextZoom > 0.02)
{
zoomInfo.Zoom = nextZoom;
ViewerPane.UpdateMenuItems();
ViewerPane.UpdateSizes();
}
}
}
}

31 changes: 31 additions & 0 deletions Source/Pablo/Actions/ZoomReset.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Linq;
using Eto.Forms;

namespace Pablo.Actions
{
public class ZoomReset : Command
{
public const string ActionID = "zoomReset";

public ViewerPane ViewerPane { get; private set; }

public ZoomReset(ViewerPane viewerPane)
{
this.ViewerPane = viewerPane;
this.ID = ActionID;
this.MenuText = "Reset Zoom";
this.ToolTip = "Get back to normal";
this.Shortcut = Application.Instance.CommonModifier | Keys.D0;
}

protected override void OnExecuted(EventArgs e)
{
base.OnExecuted(e);
ViewerPane.ZoomInfo.Zoom = 1;
ViewerPane.UpdateMenuItems();
ViewerPane.UpdateSizes();
}
}
}

68 changes: 34 additions & 34 deletions Source/Pablo/ViewerPane.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,24 @@
using Pablo;
using System.Collections.Generic;
using System.Timers;
using System.Linq;

namespace Pablo
{
public interface IUpdatableCommand
{
void UpdateState();
}

public class ViewerPane : Scrollable, IViewer
{
const float MinZoom = 0.125f;
static readonly float[] ZOOM_LEVELS = { 2, 1.5F, 1, .75F, .50F, .25F, .125F };
public static readonly float[] ZOOM_LEVELS = { 7, 6.5F, 6, 5.5F, 5, 4.5F, 4, 3.5F, 3, 2, 1.5F, 1, .75F, .50F, .25F, .125F };

#region Members

readonly ImageViewer viewer;
CheckCommand actionZoomFitWidth;
CheckCommand actionZoomFitHeight;
CheckCommand actionAllowGrow;
readonly Dictionary<float, RadioCommand> zoomLevels = new Dictionary<float, RadioCommand>();
readonly List<IUpdatableCommand> updatableCommands = new List<IUpdatableCommand>();
readonly PixelLayout layout;
UITimer scrollTimer;
Point oldScrollPosition;
Expand Down Expand Up @@ -281,19 +284,11 @@ void StartAutoScroll()
scrollTimer.Start();
}

void UpdateUI()
internal void UpdateMenuItems()
{
if (actionAllowGrow != null)
actionAllowGrow.Checked = ZoomInfo.AllowGrow;
if (actionZoomFitWidth != null)
actionZoomFitWidth.Checked = ZoomInfo.FitWidth;
if (actionZoomFitHeight != null)
actionZoomFitHeight.Checked = ZoomInfo.FitHeight;
if (zoomLevels != null)
foreach (var item in updatableCommands)
{
RadioCommand raction;
if (zoomLevels.TryGetValue(ZoomInfo.Zoom, out raction))
raction.Checked = true;
item.UpdateState();
}
}

Expand All @@ -314,31 +309,36 @@ public void GenerateCommands(GenerateCommandArgs args)

var smZoom = smView.Items.GetSubmenu("&Zoom", 500);

smZoom.Items.Add(actionZoomFitWidth = new Actions.FitWidth(this), 500);
smZoom.Items.Add(actionZoomFitHeight = new Actions.FitHeight(this), 500);
smZoom.Items.Add(actionAllowGrow = new Actions.AllowGrow(this), 500);
updatableCommands.Clear();
CreateZoomMenu(smZoom);

UpdateMenuItems();
}


smZoom.Items.AddSeparator(500);
private void CreateZoomMenu(ButtonMenuItem menu)
{
menu.Items.Add(new Actions.ZoomIn(this));
menu.Items.Add(new Actions.ZoomOut(this));
menu.Items.Add(new Actions.ZoomReset(this));

menu.Items.AddSeparator();

RadioCommand controller = null;
zoomLevels.Clear();
Actions.ZoomLevel controller = null;
foreach (float zoomLevel in ZOOM_LEVELS)
{
var raction = new RadioCommand { Controller = controller, ID = "zoom" + zoomLevel, MenuText = string.Format("{0}%", zoomLevel * 100) };
raction.Executed += (sender, e) =>
{
var action = sender as RadioCommand;
ZoomInfo.Zoom = (float)action.Tag;
UpdateSizes();
};
var zoomLevelCommand = new Actions.ZoomLevel(controller, this, zoomLevel);
if (controller == null)
controller = raction;
raction.Tag = zoomLevel;
smZoom.Items.Add(raction);
zoomLevels.Add(zoomLevel, raction);
controller = zoomLevelCommand;
menu.Items.Add(zoomLevelCommand);
}
UpdateUI();
menu.Items.AddSeparator();

menu.Items.Add(new Actions.FitWidth(this));
menu.Items.Add(new Actions.FitHeight(this));
menu.Items.Add(new Actions.AllowGrow(this));

updatableCommands.AddRange(menu.Items.Select(r => r.Command).OfType<IUpdatableCommand>());
}

protected override void Dispose(bool disposing)
Expand Down

0 comments on commit 7c6529f

Please sign in to comment.