Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent hotkey behaviour for select tools #144

Merged
merged 7 commits into from
Sep 11, 2020
73 changes: 57 additions & 16 deletions Pinta.Core/Managers/ToolManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,18 @@ public class ToolManager : IEnumerable<BaseTool>
int prev_index = -1;

private List<BaseTool> Tools;
private Dictionary<Gdk.Key, List<BaseTool>> groupedTools;
private Gdk.Key LastUsedKey;
private int PressedShortcutCounter;

public event EventHandler<ToolEventArgs> ToolAdded;
public event EventHandler<ToolEventArgs> ToolRemoved;

public ToolManager ()
{
Tools = new List<BaseTool> ();
groupedTools = new Dictionary<Gdk.Key, List<BaseTool>>();
PressedShortcutCounter = 0;
}

public void AddTool (BaseTool tool)
Expand All @@ -58,6 +63,11 @@ public void AddTool (BaseTool tool)

if (CurrentTool == null)
SetCurrentTool (tool);

if (!groupedTools.ContainsKey(tool.ShortcutKey))
groupedTools.Add(tool.ShortcutKey, new List<BaseTool>());

groupedTools[tool.ShortcutKey].Add(tool);
}

public void RemoveInstanceOfTool (System.Type tool_type)
Expand All @@ -73,11 +83,17 @@ public void RemoveInstanceOfTool (System.Type tool_type)

SetCurrentTool(new DummyTool());
OnToolRemoved (tool);

if (groupedTools[tool.ShortcutKey].Count > 1)
groupedTools[tool.ShortcutKey].Remove(tool);
else
groupedTools.Remove(tool.ShortcutKey);

return;
}
}
}

void HandlePbToolItemClicked (object sender, EventArgs e)
{
ToggleToolButton tb = (ToggleToolButton)sender;
Expand Down Expand Up @@ -172,24 +188,32 @@ public void SetCurrentTool (Gdk.Key shortcut)
if (tool != null)
SetCurrentTool(tool);
}

private BaseTool FindNextTool (Gdk.Key shortcut)
{
string key = shortcut.ToString ().ToUpperInvariant ();

// Begin looking at the tool after the current one
for (int i = index + 1; i < Tools.Count; i++) {
if (Tools[i].ShortcutKey.ToString ().ToUpperInvariant () == key)
return Tools[i];
}

// Begin at the beginning and look up to the current tool
for (int i = 0; i < index; i++) {
if (Tools[i].ShortcutKey.ToString ().ToUpperInvariant () == key)
return Tools[i];
shortcut = shortcut.ToUpper();

if (groupedTools.ContainsKey(shortcut))
{
for (int i = 0; i < groupedTools[shortcut].Count; i++)
{
if (LastUsedKey != shortcut)
{
// Return first tool in group.
PressedShortcutCounter = (1 % groupedTools[shortcut].Count);
LastUsedKey = shortcut;
return groupedTools[shortcut][0];
}
else if(i == PressedShortcutCounter)
{
var tool = groupedTools[shortcut][PressedShortcutCounter];
PressedShortcutCounter = (i + 1) % groupedTools[shortcut].Count;
return tool;
}
}
}
return null;

return null;
}

private void OnToolAdded (BaseTool tool)
Expand Down Expand Up @@ -242,4 +266,21 @@ protected override void OnBuildToolBar (Toolbar tb)
tool_sep = null;
}
}

//Key extensions for more convenient usage of Gdk key enumerator
public static class KeyExtensions
{
public static Gdk.Key ToUpper(this Gdk.Key k1)
{
try
{
return (Gdk.Key)Enum.Parse(typeof(Gdk.Key), k1.ToString().ToUpperInvariant());
}
catch (ArgumentException)
{
//there is a need to catch argument exception because some buttons have not its UpperCase variant
return k1;
}
}
}
}
3 changes: 2 additions & 1 deletion Pinta.Tools/Tools/LassoSelectTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public LassoSelectTool ()
public override string Name { get { return Catalog.GetString ("Lasso Select"); } }
public override string Icon { get { return "Tools.LassoSelect.png"; } }
public override string StatusBarText { get { return Catalog.GetString ("Click and drag to draw the outline for a selection area."); } }
public override Gdk.Cursor DefaultCursor { get { return new Gdk.Cursor (Gdk.Display.Default, PintaCore.Resources.GetIcon ("Cursor.LassoSelect.png"), 9, 18); } }
public override Gdk.Key ShortcutKey { get { return Gdk.Key.S; } }
public override Gdk.Cursor DefaultCursor { get { return new Gdk.Cursor (Gdk.Display.Default, PintaCore.Resources.GetIcon ("Cursor.LassoSelect.png"), 9, 18); } }
public override int Priority { get { return 9; } }
#endregion

Expand Down