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

@@ -8,7 +8,6 @@ using Wox.Core.UserSettings;
using Wox.Helper;
using Wox.Infrastructure.Image;
using Wox.Infrastructure.Logger;
using Wox.Infrastructure.Storage;
using Wox.ViewModel;
using Stopwatch = Wox.Infrastructure.Stopwatch;
@@ -61,7 +60,10 @@ namespace Wox
AutoStartup();
AutoUpdates();
window.Show();
if (!_settings.HideOnStartup)
{
mainVM.MainWindowVisibility = Visibility.Visible;
}
});
}
@@ -116,7 +118,7 @@ namespace Wox
[Conditional("RELEASE")]
private static void RegisterAppDomainExceptions()
{
AppDomain.CurrentDomain.UnhandledException += ErrorReporting.UnhandledExceptionHandle;
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
if (!_disposed)
{
Current.Dispatcher.Invoke(() => ((MainViewModel) Current.MainWindow?.DataContext)?.Save());
Current.Dispatcher.Invoke(() => ((MainViewModel)Current.MainWindow?.DataContext)?.Save());
_disposed = true;
}
}
public void OnSecondAppStarted()
{
API.ShowApp();
Current.MainWindow.Visibility = Visibility.Visible;
}
}
}

View File

@@ -55,7 +55,7 @@ namespace Wox
SetHotkey(ctlHotkey.CurrentHotkey, delegate
{
App.API.ChangeQuery(pluginHotkey.ActionKeyword);
App.API.ShowApp();
Application.Current.MainWindow.Visibility = Visibility.Visible;
});
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("succeed"));
}
@@ -73,8 +73,8 @@ namespace Wox
RemoveHotkey(oldHotkey);
SetHotkey(new HotkeyModel(updateCustomHotkey.Hotkey), delegate
{
App.API.ShowApp();
App.API.ChangeQuery(updateCustomHotkey.ActionKeyword);
Application.Current.MainWindow.Visibility = Visibility.Visible;
});
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("succeed"));
}
@@ -100,8 +100,8 @@ namespace Wox
private void BtnTestActionKeyword_OnClick(object sender, RoutedEventArgs e)
{
App.API.ShowApp();
App.API.ChangeQuery(tbAction.Text);
Application.Current.MainWindow.Visibility = Visibility.Visible;
}
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))
?? (T)Activator.CreateInstance(typeof(T), args);
App.API.HideApp();
Application.Current.MainWindow.Hide();
window.Show();
window.Focus();

View File

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

View File

