Refactoring visibility related events

1. remove unnecessary events from MainViewModel
2. remove usage of Obsolete api (show, hide etc)
3. fix space problem in #660
4. part of #486
5. fix up/down key bug introduced in 92b7ca6a1bafd254e39ee92812ff691906cd85c1
6. fix #678
This commit is contained in:
bao-qian
2016-05-25 01:00:10 +01:00
parent 3aada68eb9
commit 245cffc4f7
14 changed files with 187 additions and 193 deletions

View File

@@ -152,7 +152,6 @@ namespace Wox.Plugin.PluginManagement
if (!Directory.Exists(folder)) Directory.CreateDirectory(folder); if (!Directory.Exists(folder)) Directory.CreateDirectory(folder);
string filePath = Path.Combine(folder, Guid.NewGuid().ToString() + ".wox"); string filePath = Path.Combine(folder, Guid.NewGuid().ToString() + ".wox");
context.API.StartLoadingBar();
string pluginUrl = APIBASE + "/media/" + r1.plugin_file; string pluginUrl = APIBASE + "/media/" + r1.plugin_file;
try try
@@ -168,7 +167,6 @@ namespace Wox.Plugin.PluginManagement
return false; return false;
} }
context.API.InstallPlugin(filePath); context.API.InstallPlugin(filePath);
context.API.StopLoadingBar();
} }
return false; return false;
} }

View File

@@ -3,13 +3,14 @@ using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Windows.Forms; using System.Windows;
using WindowsInput; using WindowsInput;
using WindowsInput.Native;
using Wox.Infrastructure.Hotkey; using Wox.Infrastructure.Hotkey;
using Wox.Infrastructure.Logger; using Wox.Infrastructure.Logger;
using Wox.Infrastructure.Storage; using Wox.Infrastructure.Storage;
using Application = System.Windows.Application;
using Control = System.Windows.Controls.Control; using Control = System.Windows.Controls.Control;
using Keys = System.Windows.Forms.Keys;
namespace Wox.Plugin.CMD namespace Wox.Plugin.CMD
{ {
@@ -191,7 +192,7 @@ namespace Wox.Plugin.CMD
} }
else if (_settings.Shell == Shell.RunCommand) else if (_settings.Shell == Shell.RunCommand)
{ {
var parts = command.Split(new[] {' '}, 2); var parts = command.Split(new[] { ' ' }, 2);
if (parts.Length == 2) if (parts.Length == 2)
{ {
var filename = parts[0]; var filename = parts[0];
@@ -283,17 +284,16 @@ namespace Wox.Plugin.CMD
if (keyevent == (int)KeyEvent.WM_KEYUP && WinRStroked && vkcode == (int)Keys.LWin) if (keyevent == (int)KeyEvent.WM_KEYUP && WinRStroked && vkcode == (int)Keys.LWin)
{ {
WinRStroked = false; WinRStroked = false;
keyboardSimulator.ModifiedKeyStroke(VirtualKeyCode.LWIN, VirtualKeyCode.CONTROL);
return false; return false;
} }
} }
return true; return false;
} }
private void OnWinRPressed() private void OnWinRPressed()
{ {
context.API.ShowApp();
context.API.ChangeQuery($"{context.CurrentPluginMetadata.ActionKeywords[0]}{Plugin.Query.TermSeperater}"); context.API.ChangeQuery($"{context.CurrentPluginMetadata.ActionKeywords[0]}{Plugin.Query.TermSeperater}");
Application.Current.MainWindow.Visibility = Visibility.Visible;
} }
public Control CreateSettingPanel() public Control CreateSettingPanel()
@@ -311,8 +311,6 @@ namespace Wox.Plugin.CMD
return context.API.GetTranslation("wox_plugin_cmd_plugin_description"); return context.API.GetTranslation("wox_plugin_cmd_plugin_description");
} }
public bool IsInstantQuery(string query) => false;
public List<Result> LoadContextMenus(Result selectedResult) public List<Result> LoadContextMenus(Result selectedResult)
{ {
return new List<Result> return new List<Result>
@@ -322,7 +320,6 @@ namespace Wox.Plugin.CMD
Title = context.API.GetTranslation("wox_plugin_cmd_run_as_administrator"), Title = context.API.GetTranslation("wox_plugin_cmd_run_as_administrator"),
Action = c => Action = c =>
{ {
context.API.HideApp();
Execute(selectedResult.Title, true); Execute(selectedResult.Title, true);
return true; return true;
}, },

View File

@@ -161,7 +161,7 @@ namespace Wox.Plugin.Sys
IcoPath = "Images\\app.png", IcoPath = "Images\\app.png",
Action = c => Action = c =>
{ {
context.API.CloseApp(); Application.Current.MainWindow.Close();
return true; return true;
} }
}, },

