Refactoring

This commit is contained in:
bao-qian 2016-02-26 23:43:57 +00:00
parent 3adc3ed5d2
commit 49ca3bfd00
4 changed files with 73 additions and 105 deletions

View File

@ -1,34 +1,41 @@
<Window x:Class="Wox.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:wox="clr-namespace:Wox"
xmlns:vm="clr-namespace:Wox.ViewModel" xmlns:converters="clr-namespace:Wox.Converters"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wox="clr-namespace:Wox"
xmlns:vm="clr-namespace:Wox.ViewModel"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
Title="Wox"
Topmost="True"
Loaded="MainWindow_OnLoaded"
Loaded="OnLoaded"
Closing="OnClosing"
PreviewKeyDown="OnPreviewKeyDown"
Drop="OnDrop"
Deactivated="OnDeactivated"
SizeToContent="Height"
ResizeMode="NoResize"
Deactivated="MainWindow_OnDeactivated"
WindowStyle="None"
WindowStartupLocation="Manual"
Drop="MainWindow_OnDrop"
AllowDrop="True"
ShowInTaskbar="False"
Style="{DynamicResource WindowStyle}"
Icon="Images\app.png"
AllowsTransparency="True"
Visibility="{Binding MainWindowVisibility}"
PreviewKeyDown="Window_PreviewKeyDown" d:DataContext="{d:DesignInstance vm:MainViewModel, IsDesignTimeCreatable=True}">
Left="{Binding Left, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Top="{Binding Top, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Visibility="{Binding MainWindowVisibility, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
d:DataContext="{d:DesignInstance vm:MainViewModel, IsDesignTimeCreatable=True}">
<Window.Resources>
<DataTemplate DataType="{x:Type vm:ResultsViewModel}">
<wox:ResultListBox></wox:ResultListBox>
</DataTemplate>
</Window.Resources>
<Border Style="{DynamicResource WindowBorderStyle}" MouseDown="Border_OnMouseDown">
<Border Style="{DynamicResource WindowBorderStyle}" MouseDown="OnMouseDown">
<StackPanel Orientation="Vertical">
<TextBox Style="{DynamicResource QueryBoxStyle}" Text="{Binding QueryText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
PreviewDragOver="TbQuery_OnPreviewDragOver" AllowDrop="True"
<TextBox Style="{DynamicResource QueryBoxStyle}"
Text="{Binding QueryText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
PreviewDragOver="OnPreviewDragOver"
AllowDrop="True"
x:Name="QueryTextBox" />
<Line Style="{DynamicResource PendingLineStyle}" x:Name="progressBar" Y1="0" Y2="0" X2="100" Height="2" StrokeThickness="1"
Visibility="{Binding ProgressBarVisibility}">

View File

@ -21,20 +21,18 @@ namespace Wox
public partial class MainWindow
{
#region Properties
#region Private Fields
private readonly Storyboard progressBarStoryboard = new Storyboard();
private readonly Storyboard _progressBarStoryboard = new Storyboard();
#endregion
public MainWindow()
{
InitializeComponent();
Closing += MainWindow_Closing;
}
void MainWindow_Closing(object sender, CancelEventArgs e)
private void OnClosing(object sender, CancelEventArgs e)
{
UserSettingStorage.Instance.WindowLeft = Left;
UserSettingStorage.Instance.WindowTop = Top;
@ -42,44 +40,29 @@ namespace Wox
e.Cancel = true;
}
private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
private void OnLoaded(object sender, RoutedEventArgs _)
{
CheckUpdate();
ThemeManager.Theme.ChangeTheme(UserSettingStorage.Instance.Theme);
InternationalizationManager.Instance.ChangeLanguage(UserSettingStorage.Instance.Language);
InitProgressbarAnimation();
WindowIntelopHelper.DisableControlBox(this);
CheckUpdate();
var vm = DataContext as MainViewModel;
vm.PropertyChanged += (o, eve) =>
var vm = (MainViewModel)DataContext;
vm.TextBoxSelected += (o, e) => QueryTextBox.SelectAll();
vm.CursorMovedToEnd += (o, e) =>
{
if(eve.PropertyName == "SelectAllText")
QueryTextBox.Focus();
QueryTextBox.CaretIndex = QueryTextBox.Text.Length;
};
vm.MainWindowVisibilityChanged += (o, e) =>
{
if (vm.MainWindowVisibility.IsVisible())
{
if (vm.SelectAllText)
{
QueryTextBox.SelectAll();
}
}
else if(eve.PropertyName == "CaretIndex")
{
QueryTextBox.CaretIndex = vm.CaretIndex;
}
else if(eve.PropertyName == "Left")
{
Left = vm.Left;
}
else if(eve.PropertyName == "Top")
{
Top = vm.Top;
}
else if(eve.PropertyName == "MainWindowVisibility")
{
if (vm.MainWindowVisibility.IsVisible())
{
Activate();
QueryTextBox.Focus();
}
Activate();
QueryTextBox.Focus();
}
};
@ -94,7 +77,7 @@ namespace Wox
var screen = Screen.FromPoint(System.Windows.Forms.Cursor.Position);
var dipPoint = WindowIntelopHelper.TransformPixelsToDIP(this, screen.WorkingArea.Width, 0);
UserSettingStorage.Instance.WindowLeft = (dipPoint.X - ActualWidth)/2;
UserSettingStorage.Instance.WindowLeft = (dipPoint.X - ActualWidth) / 2;
return UserSettingStorage.Instance.WindowLeft;
}
@ -104,7 +87,7 @@ namespace Wox
var screen = Screen.FromPoint(System.Windows.Forms.Cursor.Position);
var dipPoint = WindowIntelopHelper.TransformPixelsToDIP(this, 0, screen.WorkingArea.Height);
UserSettingStorage.Instance.WindowTop = (dipPoint.Y - QueryTextBox.ActualHeight)/4;
UserSettingStorage.Instance.WindowTop = (dipPoint.Y - QueryTextBox.ActualHeight) / 4;
return UserSettingStorage.Instance.WindowTop;
}
@ -135,19 +118,19 @@ namespace Wox
var da1 = new DoubleAnimation(progressBar.X1, ActualWidth, new Duration(new TimeSpan(0, 0, 0, 0, 1600)));
Storyboard.SetTargetProperty(da, new PropertyPath("(Line.X2)"));
Storyboard.SetTargetProperty(da1, new PropertyPath("(Line.X1)"));
progressBarStoryboard.Children.Add(da);
progressBarStoryboard.Children.Add(da1);
progressBarStoryboard.RepeatBehavior = RepeatBehavior.Forever;
_progressBarStoryboard.Children.Add(da);
_progressBarStoryboard.Children.Add(da1);
_progressBarStoryboard.RepeatBehavior = RepeatBehavior.Forever;
progressBar.Visibility = Visibility.Hidden;
progressBar.BeginStoryboard(progressBarStoryboard);
progressBar.BeginStoryboard(_progressBarStoryboard);
}
private void Border_OnMouseDown(object sender, MouseButtonEventArgs e)
private void OnMouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left) DragMove();
}
private void MainWindow_OnDeactivated(object sender, EventArgs e)
private void OnDeactivated(object sender, EventArgs e)
{
if (UserSettingStorage.Instance.HideWhenDeactive)
{
@ -155,7 +138,7 @@ namespace Wox
}
}
private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
private void OnPreviewKeyDown(object sender, KeyEventArgs e)
{
var vm = DataContext as MainViewModel;
@ -317,7 +300,7 @@ namespace Wox
}
}
private void MainWindow_OnDrop(object sender, DragEventArgs e)
private void OnDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
@ -335,7 +318,7 @@ namespace Wox
e.Handled = false;
}
private void TbQuery_OnPreviewDragOver(object sender, DragEventArgs e)
private void OnPreviewDragOver(object sender, DragEventArgs e)
{
e.Handled = true;
}

