Skip to content

Commit

Permalink
reponse to arrow keys
Browse files Browse the repository at this point in the history
  • Loading branch information
xupefei committed May 16, 2017
1 parent 13d6edd commit 892b9bf
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ private void ShowErrorNotification(object sender, MediaErrorRoutedEventArgs e)
mediaElement.Stop();

_context.ShowNotification("", "An error occurred while loading the video.");

throw new Exception();
}

public void LoadAndPlay(string path)
Expand Down
16 changes: 10 additions & 6 deletions QuickLook/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ public partial class App : Application

protected override void OnStartup(StartupEventArgs e)
{
AppDomain.CurrentDomain.UnhandledException +=
(sender, args) => MessageBox.Show(((Exception) args.ExceptionObject).Message + Environment.NewLine +
((Exception) args.ExceptionObject).StackTrace);
AppDomain.CurrentDomain.UnhandledException += (sender, args) =>
{
MessageBox.Show(((Exception) args.ExceptionObject).Message + Environment.NewLine +
((Exception) args.ExceptionObject).StackTrace);

Current.Shutdown();
};

base.OnStartup(e);
}
Expand Down Expand Up @@ -68,9 +72,9 @@ private void RunAsListener(StartupEventArgs e)

PidHelper.WritePid();

TrayIcon.GetInstance();
TrayIconManager.GetInstance();
if (!e.Args.Contains("/autorun"))
TrayIcon.GetInstance().ShowNotification("", "QuickLook is running in the background.");
TrayIconManager.GetInstance().ShowNotification("", "QuickLook is running in the background.");

PluginManager.GetInstance();

Expand All @@ -79,7 +83,7 @@ private void RunAsListener(StartupEventArgs e)

private void App_OnExit(object sender, ExitEventArgs e)
{
TrayIcon.GetInstance().Dispose();
TrayIconManager.GetInstance().Dispose();
BackgroundListener.GetInstance().Dispose();

PidHelper.DeletePid();
Expand Down
6 changes: 5 additions & 1 deletion QuickLook/BackgroundListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,18 @@ private void HotkeyEventHandler(object sender, KeyEventArgs e)
if (e.Modifiers != Keys.None)
return;

ViewWindowManager.GetInstance().InvokeRoutine();
ViewWindowManager.GetInstance().InvokeRoutine(e.KeyCode != Keys.Space);
}

private void InstallHook(KeyEventHandler handler)
{
_hook = GlobalKeyboardHook.GetInstance();

_hook.HookedKeys.Add(Keys.Space);
_hook.HookedKeys.Add(Keys.Up);
_hook.HookedKeys.Add(Keys.Down);
_hook.HookedKeys.Add(Keys.Left);
_hook.HookedKeys.Add(Keys.Right);

_hook.KeyUp += handler;
}
Expand Down
19 changes: 19 additions & 0 deletions QuickLook/GlobalKeyboardHook.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Windows.Input;
using QuickLook.NativeMethods;
using KeyEventArgs = System.Windows.Forms.KeyEventArgs;
using KeyEventHandler = System.Windows.Forms.KeyEventHandler;

namespace QuickLook
{
Expand Down Expand Up @@ -62,6 +65,8 @@ private int HookProc(int code, int wParam, ref User32.KeyboardHookStruct lParam)
var key = (Keys) lParam.vkCode;
if (HookedKeys.Contains(key))
{
key = AddModifiers(key);

var kea = new KeyEventArgs(key);
if (wParam == User32.WM_KEYDOWN || wParam == User32.WM_SYSKEYDOWN)
KeyDown?.Invoke(this, kea);
Expand All @@ -73,5 +78,19 @@ private int HookProc(int code, int wParam, ref User32.KeyboardHookStruct lParam)
}
return User32.CallNextHookEx(_hhook, code, wParam, ref lParam);
}