View File

@@ -3,6 +3,7 @@ using System.IO;
using NLog; using NLog;
using NLog.Config; using NLog.Config;
using NLog.Targets; using NLog.Targets;
using Wox.Infrastructure.Exception;
namespace Wox.Infrastructure.Logger namespace Wox.Infrastructure.Logger
{ {

View File

@@ -31,11 +31,13 @@ namespace Wox.Plugin
/// Just change the query text, this won't raise search /// Just change the query text, this won't raise search
/// </summary> /// </summary>
/// <param name="query"></param> /// <param name="query"></param>
[Obsolete]
void ChangeQueryText(string query, bool selectAll = false); void ChangeQueryText(string query, bool selectAll = false);
/// <summary> /// <summary>
/// Close Wox /// Close Wox
/// </summary> /// </summary>
[Obsolete]
void CloseApp(); void CloseApp();
/// <summary> /// <summary>
@@ -46,11 +48,13 @@ namespace Wox.Plugin
/// <summary> /// <summary>
/// Hide Wox /// Hide Wox
/// </summary> /// </summary>
[Obsolete]
void HideApp(); void HideApp();
/// <summary> /// <summary>
/// Show Wox /// Show Wox
/// </summary> /// </summary>
[Obsolete]
void ShowApp(); void ShowApp();
/// <summary> /// <summary>
@@ -69,11 +73,13 @@ namespace Wox.Plugin
/// <summary> /// <summary>
/// Show loading animation /// Show loading animation
/// </summary> /// </summary>
[Obsolete("automatically start")]
void StartLoadingBar(); void StartLoadingBar();
/// <summary> /// <summary>
/// Stop loading animation /// Stop loading animation
/// </summary> /// </summary>
[Obsolete("automatically stop")]
void StopLoadingBar(); void StopLoadingBar();
/// <summary> /// <summary>

View File

@@ -8,7 +8,6 @@ using Wox.Core.UserSettings;
using Wox.Helper; using Wox.Helper;
using Wox.Infrastructure.Image; using Wox.Infrastructure.Image;
using Wox.Infrastructure.Logger; using Wox.Infrastructure.Logger;
using Wox.Infrastructure.Storage;
using Wox.ViewModel; using Wox.ViewModel;
using Stopwatch = Wox.Infrastructure.Stopwatch; using Stopwatch = Wox.Infrastructure.Stopwatch;
@@ -61,7 +60,10 @@ namespace Wox
AutoStartup(); AutoStartup();
AutoUpdates(); AutoUpdates();
window.Show(); if (!_settings.HideOnStartup)
{
mainVM.MainWindowVisibility = Visibility.Visible;
}
}); });
} }
@@ -116,7 +118,7 @@ namespace Wox
[Conditional("RELEASE")] [Conditional("RELEASE")]
private static void RegisterAppDomainExceptions() private static void RegisterAppDomainExceptions()
{ {
AppDomain.CurrentDomain.UnhandledException += ErrorReporting.UnhandledExceptionHandle; AppDomain.CurrentDomain.UnhandledException += ErrorReporting.UnhandledExceptionHandle;
AppDomain.CurrentDomain.FirstChanceException += (s, e) => AppDomain.CurrentDomain.FirstChanceException += (s, e) =>
{ {
@@ -131,14 +133,14 @@ namespace Wox
// but if sessionending is not called, exit won't be called when log off / shutdown // but if sessionending is not called, exit won't be called when log off / shutdown
if (!_disposed) if (!_disposed)
{ {
Current.Dispatcher.Invoke(() => ((MainViewModel) Current.MainWindow?.DataContext)?.Save()); Current.Dispatcher.Invoke(() => ((MainViewModel)Current.MainWindow?.DataContext)?.Save());
_disposed = true; _disposed = true;
} }
} }
public void OnSecondAppStarted() public void OnSecondAppStarted()
{ {
API.ShowApp(); Current.MainWindow.Visibility = Visibility.Visible;
} }
} }
} }

View File

