Skip to content

Commit

Permalink
support hotkeys for toolbar button
Browse files Browse the repository at this point in the history
  • Loading branch information
d2phap committed Nov 30, 2024
1 parent 76418f1 commit 962a0ae
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 36 deletions.
7 changes: 7 additions & 0 deletions Source/Components/ImageGlass.Base/Types/ToolbarItemModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
using ImageGlass.Base.Actions;
using System.Text.Json.Serialization;

namespace ImageGlass.Base;

Expand All @@ -37,6 +38,12 @@ public record ToolbarItemModel

public string Image { get; set; } = string.Empty;
public SingleAction OnClick { get; set; } = new();

/// <summary>
/// Gets, sets hotkeys.
/// </summary>
[JsonConverter(typeof(HotkeyListJsonConverter))]
public List<Hotkey> Hotkeys { get; set; } = [];
}

public enum ToolbarItemModelType
Expand Down
52 changes: 44 additions & 8 deletions Source/Components/ImageGlass.Settings/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,21 @@ public static void Load(IConfigurationRoot? items = null)
// toolbar buttons
var toolbarItems = items.GetSection(nameof(ToolbarButtons))
.GetChildren()
.Select(i => i.Get<ToolbarItemModel>());
.Select(i =>
{
var item = i.Get<ToolbarItemModel>();
var hotkeysArr = i.GetChildren()
.FirstOrDefault(i => i.Key == nameof(ToolbarItemModel.Hotkeys))
?.Get<string[]>() ?? [];

item.Hotkeys = hotkeysArr.Distinct()
.Where(i => !string.IsNullOrEmpty(i))
.Select(i => new Hotkey(i))
.ToList();

return item;
})
.Where(i => i != null);
ToolbarButtons = toolbarItems.ToList();


