双开程序时仅拉起已有实例
Browse files- src/WpfEditor/App.xaml.cs +63 -0
- src/WpfEditor/EchoDict.csproj +1 -1
- src/WpfEditor/EchoDictWindow.xaml.cs +100 -67
src/WpfEditor/App.xaml.cs
CHANGED
|
@@ -1,13 +1,76 @@
|
|
|
|
|
| 1 |
using System.Configuration;
|
| 2 |
using System.Data;
|
|
|
|
|
|
|
|
|
|
| 3 |
using System.Windows;
|
| 4 |
|
| 5 |
namespace WpfEditor
|
| 6 |
{
|
| 7 |
public partial class App : Application
|
| 8 |
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
protected override void OnStartup(StartupEventArgs e)
|
| 10 |
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
base.OnStartup(e);
|
| 12 |
|
| 13 |
var win = Echodict.EchodictWindow.Instance; // 第一次实例化,后面启动就快了
|
|
|
|
| 1 |
+
using System;
|
| 2 |
using System.Configuration;
|
| 3 |
using System.Data;
|
| 4 |
+
using System.Diagnostics;
|
| 5 |
+
using System.Runtime.InteropServices;
|
| 6 |
+
using System.Threading;
|
| 7 |
using System.Windows;
|
| 8 |
|
| 9 |
namespace WpfEditor
|
| 10 |
{
|
| 11 |
public partial class App : Application
|
| 12 |
{
|
| 13 |
+
private static Mutex? _mutex;
|
| 14 |
+
|
| 15 |
+
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
|
| 16 |
+
private static extern uint RegisterWindowMessage(string lpString);
|
| 17 |
+
|
| 18 |
+
[DllImport("user32.dll", SetLastError = true)]
|
| 19 |
+
private static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
|
| 20 |
+
|
| 21 |
+
[DllImport("user32.dll")]
|
| 22 |
+
[return: MarshalAs(UnmanagedType.Bool)]
|
| 23 |
+
private static extern bool SetForegroundWindow(IntPtr hWnd);
|
| 24 |
+
|
| 25 |
+
[DllImport("user32.dll")]
|
| 26 |
+
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
|
| 27 |
+
|
| 28 |
+
[DllImport("user32.dll")]
|
| 29 |
+
[return: MarshalAs(UnmanagedType.Bool)]
|
| 30 |
+
private static extern bool IsIconic(IntPtr hWnd);
|
| 31 |
+
|
| 32 |
+
private const int SW_RESTORE = 9;
|
| 33 |
+
private const int HWND_BROADCAST = 0xffff;
|
| 34 |
+
private const string UniqueMessageName = "WM_SHOWME_EchoDict_WpfEditor_2025";
|
| 35 |
+
|
| 36 |
protected override void OnStartup(StartupEventArgs e)
|
| 37 |
{
|
| 38 |
+
const string mutexName = "Global\\EchoDict_WpfEditor_Mutex_App";
|
| 39 |
+
|
| 40 |
+
_mutex = new Mutex(true, mutexName, out bool createdNew);
|
| 41 |
+
|
| 42 |
+
if (!createdNew)
|
| 43 |
+
{
|
| 44 |
+
// Instance already running, broadcast message to show it
|
| 45 |
+
uint wmShowMe = RegisterWindowMessage(UniqueMessageName);
|
| 46 |
+
if (wmShowMe != 0)
|
| 47 |
+
{
|
| 48 |
+
PostMessage((IntPtr)HWND_BROADCAST, wmShowMe, IntPtr.Zero, IntPtr.Zero);
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
// Also try the standard way just in case
|
| 52 |
+
Process current = Process.GetCurrentProcess();
|
| 53 |
+
foreach (Process process in Process.GetProcessesByName(current.ProcessName))
|
| 54 |
+
{
|
| 55 |
+
if (process.Id != current.Id)
|
| 56 |
+
{
|
| 57 |
+
IntPtr hWnd = process.MainWindowHandle;
|
| 58 |
+
if (hWnd != IntPtr.Zero)
|
| 59 |
+
{
|
| 60 |
+
if (IsIconic(hWnd))
|
| 61 |
+
{
|
| 62 |
+
ShowWindow(hWnd, SW_RESTORE);
|
| 63 |
+
}
|
| 64 |
+
SetForegroundWindow(hWnd);
|
| 65 |
+
}
|
| 66 |
+
// Don't break here, broadcast might be needed if window handle is 0
|
| 67 |
+
}
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
Shutdown();
|
| 71 |
+
return;
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
base.OnStartup(e);
|
| 75 |
|
| 76 |
var win = Echodict.EchodictWindow.Instance; // 第一次实例化,后面启动就快了
|
src/WpfEditor/EchoDict.csproj
CHANGED
|
@@ -27,7 +27,7 @@
|
|
| 27 |
<ItemGroup>
|
| 28 |
<ProjectReference Include="..\EchoDictLib\EchoDictLib.csproj" />
|
| 29 |
<ProjectReference Include="..\ScreenGrab\ScreenGrab.csproj" />
|
| 30 |
-
|
| 31 |
</ItemGroup>
|
| 32 |
|
| 33 |
|
|
|
|
| 27 |
<ItemGroup>
|
| 28 |
<ProjectReference Include="..\EchoDictLib\EchoDictLib.csproj" />
|
| 29 |
<ProjectReference Include="..\ScreenGrab\ScreenGrab.csproj" />
|
| 30 |
+
<ProjectReference Include="..\WeChatOcrCpp\WeChatOcrCpp.vcxproj" />
|
| 31 |
</ItemGroup>
|
| 32 |
|
| 33 |
|
src/WpfEditor/EchoDictWindow.xaml.cs
CHANGED
|
@@ -15,6 +15,8 @@ using System.Linq;
|
|
| 15 |
using CefSharp;
|
| 16 |
using CefSharp.Wpf;
|
| 17 |
using System.Text.Json;
|
|
|
|
|
|
|
| 18 |
|
| 19 |
namespace Echodict
|
| 20 |
{
|
|
@@ -23,6 +25,10 @@ namespace Echodict
|
|
| 23 |
/// </summary>
|
| 24 |
public partial class EchodictWindow : Window
|
| 25 |
{
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
private static EchodictWindow? _instance;
|
| 27 |
public static EchodictWindow Instance
|
| 28 |
{
|
|
@@ -53,7 +59,7 @@ namespace Echodict
|
|
| 53 |
// Default hidden and minimized
|
| 54 |
//this.Visibility = Visibility.Hidden;
|
| 55 |
//this.WindowState = WindowState.Minimized;
|
| 56 |
-
|
| 57 |
this.StateChanged += (s, e) =>
|
| 58 |
{
|
| 59 |
if (this.WindowState != WindowState.Minimized)
|
|
@@ -62,10 +68,10 @@ namespace Echodict
|
|
| 62 |
}
|
| 63 |
};
|
| 64 |
|
| 65 |
-
Application.Current.Exit += (s, e) =>
|
| 66 |
-
{
|
| 67 |
-
_forceClose = true;
|
| 68 |
-
this.Close();
|
| 69 |
};
|
| 70 |
|
| 71 |
LoadConfig();
|
|
@@ -95,6 +101,31 @@ namespace Echodict
|
|
| 95 |
_isReady = true;
|
| 96 |
}
|
| 97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
|
| 99 |
{
|
| 100 |
if (_forceClose)
|
|
@@ -102,33 +133,33 @@ namespace Echodict
|
|
| 102 |
base.OnClosing(e);
|
| 103 |
return;
|
| 104 |
}
|
| 105 |
-
|
| 106 |
e.Cancel = true;
|
| 107 |
this.Hide();
|
| 108 |
}
|
| 109 |
|
| 110 |
public void ShowWord(string word)
|
| 111 |
{
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
//this.WindowState = LastWindowState;
|
| 127 |
this.WindowState = WindowState.Normal;
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
}
|
| 133 |
|
| 134 |
public void OpenUrl(string url)
|
|
@@ -136,7 +167,8 @@ namespace Echodict
|
|
| 136 |
if (this.WindowState == WindowState.Minimized)
|
| 137 |
{
|
| 138 |
this.WindowState = LastWindowState;
|
| 139 |
-
}
|
|
|
|
| 140 |
{
|
| 141 |
//this.WindowState = LastWindowState;
|
| 142 |
this.WindowState = WindowState.Normal;
|
|
@@ -176,17 +208,17 @@ namespace Echodict
|
|
| 176 |
var tabItem = new TabItem();
|
| 177 |
|
| 178 |
// Custom header with close button
|
| 179 |
-
var headerPanel = new DockPanel
|
| 180 |
-
{
|
| 181 |
Background = Brushes.Transparent // Ensure hit testing works on the whole panel
|
| 182 |
};
|
| 183 |
|
| 184 |
-
var textBlock = new TextBlock
|
| 185 |
-
{
|
| 186 |
-
Text = "Loading...",
|
| 187 |
-
VerticalAlignment = VerticalAlignment.Center
|
| 188 |
};
|
| 189 |
-
|
| 190 |
var closeButton = new Button
|
| 191 |
{
|
| 192 |
Content = "×",
|
|
@@ -258,7 +290,7 @@ namespace Echodict
|
|
| 258 |
_sourcePaths = config.SourcePaths ?? new List<string>();
|
| 259 |
_groups = config.Groups ?? new List<DictGroup>();
|
| 260 |
_currentSelectionId = config.LastSelectedDictId ?? "ALL";
|
| 261 |
-
|
| 262 |
if (config.History != null)
|
| 263 |
{
|
| 264 |
foreach (var item in config.History)
|
|
@@ -286,8 +318,8 @@ namespace Echodict
|
|
| 286 |
LastSelectedDictId = _currentSelectionId,
|
| 287 |
History = HistoryList.Items.Cast<string>().ToList()
|
| 288 |
};
|
| 289 |
-
var options = new JsonSerializerOptions
|
| 290 |
-
{
|
| 291 |
WriteIndented = true,
|
| 292 |
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
|
| 293 |
};
|
|
@@ -297,10 +329,11 @@ namespace Echodict
|
|
| 297 |
}
|
| 298 |
catch (Exception ex)
|
| 299 |
{
|
| 300 |
-
|
| 301 |
}
|
| 302 |
}
|
| 303 |
|
|
|
|
| 304 |
private void BtnAddDict_Click(object sender, RoutedEventArgs e)
|
| 305 |
{
|
| 306 |
var dlg = new EditDictionaries(_sourcePaths, _groups, _dicts);
|
|
@@ -309,7 +342,7 @@ namespace Echodict
|
|
| 309 |
{
|
| 310 |
_sourcePaths = dlg.SourcePaths;
|
| 311 |
_groups = dlg.Groups;
|
| 312 |
-
|
| 313 |
// Save immediately
|
| 314 |
SaveConfig();
|
| 315 |
|
|
@@ -327,15 +360,15 @@ namespace Echodict
|
|
| 327 |
|
| 328 |
private void BtnRescan_Click(object sender, RoutedEventArgs e)
|
| 329 |
{
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
}
|
| 340 |
|
| 341 |
private void ScanAndLoadDictionaries(bool forceRebuild = false)
|
|
@@ -347,7 +380,7 @@ namespace Echodict
|
|
| 347 |
}
|
| 348 |
_dicts.Clear();
|
| 349 |
DictsList.Items.Clear();
|
| 350 |
-
|
| 351 |
string cacheDir = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Cache");
|
| 352 |
if (!System.IO.Directory.Exists(cacheDir))
|
| 353 |
{
|
|
@@ -379,10 +412,10 @@ namespace Echodict
|
|
| 379 |
}
|
| 380 |
}
|
| 381 |
}
|
| 382 |
-
|
| 383 |
RefreshDictSelector();
|
| 384 |
SaveConfig(); // Save to persist any state if needed, though mostly redundant here
|
| 385 |
-
|
| 386 |
// Handle startup word
|
| 387 |
if (!string.IsNullOrEmpty(_startupWord))
|
| 388 |
{
|
|
@@ -394,28 +427,28 @@ namespace Echodict
|
|
| 394 |
|
| 395 |
// MessageBox.Show($"Loaded {_dicts.Count} dictionaries.", "Scan Complete");
|
| 396 |
}
|
| 397 |
-
|
| 398 |
private void RefreshDictSelector()
|
| 399 |
{
|
| 400 |
// Preserve selection if possible?
|
| 401 |
// For now reset to All
|
| 402 |
DictSelector.Items.Clear();
|
| 403 |
-
|
| 404 |
var allItem = new ComboBoxItem { Content = "All Dictionaries", Tag = "ALL" };
|
| 405 |
DictSelector.Items.Add(allItem);
|
| 406 |
-
|
| 407 |
// Groups
|
| 408 |
foreach (var group in _groups)
|
| 409 |
{
|
| 410 |
DictSelector.Items.Add(new ComboBoxItem { Content = $"[Group] {group.Name}", Tag = group });
|
| 411 |
}
|
| 412 |
-
|
| 413 |
// Individual Dictionaries
|
| 414 |
foreach (var dict in _dicts)
|
| 415 |
{
|
| 416 |
DictSelector.Items.Add(new ComboBoxItem { Content = dict.Name, Tag = dict });
|
| 417 |
}
|
| 418 |
-
|
| 419 |
// Restore selection
|
| 420 |
bool matched = false;
|
| 421 |
foreach (ComboBoxItem item in DictSelector.Items)
|
|
@@ -456,7 +489,7 @@ namespace Echodict
|
|
| 456 |
if (HistoryList.Items.Count == 0) return;
|
| 457 |
|
| 458 |
int newIndex = HistoryList.SelectedIndex + 1;
|
| 459 |
-
|
| 460 |
// If nothing is selected, we assume we are at the "latest" state (conceptually index 0 is current),
|
| 461 |
// so Back should go to index 1.
|
| 462 |
// Note: If we just added an item, it is at index 0 but typically not selected.
|
|
@@ -522,7 +555,7 @@ namespace Echodict
|
|
| 522 |
|
| 523 |
// Determine which dictionaries to search
|
| 524 |
List<Dictionary> targetDicts = new List<Dictionary>();
|
| 525 |
-
|
| 526 |
if (DictSelector.SelectedItem is ComboBoxItem selectedItem)
|
| 527 |
{
|
| 528 |
if (selectedItem.Tag is string s && s == "ALL")
|
|
@@ -597,7 +630,7 @@ namespace Echodict
|
|
| 597 |
string pattern = @"(?i)(src|href)=[""'](?!http|https|data:|#|entry:)([^""']+)[""']";
|
| 598 |
string replacement = $"$1=\"http://custom-rendering/{dict.Id}/$2\"";
|
| 599 |
resolvedDef = System.Text.RegularExpressions.Regex.Replace(resolvedDef, pattern, replacement);
|
| 600 |
-
|
| 601 |
dictContent.Append(resolvedDef);
|
| 602 |
dictContent.Append("<br/>");
|
| 603 |
hasContent = true;
|
|
@@ -614,7 +647,7 @@ namespace Echodict
|
|
| 614 |
string pattern = @"(?i)(src|href)=[""'](?!http|https|data:|#|entry:)([^""']+)[""']";
|
| 615 |
string replacement = $"$1=\"http://custom-rendering/{dict.Id}/$2\"";
|
| 616 |
def = System.Text.RegularExpressions.Regex.Replace(def, pattern, replacement);
|
| 617 |
-
|
| 618 |
dictContent.Append(def);
|
| 619 |
dictContent.Append("<br/>");
|
| 620 |
hasContent = true;
|
|
@@ -625,7 +658,7 @@ namespace Echodict
|
|
| 625 |
if (hasContent)
|
| 626 |
{
|
| 627 |
found = true;
|
| 628 |
-
|
| 629 |
// Add dictionary to the result panel list
|
| 630 |
ListBoxItem resultItem = new ListBoxItem();
|
| 631 |
resultItem.Content = dict.Name;
|
|
@@ -634,7 +667,7 @@ namespace Echodict
|
|
| 634 |
DictsList.Items.Add(resultItem);
|
| 635 |
|
| 636 |
sb.Append($"<a name=\"{anchorName}\"></a>");
|
| 637 |
-
sb.Append($"<h3>{query} <small>({dict.Name})</small></h3>");
|
| 638 |
sb.Append("<hr/>");
|
| 639 |
sb.Append(dictContent.ToString());
|
| 640 |
sb.Append("<br/><br/>");
|
|
@@ -669,7 +702,7 @@ namespace Echodict
|
|
| 669 |
}
|
| 670 |
|
| 671 |
sb.Append("</body></html>");
|
| 672 |
-
|
| 673 |
// Use RequestHandler to serve the content
|
| 674 |
// Switch to the first tab (default browser)
|
| 675 |
MainTabControl.SelectedIndex = 0;
|
|
@@ -702,16 +735,16 @@ namespace Echodict
|
|
| 702 |
// Re-search if we have a query
|
| 703 |
if (!string.IsNullOrEmpty(SideSearchBox.Text))
|
| 704 |
{
|
| 705 |
-
Search(SideSearchBox.Text, false);
|
| 706 |
}
|
| 707 |
}
|
| 708 |
|
| 709 |
private void WordList_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
| 710 |
{
|
| 711 |
-
|
| 712 |
-
|
| 713 |
-
|
| 714 |
-
|
| 715 |
}
|
| 716 |
|
| 717 |
private void ResultItem_MouseDoubleClick(object sender, MouseButtonEventArgs e)
|
|
@@ -736,7 +769,7 @@ namespace Echodict
|
|
| 736 |
HistoryList.Items.Remove(query);
|
| 737 |
}
|
| 738 |
HistoryList.Items.Insert(0, query);
|
| 739 |
-
|
| 740 |
// Limit history size to prevent config bloat
|
| 741 |
while (HistoryList.Items.Count > 100)
|
| 742 |
{
|
|
|
|
| 15 |
using CefSharp;
|
| 16 |
using CefSharp.Wpf;
|
| 17 |
using System.Text.Json;
|
| 18 |
+
using System.Runtime.InteropServices;
|
| 19 |
+
using System.Windows.Interop;
|
| 20 |
|
| 21 |
namespace Echodict
|
| 22 |
{
|
|
|
|
| 25 |
/// </summary>
|
| 26 |
public partial class EchodictWindow : Window
|
| 27 |
{
|
| 28 |
+
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
|
| 29 |
+
private static extern uint RegisterWindowMessage(string lpString);
|
| 30 |
+
private const string UniqueMessageName = "WM_SHOWME_EchoDict_WpfEditor_2025";
|
| 31 |
+
|
| 32 |
private static EchodictWindow? _instance;
|
| 33 |
public static EchodictWindow Instance
|
| 34 |
{
|
|
|
|
| 59 |
// Default hidden and minimized
|
| 60 |
//this.Visibility = Visibility.Hidden;
|
| 61 |
//this.WindowState = WindowState.Minimized;
|
| 62 |
+
|
| 63 |
this.StateChanged += (s, e) =>
|
| 64 |
{
|
| 65 |
if (this.WindowState != WindowState.Minimized)
|
|
|
|
| 68 |
}
|
| 69 |
};
|
| 70 |
|
| 71 |
+
Application.Current.Exit += (s, e) =>
|
| 72 |
+
{
|
| 73 |
+
_forceClose = true;
|
| 74 |
+
this.Close();
|
| 75 |
};
|
| 76 |
|
| 77 |
LoadConfig();
|
|
|
|
| 101 |
_isReady = true;
|
| 102 |
}
|
| 103 |
|
| 104 |
+
protected override void OnSourceInitialized(EventArgs e)
|
| 105 |
+
{
|
| 106 |
+
base.OnSourceInitialized(e);
|
| 107 |
+
HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
|
| 108 |
+
source?.AddHook(WndProc);
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
|
| 112 |
+
{
|
| 113 |
+
uint wmShowMe = RegisterWindowMessage(UniqueMessageName);
|
| 114 |
+
if (msg == wmShowMe)
|
| 115 |
+
{
|
| 116 |
+
this.Show();
|
| 117 |
+
if (this.WindowState == WindowState.Minimized)
|
| 118 |
+
{
|
| 119 |
+
this.WindowState = WindowState.Normal;
|
| 120 |
+
}
|
| 121 |
+
this.Activate();
|
| 122 |
+
this.Topmost = true;
|
| 123 |
+
// Optional: Briefly flash or focus
|
| 124 |
+
handled = true;
|
| 125 |
+
}
|
| 126 |
+
return IntPtr.Zero;
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
|
| 130 |
{
|
| 131 |
if (_forceClose)
|
|
|
|
| 133 |
base.OnClosing(e);
|
| 134 |
return;
|
| 135 |
}
|
| 136 |
+
|
| 137 |
e.Cancel = true;
|
| 138 |
this.Hide();
|
| 139 |
}
|
| 140 |
|
| 141 |
public void ShowWord(string word)
|
| 142 |
{
|
| 143 |
+
if (!this.IsLoaded)
|
| 144 |
+
{
|
| 145 |
+
_startupWord = word;
|
| 146 |
+
}
|
| 147 |
+
else
|
| 148 |
+
{
|
| 149 |
+
SideSearchBox.Text = word;
|
| 150 |
+
Search(word, true);
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
if (this.WindowState != WindowState.Maximized)
|
| 156 |
+
{
|
| 157 |
//this.WindowState = LastWindowState;
|
| 158 |
this.WindowState = WindowState.Normal;
|
| 159 |
+
}
|
| 160 |
+
this.Show();
|
| 161 |
+
this.Activate();
|
| 162 |
+
this.Topmost = true;
|
| 163 |
}
|
| 164 |
|
| 165 |
public void OpenUrl(string url)
|
|
|
|
| 167 |
if (this.WindowState == WindowState.Minimized)
|
| 168 |
{
|
| 169 |
this.WindowState = LastWindowState;
|
| 170 |
+
}
|
| 171 |
+
else if (this.WindowState != WindowState.Maximized)
|
| 172 |
{
|
| 173 |
//this.WindowState = LastWindowState;
|
| 174 |
this.WindowState = WindowState.Normal;
|
|
|
|
| 208 |
var tabItem = new TabItem();
|
| 209 |
|
| 210 |
// Custom header with close button
|
| 211 |
+
var headerPanel = new DockPanel
|
| 212 |
+
{
|
| 213 |
Background = Brushes.Transparent // Ensure hit testing works on the whole panel
|
| 214 |
};
|
| 215 |
|
| 216 |
+
var textBlock = new TextBlock
|
| 217 |
+
{
|
| 218 |
+
Text = "Loading...",
|
| 219 |
+
VerticalAlignment = VerticalAlignment.Center
|
| 220 |
};
|
| 221 |
+
|
| 222 |
var closeButton = new Button
|
| 223 |
{
|
| 224 |
Content = "×",
|
|
|
|
| 290 |
_sourcePaths = config.SourcePaths ?? new List<string>();
|
| 291 |
_groups = config.Groups ?? new List<DictGroup>();
|
| 292 |
_currentSelectionId = config.LastSelectedDictId ?? "ALL";
|
| 293 |
+
|
| 294 |
if (config.History != null)
|
| 295 |
{
|
| 296 |
foreach (var item in config.History)
|
|
|
|
| 318 |
LastSelectedDictId = _currentSelectionId,
|
| 319 |
History = HistoryList.Items.Cast<string>().ToList()
|
| 320 |
};
|
| 321 |
+
var options = new JsonSerializerOptions
|
| 322 |
+
{
|
| 323 |
WriteIndented = true,
|
| 324 |
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
|
| 325 |
};
|
|
|
|
| 329 |
}
|
| 330 |
catch (Exception ex)
|
| 331 |
{
|
| 332 |
+
System.Diagnostics.Debug.WriteLine($"Error saving config: {ex.Message}");
|
| 333 |
}
|
| 334 |
}
|
| 335 |
|
| 336 |
+
|
| 337 |
private void BtnAddDict_Click(object sender, RoutedEventArgs e)
|
| 338 |
{
|
| 339 |
var dlg = new EditDictionaries(_sourcePaths, _groups, _dicts);
|
|
|
|
| 342 |
{
|
| 343 |
_sourcePaths = dlg.SourcePaths;
|
| 344 |
_groups = dlg.Groups;
|
| 345 |
+
|
| 346 |
// Save immediately
|
| 347 |
SaveConfig();
|
| 348 |
|
|
|
|
| 360 |
|
| 361 |
private void BtnRescan_Click(object sender, RoutedEventArgs e)
|
| 362 |
{
|
| 363 |
+
bool force = (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control;
|
| 364 |
+
if (force)
|
| 365 |
+
{
|
| 366 |
+
if (MessageBox.Show("Force rebuild all indexes? This may take time.", "Rebuild Indexes", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
|
| 367 |
+
{
|
| 368 |
+
return;
|
| 369 |
+
}
|
| 370 |
+
}
|
| 371 |
+
ScanAndLoadDictionaries(force);
|
| 372 |
}
|
| 373 |
|
| 374 |
private void ScanAndLoadDictionaries(bool forceRebuild = false)
|
|
|
|
| 380 |
}
|
| 381 |
_dicts.Clear();
|
| 382 |
DictsList.Items.Clear();
|
| 383 |
+
|
| 384 |
string cacheDir = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Cache");
|
| 385 |
if (!System.IO.Directory.Exists(cacheDir))
|
| 386 |
{
|
|
|
|
| 412 |
}
|
| 413 |
}
|
| 414 |
}
|
| 415 |
+
|
| 416 |
RefreshDictSelector();
|
| 417 |
SaveConfig(); // Save to persist any state if needed, though mostly redundant here
|
| 418 |
+
|
| 419 |
// Handle startup word
|
| 420 |
if (!string.IsNullOrEmpty(_startupWord))
|
| 421 |
{
|
|
|
|
| 427 |
|
| 428 |
// MessageBox.Show($"Loaded {_dicts.Count} dictionaries.", "Scan Complete");
|
| 429 |
}
|
| 430 |
+
|
| 431 |
private void RefreshDictSelector()
|
| 432 |
{
|
| 433 |
// Preserve selection if possible?
|
| 434 |
// For now reset to All
|
| 435 |
DictSelector.Items.Clear();
|
| 436 |
+
|
| 437 |
var allItem = new ComboBoxItem { Content = "All Dictionaries", Tag = "ALL" };
|
| 438 |
DictSelector.Items.Add(allItem);
|
| 439 |
+
|
| 440 |
// Groups
|
| 441 |
foreach (var group in _groups)
|
| 442 |
{
|
| 443 |
DictSelector.Items.Add(new ComboBoxItem { Content = $"[Group] {group.Name}", Tag = group });
|
| 444 |
}
|
| 445 |
+
|
| 446 |
// Individual Dictionaries
|
| 447 |
foreach (var dict in _dicts)
|
| 448 |
{
|
| 449 |
DictSelector.Items.Add(new ComboBoxItem { Content = dict.Name, Tag = dict });
|
| 450 |
}
|
| 451 |
+
|
| 452 |
// Restore selection
|
| 453 |
bool matched = false;
|
| 454 |
foreach (ComboBoxItem item in DictSelector.Items)
|
|
|
|
| 489 |
if (HistoryList.Items.Count == 0) return;
|
| 490 |
|
| 491 |
int newIndex = HistoryList.SelectedIndex + 1;
|
| 492 |
+
|
| 493 |
// If nothing is selected, we assume we are at the "latest" state (conceptually index 0 is current),
|
| 494 |
// so Back should go to index 1.
|
| 495 |
// Note: If we just added an item, it is at index 0 but typically not selected.
|
|
|
|
| 555 |
|
| 556 |
// Determine which dictionaries to search
|
| 557 |
List<Dictionary> targetDicts = new List<Dictionary>();
|
| 558 |
+
|
| 559 |
if (DictSelector.SelectedItem is ComboBoxItem selectedItem)
|
| 560 |
{
|
| 561 |
if (selectedItem.Tag is string s && s == "ALL")
|
|
|
|
| 630 |
string pattern = @"(?i)(src|href)=[""'](?!http|https|data:|#|entry:)([^""']+)[""']";
|
| 631 |
string replacement = $"$1=\"http://custom-rendering/{dict.Id}/$2\"";
|
| 632 |
resolvedDef = System.Text.RegularExpressions.Regex.Replace(resolvedDef, pattern, replacement);
|
| 633 |
+
|
| 634 |
dictContent.Append(resolvedDef);
|
| 635 |
dictContent.Append("<br/>");
|
| 636 |
hasContent = true;
|
|
|
|
| 647 |
string pattern = @"(?i)(src|href)=[""'](?!http|https|data:|#|entry:)([^""']+)[""']";
|
| 648 |
string replacement = $"$1=\"http://custom-rendering/{dict.Id}/$2\"";
|
| 649 |
def = System.Text.RegularExpressions.Regex.Replace(def, pattern, replacement);
|
| 650 |
+
|
| 651 |
dictContent.Append(def);
|
| 652 |
dictContent.Append("<br/>");
|
| 653 |
hasContent = true;
|
|
|
|
| 658 |
if (hasContent)
|
| 659 |
{
|
| 660 |
found = true;
|
| 661 |
+
|
| 662 |
// Add dictionary to the result panel list
|
| 663 |
ListBoxItem resultItem = new ListBoxItem();
|
| 664 |
resultItem.Content = dict.Name;
|
|
|
|
| 667 |
DictsList.Items.Add(resultItem);
|
| 668 |
|
| 669 |
sb.Append($"<a name=\"{anchorName}\"></a>");
|
| 670 |
+
sb.Append($"<h3>{query} <small>({dict.Name})</small></h3>");
|
| 671 |
sb.Append("<hr/>");
|
| 672 |
sb.Append(dictContent.ToString());
|
| 673 |
sb.Append("<br/><br/>");
|
|
|
|
| 702 |
}
|
| 703 |
|
| 704 |
sb.Append("</body></html>");
|
| 705 |
+
|
| 706 |
// Use RequestHandler to serve the content
|
| 707 |
// Switch to the first tab (default browser)
|
| 708 |
MainTabControl.SelectedIndex = 0;
|
|
|
|
| 735 |
// Re-search if we have a query
|
| 736 |
if (!string.IsNullOrEmpty(SideSearchBox.Text))
|
| 737 |
{
|
| 738 |
+
Search(SideSearchBox.Text, false);
|
| 739 |
}
|
| 740 |
}
|
| 741 |
|
| 742 |
private void WordList_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
| 743 |
{
|
| 744 |
+
if (WordList.SelectedItem != null)
|
| 745 |
+
{
|
| 746 |
+
Search(WordList.SelectedItem.ToString(), false);
|
| 747 |
+
}
|
| 748 |
}
|
| 749 |
|
| 750 |
private void ResultItem_MouseDoubleClick(object sender, MouseButtonEventArgs e)
|
|
|
|
| 769 |
HistoryList.Items.Remove(query);
|
| 770 |
}
|
| 771 |
HistoryList.Items.Insert(0, query);
|
| 772 |
+
|
| 773 |
// Limit history size to prevent config bloat
|
| 774 |
while (HistoryList.Items.Count > 100)
|
| 775 |
{
|