Add progress bar

This commit is contained in:
qianlifeng 2014-01-07 19:27:51 +08:00
parent 13f00edefd
commit a5f3359d32
5 changed files with 106 additions and 18 deletions

View file

@ -25,6 +25,8 @@ namespace WinAlfred.Plugin.System
public List<Result> Query(Query query)
{
if (string.IsNullOrEmpty(query.RawQuery) || query.RawQuery.EndsWith(" ") || query.RawQuery.Length <= 1) return new List<Result>();
List<Result> results = new List<Result>();
foreach (Result availableResult in availableResults)

View file

@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Threading;
namespace WinAlfred
{
public static class DispatcherExtensions
{
private static Dictionary<string, DispatcherTimer> timers =
new Dictionary<string, DispatcherTimer>();
private static readonly object syncRoot = new object();
public static string DelayInvoke(this Dispatcher dispatcher, string namedInvocation,
Action action, TimeSpan delay,
DispatcherPriority priority = DispatcherPriority.Normal)
{
lock (syncRoot)
{
if (String.IsNullOrEmpty(namedInvocation))
{
namedInvocation = Guid.NewGuid().ToString();
}
else
{
RemoveTimer(namedInvocation);
}
var timer = new DispatcherTimer(delay, priority, (s, e) =>
{
RemoveTimer(namedInvocation);
action();
}, dispatcher);
timer.Start();
timers.Add(namedInvocation, timer);
return namedInvocation;
}
}
public static void CancelNamedInvocation(this Dispatcher dispatcher, string namedInvocation)
{
lock (syncRoot)
{
RemoveTimer(namedInvocation);
}
}
private static void RemoveTimer(string namedInvocation)
{
if (!timers.ContainsKey(namedInvocation)) return;
timers[namedInvocation].Stop();
timers.Remove(namedInvocation);
}
}
}

View file

@ -12,9 +12,15 @@
ShowInTaskbar="False"
Icon="Images\ico.png"
>
<DockPanel>
<TextBox Style="{DynamicResource defaultQueryBoxStyle}" DockPanel.Dock="Top" x:Name="tbQuery" PreviewKeyDown="TbQuery_OnPreviewKeyDown" TextChanged="TextBoxBase_OnTextChanged" />
<winAlfred:ResultPanel x:Name="resultCtrl" Margin="10 0 10 0" />
</DockPanel>
<Grid>
<Grid.RowDefinitions>
<RowDefinition ></RowDefinition>
<RowDefinition Height="2"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<TextBox Style="{DynamicResource defaultQueryBoxStyle}" Grid.Row="0" x:Name="tbQuery" PreviewKeyDown="TbQuery_OnPreviewKeyDown" TextChanged="TextBoxBase_OnTextChanged" />
<Line Stroke="Blue" x:Name="progressBar" X2="100" Grid.Row="1" Height="2" StrokeThickness="1"></Line>
<winAlfred:ResultPanel x:Name="resultCtrl" Grid.Row="2" Margin="10 0 10 0" />
</Grid>
</Window>

View file

@ -6,6 +6,8 @@ using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using IWshRuntimeLibrary;
using Microsoft.Win32;
using WinAlfred.Commands;
@ -23,6 +25,7 @@ namespace WinAlfred
private KeyboardHook hook = new KeyboardHook();
private NotifyIcon notifyIcon;
private Command cmdDispatcher;
Storyboard progressBarStoryboard = new Storyboard();
public MainWindow()
{
@ -32,6 +35,20 @@ namespace WinAlfred
hook.RegisterHotKey(XModifierKeys.Alt, Keys.Space);
resultCtrl.resultItemChangedEvent += resultCtrl_resultItemChangedEvent;
ThreadPool.SetMaxThreads(30, 10);
InitProgressbarAnimation();
}
private void InitProgressbarAnimation()
{
DoubleAnimation da = new DoubleAnimation(progressBar.X2, Width + 100, new Duration(new TimeSpan(0, 0, 0,0,1600)));
DoubleAnimation da1 = new DoubleAnimation(progressBar.X1, Width, new Duration(new TimeSpan(0, 0, 0, 0,1600)));
Storyboard.SetTargetProperty(da, new PropertyPath("(Line.X1)"));
Storyboard.SetTargetProperty(da1, new PropertyPath("(Line.X2)"));
progressBarStoryboard.Children.Add(da);
progressBarStoryboard.Children.Add(da1);
progressBarStoryboard.RepeatBehavior = RepeatBehavior.Forever;
progressBar.Visibility = Visibility.Hidden;
progressBar.BeginStoryboard(progressBarStoryboard);
}
private void InitialTray()
@ -66,22 +83,26 @@ namespace WinAlfred
private void TextBoxBase_OnTextChanged(object sender, TextChangedEventArgs e)
{
//MessageBox.Show("s");
resultCtrl.Dirty = true;
////auto clear results after 50ms if there are any results returned by plugins
////why we do this? because if we clear resulsts in the start of the text changed event
////we will see the splash. The more closer that clear and addResult method, the less splash we will see.
new Timer(o =>
{
if (resultCtrl.Dirty)
{
resultCtrl.Dispatcher.Invoke(new Action(() => resultCtrl.Clear()));
}
}, null, TimeSpan.FromMilliseconds(50), TimeSpan.FromMilliseconds(-1));
if (string.IsNullOrEmpty(tbQuery.Text)) return;
Dispatcher.DelayInvoke("UpdateSearch",
() =>
{
resultCtrl.Clear();
var q = new Query(tbQuery.Text);
cmdDispatcher.DispatchCommand(q);
}, TimeSpan.FromMilliseconds(300));
var q = new Query(tbQuery.Text);
cmdDispatcher.DispatchCommand(q);
}
private void StartProgress()
{
progressBar.Visibility = Visibility.Visible;
}
private void StopProgress()
{
progressBar.Visibility = Visibility.Hidden;
}
private void HideWinAlfred()
@ -130,6 +151,7 @@ namespace WinAlfred
SetAutoStart(true);
//var engine = new Jurassic.ScriptEngine();
//MessageBox.Show(engine.Evaluate("5 * 10 + 2").ToString());
StartProgress();
}
private void TbQuery_OnPreviewKeyDown(object sender, KeyEventArgs e)

View file

@ -93,6 +93,7 @@
<Compile Include="Commands\Command.cs" />
<Compile Include="Commands\PluginCommand.cs" />
<Compile Include="Commands\SystemCommand.cs" />
<Compile Include="DispatcherExtensions.cs" />
<Compile Include="Helper\KeyboardHook.cs" />
<Compile Include="Helper\Log.cs" />
<Compile Include="Helper\WinAlfredException.cs" />