@@ -55,7 +55,7 @@ namespace Wox
SetHotkey(ctlHotkey.CurrentHotkey, delegate SetHotkey(ctlHotkey.CurrentHotkey, delegate
{ {
App.API.ChangeQuery(pluginHotkey.ActionKeyword); App.API.ChangeQuery(pluginHotkey.ActionKeyword);
App.API.ShowApp(); Application.Current.MainWindow.Visibility = Visibility.Visible;
}); });
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("succeed")); MessageBox.Show(InternationalizationManager.Instance.GetTranslation("succeed"));
} }
@@ -73,8 +73,8 @@ namespace Wox
RemoveHotkey(oldHotkey); RemoveHotkey(oldHotkey);
SetHotkey(new HotkeyModel(updateCustomHotkey.Hotkey), delegate SetHotkey(new HotkeyModel(updateCustomHotkey.Hotkey), delegate
{ {
App.API.ShowApp();
App.API.ChangeQuery(updateCustomHotkey.ActionKeyword); App.API.ChangeQuery(updateCustomHotkey.ActionKeyword);
Application.Current.MainWindow.Visibility = Visibility.Visible;
}); });
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("succeed")); MessageBox.Show(InternationalizationManager.Instance.GetTranslation("succeed"));
} }
@@ -100,8 +100,8 @@ namespace Wox
private void BtnTestActionKeyword_OnClick(object sender, RoutedEventArgs e) private void BtnTestActionKeyword_OnClick(object sender, RoutedEventArgs e)
{ {
App.API.ShowApp();
App.API.ChangeQuery(tbAction.Text); App.API.ChangeQuery(tbAction.Text);
Application.Current.MainWindow.Visibility = Visibility.Visible;
} }
private void RemoveHotkey(string hotkeyStr) private void RemoveHotkey(string hotkeyStr)

View File

