mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-02-24 04:00:02 +01:00
Add one instance support.
This commit is contained in:
@@ -111,7 +111,7 @@ namespace WinAlfred.WorkflowInstaller
|
||||
string winalfred = AppDomain.CurrentDomain.BaseDirectory + "WinAlfred.exe";
|
||||
if (File.Exists(winalfred))
|
||||
{
|
||||
Process.Start(winalfred, "refreshWorkflows");
|
||||
Process.Start(winalfred, "reloadWorkflows");
|
||||
MessageBox.Show("You have installed workflow " + plugin.Name + " successfully.");
|
||||
}
|
||||
else
|
||||
|
||||
@@ -1,28 +1,68 @@
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Windows;
|
||||
using Microsoft.VisualBasic.ApplicationServices;
|
||||
using StartupEventArgs = System.Windows.StartupEventArgs;
|
||||
|
||||
namespace WinAlfred
|
||||
{
|
||||
/// <summary>
|
||||
/// App.xaml 的交互逻辑
|
||||
/// </summary>
|
||||
public static class EntryPoint
|
||||
{
|
||||
[STAThread]
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
SingleInstanceManager manager = new SingleInstanceManager();
|
||||
manager.Run(args);
|
||||
}
|
||||
}
|
||||
|
||||
// Using VB bits to detect single instances and process accordingly:
|
||||
// * OnStartup is fired when the first instance loads
|
||||
// * OnStartupNextInstance is fired when the application is re-run again
|
||||
// NOTE: it is redirected to this instance thanks to IsSingleInstance
|
||||
public class SingleInstanceManager : WindowsFormsApplicationBase
|
||||
{
|
||||
App app;
|
||||
|
||||
public SingleInstanceManager()
|
||||
{
|
||||
this.IsSingleInstance = true;
|
||||
}
|
||||
|
||||
protected override bool OnStartup(Microsoft.VisualBasic.ApplicationServices.StartupEventArgs e)
|
||||
{
|
||||
// First time app is launched
|
||||
app = new App();
|
||||
app.InitializeComponent();
|
||||
app.Run();
|
||||
return true;
|
||||
}
|
||||
|
||||
protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs)
|
||||
{
|
||||
// Subsequent launches
|
||||
base.OnStartupNextInstance(eventArgs);
|
||||
app.Activate(eventArgs.CommandLine.ToArray());
|
||||
}
|
||||
}
|
||||
|
||||
public partial class App : Application
|
||||
{
|
||||
private MainWindow window;
|
||||
|
||||
protected override void OnStartup(StartupEventArgs e)
|
||||
{
|
||||
base.OnStartup(e);
|
||||
bool startupFlag;
|
||||
Mutex mutex = new Mutex(true, "WinAlfred", out startupFlag);
|
||||
if (!startupFlag)
|
||||
{
|
||||
Environment.Exit(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
MainWindow mainWindow = new MainWindow();
|
||||
mainWindow.Show();
|
||||
}
|
||||
|
||||
window = new MainWindow();
|
||||
window.ShowApp(e.Args);
|
||||
}
|
||||
|
||||
public void Activate(string[] commandLine)
|
||||
{
|
||||
window.ShowApp(commandLine);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,9 +59,9 @@ namespace WinAlfred
|
||||
private void InitialTray()
|
||||
{
|
||||
notifyIcon = new NotifyIcon { Text = "WinAlfred", Icon = Properties.Resources.app, Visible = true };
|
||||
notifyIcon.Click += (o, e) => ShowWinAlfred();
|
||||
notifyIcon.Click += (o, e) => ShowWinAlfred(null);
|
||||
System.Windows.Forms.MenuItem open = new System.Windows.Forms.MenuItem("Open");
|
||||
open.Click += (o, e) => ShowWinAlfred();
|
||||
open.Click += (o, e) => ShowWinAlfred(null);
|
||||
System.Windows.Forms.MenuItem exit = new System.Windows.Forms.MenuItem("Exit");
|
||||
exit.Click += (o, e) => CloseApp();
|
||||
System.Windows.Forms.MenuItem[] childen = { open, exit };
|
||||
@@ -78,7 +78,7 @@ namespace WinAlfred
|
||||
{
|
||||
if (!IsVisible)
|
||||
{
|
||||
ShowWinAlfred();
|
||||
ShowWinAlfred(null);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -132,8 +132,26 @@ namespace WinAlfred
|
||||
Hide();
|
||||
}
|
||||
|
||||
private void ShowWinAlfred()
|
||||
private void ShowWinAlfred(string[] args)
|
||||
{
|
||||
if (args != null && args.Length > 0)
|
||||
{
|
||||
switch (args[0])
|
||||
{
|
||||
case "reloadWorkflows":
|
||||
Plugins.Init(this);
|
||||
break;
|
||||
|
||||
case "query":
|
||||
if (args.Length > 1)
|
||||
{
|
||||
string query = args[1];
|
||||
tbQuery.Text = query;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Show();
|
||||
Activate();
|
||||
tbQuery.Focus();
|
||||
@@ -166,7 +184,6 @@ namespace WinAlfred
|
||||
InitialTray();
|
||||
selectedRecords.LoadSelectedRecords();
|
||||
SetAutoStart(true);
|
||||
ShowWinAlfred();
|
||||
//var engine = new Jurassic.ScriptEngine();
|
||||
//MessageBox.Show(engine.Evaluate("5 * 10 + 2").ToString());
|
||||
}
|
||||
@@ -244,9 +261,9 @@ namespace WinAlfred
|
||||
HideWinAlfred();
|
||||
}
|
||||
|
||||
public void ShowApp()
|
||||
public void ShowApp(string[] args)
|
||||
{
|
||||
ShowWinAlfred();
|
||||
ShowWinAlfred(args);
|
||||
}
|
||||
|
||||
public void ShowMsg(string title, string subTitle, string iconPath)
|
||||
|
||||
@@ -14,16 +14,11 @@ namespace WinAlfred.PluginLoader
|
||||
private static string PluginPath = "Plugins";
|
||||
private static string PluginConfigName = "plugin.ini";
|
||||
protected static List<PluginMetadata> pluginMetadatas = new List<PluginMetadata>();
|
||||
|
||||
public abstract List<PluginPair> LoadPlugin();
|
||||
|
||||
static BasePluginLoader()
|
||||
{
|
||||
ParsePlugins();
|
||||
}
|
||||
|
||||
private static void ParsePlugins()
|
||||
public static void ParsePluginsConfig()
|
||||
{
|
||||
pluginMetadatas.Clear();
|
||||
ParseSystemPlugins();
|
||||
ParseThirdPartyPlugins();
|
||||
}
|
||||
@@ -126,7 +121,7 @@ namespace WinAlfred.PluginLoader
|
||||
///// <returns></returns>
|
||||
//private static PluginMetadata filterPythonMetadata(PluginMetadata metadata)
|
||||
//{
|
||||
|
||||
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using Microsoft.CSharp;
|
||||
using WinAlfred.Plugin;
|
||||
|
||||
namespace WinAlfred.PluginLoader
|
||||
@@ -14,23 +15,22 @@ namespace WinAlfred.PluginLoader
|
||||
public static void Init(MainWindow window)
|
||||
{
|
||||
plugins.Clear();
|
||||
BasePluginLoader.ParsePluginsConfig();
|
||||
|
||||
plugins.AddRange(new PythonPluginLoader().LoadPlugin());
|
||||
plugins.AddRange(new CSharpPluginLoader().LoadPlugin());
|
||||
foreach (IPlugin plugin in plugins.Select(pluginPair => pluginPair.Plugin))
|
||||
{
|
||||
IPlugin plugin1 = plugin;
|
||||
ThreadPool.QueueUserWorkItem(o =>
|
||||
ThreadPool.QueueUserWorkItem(o => plugin1.Init(new PluginInitContext()
|
||||
{
|
||||
plugin1.Init(new PluginInitContext()
|
||||
{
|
||||
Plugins = plugins,
|
||||
ChangeQuery = s => window.ChangeQuery(s),
|
||||
CloseApp = window.CloseApp,
|
||||
HideApp = window.HideApp,
|
||||
ShowApp = window.ShowApp,
|
||||
ShowMsg = (title, subTitle, iconPath) => window.ShowMsg(title, subTitle, iconPath)
|
||||
});
|
||||
});
|
||||
Plugins = plugins,
|
||||
ChangeQuery = s => window.ChangeQuery(s),
|
||||
CloseApp = window.CloseApp,
|
||||
HideApp = window.HideApp,
|
||||
ShowApp = () => window.ShowApp(null),
|
||||
ShowMsg = (title, subTitle, iconPath) => window.ShowMsg(title, subTitle, iconPath)
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -9,12 +9,6 @@ namespace WinAlfred.PluginLoader
|
||||
{
|
||||
public class PythonPluginLoader : BasePluginLoader
|
||||
{
|
||||
|
||||
static PythonPluginLoader()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override List<PluginPair> LoadPlugin()
|
||||
{
|
||||
List<PluginPair> plugins = new List<PluginPair>();
|
||||
|
||||
@@ -16,6 +16,21 @@
|
||||
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
|
||||
<RestorePackages>true</RestorePackages>
|
||||
<TargetFrameworkProfile />
|
||||
<PublishUrl>发布\</PublishUrl>
|
||||
<Install>true</Install>
|
||||
<InstallFrom>Disk</InstallFrom>
|
||||
<UpdateEnabled>false</UpdateEnabled>
|
||||
<UpdateMode>Foreground</UpdateMode>
|
||||
<UpdateInterval>7</UpdateInterval>
|
||||
<UpdateIntervalUnits>Days</UpdateIntervalUnits>
|
||||
<UpdatePeriodically>false</UpdatePeriodically>
|
||||
<UpdateRequired>false</UpdateRequired>
|
||||
<MapFileExtensions>true</MapFileExtensions>
|
||||
<ApplicationRevision>0</ApplicationRevision>
|
||||
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
|
||||
<IsWebBootstrapper>false</IsWebBootstrapper>
|
||||
<UseApplicationTrust>false</UseApplicationTrust>
|
||||
<BootstrapperEnabled>true</BootstrapperEnabled>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
@@ -57,11 +72,15 @@
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<StartupObject>WinAlfred.EntryPoint</StartupObject>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="Accessibility" />
|
||||
<Reference Include="log4net">
|
||||
<HintPath>..\packages\log4net.2.0.3\lib\net35-full\log4net.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Microsoft.VisualBasic" />
|
||||
<Reference Include="Newtonsoft.Json">
|
||||
<HintPath>..\packages\Newtonsoft.Json.5.0.8\lib\net35\Newtonsoft.Json.dll</HintPath>
|
||||
</Reference>
|
||||
@@ -198,6 +217,24 @@
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</Resource>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<WCFMetadata Include="Service References\" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Service Include="{508349B6-6B84-4DF5-91F0-309BEEBAD82D}" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<BootstrapperPackage Include="Microsoft.Net.Client.3.5">
|
||||
<Visible>False</Visible>
|
||||
<ProductName>.NET Framework 3.5 SP1 Client Profile</ProductName>
|
||||
<Install>false</Install>
|
||||
</BootstrapperPackage>
|
||||
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
|
||||
<Visible>False</Visible>
|
||||
<ProductName>.NET Framework 3.5 SP1</ProductName>
|
||||
<Install>true</Install>
|
||||
</BootstrapperPackage>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
|
||||
<PropertyGroup>
|
||||
|
||||
Reference in New Issue
Block a user