From 6023f415a4104ae14a4967f9fcd74bb5c426b201 Mon Sep 17 00:00:00 2001 From: bao-qian Date: Thu, 26 Nov 2015 02:04:44 +0000 Subject: [PATCH] Fix Wox restart 1. refactoring restart 2. delte some windows forms methods 3. using string inteperlation and delete hard coeded new line char should fix #322 --- Plugins/Wox.Plugin.PluginManagement/Main.cs | 21 ++++----- .../Wox.Plugin.PluginManagement.csproj | 2 + Plugins/Wox.Plugin.Sys/Sys.cs | 10 +--- Wox.Core/Plugin/PluginInstaller.cs | 46 ++++++++----------- Wox.Plugin/IPublicAPI.cs | 5 ++ Wox/App.xaml.cs | 4 ++ Wox/CommandArgs/CommandArgsFactory.cs | 4 +- Wox/Helper/SingleInstance.cs | 18 ++++++-- Wox/MainWindow.xaml.cs | 19 +++++--- 9 files changed, 71 insertions(+), 58 deletions(-) diff --git a/Plugins/Wox.Plugin.PluginManagement/Main.cs b/Plugins/Wox.Plugin.PluginManagement/Main.cs index 934caea04a..c11fa86da1 100644 --- a/Plugins/Wox.Plugin.PluginManagement/Main.cs +++ b/Plugins/Wox.Plugin.PluginManagement/Main.cs @@ -205,22 +205,19 @@ namespace Wox.Plugin.PluginManagement private void UnInstallPlugin(PluginMetadata plugin) { - string content = string.Format("Do you want to uninstall following plugin?\r\n\r\nName: {0}\r\nVersion: {1}\r\nAuthor: {2}", plugin.Name, plugin.Version, plugin.Author); + string content = $"Do you want to uninstall following plugin?{Environment.NewLine}{Environment.NewLine}" + + $"Name: {plugin.Name}{Environment.NewLine}" + + $"Version: {plugin.Version}{Environment.NewLine}" + + $"Author: {plugin.Author}"; if (MessageBox.Show(content, "Wox", MessageBoxButton.YesNo) == MessageBoxResult.Yes) { File.Create(Path.Combine(plugin.PluginDirectory, "NeedDelete.txt")).Close(); - if (MessageBox.Show( - "You have uninstalled plugin " + plugin.Name + " successfully.\r\n Restart Wox to take effect?", - "Install plugin", - MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes) + var result = MessageBox.Show($"You have uninstalled plugin {plugin.Name} successfully.{Environment.NewLine}" + + "Restart Wox to take effect?", + "Install plugin", MessageBoxButton.YesNo, MessageBoxImage.Question); + if (result == MessageBoxResult.Yes) { - ProcessStartInfo Info = new ProcessStartInfo(); - Info.Arguments = "/C ping 127.0.0.1 -n 1 && \"" + Assembly.GetExecutingAssembly().Location + "\""; - Info.WindowStyle = ProcessWindowStyle.Hidden; - Info.CreateNoWindow = true; - Info.FileName = "cmd.exe"; - Process.Start(Info); - context.API.CloseApp(); + context.API.RestarApp(); } } } diff --git a/Plugins/Wox.Plugin.PluginManagement/Wox.Plugin.PluginManagement.csproj b/Plugins/Wox.Plugin.PluginManagement/Wox.Plugin.PluginManagement.csproj index f88fb7d916..d4ff899e6c 100644 --- a/Plugins/Wox.Plugin.PluginManagement/Wox.Plugin.PluginManagement.csproj +++ b/Plugins/Wox.Plugin.PluginManagement/Wox.Plugin.PluginManagement.csproj @@ -41,7 +41,9 @@ + + diff --git a/Plugins/Wox.Plugin.Sys/Sys.cs b/Plugins/Wox.Plugin.Sys/Sys.cs index 6c3d76e969..9b2f838e67 100644 --- a/Plugins/Wox.Plugin.Sys/Sys.cs +++ b/Plugins/Wox.Plugin.Sys/Sys.cs @@ -171,14 +171,8 @@ namespace Wox.Plugin.Sys IcoPath = "Images\\restart.png", Action = (c) => { - ProcessStartInfo Info = new ProcessStartInfo(); - Info.Arguments = "/C ping 127.0.0.1 -n 1 && \"" + FormsApplication.ExecutablePath + "\""; - Info.WindowStyle = ProcessWindowStyle.Hidden; - Info.CreateNoWindow = true; - Info.FileName = "cmd.exe"; - Process.Start(Info); - context.API.CloseApp(); - return true; + context.API.RestarApp(); + return false; } }, new Result diff --git a/Wox.Core/Plugin/PluginInstaller.cs b/Wox.Core/Plugin/PluginInstaller.cs index ce6318a81b..f20f2fd079 100644 --- a/Wox.Core/Plugin/PluginInstaller.cs +++ b/Wox.Core/Plugin/PluginInstaller.cs @@ -1,10 +1,10 @@ using System; using System.Diagnostics; using System.IO; -using System.Windows.Forms; using ICSharpCode.SharpZipLib.Zip; using Newtonsoft.Json; using Wox.Plugin; +using System.Windows; namespace Wox.Core.Plugin { @@ -47,22 +47,24 @@ namespace Wox.Core.Plugin .Replace("*", "_") .Replace("|", "_") + "-" + Guid.NewGuid(); - string newPluginPath = Path.Combine(pluginFolerPath,newPluginName); - string content = string.Format( - "Do you want to install following plugin?\r\n\r\nName: {0}\r\nVersion: {1}\r\nAuthor: {2}", - plugin.Name, plugin.Version, plugin.Author); + string newPluginPath = Path.Combine(pluginFolerPath, newPluginName); + string content = $"Do you want to install following plugin?{Environment.NewLine}{Environment.NewLine}" + + $"Name: {plugin.Name}{Environment.NewLine}" + + $"Version: {plugin.Version}{Environment.NewLine}" + + $"Author: {plugin.Author}"; PluginPair existingPlugin = PluginManager.GetPluginForId(plugin.ID); if (existingPlugin != null) { - content = string.Format( - "Do you want to update following plugin?\r\n\r\nName: {0}\r\nOld Version: {1}\r\nNew Version: {2}\r\nAuthor: {3}", - plugin.Name, existingPlugin.Metadata.Version, plugin.Version, plugin.Author); + content = $"Do you want to update following plugin?{Environment.NewLine}{Environment.NewLine}" + + $"Name: {plugin.Name}{Environment.NewLine}" + + $"Old Version: {existingPlugin.Metadata.Version}" + + $"{Environment.NewLine}New Version: {plugin.Version}" + + $"{Environment.NewLine}Author: {plugin.Author}"; } - DialogResult result = System.Windows.Forms.MessageBox.Show(content, "Install plugin", MessageBoxButtons.YesNo, - MessageBoxIcon.Question); - if (result == DialogResult.Yes) + var result = MessageBox.Show(content, "Install plugin", MessageBoxButton.YesNo, MessageBoxImage.Question); + if (result == MessageBoxResult.Yes) { if (existingPlugin != null && Directory.Exists(existingPlugin.Metadata.PluginDirectory)) { @@ -81,17 +83,11 @@ namespace Wox.Core.Plugin //{ // Plugins.Init(); //} - if (MessageBox.Show("You have installed plugin " + plugin.Name + " successfully.\r\n Restart Wox to take effect?", "Install plugin", - MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + if (MessageBox.Show($"You have installed plugin {plugin.Name} successfully.{Environment.NewLine}" + + " Restart Wox to take effect?", + "Install plugin", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes) { - ProcessStartInfo Info = new ProcessStartInfo(); - Info.Arguments = "/C ping 127.0.0.1 -n 1 && \"" + - System.Windows.Forms.Application.ExecutablePath + "\""; - Info.WindowStyle = ProcessWindowStyle.Hidden; - Info.CreateNoWindow = true; - Info.FileName = "cmd.exe"; - Process.Start(Info); - PluginManager.API.CloseApp(); + PluginManager.API.RestarApp(); } } } @@ -114,7 +110,7 @@ namespace Wox.Core.Plugin } catch (System.Exception) { - string error = string.Format("Parse plugin config {0} failed: json format is not valid", configPath); + string error = $"Parse plugin config {configPath} failed: json format is not valid"; #if (DEBUG) { throw new System.Exception(error); @@ -126,8 +122,7 @@ namespace Wox.Core.Plugin if (!AllowedLanguage.IsAllowed(metadata.Language)) { - string error = string.Format("Parse plugin config {0} failed: invalid language {1}", configPath, - metadata.Language); + string error = $"Parse plugin config {configPath} failed: invalid language {metadata.Language}"; #if (DEBUG) { throw new System.Exception(error); @@ -137,8 +132,7 @@ namespace Wox.Core.Plugin } if (!File.Exists(metadata.ExecuteFilePath)) { - string error = string.Format("Parse plugin config {0} failed: ExecuteFile {1} didn't exist", configPath, - metadata.ExecuteFilePath); + string error = $"Parse plugin config {configPath} failed: ExecuteFile {metadata.ExecuteFilePath} didn't exist"; #if (DEBUG) { throw new System.Exception(error); diff --git a/Wox.Plugin/IPublicAPI.cs b/Wox.Plugin/IPublicAPI.cs index e4ebb3f96b..1672ec9919 100644 --- a/Wox.Plugin/IPublicAPI.cs +++ b/Wox.Plugin/IPublicAPI.cs @@ -51,6 +51,11 @@ namespace Wox.Plugin /// void CloseApp(); + /// + /// Restart Wox + /// + void RestarApp(); + /// /// Hide Wox /// diff --git a/Wox/App.xaml.cs b/Wox/App.xaml.cs index 4e87e6a4db..de80657c37 100644 --- a/Wox/App.xaml.cs +++ b/Wox/App.xaml.cs @@ -52,6 +52,10 @@ namespace Wox public bool OnActivate(IList args) { + if (args.Count > 0 && args[0] == SingleInstance.Restart) + { + Window.CloseApp(); + } CommandArgsFactory.Execute(args); return true; } diff --git a/Wox/CommandArgs/CommandArgsFactory.cs b/Wox/CommandArgs/CommandArgsFactory.cs index 7f2fdfc98b..e6e4a91b74 100644 --- a/Wox/CommandArgs/CommandArgsFactory.cs +++ b/Wox/CommandArgs/CommandArgsFactory.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; +using Wox.Helper; namespace Wox.CommandArgs { @@ -20,7 +21,8 @@ namespace Wox.CommandArgs public static void Execute(IList args) { - if (args.Count > 0) + // todo restart command line args? + if (args.Count > 0 && args[0] != SingleInstance.Restart) { string command = args[0]; ICommandArg cmd = commandArgs.FirstOrDefault(o => o.Command.ToLower() == command); diff --git a/Wox/Helper/SingleInstance.cs b/Wox/Helper/SingleInstance.cs index c01d7e1085..bd68270ffa 100644 --- a/Wox/Helper/SingleInstance.cs +++ b/Wox/Helper/SingleInstance.cs @@ -212,6 +212,8 @@ namespace Wox.Helper where TApplication: Application , ISingleInstanceApp { + public const string Restart = "Restart"; + #region Private Fields /// @@ -273,6 +275,8 @@ namespace Wox.Helper public static bool InitializeAsFirstInstance( string uniqueName ) { commandLineArgs = GetCommandLineArgs(uniqueName); + //remove execute path itself + commandLineArgs.RemoveAt(0); // Build unique application Id and the IPC channel name. string applicationIdentifier = uniqueName + Environment.UserName; @@ -285,13 +289,20 @@ namespace Wox.Helper if (firstInstance) { CreateRemoteService(channelName); + return true; + } + else if (commandLineArgs.Count > 0 && commandLineArgs[0] == Restart) + { + SignalFirstInstance(channelName, commandLineArgs); + singleInstanceMutex = new Mutex(true, applicationIdentifier); + CreateRemoteService(channelName); + return true; } else { SignalFirstInstance(channelName, commandLineArgs); + return false; } - - return firstInstance; } /// @@ -442,8 +453,7 @@ namespace Wox.Helper { return; } - //remove execute path itself - args.RemoveAt(0); + ((TApplication)Application.Current).OnActivate(args); } diff --git a/Wox/MainWindow.xaml.cs b/Wox/MainWindow.xaml.cs index 8be83c06a7..f25e51e822 100644 --- a/Wox/MainWindow.xaml.cs +++ b/Wox/MainWindow.xaml.cs @@ -10,7 +10,6 @@ using System.Reflection; using System.Threading; using System.Windows; using System.Windows.Controls; -using System.Windows.Forms; using System.Windows.Input; using System.Windows.Media.Animation; using NHotkey; @@ -26,14 +25,10 @@ using Wox.Infrastructure.Hotkey; using Wox.Plugin; using Wox.Storage; using ContextMenu = System.Windows.Forms.ContextMenu; -using DataFormats = System.Windows.DataFormats; -using DragEventArgs = System.Windows.DragEventArgs; -using IDataObject = System.Windows.IDataObject; -using KeyEventArgs = System.Windows.Input.KeyEventArgs; +using NotifyIcon = System.Windows.Forms.NotifyIcon; +using Screen = System.Windows.Forms.Screen; using MenuItem = System.Windows.Forms.MenuItem; -using MessageBox = System.Windows.MessageBox; using Stopwatch = Wox.Infrastructure.Stopwatch; -using ToolTip = System.Windows.Controls.ToolTip; namespace Wox { @@ -93,6 +88,16 @@ namespace Wox })); } + public void RestarApp() + { + ProcessStartInfo info = new ProcessStartInfo + { + FileName = Application.ResourceAssembly.Location, + Arguments = SingleInstance.Restart + }; + Process.Start(info); + } + public void HideApp() { Dispatcher.Invoke(new Action(HideWox));