@@ -10,7 +10,7 @@ namespace Wox.Helper
{ {
var window = Application.Current.Windows.OfType<Window>().FirstOrDefault(x => x.GetType() == typeof(T)) var window = Application.Current.Windows.OfType<Window>().FirstOrDefault(x => x.GetType() == typeof(T))
?? (T)Activator.CreateInstance(typeof(T), args); ?? (T)Activator.CreateInstance(typeof(T), args);
App.API.HideApp(); Application.Current.MainWindow.Hide();
window.Show(); window.Show();
window.Focus(); window.Focus();

View File

@@ -7,10 +7,6 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
Title="Wox" Title="Wox"
Topmost="True" Topmost="True"
Loaded="OnLoaded"
Closing="OnClosing"
Drop="OnDrop"
Deactivated="OnDeactivated"
SizeToContent="Height" SizeToContent="Height"
ResizeMode="NoResize" ResizeMode="NoResize"
WindowStyle="None" WindowStyle="None"
@@ -20,17 +16,22 @@
Style="{DynamicResource WindowStyle}" Style="{DynamicResource WindowStyle}"
Icon="Images\app.png" Icon="Images\app.png"
AllowsTransparency="True" AllowsTransparency="True"
Left="{Binding Left, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Loaded="OnLoaded"
Top="{Binding Top, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Closing="OnClosing"
Drop="OnDrop"
SizeChanged="OnSizeChanged"
IsVisibleChanged="OnMainWindowVisible"
Deactivated="OnDeactivated"
PreviewKeyDown="OnKeyDown"
Visibility="{Binding MainWindowVisibility, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Visibility="{Binding MainWindowVisibility, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
d:DataContext="{d:DesignInstance vm:MainViewModel}"> d:DataContext="{d:DesignInstance vm:MainViewModel}"
FocusManager.FocusedElement="{Binding ElementName=QueryTextBox}"
FocusManager.IsFocusScope="True">
<Window.InputBindings> <Window.InputBindings>
<KeyBinding Key="Escape" Command="{Binding EscCommand}"></KeyBinding> <KeyBinding Key="Escape" Command="{Binding EscCommand}"></KeyBinding>
<KeyBinding Key="F1" Command="{Binding StartHelpCommand}"></KeyBinding> <KeyBinding Key="F1" Command="{Binding StartHelpCommand}"></KeyBinding>
<KeyBinding Key="Tab" Command="{Binding SelectNextItemCommand}"></KeyBinding> <KeyBinding Key="Tab" Command="{Binding SelectNextItemCommand}"></KeyBinding>
<KeyBinding Key="Tab" Modifiers="Shift" Command="{Binding SelectPrevItemCommand}"></KeyBinding> <KeyBinding Key="Tab" Modifiers="Shift" Command="{Binding SelectPrevItemCommand}"></KeyBinding>
<KeyBinding Key="Down" Command="{Binding SelectNextItemCommand}"></KeyBinding>
<KeyBinding Key="Up" Command="{Binding SelectPrevItemCommand}"></KeyBinding>
<KeyBinding Key="N" Modifiers="Ctrl" Command="{Binding SelectNextItemCommand}"></KeyBinding> <KeyBinding Key="N" Modifiers="Ctrl" Command="{Binding SelectNextItemCommand}"></KeyBinding>
<KeyBinding Key="J" Modifiers="Ctrl" Command="{Binding SelectNextItemCommand}"></KeyBinding> <KeyBinding Key="J" Modifiers="Ctrl" Command="{Binding SelectNextItemCommand}"></KeyBinding>
<KeyBinding Key="D" Modifiers="Ctrl" Command="{Binding SelectNextPageCommand}"></KeyBinding> <KeyBinding Key="D" Modifiers="Ctrl" Command="{Binding SelectNextPageCommand}"></KeyBinding>
@@ -50,12 +51,16 @@
<KeyBinding Key="D8" Modifiers="Alt" Command="{Binding OpenResultCommand}" CommandParameter="7"></KeyBinding> <KeyBinding Key="D8" Modifiers="Alt" Command="{Binding OpenResultCommand}" CommandParameter="7"></KeyBinding>
<KeyBinding Key="D9" Modifiers="Alt" Command="{Binding OpenResultCommand}" CommandParameter="8"></KeyBinding> <KeyBinding Key="D9" Modifiers="Alt" Command="{Binding OpenResultCommand}" CommandParameter="8"></KeyBinding>
</Window.InputBindings> </Window.InputBindings>
<Border Style="{DynamicResource WindowBorderStyle}" MouseDown="OnMouseDown"> <Border Style="{DynamicResource WindowBorderStyle}" MouseDown="OnMouseDown" >
<StackPanel Orientation="Vertical"> <StackPanel Orientation="Vertical">
<TextBox Style="{DynamicResource QueryBoxStyle}" <TextBox Style="{DynamicResource QueryBoxStyle}"
Text="{Binding QueryText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Text="{Binding QueryText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
PreviewDragOver="OnPreviewDragOver" PreviewDragOver="OnPreviewDragOver"
IsVisibleChanged="OnQueryVisible"
TextChanged="OnQueryChanged"
AllowDrop="True" AllowDrop="True"
Focusable="True"
Visibility="Visible"
x:Name="QueryTextBox"> x:Name="QueryTextBox">
<TextBox.ContextMenu> <TextBox.ContextMenu>
<ContextMenu> <ContextMenu>

View File

@@ -1,7 +1,6 @@
using System; using System;
using System.ComponentModel; using System.ComponentModel;
using System.Windows; using System.Windows;
using System.Windows.Forms;
using System.Windows.Input; using System.Windows.Input;
using System.Windows.Media.Animation; using System.Windows.Media.Animation;
using System.Windows.Controls; using System.Windows.Controls;
@@ -10,11 +9,10 @@ using Wox.Core.Resource;
using Wox.Core.UserSettings; using Wox.Core.UserSettings;
using Wox.Helper; using Wox.Helper;
using Wox.ViewModel; using Wox.ViewModel;
using Screen = System.Windows.Forms.Screen;
using ContextMenu = System.Windows.Forms.ContextMenu; using ContextMenu = System.Windows.Forms.ContextMenu;
using DataFormats = System.Windows.DataFormats;
using DragEventArgs = System.Windows.DragEventArgs;
using MenuItem = System.Windows.Forms.MenuItem; using MenuItem = System.Windows.Forms.MenuItem;
using MessageBox = System.Windows.MessageBox; using NotifyIcon = System.Windows.Forms.NotifyIcon;
namespace Wox namespace Wox
{ {
@@ -26,81 +24,42 @@ namespace Wox
private readonly Storyboard _progressBarStoryboard = new Storyboard(); private readonly Storyboard _progressBarStoryboard = new Storyboard();
private Settings _settings; private Settings _settings;
private NotifyIcon _notifyIcon; private NotifyIcon _notifyIcon;
private MainViewModel _viewModel;
#endregion #endregion
public MainWindow(Settings settings, MainViewModel mainVM) public MainWindow(Settings settings, MainViewModel mainVM)
{ {
DataContext = mainVM;
InitializeComponent(); InitializeComponent();
DataContext = mainVM;
_viewModel = mainVM;
_settings = settings; _settings = settings;
} }
public MainWindow() public MainWindow()
{ {
InitializeComponent(); InitializeComponent();
} }
private void OnClosing(object sender, CancelEventArgs e) private void OnClosing(object sender, CancelEventArgs e)
{ {
_notifyIcon.Visible = false; _notifyIcon.Visible = false;
_settings.WindowLeft = Left; _viewModel.Save();
_settings.WindowTop = Top;
var vm = (MainViewModel)DataContext;
vm.Save();
} }
private void OnLoaded(object sender, RoutedEventArgs _) private void OnLoaded(object sender, RoutedEventArgs _)
{ {
InitProgressbarAnimation(); InitProgressbarAnimation();
WindowIntelopHelper.DisableControlBox(this); WindowIntelopHelper.DisableControlBox(this);
ThemeManager.Instance.ChangeTheme(_settings.Theme); ThemeManager.Instance.ChangeTheme(_settings.Theme);
InitializeNotifyIcon(); InitializeNotifyIcon();
var vm = (MainViewModel)DataContext;
RegisterEvents(vm);
// happlebao todo delete
vm.Left = GetWindowsLeft();
vm.Top = GetWindowsTop();
vm.MainWindowVisibility = _settings.HideOnStartup ? Visibility.Hidden : Visibility.Visible;
}
private void RegisterEvents(MainViewModel vm)
{
vm.TextBoxSelected += (o, e) => QueryTextBox.SelectAll();
vm.CursorMovedToEnd += (o, e) =>
{
QueryTextBox.Focus();
QueryTextBox.CaretIndex = QueryTextBox.Text.Length;
};
vm.PropertyChanged += (o, e) =>
{
if (e.PropertyName == nameof(vm.MainWindowVisibility))
{
if (vm.MainWindowVisibility.IsVisible())
{
Activate();
QueryTextBox.Focus();
Left = GetWindowsLeft();
Top = GetWindowsTop();
_settings.ActivateTimes++;
}
else
{
_settings.WindowLeft = Left;
_settings.WindowTop = Top;
}
}
};
} }
private void InitializeNotifyIcon() private void InitializeNotifyIcon()
{ {
_notifyIcon = new NotifyIcon { Text = Infrastructure.Constant.Wox, Icon = Properties.Resources.app, Visible = true }; _notifyIcon = new NotifyIcon { Text = Infrastructure.Constant.Wox, Icon = Properties.Resources.app, Visible = true };
_notifyIcon.Click += (o, e) => App.API.ShowApp(); _notifyIcon.Click += (o, e) => Visibility = Visibility.Visible;
var open = new MenuItem(InternationalizationManager.Instance.GetTranslation("iconTrayOpen")); var open = new MenuItem(InternationalizationManager.Instance.GetTranslation("iconTrayOpen"));
open.Click += (o, e) => App.API.ShowApp(); open.Click += (o, e) => Visibility = Visibility.Visible;
var setting = new MenuItem(InternationalizationManager.Instance.GetTranslation("iconTraySettings")); var setting = new MenuItem(InternationalizationManager.Instance.GetTranslation("iconTraySettings"));
setting.Click += (o, e) => App.API.OpenSettingDialog(); setting.Click += (o, e) => App.API.OpenSettingDialog();
var exit = new MenuItem(InternationalizationManager.Instance.GetTranslation("iconTrayExit")); var exit = new MenuItem(InternationalizationManager.Instance.GetTranslation("iconTrayExit"));
@@ -109,34 +68,6 @@ namespace Wox
_notifyIcon.ContextMenu = new ContextMenu(childen); _notifyIcon.ContextMenu = new ContextMenu(childen);
} }
private double GetWindowsLeft()
{
if (_settings.RememberLastLaunchLocation)
{
return _settings.WindowLeft;
}
var screen = Screen.FromPoint(System.Windows.Forms.Cursor.Position);
var dip1 = WindowIntelopHelper.TransformPixelsToDIP(this, screen.WorkingArea.X, 0);
var dip2 = WindowIntelopHelper.TransformPixelsToDIP(this, screen.WorkingArea.Width, 0);
var left = (dip2.X - ActualWidth) / 2 + dip1.X;
return left;
}
private double GetWindowsTop()
{
if (_settings.RememberLastLaunchLocation)
{
return _settings.WindowTop;
}
var screen = Screen.FromPoint(System.Windows.Forms.Cursor.Position);
var dip1 = WindowIntelopHelper.TransformPixelsToDIP(this, 0, screen.WorkingArea.Y);
var dip2 = WindowIntelopHelper.TransformPixelsToDIP(this, 0, screen.WorkingArea.Height);
var top = (dip2.Y - ActualHeight) / 4 + dip1.Y;
return top;
}
private void InitProgressbarAnimation() private void InitProgressbarAnimation()
{ {
var da = new DoubleAnimation(ProgressBar.X2, ActualWidth + 100, new Duration(new TimeSpan(0, 0, 0, 0, 1600))); var da = new DoubleAnimation(ProgressBar.X2, ActualWidth + 100, new Duration(new TimeSpan(0, 0, 0, 0, 1600)));
@@ -146,8 +77,8 @@ namespace Wox
_progressBarStoryboard.Children.Add(da); _progressBarStoryboard.Children.Add(da);
_progressBarStoryboard.Children.Add(da1); _progressBarStoryboard.Children.Add(da1);
_progressBarStoryboard.RepeatBehavior = RepeatBehavior.Forever; _progressBarStoryboard.RepeatBehavior = RepeatBehavior.Forever;
ProgressBar.Visibility = Visibility.Hidden;
ProgressBar.BeginStoryboard(_progressBarStoryboard); ProgressBar.BeginStoryboard(_progressBarStoryboard);
_viewModel.ProgressBarVisibility = Visibility.Hidden;
} }
private void OnMouseDown(object sender, MouseButtonEventArgs e) private void OnMouseDown(object sender, MouseButtonEventArgs e)
@@ -155,14 +86,6 @@ namespace Wox
if (e.ChangedButton == MouseButton.Left) DragMove(); if (e.ChangedButton == MouseButton.Left) DragMove();
} }
private void OnDeactivated(object sender, EventArgs e)
{
if (_settings.HideWhenDeactive)
{
App.API.HideApp();
}
}
private void OnPreviewMouseButtonDown(object sender, MouseButtonEventArgs e) private void OnPreviewMouseButtonDown(object sender, MouseButtonEventArgs e)
{ {
if (sender != null && e.OriginalSource != null) if (sender != null && e.OriginalSource != null)
@@ -173,17 +96,13 @@ namespace Wox
var result = (ResultViewModel)item?.DataContext; var result = (ResultViewModel)item?.DataContext;
if (result != null) if (result != null)
{ {
var vm = DataContext as MainViewModel; if (e.ChangedButton == MouseButton.Left)
if (vm != null)
{ {
if (e.ChangedButton == MouseButton.Left) _viewModel.OpenResultCommand.Execute(null);
{ }
vm.OpenResultCommand.Execute(null); else if (e.ChangedButton == MouseButton.Right)
} {
else if (e.ChangedButton == MouseButton.Right) _viewModel.LoadContextMenuCommand.Execute(null);
{
vm.LoadContextMenuCommand.Execute(null);
}
} }
} }
} }
@@ -217,5 +136,110 @@ namespace Wox
{ {
App.API.OpenSettingDialog(); App.API.OpenSettingDialog();
} }
private void OnDeactivated(object sender, EventArgs e)
{
if (_settings.HideWhenDeactive)
{
Hide();
}
}
private void OnQueryVisible(object sender, DependencyPropertyChangedEventArgs e)
{
var visible = (bool)e.NewValue;
if (visible)
{
// the Focusable and the IsVisible both needs to be true when set focus
// logical is set in xaml
QueryTextBox.Focus();
if (_viewModel.QueryTextSelected)
{
QueryTextBox.SelectAll();
_viewModel.QueryTextSelected = false;
}
}
}
private void OnQueryChanged(object sender, TextChangedEventArgs e)
{
QueryTextBox.CaretIndex = QueryTextBox.Text.Length;
}
private void OnMainWindowVisible(object sender, DependencyPropertyChangedEventArgs e)
{
var visible = (bool)e.NewValue;
if (visible)
{
SetWindowPosition();
_settings.ActivateTimes++;
}
}
private bool _startup = true;
private void SetWindowPosition()
{
if (!_settings.RememberLastLaunchLocation && !_startup)
{
Left = WindowLeft();
Top = WindowTop();
}
else
{
_startup = false;
}
}
/// <summary>
// used to set correct position on windows first startup
// since the actual width and actual height will be avaiable after this event
/// </summary>
private void OnSizeChanged(object sender, SizeChangedEventArgs e)
{
Left = WindowLeft();
Top = WindowTop();
}
private double WindowLeft()
{
var screen = Screen.FromPoint(System.Windows.Forms.Cursor.Position);
var dip1 = WindowIntelopHelper.TransformPixelsToDIP(this, screen.WorkingArea.X, 0);
var dip2 = WindowIntelopHelper.TransformPixelsToDIP(this, screen.WorkingArea.Width, 0);
var left = (dip2.X - ActualWidth) / 2 + dip1.X;
return left;
}
private double WindowTop()
{
var screen = Screen.FromPoint(System.Windows.Forms.Cursor.Position);
var dip1 = WindowIntelopHelper.TransformPixelsToDIP(this, 0, screen.WorkingArea.Y);
var dip2 = WindowIntelopHelper.TransformPixelsToDIP(this, 0, screen.WorkingArea.Height);
var top = (dip2.Y - QueryTextBox.ActualHeight) / 4 + dip1.Y;
return top;
}
/// <summary>
/// Register up and down key
/// todo: any way to put this in xaml ?
/// </summary>
private void OnKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Down)
{
_viewModel.SelectNextItemCommand.Execute(null);
e.Handled = true;
}
else if (e.Key == Key.Up)
{
_viewModel.SelectPrevItemCommand.Execute(null);
e.Handled = true;
}
}
} }
} }

