mirror of
https://github.com/QL-Win/QuickLook.git
synced 2025-09-13 11:09:06 +00:00
Add tray icon; autorun
This commit is contained in:
@@ -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.");
|
||||
|
45
QuickLook/Helpers/AutoStartupHelper.cs
Normal file
45
QuickLook/Helpers/AutoStartupHelper.cs
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
45
QuickLook/NativeMethods/ShellLink.cs
Normal file
45
QuickLook/NativeMethods/ShellLink.cs
Normal file
@@ -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);
|
||||
}
|
||||
}
|
@@ -30,17 +30,6 @@ namespace QuickLook.Plugin
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Show a notification balloon.
|
||||
/// </summary>
|
||||
/// <param name="title">Title of the notification.</param>
|
||||
/// <param name="content">The content.</param>
|
||||
/// <param name="isError">Is this indicates a error?</param>
|
||||
public void ShowNotification(string title, string content, bool isError = false)
|
||||
{
|
||||
TrayIcon.GetInstance().ShowNotification(title, content, isError);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get or set the viewer content control.
|
||||
/// </summary>
|
||||
@@ -88,6 +77,17 @@ namespace QuickLook.Plugin
|
||||
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
|
||||
/// <summary>
|
||||
/// Show a notification balloon.
|
||||
/// </summary>
|
||||
/// <param name="title">Title of the notification.</param>
|
||||
/// <param name="content">The content.</param>
|
||||
/// <param name="isError">Is this indicates a error?</param>
|
||||
public void ShowNotification(string title, string content, bool isError = false)
|
||||
{
|
||||
TrayIcon.GetInstance().ShowNotification(title, content, isError);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set the size of viewer window and shrink to fit (to screen resolution).
|
||||
/// The window can take maximum (maxRatio*resolution) space.
|
||||
|
@@ -2,7 +2,6 @@
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace QuickLook.Plugin.InfoPanel
|
||||
{
|
||||
|
@@ -35,7 +35,7 @@ namespace QuickLook
|
||||
var matched = GetInstance()
|
||||
.LoadedPlugins.FirstOrDefault(plugin =>
|
||||
{
|
||||
bool can = false;
|
||||
var can = false;
|
||||
try
|
||||
{
|
||||
can = plugin.CreateInstance<IViewer>().CanHandle(path);
|
||||
|
@@ -8,7 +8,7 @@ using System.Windows;
|
||||
[assembly: AssemblyTitle("QuickLook")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyCompany("Paddy Xu")]
|
||||
[assembly: AssemblyProduct("QuickLook")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2017")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
|
@@ -78,10 +78,12 @@
|
||||
<Generator>MSBuild:Compile</Generator>
|
||||
<SubType>Designer</SubType>
|
||||
</ApplicationDefinition>
|
||||
<Compile Include="Helpers\AutoStartupHelper.cs" />
|
||||
<Compile Include="Controls\BackgroundVisualHost.cs" />
|
||||
<Compile Include="Controls\BusyDecorator.cs" />
|
||||
<Compile Include="Controls\VisualTargetPresentationSource.cs" />
|
||||
<Compile Include="ExtensionMethods\TypeExtensions.cs" />
|
||||
<Compile Include="NativeMethods\ShellLink.cs" />
|
||||
<Compile Include="PluginManager.cs" />
|
||||
<Compile Include="Plugin\InfoPanel\DpiHelpers.cs" />
|
||||
<Compile Include="Plugin\InfoPanel\Extensions.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)
|
||||
|
Reference in New Issue
Block a user