Expand Down Expand Up @@ -1205,14 +1219,18 @@ public static ExpandoObject PrepareJsonSettingsObject(bool useAbsoluteFileUrl =
/// </summary>
public static List<ExpandoObject> ConvertToolbarButtonsToExpandoObjList(IEnumerable<ToolbarItemModel> list)
{
var items = list.Select(i =>
var items = list.Select(btn =>
{
var obj = i.ToExpandoObject();
var obj = btn.ToExpandoObject();

// set hotkeys
obj.Set(nameof(ToolbarItemModel.Hotkeys), btn.Hotkeys.Select(hk => hk.ToString()));

if (string.IsNullOrWhiteSpace(i.Image)) return obj;
// set image url
if (string.IsNullOrWhiteSpace(btn.Image)) return obj;

var filePath = Theme.GetToolbarIconFilePath(i.Image);
if (string.IsNullOrWhiteSpace(filePath)) filePath = i.Image;
var filePath = Theme.GetToolbarIconFilePath(btn.Image);
if (string.IsNullOrWhiteSpace(filePath)) filePath = btn.Image;

try
{
Expand Down Expand Up @@ -1710,10 +1728,9 @@ public static Dictionary<string, List<Hotkey>> GetAllHotkeys(Dictionary<string,
}


// merge tool hotkeys
var toolHotkeys = Config.Tools
.ToDictionary(i => i.ToolId, i => i.Hotkeys);

// merge tool hotkeys
foreach (var item in toolHotkeys)
{
if (result.ContainsKey(item.Key))
Expand All @@ -1726,6 +1743,25 @@ public static Dictionary<string, List<Hotkey>> GetAllHotkeys(Dictionary<string,
}
}


// merge toolbar hotkeys
var toolbarHotkeys = Config.ToolbarButtons
.Where(i => i.Type == ToolbarItemModelType.Button && i.Hotkeys.Count > 0)
.ToDictionary(i => i.Id, i => i.Hotkeys);

foreach (var item in toolbarHotkeys)
{
if (result.ContainsKey(item.Key))
{
result[item.Key] = item.Value;
}
else
{
result.Add(item.Key, item.Value);
}
}


return result;
}

Expand Down
31 changes: 27 additions & 4 deletions Source/ImageGlass/FrmMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,12 @@ private void Application_ApplicationExit(object? sender, EventArgs e)
private void FrmMain_KeyDown(object sender, KeyEventArgs e)
{
//Text = new Hotkey(e.KeyData).ToString() + " - " + e.KeyValue.ToString();

var hotkey = new Hotkey(e.KeyData);


// 1. check if the hotkey action is special action
#region Special actions

var actions = Config.GetHotkeyActions(CurrentMenuHotkeys, hotkey);

// open main menu
Expand All @@ -159,9 +163,11 @@ private void FrmMain_KeyDown(object sender, KeyEventArgs e)
return;
}

#endregion // Special actions

// Register and run MAIN MENU shortcuts
#region Register and run MAIN MENU shortcuts

// 2. check hotkey if it's from menu items
#region Menu Hotkey

bool CheckMenuShortcut(ToolStripMenuItem mnu)
{
Expand Down Expand Up @@ -199,7 +205,24 @@ bool CheckMenuShortcut(ToolStripMenuItem mnu)
{
if (CheckMenuShortcut(item)) return;
}
#endregion
#endregion // Menu Hotkey


// 3. check hotkey if it's from toolbar buttons
#region Toolbar Hotkey

var toolbarBtn = Config.ToolbarButtons.Find(btn =>
{
var btnHotkey = btn.Hotkeys.SingleOrDefault(k => k.KeyData == e.KeyData);
return btnHotkey != null;
});

if (Toolbar.Items.ContainsKey(toolbarBtn?.Id))
{
Toolbar.Items[toolbarBtn.Id].PerformClick();
}

#endregion // Toolbar Hotkey
}


Expand Down
55 changes: 31 additions & 24 deletions Source/ImageGlass/FrmMain/FrmMain.Configs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -876,37 +876,44 @@ private void LoadMenuDisabledState()
/// </summary>
public void LoadToolbarItemsText(ModernToolbar modernToolbar)
{
Parallel.For(0, modernToolbar.Items.Count, (i) =>
foreach (var tItem in modernToolbar.Items.OfType<ToolStripButton>())
{
if (modernToolbar.Items[i] is ToolStripButton tItem)
if (tItem.Tag is ToolbarItemTagModel tagModel)
{
if (tItem.Tag is ToolbarItemTagModel tagModel)
// get the custom hotkeys
var hotkeyList = Config.ToolbarButtons.Find(btn => btn.Id == tItem.Name)
?.Hotkeys
.Select(hk => hk.ToString()) ?? [];
var customHotkey = ZString.Join(", ", hotkeyList);
var hotkeyText = string.IsNullOrEmpty(customHotkey) ? null : customHotkey;
string langKey;

if (tItem.Name == Toolbar.MainMenuButton.Name)
{
string langKey;
string hotkey;
if (tItem.Name == Toolbar.MainMenuButton.Name)
{
langKey = $"{Name}.MnuMain";
hotkey = Config.GetHotkeyString(CurrentMenuHotkeys, nameof(MnuMain));
}
else
{
langKey = $"{Name}.{tagModel.OnClick.Executable}";
hotkey = Config.GetHotkeyString(CurrentMenuHotkeys, tagModel.OnClick.Executable);
}
langKey = $"{nameof(FrmMain)}.{nameof(MnuMain)}";
hotkeyText ??= Config.GetHotkeyString(CurrentMenuHotkeys, nameof(MnuMain));
}
else
{
langKey = $"{nameof(FrmMain)}.{tagModel.OnClick.Executable}";
hotkeyText ??= Config.GetHotkeyString(CurrentMenuHotkeys, tagModel.OnClick.Executable);
}

if (Config.Language.TryGetValue(langKey, out var value))
{
tItem.Text = tItem.ToolTipText = value;

if (!string.IsNullOrEmpty(hotkey))
{
tItem.ToolTipText += $" ({hotkey})";
}
}
// set text
if (Config.Language.TryGetValue(langKey, out var value))
{
tItem.Text = tItem.ToolTipText = value;
}

// set hotkey tooltip
if (!string.IsNullOrEmpty(hotkeyText))
{
tItem.ToolTipText = $"{tItem.Text} ({hotkeyText})";
}
}
});
}

}


Expand Down

0 comments on commit 962a0ae

Please sign in to comment.