View File

@@ -16,15 +16,15 @@ namespace Wox
{ {
public class PublicAPIInstance : IPublicAPI public class PublicAPIInstance : IPublicAPI
{ {
private readonly SettingWindowViewModel _settingsViewModel; private readonly SettingWindowViewModel _settingsVM;
private readonly MainViewModel _mainVM;
#region Constructor #region Constructor
public PublicAPIInstance(SettingWindowViewModel settingsViewModel, MainViewModel mainVM) public PublicAPIInstance(SettingWindowViewModel settingsVM, MainViewModel mainVM)
{ {
_settingsViewModel = settingsViewModel; _settingsVM = settingsVM;
MainVM = mainVM; _mainVM = mainVM;
//_settings = settings;
GlobalHotkey.Instance.hookedKeyboardCallback += KListener_hookedKeyboardCallback; GlobalHotkey.Instance.hookedKeyboardCallback += KListener_hookedKeyboardCallback;
WebRequest.RegisterPrefix("data", new DataWebRequestFactory()); WebRequest.RegisterPrefix("data", new DataWebRequestFactory());
@@ -32,39 +32,27 @@ namespace Wox
#endregion #endregion
#region Properties
public MainViewModel MainVM
{
get;
set;
}
#endregion
#region Public API #region Public API
public void ChangeQuery(string query, bool requery = false) public void ChangeQuery(string query, bool requery = false)
{ {
MainVM.QueryText = query; _mainVM.QueryText = query;
MainVM.OnCursorMovedToEnd();
} }
public void ChangeQueryText(string query, bool selectAll = false) public void ChangeQueryText(string query, bool selectAll = false)
{ {
MainVM.QueryText = query; _mainVM.QueryText = query;
MainVM.OnTextBoxSelected();
} }
[Obsolete]
public void CloseApp() public void CloseApp()
{ {
//notifyIcon.Visible = false;
Application.Current.MainWindow.Close(); Application.Current.MainWindow.Close();
} }
public void RestarApp() public void RestarApp()
{ {
HideWox(); _mainVM.MainWindowVisibility = Visibility.Hidden;
// we must manually save // we must manually save
// UpdateManager.RestartApp() will call Environment.Exit(0) // UpdateManager.RestartApp() will call Environment.Exit(0)
// which will cause ungraceful exit // which will cause ungraceful exit
@@ -73,14 +61,16 @@ namespace Wox
UpdateManager.RestartApp(); UpdateManager.RestartApp();
} }
[Obsolete]
public void HideApp() public void HideApp()
{ {
HideWox(); _mainVM.MainWindowVisibility = Visibility.Hidden;
} }
[Obsolete]
public void ShowApp() public void ShowApp()
{ {
ShowWox(); _mainVM.MainWindowVisibility = Visibility.Visible;
} }
public void ShowMsg(string title, string subTitle = "", string iconPath = "") public void ShowMsg(string title, string subTitle = "", string iconPath = "")
@@ -96,18 +86,18 @@ namespace Wox
{ {
Application.Current.Dispatcher.Invoke(() => Application.Current.Dispatcher.Invoke(() =>
{ {
SettingWindow sw = SingletonWindowOpener.Open<SettingWindow>(this, _settingsViewModel); SettingWindow sw = SingletonWindowOpener.Open<SettingWindow>(this, _settingsVM);
}); });
} }
public void StartLoadingBar() public void StartLoadingBar()
{ {
MainVM.ProgressBarVisibility = Visibility.Visible; _mainVM.ProgressBarVisibility = Visibility.Visible;
} }
public void StopLoadingBar() public void StopLoadingBar()
{ {
MainVM.ProgressBarVisibility = Visibility.Collapsed; _mainVM.ProgressBarVisibility = Visibility.Collapsed;
} }
public void InstallPlugin(string path) public void InstallPlugin(string path)
@@ -138,7 +128,7 @@ namespace Wox
}); });
Task.Run(() => Task.Run(() =>
{ {
MainVM.UpdateResultView(results, plugin, query); _mainVM.UpdateResultView(results, plugin, query);
}); });
} }
@@ -154,17 +144,6 @@ namespace Wox
} }
return true; return true;
} }
private void HideWox()
{
MainVM.MainWindowVisibility = Visibility.Collapsed;
}
private void ShowWox(bool selectAll = true)
{
MainVM.MainWindowVisibility = Visibility.Visible;
MainVM.OnTextBoxSelected();
}
#endregion #endregion
} }
} }