View File

@ -63,14 +63,13 @@ namespace Wox
public void ChangeQuery(string query, bool requery = false)
{
MainVM.QueryText = query;
MainVM.CaretIndex = MainVM.QueryText.Length;
MainVM.OnCursorMovedToEnd();
}
public void ChangeQueryText(string query, bool selectAll = false)
{
MainVM.QueryText = query;
MainVM.SelectAllText = true;
MainVM.OnTextBoxSelected();
}
public void CloseApp()
@ -200,7 +199,7 @@ namespace Wox
{
UserSettingStorage.Instance.IncreaseActivateTimes();
MainVM.MainWindowVisibility = Visibility.Visible;
MainVM.SelectAllText = true;
MainVM.OnTextBoxSelected();
}
public void SetHotkey(string hotkeyStr, EventHandler<HotkeyEventArgs> action)

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Windows;
@ -21,8 +22,6 @@ namespace Wox.ViewModel
private string _queryText;
private bool _isProgressBarTooltipVisible;
private bool _selectAllText;
private int _caretIndex;
private double _left;
private double _top;
@ -68,37 +67,10 @@ namespace Wox.ViewModel
{
_queryText = value;
OnPropertyChanged("QueryText");
HandleQueryTextUpdated();
}
}
public bool SelectAllText
{
get
{
return _selectAllText;
}
set
{
_selectAllText = value;
OnPropertyChanged("SelectAllText");
}
}
public int CaretIndex
{
get
{
return _caretIndex;
}
set
{
_caretIndex = value;
OnPropertyChanged("CaretIndex");
}
}
public bool IsProgressBarTooltipVisible
{
get
@ -187,6 +159,7 @@ namespace Wox.ViewModel
{
_mainWindowVisibility = value;
OnPropertyChanged("MainWindowVisibility");
MainWindowVisibilityChanged?.Invoke(this, new EventArgs());
if (!value.IsVisible() && ContextMenuVisibility.IsVisible())
{
@ -225,7 +198,7 @@ namespace Wox.ViewModel
}
});
SelectNextItemCommand = new RelayCommand(o =>
SelectNextItemCommand = new RelayCommand(_ =>
{
if (ContextMenuVisibility.IsVisible())
{
@ -390,7 +363,11 @@ namespace Wox.ViewModel
private void HandleQueryTextUpdated()
{
if (_ignoreTextChange) { _ignoreTextChange = false; return; }
if (_ignoreTextChange)
{
_ignoreTextChange = false;
return;
}
IsProgressBarTooltipVisible = false;
if (ContextMenuVisibility.IsVisible())
@ -475,15 +452,6 @@ namespace Wox.ViewModel
}
};
action.Invoke();
//Application.Current.Dispatcher.InvokeAsync(async () =>
//{
// await Task.Delay(150);
// if (!string.IsNullOrEmpty(query.RawQuery) && query.RawQuery == _lastQuery.RawQuery && !_queryHasReturn)
// {
// StartProgress();
// }
//});
PluginManager.QueryForAllPlugins(query);
}
@ -507,7 +475,7 @@ namespace Wox.ViewModel
QueryText = _textBeforeEnterContextMenuMode;
ContextMenuVisibility = Visibility.Collapsed;
ResultListBoxVisibility = Visibility.Visible;
CaretIndex = QueryText.Length;
OnCursorMovedToEnd();
}
private void DisplayQueryHistory(HistoryItem history)
@ -517,7 +485,7 @@ namespace Wox.ViewModel
var historyMetadata = QueryHistoryStorage.MetaData;
QueryText = history.Query;
SelectAllText = true;
OnTextBoxSelected();
var executeQueryHistoryTitle = InternationalizationManager.Instance.GetTranslation("executeQuery");
var lastExecuteTime = InternationalizationManager.Instance.GetTranslation("lastExecuteTime");
@ -531,10 +499,8 @@ namespace Wox.ViewModel
IcoPath = "Images\\history.png",
PluginDirectory = WoxDirectroy.Executable,
Action = _ =>{
QueryText = history.Query;
SelectAllText = true;
OnTextBoxSelected();
return false;
}
}
@ -577,6 +543,19 @@ namespace Wox.ViewModel
#endregion
public event EventHandler<ListeningKeyPressedEventArgs> ListeningKeyPressed;
public event EventHandler MainWindowVisibilityChanged;
public event EventHandler CursorMovedToEnd;
public void OnCursorMovedToEnd()
{
CursorMovedToEnd?.Invoke(this, new EventArgs());
}
public event EventHandler TextBoxSelected;
public void OnTextBoxSelected()
{
TextBoxSelected?.Invoke(this, new EventArgs());
}
}