From 7fdc1d75ff121881fa0844ad4db51bbaab5065a4 Mon Sep 17 00:00:00 2001 From: Anna Date: Mon, 30 May 2022 14:58:47 -0400 Subject: [PATCH] feat: add autotranslate qol features Allow using Ctrl+# to select the first 10 results. Allow pressing Enter to select the first result. Fix the Escape key not closing the autotranslate window. Increase the window size. --- ChatTwo/Resources/Language.Designer.cs | 6 ++++ ChatTwo/Resources/Language.resx | 3 ++ ChatTwo/Ui/ChatLog.cs | 42 ++++++++++++++++++++++++-- 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/ChatTwo/Resources/Language.Designer.cs b/ChatTwo/Resources/Language.Designer.cs index 3e6fca3..345c480 100755 --- a/ChatTwo/Resources/Language.Designer.cs +++ b/ChatTwo/Resources/Language.Designer.cs @@ -812,5 +812,11 @@ namespace ChatTwo.Resources { return ResourceManager.GetString("Options_SortAutoTranslate_Description", resourceCulture); } } + + internal static string AutoTranslate_Completion_Key { + get { + return ResourceManager.GetString("AutoTranslate_Completion_Key", resourceCulture); + } + } } } diff --git a/ChatTwo/Resources/Language.resx b/ChatTwo/Resources/Language.resx index f44f61f..5c1cf0c 100755 --- a/ChatTwo/Resources/Language.resx +++ b/ChatTwo/Resources/Language.resx @@ -506,4 +506,7 @@ If this is enabled, the Auto Translate list will be sorted alphabetically. + + Ctrl + {0} + diff --git a/ChatTwo/Ui/ChatLog.cs b/ChatTwo/Ui/ChatLog.cs index d1fe6ef..ae32b52 100755 --- a/ChatTwo/Ui/ChatLog.cs +++ b/ChatTwo/Ui/ChatLog.cs @@ -962,7 +962,7 @@ internal sealed class ChatLog : IUiComponent { this._autoCompleteOpen = false; } - ImGui.SetNextWindowSize(new Vector2(350, 250) * ImGuiHelpers.GlobalScale); + ImGui.SetNextWindowSize(new Vector2(400, 300) * ImGuiHelpers.GlobalScale); if (!ImGui.BeginPopup(AutoCompleteId)) { if (this._activatePos == -1) { this._activatePos = this._autoCompleteInfo.EndPos; @@ -979,6 +979,31 @@ internal sealed class ChatLog : IUiComponent { this._autoCompleteList = AutoTranslate.Matching(this.Ui.Plugin.DataManager, this._autoCompleteInfo.ToComplete, this.Ui.Plugin.Config.SortAutoTranslate); } + var selected = -1; + if (ImGui.IsItemActive() && ImGui.GetIO().KeyCtrl) { + for (var i = 0; i < 10 && i < this._autoCompleteList.Count; i++) { + var num = (i + 1) % 10; + var key = (int) VirtualKey.KEY_0 + num; + var key2 = (int) VirtualKey.NUMPAD0 + num; + if (ImGui.IsKeyDown(key) || ImGui.IsKeyDown(key2)) { + selected = i; + } + } + } + + if (ImGui.IsItemDeactivated()) { + if (ImGui.IsKeyDown(ImGui.GetKeyIndex(ImGuiKey.Escape))) { + ImGui.CloseCurrentPopup(); + goto End; + } + + var enter = ImGui.IsKeyDown(ImGui.GetKeyIndex(ImGuiKey.Enter)) + || ImGui.IsKeyDown(ImGui.GetKeyIndex(ImGuiKey.KeyPadEnter)); + if (this._autoCompleteList.Count > 0 && enter) { + selected = 0; + } + } + if (ImGui.IsWindowAppearing()) { this._fixCursor = true; ImGui.SetKeyboardFocusHere(); @@ -992,7 +1017,19 @@ internal sealed class ChatLog : IUiComponent { for (var i = clipper.DisplayStart; i < clipper.DisplayEnd; i++) { var entry = this._autoCompleteList[i]; - if (!ImGui.Selectable($"{entry.String}##{entry.Group}/{entry.Row}")) { + var clicked = ImGui.Selectable($"{entry.String}##{entry.Group}/{entry.Row}") || selected == i; + + if (i < 10) { + var button = (i + 1) % 10; + var text = string.Format(Language.AutoTranslate_Completion_Key, button); + var size = ImGui.CalcTextSize(text); + ImGui.SameLine(ImGui.GetContentRegionAvail().X - size.X); + ImGui.PushStyleColor(ImGuiCol.Text, *ImGui.GetStyleColorVec4(ImGuiCol.TextDisabled)); + ImGui.TextUnformatted(text); + ImGui.PopStyleColor(); + } + + if (!clicked) { continue; } @@ -1009,6 +1046,7 @@ internal sealed class ChatLog : IUiComponent { ImGui.EndChild(); } + End: ImGui.EndPopup(); }