View File

@@ -128,15 +128,15 @@ namespace Wox
{ {
if (HotkeyControl.CurrentHotkeyAvailable) if (HotkeyControl.CurrentHotkeyAvailable)
{ {
SetHotkey(HotkeyControl.CurrentHotkey, delegate SetHotkey(HotkeyControl.CurrentHotkey, (o, args) =>
{ {
if (!Application.Current.MainWindow.IsVisible) if (!Application.Current.MainWindow.IsVisible)
{ {
_api.ShowApp(); Application.Current.MainWindow.Visibility = Visibility.Visible;
} }
else else
{ {
_api.HideApp(); Application.Current.MainWindow.Visibility = Visibility.Hidden;
} }
}); });
RemoveHotkey(_settings.Hotkey); RemoveHotkey(_settings.Hotkey);

View File

@@ -291,9 +291,7 @@ namespace Wox.ViewModel
} }
} }
public double Left { get; set; } public bool QueryTextSelected { get; set; }
public double Top { get; set; }
public Visibility ContextMenuVisibility public Visibility ContextMenuVisibility
@@ -308,7 +306,6 @@ namespace Wox.ViewModel
{ {
QueryText = _queryTextBeforeLoadContextMenu; QueryText = _queryTextBeforeLoadContextMenu;
ResultListBoxVisibility = Visibility.Visible; ResultListBoxVisibility = Visibility.Visible;
OnCursorMovedToEnd();
} }
else else
{ {
@@ -572,12 +569,11 @@ namespace Wox.ViewModel
if (_settings.CustomPluginHotkeys == null) return; if (_settings.CustomPluginHotkeys == null) return;
foreach (CustomPluginHotkey hotkey in _settings.CustomPluginHotkeys) foreach (CustomPluginHotkey hotkey in _settings.CustomPluginHotkeys)
{ {
CustomPluginHotkey hotkey1 = hotkey; SetHotkey(hotkey.Hotkey, (s, e) =>
SetHotkey(hotkey.Hotkey, delegate
{ {
if (ShouldIgnoreHotkeys()) return; if (ShouldIgnoreHotkeys()) return;
App.API.ShowApp(); QueryText = hotkey.ActionKeyword;
App.API.ChangeQuery(hotkey1.ActionKeyword, true); MainWindowVisibility = Visibility.Visible;
}); });
} }
} }
@@ -585,6 +581,7 @@ namespace Wox.ViewModel
private void OnHotkey(object sender, HotkeyEventArgs e) private void OnHotkey(object sender, HotkeyEventArgs e)
{ {
if (ShouldIgnoreHotkeys()) return; if (ShouldIgnoreHotkeys()) return;
QueryTextSelected = true;
ToggleWox(); ToggleWox();
e.Handled = true; e.Handled = true;
} }
@@ -594,7 +591,6 @@ namespace Wox.ViewModel
if (!MainWindowVisibility.IsVisible()) if (!MainWindowVisibility.IsVisible())
{ {
MainWindowVisibility = Visibility.Visible; MainWindowVisibility = Visibility.Visible;
OnTextBoxSelected();
} }
else else
{ {
@@ -653,19 +649,5 @@ namespace Wox.ViewModel
} }
#endregion #endregion
public event EventHandler CursorMovedToEnd;
public void OnCursorMovedToEnd()
{
CursorMovedToEnd?.Invoke(this, new EventArgs());
}
public event EventHandler TextBoxSelected;
public void OnTextBoxSelected()
{
TextBoxSelected?.Invoke(this, new EventArgs());
}
} }
} }

View File

@@ -439,7 +439,7 @@ cd "$(TargetDir)" &amp; del /s /q *.xml
</PostBuildEvent> </PostBuildEvent>
</PropertyGroup> </PropertyGroup>
<PropertyGroup> <PropertyGroup>
<PreBuildEvent>taskkill /f /fi "IMAGENAME eq Wox.exe</PreBuildEvent> <PreBuildEvent>taskkill /f /fi "IMAGENAME eq Wox.exe"</PreBuildEvent>
</PropertyGroup> </PropertyGroup>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets. Other similar extension points exist, see Microsoft.Common.targets.