private Keys AddModifiers(Keys key)
{
//Ctrl
if ((Keyboard.Modifiers & ModifierKeys.Control) != 0) key = key | Keys.Control;

//Shift
if ((Keyboard.Modifiers & ModifierKeys.Shift) != 0) key = key | Keys.Shift;

//Alt
if ((Keyboard.Modifiers & ModifierKeys.Alt) != 0) key = key | Keys.Alt;

return key;
}
}
}
2 changes: 1 addition & 1 deletion QuickLook/Helpers/AutoStartupHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ internal static void CreateAutorunShortcut()
}
catch (Exception)
{
TrayIcon.GetInstance().ShowNotification("", "Failed to add QuickLook to Startup folder.");
TrayIconManager.GetInstance().ShowNotification("", "Failed to add QuickLook to Startup folder.");
}
}

Expand Down
8 changes: 3 additions & 5 deletions QuickLook/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ internal MainWindow()

InitializeComponent();

// revert designer changes
windowPanel.Opacity = 0d;
busyIndicatorLayer.Visibility = Visibility.Visible;
busyIndicatorLayer.Opacity = 1d;

// do not set TopMost property if we are now debugging. it makes debugging painful...
if (!Debugger.IsAttached)
Topmost = true;
Expand Down Expand Up @@ -59,6 +54,9 @@ private void DragMoveCurrentWindow(object sender, MouseButtonEventArgs e)

private new void Show()
{
// revert UI changes
ContextObject.IsBusy = true;

Height = ContextObject.PreferredSize.Height + titlebar.Height + windowBorder.BorderThickness.Top +
windowBorder.BorderThickness.Bottom;
Width = ContextObject.PreferredSize.Width + windowBorder.BorderThickness.Left +
Expand Down
2 changes: 1 addition & 1 deletion QuickLook/Plugin/ContextObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void DisposePlugin()
/// <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);
TrayIconManager.GetInstance().ShowNotification(title, content, isError);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion QuickLook/QuickLook.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
<Compile Include="Plugin\InfoPanel\WindowsThumbnailProvider.cs" />
<Compile Include="Plugin\IViewer.cs" />
<Compile Include="Helpers\DpiHelpers.cs" />
<Compile Include="TrayIcon.cs" />
<Compile Include="TrayIconManager.cs" />
<Compile Include="Plugin\ContextObject.cs" />
<Compile Include="Helpers\WindowHelper.cs" />
<Compile Include="ViewWindowManager.cs" />
Expand Down
12 changes: 6 additions & 6 deletions QuickLook/TrayIcon.cs → QuickLook/TrayIconManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

namespace QuickLook
{
public class TrayIcon : IDisposable
public class TrayIconManager : IDisposable
{
private static TrayIcon _instance;
private static TrayIconManager _instance;

private readonly NotifyIcon _icon;

Expand All @@ -22,11 +22,11 @@ public class TrayIcon : IDisposable
AutoStartupHelper.CreateAutorunShortcut();
});

private TrayIcon()
private TrayIconManager()
{
_icon = new NotifyIcon
{
Icon = Resources.app_white,
Icon = Resources.app,
Visible = true,
ContextMenu = new ContextMenu(new[]
{
Expand All @@ -50,9 +50,9 @@ public void ShowNotification(string title, string content, bool isError = false)
_icon.ShowBalloonTip(5000, title, content, isError ? ToolTipIcon.Error : ToolTipIcon.Info);
}

internal static TrayIcon GetInstance()
internal static TrayIconManager GetInstance()
{
return _instance ?? (_instance = new TrayIcon());
return _instance ?? (_instance = new TrayIconManager());
}
}
}
4 changes: 2 additions & 2 deletions QuickLook/ViewWindowManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ internal ViewWindowManager()
_viewWindow = new MainWindow();
}

internal void InvokeRoutine()
internal void InvokeRoutine(bool replaceView = false)
{
if (!WindowHelper.IsFocusedControlExplorerItem())
if (!WindowHelper.IsFocusedWindowSelf())
return;

if (_viewWindow.BeginHide())
if (!replaceView && _viewWindow.BeginHide())
return;

var path = GetCurrentSelection();
Expand Down

0 comments on commit 892b9bf

Please sign in to comment.