mirror of
https://github.com/Flow-Launcher/Flow.Launcher.git
synced 2026-03-11 08:54:32 +00:00
Merge branch 'dev'
This commit is contained in:
commit
1a489652ff
28 changed files with 1878 additions and 1194 deletions
|
|
@ -22,7 +22,7 @@ namespace Wox.Plugin.Folder
|
|||
{
|
||||
this.context = context;
|
||||
this.context.API.BackKeyDownEvent += ApiBackKeyDownEvent;
|
||||
this.context.API.ResultItemDropEvent += API_ResultItemDropEvent;
|
||||
this.context.API.ResultItemDropEvent += ResultDropEvent;
|
||||
InitialDriverList();
|
||||
if (FolderStorage.Instance.FolderLinks == null)
|
||||
{
|
||||
|
|
@ -31,7 +31,7 @@ namespace Wox.Plugin.Folder
|
|||
}
|
||||
}
|
||||
|
||||
void API_ResultItemDropEvent(Result result, IDataObject dropObject, DragEventArgs e)
|
||||
void ResultDropEvent(Result result, IDataObject dropObject, DragEventArgs e)
|
||||
{
|
||||
if (dropObject.GetDataPresent(DataFormats.FileDrop))
|
||||
{
|
||||
|
|
|
|||
|
|
@ -61,7 +61,7 @@ namespace Wox.Plugin.Program
|
|||
public void Init(PluginInitContext context)
|
||||
{
|
||||
this.context = context;
|
||||
this.context.API.ResultItemDropEvent += API_ResultItemDropEvent;
|
||||
this.context.API.ResultItemDropEvent += ResultDropEvent;
|
||||
Stopwatch.Debug("Preload programs", () =>
|
||||
{
|
||||
programs = ProgramCacheStorage.Instance.Programs;
|
||||
|
|
@ -70,7 +70,7 @@ namespace Wox.Plugin.Program
|
|||
Stopwatch.Debug("Program Index", IndexPrograms);
|
||||
}
|
||||
|
||||
void API_ResultItemDropEvent(Result result, IDataObject dropObject, DragEventArgs e)
|
||||
void ResultDropEvent(Result result, IDataObject dropObject, DragEventArgs e)
|
||||
{
|
||||
|
||||
e.Handled = true;
|
||||
|
|
|
|||
|
|
@ -87,10 +87,10 @@ namespace Wox.Core.Resource
|
|||
Style resultSubItemSelectedStyle = dict["ItemSubTitleSelectedStyle"] as Style;
|
||||
if (resultItemStyle != null && resultSubItemStyle != null && resultSubItemSelectedStyle != null && resultItemSelectedStyle != null)
|
||||
{
|
||||
Setter fontFamily = new Setter(TextBlock.FontFamilyProperty, new FontFamily(UserSettingStorage.Instance.ResultItemFont));
|
||||
Setter fontStyle = new Setter(TextBlock.FontStyleProperty, FontHelper.GetFontStyleFromInvariantStringOrNormal(UserSettingStorage.Instance.ResultItemFontStyle));
|
||||
Setter fontWeight = new Setter(TextBlock.FontWeightProperty, FontHelper.GetFontWeightFromInvariantStringOrNormal(UserSettingStorage.Instance.ResultItemFontWeight));
|
||||
Setter fontStretch = new Setter(TextBlock.FontStretchProperty, FontHelper.GetFontStretchFromInvariantStringOrNormal(UserSettingStorage.Instance.ResultItemFontStretch));
|
||||
Setter fontFamily = new Setter(TextBlock.FontFamilyProperty, new FontFamily(UserSettingStorage.Instance.ResultFont));
|
||||
Setter fontStyle = new Setter(TextBlock.FontStyleProperty, FontHelper.GetFontStyleFromInvariantStringOrNormal(UserSettingStorage.Instance.ResultFontStyle));
|
||||
Setter fontWeight = new Setter(TextBlock.FontWeightProperty, FontHelper.GetFontWeightFromInvariantStringOrNormal(UserSettingStorage.Instance.ResultFontWeight));
|
||||
Setter fontStretch = new Setter(TextBlock.FontStretchProperty, FontHelper.GetFontStretchFromInvariantStringOrNormal(UserSettingStorage.Instance.ResultFontStretch));
|
||||
|
||||
Setter[] setters = { fontFamily, fontStyle, fontWeight, fontStretch };
|
||||
Array.ForEach(new[] { resultItemStyle, resultSubItemStyle, resultItemSelectedStyle, resultSubItemSelectedStyle }, o => Array.ForEach(setters, p => o.Setters.Add(p)));
|
||||
|
|
|
|||
|
|
@ -44,16 +44,16 @@ namespace Wox.Core.UserSettings
|
|||
public string QueryBoxFontStretch { get; set; }
|
||||
|
||||
[JsonProperty]
|
||||
public string ResultItemFont { get; set; }
|
||||
public string ResultFont { get; set; }
|
||||
|
||||
[JsonProperty]
|
||||
public string ResultItemFontStyle { get; set; }
|
||||
public string ResultFontStyle { get; set; }
|
||||
|
||||
[JsonProperty]
|
||||
public string ResultItemFontWeight { get; set; }
|
||||
public string ResultFontWeight { get; set; }
|
||||
|
||||
[JsonProperty]
|
||||
public string ResultItemFontStretch { get; set; }
|
||||
public string ResultFontStretch { get; set; }
|
||||
|
||||
[JsonProperty]
|
||||
public double WindowLeft { get; set; }
|
||||
|
|
@ -126,7 +126,7 @@ namespace Wox.Core.UserSettings
|
|||
CustomizedPluginConfigs = new List<CustomizedPluginConfig>();
|
||||
Hotkey = "Alt + Space";
|
||||
QueryBoxFont = FontFamily.GenericSansSerif.Name;
|
||||
ResultItemFont = FontFamily.GenericSansSerif.Name;
|
||||
ResultFont = FontFamily.GenericSansSerif.Name;
|
||||
Opacity = 1;
|
||||
OpacityMode = OpacityMode.Normal;
|
||||
LeaveCmdOpen = false;
|
||||
|
|
@ -147,9 +147,9 @@ namespace Wox.Core.UserSettings
|
|||
{
|
||||
storage.QueryBoxFont = FontFamily.GenericSansSerif.Name;
|
||||
}
|
||||
if (storage.ResultItemFont == null)
|
||||
if (storage.ResultFont == null)
|
||||
{
|
||||
storage.ResultItemFont = FontFamily.GenericSansSerif.Name;
|
||||
storage.ResultFont = FontFamily.GenericSansSerif.Name;
|
||||
}
|
||||
if (storage.Language == null)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -119,6 +119,7 @@ namespace Wox.Plugin
|
|||
/// <summary>
|
||||
/// Fired after drop to result item of current plugin
|
||||
/// </summary>
|
||||
/// todo: ResultItem -> Result
|
||||
event ResultItemDropEventHandler ResultItemDropEvent;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,6 +10,8 @@ using Wox.CommandArgs;
|
|||
using Wox.Core.Plugin;
|
||||
using Wox.Helper;
|
||||
using Wox.Infrastructure;
|
||||
using Wox.Plugin;
|
||||
using Wox.ViewModel;
|
||||
using Stopwatch = Wox.Infrastructure.Stopwatch;
|
||||
|
||||
|
||||
|
|
@ -20,6 +22,8 @@ namespace Wox
|
|||
private const string Unique = "Wox_Unique_Application_Mutex";
|
||||
public static MainWindow Window { get; private set; }
|
||||
|
||||
public static IPublicAPI API { get; private set; }
|
||||
|
||||
[STAThread]
|
||||
public static void Main()
|
||||
{
|
||||
|
|
@ -39,9 +43,18 @@ namespace Wox
|
|||
base.OnStartup(e);
|
||||
WoxDirectroy.Executable = Directory.GetParent(Assembly.GetExecutingAssembly().Location).ToString();
|
||||
RegisterUnhandledException();
|
||||
ThreadPool.QueueUserWorkItem(o => { ImageLoader.ImageLoader.PreloadImages(); });
|
||||
Window = new MainWindow();
|
||||
PluginManager.Init(Window);
|
||||
|
||||
ThreadPool.SetMaxThreads(30, 10);
|
||||
ThreadPool.SetMinThreads(10, 5);
|
||||
ThreadPool.QueueUserWorkItem(_ => { ImageLoader.ImageLoader.PreloadImages(); });
|
||||
|
||||
MainViewModel mainVM = new MainViewModel();
|
||||
API = new PublicAPIInstance(mainVM);
|
||||
Window = new MainWindow {DataContext = mainVM};
|
||||
|
||||
NotifyIconManager notifyIconManager = new NotifyIconManager(API);
|
||||
|
||||
PluginManager.Init(API);
|
||||
CommandArgsFactory.Execute(e.Args.ToList());
|
||||
});
|
||||
|
||||
|
|
@ -59,7 +72,7 @@ namespace Wox
|
|||
{
|
||||
if (args.Count > 0 && args[0] == SingleInstance<App>.Restart)
|
||||
{
|
||||
Window.CloseApp();
|
||||
API.CloseApp();
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ namespace Wox.CommandArgs
|
|||
}
|
||||
else
|
||||
{
|
||||
App.Window.ShowApp();
|
||||
App.API.ShowApp();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,9 +16,9 @@ namespace Wox.CommandArgs
|
|||
if (args.Count > 0)
|
||||
{
|
||||
string query = args[0];
|
||||
App.Window.ChangeQuery(query);
|
||||
App.API.ChangeQuery(query);
|
||||
}
|
||||
App.Window.ShowApp();
|
||||
App.API.ShowApp();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ namespace Wox.CommandArgs
|
|||
|
||||
public void Execute(IList<string> args)
|
||||
{
|
||||
PluginManager.Init(App.Window);
|
||||
PluginManager.Init(App.API);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,7 +11,14 @@ namespace Wox.CommandArgs
|
|||
|
||||
public void Execute(IList<string> args)
|
||||
{
|
||||
App.Window.ToggleWox();
|
||||
if (App.Window.IsVisible)
|
||||
{
|
||||
App.API.HideApp();
|
||||
}
|
||||
else
|
||||
{
|
||||
App.API.ShowApp();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
30
Wox/Converters/VisibilityConverter.cs
Normal file
30
Wox/Converters/VisibilityConverter.cs
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Data;
|
||||
|
||||
namespace Wox.Converters
|
||||
{
|
||||
class VisibilityConverter : ConvertorBase<VisibilityConverter>
|
||||
{
|
||||
public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
if (value == null || value == DependencyProperty.UnsetValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return bool.Parse(value.ToString()) ? Visibility.Visible : Visibility.Collapsed;
|
||||
}
|
||||
|
||||
public override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,8 +1,12 @@
|
|||
using System.Collections.Generic;
|
||||
using NHotkey;
|
||||
using NHotkey.Wpf;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
using Wox.Core.Resource;
|
||||
using Wox.Core.UserSettings;
|
||||
using Wox.Infrastructure.Hotkey;
|
||||
|
||||
namespace Wox
|
||||
{
|
||||
|
|
@ -44,10 +48,11 @@ namespace Wox
|
|||
ActionKeyword = tbAction.Text
|
||||
};
|
||||
UserSettingStorage.Instance.CustomPluginHotkeys.Add(pluginHotkey);
|
||||
settingWidow.MainWindow.SetHotkey(ctlHotkey.CurrentHotkey, delegate
|
||||
|
||||
SetHotkey(ctlHotkey.CurrentHotkey, delegate
|
||||
{
|
||||
settingWidow.MainWindow.ChangeQuery(pluginHotkey.ActionKeyword);
|
||||
settingWidow.MainWindow.ShowApp();
|
||||
App.API.ChangeQuery(pluginHotkey.ActionKeyword);
|
||||
App.API.ShowApp();
|
||||
});
|
||||
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("succeed"));
|
||||
}
|
||||
|
|
@ -62,11 +67,11 @@ namespace Wox
|
|||
updateCustomHotkey.ActionKeyword = tbAction.Text;
|
||||
updateCustomHotkey.Hotkey = ctlHotkey.CurrentHotkey.ToString();
|
||||
//remove origin hotkey
|
||||
settingWidow.MainWindow.RemoveHotkey(oldHotkey);
|
||||
settingWidow.MainWindow.SetHotkey(updateCustomHotkey.Hotkey, delegate
|
||||
RemoveHotkey(oldHotkey);
|
||||
SetHotkey(new HotkeyModel(updateCustomHotkey.Hotkey), delegate
|
||||
{
|
||||
settingWidow.MainWindow.ShowApp();
|
||||
settingWidow.MainWindow.ChangeQuery(updateCustomHotkey.ActionKeyword);
|
||||
App.API.ShowApp();
|
||||
App.API.ChangeQuery(updateCustomHotkey.ActionKeyword);
|
||||
});
|
||||
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("succeed"));
|
||||
}
|
||||
|
|
@ -94,8 +99,30 @@ namespace Wox
|
|||
|
||||
private void BtnTestActionKeyword_OnClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
settingWidow.MainWindow.ShowApp();
|
||||
settingWidow.MainWindow.ChangeQuery(tbAction.Text);
|
||||
App.API.ShowApp();
|
||||
App.API.ChangeQuery(tbAction.Text);
|
||||
}
|
||||
|
||||
private void RemoveHotkey(string hotkeyStr)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(hotkeyStr))
|
||||
{
|
||||
HotkeyManager.Current.Remove(hotkeyStr);
|
||||
}
|
||||
}
|
||||
|
||||
private void SetHotkey(HotkeyModel hotkey, EventHandler<HotkeyEventArgs> action)
|
||||
{
|
||||
string hotkeyStr = hotkey.ToString();
|
||||
try
|
||||
{
|
||||
HotkeyManager.Current.AddOrReplace(hotkeyStr, hotkey.CharKey, hotkey.ModifierKeys, action);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
string errorMsg = string.Format(InternationalizationManager.Instance.GetTranslation("registerHotkeyFailed"), hotkeyStr);
|
||||
MessageBox.Show(errorMsg);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,71 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Collections.Specialized;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using Wox.Plugin;
|
||||
|
||||
namespace Wox.Helper
|
||||
{
|
||||
class ListBoxItems : ObservableCollection<Result>
|
||||
// todo implement custom moveItem,removeItem,insertItem for better performance
|
||||
{
|
||||
public void RemoveAll(Predicate<Result> predicate)
|
||||
{
|
||||
CheckReentrancy();
|
||||
|
||||
List<Result> itemsToRemove = Items.Where(x => predicate(x)).ToList();
|
||||
if (itemsToRemove.Count > 0)
|
||||
{
|
||||
|
||||
itemsToRemove.ForEach(item => Items.Remove(item));
|
||||
|
||||
OnPropertyChanged(new PropertyChangedEventArgs("Count"));
|
||||
OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
|
||||
// fuck ms
|
||||
// http://blogs.msdn.com/b/nathannesbit/archive/2009/04/20/addrange-and-observablecollection.aspx
|
||||
// http://geekswithblogs.net/NewThingsILearned/archive/2008/01/16/listcollectionviewcollectionview-doesnt-support-notifycollectionchanged-with-multiple-items.aspx
|
||||
// PS: don't use Reset for other data updates, it will cause UI flickering
|
||||
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
|
||||
}
|
||||
}
|
||||
|
||||
public void Update(List<Result> newItems)
|
||||
{
|
||||
int newCount = newItems.Count;
|
||||
int oldCount = Items.Count;
|
||||
int location = newCount > oldCount ? oldCount : newCount;
|
||||
for (int i = 0; i < location; i++)
|
||||
{
|
||||
Result oldItem = Items[i];
|
||||
Result newItem = newItems[i];
|
||||
if (!oldItem.Equals(newItem))
|
||||
{
|
||||
this[i] = newItem;
|
||||
}
|
||||
else if (oldItem.Score != newItem.Score)
|
||||
{
|
||||
this[i].Score = newItem.Score;
|
||||
}
|
||||
}
|
||||
|
||||
if (newCount > oldCount)
|
||||
{
|
||||
for (int i = oldCount; i < newCount; i++)
|
||||
{
|
||||
Add(newItems[i]);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
int removeIndex = newCount;
|
||||
for (int i = newCount; i < oldCount; i++)
|
||||
{
|
||||
RemoveAt(removeIndex);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
Application.Current.MainWindow.Hide();
|
||||
App.API.HideApp();
|
||||
window.Show();
|
||||
window.Focus();
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,9 @@
|
|||
<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: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"
|
||||
|
|
@ -15,14 +18,29 @@
|
|||
Style="{DynamicResource WindowStyle}"
|
||||
Icon="Images\app.png"
|
||||
AllowsTransparency="True"
|
||||
>
|
||||
Visibility="{Binding IsVisible,Converter={converters:VisibilityConverter}}"
|
||||
PreviewKeyDown="Window_PreviewKeyDown" d:DataContext="{d:DesignInstance vm:MainViewModel, IsDesignTimeCreatable=True}">
|
||||
<Window.Resources>
|
||||
<DataTemplate DataType="{x:Type vm:ResultsViewModel}">
|
||||
<wox:ResultListBox></wox:ResultListBox>
|
||||
</DataTemplate>
|
||||
<converters:VisibilityConverter x:Key="VisibilityConverter" />
|
||||
</Window.Resources>
|
||||
<Border Style="{DynamicResource WindowBorderStyle}" MouseDown="Border_OnMouseDown">
|
||||
<StackPanel Orientation="Vertical">
|
||||
<TextBox Style="{DynamicResource QueryBoxStyle}" PreviewDragOver="TbQuery_OnPreviewDragOver" AllowDrop="True"
|
||||
x:Name="tbQuery" PreviewKeyDown="TbQuery_OnPreviewKeyDown" TextChanged="TbQuery_OnTextChanged" />
|
||||
<Line Style="{DynamicResource PendingLineStyle}" x:Name="progressBar" Y1="0" Y2="0" X2="100" Height="2" StrokeThickness="1"></Line>
|
||||
<wox:ResultPanel x:Name="pnlResult" />
|
||||
<wox:ResultPanel x:Name="pnlContextMenu" Visibility="Collapsed" />
|
||||
<TextBox Style="{DynamicResource QueryBoxStyle}" Text="{Binding QueryText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
|
||||
PreviewDragOver="TbQuery_OnPreviewDragOver" AllowDrop="True"
|
||||
x:Name="tbQuery" />
|
||||
<Line Style="{DynamicResource PendingLineStyle}" x:Name="progressBar" Y1="0" Y2="0" X2="100" Height="2" StrokeThickness="1"
|
||||
Visibility="{Binding IsProgressBarVisible,Converter={StaticResource VisibilityConverter}}">
|
||||
<Line.ToolTip>
|
||||
<ToolTip IsOpen="{Binding IsProgressBarTooltipVisible}"></ToolTip>
|
||||
</Line.ToolTip>
|
||||
</Line>
|
||||
<ContentControl Content="{Binding Results}" Visibility="{Binding IsResultListBoxVisible,Converter={StaticResource VisibilityConverter}}">
|
||||
</ContentControl>
|
||||
<ContentControl Content="{Binding ContextMenu}" Visibility="{Binding IsContextMenuVisible,Converter={StaticResource VisibilityConverter}}">
|
||||
</ContentControl>
|
||||
</StackPanel>
|
||||
</Border>
|
||||
</Window>
|
||||
|
|
@ -1,244 +1,38 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Reflection;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Forms;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media.Animation;
|
||||
using NHotkey;
|
||||
using NHotkey.Wpf;
|
||||
using Wox.Core.Plugin;
|
||||
using Wox.Core.Resource;
|
||||
using Wox.Core.Updater;
|
||||
using Wox.Core.UserSettings;
|
||||
using Wox.Helper;
|
||||
using Wox.Infrastructure;
|
||||
using Wox.Infrastructure.Hotkey;
|
||||
using Wox.Plugin;
|
||||
using Wox.Storage;
|
||||
using Application = System.Windows.Application;
|
||||
using ContextMenu = System.Windows.Forms.ContextMenu;
|
||||
using DataFormats = System.Windows.DataFormats;
|
||||
using DragEventArgs = System.Windows.DragEventArgs;
|
||||
using IDataObject = System.Windows.IDataObject;
|
||||
using KeyEventArgs = System.Windows.Input.KeyEventArgs;
|
||||
using MenuItem = System.Windows.Forms.MenuItem;
|
||||
using MessageBox = System.Windows.MessageBox;
|
||||
using Stopwatch = Wox.Infrastructure.Stopwatch;
|
||||
using ToolTip = System.Windows.Controls.ToolTip;
|
||||
using Wox.ViewModel;
|
||||
using Wox.Plugin;
|
||||
|
||||
namespace Wox
|
||||
{
|
||||
public partial class MainWindow : IPublicAPI
|
||||
public partial class MainWindow
|
||||
{
|
||||
|
||||
#region Properties
|
||||
|
||||
private readonly Storyboard progressBarStoryboard = new Storyboard();
|
||||
private NotifyIcon notifyIcon;
|
||||
private bool _queryHasReturn;
|
||||
private Query _lastQuery = new Query();
|
||||
private ToolTip toolTip = new ToolTip();
|
||||
|
||||
private bool _ignoreTextChange;
|
||||
private List<Result> CurrentContextMenus = new List<Result>();
|
||||
private string textBeforeEnterContextMenuMode;
|
||||
private readonly Storyboard progressBarStoryboard = new Storyboard();
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public API
|
||||
|
||||
public void ChangeQuery(string query, bool requery = false)
|
||||
{
|
||||
Dispatcher.Invoke(() =>
|
||||
{
|
||||
tbQuery.Text = query;
|
||||
tbQuery.CaretIndex = tbQuery.Text.Length;
|
||||
if (requery)
|
||||
{
|
||||
TbQuery_OnTextChanged(null, null);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void ChangeQueryText(string query, bool selectAll = false)
|
||||
{
|
||||
Dispatcher.Invoke(() =>
|
||||
{
|
||||
_ignoreTextChange = true;
|
||||
tbQuery.Text = query;
|
||||
tbQuery.CaretIndex = tbQuery.Text.Length;
|
||||
if (selectAll)
|
||||
{
|
||||
tbQuery.SelectAll();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void CloseApp()
|
||||
{
|
||||
notifyIcon.Visible = false;
|
||||
Application.Current.Shutdown();
|
||||
}
|
||||
|
||||
public void RestarApp()
|
||||
{
|
||||
ProcessStartInfo info = new ProcessStartInfo
|
||||
{
|
||||
FileName = Application.ResourceAssembly.Location,
|
||||
Arguments = SingleInstance<App>.Restart
|
||||
};
|
||||
Process.Start(info);
|
||||
}
|
||||
|
||||
public void HideApp()
|
||||
{
|
||||
Dispatcher.Invoke(HideWox);
|
||||
}
|
||||
|
||||
public void ShowApp()
|
||||
{
|
||||
Dispatcher.Invoke(() => ShowWox());
|
||||
}
|
||||
|
||||
public void ShowMsg(string title, string subTitle, string iconPath)
|
||||
{
|
||||
Dispatcher.Invoke(() =>
|
||||
{
|
||||
var m = new Msg { Owner = GetWindow(this) };
|
||||
m.Show(title, subTitle, iconPath);
|
||||
});
|
||||
}
|
||||
|
||||
public void OpenSettingDialog(string tabName = "general")
|
||||
{
|
||||
Dispatcher.Invoke(() =>
|
||||
{
|
||||
SettingWindow sw = SingletonWindowOpener.Open<SettingWindow>(this);
|
||||
sw.SwitchTo(tabName);
|
||||
});
|
||||
}
|
||||
|
||||
public void StartLoadingBar()
|
||||
{
|
||||
Dispatcher.Invoke(StartProgress);
|
||||
}
|
||||
|
||||
public void StopLoadingBar()
|
||||
{
|
||||
Dispatcher.Invoke(StopProgress);
|
||||
}
|
||||
|
||||
public void InstallPlugin(string path)
|
||||
{
|
||||
Dispatcher.Invoke(() => PluginManager.InstallPlugin(path));
|
||||
}
|
||||
|
||||
public void ReloadPlugins()
|
||||
{
|
||||
Dispatcher.Invoke(() => PluginManager.Init(this));
|
||||
}
|
||||
|
||||
public string GetTranslation(string key)
|
||||
{
|
||||
return InternationalizationManager.Instance.GetTranslation(key);
|
||||
}
|
||||
|
||||
public List<PluginPair> GetAllPlugins()
|
||||
{
|
||||
return PluginManager.AllPlugins.ToList();
|
||||
}
|
||||
|
||||
public event WoxKeyDownEventHandler BackKeyDownEvent;
|
||||
public event WoxGlobalKeyboardEventHandler GlobalKeyboardEvent;
|
||||
public event ResultItemDropEventHandler ResultItemDropEvent;
|
||||
|
||||
public void PushResults(Query query, PluginMetadata plugin, List<Result> results)
|
||||
{
|
||||
results.ForEach(o =>
|
||||
{
|
||||
o.PluginDirectory = plugin.PluginDirectory;
|
||||
o.PluginID = plugin.ID;
|
||||
o.OriginQuery = query;
|
||||
});
|
||||
UpdateResultView(results, plugin, query);
|
||||
}
|
||||
|
||||
public void ShowContextMenu(PluginMetadata plugin, List<Result> results)
|
||||
{
|
||||
if (results != null && results.Count > 0)
|
||||
{
|
||||
results.ForEach(o =>
|
||||
{
|
||||
o.PluginDirectory = plugin.PluginDirectory;
|
||||
o.PluginID = plugin.ID;
|
||||
o.ContextMenu = null;
|
||||
});
|
||||
pnlContextMenu.Clear();
|
||||
pnlContextMenu.AddResults(results, plugin.ID);
|
||||
pnlContextMenu.Visibility = Visibility.Visible;
|
||||
pnlResult.Visibility = Visibility.Collapsed;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
public MainWindow()
|
||||
{
|
||||
InitializeComponent();
|
||||
ThreadPool.SetMaxThreads(30, 10);
|
||||
ThreadPool.SetMinThreads(10, 5);
|
||||
|
||||
WebRequest.RegisterPrefix("data", new DataWebRequestFactory());
|
||||
GlobalHotkey.Instance.hookedKeyboardCallback += KListener_hookedKeyboardCallback;
|
||||
progressBar.ToolTip = toolTip;
|
||||
pnlResult.LeftMouseClickEvent += SelectResult;
|
||||
pnlResult.ItemDropEvent += pnlResult_ItemDropEvent;
|
||||
pnlContextMenu.LeftMouseClickEvent += SelectResult;
|
||||
pnlResult.RightMouseClickEvent += pnlResult_RightMouseClickEvent;
|
||||
|
||||
Closing += MainWindow_Closing;
|
||||
|
||||
|
||||
SetHotkey(UserSettingStorage.Instance.Hotkey, OnHotkey);
|
||||
SetCustomPluginHotkey();
|
||||
InitialTray();
|
||||
}
|
||||
|
||||
void pnlResult_ItemDropEvent(Result result, IDataObject dropDataObject, DragEventArgs args)
|
||||
{
|
||||
PluginPair pluginPair = PluginManager.AllPlugins.FirstOrDefault(o => o.Metadata.ID == result.PluginID);
|
||||
if (ResultItemDropEvent != null && pluginPair != null)
|
||||
{
|
||||
foreach (var delegateHandler in ResultItemDropEvent.GetInvocationList())
|
||||
{
|
||||
if (delegateHandler.Target == pluginPair.Plugin)
|
||||
{
|
||||
delegateHandler.DynamicInvoke(result, dropDataObject, args);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool KListener_hookedKeyboardCallback(KeyEvent keyevent, int vkcode, SpecialKeyState state)
|
||||
{
|
||||
if (GlobalKeyboardEvent != null)
|
||||
{
|
||||
return GlobalKeyboardEvent((int)keyevent, vkcode, state);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void pnlResult_RightMouseClickEvent(Result result)
|
||||
{
|
||||
ShowContextMenu(result);
|
||||
}
|
||||
|
||||
void MainWindow_Closing(object sender, CancelEventArgs e)
|
||||
|
|
@ -246,7 +40,6 @@ namespace Wox
|
|||
UserSettingStorage.Instance.WindowLeft = Left;
|
||||
UserSettingStorage.Instance.WindowTop = Top;
|
||||
UserSettingStorage.Instance.Save();
|
||||
HideWox();
|
||||
e.Cancel = true;
|
||||
}
|
||||
|
||||
|
|
@ -255,12 +48,46 @@ namespace Wox
|
|||
ThemeManager.Theme.ChangeTheme(UserSettingStorage.Instance.Theme);
|
||||
InternationalizationManager.Instance.ChangeLanguage(UserSettingStorage.Instance.Language);
|
||||
|
||||
Left = GetWindowsLeft();
|
||||
Top = GetWindowsTop();
|
||||
|
||||
InitProgressbarAnimation();
|
||||
WindowIntelopHelper.DisableControlBox(this);
|
||||
CheckUpdate();
|
||||
|
||||
var vm = DataContext as MainViewModel;
|
||||
vm.PropertyChanged += (o, eve) =>
|
||||
{
|
||||
if(eve.PropertyName == "SelectAllText")
|
||||
{
|
||||
if (vm.SelectAllText)
|
||||
{
|
||||
tbQuery.SelectAll();
|
||||
}
|
||||
}
|
||||
else if(eve.PropertyName == "CaretIndex")
|
||||
{
|
||||
tbQuery.CaretIndex = vm.CaretIndex;
|
||||
}
|
||||
else if(eve.PropertyName == "Left")
|
||||
{
|
||||
Left = vm.Left;
|
||||
}
|
||||
else if(eve.PropertyName == "Top")
|
||||
{
|
||||
Top = vm.Top;
|
||||
}
|
||||
else if(eve.PropertyName == "IsVisible")
|
||||
{
|
||||
if (vm.IsVisible)
|
||||
{
|
||||
tbQuery.Focus();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
vm.Left = GetWindowsLeft();
|
||||
vm.Top = GetWindowsTop();
|
||||
Activate();
|
||||
Focus();
|
||||
tbQuery.Focus();
|
||||
}
|
||||
|
||||
private double GetWindowsLeft()
|
||||
|
|
@ -304,82 +131,6 @@ namespace Wox
|
|||
});
|
||||
}
|
||||
|
||||
public void SetHotkey(string hotkeyStr, EventHandler<HotkeyEventArgs> action)
|
||||
{
|
||||
var hotkey = new HotkeyModel(hotkeyStr);
|
||||
SetHotkey(hotkey, action);
|
||||
}
|
||||
|
||||
public void SetHotkey(HotkeyModel hotkey, EventHandler<HotkeyEventArgs> action)
|
||||
{
|
||||
string hotkeyStr = hotkey.ToString();
|
||||
try
|
||||
{
|
||||
HotkeyManager.Current.AddOrReplace(hotkeyStr, hotkey.CharKey, hotkey.ModifierKeys, action);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
string errorMsg = string.Format(InternationalizationManager.Instance.GetTranslation("registerHotkeyFailed"), hotkeyStr);
|
||||
MessageBox.Show(errorMsg);
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveHotkey(string hotkeyStr)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(hotkeyStr))
|
||||
{
|
||||
HotkeyManager.Current.Remove(hotkeyStr);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if Wox should ignore any hotkeys
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private bool ShouldIgnoreHotkeys()
|
||||
{
|
||||
//double if to omit calling win32 function
|
||||
if (UserSettingStorage.Instance.IgnoreHotkeysOnFullscreen)
|
||||
if (WindowIntelopHelper.IsWindowFullscreen())
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private void SetCustomPluginHotkey()
|
||||
{
|
||||
if (UserSettingStorage.Instance.CustomPluginHotkeys == null) return;
|
||||
foreach (CustomPluginHotkey hotkey in UserSettingStorage.Instance.CustomPluginHotkeys)
|
||||
{
|
||||
CustomPluginHotkey hotkey1 = hotkey;
|
||||
SetHotkey(hotkey.Hotkey, delegate
|
||||
{
|
||||
if (ShouldIgnoreHotkeys()) return;
|
||||
ShowApp();
|
||||
ChangeQuery(hotkey1.ActionKeyword, true);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void OnHotkey(object sender, HotkeyEventArgs e)
|
||||
{
|
||||
if (ShouldIgnoreHotkeys()) return;
|
||||
ToggleWox();
|
||||
e.Handled = true;
|
||||
}
|
||||
|
||||
public void ToggleWox()
|
||||
{
|
||||
if (!IsVisible)
|
||||
{
|
||||
ShowWox();
|
||||
}
|
||||
else
|
||||
{
|
||||
HideWox();
|
||||
}
|
||||
}
|
||||
|
||||
private void InitProgressbarAnimation()
|
||||
{
|
||||
var da = new DoubleAnimation(progressBar.X2, ActualWidth + 100, new Duration(new TimeSpan(0, 0, 0, 0, 1600)));
|
||||
|
|
@ -393,197 +144,41 @@ namespace Wox
|
|||
progressBar.BeginStoryboard(progressBarStoryboard);
|
||||
}
|
||||
|
||||
private void InitialTray()
|
||||
{
|
||||
notifyIcon = new NotifyIcon { Text = "Wox", Icon = Properties.Resources.app, Visible = true };
|
||||
notifyIcon.Click += (o, e) => ShowWox();
|
||||
var open = new MenuItem(GetTranslation("iconTrayOpen"));
|
||||
open.Click += (o, e) => ShowWox();
|
||||
var setting = new MenuItem(GetTranslation("iconTraySettings"));
|
||||
setting.Click += (o, e) => OpenSettingDialog();
|
||||
var about = new MenuItem(GetTranslation("iconTrayAbout"));
|
||||
about.Click += (o, e) => OpenSettingDialog("about");
|
||||
var exit = new MenuItem(GetTranslation("iconTrayExit"));
|
||||
exit.Click += (o, e) => CloseApp();
|
||||
MenuItem[] childen = { open, setting, about, exit };
|
||||
notifyIcon.ContextMenu = new ContextMenu(childen);
|
||||
}
|
||||
|
||||
private void QueryContextMenu()
|
||||
{
|
||||
var contextMenuId = "Context Menu Id";
|
||||
pnlContextMenu.Clear();
|
||||
var query = tbQuery.Text.ToLower();
|
||||
if (string.IsNullOrEmpty(query))
|
||||
{
|
||||
pnlContextMenu.AddResults(CurrentContextMenus, contextMenuId);
|
||||
}
|
||||
else
|
||||
{
|
||||
List<Result> filterResults = new List<Result>();
|
||||
foreach (Result contextMenu in CurrentContextMenus)
|
||||
{
|
||||
if (StringMatcher.IsMatch(contextMenu.Title, query)
|
||||
|| StringMatcher.IsMatch(contextMenu.SubTitle, query))
|
||||
{
|
||||
filterResults.Add(contextMenu);
|
||||
}
|
||||
}
|
||||
pnlContextMenu.AddResults(filterResults, contextMenuId);
|
||||
}
|
||||
}
|
||||
|
||||
private void TbQuery_OnTextChanged(object sender, TextChangedEventArgs e)
|
||||
{
|
||||
if (_ignoreTextChange) { _ignoreTextChange = false; return; }
|
||||
|
||||
toolTip.IsOpen = false;
|
||||
if (IsInContextMenuMode)
|
||||
{
|
||||
QueryContextMenu();
|
||||
return;
|
||||
}
|
||||
|
||||
string query = tbQuery.Text.Trim();
|
||||
if (!string.IsNullOrEmpty(query))
|
||||
{
|
||||
Query(query);
|
||||
//reset query history index after user start new query
|
||||
ResetQueryHistoryIndex();
|
||||
}
|
||||
else
|
||||
{
|
||||
pnlResult.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
private void ResetQueryHistoryIndex()
|
||||
{
|
||||
pnlResult.RemoveResultsFor(QueryHistoryStorage.MetaData);
|
||||
QueryHistoryStorage.Instance.Reset();
|
||||
}
|
||||
private void Query(string text)
|
||||
{
|
||||
_queryHasReturn = false;
|
||||
var query = PluginManager.QueryInit(text);
|
||||
if (query != null)
|
||||
{
|
||||
// handle the exclusiveness of plugin using action keyword
|
||||
string lastKeyword = _lastQuery.ActionKeyword;
|
||||
string keyword = query.ActionKeyword;
|
||||
if (string.IsNullOrEmpty(lastKeyword))
|
||||
{
|
||||
if (!string.IsNullOrEmpty(keyword))
|
||||
{
|
||||
pnlResult.RemoveResultsExcept(PluginManager.NonGlobalPlugins[keyword].Metadata);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (string.IsNullOrEmpty(keyword))
|
||||
{
|
||||
pnlResult.RemoveResultsFor(PluginManager.NonGlobalPlugins[lastKeyword].Metadata);
|
||||
}
|
||||
else if (lastKeyword != keyword)
|
||||
{
|
||||
pnlResult.RemoveResultsExcept(PluginManager.NonGlobalPlugins[keyword].Metadata);
|
||||
}
|
||||
}
|
||||
_lastQuery = query;
|
||||
Dispatcher.InvokeAsync(async () =>
|
||||
{
|
||||
await Task.Delay(150);
|
||||
if (!string.IsNullOrEmpty(query.RawQuery) && query.RawQuery == _lastQuery.RawQuery && !_queryHasReturn)
|
||||
{
|
||||
StartProgress();
|
||||
}
|
||||
});
|
||||
PluginManager.QueryForAllPlugins(query);
|
||||
}
|
||||
StopProgress();
|
||||
}
|
||||
|
||||
private void BackToResultMode()
|
||||
{
|
||||
ChangeQueryText(textBeforeEnterContextMenuMode);
|
||||
pnlResult.Visibility = Visibility.Visible;
|
||||
pnlContextMenu.Visibility = Visibility.Collapsed;
|
||||
}
|
||||
|
||||
private void Border_OnMouseDown(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
if (e.ChangedButton == MouseButton.Left) DragMove();
|
||||
}
|
||||
|
||||
private void StartProgress()
|
||||
{
|
||||
progressBar.Visibility = Visibility.Visible;
|
||||
}
|
||||
|
||||
private void StopProgress()
|
||||
{
|
||||
progressBar.Visibility = Visibility.Hidden;
|
||||
}
|
||||
|
||||
private void HideWox()
|
||||
{
|
||||
UserSettingStorage.Instance.WindowLeft = Left;
|
||||
UserSettingStorage.Instance.WindowTop = Top;
|
||||
if (IsInContextMenuMode)
|
||||
{
|
||||
BackToResultMode();
|
||||
}
|
||||
Hide();
|
||||
}
|
||||
|
||||
private void ShowWox(bool selectAll = true)
|
||||
{
|
||||
UserSettingStorage.Instance.IncreaseActivateTimes();
|
||||
Left = GetWindowsLeft();
|
||||
Top = GetWindowsTop();
|
||||
|
||||
Show();
|
||||
Activate();
|
||||
Focus();
|
||||
tbQuery.Focus();
|
||||
ResetQueryHistoryIndex();
|
||||
if (selectAll) tbQuery.SelectAll();
|
||||
}
|
||||
|
||||
private void MainWindow_OnDeactivated(object sender, EventArgs e)
|
||||
{
|
||||
if (UserSettingStorage.Instance.HideWhenDeactive)
|
||||
{
|
||||
HideWox();
|
||||
App.API.HideApp();
|
||||
}
|
||||
}
|
||||
|
||||
private void TbQuery_OnPreviewKeyDown(object sender, KeyEventArgs e)
|
||||
private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
|
||||
{
|
||||
var vm = DataContext as MainViewModel;
|
||||
|
||||
if (null == vm) return;
|
||||
//when alt is pressed, the real key should be e.SystemKey
|
||||
Key key = (e.Key == Key.System ? e.SystemKey : e.Key);
|
||||
var key = (e.Key == Key.System ? e.SystemKey : e.Key);
|
||||
switch (key)
|
||||
{
|
||||
case Key.Escape:
|
||||
if (IsInContextMenuMode)
|
||||
{
|
||||
BackToResultMode();
|
||||
}
|
||||
else
|
||||
{
|
||||
HideWox();
|
||||
}
|
||||
vm.EscCommand.Execute(null);
|
||||
e.Handled = true;
|
||||
break;
|
||||
|
||||
case Key.Tab:
|
||||
if (GlobalHotkey.Instance.CheckModifiers().ShiftPressed)
|
||||
{
|
||||
SelectPrevItem();
|
||||
vm.SelectPrevItemCommand.Execute(null);
|
||||
}
|
||||
else
|
||||
{
|
||||
SelectNextItem();
|
||||
vm.SelectNextItemCommand.Execute(null);
|
||||
}
|
||||
e.Handled = true;
|
||||
break;
|
||||
|
|
@ -592,7 +187,7 @@ namespace Wox
|
|||
case Key.J:
|
||||
if (GlobalHotkey.Instance.CheckModifiers().CtrlPressed)
|
||||
{
|
||||
SelectNextItem();
|
||||
vm.SelectNextItemCommand.Execute(null);
|
||||
}
|
||||
break;
|
||||
|
||||
|
|
@ -600,32 +195,25 @@ namespace Wox
|
|||
case Key.K:
|
||||
if (GlobalHotkey.Instance.CheckModifiers().CtrlPressed)
|
||||
{
|
||||
SelectPrevItem();
|
||||
vm.SelectPrevItemCommand.Execute(null);
|
||||
}
|
||||
break;
|
||||
|
||||
case Key.O:
|
||||
if (GlobalHotkey.Instance.CheckModifiers().CtrlPressed)
|
||||
{
|
||||
if (IsInContextMenuMode)
|
||||
{
|
||||
BackToResultMode();
|
||||
}
|
||||
else
|
||||
{
|
||||
ShowContextMenu(GetActiveResult());
|
||||
}
|
||||
vm.CtrlOCommand.Execute(null);
|
||||
}
|
||||
break;
|
||||
|
||||
case Key.Down:
|
||||
if (GlobalHotkey.Instance.CheckModifiers().CtrlPressed)
|
||||
{
|
||||
DisplayNextQuery();
|
||||
vm.DisplayNextQueryCommand.Execute(null);
|
||||
}
|
||||
else
|
||||
{
|
||||
SelectNextItem();
|
||||
vm.SelectNextItemCommand.Execute(null);
|
||||
}
|
||||
e.Handled = true;
|
||||
break;
|
||||
|
|
@ -633,11 +221,11 @@ namespace Wox
|
|||
case Key.Up:
|
||||
if (GlobalHotkey.Instance.CheckModifiers().CtrlPressed)
|
||||
{
|
||||
DisplayPrevQuery();
|
||||
vm.DisplayPrevQueryCommand.Execute(null);
|
||||
}
|
||||
else
|
||||
{
|
||||
SelectPrevItem();
|
||||
vm.SelectPrevItemCommand.Execute(null);
|
||||
}
|
||||
e.Handled = true;
|
||||
break;
|
||||
|
|
@ -645,272 +233,92 @@ namespace Wox
|
|||
case Key.D:
|
||||
if (GlobalHotkey.Instance.CheckModifiers().CtrlPressed)
|
||||
{
|
||||
pnlResult.SelectNextPage();
|
||||
vm.SelectNextPageCommand.Execute(null);
|
||||
}
|
||||
break;
|
||||
|
||||
case Key.PageDown:
|
||||
pnlResult.SelectNextPage();
|
||||
vm.SelectNextPageCommand.Execute(null);
|
||||
e.Handled = true;
|
||||
break;
|
||||
|
||||
case Key.U:
|
||||
if (GlobalHotkey.Instance.CheckModifiers().CtrlPressed)
|
||||
{
|
||||
pnlResult.SelectPrevPage();
|
||||
vm.SelectPrevPageCommand.Execute(null);
|
||||
}
|
||||
break;
|
||||
|
||||
case Key.PageUp:
|
||||
pnlResult.SelectPrevPage();
|
||||
vm.SelectPrevPageCommand.Execute(null);
|
||||
e.Handled = true;
|
||||
break;
|
||||
|
||||
case Key.Back:
|
||||
if (BackKeyDownEvent != null)
|
||||
{
|
||||
BackKeyDownEvent(new WoxKeyDownEventArgs
|
||||
{
|
||||
Query = tbQuery.Text,
|
||||
keyEventArgs = e
|
||||
});
|
||||
}
|
||||
vm.BackCommand.Execute(e);
|
||||
break;
|
||||
|
||||
case Key.F1:
|
||||
Process.Start("http://doc.getwox.com");
|
||||
vm.StartHelpCommand.Execute(null);
|
||||
break;
|
||||
|
||||
case Key.Enter:
|
||||
Result activeResult = GetActiveResult();
|
||||
if (GlobalHotkey.Instance.CheckModifiers().ShiftPressed)
|
||||
{
|
||||
ShowContextMenu(activeResult);
|
||||
vm.ShiftEnterCommand.Execute(null);
|
||||
}
|
||||
else
|
||||
{
|
||||
SelectResult(activeResult);
|
||||
vm.OpenResultCommand.Execute(null);
|
||||
}
|
||||
e.Handled = true;
|
||||
break;
|
||||
|
||||
case Key.D1:
|
||||
SelectItem(1);
|
||||
|
||||
if (GlobalHotkey.Instance.CheckModifiers().AltPressed)
|
||||
{
|
||||
vm.OpenResultCommand.Execute(0);
|
||||
}
|
||||
break;
|
||||
|
||||
case Key.D2:
|
||||
SelectItem(2);
|
||||
if (GlobalHotkey.Instance.CheckModifiers().AltPressed)
|
||||
{
|
||||
vm.OpenResultCommand.Execute(1);
|
||||
}
|
||||
break;
|
||||
|
||||
case Key.D3:
|
||||
SelectItem(3);
|
||||
if (GlobalHotkey.Instance.CheckModifiers().AltPressed)
|
||||
{
|
||||
vm.OpenResultCommand.Execute(2);
|
||||
}
|
||||
break;
|
||||
|
||||
case Key.D4:
|
||||
SelectItem(4);
|
||||
if (GlobalHotkey.Instance.CheckModifiers().AltPressed)
|
||||
{
|
||||
vm.OpenResultCommand.Execute(3);
|
||||
}
|
||||
break;
|
||||
|
||||
case Key.D5:
|
||||
SelectItem(5);
|
||||
if (GlobalHotkey.Instance.CheckModifiers().AltPressed)
|
||||
{
|
||||
vm.OpenResultCommand.Execute(4);
|
||||
}
|
||||
break;
|
||||
case Key.D6:
|
||||
SelectItem(6);
|
||||
if (GlobalHotkey.Instance.CheckModifiers().AltPressed)
|
||||
{
|
||||
vm.OpenResultCommand.Execute(5);
|
||||
}
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void DisplayPrevQuery()
|
||||
{
|
||||
var prev = QueryHistoryStorage.Instance.Previous();
|
||||
DisplayQueryHistory(prev);
|
||||
}
|
||||
|
||||
private void DisplayNextQuery()
|
||||
{
|
||||
var nextQuery = QueryHistoryStorage.Instance.Next();
|
||||
DisplayQueryHistory(nextQuery);
|
||||
}
|
||||
|
||||
private void DisplayQueryHistory(HistoryItem history)
|
||||
{
|
||||
if (history != null)
|
||||
{
|
||||
var historyMetadata = QueryHistoryStorage.MetaData;
|
||||
ChangeQueryText(history.Query, true);
|
||||
var executeQueryHistoryTitle = GetTranslation("executeQuery");
|
||||
var lastExecuteTime = GetTranslation("lastExecuteTime");
|
||||
pnlResult.RemoveResultsExcept(historyMetadata);
|
||||
UpdateResultViewInternal(new List<Result>
|
||||
{
|
||||
new Result
|
||||
{
|
||||
Title = string.Format(executeQueryHistoryTitle,history.Query),
|
||||
SubTitle = string.Format(lastExecuteTime,history.ExecutedDateTime),
|
||||
IcoPath = "Images\\history.png",
|
||||
PluginDirectory = WoxDirectroy.Executable,
|
||||
Action = _ =>{
|
||||
ChangeQuery(history.Query,true);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}, historyMetadata);
|
||||
}
|
||||
}
|
||||
|
||||
private void SelectItem(int index)
|
||||
{
|
||||
int zeroBasedIndex = index - 1;
|
||||
SpecialKeyState keyState = GlobalHotkey.Instance.CheckModifiers();
|
||||
if (keyState.AltPressed || keyState.CtrlPressed)
|
||||
{
|
||||
List<Result> visibleResults = pnlResult.GetVisibleResults();
|
||||
if (zeroBasedIndex < visibleResults.Count)
|
||||
{
|
||||
SelectResult(visibleResults[zeroBasedIndex]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsInContextMenuMode
|
||||
{
|
||||
get { return pnlContextMenu.Visibility == Visibility.Visible; }
|
||||
}
|
||||
|
||||
private Result GetActiveResult()
|
||||
{
|
||||
if (IsInContextMenuMode)
|
||||
{
|
||||
return pnlContextMenu.GetActiveResult();
|
||||
}
|
||||
else
|
||||
{
|
||||
return pnlResult.GetActiveResult();
|
||||
}
|
||||
}
|
||||
|
||||
private void SelectPrevItem()
|
||||
{
|
||||
if (IsInContextMenuMode)
|
||||
{
|
||||
pnlContextMenu.SelectPrev();
|
||||
}
|
||||
else
|
||||
{
|
||||
pnlResult.SelectPrev();
|
||||
}
|
||||
toolTip.IsOpen = false;
|
||||
}
|
||||
|
||||
private void SelectNextItem()
|
||||
{
|
||||
if (IsInContextMenuMode)
|
||||
{
|
||||
pnlContextMenu.SelectNext();
|
||||
}
|
||||
else
|
||||
{
|
||||
pnlResult.SelectNext();
|
||||
}
|
||||
toolTip.IsOpen = false;
|
||||
}
|
||||
|
||||
private void SelectResult(Result result)
|
||||
{
|
||||
if (result != null)
|
||||
{
|
||||
if (result.Action != null)
|
||||
{
|
||||
bool hideWindow = result.Action(new ActionContext
|
||||
{
|
||||
SpecialKeyState = GlobalHotkey.Instance.CheckModifiers()
|
||||
});
|
||||
if (hideWindow)
|
||||
{
|
||||
HideWox();
|
||||
}
|
||||
UserSelectedRecordStorage.Instance.Add(result);
|
||||
QueryHistoryStorage.Instance.Add(tbQuery.Text);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateResultView(List<Result> list, PluginMetadata metadata, Query originQuery)
|
||||
{
|
||||
_queryHasReturn = true;
|
||||
progressBar.Dispatcher.Invoke(StopProgress);
|
||||
|
||||
list.ForEach(o =>
|
||||
{
|
||||
o.Score += UserSelectedRecordStorage.Instance.GetSelectedCount(o) * 5;
|
||||
});
|
||||
if (originQuery.RawQuery == _lastQuery.RawQuery)
|
||||
{
|
||||
UpdateResultViewInternal(list, metadata);
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateResultViewInternal(List<Result> list, PluginMetadata metadata)
|
||||
{
|
||||
Dispatcher.Invoke(() =>
|
||||
{
|
||||
Stopwatch.Normal($"UI update cost for {metadata.Name}",
|
||||
() => { pnlResult.AddResults(list, metadata.ID); });
|
||||
});
|
||||
}
|
||||
|
||||
private Result GetTopMostContextMenu(Result result)
|
||||
{
|
||||
if (TopMostRecordStorage.Instance.IsTopMost(result))
|
||||
{
|
||||
return new Result(GetTranslation("cancelTopMostInThisQuery"), "Images\\down.png")
|
||||
{
|
||||
PluginDirectory = WoxDirectroy.Executable,
|
||||
Action = _ =>
|
||||
{
|
||||
TopMostRecordStorage.Instance.Remove(result);
|
||||
ShowMsg("Succeed", "", "");
|
||||
return false;
|
||||
}
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
return new Result(GetTranslation("setAsTopMostInThisQuery"), "Images\\up.png")
|
||||
{
|
||||
PluginDirectory = WoxDirectroy.Executable,
|
||||
Action = _ =>
|
||||
{
|
||||
TopMostRecordStorage.Instance.AddOrUpdate(result);
|
||||
ShowMsg("Succeed", "", "");
|
||||
return false;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
private void ShowContextMenu(Result result)
|
||||
{
|
||||
if (result == null) return;
|
||||
List<Result> results = PluginManager.GetContextMenusForPlugin(result);
|
||||
results.ForEach(o =>
|
||||
{
|
||||
o.PluginDirectory = PluginManager.GetPluginForId(result.PluginID).Metadata.PluginDirectory;
|
||||
o.PluginID = result.PluginID;
|
||||
o.OriginQuery = result.OriginQuery;
|
||||
});
|
||||
|
||||
results.Add(GetTopMostContextMenu(result));
|
||||
|
||||
textBeforeEnterContextMenuMode = tbQuery.Text;
|
||||
ChangeQueryText("");
|
||||
pnlContextMenu.Clear();
|
||||
pnlContextMenu.AddResults(results, result.PluginID);
|
||||
CurrentContextMenus = results;
|
||||
pnlContextMenu.Visibility = Visibility.Visible;
|
||||
pnlResult.Visibility = Visibility.Collapsed;
|
||||
}
|
||||
|
||||
private void MainWindow_OnDrop(object sender, DragEventArgs e)
|
||||
{
|
||||
if (e.Data.GetDataPresent(DataFormats.FileDrop))
|
||||
|
|
@ -926,6 +334,7 @@ namespace Wox
|
|||
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("invalidWoxPluginFileFormat"));
|
||||
}
|
||||
}
|
||||
e.Handled = false;
|
||||
}
|
||||
|
||||
private void TbQuery_OnPreviewDragOver(object sender, DragEventArgs e)
|
||||
|
|
|
|||
40
Wox/NotifyIconManager.cs
Normal file
40
Wox/NotifyIconManager.cs
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
using Wox.Core.Resource;
|
||||
using Wox.Plugin;
|
||||
|
||||
namespace Wox
|
||||
{
|
||||
public class NotifyIconManager
|
||||
{
|
||||
|
||||
private NotifyIcon notifyIcon;
|
||||
private IPublicAPI _api;
|
||||
|
||||
public NotifyIconManager(IPublicAPI api)
|
||||
{
|
||||
InitialTray();
|
||||
_api = api;
|
||||
}
|
||||
|
||||
private void InitialTray()
|
||||
{
|
||||
notifyIcon = new NotifyIcon { Text = "Wox", Icon = Properties.Resources.app, Visible = true };
|
||||
notifyIcon.Click += (o, e) => _api.ShowApp();
|
||||
var open = new MenuItem(InternationalizationManager.Instance.GetTranslation("iconTrayOpen"));
|
||||
open.Click += (o, e) => _api.ShowApp();
|
||||
var setting = new MenuItem(InternationalizationManager.Instance.GetTranslation("iconTraySettings"));
|
||||
setting.Click += (o, e) => _api.OpenSettingDialog();
|
||||
var about = new MenuItem(InternationalizationManager.Instance.GetTranslation("iconTrayAbout"));
|
||||
about.Click += (o, e) => _api.OpenSettingDialog("about");
|
||||
var exit = new MenuItem(InternationalizationManager.Instance.GetTranslation("iconTrayExit"));
|
||||
exit.Click += (o, e) => _api.CloseApp();
|
||||
MenuItem[] childen = { open, setting, about, exit };
|
||||
notifyIcon.ContextMenu = new ContextMenu(childen);
|
||||
}
|
||||
}
|
||||
}
|
||||
292
Wox/PublicAPIInstance.cs
Normal file
292
Wox/PublicAPIInstance.cs
Normal file
|
|
@ -0,0 +1,292 @@
|
|||
using NHotkey;
|
||||
using NHotkey.Wpf;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Threading;
|
||||
using Wox.Core.Plugin;
|
||||
using Wox.Core.Resource;
|
||||
using Wox.Core.UserSettings;
|
||||
using Wox.Helper;
|
||||
using Wox.Infrastructure.Hotkey;
|
||||
using Wox.Plugin;
|
||||
using Wox.ViewModel;
|
||||
|
||||
namespace Wox
|
||||
{
|
||||
public class PublicAPIInstance : IPublicAPI
|
||||
{
|
||||
|
||||
#region Constructor
|
||||
|
||||
public PublicAPIInstance(MainViewModel mainVM)
|
||||
{
|
||||
MainVM = mainVM;
|
||||
|
||||
|
||||
GlobalHotkey.Instance.hookedKeyboardCallback += KListener_hookedKeyboardCallback;
|
||||
WebRequest.RegisterPrefix("data", new DataWebRequestFactory());
|
||||
|
||||
SetHotkey(UserSettingStorage.Instance.Hotkey, OnHotkey);
|
||||
SetCustomPluginHotkey();
|
||||
|
||||
MainVM.ListeningKeyPressed += (o, e) => {
|
||||
|
||||
if(e.KeyEventArgs.Key == Key.Back)
|
||||
{
|
||||
if (null != BackKeyDownEvent)
|
||||
{
|
||||
BackKeyDownEvent(new WoxKeyDownEventArgs
|
||||
{
|
||||
Query = MainVM.QueryText,
|
||||
keyEventArgs = e.KeyEventArgs
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties
|
||||
|
||||
private MainViewModel MainVM
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public API
|
||||
|
||||
public void ChangeQuery(string query, bool requery = false)
|
||||
{
|
||||
MainVM.QueryText = query;
|
||||
MainVM.CaretIndex = MainVM.QueryText.Length;
|
||||
|
||||
}
|
||||
|
||||
public void ChangeQueryText(string query, bool selectAll = false)
|
||||
{
|
||||
MainVM.QueryText = query;
|
||||
MainVM.SelectAllText = true;
|
||||
}
|
||||
|
||||
public void CloseApp()
|
||||
{
|
||||
//notifyIcon.Visible = false;
|
||||
Application.Current.Shutdown();
|
||||
}
|
||||
|
||||
public void RestarApp()
|
||||
{
|
||||
ProcessStartInfo info = new ProcessStartInfo
|
||||
{
|
||||
FileName = Application.ResourceAssembly.Location,
|
||||
Arguments = SingleInstance<App>.Restart
|
||||
};
|
||||
Process.Start(info);
|
||||
}
|
||||
|
||||
public void HideApp()
|
||||
{
|
||||
HideWox();
|
||||
}
|
||||
|
||||
public void ShowApp()
|
||||
{
|
||||
ShowWox();
|
||||
}
|
||||
|
||||
public void ShowMsg(string title, string subTitle, string iconPath)
|
||||
{
|
||||
Application.Current.Dispatcher.Invoke(() =>
|
||||
{
|
||||
var m = new Msg { Owner = Application.Current.MainWindow };
|
||||
m.Show(title, subTitle, iconPath);
|
||||
});
|
||||
}
|
||||
|
||||
public void OpenSettingDialog(string tabName = "general")
|
||||
{
|
||||
Application.Current.Dispatcher.Invoke(() =>
|
||||
{
|
||||
SettingWindow sw = SingletonWindowOpener.Open<SettingWindow>(this);
|
||||
sw.SwitchTo(tabName);
|
||||
});
|
||||
}
|
||||
|
||||
public void StartLoadingBar()
|
||||
{
|
||||
MainVM.IsProgressBarVisible = true;
|
||||
}
|
||||
|
||||
public void StopLoadingBar()
|
||||
{
|
||||
MainVM.IsProgressBarVisible = false;
|
||||
}
|
||||
|
||||
public void InstallPlugin(string path)
|
||||
{
|
||||
Application.Current.Dispatcher.Invoke(() => PluginManager.InstallPlugin(path));
|
||||
}
|
||||
|
||||
public void ReloadPlugins()
|
||||
{
|
||||
Application.Current.Dispatcher.Invoke(() => PluginManager.Init(this));
|
||||
}
|
||||
|
||||
public string GetTranslation(string key)
|
||||
{
|
||||
return InternationalizationManager.Instance.GetTranslation(key);
|
||||
}
|
||||
|
||||
public List<PluginPair> GetAllPlugins()
|
||||
{
|
||||
return PluginManager.AllPlugins.ToList();
|
||||
}
|
||||
|
||||
public event WoxKeyDownEventHandler BackKeyDownEvent;
|
||||
public event WoxGlobalKeyboardEventHandler GlobalKeyboardEvent;
|
||||
public event ResultItemDropEventHandler ResultItemDropEvent;
|
||||
|
||||
public void PushResults(Query query, PluginMetadata plugin, List<Result> results)
|
||||
{
|
||||
results.ForEach(o =>
|
||||
{
|
||||
o.PluginDirectory = plugin.PluginDirectory;
|
||||
o.PluginID = plugin.ID;
|
||||
o.OriginQuery = query;
|
||||
});
|
||||
MainVM.UpdateResultView(results, plugin, query);
|
||||
}
|
||||
|
||||
public void ShowContextMenu(PluginMetadata plugin, List<Result> results)
|
||||
{
|
||||
if (results != null && results.Count > 0)
|
||||
{
|
||||
results.ForEach(o =>
|
||||
{
|
||||
o.PluginDirectory = plugin.PluginDirectory;
|
||||
o.PluginID = plugin.ID;
|
||||
});
|
||||
|
||||
MainVM.ShowContextMenu(results, plugin.ID);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Methods
|
||||
|
||||
private bool KListener_hookedKeyboardCallback(KeyEvent keyevent, int vkcode, SpecialKeyState state)
|
||||
{
|
||||
if (GlobalKeyboardEvent != null)
|
||||
{
|
||||
return GlobalKeyboardEvent((int)keyevent, vkcode, state);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private void HideWox()
|
||||
{
|
||||
UserSettingStorage.Instance.WindowLeft = MainVM.Left;
|
||||
UserSettingStorage.Instance.WindowTop = MainVM.Top;
|
||||
MainVM.IsVisible = false;
|
||||
}
|
||||
|
||||
private void ShowWox(bool selectAll = true)
|
||||
{
|
||||
UserSettingStorage.Instance.IncreaseActivateTimes();
|
||||
MainVM.IsVisible = true;
|
||||
MainVM.SelectAllText = true;
|
||||
}
|
||||
|
||||
public void SetHotkey(string hotkeyStr, EventHandler<HotkeyEventArgs> action)
|
||||
{
|
||||
var hotkey = new HotkeyModel(hotkeyStr);
|
||||
SetHotkey(hotkey, action);
|
||||
}
|
||||
|
||||
public void SetHotkey(HotkeyModel hotkey, EventHandler<HotkeyEventArgs> action)
|
||||
{
|
||||
string hotkeyStr = hotkey.ToString();
|
||||
try
|
||||
{
|
||||
HotkeyManager.Current.AddOrReplace(hotkeyStr, hotkey.CharKey, hotkey.ModifierKeys, action);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
string errorMsg = string.Format(InternationalizationManager.Instance.GetTranslation("registerHotkeyFailed"), hotkeyStr);
|
||||
MessageBox.Show(errorMsg);
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveHotkey(string hotkeyStr)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(hotkeyStr))
|
||||
{
|
||||
HotkeyManager.Current.Remove(hotkeyStr);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if Wox should ignore any hotkeys
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private bool ShouldIgnoreHotkeys()
|
||||
{
|
||||
//double if to omit calling win32 function
|
||||
if (UserSettingStorage.Instance.IgnoreHotkeysOnFullscreen)
|
||||
if (WindowIntelopHelper.IsWindowFullscreen())
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private void SetCustomPluginHotkey()
|
||||
{
|
||||
if (UserSettingStorage.Instance.CustomPluginHotkeys == null) return;
|
||||
foreach (CustomPluginHotkey hotkey in UserSettingStorage.Instance.CustomPluginHotkeys)
|
||||
{
|
||||
CustomPluginHotkey hotkey1 = hotkey;
|
||||
SetHotkey(hotkey.Hotkey, delegate
|
||||
{
|
||||
if (ShouldIgnoreHotkeys()) return;
|
||||
ShowApp();
|
||||
ChangeQuery(hotkey1.ActionKeyword, true);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void OnHotkey(object sender, HotkeyEventArgs e)
|
||||
{
|
||||
if (ShouldIgnoreHotkeys()) return;
|
||||
ToggleWox();
|
||||
e.Handled = true;
|
||||
}
|
||||
|
||||
private void ToggleWox()
|
||||
{
|
||||
if (!MainVM.IsVisible)
|
||||
{
|
||||
ShowWox();
|
||||
}
|
||||
else
|
||||
{
|
||||
HideWox();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
|
@ -1,53 +1,67 @@
|
|||
<UserControl x:Class="Wox.ResultPanel"
|
||||
x:Name="Results"
|
||||
<ListBox x:Class="Wox.ResultListBox"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:converters="clr-namespace:Wox.Converters"
|
||||
mc:Ignorable="d" d:DesignWidth="100" d:DesignHeight="100">
|
||||
|
||||
<ListBox x:Name="lbResults" MaxHeight="{Binding ElementName=Results, Path=MaxResultsToShow}"
|
||||
HorizontalContentAlignment="Stretch" PreviewMouseDown="LbResults_OnPreviewMouseDown"
|
||||
xmlns:vm ="clr-namespace:Wox.ViewModel"
|
||||
mc:Ignorable="d" d:DesignWidth="100" d:DesignHeight="100" d:DataContext="{d:DesignInstance vm:ResultsViewModel}"
|
||||
MaxHeight="{Binding MaxHeight}" SelectedItem="{Binding SelectedResult}"
|
||||
HorizontalContentAlignment="Stretch" ItemsSource="{Binding Results}" Margin="{Binding Margin}"
|
||||
Style="{DynamicResource BaseListboxStyle}" SelectionChanged="lbResults_SelectionChanged" Focusable="False"
|
||||
KeyboardNavigation.DirectionalNavigation="Cycle" SelectionMode="Single"
|
||||
VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Standard">
|
||||
<ListBox.ItemTemplate>
|
||||
<DataTemplate>
|
||||
<!-- a result item height is 50 including margin -->
|
||||
<Grid HorizontalAlignment="Stretch" Height="40" VerticalAlignment="Stretch" Margin="5"
|
||||
<DataTemplate.DataType>
|
||||
<x:Type TypeName="vm:ResultViewModel" />
|
||||
</DataTemplate.DataType>
|
||||
<Button Command="{Binding OpenResultListBoxItemCommand}">
|
||||
<Button.InputBindings>
|
||||
<MouseBinding Command="{Binding OpenContextMenuItemCommand}" MouseAction="RightClick"></MouseBinding>
|
||||
</Button.InputBindings>
|
||||
<Button.Template>
|
||||
<ControlTemplate>
|
||||
<ContentPresenter Content="{TemplateBinding Button.Content}"></ContentPresenter>
|
||||
</ControlTemplate>
|
||||
</Button.Template>
|
||||
<Button.Content>
|
||||
<Grid HorizontalAlignment="Stretch" Height="40" VerticalAlignment="Stretch" Margin="5"
|
||||
Cursor="Hand">
|
||||
<Grid.Resources>
|
||||
<converters:ImagePathConverter x:Key="ImageConverter" />
|
||||
</Grid.Resources>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="32" />
|
||||
<ColumnDefinition />
|
||||
<ColumnDefinition Width="0" />
|
||||
</Grid.ColumnDefinitions>
|
||||
<Image x:Name="imgIco" Width="32" Height="32" HorizontalAlignment="Left"
|
||||
<Grid.Resources>
|
||||
<converters:ImagePathConverter x:Key="ImageConverter" />
|
||||
</Grid.Resources>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="32" />
|
||||
<ColumnDefinition />
|
||||
<ColumnDefinition Width="0" />
|
||||
</Grid.ColumnDefinitions>
|
||||
<Image x:Name="imgIco" Width="32" Height="32" HorizontalAlignment="Left"
|
||||
Source="{Binding FullIcoPath,Converter={StaticResource ImageConverter},IsAsync=True}" />
|
||||
<Grid Margin="5 0 5 0" Grid.Column="1" HorizontalAlignment="Stretch">
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition />
|
||||
<RowDefinition Height="Auto" x:Name="SubTitleRowDefinition" />
|
||||
</Grid.RowDefinitions>
|
||||
<TextBlock Style="{DynamicResource ItemTitleStyle}" DockPanel.Dock="Left"
|
||||
<Grid Margin="5 0 5 0" Grid.Column="1" HorizontalAlignment="Stretch">
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition />
|
||||
<RowDefinition Height="Auto" x:Name="SubTitleRowDefinition" />
|
||||
</Grid.RowDefinitions>
|
||||
<TextBlock Style="{DynamicResource ItemTitleStyle}" DockPanel.Dock="Left"
|
||||
VerticalAlignment="Center" ToolTip="{Binding Title}" x:Name="tbTitle"
|
||||
Text="{Binding Title}" />
|
||||
<TextBlock Style="{DynamicResource ItemSubTitleStyle}" ToolTip="{Binding SubTitle}"
|
||||
Text="{Binding Title}" >
|
||||
</TextBlock>
|
||||
<TextBlock Style="{DynamicResource ItemSubTitleStyle}" ToolTip="{Binding SubTitle}"
|
||||
Visibility="{Binding SubTitle, Converter={converters:StringNullOrEmptyToVisibilityConverter}}"
|
||||
Grid.Row="1" x:Name="tbSubTitle" Text="{Binding SubTitle}" />
|
||||
</Grid>
|
||||
<TextBlock Grid.Column="2" x:Name="tbItemNumber" Style="{DynamicResource ItemNumberStyle}" Text="9"/>
|
||||
</Grid>
|
||||
Grid.Row="1" x:Name="tbSubTitle" Text="{Binding SubTitle}" >
|
||||
</TextBlock>
|
||||
</Grid>
|
||||
</Grid>
|
||||
</Button.Content>
|
||||
</Button>
|
||||
<!-- a result item height is 50 including margin -->
|
||||
<DataTemplate.Triggers>
|
||||
<DataTrigger
|
||||
Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsSelected}"
|
||||
Value="True">
|
||||
<DataTrigger Binding="{Binding Path=IsSelected}" Value="True">
|
||||
<Setter TargetName="tbTitle" Property="Style" Value="{DynamicResource ItemTitleSelectedStyle}" />
|
||||
<Setter TargetName="tbSubTitle" Property="Style"
|
||||
Value="{DynamicResource ItemSubTitleSelectedStyle}" />
|
||||
</DataTrigger>
|
||||
<DataTrigger Binding="{Binding Path=IsSelected}" Value="True">
|
||||
<Setter TargetName="tbSubTitle" Property="Style" Value="{DynamicResource ItemSubTitleSelectedStyle}" />
|
||||
</DataTrigger>
|
||||
</DataTemplate.Triggers>
|
||||
</DataTemplate>
|
||||
|
|
@ -84,5 +98,4 @@
|
|||
</Setter>
|
||||
</Style>
|
||||
</ListBox.ItemContainerStyle>
|
||||
</ListBox>
|
||||
</UserControl>
|
||||
</ListBox>
|
||||
41
Wox/ResultListBox.xaml.cs
Normal file
41
Wox/ResultListBox.xaml.cs
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.Remoting.Contexts;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using Wox.Core.UserSettings;
|
||||
using Wox.Helper;
|
||||
using Wox.Plugin;
|
||||
using Wox.Storage;
|
||||
using Wox.ViewModel;
|
||||
|
||||
namespace Wox
|
||||
{
|
||||
[Synchronization]
|
||||
public partial class ResultListBox
|
||||
{
|
||||
public void AddResults(List<Result> newResults, string resultId)
|
||||
{
|
||||
var vm = DataContext as ResultsViewModel;
|
||||
vm.AddResults(newResults, resultId);
|
||||
}
|
||||
|
||||
|
||||
public ResultListBox()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private void lbResults_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
||||
{
|
||||
if (e.AddedItems.Count > 0 && e.AddedItems[0] != null)
|
||||
{
|
||||
ScrollIntoView(e.AddedItems[0]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -1,323 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.Remoting.Contexts;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using Wox.Core.UserSettings;
|
||||
using Wox.Helper;
|
||||
using Wox.Plugin;
|
||||
using Wox.Storage;
|
||||
|
||||
namespace Wox
|
||||
{
|
||||
[Synchronization]
|
||||
public partial class ResultPanel : UserControl
|
||||
{
|
||||
public event Action<Result> LeftMouseClickEvent;
|
||||
public event Action<Result> RightMouseClickEvent;
|
||||
public event Action<Result, IDataObject, DragEventArgs> ItemDropEvent;
|
||||
private readonly ListBoxItems _results;
|
||||
private readonly object _resultsUpdateLock = new object();
|
||||
|
||||
protected virtual void OnRightMouseClick(Result result)
|
||||
{
|
||||
Action<Result> handler = RightMouseClickEvent;
|
||||
if (handler != null) handler(result);
|
||||
}
|
||||
|
||||
protected virtual void OnLeftMouseClick(Result result)
|
||||
{
|
||||
Action<Result> handler = LeftMouseClickEvent;
|
||||
if (handler != null) handler(result);
|
||||
}
|
||||
|
||||
|
||||
public int MaxResultsToShow { get { return UserSettingStorage.Instance.MaxResultsToShow * 50; } }
|
||||
|
||||
internal void RemoveResultsFor(PluginMetadata metadata)
|
||||
{
|
||||
lock (_resultsUpdateLock)
|
||||
{
|
||||
_results.RemoveAll(r => r.PluginID == metadata.ID);
|
||||
}
|
||||
}
|
||||
|
||||
internal void RemoveResultsExcept(PluginMetadata metadata)
|
||||
{
|
||||
lock (_resultsUpdateLock)
|
||||
{
|
||||
_results.RemoveAll(r => r.PluginID != metadata.ID);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddResults(List<Result> newResults, string resultId)
|
||||
{
|
||||
lock (_resultsUpdateLock)
|
||||
{
|
||||
// todo use async to do new result calculation
|
||||
var resultsCopy = _results.ToList();
|
||||
var oldResults = resultsCopy.Where(r => r.PluginID == resultId).ToList();
|
||||
// intersection of A (old results) and B (new newResults)
|
||||
var intersection = oldResults.Intersect(newResults).ToList();
|
||||
// remove result of relative complement of B in A
|
||||
foreach (var result in oldResults.Except(intersection))
|
||||
{
|
||||
resultsCopy.Remove(result);
|
||||
}
|
||||
|
||||
// update scores
|
||||
foreach (var result in newResults)
|
||||
{
|
||||
if (IsTopMostResult(result))
|
||||
{
|
||||
result.Score = int.MaxValue;
|
||||
}
|
||||
}
|
||||
|
||||
// update index for result in intersection of A and B
|
||||
foreach (var commonResult in intersection)
|
||||
{
|
||||
int oldIndex = resultsCopy.IndexOf(commonResult);
|
||||
int oldScore = resultsCopy[oldIndex].Score;
|
||||
int newScore = newResults[newResults.IndexOf(commonResult)].Score;
|
||||
if (newScore != oldScore)
|
||||
{
|
||||
var oldResult = resultsCopy[oldIndex];
|
||||
oldResult.Score = newScore;
|
||||
resultsCopy.RemoveAt(oldIndex);
|
||||
int newIndex = InsertIndexOf(newScore, resultsCopy);
|
||||
resultsCopy.Insert(newIndex, oldResult);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// insert result in relative complement of A in B
|
||||
foreach (var result in newResults.Except(intersection))
|
||||
{
|
||||
int newIndex = InsertIndexOf(result.Score, resultsCopy);
|
||||
resultsCopy.Insert(newIndex, result);
|
||||
}
|
||||
|
||||
// update UI in one run, so it can avoid UI flickering
|
||||
_results.Update(resultsCopy);
|
||||
|
||||
lbResults.Margin = lbResults.Items.Count > 0 ? new Thickness { Top = 8 } : new Thickness { Top = 0 };
|
||||
SelectFirst();
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsTopMostResult(Result result)
|
||||
{
|
||||
return TopMostRecordStorage.Instance.IsTopMost(result);
|
||||
}
|
||||
|
||||
private int InsertIndexOf(int newScore, IList<Result> list)
|
||||
{
|
||||
int index = 0;
|
||||
for (; index < list.Count; index++)
|
||||
{
|
||||
var result = list[index];
|
||||
if (newScore > result.Score)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
public void SelectNext()
|
||||
{
|
||||
int index = lbResults.SelectedIndex;
|
||||
if (index == lbResults.Items.Count - 1)
|
||||
{
|
||||
index = -1;
|
||||
}
|
||||
Select(index + 1);
|
||||
}
|
||||
|
||||
public void SelectPrev()
|
||||
{
|
||||
int index = lbResults.SelectedIndex;
|
||||
if (index == 0)
|
||||
{
|
||||
index = lbResults.Items.Count;
|
||||
}
|
||||
Select(index - 1);
|
||||
}
|
||||
|
||||
private void SelectFirst()
|
||||
{
|
||||
Select(0);
|
||||
}
|
||||
|
||||
private void Select(int index)
|
||||
{
|
||||
if (index >= 0 && index < lbResults.Items.Count)
|
||||
{
|
||||
lbResults.SelectedItem = lbResults.Items.GetItemAt(index);
|
||||
}
|
||||
}
|
||||
|
||||
public List<Result> GetVisibleResults()
|
||||
{
|
||||
List<Result> visibleElements = new List<Result>();
|
||||
VirtualizingStackPanel virtualizingStackPanel = GetInnerStackPanel(lbResults);
|
||||
for (int i = (int)virtualizingStackPanel.VerticalOffset; i <= virtualizingStackPanel.VerticalOffset + virtualizingStackPanel.ViewportHeight; i++)
|
||||
{
|
||||
ListBoxItem item = lbResults.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
|
||||
if (item != null)
|
||||
{
|
||||
visibleElements.Add(item.DataContext as Result);
|
||||
}
|
||||
}
|
||||
return visibleElements;
|
||||
}
|
||||
|
||||
private void UpdateItemNumber()
|
||||
{
|
||||
//VirtualizingStackPanel virtualizingStackPanel = GetInnerStackPanel(lbResults);
|
||||
//int index = 0;
|
||||
//for (int i = (int)virtualizingStackPanel.VerticalOffset; i <= virtualizingStackPanel.VerticalOffset + virtualizingStackPanel.ViewportHeight; i++)
|
||||
//{
|
||||
// index++;
|
||||
// ListBoxItem item = lbResults.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
|
||||
// if (item != null)
|
||||
// {
|
||||
// ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(item);
|
||||
// if (myContentPresenter != null)
|
||||
// {
|
||||
// DataTemplate dataTemplate = myContentPresenter.ContentTemplate;
|
||||
// TextBlock tbItemNumber = (TextBlock)dataTemplate.FindName("tbItemNumber", myContentPresenter);
|
||||
// tbItemNumber.Text = index.ToString();
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
}
|
||||
|
||||
private childItem FindVisualChild<childItem>(DependencyObject obj) where childItem : DependencyObject
|
||||
{
|
||||
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
|
||||
{
|
||||
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
|
||||
if (child != null && child is childItem)
|
||||
return (childItem)child;
|
||||
else
|
||||
{
|
||||
childItem childOfChild = FindVisualChild<childItem>(child);
|
||||
if (childOfChild != null)
|
||||
return childOfChild;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private VirtualizingStackPanel GetInnerStackPanel(FrameworkElement element)
|
||||
{
|
||||
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++)
|
||||
{
|
||||
var child = VisualTreeHelper.GetChild(element, i) as FrameworkElement;
|
||||
|
||||
if (child == null) continue;
|
||||
|
||||
if (child is VirtualizingStackPanel) return child as VirtualizingStackPanel;
|
||||
|
||||
var panel = GetInnerStackPanel(child);
|
||||
|
||||
if (panel != null)
|
||||
return panel;
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
public Result GetActiveResult()
|
||||
{
|
||||
int index = lbResults.SelectedIndex;
|
||||
if (index < 0) return null;
|
||||
|
||||
return lbResults.Items[index] as Result;
|
||||
}
|
||||
|
||||
public ResultPanel()
|
||||
{
|
||||
InitializeComponent();
|
||||
_results = new ListBoxItems();
|
||||
lbResults.ItemsSource = _results;
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
lock (_resultsUpdateLock)
|
||||
{
|
||||
_results.Clear();
|
||||
lbResults.Margin = new Thickness { Top = 0 };
|
||||
}
|
||||
}
|
||||
|
||||
private void lbResults_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
||||
{
|
||||
if (e.AddedItems.Count > 0 && e.AddedItems[0] != null)
|
||||
{
|
||||
lbResults.ScrollIntoView(e.AddedItems[0]);
|
||||
//Dispatcher.DelayInvoke("UpdateItemNumber", () =>
|
||||
//{
|
||||
//UpdateItemNumber();
|
||||
//}, TimeSpan.FromMilliseconds(3));
|
||||
}
|
||||
}
|
||||
|
||||
private void LbResults_OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
var item = ItemsControl.ContainerFromElement(lbResults, e.OriginalSource as DependencyObject) as ListBoxItem;
|
||||
if (item != null && e.ChangedButton == MouseButton.Left)
|
||||
{
|
||||
OnLeftMouseClick(item.DataContext as Result);
|
||||
}
|
||||
if (item != null && e.ChangedButton == MouseButton.Right)
|
||||
{
|
||||
OnRightMouseClick(item.DataContext as Result);
|
||||
}
|
||||
}
|
||||
|
||||
public void SelectNextPage()
|
||||
{
|
||||
int index = lbResults.SelectedIndex;
|
||||
index += 5;
|
||||
if (index >= lbResults.Items.Count)
|
||||
{
|
||||
index = lbResults.Items.Count - 1;
|
||||
}
|
||||
Select(index);
|
||||
}
|
||||
|
||||
public void SelectPrevPage()
|
||||
{
|
||||
int index = lbResults.SelectedIndex;
|
||||
index -= 5;
|
||||
if (index < 0)
|
||||
{
|
||||
index = 0;
|
||||
}
|
||||
Select(index);
|
||||
}
|
||||
|
||||
private void ListBoxItem_OnDrop(object sender, DragEventArgs e)
|
||||
{
|
||||
var item = ItemsControl.ContainerFromElement(lbResults, e.OriginalSource as DependencyObject) as ListBoxItem;
|
||||
if (item != null)
|
||||
{
|
||||
OnItemDropEvent(item.DataContext as Result, e.Data, e);
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void OnItemDropEvent(Result obj, IDataObject data, DragEventArgs e)
|
||||
{
|
||||
var handler = ItemDropEvent;
|
||||
if (handler != null) handler(obj, data, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -149,7 +149,7 @@
|
|||
<RowDefinition/>
|
||||
</Grid.RowDefinitions>
|
||||
<TextBox Text="{DynamicResource helloWox}" IsReadOnly="True" Style="{DynamicResource QueryBoxStyle}" Grid.Row="0"/>
|
||||
<wox:ResultPanel Grid.Row="1" x:Name="resultPanelPreview"/>
|
||||
<wox:ResultListBox Grid.Row="1" x:Name="ResultListBoxPreview"/>
|
||||
</Grid>
|
||||
</Border>
|
||||
</StackPanel>
|
||||
|
|
@ -177,10 +177,10 @@
|
|||
</StackPanel>
|
||||
<StackPanel Orientation="Horizontal" Margin="2">
|
||||
<TextBlock Text="{DynamicResource resultItemFont}" />
|
||||
<ComboBox Margin="5 -2 5 0" x:Name="cbResultItemFont" ItemsSource="{x:Static Fonts.SystemFontFamilies}" SelectionChanged="CbResultItemFont_OnSelectionChanged" HorizontalAlignment="Left" VerticalAlignment="Top" Width="160"/>
|
||||
<ComboBox Margin="5 -2 5 0" x:Name="ResultFontComboBox" ItemsSource="{x:Static Fonts.SystemFontFamilies}" SelectionChanged="OnResultFontSelectionChanged" HorizontalAlignment="Left" VerticalAlignment="Top" Width="160"/>
|
||||
<ComboBox Margin="0 -2 0 0"
|
||||
x:Name="cbResultItemFontFaces"
|
||||
ItemsSource="{Binding SelectedValue.FamilyTypefaces, ElementName=cbResultItemFont}" SelectionChanged="CbResultItemFontFaces_OnSelectionChanged" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120">
|
||||
x:Name="ResultFontFacesComboBox"
|
||||
ItemsSource="{Binding SelectedValue.FamilyTypefaces, ElementName=ResultFontComboBox}" SelectionChanged="OnResultFontFacesSelectionChanged" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120">
|
||||
<ComboBox.ItemTemplate>
|
||||
<DataTemplate>
|
||||
<ItemsControl ItemsSource="{Binding AdjustedFaceNames}">
|
||||
|
|
|
|||
|
|
@ -19,20 +19,25 @@ using Wox.Helper;
|
|||
using Wox.Plugin;
|
||||
using Application = System.Windows.Forms.Application;
|
||||
using Stopwatch = Wox.Infrastructure.Stopwatch;
|
||||
using Wox.Infrastructure.Hotkey;
|
||||
using NHotkey.Wpf;
|
||||
using NHotkey;
|
||||
using Wox.ViewModel;
|
||||
|
||||
namespace Wox
|
||||
{
|
||||
public partial class SettingWindow : Window
|
||||
{
|
||||
public readonly MainWindow MainWindow;
|
||||
public readonly IPublicAPI _api;
|
||||
bool settingsLoaded;
|
||||
private Dictionary<ISettingProvider, Control> featureControls = new Dictionary<ISettingProvider, Control>();
|
||||
private bool themeTabLoaded;
|
||||
|
||||
public SettingWindow(MainWindow mainWindow)
|
||||
public SettingWindow(IPublicAPI api)
|
||||
{
|
||||
MainWindow = mainWindow;
|
||||
_api = api;
|
||||
InitializeComponent();
|
||||
ResultListBoxPreview.DataContext = new ResultsViewModel();
|
||||
Loaded += Setting_Loaded;
|
||||
}
|
||||
|
||||
|
|
@ -94,7 +99,7 @@ namespace Wox
|
|||
{
|
||||
UserSettingStorage.Instance.MaxResultsToShow = (int)comboMaxResultsToShow.SelectedItem;
|
||||
UserSettingStorage.Instance.Save();
|
||||
MainWindow.pnlResult.lbResults.GetBindingExpression(MaxHeightProperty).UpdateTarget();
|
||||
//MainWindow.pnlResult.lbResults.GetBindingExpression(MaxHeightProperty).UpdateTarget();
|
||||
};
|
||||
|
||||
cbHideWhenDeactive.IsChecked = UserSettingStorage.Instance.HideWhenDeactive;
|
||||
|
|
@ -250,23 +255,45 @@ namespace Wox
|
|||
{
|
||||
if (ctlHotkey.CurrentHotkeyAvailable)
|
||||
{
|
||||
MainWindow.SetHotkey(ctlHotkey.CurrentHotkey, delegate
|
||||
SetHotkey(ctlHotkey.CurrentHotkey, delegate
|
||||
{
|
||||
if (!MainWindow.IsVisible)
|
||||
if (!App.Window.IsVisible)
|
||||
{
|
||||
MainWindow.ShowApp();
|
||||
_api.ShowApp();
|
||||
}
|
||||
else
|
||||
{
|
||||
MainWindow.HideApp();
|
||||
_api.HideApp();
|
||||
}
|
||||
});
|
||||
MainWindow.RemoveHotkey(UserSettingStorage.Instance.Hotkey);
|
||||
RemoveHotkey(UserSettingStorage.Instance.Hotkey);
|
||||
UserSettingStorage.Instance.Hotkey = ctlHotkey.CurrentHotkey.ToString();
|
||||
UserSettingStorage.Instance.Save();
|
||||
}
|
||||
}
|
||||
|
||||
void SetHotkey(HotkeyModel hotkey, EventHandler<HotkeyEventArgs> action)
|
||||
{
|
||||
string hotkeyStr = hotkey.ToString();
|
||||
try
|
||||
{
|
||||
HotkeyManager.Current.AddOrReplace(hotkeyStr, hotkey.CharKey, hotkey.ModifierKeys, action);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
string errorMsg = string.Format(InternationalizationManager.Instance.GetTranslation("registerHotkeyFailed"), hotkeyStr);
|
||||
MessageBox.Show(errorMsg);
|
||||
}
|
||||
}
|
||||
|
||||
void RemoveHotkey(string hotkeyStr)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(hotkeyStr))
|
||||
{
|
||||
HotkeyManager.Current.Remove(hotkeyStr);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnHotkeyTabSelected()
|
||||
{
|
||||
ctlHotkey.HotkeyChanged += ctlHotkey_OnHotkeyChanged;
|
||||
|
|
@ -289,7 +316,7 @@ namespace Wox
|
|||
UserSettingStorage.Instance.CustomPluginHotkeys.Remove(item);
|
||||
lvCustomHotkey.Items.Refresh();
|
||||
UserSettingStorage.Instance.Save();
|
||||
MainWindow.RemoveHotkey(item.Hotkey);
|
||||
RemoveHotkey(item.Hotkey);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -348,19 +375,19 @@ namespace Wox
|
|||
UserSettingStorage.Instance.QueryBoxFontStretch
|
||||
));
|
||||
}
|
||||
if (!string.IsNullOrEmpty(UserSettingStorage.Instance.ResultItemFont) &&
|
||||
Fonts.SystemFontFamilies.Count(o => o.FamilyNames.Values.Contains(UserSettingStorage.Instance.ResultItemFont)) > 0)
|
||||
if (!string.IsNullOrEmpty(UserSettingStorage.Instance.ResultFont) &&
|
||||
Fonts.SystemFontFamilies.Count(o => o.FamilyNames.Values.Contains(UserSettingStorage.Instance.ResultFont)) > 0)
|
||||
{
|
||||
cbResultItemFont.Text = UserSettingStorage.Instance.ResultItemFont;
|
||||
ResultFontComboBox.Text = UserSettingStorage.Instance.ResultFont;
|
||||
|
||||
cbResultItemFontFaces.SelectedItem = SyntaxSugars.CallOrRescueDefault(() => ((FontFamily)cbResultItemFont.SelectedItem).ConvertFromInvariantStringsOrNormal(
|
||||
UserSettingStorage.Instance.ResultItemFontStyle,
|
||||
UserSettingStorage.Instance.ResultItemFontWeight,
|
||||
UserSettingStorage.Instance.ResultItemFontStretch
|
||||
ResultFontFacesComboBox.SelectedItem = SyntaxSugars.CallOrRescueDefault(() => ((FontFamily)ResultFontComboBox.SelectedItem).ConvertFromInvariantStringsOrNormal(
|
||||
UserSettingStorage.Instance.ResultFontStyle,
|
||||
UserSettingStorage.Instance.ResultFontWeight,
|
||||
UserSettingStorage.Instance.ResultFontStretch
|
||||
));
|
||||
}
|
||||
|
||||
resultPanelPreview.AddResults(new List<Result>
|
||||
ResultListBoxPreview.AddResults(new List<Result>
|
||||
{
|
||||
new Result
|
||||
{
|
||||
|
|
@ -475,30 +502,30 @@ namespace Wox
|
|||
}
|
||||
}
|
||||
|
||||
private void CbResultItemFont_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
|
||||
private void OnResultFontSelectionChanged(object sender, SelectionChangedEventArgs e)
|
||||
{
|
||||
if (!settingsLoaded) return;
|
||||
string resultItemFont = cbResultItemFont.SelectedItem.ToString();
|
||||
UserSettingStorage.Instance.ResultItemFont = resultItemFont;
|
||||
cbResultItemFontFaces.SelectedItem = ((FontFamily)cbResultItemFont.SelectedItem).ChooseRegularFamilyTypeface();
|
||||
string resultItemFont = ResultFontComboBox.SelectedItem.ToString();
|
||||
UserSettingStorage.Instance.ResultFont = resultItemFont;
|
||||
ResultFontFacesComboBox.SelectedItem = ((FontFamily)ResultFontComboBox.SelectedItem).ChooseRegularFamilyTypeface();
|
||||
UserSettingStorage.Instance.Save();
|
||||
ThemeManager.Theme.ChangeTheme(UserSettingStorage.Instance.Theme);
|
||||
}
|
||||
|
||||
private void CbResultItemFontFaces_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
|
||||
private void OnResultFontFacesSelectionChanged(object sender, SelectionChangedEventArgs e)
|
||||
{
|
||||
if (!settingsLoaded) return;
|
||||
FamilyTypeface typeface = (FamilyTypeface)cbResultItemFontFaces.SelectedItem;
|
||||
FamilyTypeface typeface = (FamilyTypeface)ResultFontFacesComboBox.SelectedItem;
|
||||
if (typeface == null)
|
||||
{
|
||||
if (cbResultItemFontFaces.Items.Count > 0)
|
||||
cbResultItemFontFaces.SelectedIndex = 0;
|
||||
if (ResultFontFacesComboBox.Items.Count > 0)
|
||||
ResultFontFacesComboBox.SelectedIndex = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
UserSettingStorage.Instance.ResultItemFontStretch = typeface.Stretch.ToString();
|
||||
UserSettingStorage.Instance.ResultItemFontWeight = typeface.Weight.ToString();
|
||||
UserSettingStorage.Instance.ResultItemFontStyle = typeface.Style.ToString();
|
||||
UserSettingStorage.Instance.ResultFontStretch = typeface.Stretch.ToString();
|
||||
UserSettingStorage.Instance.ResultFontWeight = typeface.Weight.ToString();
|
||||
UserSettingStorage.Instance.ResultFontStyle = typeface.Style.ToString();
|
||||
UserSettingStorage.Instance.Save();
|
||||
ThemeManager.Theme.ChangeTheme(UserSettingStorage.Instance.Theme);
|
||||
}
|
||||
|
|
|
|||
50
Wox/ViewModel/BaseViewModel.cs
Normal file
50
Wox/ViewModel/BaseViewModel.cs
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace Wox.ViewModel
|
||||
{
|
||||
public class BaseViewModel : INotifyPropertyChanged
|
||||
{
|
||||
|
||||
protected void OnPropertyChanged(string propertyName)
|
||||
{
|
||||
if (null != PropertyChanged)
|
||||
{
|
||||
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
|
||||
}
|
||||
}
|
||||
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
}
|
||||
|
||||
public class RelayCommand : ICommand
|
||||
{
|
||||
|
||||
private Action<object> _action;
|
||||
|
||||
public RelayCommand(Action<object> action)
|
||||
{
|
||||
_action = action;
|
||||
}
|
||||
|
||||
public virtual bool CanExecute(object parameter)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public event EventHandler CanExecuteChanged;
|
||||
|
||||
public virtual void Execute(object parameter)
|
||||
{
|
||||
if (null != _action)
|
||||
{
|
||||
_action(parameter);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
633
Wox/ViewModel/MainViewModel.cs
Normal file
633
Wox/ViewModel/MainViewModel.cs
Normal file
|
|
@ -0,0 +1,633 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Forms;
|
||||
using System.Windows.Input;
|
||||
using Wox.Core.Plugin;
|
||||
using Wox.Core.Resource;
|
||||
using Wox.Core.UserSettings;
|
||||
using Wox.Infrastructure;
|
||||
using Wox.Infrastructure.Hotkey;
|
||||
using Wox.Plugin;
|
||||
using Wox.Storage;
|
||||
|
||||
namespace Wox.ViewModel
|
||||
{
|
||||
public class MainViewModel : BaseViewModel
|
||||
{
|
||||
#region Private Fields
|
||||
|
||||
private string _queryText;
|
||||
private bool _isVisible;
|
||||
private bool _isResultListBoxVisible;
|
||||
private bool _isContextMenuVisible;
|
||||
private bool _isProgressBarVisible;
|
||||
private bool _isProgressBarTooltipVisible;
|
||||
private bool _selectAllText;
|
||||
private int _caretIndex;
|
||||
private double _left;
|
||||
private double _top;
|
||||
|
||||
private bool _queryHasReturn;
|
||||
private Query _lastQuery = new Query();
|
||||
private bool _ignoreTextChange;
|
||||
private List<Result> CurrentContextMenus = new List<Result>();
|
||||
private string _textBeforeEnterContextMenuMode;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
|
||||
public MainViewModel()
|
||||
{
|
||||
InitializeResultListBox();
|
||||
InitializeContextMenu();
|
||||
InitializeKeyCommands();
|
||||
|
||||
_queryHasReturn = false;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region ViewModel Properties
|
||||
|
||||
public ResultsViewModel Results { get; private set; }
|
||||
|
||||
public ResultsViewModel ContextMenu { get; private set; }
|
||||
|
||||
public string QueryText
|
||||
{
|
||||
get
|
||||
{
|
||||
return _queryText;
|
||||
}
|
||||
set
|
||||
{
|
||||
_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 IsVisible
|
||||
{
|
||||
get
|
||||
{
|
||||
return _isVisible;
|
||||
}
|
||||
set
|
||||
{
|
||||
_isVisible = value;
|
||||
OnPropertyChanged("IsVisible");
|
||||
|
||||
if (!value && IsContextMenuVisible)
|
||||
{
|
||||
BackToSearchMode();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsResultListBoxVisible
|
||||
{
|
||||
get
|
||||
{
|
||||
return _isResultListBoxVisible;
|
||||
}
|
||||
set
|
||||
{
|
||||
_isResultListBoxVisible = value;
|
||||
OnPropertyChanged("IsResultListBoxVisible");
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsContextMenuVisible
|
||||
{
|
||||
get
|
||||
{
|
||||
return _isContextMenuVisible;
|
||||
}
|
||||
set
|
||||
{
|
||||
_isContextMenuVisible = value;
|
||||
OnPropertyChanged("IsContextMenuVisible");
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsProgressBarVisible
|
||||
{
|
||||
get
|
||||
{
|
||||
return _isProgressBarVisible;
|
||||
}
|
||||
set
|
||||
{
|
||||
_isProgressBarVisible = value;
|
||||
OnPropertyChanged("IsProgressBarVisible");
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsProgressBarTooltipVisible
|
||||
{
|
||||
get
|
||||
{
|
||||
return _isProgressBarTooltipVisible;
|
||||
}
|
||||
set
|
||||
{
|
||||
_isProgressBarTooltipVisible = value;
|
||||
OnPropertyChanged("IsProgressBarTooltipVisible");
|
||||
}
|
||||
}
|
||||
|
||||
public double Left
|
||||
{
|
||||
get
|
||||
{
|
||||
return _left;
|
||||
}
|
||||
set
|
||||
{
|
||||
_left = value;
|
||||
OnPropertyChanged("Left");
|
||||
}
|
||||
}
|
||||
|
||||
public double Top
|
||||
{
|
||||
get
|
||||
{
|
||||
return _top;
|
||||
}
|
||||
set
|
||||
{
|
||||
_top = value;
|
||||
OnPropertyChanged("Top");
|
||||
}
|
||||
}
|
||||
|
||||
public ICommand EscCommand { get; set; }
|
||||
|
||||
public ICommand SelectNextItemCommand { get; set; }
|
||||
|
||||
public ICommand SelectPrevItemCommand { get; set; }
|
||||
|
||||
public ICommand CtrlOCommand { get; set; }
|
||||
|
||||
public ICommand DisplayNextQueryCommand { get; set; }
|
||||
|
||||
public ICommand DisplayPrevQueryCommand { get; set; }
|
||||
|
||||
public ICommand SelectNextPageCommand { get; set; }
|
||||
|
||||
public ICommand SelectPrevPageCommand { get; set; }
|
||||
|
||||
public ICommand StartHelpCommand { get; set; }
|
||||
public ICommand ShiftEnterCommand { get; set; }
|
||||
public ICommand OpenResultCommand { get; set; }
|
||||
public ICommand BackCommand { get; set; }
|
||||
#endregion
|
||||
|
||||
#region Private Methods
|
||||
|
||||
private void InitializeKeyCommands()
|
||||
{
|
||||
EscCommand = new RelayCommand((parameter) =>
|
||||
{
|
||||
|
||||
if (IsContextMenuVisible)
|
||||
{
|
||||
BackToSearchMode();
|
||||
}
|
||||
else
|
||||
{
|
||||
IsVisible = false;
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
SelectNextItemCommand = new RelayCommand((parameter) =>
|
||||
{
|
||||
|
||||
if (IsContextMenuVisible)
|
||||
{
|
||||
ContextMenu.SelectNextResult();
|
||||
}
|
||||
else
|
||||
{
|
||||
Results.SelectNextResult();
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
SelectPrevItemCommand = new RelayCommand((parameter) =>
|
||||
{
|
||||
|
||||
if (IsContextMenuVisible)
|
||||
{
|
||||
ContextMenu.SelectPrevResult();
|
||||
}
|
||||
else
|
||||
{
|
||||
Results.SelectPrevResult();
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
CtrlOCommand = new RelayCommand((parameter) =>
|
||||
{
|
||||
|
||||
if (IsContextMenuVisible)
|
||||
{
|
||||
BackToSearchMode();
|
||||
}
|
||||
else
|
||||
{
|
||||
ShowContextMenu(Results.SelectedResult.RawResult);
|
||||
}
|
||||
});
|
||||
|
||||
DisplayNextQueryCommand = new RelayCommand((parameter) =>
|
||||
{
|
||||
|
||||
var nextQuery = QueryHistoryStorage.Instance.Next();
|
||||
DisplayQueryHistory(nextQuery);
|
||||
|
||||
});
|
||||
|
||||
DisplayPrevQueryCommand = new RelayCommand((parameter) =>
|
||||
{
|
||||
|
||||
var prev = QueryHistoryStorage.Instance.Previous();
|
||||
DisplayQueryHistory(prev);
|
||||
|
||||
});
|
||||
|
||||
SelectNextPageCommand = new RelayCommand((parameter) =>
|
||||
{
|
||||
|
||||
Results.SelectNextPage();
|
||||
|
||||
});
|
||||
|
||||
SelectPrevPageCommand = new RelayCommand((parameter) =>
|
||||
{
|
||||
|
||||
Results.SelectPrevPage();
|
||||
|
||||
});
|
||||
|
||||
StartHelpCommand = new RelayCommand((parameter) =>
|
||||
{
|
||||
Process.Start("http://doc.getwox.com");
|
||||
});
|
||||
|
||||
ShiftEnterCommand = new RelayCommand((parameter) =>
|
||||
{
|
||||
|
||||
if (!IsContextMenuVisible && null != Results.SelectedResult)
|
||||
{
|
||||
ShowContextMenu(Results.SelectedResult.RawResult);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
OpenResultCommand = new RelayCommand((parameter) =>
|
||||
{
|
||||
|
||||
if (null != parameter)
|
||||
{
|
||||
var index = int.Parse(parameter.ToString());
|
||||
Results.SelectResult(index);
|
||||
}
|
||||
|
||||
if (null != Results.SelectedResult)
|
||||
{
|
||||
Results.SelectedResult.OpenResultListBoxItemCommand.Execute(null);
|
||||
}
|
||||
});
|
||||
|
||||
BackCommand = new RelayCommand((parameter) =>
|
||||
{
|
||||
if (null != ListeningKeyPressed)
|
||||
{
|
||||
ListeningKeyPressed(this, new ListeningKeyPressedEventArgs(parameter as System.Windows.Input.KeyEventArgs));
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
private void InitializeResultListBox()
|
||||
{
|
||||
Results = new ResultsViewModel();
|
||||
IsResultListBoxVisible = false;
|
||||
}
|
||||
|
||||
private void ShowContextMenu(Result result)
|
||||
{
|
||||
if (result == null) return;
|
||||
ShowContextMenu(result, PluginManager.GetContextMenusForPlugin(result));
|
||||
}
|
||||
|
||||
private void ShowContextMenu(Result result, List<Result> actions)
|
||||
{
|
||||
actions.ForEach(o =>
|
||||
{
|
||||
o.PluginDirectory = PluginManager.GetPluginForId(result.PluginID).Metadata.PluginDirectory;
|
||||
o.PluginID = result.PluginID;
|
||||
o.OriginQuery = result.OriginQuery;
|
||||
});
|
||||
|
||||
actions.Add(GetTopMostContextMenu(result));
|
||||
|
||||
DisplayContextMenu(actions, result.PluginID);
|
||||
}
|
||||
|
||||
private void DisplayContextMenu(List<Result> actions, string pluginID)
|
||||
{
|
||||
_textBeforeEnterContextMenuMode = QueryText;
|
||||
|
||||
ContextMenu.Clear();
|
||||
ContextMenu.AddResults(actions, pluginID);
|
||||
CurrentContextMenus = actions;
|
||||
|
||||
IsContextMenuVisible = true;
|
||||
IsResultListBoxVisible = false;
|
||||
|
||||
QueryText = "";
|
||||
}
|
||||
|
||||
private Result GetTopMostContextMenu(Result result)
|
||||
{
|
||||
if (TopMostRecordStorage.Instance.IsTopMost(result))
|
||||
{
|
||||
return new Result(InternationalizationManager.Instance.GetTranslation("cancelTopMostInThisQuery"), "Images\\down.png")
|
||||
{
|
||||
PluginDirectory = WoxDirectroy.Executable,
|
||||
Action = _ =>
|
||||
{
|
||||
TopMostRecordStorage.Instance.Remove(result);
|
||||
App.API.ShowMsg("Succeed", "", "");
|
||||
return false;
|
||||
}
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
return new Result(InternationalizationManager.Instance.GetTranslation("setAsTopMostInThisQuery"), "Images\\up.png")
|
||||
{
|
||||
PluginDirectory = WoxDirectroy.Executable,
|
||||
Action = _ =>
|
||||
{
|
||||
TopMostRecordStorage.Instance.AddOrUpdate(result);
|
||||
App.API.ShowMsg("Succeed", "", "");
|
||||
return false;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
private void InitializeContextMenu()
|
||||
{
|
||||
ContextMenu = new ResultsViewModel();
|
||||
IsContextMenuVisible = false;
|
||||
}
|
||||
|
||||
private void HandleQueryTextUpdated()
|
||||
{
|
||||
if (_ignoreTextChange) { _ignoreTextChange = false; return; }
|
||||
|
||||
IsProgressBarTooltipVisible = false;
|
||||
if (IsContextMenuVisible)
|
||||
{
|
||||
QueryContextMenu();
|
||||
}
|
||||
else
|
||||
{
|
||||
string query = QueryText.Trim();
|
||||
if (!string.IsNullOrEmpty(query))
|
||||
{
|
||||
Query(query);
|
||||
//reset query history index after user start new query
|
||||
ResetQueryHistoryIndex();
|
||||
}
|
||||
else
|
||||
{
|
||||
Results.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void QueryContextMenu()
|
||||
{
|
||||
var contextMenuId = "Context Menu Id";
|
||||
ContextMenu.Clear();
|
||||
var query = QueryText.ToLower();
|
||||
if (string.IsNullOrEmpty(query))
|
||||
{
|
||||
ContextMenu.AddResults(CurrentContextMenus, contextMenuId);
|
||||
}
|
||||
else
|
||||
{
|
||||
List<Result> filterResults = new List<Result>();
|
||||
foreach (Result contextMenu in CurrentContextMenus)
|
||||
{
|
||||
if (StringMatcher.IsMatch(contextMenu.Title, query)
|
||||
|| StringMatcher.IsMatch(contextMenu.SubTitle, query))
|
||||
{
|
||||
filterResults.Add(contextMenu);
|
||||
}
|
||||
}
|
||||
ContextMenu.AddResults(filterResults, contextMenuId);
|
||||
}
|
||||
}
|
||||
|
||||
private void Query(string text)
|
||||
{
|
||||
_queryHasReturn = false;
|
||||
var query = PluginManager.QueryInit(text);
|
||||
if (query != null)
|
||||
{
|
||||
// handle the exclusiveness of plugin using action keyword
|
||||
string lastKeyword = _lastQuery.ActionKeyword;
|
||||
string keyword = query.ActionKeyword;
|
||||
if (string.IsNullOrEmpty(lastKeyword))
|
||||
{
|
||||
if (!string.IsNullOrEmpty(keyword))
|
||||
{
|
||||
Results.RemoveResultsExcept(PluginManager.NonGlobalPlugins[keyword].Metadata);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (string.IsNullOrEmpty(keyword))
|
||||
{
|
||||
Results.RemoveResultsFor(PluginManager.NonGlobalPlugins[lastKeyword].Metadata);
|
||||
}
|
||||
else if (lastKeyword != keyword)
|
||||
{
|
||||
Results.RemoveResultsExcept(PluginManager.NonGlobalPlugins[keyword].Metadata);
|
||||
}
|
||||
}
|
||||
_lastQuery = query;
|
||||
|
||||
Action action = new Action(async () =>
|
||||
{
|
||||
await Task.Delay(150);
|
||||
if (!string.IsNullOrEmpty(query.RawQuery) && query.RawQuery == _lastQuery.RawQuery && !_queryHasReturn)
|
||||
{
|
||||
IsProgressBarTooltipVisible = true;
|
||||
}
|
||||
});
|
||||
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);
|
||||
}
|
||||
|
||||
IsProgressBarTooltipVisible = false;
|
||||
}
|
||||
|
||||
private void ResetQueryHistoryIndex()
|
||||
{
|
||||
Results.RemoveResultsFor(QueryHistoryStorage.MetaData);
|
||||
QueryHistoryStorage.Instance.Reset();
|
||||
}
|
||||
|
||||
private void UpdateResultViewInternal(List<Result> list, PluginMetadata metadata)
|
||||
{
|
||||
Infrastructure.Stopwatch.Normal($"UI update cost for {metadata.Name}",
|
||||
() => { Results.AddResults(list, metadata.ID); });
|
||||
}
|
||||
|
||||
private void BackToSearchMode()
|
||||
{
|
||||
QueryText = _textBeforeEnterContextMenuMode;
|
||||
IsContextMenuVisible = false;
|
||||
IsResultListBoxVisible = true;
|
||||
CaretIndex = QueryText.Length;
|
||||
}
|
||||
|
||||
private void DisplayQueryHistory(HistoryItem history)
|
||||
{
|
||||
if (history != null)
|
||||
{
|
||||
var historyMetadata = QueryHistoryStorage.MetaData;
|
||||
|
||||
QueryText = history.Query;
|
||||
SelectAllText = true;
|
||||
|
||||
var executeQueryHistoryTitle = InternationalizationManager.Instance.GetTranslation("executeQuery");
|
||||
var lastExecuteTime = InternationalizationManager.Instance.GetTranslation("lastExecuteTime");
|
||||
Results.RemoveResultsExcept(historyMetadata);
|
||||
UpdateResultViewInternal(new List<Result>
|
||||
{
|
||||
new Result
|
||||
{
|
||||
Title = string.Format(executeQueryHistoryTitle,history.Query),
|
||||
SubTitle = string.Format(lastExecuteTime,history.ExecutedDateTime),
|
||||
IcoPath = "Images\\history.png",
|
||||
PluginDirectory = WoxDirectroy.Executable,
|
||||
Action = _ =>{
|
||||
|
||||
QueryText = history.Query;
|
||||
SelectAllText = true;
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}, historyMetadata);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Methods
|
||||
|
||||
public void UpdateResultView(List<Result> list, PluginMetadata metadata, Query originQuery)
|
||||
{
|
||||
_queryHasReturn = true;
|
||||
IsProgressBarTooltipVisible = false;
|
||||
|
||||
list.ForEach(o =>
|
||||
{
|
||||
o.Score += UserSelectedRecordStorage.Instance.GetSelectedCount(o) * 5;
|
||||
});
|
||||
if (originQuery.RawQuery == _lastQuery.RawQuery)
|
||||
{
|
||||
System.Windows.Application.Current.Dispatcher.Invoke(() =>
|
||||
{
|
||||
UpdateResultViewInternal(list, metadata);
|
||||
});
|
||||
}
|
||||
|
||||
if (list.Count > 0)
|
||||
{
|
||||
IsResultListBoxVisible = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void ShowContextMenu(List<Result> actions, string pluginID)
|
||||
{
|
||||
DisplayContextMenu(actions, pluginID);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public event EventHandler<ListeningKeyPressedEventArgs> ListeningKeyPressed;
|
||||
|
||||
}
|
||||
|
||||
public class ListeningKeyPressedEventArgs : EventArgs
|
||||
{
|
||||
|
||||
public System.Windows.Input.KeyEventArgs KeyEventArgs
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
public ListeningKeyPressedEventArgs(System.Windows.Input.KeyEventArgs keyEventArgs)
|
||||
{
|
||||
KeyEventArgs = keyEventArgs;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
153
Wox/ViewModel/ResultViewModel.cs
Normal file
153
Wox/ViewModel/ResultViewModel.cs
Normal file
|
|
@ -0,0 +1,153 @@
|
|||
using Wox.Core.Plugin;
|
||||
using Wox.Core.Resource;
|
||||
using Wox.Infrastructure;
|
||||
using Wox.Infrastructure.Hotkey;
|
||||
using Wox.Plugin;
|
||||
using Wox.Storage;
|
||||
|
||||
namespace Wox.ViewModel
|
||||
{
|
||||
public class ResultViewModel : BaseViewModel
|
||||
{
|
||||
#region Private Fields
|
||||
|
||||
private bool _isSelected;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
|
||||
public ResultViewModel(Result result)
|
||||
{
|
||||
if (null != result)
|
||||
{
|
||||
RawResult = result;
|
||||
|
||||
OpenResultListBoxItemCommand = new RelayCommand(_ =>
|
||||
{
|
||||
|
||||
bool hideWindow = result.Action(new ActionContext
|
||||
{
|
||||
SpecialKeyState = GlobalHotkey.Instance.CheckModifiers()
|
||||
});
|
||||
|
||||
if (hideWindow)
|
||||
{
|
||||
App.API.HideApp();
|
||||
UserSelectedRecordStorage.Instance.Add(RawResult);
|
||||
QueryHistoryStorage.Instance.Add(RawResult.OriginQuery.RawQuery);
|
||||
}
|
||||
});
|
||||
|
||||
OpenContextMenuItemCommand = new RelayCommand(_ =>
|
||||
{
|
||||
|
||||
var actions = PluginManager.GetContextMenusForPlugin(result);
|
||||
|
||||
var pluginMetaData = PluginManager.GetPluginForId(result.PluginID).Metadata;
|
||||
actions.ForEach(o =>
|
||||
{
|
||||
o.PluginDirectory = pluginMetaData.PluginDirectory;
|
||||
o.PluginID = result.PluginID;
|
||||
o.OriginQuery = result.OriginQuery;
|
||||
});
|
||||
|
||||
actions.Add(GetTopMostContextMenu(result));
|
||||
|
||||
App.API.ShowContextMenu(pluginMetaData, actions);
|
||||
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region ViewModel Properties
|
||||
|
||||
public string Title => RawResult.Title;
|
||||
|
||||
public string SubTitle => RawResult.SubTitle;
|
||||
|
||||
public string FullIcoPath => RawResult.FullIcoPath;
|
||||
|
||||
public bool IsSelected
|
||||
{
|
||||
get { return _isSelected; }
|
||||
set
|
||||
{
|
||||
_isSelected = value;
|
||||
OnPropertyChanged("IsSelected");
|
||||
}
|
||||
}
|
||||
|
||||
public RelayCommand OpenResultListBoxItemCommand { get; set; }
|
||||
|
||||
public RelayCommand OpenContextMenuItemCommand { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties
|
||||
|
||||
public Result RawResult { get; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Methods
|
||||
|
||||
private Result GetTopMostContextMenu(Result result)
|
||||
{
|
||||
if (TopMostRecordStorage.Instance.IsTopMost(result))
|
||||
{
|
||||
return new Result(InternationalizationManager.Instance.GetTranslation("cancelTopMostInThisQuery"), "Images\\down.png")
|
||||
{
|
||||
PluginDirectory = WoxDirectroy.Executable,
|
||||
Action = _ =>
|
||||
{
|
||||
TopMostRecordStorage.Instance.Remove(result);
|
||||
App.API.ShowMsg("Succeed", "", "");
|
||||
return false;
|
||||
}
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
return new Result(InternationalizationManager.Instance.GetTranslation("setAsTopMostInThisQuery"), "Images\\up.png")
|
||||
{
|
||||
PluginDirectory = WoxDirectroy.Executable,
|
||||
Action = _ =>
|
||||
{
|
||||
TopMostRecordStorage.Instance.AddOrUpdate(result);
|
||||
App.API.ShowMsg("Succeed", "", "");
|
||||
return false;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
ResultViewModel r = obj as ResultViewModel;
|
||||
if (r != null)
|
||||
{
|
||||
return RawResult.Equals(r.RawResult);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return RawResult.GetHashCode();
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return RawResult.ToString();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
318
Wox/ViewModel/ResultsViewModel.cs
Normal file
318
Wox/ViewModel/ResultsViewModel.cs
Normal file
|
|
@ -0,0 +1,318 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Collections.Specialized;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using Wox.Core.UserSettings;
|
||||
using Wox.Plugin;
|
||||
using Wox.Storage;
|
||||
|
||||
namespace Wox.ViewModel
|
||||
{
|
||||
public class ResultsViewModel : BaseViewModel
|
||||
{
|
||||
#region Private Fields
|
||||
|
||||
private ResultViewModel _selectedResult;
|
||||
public ResultCollection Results { get; } = new ResultCollection();
|
||||
private Thickness _margin;
|
||||
|
||||
private readonly object _resultsUpdateLock = new object();
|
||||
|
||||
#endregion
|
||||
|
||||
#region ViewModel Properties
|
||||
|
||||
public int MaxHeight => UserSettingStorage.Instance.MaxResultsToShow * 50;
|
||||
|
||||
public ResultViewModel SelectedResult
|
||||
{
|
||||
get
|
||||
{
|
||||
return _selectedResult;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (null != value)
|
||||
{
|
||||
if (null != _selectedResult)
|
||||
{
|
||||
_selectedResult.IsSelected = false;
|
||||
}
|
||||
|
||||
_selectedResult = value;
|
||||
|
||||
if (null != _selectedResult)
|
||||
{
|
||||
_selectedResult.IsSelected = true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
OnPropertyChanged("SelectedResult");
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public Thickness Margin
|
||||
{
|
||||
get
|
||||
{
|
||||
return _margin;
|
||||
}
|
||||
set
|
||||
{
|
||||
_margin = value;
|
||||
OnPropertyChanged("Margin");
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Methods
|
||||
|
||||
private bool IsTopMostResult(Result result)
|
||||
{
|
||||
return TopMostRecordStorage.Instance.IsTopMost(result);
|
||||
}
|
||||
|
||||
private int InsertIndexOf(int newScore, IList<ResultViewModel> list)
|
||||
{
|
||||
int index = 0;
|
||||
for (; index < list.Count; index++)
|
||||
{
|
||||
var result = list[index];
|
||||
if (newScore > result.RawResult.Score)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Methods
|
||||
|
||||
public void SelectResult(int index)
|
||||
{
|
||||
if (index <= Results.Count - 1)
|
||||
{
|
||||
SelectedResult = Results[index];
|
||||
}
|
||||
}
|
||||
|
||||
public void SelectNextResult()
|
||||
{
|
||||
if (null != SelectedResult)
|
||||
{
|
||||
var index = Results.IndexOf(SelectedResult);
|
||||
if (index == Results.Count - 1)
|
||||
{
|
||||
index = -1;
|
||||
}
|
||||
SelectedResult = Results.ElementAt(index + 1);
|
||||
}
|
||||
}
|
||||
|
||||
public void SelectPrevResult()
|
||||
{
|
||||
if (null != SelectedResult)
|
||||
{
|
||||
var index = Results.IndexOf(SelectedResult);
|
||||
if (index == 0)
|
||||
{
|
||||
index = Results.Count;
|
||||
}
|
||||
SelectedResult = Results.ElementAt(index - 1);
|
||||
}
|
||||
}
|
||||
|
||||
public void SelectNextPage()
|
||||
{
|
||||
var index = 0;
|
||||
if (null != SelectedResult)
|
||||
{
|
||||
index = Results.IndexOf(SelectedResult);
|
||||
}
|
||||
index += 5;
|
||||
if (index > Results.Count - 1)
|
||||
{
|
||||
index = Results.Count - 1;
|
||||
}
|
||||
SelectedResult = Results.ElementAt(index);
|
||||
}
|
||||
|
||||
public void SelectPrevPage()
|
||||
{
|
||||
var index = 0;
|
||||
if (null != SelectedResult)
|
||||
{
|
||||
index = Results.IndexOf(SelectedResult);
|
||||
}
|
||||
index -= 5;
|
||||
if (index < 0)
|
||||
{
|
||||
index = 0;
|
||||
}
|
||||
SelectedResult = Results.ElementAt(index);
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
Results.Clear();
|
||||
}
|
||||
|
||||
public void RemoveResultsExcept(PluginMetadata metadata)
|
||||
{
|
||||
lock (_resultsUpdateLock)
|
||||
{
|
||||
Results.RemoveAll(r => r.RawResult.PluginID != metadata.ID);
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveResultsFor(PluginMetadata metadata)
|
||||
{
|
||||
lock (_resultsUpdateLock)
|
||||
{
|
||||
Results.RemoveAll(r => r.RawResult.PluginID == metadata.ID);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddResults(List<Result> newRawResults, string resultId)
|
||||
{
|
||||
lock (_resultsUpdateLock)
|
||||
{
|
||||
var newResults = new List<ResultViewModel>();
|
||||
newRawResults.ForEach((re) => { newResults.Add(new ResultViewModel(re)); });
|
||||
// todo use async to do new result calculation
|
||||
var resultsCopy = Results.ToList();
|
||||
var oldResults = resultsCopy.Where(r => r.RawResult.PluginID == resultId).ToList();
|
||||
// intersection of A (old results) and B (new newResults)
|
||||
var intersection = oldResults.Intersect(newResults).ToList();
|
||||
// remove result of relative complement of B in A
|
||||
foreach (var result in oldResults.Except(intersection))
|
||||
{
|
||||
resultsCopy.Remove(result);
|
||||
}
|
||||
|
||||
// update scores
|
||||
foreach (var result in newResults)
|
||||
{
|
||||
if (IsTopMostResult(result.RawResult))
|
||||
{
|
||||
result.RawResult.Score = int.MaxValue;
|
||||
}
|
||||
}
|
||||
|
||||
// update index for result in intersection of A and B
|
||||
foreach (var commonResult in intersection)
|
||||
{
|
||||
int oldIndex = resultsCopy.IndexOf(commonResult);
|
||||
int oldScore = resultsCopy[oldIndex].RawResult.Score;
|
||||
int newScore = newResults[newResults.IndexOf(commonResult)].RawResult.Score;
|
||||
if (newScore != oldScore)
|
||||
{
|
||||
var oldResult = resultsCopy[oldIndex];
|
||||
oldResult.RawResult.Score = newScore;
|
||||
resultsCopy.RemoveAt(oldIndex);
|
||||
int newIndex = InsertIndexOf(newScore, resultsCopy);
|
||||
resultsCopy.Insert(newIndex, oldResult);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// insert result in relative complement of A in B
|
||||
foreach (var result in newResults.Except(intersection))
|
||||
{
|
||||
int newIndex = InsertIndexOf(result.RawResult.Score, resultsCopy);
|
||||
resultsCopy.Insert(newIndex, result);
|
||||
}
|
||||
|
||||
// update UI in one run, so it can avoid UI flickering
|
||||
Results.Update(resultsCopy);
|
||||
|
||||
if (Results.Count > 0)
|
||||
{
|
||||
Margin = new Thickness { Top = 8 };
|
||||
SelectedResult = Results[0];
|
||||
}
|
||||
else
|
||||
{
|
||||
Margin = new Thickness { Top = 0 };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
public class ResultCollection : ObservableCollection<ResultViewModel>
|
||||
{
|
||||
|
||||
public void RemoveAll(Predicate<ResultViewModel> predicate)
|
||||
{
|
||||
CheckReentrancy();
|
||||
|
||||
List<ResultViewModel> itemsToRemove = Items.Where(x => predicate(x)).ToList();
|
||||
if (itemsToRemove.Count > 0)
|
||||
{
|
||||
itemsToRemove.ForEach(item => { Items.Remove(item); });
|
||||
|
||||
OnPropertyChanged(new PropertyChangedEventArgs("Count"));
|
||||
OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
|
||||
// fuck ms
|
||||
// http://blogs.msdn.com/b/nathannesbit/archive/2009/04/20/addrange-and-observablecollection.aspx
|
||||
// http://geekswithblogs.net/NewThingsILearned/archive/2008/01/16/listcollectionviewcollectionview-doesnt-support-notifycollectionchanged-with-multiple-items.aspx
|
||||
// PS: don't use Reset for other data updates, it will cause UI flickering
|
||||
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void Update(List<ResultViewModel> newItems)
|
||||
{
|
||||
int newCount = newItems.Count;
|
||||
int oldCount = Items.Count;
|
||||
int location = newCount > oldCount ? oldCount : newCount;
|
||||
for (int i = 0; i < location; i++)
|
||||
{
|
||||
ResultViewModel oldResult = Items[i];
|
||||
ResultViewModel newResult = newItems[i];
|
||||
if (!oldResult.Equals(newResult))
|
||||
{
|
||||
this[i] = newResult;
|
||||
}
|
||||
else if (oldResult.RawResult.Score != newResult.RawResult.Score)
|
||||
{
|
||||
this[i].RawResult.Score = newResult.RawResult.Score;
|
||||
}
|
||||
}
|
||||
|
||||
if (newCount > oldCount)
|
||||
{
|
||||
for (int i = oldCount; i < newCount; i++)
|
||||
{
|
||||
Add(newItems[i]);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
int removeIndex = newCount;
|
||||
for (int i = newCount; i < oldCount; i++)
|
||||
{
|
||||
RemoveAt(removeIndex);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -123,12 +123,21 @@
|
|||
<Compile Include="Converters\OpacityModeConverter.cs" />
|
||||
<Compile Include="Converters\StringEmptyConverter.cs" />
|
||||
<Compile Include="Converters\StringNullOrEmptyToVisibilityConverter.cs" />
|
||||
<Compile Include="Helper\ListBoxItems.cs" />
|
||||
<Compile Include="Converters\VisibilityConverter.cs" />
|
||||
<Compile Include="Helper\SingletonWindowOpener.cs" />
|
||||
<Compile Include="ImageLoader\ImageCacheStroage.cs" />
|
||||
<Compile Include="NotifyIconManager.cs" />
|
||||
<Compile Include="PublicAPIInstance.cs" />
|
||||
<Compile Include="ResultListBox.xaml.cs">
|
||||
<DependentUpon>ResultListBox.xaml</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Storage\QueryHistoryStorage.cs" />
|
||||
<Compile Include="Storage\TopMostRecordStorage.cs" />
|
||||
<Compile Include="Storage\UserSelectedRecordStorage.cs" />
|
||||
<Compile Include="ViewModel\BaseViewModel.cs" />
|
||||
<Compile Include="ViewModel\MainViewModel.cs" />
|
||||
<Compile Include="ViewModel\ResultViewModel.cs" />
|
||||
<Compile Include="ViewModel\ResultsViewModel.cs" />
|
||||
<Compile Include="WoxUpdate.xaml.cs">
|
||||
<DependentUpon>WoxUpdate.xaml</DependentUpon>
|
||||
</Compile>
|
||||
|
|
@ -163,9 +172,6 @@
|
|||
<DependentUpon>Msg.xaml</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Properties\Annotations.cs" />
|
||||
<Compile Include="ResultPanel.xaml.cs">
|
||||
<DependentUpon>ResultPanel.xaml</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="SettingWindow.xaml.cs">
|
||||
<DependentUpon>SettingWindow.xaml</DependentUpon>
|
||||
</Compile>
|
||||
|
|
@ -237,9 +243,9 @@
|
|||
<SubType>Designer</SubType>
|
||||
<Generator>MSBuild:Compile</Generator>
|
||||
</Page>
|
||||
<Page Include="ResultPanel.xaml">
|
||||
<SubType>Designer</SubType>
|
||||
<Page Include="ResultListBox.xaml">
|
||||
<Generator>MSBuild:Compile</Generator>
|
||||
<SubType>Designer</SubType>
|
||||
</Page>
|
||||
<Page Include="SettingWindow.xaml">
|
||||
<SubType>Designer</SubType>
|
||||
|
|
|
|||
Loading…
Reference in a new issue