diff --git a/QuickLook/App.xaml.cs b/QuickLook/App.xaml.cs index 6e101a5..e5e25e4 100644 --- a/QuickLook/App.xaml.cs +++ b/QuickLook/App.xaml.cs @@ -15,7 +15,7 @@ namespace QuickLook public static readonly string AppFullPath = Assembly.GetExecutingAssembly().Location; public static readonly string AppPath = Path.GetDirectoryName(AppFullPath); - private Mutex isRunning; + private Mutex _isRunning; protected override void OnStartup(StartupEventArgs e) { @@ -30,6 +30,7 @@ namespace QuickLook { EnsureSingleInstance(); + TrayIcon.GetInstance(); if (!e.Args.Contains("/autorun")) TrayIcon.GetInstance().ShowNotification("", "QuickLook is running in the background."); @@ -40,8 +41,8 @@ namespace QuickLook private void EnsureSingleInstance() { - bool isNew = false; - isRunning = new Mutex(true, "QuickLook.App", out isNew); + var isNew = false; + _isRunning = new Mutex(true, "QuickLook.App", out isNew); if (!isNew) { MessageBox.Show("QuickLook is already running in the background."); diff --git a/QuickLook/Helpers/AutoStartupHelper.cs b/QuickLook/Helpers/AutoStartupHelper.cs new file mode 100644 index 0000000..63c35b4 --- /dev/null +++ b/QuickLook/Helpers/AutoStartupHelper.cs @@ -0,0 +1,45 @@ +using System; +using System.IO; +using System.Runtime.InteropServices.ComTypes; +using QuickLook.NativeMethods; + +namespace QuickLook.Helpers +{ + internal static class AutoStartupHelper + { + private static readonly string _startupFullPath = Path.Combine( + Environment.GetFolderPath(Environment.SpecialFolder.Startup), + Path.ChangeExtension(Path.GetFileName(App.AppFullPath), ".lnk")); + + internal static void CreateAutorunShortcut() + { + try + { + var link = (IShellLink) new ShellLink(); + + link.SetPath(App.AppFullPath); + link.SetWorkingDirectory(App.AppPath); + link.SetIconLocation(App.AppFullPath, 0); + + link.SetArguments($"/autorun"); // silent + + var file = (IPersistFile) link; + file.Save(_startupFullPath, false); + } + catch (Exception) + { + TrayIcon.GetInstance().ShowNotification("", "Failed to add QuickLook to Startup folder."); + } + } + + internal static void RemoveAutorunShortcut() + { + File.Delete(_startupFullPath); + } + + internal static bool IsAutorun() + { + return File.Exists(_startupFullPath); + } + } +} \ No newline at end of file diff --git a/QuickLook/NativeMethods/ShellLink.cs b/QuickLook/NativeMethods/ShellLink.cs new file mode 100644 index 0000000..017e144 --- /dev/null +++ b/QuickLook/NativeMethods/ShellLink.cs @@ -0,0 +1,45 @@ +using System; +using System.Runtime.InteropServices; +using System.Text; + +namespace QuickLook.NativeMethods +{ + [ComImport] + [Guid("00021401-0000-0000-C000-000000000046")] + internal class ShellLink + { + } + + [ComImport] + [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] + [Guid("000214F9-0000-0000-C000-000000000046")] + internal interface IShellLink + { + void GetPath([Out] [MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile, + int cchMaxPath, + out IntPtr pfd, + int fFlags); + + void GetIDList(out IntPtr ppidl); + void SetIDList(IntPtr pidl); + void GetDescription([Out] [MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszName, int cchMaxName); + void SetDescription([MarshalAs(UnmanagedType.LPWStr)] string pszName); + void GetWorkingDirectory([Out] [MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszDir, int cchMaxPath); + void SetWorkingDirectory([MarshalAs(UnmanagedType.LPWStr)] string pszDir); + void GetArguments([Out] [MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszArgs, int cchMaxPath); + void SetArguments([MarshalAs(UnmanagedType.LPWStr)] string pszArgs); + void GetHotkey(out short pwHotkey); + void SetHotkey(short wHotkey); + void GetShowCmd(out int piShowCmd); + void SetShowCmd(int iShowCmd); + + void GetIconLocation([Out] [MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszIconPath, + int cchIconPath, + out int piIcon); + + void SetIconLocation([MarshalAs(UnmanagedType.LPWStr)] string pszIconPath, int iIcon); + void SetRelativePath([MarshalAs(UnmanagedType.LPWStr)] string pszPathRel, int dwReserved); + void Resolve(IntPtr hwnd, int fFlags); + void SetPath([MarshalAs(UnmanagedType.LPWStr)] string pszFile); + } +} \ No newline at end of file diff --git a/QuickLook/Plugin/ContextObject.cs b/QuickLook/Plugin/ContextObject.cs index c476d09..b161293 100644 --- a/QuickLook/Plugin/ContextObject.cs +++ b/QuickLook/Plugin/ContextObject.cs @@ -30,17 +30,6 @@ namespace QuickLook.Plugin } } - /// - /// Show a notification balloon. - /// - /// Title of the notification. - /// The content. - /// Is this indicates a error? - public void ShowNotification(string title, string content, bool isError = false) - { - TrayIcon.GetInstance().ShowNotification(title, content, isError); - } - /// /// Get or set the viewer content control. /// @@ -88,6 +77,17 @@ namespace QuickLook.Plugin public event PropertyChangedEventHandler PropertyChanged; + /// + /// Show a notification balloon. + /// + /// Title of the notification. + /// The content. + /// Is this indicates a error? + public void ShowNotification(string title, string content, bool isError = false) + { + TrayIcon.GetInstance().ShowNotification(title, content, isError); + } + /// /// Set the size of viewer window and shrink to fit (to screen resolution). /// The window can take maximum (maxRatio*resolution) space. diff --git a/QuickLook/Plugin/InfoPanel/InfoPanel.xaml.cs b/QuickLook/Plugin/InfoPanel/InfoPanel.xaml.cs index f682509..dcb3049 100644 --- a/QuickLook/Plugin/InfoPanel/InfoPanel.xaml.cs +++ b/QuickLook/Plugin/InfoPanel/InfoPanel.xaml.cs @@ -2,7 +2,6 @@ using System.IO; using System.Threading.Tasks; using System.Windows.Controls; -using System.Windows.Input; namespace QuickLook.Plugin.InfoPanel { diff --git a/QuickLook/PluginManager.cs b/QuickLook/PluginManager.cs index 4df8f13..e1d1ff1 100644 --- a/QuickLook/PluginManager.cs +++ b/QuickLook/PluginManager.cs @@ -35,7 +35,7 @@ namespace QuickLook var matched = GetInstance() .LoadedPlugins.FirstOrDefault(plugin => { - bool can = false; + var can = false; try { can = plugin.CreateInstance().CanHandle(path); diff --git a/QuickLook/Properties/AssemblyInfo.cs b/QuickLook/Properties/AssemblyInfo.cs index 64e51b7..002b4b3 100644 --- a/QuickLook/Properties/AssemblyInfo.cs +++ b/QuickLook/Properties/AssemblyInfo.cs @@ -8,9 +8,9 @@ using System.Windows; [assembly: AssemblyTitle("QuickLook")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] +[assembly: AssemblyCompany("Paddy Xu")] [assembly: AssemblyProduct("QuickLook")] -[assembly: AssemblyCopyright("Copyright © 2017")] +[assembly: AssemblyCopyright("Copyright © 2017")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] diff --git a/QuickLook/QuickLook.csproj b/QuickLook/QuickLook.csproj index 6604217..00ce624 100644 --- a/QuickLook/QuickLook.csproj +++ b/QuickLook/QuickLook.csproj @@ -78,10 +78,12 @@ MSBuild:Compile Designer + + diff --git a/QuickLook/TrayIcon.cs b/QuickLook/TrayIcon.cs index 95e2146..a0963bd 100644 --- a/QuickLook/TrayIcon.cs +++ b/QuickLook/TrayIcon.cs @@ -1,10 +1,8 @@ -using System; -using System.Collections.Generic; -using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.Diagnostics; using System.Windows.Forms; +using QuickLook.Helpers; +using QuickLook.Properties; +using Application = System.Windows.Application; namespace QuickLook { @@ -12,15 +10,33 @@ namespace QuickLook { private static TrayIcon _instance; - private NotifyIcon _icon; + private readonly NotifyIcon _icon; - internal TrayIcon() + private readonly MenuItem _itemAutorun = + new MenuItem("Run at &Startup", (sender, e) => + { + if (AutoStartupHelper.IsAutorun()) + AutoStartupHelper.RemoveAutorunShortcut(); + else + AutoStartupHelper.CreateAutorunShortcut(); + }); + + private TrayIcon() { _icon = new NotifyIcon { - Icon = Properties.Resources.app_white, - Visible = true + Icon = Resources.app_white, + Visible = true, + ContextMenu = new ContextMenu(new[] + { + new MenuItem("Check for &Updates...", + (sender, e) => Process.Start(@"http://pooi.moe/QuickLook/")), + _itemAutorun, + new MenuItem("&Quit", (sender, e) => Application.Current.Shutdown()) + }) }; + + _icon.ContextMenu.Popup += (sender, e) => { _itemAutorun.Checked = AutoStartupHelper.IsAutorun(); }; } public void ShowNotification(string title, string content, bool isError = false) @@ -33,4 +49,4 @@ namespace QuickLook return _instance ?? (_instance = new TrayIcon()); } } -} +} \ No newline at end of file