@@ -1,7 +1,6 @@
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media.Animation;
using System.Windows.Controls;
@@ -10,11 +9,10 @@ using Wox.Core.Resource;
using Wox.Core.UserSettings;
using Wox.Helper;
using Wox.ViewModel;
using Screen = System.Windows.Forms.Screen;
using ContextMenu = System.Windows.Forms.ContextMenu;
using DataFormats = System.Windows.DataFormats;
using DragEventArgs = System.Windows.DragEventArgs;
using MenuItem = System.Windows.Forms.MenuItem;
using MessageBox = System.Windows.MessageBox;
using NotifyIcon = System.Windows.Forms.NotifyIcon;
namespace Wox
{
@@ -26,81 +24,42 @@ namespace Wox
private readonly Storyboard _progressBarStoryboard = new Storyboard();
private Settings _settings;
private NotifyIcon _notifyIcon;
private MainViewModel _viewModel;
#endregion
public MainWindow(Settings settings, MainViewModel mainVM)
{
DataContext = mainVM;
InitializeComponent();
DataContext = mainVM;
_viewModel = mainVM;
_settings = settings;
}
public MainWindow()
{
InitializeComponent();
}
private void OnClosing(object sender, CancelEventArgs e)
{
_notifyIcon.Visible = false;
_settings.WindowLeft = Left;
_settings.WindowTop = Top;
var vm = (MainViewModel)DataContext;
vm.Save();
_viewModel.Save();
}
private void OnLoaded(object sender, RoutedEventArgs _)
{
InitProgressbarAnimation();
WindowIntelopHelper.DisableControlBox(this);
ThemeManager.Instance.ChangeTheme(_settings.Theme);
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()
{
_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"));
open.Click += (o, e) => App.API.ShowApp();
open.Click += (o, e) => Visibility = Visibility.Visible;
var setting = new MenuItem(InternationalizationManager.Instance.GetTranslation("iconTraySettings"));
setting.Click += (o, e) => App.API.OpenSettingDialog();
var exit = new MenuItem(InternationalizationManager.Instance.GetTranslation("iconTrayExit"));
@@ -109,34 +68,6 @@ namespace Wox
_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()
{
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(da1);
_progressBarStoryboard.RepeatBehavior = RepeatBehavior.Forever;
ProgressBar.Visibility = Visibility.Hidden;
ProgressBar.BeginStoryboard(_progressBarStoryboard);
_viewModel.ProgressBarVisibility = Visibility.Hidden;
}
private void OnMouseDown(object sender, MouseButtonEventArgs e)
@@ -155,14 +86,6 @@ namespace Wox
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)
{
if (sender != null && e.OriginalSource != null)
@@ -173,17 +96,13 @@ namespace Wox
var result = (ResultViewModel)item?.DataContext;
if (result != null)
{
var vm = DataContext as MainViewModel;
if (vm != null)
if (e.ChangedButton == MouseButton.Left)
{
if (e.ChangedButton == MouseButton.Left)
{
vm.OpenResultCommand.Execute(null);
}
else if (e.ChangedButton == MouseButton.Right)
{
vm.LoadContextMenuCommand.Execute(null);
}
_viewModel.OpenResultCommand.Execute(null);
}
else if (e.ChangedButton == MouseButton.Right)
{
_viewModel.LoadContextMenuCommand.Execute(null);
}
}
}
@@ -217,5 +136,110 @@ namespace Wox
{
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
{
private readonly SettingWindowViewModel _settingsViewModel;
private readonly SettingWindowViewModel _settingsVM;
private readonly MainViewModel _mainVM;
#region Constructor
public PublicAPIInstance(SettingWindowViewModel settingsViewModel, MainViewModel mainVM)
public PublicAPIInstance(SettingWindowViewModel settingsVM, MainViewModel mainVM)
{
_settingsViewModel = settingsViewModel;
MainVM = mainVM;
//_settings = settings;
_settingsVM = settingsVM;
_mainVM = mainVM;
GlobalHotkey.Instance.hookedKeyboardCallback += KListener_hookedKeyboardCallback;
WebRequest.RegisterPrefix("data", new DataWebRequestFactory());
@@ -32,39 +32,27 @@ namespace Wox
#endregion
#region Properties
public MainViewModel MainVM
{
get;
set;
}
#endregion
#region Public API
public void ChangeQuery(string query, bool requery = false)
{
MainVM.QueryText = query;
MainVM.OnCursorMovedToEnd();
_mainVM.QueryText = query;
}
public void ChangeQueryText(string query, bool selectAll = false)
{
MainVM.QueryText = query;
MainVM.OnTextBoxSelected();
_mainVM.QueryText = query;
}
[Obsolete]
public void CloseApp()
{
//notifyIcon.Visible = false;
Application.Current.MainWindow.Close();
}
public void RestarApp()
{
HideWox();
_mainVM.MainWindowVisibility = Visibility.Hidden;
// we must manually save
// UpdateManager.RestartApp() will call Environment.Exit(0)
// which will cause ungraceful exit
@@ -73,14 +61,16 @@ namespace Wox
UpdateManager.RestartApp();
}
[Obsolete]
public void HideApp()
{
HideWox();
_mainVM.MainWindowVisibility = Visibility.Hidden;
}
[Obsolete]
public void ShowApp()
{
ShowWox();
_mainVM.MainWindowVisibility = Visibility.Visible;
}
public void ShowMsg(string title, string subTitle = "", string iconPath = "")
@@ -96,18 +86,18 @@ namespace Wox
{
Application.Current.Dispatcher.Invoke(() =>
{
SettingWindow sw = SingletonWindowOpener.Open<SettingWindow>(this, _settingsViewModel);
SettingWindow sw = SingletonWindowOpener.Open<SettingWindow>(this, _settingsVM);
});
}
public void StartLoadingBar()
{
MainVM.ProgressBarVisibility = Visibility.Visible;
_mainVM.ProgressBarVisibility = Visibility.Visible;
}
public void StopLoadingBar()
{
MainVM.ProgressBarVisibility = Visibility.Collapsed;
_mainVM.ProgressBarVisibility = Visibility.Collapsed;
}
public void InstallPlugin(string path)
@@ -138,7 +128,7 @@ namespace Wox
});
Task.Run(() =>
{
MainVM.UpdateResultView(results, plugin, query);
_mainVM.UpdateResultView(results, plugin, query);
});
}
@@ -154,17 +144,6 @@ namespace Wox
}
return true;
}
private void HideWox()
{
MainVM.MainWindowVisibility = Visibility.Collapsed;
}
private void ShowWox(bool selectAll = true)
{
MainVM.MainWindowVisibility = Visibility.Visible;
MainVM.OnTextBoxSelected();
}
#endregion
}
}

View File

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

View File

@@ -291,9 +291,7 @@ namespace Wox.ViewModel
}
}
public double Left { get; set; }
public double Top { get; set; }
public bool QueryTextSelected { get; set; }
public Visibility ContextMenuVisibility
@@ -308,7 +306,6 @@ namespace Wox.ViewModel
{
QueryText = _queryTextBeforeLoadContextMenu;
ResultListBoxVisibility = Visibility.Visible;
OnCursorMovedToEnd();
}
else
{
@@ -572,12 +569,11 @@ namespace Wox.ViewModel
if (_settings.CustomPluginHotkeys == null) return;
foreach (CustomPluginHotkey hotkey in _settings.CustomPluginHotkeys)
{
CustomPluginHotkey hotkey1 = hotkey;
SetHotkey(hotkey.Hotkey, delegate
SetHotkey(hotkey.Hotkey, (s, e) =>
{
if (ShouldIgnoreHotkeys()) return;
App.API.ShowApp();
App.API.ChangeQuery(hotkey1.ActionKeyword, true);
QueryText = hotkey.ActionKeyword;
MainWindowVisibility = Visibility.Visible;
});
}
}
@@ -585,6 +581,7 @@ namespace Wox.ViewModel
private void OnHotkey(object sender, HotkeyEventArgs e)
{
if (ShouldIgnoreHotkeys()) return;
QueryTextSelected = true;
ToggleWox();
e.Handled = true;
}
@@ -594,7 +591,6 @@ namespace Wox.ViewModel
if (!MainWindowVisibility.IsVisible())
{
MainWindowVisibility = Visibility.Visible;
OnTextBoxSelected();
}
else
{
@@ -653,19 +649,5 @@ namespace Wox.ViewModel
}
#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>
</PropertyGroup>
<PropertyGroup>
<PreBuildEvent>taskkill /f /fi "IMAGENAME eq Wox.exe</PreBuildEvent>
<PreBuildEvent>taskkill /f /fi "IMAGENAME eq Wox.exe"</PreBuildEvent>
</PropertyGroup>
<!-- 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.