From 9bcbb0e29f79bb7d1275bf8a134ab89eb213d46c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A3=8A?= Date: Sun, 15 May 2022 22:21:49 +0800 Subject: [PATCH 01/17] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=BF=9B=E7=A8=8B?= =?UTF-8?q?=E5=8D=95=E5=AE=9E=E4=BE=8B=E7=AA=97=E5=8F=A3=E5=94=A4=E9=86=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TextLocator/App.xaml.cs | 103 +++--- .../SingleInstance/ISingleInstanceApp.cs | 9 + TextLocator/SingleInstance/NativeMethods.cs | 55 ++++ TextLocator/SingleInstance/SingleInstance.cs | 301 ++++++++++++++++++ TextLocator/SingleInstance/VM.cs | 119 +++++++ TextLocator/TextLocator.csproj | 9 +- 6 files changed, 530 insertions(+), 66 deletions(-) create mode 100644 TextLocator/SingleInstance/ISingleInstanceApp.cs create mode 100644 TextLocator/SingleInstance/NativeMethods.cs create mode 100644 TextLocator/SingleInstance/SingleInstance.cs create mode 100644 TextLocator/SingleInstance/VM.cs diff --git a/TextLocator/App.xaml.cs b/TextLocator/App.xaml.cs index e248e7b..19983d5 100644 --- a/TextLocator/App.xaml.cs +++ b/TextLocator/App.xaml.cs @@ -1,6 +1,9 @@ using Hardcodet.Wpf.TaskbarNotification; using log4net; using System; +using System.Collections.Generic; +using System.Globalization; +using System.Reflection; using System.Runtime.InteropServices; using System.Text; using System.Threading; @@ -10,6 +13,7 @@ using TextLocator.Core; using TextLocator.Enums; using TextLocator.Factory; using TextLocator.Service; +using TextLocator.SingleInstance; using TextLocator.Util; namespace TextLocator @@ -17,17 +21,48 @@ namespace TextLocator /// /// App.xaml 的交互逻辑 /// - public partial class App : Application + public partial class App : Application, ISingleInstanceApp { private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); + /// + /// 入口函数 + /// + [STAThread] + public static void Main() + { + Assembly assembly = Assembly.GetExecutingAssembly(); + string uniqueName = string.Format(CultureInfo.InvariantCulture, "Local\\{{{0}}}{{{1}}}", assembly.GetType().GUID, assembly.GetName().Name); + if (SingleInstance.InitializeAsFirstInstance(uniqueName)) { + var app = new App(); + app.InitializeComponent(); + app.Run(); + + SingleInstance.Cleanup(); + } + } + + /// + /// 信号外部命令行参数 + /// + /// + /// + public bool SignalExternalCommandLineArgs(IList args) + { + if (this.MainWindow.WindowState == WindowState.Minimized) + { + this.MainWindow.WindowState = WindowState.Normal; + } + + this.MainWindow.Activate(); + + return true; + } + // 托盘图标 private static TaskbarIcon _taskbar; public static TaskbarIcon Taskbar { get => _taskbar; set => _taskbar = value; } - // 单实例 - private Mutex _mutex; - public App() { @@ -47,25 +82,6 @@ namespace TextLocator /// protected override void OnStartup(StartupEventArgs e) { - // 互斥 - _mutex = new Mutex(true, "TextLocator", out bool isNewInstance); - // 是否启动新实例 - if (!isNewInstance) - { - // 找到已经在运行的实例句柄(给出你的窗体标题名 “XXX影院”) - IntPtr hWndPtr = FindWindow(null, "文本搜索定位器"); - - // 还原窗口 - _ = IsIconic(hWndPtr) ? ShowWindow(hWndPtr, SW_RESTORE) : ShowWindow(hWndPtr, SW_SHOW); - - // 激活窗口 - SetForegroundWindow(hWndPtr); - - // 退出当前实例 - AppCore.Shutdown(); - return; - } - // 托盘图标 _taskbar = (TaskbarIcon)FindResource("Taskbar"); @@ -186,46 +202,5 @@ namespace TextLocator } } #endregion - - #region Windows API - //ShowWindow 参数 - private const int SW_SHOW = 5; - private const int SW_RESTORE = 9; - - /// - /// 是标志性的 - /// - /// 窗口句柄 - /// - [DllImport("USER32.DLL", SetLastError = true, CharSet = CharSet.Auto)] - private static extern bool IsIconic(IntPtr hWnd); - /// - /// 在桌面窗口列表中寻找与指定条件相符的第一个窗口。 - /// - /// 指向指定窗口的类名。如果 lpClassName 是 NULL,所有类名匹配。 - /// 指向指定窗口名称(窗口的标题)。如果 lpWindowName 是 NULL,所有windows命名匹配。 - /// 返回指定窗口句柄 - [DllImport("USER32.DLL", SetLastError = true, CharSet = CharSet.Auto)] - public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); - - /// - /// 将窗口还原,可从最小化还原 - /// - /// - /// - /// - [DllImport("USER32.DLL")] - [return: MarshalAs(UnmanagedType.Bool)] - public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); - - /// - /// 激活指定窗口 - /// - /// 指定窗口句柄 - /// - [DllImport("USER32.DLL")] - [return: MarshalAs(UnmanagedType.Bool)] - public static extern bool SetForegroundWindow(IntPtr hWnd); - #endregion } } diff --git a/TextLocator/SingleInstance/ISingleInstanceApp.cs b/TextLocator/SingleInstance/ISingleInstanceApp.cs new file mode 100644 index 0000000..50940aa --- /dev/null +++ b/TextLocator/SingleInstance/ISingleInstanceApp.cs @@ -0,0 +1,9 @@ +using System.Collections.Generic; + +namespace TextLocator.SingleInstance +{ + public interface ISingleInstanceApp + { + bool SignalExternalCommandLineArgs(IList args); + } +} diff --git a/TextLocator/SingleInstance/NativeMethods.cs b/TextLocator/SingleInstance/NativeMethods.cs new file mode 100644 index 0000000..cc3b9a4 --- /dev/null +++ b/TextLocator/SingleInstance/NativeMethods.cs @@ -0,0 +1,55 @@ +using System; +using System.ComponentModel; +using System.Runtime.InteropServices; +using System.Security; + +namespace TextLocator.SingleInstance +{ + [SuppressUnmanagedCodeSecurity] + internal static class NativeMethods + { + /// + /// Delegate declaration that matches WndProc signatures. + /// + public delegate IntPtr MessageHandler(WM uMsg, IntPtr wParam, IntPtr lParam, out bool handled); + + [DllImport("shell32.dll", EntryPoint = "CommandLineToArgvW", CharSet = CharSet.Unicode)] + private static extern IntPtr _CommandLineToArgvW([MarshalAs(UnmanagedType.LPWStr)] string cmdLine, out int numArgs); + + + [DllImport("kernel32.dll", EntryPoint = "LocalFree", SetLastError = true)] + private static extern IntPtr _LocalFree(IntPtr hMem); + + + public static string[] CommandLineToArgvW(string cmdLine) + { + IntPtr argv = IntPtr.Zero; + try + { + int numArgs = 0; + + argv = _CommandLineToArgvW(cmdLine, out numArgs); + if (argv == IntPtr.Zero) + { + throw new Win32Exception(); + } + var result = new string[numArgs]; + + for (int i = 0; i < numArgs; i++) + { + IntPtr currArg = Marshal.ReadIntPtr(argv, i * Marshal.SizeOf(typeof(IntPtr))); + result[i] = Marshal.PtrToStringUni(currArg); + } + + return result; + } + finally + { + + IntPtr p = _LocalFree(argv); + // Otherwise LocalFree failed. + // Assert.AreEqual(IntPtr.Zero, p); + } + } + } +} diff --git a/TextLocator/SingleInstance/SingleInstance.cs b/TextLocator/SingleInstance/SingleInstance.cs new file mode 100644 index 0000000..5648337 --- /dev/null +++ b/TextLocator/SingleInstance/SingleInstance.cs @@ -0,0 +1,301 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.IO; +using System.Runtime.Remoting; +using System.Runtime.Remoting.Channels; +using System.Runtime.Remoting.Channels.Ipc; +using System.Runtime.Serialization.Formatters; +using System.Threading; +using System.Windows; +using System.Windows.Threading; + +namespace TextLocator.SingleInstance +{ + + /// + /// This class checks to make sure that only one instance of + /// this application is running at a time. + /// + /// + /// Note: this class should be used with some caution, because it does no + /// security checking. For example, if one instance of an app that uses this class + /// is running as Administrator, any other instance, even if it is not + /// running as Administrator, can activate it with command line arguments. + /// For most apps, this will not be much of an issue. + /// + public static class SingleInstance + where TApplication : Application, ISingleInstanceApp + { + #region Private Fields + + /// + /// String delimiter used in channel names. + /// + private const string Delimiter = ":"; + + /// + /// Suffix to the channel name. + /// + private const string ChannelNameSuffix = "SingeInstanceIPCChannel"; + + /// + /// Remote service name. + /// + private const string RemoteServiceName = "SingleInstanceApplicationService"; + + /// + /// IPC protocol used (string). + /// + private const string IpcProtocol = "ipc://"; + + /// + /// Application mutex. + /// + private static Mutex singleInstanceMutex; + + /// + /// IPC channel for communications. + /// + private static IpcServerChannel channel; + + /// + /// List of command line arguments for the application. + /// + private static IList commandLineArgs; + + #endregion + + #region Public Properties + + /// + /// Gets list of command line arguments for the application. + /// + public static IList CommandLineArgs + { + get { return commandLineArgs; } + } + + #endregion + + #region Public Methods + + /// + /// Checks if the instance of the application attempting to start is the first instance. + /// If not, activates the first instance. + /// + /// True if this is the first instance of the application. + public static bool InitializeAsFirstInstance(string uniqueName) + { + commandLineArgs = GetCommandLineArgs(uniqueName); + + // Build unique application Id and the IPC channel name. + string applicationIdentifier = uniqueName + Environment.UserName; + + string channelName = String.Concat(applicationIdentifier, Delimiter, ChannelNameSuffix); + + // Create mutex based on unique application Id to check if this is the first instance of the application. + bool firstInstance; + singleInstanceMutex = new Mutex(true, applicationIdentifier, out firstInstance); + if (firstInstance) + { + CreateRemoteService(channelName); + } + else + { + SignalFirstInstance(channelName, commandLineArgs); + } + + return firstInstance; + } + + /// + /// Cleans up single-instance code, clearing shared resources, mutexes, etc. + /// + public static void Cleanup() + { + if (singleInstanceMutex != null) + { + singleInstanceMutex.Close(); + singleInstanceMutex = null; + } + + if (channel != null) + { + ChannelServices.UnregisterChannel(channel); + channel = null; + } + } + + #endregion + + #region Private Methods + + /// + /// Gets command line args - for ClickOnce deployed applications, command line args may not be passed directly, they have to be retrieved. + /// + /// List of command line arg strings. + private static IList GetCommandLineArgs(string uniqueApplicationName) + { + string[] args = null; + if (AppDomain.CurrentDomain.ActivationContext == null) + { + // The application was not clickonce deployed, get args from standard API's + args = Environment.GetCommandLineArgs(); + } + else + { + // The application was clickonce deployed + // Clickonce deployed apps cannot recieve traditional commandline arguments + // As a workaround commandline arguments can be written to a shared location before + // the app is launched and the app can obtain its commandline arguments from the + // shared location + string appFolderPath = Path.Combine( + Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), uniqueApplicationName); + + string cmdLinePath = Path.Combine(appFolderPath, "cmdline.txt"); + if (File.Exists(cmdLinePath)) + { + try + { + using (TextReader reader = new StreamReader(cmdLinePath, System.Text.Encoding.Unicode)) + { + args = NativeMethods.CommandLineToArgvW(reader.ReadToEnd()); + } + + File.Delete(cmdLinePath); + } + catch (IOException) + { + } + } + } + + if (args == null) + { + args = new string[] { }; + } + + return new List(args); + } + + /// + /// Creates a remote service for communication. + /// + /// Application's IPC channel name. + private static void CreateRemoteService(string channelName) + { + BinaryServerFormatterSinkProvider serverProvider = new BinaryServerFormatterSinkProvider(); + serverProvider.TypeFilterLevel = TypeFilterLevel.Full; + IDictionary props = new Dictionary(); + + props["name"] = channelName; + props["portName"] = channelName; + props["exclusiveAddressUse"] = "false"; + + // Create the IPC Server channel with the channel properties + channel = new IpcServerChannel(props, serverProvider); + + // Register the channel with the channel services + ChannelServices.RegisterChannel(channel, true); + + // Expose the remote service with the REMOTE_SERVICE_NAME + IPCRemoteService remoteService = new IPCRemoteService(); + RemotingServices.Marshal(remoteService, RemoteServiceName); + } + + /// + /// Creates a client channel and obtains a reference to the remoting service exposed by the server - + /// in this case, the remoting service exposed by the first instance. Calls a function of the remoting service + /// class to pass on command line arguments from the second instance to the first and cause it to activate itself. + /// + /// Application's IPC channel name. + /// + /// Command line arguments for the second instance, passed to the first instance to take appropriate action. + /// + private static void SignalFirstInstance(string channelName, IList args) + { + IpcClientChannel secondInstanceChannel = new IpcClientChannel(); + ChannelServices.RegisterChannel(secondInstanceChannel, true); + + string remotingServiceUrl = IpcProtocol + channelName + "/" + RemoteServiceName; + + // Obtain a reference to the remoting service exposed by the server i.e the first instance of the application + IPCRemoteService firstInstanceRemoteServiceReference = (IPCRemoteService)RemotingServices.Connect(typeof(IPCRemoteService), remotingServiceUrl); + + // Check that the remote service exists, in some cases the first instance may not yet have created one, in which case + // the second instance should just exit + if (firstInstanceRemoteServiceReference != null) + { + // Invoke a method of the remote service exposed by the first instance passing on the command line + // arguments and causing the first instance to activate itself + firstInstanceRemoteServiceReference.InvokeFirstInstance(args); + } + } + + /// + /// Callback for activating first instance of the application. + /// + /// Callback argument. + /// Always null. + private static object ActivateFirstInstanceCallback(object arg) + { + // Get command line args to be passed to first instance + IList args = arg as IList; + ActivateFirstInstance(args); + return null; + } + + /// + /// Activates the first instance of the application with arguments from a second instance. + /// + /// List of arguments to supply the first instance of the application. + private static void ActivateFirstInstance(IList args) + { + // Set main window state and process command line args + if (Application.Current == null) + { + return; + } + + ((TApplication)Application.Current).SignalExternalCommandLineArgs(args); + } + + #endregion + + #region Private Classes + + /// + /// Remoting service class which is exposed by the server i.e the first instance and called by the second instance + /// to pass on the command line arguments to the first instance and cause it to activate itself. + /// + private class IPCRemoteService : MarshalByRefObject + { + /// + /// Activates the first instance of the application. + /// + /// List of arguments to pass to the first instance. + public void InvokeFirstInstance(IList args) + { + if (Application.Current != null) + { + // Do an asynchronous call to ActivateFirstInstance function + Application.Current.Dispatcher.BeginInvoke( + DispatcherPriority.Normal, new DispatcherOperationCallback(SingleInstance.ActivateFirstInstanceCallback), args); + } + } + + /// + /// Remoting Object's ease expires after every 5 minutes by default. We need to override the InitializeLifetimeService class + /// to ensure that lease never expires. + /// + /// Always null. + public override object InitializeLifetimeService() + { + return null; + } + } + + #endregion + } +} diff --git a/TextLocator/SingleInstance/VM.cs b/TextLocator/SingleInstance/VM.cs new file mode 100644 index 0000000..b0dd6da --- /dev/null +++ b/TextLocator/SingleInstance/VM.cs @@ -0,0 +1,119 @@ +namespace TextLocator.SingleInstance +{ + internal enum WM + { + NULL = 0x0000, + CREATE = 0x0001, + DESTROY = 0x0002, + MOVE = 0x0003, + SIZE = 0x0005, + ACTIVATE = 0x0006, + SETFOCUS = 0x0007, + KILLFOCUS = 0x0008, + ENABLE = 0x000A, + SETREDRAW = 0x000B, + SETTEXT = 0x000C, + GETTEXT = 0x000D, + GETTEXTLENGTH = 0x000E, + PAINT = 0x000F, + CLOSE = 0x0010, + QUERYENDSESSION = 0x0011, + QUIT = 0x0012, + QUERYOPEN = 0x0013, + ERASEBKGND = 0x0014, + SYSCOLORCHANGE = 0x0015, + SHOWWINDOW = 0x0018, + ACTIVATEAPP = 0x001C, + SETCURSOR = 0x0020, + MOUSEACTIVATE = 0x0021, + CHILDACTIVATE = 0x0022, + QUEUESYNC = 0x0023, + GETMINMAXINFO = 0x0024, + + WINDOWPOSCHANGING = 0x0046, + WINDOWPOSCHANGED = 0x0047, + + CONTEXTMENU = 0x007B, + STYLECHANGING = 0x007C, + STYLECHANGED = 0x007D, + DISPLAYCHANGE = 0x007E, + GETICON = 0x007F, + SETICON = 0x0080, + NCCREATE = 0x0081, + NCDESTROY = 0x0082, + NCCALCSIZE = 0x0083, + NCHITTEST = 0x0084, + NCPAINT = 0x0085, + NCACTIVATE = 0x0086, + GETDLGCODE = 0x0087, + SYNCPAINT = 0x0088, + NCMOUSEMOVE = 0x00A0, + NCLBUTTONDOWN = 0x00A1, + NCLBUTTONUP = 0x00A2, + NCLBUTTONDBLCLK = 0x00A3, + NCRBUTTONDOWN = 0x00A4, + NCRBUTTONUP = 0x00A5, + NCRBUTTONDBLCLK = 0x00A6, + NCMBUTTONDOWN = 0x00A7, + NCMBUTTONUP = 0x00A8, + NCMBUTTONDBLCLK = 0x00A9, + + SYSKEYDOWN = 0x0104, + SYSKEYUP = 0x0105, + SYSCHAR = 0x0106, + SYSDEADCHAR = 0x0107, + COMMAND = 0x0111, + SYSCOMMAND = 0x0112, + + MOUSEMOVE = 0x0200, + LBUTTONDOWN = 0x0201, + LBUTTONUP = 0x0202, + LBUTTONDBLCLK = 0x0203, + RBUTTONDOWN = 0x0204, + RBUTTONUP = 0x0205, + RBUTTONDBLCLK = 0x0206, + MBUTTONDOWN = 0x0207, + MBUTTONUP = 0x0208, + MBUTTONDBLCLK = 0x0209, + MOUSEWHEEL = 0x020A, + XBUTTONDOWN = 0x020B, + XBUTTONUP = 0x020C, + XBUTTONDBLCLK = 0x020D, + MOUSEHWHEEL = 0x020E, + + + CAPTURECHANGED = 0x0215, + + ENTERSIZEMOVE = 0x0231, + EXITSIZEMOVE = 0x0232, + + IME_SETCONTEXT = 0x0281, + IME_NOTIFY = 0x0282, + IME_CONTROL = 0x0283, + IME_COMPOSITIONFULL = 0x0284, + IME_SELECT = 0x0285, + IME_CHAR = 0x0286, + IME_REQUEST = 0x0288, + IME_KEYDOWN = 0x0290, + IME_KEYUP = 0x0291, + + NCMOUSELEAVE = 0x02A2, + + DWMCOMPOSITIONCHANGED = 0x031E, + DWMNCRENDERINGCHANGED = 0x031F, + DWMCOLORIZATIONCOLORCHANGED = 0x0320, + DWMWINDOWMAXIMIZEDCHANGE = 0x0321, + + #region Windows 7 + DWMSENDICONICTHUMBNAIL = 0x0323, + DWMSENDICONICLIVEPREVIEWBITMAP = 0x0326, + #endregion + + USER = 0x0400, + + // This is the hard-coded message value used by WinForms for Shell_NotifyIcon. + // It's relatively safe to reuse. + TRAYMOUSEMESSAGE = 0x800, //WM_USER + 1024 + APP = 0x8000, + } +} diff --git a/TextLocator/TextLocator.csproj b/TextLocator/TextLocator.csproj index d9057dd..1c4e94b 100644 --- a/TextLocator/TextLocator.csproj +++ b/TextLocator/TextLocator.csproj @@ -235,6 +235,7 @@ ..\packages\System.Runtime.CompilerServices.Unsafe.6.0.0\lib\net461\System.Runtime.CompilerServices.Unsafe.dll + @@ -266,10 +267,10 @@ - + MSBuild:Compile Designer - + AreaEditWindow.xaml @@ -281,6 +282,9 @@ + + + @@ -337,6 +341,7 @@ + -- Gitee From ed52e227504615a6e55bdfa79311f0c61c90d782 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A3=8A?= Date: Sun, 15 May 2022 22:33:41 +0800 Subject: [PATCH 02/17] =?UTF-8?q?=E7=BB=86=E8=8A=82=E5=BE=AE=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TextLocator/App.xaml.cs | 4 ++- TextLocator/MainWindow.xaml | 2 +- TextLocator/MainWindow.xaml.cs | 39 ++++++++++++++++++-------- TextLocator/Properties/AssemblyInfo.cs | 4 +-- 4 files changed, 33 insertions(+), 16 deletions(-) diff --git a/TextLocator/App.xaml.cs b/TextLocator/App.xaml.cs index 19983d5..6ce6b7f 100644 --- a/TextLocator/App.xaml.cs +++ b/TextLocator/App.xaml.cs @@ -65,7 +65,6 @@ namespace TextLocator public App() { - // 初始化线程池大小 AppCore.SetThreadPoolSize(); @@ -74,6 +73,9 @@ namespace TextLocator // 初始化文件服务引擎 InitFileInfoServiceEngine(); + + // 初始化窗口状态尺寸 + CacheUtil.Put("WindowState", WindowState.Normal); } /// diff --git a/TextLocator/MainWindow.xaml b/TextLocator/MainWindow.xaml index d8437ac..7d7a906 100644 --- a/TextLocator/MainWindow.xaml +++ b/TextLocator/MainWindow.xaml @@ -7,7 +7,7 @@ xmlns:rubyer="clr-namespace:Rubyer;assembly=Rubyer" mc:Ignorable="d" Title="文本搜索定位器" Width="1600" Height="900" Icon="Resource/App.ico" - WindowStartupLocation="CenterScreen" Loaded="Window_Loaded" Closing="Window_Closing" Activated="Window_Activated"> + WindowStartupLocation="CenterScreen" Loaded="Window_Loaded" Closing="Window_Closing" Activated="Window_Activated" SizeChanged="Window_SizeChanged" StateChanged="Window_StateChanged"> diff --git a/TextLocator/MainWindow.xaml.cs b/TextLocator/MainWindow.xaml.cs index 746d58a..eba055b 100644 --- a/TextLocator/MainWindow.xaml.cs +++ b/TextLocator/MainWindow.xaml.cs @@ -51,11 +51,6 @@ namespace TextLocator /// private static volatile bool build = false; - /// - /// 窗口状态 - /// - private WindowState _windowState = WindowState.Normal; - /// /// 数据模型 /// @@ -141,6 +136,17 @@ namespace TextLocator HotKeySettingManager.Instance.RegisterGlobalHotKeyEvent += Instance_RegisterGlobalHotKeyEvent; } + /// + /// 窗口激活 + /// + /// + /// + private void Window_Activated(object sender, EventArgs e) + { + this.Show(); + this.WindowState = CacheUtil.Get("WindowState"); + } + /// /// 窗口关闭中,改为隐藏 /// @@ -148,20 +154,30 @@ namespace TextLocator /// private void Window_Closing(object sender, CancelEventArgs e) { - _windowState = this.WindowState; this.Hide(); e.Cancel = true; + CacheUtil.Put("WindowState", this.WindowState); } /// - /// 窗口激活 + /// 尺寸变化 /// /// /// - private void Window_Activated(object sender, EventArgs e) + private void Window_SizeChanged(object sender, SizeChangedEventArgs e) { - this.Show(); - this.WindowState = _windowState; + CacheUtil.Put("WindowWidth", this.Width); + CacheUtil.Put("WindowHeight", this.Height); + } + + /// + /// 状态变化 + /// + /// + /// + private void Window_StateChanged(object sender, EventArgs e) + { + CacheUtil.Put("WindowState", this.WindowState); } #endregion @@ -176,7 +192,6 @@ namespace TextLocator // 设置标题 this.Title = string.Format("{0} v{1} (开放版)", this.Title, version); - } /// @@ -1360,7 +1375,7 @@ namespace TextLocator return keywords; } - + #endregion } } diff --git a/TextLocator/Properties/AssemblyInfo.cs b/TextLocator/Properties/AssemblyInfo.cs index a777f27..8e62e87 100644 --- a/TextLocator/Properties/AssemblyInfo.cs +++ b/TextLocator/Properties/AssemblyInfo.cs @@ -50,9 +50,9 @@ using System.Windows; //通过使用 "*",如下所示: // [assembly: AssemblyVersion("1.0.*")] // 大版本,强制更新最小版本 -[assembly: AssemblyVersion("2.1.20")] +[assembly: AssemblyVersion("2.1.21")] // 小版本,选择更新版本 -[assembly: AssemblyFileVersion("2.1.20.1")] +[assembly: AssemblyFileVersion("2.1.21.2")] // Version minVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version; // Version version = new Version(FileVersionInfo.GetVersionInfo(System.Windows.Forms.Application.ExecutablePath).ProductVersion); -- Gitee From e94d8f16e3fbb184db8067402309235d2e25e8af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A3=8A?= Date: Sun, 15 May 2022 23:09:18 +0800 Subject: [PATCH 03/17] =?UTF-8?q?bug=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TextLocator/App.xaml.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/TextLocator/App.xaml.cs b/TextLocator/App.xaml.cs index 6ce6b7f..119e2e5 100644 --- a/TextLocator/App.xaml.cs +++ b/TextLocator/App.xaml.cs @@ -51,11 +51,11 @@ namespace TextLocator { if (this.MainWindow.WindowState == WindowState.Minimized) { - this.MainWindow.WindowState = WindowState.Normal; + this.MainWindow.WindowState = CacheUtil.Get("WindowState"); } + this.MainWindow.Show(); this.MainWindow.Activate(); - return true; } -- Gitee From 086770321f0d68f104eab92ec00776c681f2b4f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A3=8A?= Date: Sun, 15 May 2022 23:52:10 +0800 Subject: [PATCH 04/17] =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E6=94=B9=E4=B8=BA=E7=A8=8B=E5=BA=8F=E6=BA=90=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TextLocator/App.xaml.cs | 2 +- TextLocator/Enums/FileType.cs | 4 ++-- TextLocator/Properties/AssemblyInfo.cs | 4 ++-- TextLocator/Util/FileUtil.cs | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/TextLocator/App.xaml.cs b/TextLocator/App.xaml.cs index 119e2e5..07bf8f3 100644 --- a/TextLocator/App.xaml.cs +++ b/TextLocator/App.xaml.cs @@ -140,7 +140,7 @@ namespace TextLocator // 常用压缩包 FileInfoServiceFactory.Register(FileType.常用压缩包, new ZipFileService()); // 程序员服务 - FileInfoServiceFactory.Register(FileType.程序员代码, new CodeFileService()); + FileInfoServiceFactory.Register(FileType.程序源代码, new CodeFileService()); } catch (Exception ex) { diff --git a/TextLocator/Enums/FileType.cs b/TextLocator/Enums/FileType.cs index d94aa78..2375b36 100644 --- a/TextLocator/Enums/FileType.cs +++ b/TextLocator/Enums/FileType.cs @@ -53,9 +53,9 @@ namespace TextLocator.Enums [Description("rar,zip,7z,tar,jar")] 常用压缩包, /// - /// 程序员代码 + /// 程序源代码 /// [Description("cs,java,js,css,md,py,c,h,cpp,lua,sql,jsp,json,php,rs,rb,yml,yaml,bat,ps1")] - 程序员代码 + 程序源代码 } } diff --git a/TextLocator/Properties/AssemblyInfo.cs b/TextLocator/Properties/AssemblyInfo.cs index 8e62e87..fdc74bc 100644 --- a/TextLocator/Properties/AssemblyInfo.cs +++ b/TextLocator/Properties/AssemblyInfo.cs @@ -50,9 +50,9 @@ using System.Windows; //通过使用 "*",如下所示: // [assembly: AssemblyVersion("1.0.*")] // 大版本,强制更新最小版本 -[assembly: AssemblyVersion("2.1.21")] +[assembly: AssemblyVersion("2.1.23")] // 小版本,选择更新版本 -[assembly: AssemblyFileVersion("2.1.21.2")] +[assembly: AssemblyFileVersion("2.1.23.6")] // Version minVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version; // Version version = new Version(FileVersionInfo.GetVersionInfo(System.Windows.Forms.Application.ExecutablePath).ProductVersion); diff --git a/TextLocator/Util/FileUtil.cs b/TextLocator/Util/FileUtil.cs index 2e58db6..efbfc63 100644 --- a/TextLocator/Util/FileUtil.cs +++ b/TextLocator/Util/FileUtil.cs @@ -52,7 +52,7 @@ namespace TextLocator.Util case FileType.常用图片: bitmap = Properties.Resources.image; break; - case FileType.程序员代码: + case FileType.程序源代码: bitmap = Properties.Resources.code; break; case FileType.TXT文档: -- Gitee From d1e975ecd897ca206c6903f0ed577e78ef846559 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A3=8A?= Date: Fri, 20 May 2022 12:55:49 +0800 Subject: [PATCH 05/17] =?UTF-8?q?Revert=20"bug=E4=BF=AE=E6=AD=A3"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit e94d8f16e3fbb184db8067402309235d2e25e8af. --- TextLocator/App.xaml.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/TextLocator/App.xaml.cs b/TextLocator/App.xaml.cs index 07bf8f3..246ecdb 100644 --- a/TextLocator/App.xaml.cs +++ b/TextLocator/App.xaml.cs @@ -51,11 +51,11 @@ namespace TextLocator { if (this.MainWindow.WindowState == WindowState.Minimized) { - this.MainWindow.WindowState = CacheUtil.Get("WindowState"); + this.MainWindow.WindowState = WindowState.Normal; } - this.MainWindow.Show(); this.MainWindow.Activate(); + return true; } -- Gitee From ad84f288363c7725c1430c9c85fd32bd2d7eb970 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A3=8A?= Date: Fri, 20 May 2022 13:05:58 +0800 Subject: [PATCH 06/17] =?UTF-8?q?=E5=85=BC=E5=AE=B9=E8=80=81=E7=9A=84?= =?UTF-8?q?=E7=B4=A2=E5=BC=95=EF=BC=8C=E7=A8=8B=E5=BA=8F=E5=91=98=E4=B8=8D?= =?UTF-8?q?=E8=83=BD=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TextLocator/App.xaml.cs | 2 +- TextLocator/Enums/FileType.cs | 4 ++-- TextLocator/Util/FileUtil.cs | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/TextLocator/App.xaml.cs b/TextLocator/App.xaml.cs index 246ecdb..6ce6b7f 100644 --- a/TextLocator/App.xaml.cs +++ b/TextLocator/App.xaml.cs @@ -140,7 +140,7 @@ namespace TextLocator // 常用压缩包 FileInfoServiceFactory.Register(FileType.常用压缩包, new ZipFileService()); // 程序员服务 - FileInfoServiceFactory.Register(FileType.程序源代码, new CodeFileService()); + FileInfoServiceFactory.Register(FileType.程序员代码, new CodeFileService()); } catch (Exception ex) { diff --git a/TextLocator/Enums/FileType.cs b/TextLocator/Enums/FileType.cs index 2375b36..d94aa78 100644 --- a/TextLocator/Enums/FileType.cs +++ b/TextLocator/Enums/FileType.cs @@ -53,9 +53,9 @@ namespace TextLocator.Enums [Description("rar,zip,7z,tar,jar")] 常用压缩包, /// - /// 程序源代码 + /// 程序员代码 /// [Description("cs,java,js,css,md,py,c,h,cpp,lua,sql,jsp,json,php,rs,rb,yml,yaml,bat,ps1")] - 程序源代码 + 程序员代码 } } diff --git a/TextLocator/Util/FileUtil.cs b/TextLocator/Util/FileUtil.cs index efbfc63..2e58db6 100644 --- a/TextLocator/Util/FileUtil.cs +++ b/TextLocator/Util/FileUtil.cs @@ -52,7 +52,7 @@ namespace TextLocator.Util case FileType.常用图片: bitmap = Properties.Resources.image; break; - case FileType.程序源代码: + case FileType.程序员代码: bitmap = Properties.Resources.code; break; case FileType.TXT文档: -- Gitee From 84e1a19cf40d62d42ce71f41e5d577f7f2d11077 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A3=8A?= Date: Fri, 20 May 2022 15:04:39 +0800 Subject: [PATCH 07/17] =?UTF-8?q?=E4=BC=98=E5=8C=96Excel=E9=A2=84=E8=A7=88?= =?UTF-8?q?=E5=86=85=E5=AE=B9=E7=A9=BA=E6=A0=BC=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TextLocator/Core/AppConst.cs | 2 +- TextLocator/Service/ExcelFileService.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/TextLocator/Core/AppConst.cs b/TextLocator/Core/AppConst.cs index 14e97ec..0f95dd3 100644 --- a/TextLocator/Core/AppConst.cs +++ b/TextLocator/Core/AppConst.cs @@ -93,7 +93,7 @@ namespace TextLocator.Core /// /// 匹配空白和换行 /// - public static readonly Regex REGEX_LINE_BREAKS_WHITESPACE = new Regex(@" |\r\r|\n\n|┄|\s"); + public static readonly Regex REGEX_LINE_BREAKS_WHITESPACE = new Regex(@" |\r\r|\n\n|\s\s|┄"); /// /// 匹配HTML和XML标签 /// diff --git a/TextLocator/Service/ExcelFileService.cs b/TextLocator/Service/ExcelFileService.cs index d09ebd8..1b9f9cf 100644 --- a/TextLocator/Service/ExcelFileService.cs +++ b/TextLocator/Service/ExcelFileService.cs @@ -97,7 +97,7 @@ namespace TextLocator.Service { for (int j = 0; j < columnCount; j++) { - builder.Append(sheet.Cells[i + 1, j + 1].Value + " "); + builder.Append(sheet.Cells[i + 1, j + 1].Value + " "); } builder.AppendLine(); } @@ -199,7 +199,7 @@ namespace TextLocator.Service int cellCount = row.LastCellNum; for (int k = 0; k < cellCount; k++) { - builder.Append(row.GetCell(j) + " "); + builder.Append(row.GetCell(j) + " "); } builder.AppendLine(); } -- Gitee From 7dbb395011cb29cb24475e90fb5a5a38e6b0c2f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A3=8A?= Date: Fri, 20 May 2022 15:24:01 +0800 Subject: [PATCH 08/17] =?UTF-8?q?=E4=B8=AD=E9=97=B4=E5=88=86=E9=9A=94?= =?UTF-8?q?=E7=BA=BF=EF=BC=8C=E5=A2=9E=E5=8A=A0=E5=AE=BD=E5=BA=A6=EF=BC=8C?= =?UTF-8?q?=E9=BC=A0=E6=A0=87=E6=9B=B4=E5=AE=B9=E6=98=93=E6=8B=96=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TextLocator/MainWindow.xaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/TextLocator/MainWindow.xaml b/TextLocator/MainWindow.xaml index 7d7a906..b08c7f0 100644 --- a/TextLocator/MainWindow.xaml +++ b/TextLocator/MainWindow.xaml @@ -109,9 +109,9 @@ - - - + + + -- Gitee From 299d26dc114ec3de1aaac9abea3ade12dcd04e8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A3=8A?= Date: Fri, 20 May 2022 16:55:14 +0800 Subject: [PATCH 09/17] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=A3=80=E6=9F=A5=E9=80=BB=E8=BE=91=EF=BC=8C?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=87=AA=E5=8A=A8=E6=9B=B4=E6=96=B0=E6=A3=80?= =?UTF-8?q?=E6=9F=A5=E9=97=B4=E9=9A=94=E6=97=B6=E9=97=B4bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TextLocator/MainWindow.xaml.cs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/TextLocator/MainWindow.xaml.cs b/TextLocator/MainWindow.xaml.cs index eba055b..60b8d0b 100644 --- a/TextLocator/MainWindow.xaml.cs +++ b/TextLocator/MainWindow.xaml.cs @@ -1028,12 +1028,16 @@ namespace TextLocator if (build) { log.Info("上次任务还没执行完成,跳过本次任务。"); - return; } - build = true; - // 执行索引更新,扫描新文件。 - log.Info("开始执行索引更新检查。"); - BuildIndex(false, true); + else + { + // 执行索引更新,扫描新文件。 + log.Info("开始执行索引更新检查。"); + + build = true; + + BuildIndex(false, true); + } // 配置参数错误导致的bug矫正 if (AppConst.INDEX_UPDATE_TASK_INTERVAL <= 5) -- Gitee From 2368bfba43349c40dbb58ad40d8971cf6363149c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A3=8A?= Date: Fri, 20 May 2022 23:00:31 +0800 Subject: [PATCH 10/17] =?UTF-8?q?1=E3=80=81=E4=BC=98=E5=8C=96=E9=A2=84?= =?UTF-8?q?=E8=A7=88=EF=BC=9A=E5=A2=9E=E5=8A=A0=E9=A2=84=E8=A7=88=E5=85=A8?= =?UTF-8?q?=E6=96=87=20=E6=88=96=20=E9=A2=84=E8=A7=88=E5=91=BD=E4=B8=AD?= =?UTF-8?q?=E7=82=B9=E6=A6=82=E8=A6=81=E5=86=85=E5=AE=B9=E9=80=89=E9=A1=B9?= =?UTF-8?q?=202=E3=80=81=E4=BF=AE=E5=A4=8D=E6=96=87=E4=BB=B6=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E6=97=B6=E6=97=A0=E6=B3=95=E8=AF=BB=E5=8F=96=E5=88=B0?= =?UTF-8?q?=E6=9C=80=E6=96=B0=E5=86=85=E5=AE=B9=E7=9A=84bug=EF=BC=88?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=E5=86=85=E5=AE=B9=E8=AF=BB?= =?UTF-8?q?=E5=8F=96=E7=BC=93=E5=AD=98=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TextLocator/Core/AppConst.cs | 21 +++- TextLocator/Factory/FileInfoServiceFactory.cs | 76 ++++++------ TextLocator/Index/IndexCore.cs | 2 +- TextLocator/MainWindow.xaml | 2 + TextLocator/MainWindow.xaml.cs | 99 +++++++++++++--- TextLocator/Properties/AssemblyInfo.cs | 4 +- TextLocator/SettingWindow.xaml | 7 ++ TextLocator/SettingWindow.xaml.cs | 10 +- TextLocator/Util/FileContentUtil.cs | 108 +++++++++++++++++- 9 files changed, 261 insertions(+), 68 deletions(-) diff --git a/TextLocator/Core/AppConst.cs b/TextLocator/Core/AppConst.cs index 0f95dd3..dd4cb7d 100644 --- a/TextLocator/Core/AppConst.cs +++ b/TextLocator/Core/AppConst.cs @@ -14,10 +14,6 @@ namespace TextLocator.Core /// public class AppConst { - /// - /// 结果列表分页条数 - /// - public static int MRESULT_LIST_PAGE_SIZE = int.Parse(AppUtil.ReadValue("AppConfig", "ResultListPageSize", "100")); /// /// 文件读取超时时间,单位:秒 /// @@ -27,17 +23,30 @@ namespace TextLocator.Core /// public static int FILE_SIZE_LIMIT = int.Parse(AppUtil.ReadValue("AppConfig", "FileSizeLimit", "200000000")); /// + /// 文件内容摘要切割长度 + /// + public static int FILE_CONTENT_BREVIARY_CUT_LENGTH = int.Parse(AppUtil.ReadValue("AppConfig", "FileContentBreviaryCutLength", "60")); + /// + /// 结果列表分页条数 + /// + public static int MRESULT_LIST_PAGE_SIZE = int.Parse(AppUtil.ReadValue("AppConfig", "ResultListPageSize", "100")); + /// /// 缓存池容量 /// public static int CACHE_POOL_CAPACITY = int.Parse(AppUtil.ReadValue("AppConfig", "CachePoolCapacity", "100000")); + /// + /// 索引更新任务间隔时间,单位:分 + /// + public static int INDEX_UPDATE_TASK_INTERVAL = int.Parse(AppUtil.ReadValue("AppConfig", "IndexUpdateTaskInterval", "10")); + /// /// 启用索引更新任务,默认启用 /// public static bool ENABLE_INDEX_UPDATE_TASK = bool.Parse(AppUtil.ReadValue("AppConfig", "EnableIndexUpdateTask", "True")); /// - /// 索引更新任务间隔时间,单位:分 + /// 启用预览摘要 /// - public static int INDEX_UPDATE_TASK_INTERVAL = int.Parse(AppUtil.ReadValue("AppConfig", "IndexUpdateTaskInterval", "10")); + public static bool ENABLE_PREVIEW_SUMMARY = bool.Parse(AppUtil.ReadValue("AppConfig", "EnablePreviewSummary", "False")); /// /// 最小工作线程(CPU线程数 * 2) diff --git a/TextLocator/Factory/FileInfoServiceFactory.cs b/TextLocator/Factory/FileInfoServiceFactory.cs index 4b64b9b..48a4be8 100644 --- a/TextLocator/Factory/FileInfoServiceFactory.cs +++ b/TextLocator/Factory/FileInfoServiceFactory.cs @@ -32,52 +32,27 @@ namespace TextLocator.Factory /// public static string GetFileContent(string filePath) { - FileInfo fileInfo = new FileInfo(filePath); + // 读取文件内容 + string content = String.Empty; try { - // 如果文件存在 - if (fileInfo == null && !fileInfo.Exists) - { - throw new FileNotFoundException("文件未找到,请确认"); - } - // 文件太大 - if (fileInfo.Length > AppConst.FILE_SIZE_LIMIT) - { - throw new FileBigSizeException("不支持大于 " + FileUtil.GetFileSizeFriendly(AppConst.FILE_SIZE_LIMIT) + " 的文件解析"); - } - } - catch (Exception ex) - { - log.Error(filePath + "->" + ex.Message, ex); - - return null; - } + // 检查文件信息 + CheckFileInfo(filePath); - // 获取文件服务对象 - IFileInfoService fileInfoService = GetFileInfoService(FileTypeUtil.GetFileType(filePath)); + // 获取文件服务对象 + IFileInfoService fileInfoService = GetFileInfoService(FileTypeUtil.GetFileType(filePath)); - // 内容 - string content = ""; - // 缓存Key - string cacheKey = MD5Util.GetMD5Hash(filePath); + // 获取文件内容 + content = WaitTimeout(fileInfoService.GetFileContent, filePath); - if (CacheUtil.Exists(cacheKey)) - { - // 从缓存中读取 - content = CacheUtil.Get(cacheKey); -#if DEBUG - log.Debug(filePath + ",缓存生效。"); -#endif + // 特殊字符替换 + content = AppConst.REGEX_LINE_BREAKS_WHITESPACE.Replace(content, " "); } - else + catch (Exception ex) { - // 读取文件内容 - content = WaitTimeout(fileInfoService.GetFileContent, filePath); - - // 写入缓存 - CacheUtil.Put(cacheKey, content); + log.Error(filePath + " -> 文件读取错误:" + ex.Message, ex); } - + // 返回 return content; } @@ -109,6 +84,25 @@ namespace TextLocator.Factory throw new NotFoundFileServiceException("暂无[" + fileType.ToString() + "]服务实例, 返回默认其他类型文件服务实例"); } } + + /// + /// 检查文件信息 + /// + /// 文件路径 + private static void CheckFileInfo(string filePath) + { + FileInfo fileInfo = new FileInfo(filePath); + // 如果文件存在 + if (fileInfo == null && !fileInfo.Exists) + { + throw new FileNotFoundException("文件未找到,请确认"); + } + // 文件太大 + if (fileInfo.Length > AppConst.FILE_SIZE_LIMIT) + { + throw new FileBigSizeException(string.Format("不支持大于 {0} 的文件解析", FileUtil.GetFileSizeFriendly(AppConst.FILE_SIZE_LIMIT))); + } + } #endregion #region 超时函数 @@ -130,7 +124,11 @@ namespace TextLocator.Factory { string obj = null; AutoResetEvent are = new AutoResetEvent(false); - Thread t = new Thread(delegate () { obj = method(filePath); are.Set(); }); + Thread t = new Thread(() => + { + obj = method(filePath); + are.Set(); + }); t.Start(); Wait(t, are); return obj; diff --git a/TextLocator/Index/IndexCore.cs b/TextLocator/Index/IndexCore.cs index 3cc655b..a14ccb1 100644 --- a/TextLocator/Index/IndexCore.cs +++ b/TextLocator/Index/IndexCore.cs @@ -888,7 +888,7 @@ namespace TextLocator.Index // 获取内容 string content = AppConst.REGEX_CONTENT_PAGE.Replace(fileInfo.Preview, ""); // 缩略信息 - string breviary = AppConst.REGEX_LINE_BREAKS_WHITESPACE.Replace(content, ""); + string breviary = AppConst.REGEX_LINE_BREAKS_WHITESPACE.Replace(content, " "); int min = 0; int max = breviary.Length; diff --git a/TextLocator/MainWindow.xaml b/TextLocator/MainWindow.xaml index b08c7f0..1a37602 100644 --- a/TextLocator/MainWindow.xaml +++ b/TextLocator/MainWindow.xaml @@ -97,6 +97,8 @@ + + private void IndexUpdateTask() { + // 方案一:定时器 + /*if (AppConst.INDEX_UPDATE_TASK_INTERVAL <= 5) + AppConst.INDEX_UPDATE_TASK_INTERVAL = 5; + + System.Timers.Timer timer = new System.Timers.Timer(); + timer.Interval = AppConst.INDEX_UPDATE_TASK_INTERVAL * 60 * 1000; + timer.Elapsed += Timer_Elapsed; + timer.AutoReset = true; + timer.Enabled = true; + timer.Start();*/ + + // 方案二:线程 Task.Factory.StartNew(() => { try @@ -1028,18 +1072,18 @@ namespace TextLocator if (build) { log.Info("上次任务还没执行完成,跳过本次任务。"); + return; } else { - // 执行索引更新,扫描新文件。 log.Info("开始执行索引更新检查。"); build = true; - + BuildIndex(false, true); } - // 配置参数错误导致的bug矫正 + // 修复bug容错处理 if (AppConst.INDEX_UPDATE_TASK_INTERVAL <= 5) AppConst.INDEX_UPDATE_TASK_INTERVAL = 5; @@ -1053,6 +1097,27 @@ namespace TextLocator }); } + /// + /// 定时器执行逻辑 + /// + /// + /// + private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) + { + if (build) + { + log.Info("上次任务还没执行完成,跳过本次任务。"); + } + else + { + log.Info("开始执行索引更新检查。"); + + build = true; + + BuildIndex(false, true); + } + } + /// /// 检查索引是否存在 /// diff --git a/TextLocator/Properties/AssemblyInfo.cs b/TextLocator/Properties/AssemblyInfo.cs index fdc74bc..8bf4c6e 100644 --- a/TextLocator/Properties/AssemblyInfo.cs +++ b/TextLocator/Properties/AssemblyInfo.cs @@ -50,9 +50,9 @@ using System.Windows; //通过使用 "*",如下所示: // [assembly: AssemblyVersion("1.0.*")] // 大版本,强制更新最小版本 -[assembly: AssemblyVersion("2.1.23")] +[assembly: AssemblyVersion("2.1.26")] // 小版本,选择更新版本 -[assembly: AssemblyFileVersion("2.1.23.6")] +[assembly: AssemblyFileVersion("2.1.26.1")] // Version minVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version; // Version version = new Version(FileVersionInfo.GetVersionInfo(System.Windows.Forms.Application.ExecutablePath).ProductVersion); diff --git a/TextLocator/SettingWindow.xaml b/TextLocator/SettingWindow.xaml index 6f8dbc0..3b50239 100644 --- a/TextLocator/SettingWindow.xaml +++ b/TextLocator/SettingWindow.xaml @@ -59,6 +59,13 @@ + + + + + + + diff --git a/TextLocator/SettingWindow.xaml.cs b/TextLocator/SettingWindow.xaml.cs index 269c159..21dc1b0 100644 --- a/TextLocator/SettingWindow.xaml.cs +++ b/TextLocator/SettingWindow.xaml.cs @@ -70,6 +70,9 @@ namespace TextLocator this.EnableIndexUpdateTask.IsChecked = AppConst.ENABLE_INDEX_UPDATE_TASK; // 索引更新时间间隔 this.IndexUpdateTaskInterval.Text = AppConst.INDEX_UPDATE_TASK_INTERVAL + ""; + + // 启用预览内容摘要 + this.EnablePreviewSummary.IsChecked = AppConst.ENABLE_PREVIEW_SUMMARY; } #region 保存并关闭 @@ -136,7 +139,6 @@ namespace TextLocator // 启用索引更新任务 bool enableIndexUpdateTask = (bool)this.EnableIndexUpdateTask.IsChecked; - if (enableIndexUpdateTask) { // 索引更新时间间隔 @@ -160,6 +162,9 @@ namespace TextLocator AppConst.INDEX_UPDATE_TASK_INTERVAL = indexUpdateTaskInterval; } + // 启用预览内容摘要 + bool enablePreviewSummary = (bool)this.EnablePreviewSummary.IsChecked; + AppConst.CACHE_POOL_CAPACITY = cachePoolCapacity; AppUtil.WriteValue("AppConfig", "CachePoolCapacity", AppConst.CACHE_POOL_CAPACITY + ""); log.Debug("修改缓存池容量:" + AppConst.CACHE_POOL_CAPACITY); @@ -182,6 +187,9 @@ namespace TextLocator log.Debug("修改索引更新任务间隔时间:" + AppConst.INDEX_UPDATE_TASK_INTERVAL); } + AppConst.ENABLE_PREVIEW_SUMMARY = enablePreviewSummary; + AppUtil.WriteValue("AppConfig", "EnableIndexUpdateTask", AppConst.ENABLE_PREVIEW_SUMMARY + ""); + this.Close(); } #endregion diff --git a/TextLocator/Util/FileContentUtil.cs b/TextLocator/Util/FileContentUtil.cs index e0f868a..a12ad70 100644 --- a/TextLocator/Util/FileContentUtil.cs +++ b/TextLocator/Util/FileContentUtil.cs @@ -11,7 +11,6 @@ namespace TextLocator.Util { public class FileContentUtil { - #region RichText操作 /// /// 清空RichText的Document /// @@ -126,6 +125,111 @@ namespace TextLocator.Util return tpEnd.GetNextContextPosition(LogicalDirection.Forward); } - #endregion + + /// + /// 获取命中摘要列表 + /// + /// 内容文本 + /// 关键词列表 + /// 高亮色 + /// 是否高亮背景 + /// 切割长度 + /// + public static FlowDocument GetHitBreviaryFlowDocument(string content, List keywords, System.Windows.Media.Color color, bool isBackground = false, int cutLength = int.MinValue) + { + // 定义接收命中内容上下文的列表 + FlowDocument document = new FlowDocument(); + if (string.IsNullOrEmpty(content)) + { + return document; + } + // 如果默认值,就使用参数定义值 + if (cutLength == int.MinValue) + { + cutLength = AppConst.FILE_CONTENT_BREVIARY_CUT_LENGTH; + } + + // 内容(替换页码分隔符) + content = AppConst.REGEX_CONTENT_PAGE.Replace(content, ""); + // 替换多余的换行 + content = AppConst.REGEX_LINE_BREAKS_WHITESPACE.Replace(content, " "); + // 定义最大值和最小值、截取长度 + int min = 0; + int max = content.Length; + // 命中数索引下标 + int page = 1; + // 遍历关键词列表 + foreach (string keyword in keywords) + { + // 定义关键词正则 + Regex regex = new Regex(keyword, RegexOptions.IgnoreCase); + // 匹配集合 + MatchCollection collection = regex.Matches(content); + // 遍历命中列表 + foreach (Match match in collection) + { + // 匹配位置 + int index = match.Index; + + int startIndex = index - cutLength / 2; + int endIndex = index + match.Length + cutLength / 2; + + // 顺序不能乱 + if (startIndex < min) startIndex = min; + if (endIndex > max) endIndex = max; + if (startIndex > endIndex) startIndex = endIndex - cutLength; + if (startIndex < min) startIndex = min; + if (startIndex + endIndex < cutLength) endIndex = endIndex + cutLength - (startIndex + endIndex); + if (endIndex > max) endIndex = max; + + // 开始位置 + string before = content.Substring(startIndex, index - startIndex); + if (startIndex > min) + { + before = "..." + before; + } + // 关键词位置(高亮处理) + string highlight = content.Substring(index, match.Length); + // 结束位置 + string after = content.Substring(index + match.Length, endIndex - (index + match.Length)); + if (endIndex < max) + { + after = after + "..."; + } + + Paragraph paragraph = new Paragraph(); + paragraph.FontSize = 13; + paragraph.FontFamily = new System.Windows.Media.FontFamily("微软雅黑"); + + Run beforeRun = new Run(before); + paragraph.Inlines.Add(beforeRun); + + Run highlightRun = new Run(highlight); + highlightRun.FontWeight = FontWeight.FromOpenTypeWeight(700); + if (isBackground) + { + highlightRun.Background = new SolidColorBrush(color); + highlightRun.Foreground = new SolidColorBrush(Colors.White); + } + else + { + highlightRun.Foreground = new SolidColorBrush(color); + } + + paragraph.Inlines.Add(highlightRun); + + Run afterRun = new Run(after); + paragraph.Inlines.Add(afterRun); + + // 分割线 + Run pageRun = new Run(string.Format("\r\n------------------------------------------------------------------------------ {0}\r\n", page)); + paragraph.Inlines.Add(pageRun); + document.Blocks.Add(paragraph); + + page++; + } + } + return document; + } } } -- Gitee From 445b7a9ff1b74be1fbbbf3a3d5685b4b3d367df1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A3=8A?= Date: Fri, 20 May 2022 23:53:00 +0800 Subject: [PATCH 11/17] =?UTF-8?q?1=E3=80=81=E4=BC=98=E5=8C=96=E4=BA=8C?= =?UTF-8?q?=E7=BA=A7=E7=AA=97=E5=8F=A3=E4=BD=8D=E7=BD=AE=E7=BB=86=E8=8A=82?= =?UTF-8?q?=202=E3=80=81=E8=AF=8D=E9=A2=91=E7=BB=9F=E8=AE=A1=E8=AF=A6?= =?UTF-8?q?=E6=83=85=EF=BC=8C=E6=94=B9=E4=B8=BA=E9=BC=A0=E6=A0=87=E7=A7=BB?= =?UTF-8?q?=E5=85=A5=E5=8A=A0=E8=BD=BD=EF=BC=88=E6=8F=90=E9=AB=98=E5=88=97?= =?UTF-8?q?=E8=A1=A8=E5=8A=A0=E8=BD=BD=E6=95=88=E7=8E=87=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TextLocator/App.xaml.cs | 3 ++ TextLocator/Core/AppConst.cs | 2 +- TextLocator/Entity/FileInfo.cs | 1 - TextLocator/FileInfoItem.xaml | 2 +- TextLocator/FileInfoItem.xaml.cs | 32 ++++++++++++++----- TextLocator/HotkeyWindow.xaml | 2 +- TextLocator/MainWindow.xaml | 2 +- TextLocator/NotifyIcon/NotifyIconViewModel.cs | 2 ++ TextLocator/SettingWindow.xaml | 2 +- TextLocator/Util/FileContentUtil.cs | 2 +- 10 files changed, 35 insertions(+), 15 deletions(-) diff --git a/TextLocator/App.xaml.cs b/TextLocator/App.xaml.cs index 6ce6b7f..664a990 100644 --- a/TextLocator/App.xaml.cs +++ b/TextLocator/App.xaml.cs @@ -111,6 +111,9 @@ namespace TextLocator // 文件读取超时时间 AppUtil.WriteValue("AppConfig", "FileReadTimeout", AppConst.FILE_READ_TIMEOUT + ""); + + // 文件内容摘要切割长度 + AppUtil.WriteValue("AppConfig", "FileContentBreviaryCutLength", AppConst.FILE_CONTENT_BREVIARY_CUT_LENGTH + ""); } #endregion diff --git a/TextLocator/Core/AppConst.cs b/TextLocator/Core/AppConst.cs index dd4cb7d..ee402f4 100644 --- a/TextLocator/Core/AppConst.cs +++ b/TextLocator/Core/AppConst.cs @@ -25,7 +25,7 @@ namespace TextLocator.Core /// /// 文件内容摘要切割长度 /// - public static int FILE_CONTENT_BREVIARY_CUT_LENGTH = int.Parse(AppUtil.ReadValue("AppConfig", "FileContentBreviaryCutLength", "60")); + public static int FILE_CONTENT_BREVIARY_CUT_LENGTH = int.Parse(AppUtil.ReadValue("AppConfig", "FileContentBreviaryCutLength", "120")); /// /// 结果列表分页条数 /// diff --git a/TextLocator/Entity/FileInfo.cs b/TextLocator/Entity/FileInfo.cs index c1be0b1..45b2393 100644 --- a/TextLocator/Entity/FileInfo.cs +++ b/TextLocator/Entity/FileInfo.cs @@ -43,7 +43,6 @@ namespace TextLocator.Entity /// private List keywords = new List(); public List Keywords { get => keywords; set => keywords = value; } - /// /// 搜索域 /// diff --git a/TextLocator/FileInfoItem.xaml b/TextLocator/FileInfoItem.xaml index c34611b..658d59e 100644 --- a/TextLocator/FileInfoItem.xaml +++ b/TextLocator/FileInfoItem.xaml @@ -9,7 +9,7 @@ - + diff --git a/TextLocator/FileInfoItem.xaml.cs b/TextLocator/FileInfoItem.xaml.cs index 986e303..e744b3a 100644 --- a/TextLocator/FileInfoItem.xaml.cs +++ b/TextLocator/FileInfoItem.xaml.cs @@ -1,5 +1,6 @@ using log4net; using System; +using System.Threading; using System.Threading.Tasks; using System.Windows.Controls; using System.Windows.Media; @@ -83,19 +84,34 @@ namespace TextLocator FileContentUtil.FlowDocumentHighlight(this.ContentBreviary, Colors.Red, fileInfo.Keywords); } }); - }); + }); + } - // 词频统计 - Task.Factory.StartNew(() => { - string matchCountDetails = IndexCore.GetMatchCountDetails(fileInfo); - Dispatcher.InvokeAsync(() => { + /// + /// 光标在文件类型图标上移动事件(词频统计详情放在这里加载,主要是为了节省列表加载事件) + /// + /// + /// + private void FileTypeIcon_MouseMove(object sender, System.Windows.Input.MouseEventArgs e) + { + if (this.FileTypeIcon.ToolTip == null) + { + var fi = this.Tag as Entity.FileInfo; + Thread t = new Thread(() => { + string matchCountDetails = IndexCore.GetMatchCountDetails(fi); if (!string.IsNullOrWhiteSpace(matchCountDetails)) { - // 关键词匹配次数 - this.FileTypeIcon.ToolTip = matchCountDetails; + Dispatcher.InvokeAsync(() => + { + // 关键词匹配次数 + this.FileTypeIcon.ToolTip = matchCountDetails; + ToolTipService.SetShowDuration(this.FileTypeIcon, 600000); + }); } }); - }); + t.Priority = ThreadPriority.BelowNormal; + t.Start(); + } } } } diff --git a/TextLocator/HotkeyWindow.xaml b/TextLocator/HotkeyWindow.xaml index ff58c20..9e462da 100644 --- a/TextLocator/HotkeyWindow.xaml +++ b/TextLocator/HotkeyWindow.xaml @@ -6,7 +6,7 @@ xmlns:local="clr-namespace:TextLocator" mc:Ignorable="d" x:Name="hotkey" - Title="热键设置" Height="380" Width="520" WindowStartupLocation="CenterScreen" WindowStyle="ToolWindow" ResizeMode="CanMinimize" Icon="/Resource/App.ico" Loaded="Window_Loaded" Closed="Window_Closed" > + Title="热键设置" Height="380" Width="520" WindowStartupLocation="CenterOwner" WindowStyle="ToolWindow" ResizeMode="CanMinimize" Icon="/Resource/App.ico" Loaded="Window_Loaded" Closed="Window_Closed" > diff --git a/TextLocator/MainWindow.xaml b/TextLocator/MainWindow.xaml index 1a37602..2d63d57 100644 --- a/TextLocator/MainWindow.xaml +++ b/TextLocator/MainWindow.xaml @@ -913,7 +913,7 @@ rubyer:ProgressBarHelper.Thickness="100" rubyer:ProgressBarHelper.IsShowPercent="false"/> - + diff --git a/TextLocator/NotifyIcon/NotifyIconViewModel.cs b/TextLocator/NotifyIcon/NotifyIconViewModel.cs index 68cec86..975fada 100644 --- a/TextLocator/NotifyIcon/NotifyIconViewModel.cs +++ b/TextLocator/NotifyIcon/NotifyIconViewModel.cs @@ -46,6 +46,7 @@ namespace TextLocator.NotifyIcon if (!win.IsVisible) { win.Topmost = true; + win.Owner = Application.Current.MainWindow; win.ShowDialog(); } else @@ -72,6 +73,7 @@ namespace TextLocator.NotifyIcon if (!win.IsVisible) { win.Topmost = true; + win.Owner = Application.Current.MainWindow; win.ShowDialog(); } else diff --git a/TextLocator/SettingWindow.xaml b/TextLocator/SettingWindow.xaml index 3b50239..079e29c 100644 --- a/TextLocator/SettingWindow.xaml +++ b/TextLocator/SettingWindow.xaml @@ -7,7 +7,7 @@ xmlns:input="clr-namespace:System.Windows.Input;assembly=PresentationCore" mc:Ignorable="d" x:Name="hotkey" - Title="设置" Height="380" Width="520" WindowStartupLocation="CenterScreen" WindowStyle="ToolWindow" ResizeMode="CanMinimize" Icon="/Resource/App.ico" Loaded="Window_Loaded" Closed="Window_Closed" > + Title="设置" Height="380" Width="520" WindowStartupLocation="CenterOwner" WindowStyle="ToolWindow" ResizeMode="CanMinimize" Icon="/Resource/App.ico" Loaded="Window_Loaded" Closed="Window_Closed" > diff --git a/TextLocator/Util/FileContentUtil.cs b/TextLocator/Util/FileContentUtil.cs index a12ad70..f9ff71f 100644 --- a/TextLocator/Util/FileContentUtil.cs +++ b/TextLocator/Util/FileContentUtil.cs @@ -222,7 +222,7 @@ namespace TextLocator.Util paragraph.Inlines.Add(afterRun); // 分割线 - Run pageRun = new Run(string.Format("\r\n------------------------------------------------------------------------------ {0}\r\n", page)); + Run pageRun = new Run(string.Format("\n------------------------------------------------------------------------------ {0}\n", page)); paragraph.Inlines.Add(pageRun); document.Blocks.Add(paragraph); -- Gitee From cc02159170ceb157ae5af01fa083ddb839cdc13d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A3=8A?= Date: Sat, 21 May 2022 00:02:07 +0800 Subject: [PATCH 12/17] =?UTF-8?q?=E7=A7=BB=E5=8A=A8=E6=94=B9=E4=B8=BA?= =?UTF-8?q?=E7=A7=BB=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TextLocator/FileInfoItem.xaml | 2 +- TextLocator/FileInfoItem.xaml.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/TextLocator/FileInfoItem.xaml b/TextLocator/FileInfoItem.xaml index 658d59e..32d4d9f 100644 --- a/TextLocator/FileInfoItem.xaml +++ b/TextLocator/FileInfoItem.xaml @@ -9,7 +9,7 @@ - + diff --git a/TextLocator/FileInfoItem.xaml.cs b/TextLocator/FileInfoItem.xaml.cs index e744b3a..9de7f6c 100644 --- a/TextLocator/FileInfoItem.xaml.cs +++ b/TextLocator/FileInfoItem.xaml.cs @@ -88,11 +88,11 @@ namespace TextLocator } /// - /// 光标在文件类型图标上移动事件(词频统计详情放在这里加载,主要是为了节省列表加载事件) + /// 光标在文件类型图标边界移入事件(词频统计详情放在这里加载,主要是为了节省列表加载事件) /// /// /// - private void FileTypeIcon_MouseMove(object sender, System.Windows.Input.MouseEventArgs e) + private void FileTypeIcon_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e) { if (this.FileTypeIcon.ToolTip == null) { -- Gitee From 3a0b41397a58479b46051deaffe21acc831c98a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A3=8A?= Date: Sat, 21 May 2022 01:31:30 +0800 Subject: [PATCH 13/17] =?UTF-8?q?=E6=91=98=E8=A6=81=E5=92=8C=E8=AF=8D?= =?UTF-8?q?=E9=A2=91=E7=BB=9F=E8=AE=A1=E6=94=B9=E4=B8=BA=E5=BB=B6=E8=BF=9F?= =?UTF-8?q?=E5=8A=A0=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TextLocator/Entity/FileInfo.cs | 6 +++ TextLocator/FileInfoItem.xaml | 2 +- TextLocator/FileInfoItem.xaml.cs | 71 ++++++++++++++++++++------------ TextLocator/MainWindow.xaml.cs | 4 +- 4 files changed, 55 insertions(+), 28 deletions(-) diff --git a/TextLocator/Entity/FileInfo.cs b/TextLocator/Entity/FileInfo.cs index 45b2393..77d3a9d 100644 --- a/TextLocator/Entity/FileInfo.cs +++ b/TextLocator/Entity/FileInfo.cs @@ -8,6 +8,12 @@ namespace TextLocator.Entity /// public class FileInfo { + // -------- 查询参数携带回传 -------- + /// + /// 列表序号 + /// + public int Index { get; set; } + /// /// 文件类型 /// diff --git a/TextLocator/FileInfoItem.xaml b/TextLocator/FileInfoItem.xaml index 32d4d9f..3c7ad70 100644 --- a/TextLocator/FileInfoItem.xaml +++ b/TextLocator/FileInfoItem.xaml @@ -9,7 +9,7 @@ - + diff --git a/TextLocator/FileInfoItem.xaml.cs b/TextLocator/FileInfoItem.xaml.cs index 9de7f6c..b52e9dc 100644 --- a/TextLocator/FileInfoItem.xaml.cs +++ b/TextLocator/FileInfoItem.xaml.cs @@ -1,9 +1,10 @@ using log4net; using System; -using System.Threading; using System.Threading.Tasks; using System.Windows.Controls; using System.Windows.Media; +using System.Windows.Threading; +using TextLocator.Core; using TextLocator.Index; using TextLocator.Util; @@ -20,8 +21,7 @@ namespace TextLocator /// 文件信息显示条目 /// /// 文件信息 - /// 搜索域 - public FileInfoItem(Entity.FileInfo fileInfo, Enums.SearchRegion searchRegion) + public FileInfoItem(Entity.FileInfo fileInfo) { InitializeComponent(); @@ -29,14 +29,14 @@ namespace TextLocator try { - Refresh(fileInfo, searchRegion); + Refresh(fileInfo); } catch { Dispatcher.InvokeAsync(() => { try { - Refresh(fileInfo, searchRegion); + Refresh(fileInfo); } catch (Exception ex) { @@ -50,8 +50,7 @@ namespace TextLocator /// 刷新数据 /// /// 文件信息 - /// 搜索域 - public void Refresh(Entity.FileInfo fileInfo, Enums.SearchRegion searchRegion) + public void Refresh(Entity.FileInfo fileInfo) { // 根据文件类型显示图标 this.FileTypeIcon.Source = FileUtil.GetFileIcon(fileInfo.FileType); @@ -63,7 +62,7 @@ namespace TextLocator string fileName = fileInfo.FileName; // 显示文件名称 FileContentUtil.FillFlowDocument(this.FileName, fileName.Length > 55 ? fileName.Substring(0, 55) + "..." : fileName, (Brush)new BrushConverter().ConvertFromString("#1A0DAB"), true); - if (searchRegion == Enums.SearchRegion.文件名和内容 || searchRegion == Enums.SearchRegion.仅文件名) + if (fileInfo.SearchRegion == Enums.SearchRegion.文件名和内容 || fileInfo.SearchRegion == Enums.SearchRegion.仅文件名) { FileContentUtil.FlowDocumentHighlight(this.FileName, Colors.Red, fileInfo.Keywords); } @@ -74,17 +73,24 @@ namespace TextLocator // 获取摘要 FileContentUtil.EmptyRichTextDocument(this.ContentBreviary); - Task.Factory.StartNew(() => { + Task.Factory.StartNew(async () => { + await Task.Delay(new Random().Next(50) * fileInfo.Index); string breviary = IndexCore.GetContentBreviary(fileInfo); - Dispatcher.InvokeAsync(() => + await Dispatcher.InvokeAsync(() => { FileContentUtil.FillFlowDocument(this.ContentBreviary, breviary, (Brush)new BrushConverter().ConvertFromString("#545454")); - if (searchRegion == Enums.SearchRegion.文件名和内容 || searchRegion == Enums.SearchRegion.仅文件内容) + if (fileInfo.SearchRegion == Enums.SearchRegion.文件名和内容 || fileInfo.SearchRegion == Enums.SearchRegion.仅文件内容) { FileContentUtil.FlowDocumentHighlight(this.ContentBreviary, Colors.Red, fileInfo.Keywords); } }); - }); + }); + + // 词频统计明细 + Task.Factory.StartNew(async () => { + await Task.Delay(new Random().Next(100) * fileInfo.Index); + LoadMatchCountDetails(fileInfo); + }); } /// @@ -96,21 +102,34 @@ namespace TextLocator { if (this.FileTypeIcon.ToolTip == null) { - var fi = this.Tag as Entity.FileInfo; - Thread t = new Thread(() => { - string matchCountDetails = IndexCore.GetMatchCountDetails(fi); - if (!string.IsNullOrWhiteSpace(matchCountDetails)) + LoadMatchCountDetails(this.Tag as Entity.FileInfo); + } + } + + /// + /// 加载词频统计详情放在这里 + /// + private void LoadMatchCountDetails(Entity.FileInfo fileInfo) + { + string matchCountDetails = IndexCore.GetMatchCountDetails(fileInfo); + if (!string.IsNullOrWhiteSpace(matchCountDetails)) + { + void Load() + { + this.FileTypeIcon.ToolTip = matchCountDetails; + ToolTipService.SetShowDuration(this.FileTypeIcon, 600000); + } + try + { + Load(); + } + catch + { + Dispatcher.InvokeAsync(() => { - Dispatcher.InvokeAsync(() => - { - // 关键词匹配次数 - this.FileTypeIcon.ToolTip = matchCountDetails; - ToolTipService.SetShowDuration(this.FileTypeIcon, 600000); - }); - } - }); - t.Priority = ThreadPriority.BelowNormal; - t.Start(); + Load(); + }); + } } } } diff --git a/TextLocator/MainWindow.xaml.cs b/TextLocator/MainWindow.xaml.cs index c28e1b7..28cad64 100644 --- a/TextLocator/MainWindow.xaml.cs +++ b/TextLocator/MainWindow.xaml.cs @@ -603,15 +603,17 @@ namespace TextLocator } // 3、---- 遍历结果 + int index = 1; foreach (Entity.FileInfo fileInfo in searchResult.Results) { if (_timestamp != timestamp) { return; } + fileInfo.Index = index++; Dispatcher.Invoke(() => { - this.SearchResultList.Items.Add(new FileInfoItem(fileInfo, searchParam.SearchRegion)); + this.SearchResultList.Items.Add(new FileInfoItem(fileInfo)); }); } -- Gitee From dda3b7fb363bbb5728866761ad5afea443c5a959 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A3=8A?= Date: Sat, 21 May 2022 01:32:50 +0800 Subject: [PATCH 14/17] =?UTF-8?q?=E6=B3=A8=E9=87=8A=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TextLocator/Entity/FileInfo.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/TextLocator/Entity/FileInfo.cs b/TextLocator/Entity/FileInfo.cs index 77d3a9d..747d8ad 100644 --- a/TextLocator/Entity/FileInfo.cs +++ b/TextLocator/Entity/FileInfo.cs @@ -8,12 +8,13 @@ namespace TextLocator.Entity /// public class FileInfo { - // -------- 查询参数携带回传 -------- + // -------- 列表索引 -------- /// /// 列表序号 /// public int Index { get; set; } + // -------- 文件信息 -------- /// /// 文件类型 /// -- Gitee From 6ac1537af0dd459a438e5b7ff67ae4b8d509e349 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A3=8A?= Date: Sat, 21 May 2022 02:05:37 +0800 Subject: [PATCH 15/17] =?UTF-8?q?=E5=A2=9E=E5=8A=A0Loading?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TextLocator/MainWindow.xaml.cs | 34 +++++++++++++++++++++++--- TextLocator/Properties/AssemblyInfo.cs | 4 +-- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/TextLocator/MainWindow.xaml.cs b/TextLocator/MainWindow.xaml.cs index 28cad64..d30dc54 100644 --- a/TextLocator/MainWindow.xaml.cs +++ b/TextLocator/MainWindow.xaml.cs @@ -581,6 +581,7 @@ namespace TextLocator } ShowStatus("搜索处理中..."); + ShowSearchLoading(); Thread t = new Thread(() => { @@ -599,6 +600,7 @@ namespace TextLocator if (null == searchResult || searchResult.Results.Count <= 0) { MessageCore.ShowWarning("没有搜到你想要的内容,请更换搜索条件。"); + HideSearchLoading(); return; } @@ -617,17 +619,19 @@ namespace TextLocator }); } - // 4、---- 显示预览列表分页信息 - _viewModel.PreviewPage = string.Format("0/{0}", searchResult.Results.Count); - - // 5、---- 分页总数 + // 4、---- 分页总数、显示预览列表分页信息 _viewModel.TotalCount = searchResult.Total; + _viewModel.PreviewPage = string.Format("0/{0}", searchResult.Results.Count); _viewModel.PreviewSwitchVisibility = searchResult.Total > 0 ? Visibility.Visible : Visibility.Hidden; } catch (Exception ex) { log.Error("搜索错误:" + ex.Message, ex); } + finally + { + HideSearchLoading(); + } }); t.Priority = ThreadPriority.Highest; t.Start(); @@ -1445,8 +1449,30 @@ namespace TextLocator } return keywords; } + #endregion + #region Loading + /// + /// 显示搜索Loading + /// + private void ShowSearchLoading() + { + Dispatcher.Invoke(new Action(() => + { + this._searchLoading.Visibility = Visibility.Visible; + })); + } + /// + /// 隐藏搜索Loading + /// + private void HideSearchLoading() + { + Dispatcher.Invoke(new Action(() => + { + this._searchLoading.Visibility = Visibility.Collapsed; + })); + } #endregion } } diff --git a/TextLocator/Properties/AssemblyInfo.cs b/TextLocator/Properties/AssemblyInfo.cs index 8bf4c6e..7b8c94e 100644 --- a/TextLocator/Properties/AssemblyInfo.cs +++ b/TextLocator/Properties/AssemblyInfo.cs @@ -50,9 +50,9 @@ using System.Windows; //通过使用 "*",如下所示: // [assembly: AssemblyVersion("1.0.*")] // 大版本,强制更新最小版本 -[assembly: AssemblyVersion("2.1.26")] +[assembly: AssemblyVersion("2.1.28")] // 小版本,选择更新版本 -[assembly: AssemblyFileVersion("2.1.26.1")] +[assembly: AssemblyFileVersion("2.1.28.1")] // Version minVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version; // Version version = new Version(FileVersionInfo.GetVersionInfo(System.Windows.Forms.Application.ExecutablePath).ProductVersion); -- Gitee From 90e3534264aa5837303dfcde9def7e6cca743277 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A3=8A?= Date: Sat, 21 May 2022 18:52:13 +0800 Subject: [PATCH 16/17] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=BB=B6=E8=BF=9F?= =?UTF-8?q?=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TextLocator/FileInfoItem.xaml.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/TextLocator/FileInfoItem.xaml.cs b/TextLocator/FileInfoItem.xaml.cs index b52e9dc..c60386c 100644 --- a/TextLocator/FileInfoItem.xaml.cs +++ b/TextLocator/FileInfoItem.xaml.cs @@ -74,7 +74,7 @@ namespace TextLocator // 获取摘要 FileContentUtil.EmptyRichTextDocument(this.ContentBreviary); Task.Factory.StartNew(async () => { - await Task.Delay(new Random().Next(50) * fileInfo.Index); + await Task.Delay((AppConst.MRESULT_LIST_PAGE_SIZE - 10) * fileInfo.Index); string breviary = IndexCore.GetContentBreviary(fileInfo); await Dispatcher.InvokeAsync(() => { @@ -88,7 +88,7 @@ namespace TextLocator // 词频统计明细 Task.Factory.StartNew(async () => { - await Task.Delay(new Random().Next(100) * fileInfo.Index); + await Task.Delay((AppConst.MRESULT_LIST_PAGE_SIZE - 15) * fileInfo.Index); LoadMatchCountDetails(fileInfo); }); } -- Gitee From ba3a84bd986e13fa097e76bfd3b517be0cb1881b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A3=8A?= Date: Sun, 22 May 2022 13:27:22 +0800 Subject: [PATCH 17/17] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TextLocator/FileInfoItem.xaml.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/TextLocator/FileInfoItem.xaml.cs b/TextLocator/FileInfoItem.xaml.cs index c60386c..d9e4721 100644 --- a/TextLocator/FileInfoItem.xaml.cs +++ b/TextLocator/FileInfoItem.xaml.cs @@ -74,7 +74,7 @@ namespace TextLocator // 获取摘要 FileContentUtil.EmptyRichTextDocument(this.ContentBreviary); Task.Factory.StartNew(async () => { - await Task.Delay((AppConst.MRESULT_LIST_PAGE_SIZE - 10) * fileInfo.Index); + await Task.Delay(AppConst.MRESULT_LIST_PAGE_SIZE / 2 * fileInfo.Index); string breviary = IndexCore.GetContentBreviary(fileInfo); await Dispatcher.InvokeAsync(() => { @@ -88,7 +88,7 @@ namespace TextLocator // 词频统计明细 Task.Factory.StartNew(async () => { - await Task.Delay((AppConst.MRESULT_LIST_PAGE_SIZE - 15) * fileInfo.Index); + await Task.Delay(AppConst.MRESULT_LIST_PAGE_SIZE * fileInfo.Index); LoadMatchCountDetails(fileInfo); }); } -- Gitee