add translator plugin

This commit is contained in:
qianlifeng
2014-01-05 17:56:02 +08:00
parent 07d002da48
commit b2081dcbc9
22 changed files with 820 additions and 47 deletions

View File

@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using WinAlfred.Helper;
using WinAlfred.Plugin;

View File

@@ -85,6 +85,7 @@ namespace WinAlfred.Helper
if (!RegisterHotKey(window.Handle, currentId, (uint)xModifier, (uint)key))
{
Log.Error("Couldnt register the hot key.");
MessageBox.Show("Couldnt register the hot key.");
#if (DEBUG)
{
throw new InvalidOperationException("Couldnt register the hot key.");

View File

@@ -6,6 +6,7 @@ using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Input;
using Noesis.Javascript;
using WinAlfred.Commands;
using WinAlfred.Helper;
using WinAlfred.Plugin;
@@ -39,11 +40,7 @@ namespace WinAlfred
System.Windows.Forms.MenuItem open = new System.Windows.Forms.MenuItem("Open");
open.Click += (o, e) => ShowWinAlfred();
System.Windows.Forms.MenuItem exit = new System.Windows.Forms.MenuItem("Exit");
exit.Click += (o, e) =>
{
notifyIcon.Visible = false;
Close();
};
exit.Click += (o, e) => CloseApp();
System.Windows.Forms.MenuItem[] childen = { open, exit };
notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(childen);
}
@@ -68,6 +65,30 @@ namespace WinAlfred
private void TextBoxBase_OnTextChanged(object sender, TextChangedEventArgs e)
{
// Initialize a context
using (JavascriptContext context = new JavascriptContext())
{
// Setting external parameters for the context
context.SetParameter("message", "Hello World !");
context.SetParameter("number", 1);
// Script
string script = @"
var i;
for (i = 0; i < 5; i++)
console.Print(message + ' (' + i + ')');
number += i;
";
// Running the script
context.Run(script);
// Getting a parameter
MessageBox.Show("number: " + context.GetParameter("number"));
}
//MessageBox.Show("s");
resultCtrl.Dirty = true;
////auto clear results after 50ms if there are any results returned by plugins
@@ -91,7 +112,7 @@ namespace WinAlfred
Hide();
}
public void ShowWinAlfred()
private void ShowWinAlfred()
{
Show();
//FocusManager.SetFocusedElement(this, tbQuery);
@@ -104,6 +125,9 @@ namespace WinAlfred
Plugins.Init(this);
cmdDispatcher = new Command(this);
InitialTray();
}
private void TbQuery_OnPreviewKeyDown(object sender, KeyEventArgs e)
@@ -126,8 +150,7 @@ namespace WinAlfred
break;
case Key.Enter:
resultCtrl.AcceptSelect();
HideWinAlfred();
if (resultCtrl.AcceptSelect()) HideWinAlfred();
e.Handled = true;
break;
}
@@ -141,5 +164,39 @@ namespace WinAlfred
resultCtrl.AddResults(l);
}));
}
#region Public API
//Those method can be invoked by plugins
public void ChangeQuery(string query)
{
tbQuery.Text = query;
tbQuery.CaretIndex = tbQuery.Text.Length;
}
public void CloseApp()
{
notifyIcon.Visible = false;
Close();
}
public void HideApp()
{
HideWinAlfred();
}
public void ShowApp()
{
ShowWinAlfred();
}
public void ShowMsg(string title, string subTitle, string iconPath)
{
Msg m = new Msg { Owner = GetWindow(this) };
m.Show(title, subTitle, iconPath);
}
#endregion
}
}

38
WinAlfred/Msg.xaml Normal file
View File

@@ -0,0 +1,38 @@
<Window x:Class="WinAlfred.Msg"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Background="#ebebeb"
Topmost="True"
SizeToContent="Height"
ResizeMode="NoResize"
WindowStyle="None"
ShowInTaskbar="False"
Title="Msg" Height="60" Width="382.978">
<Window.Triggers>
<EventTrigger RoutedEvent="Window.Loaded">
<BeginStoryboard>
<Storyboard >
<DoubleAnimation x:Name="showAnimation" Duration="0:0:0.3" Storyboard.TargetProperty="Top" AccelerationRatio="0.2"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Window.Triggers>
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="32"></ColumnDefinition>
<ColumnDefinition/>
<ColumnDefinition Width="32"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Image x:Name="imgIco" Width="32" Height="32" HorizontalAlignment="Left" ></Image>
<Grid HorizontalAlignment="Stretch" Margin="5 0 0 0" Grid.Column="1" VerticalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="29"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<TextBlock x:Name="tbTitle" FontSize="16" Foreground="#37392c" FontWeight="Medium">Title</TextBlock>
<TextBlock Grid.Row="1" Foreground="#8e94a4" x:Name="tbSubTitle">sdfdsf</TextBlock>
</Grid>
<Image x:Name="imgClose" Grid.Column="2" Cursor="Hand" Width="16" VerticalAlignment="Top" HorizontalAlignment="Right" />
</Grid>
</Window>

82
WinAlfred/Msg.xaml.cs Normal file
View File

@@ -0,0 +1,82 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Timer = System.Threading.Timer;
namespace WinAlfred
{
public partial class Msg : Window
{
Storyboard fadeOutStoryboard = new Storyboard();
private bool closing = false;
public Msg()
{
InitializeComponent();
Left = Screen.PrimaryScreen.WorkingArea.Right - this.Width;
Top = Screen.PrimaryScreen.Bounds.Bottom;
showAnimation.From = Screen.PrimaryScreen.Bounds.Bottom;
showAnimation.To = Screen.PrimaryScreen.WorkingArea.Bottom - Height;
// Create the fade out storyboard
fadeOutStoryboard.Completed += new EventHandler(fadeOutStoryboard_Completed);
DoubleAnimation fadeOutAnimation = new DoubleAnimation(Screen.PrimaryScreen.WorkingArea.Bottom - Height, Screen.PrimaryScreen.Bounds.Bottom, new Duration(TimeSpan.FromSeconds(0.3)))
{
AccelerationRatio = 0.2
};
Storyboard.SetTarget(fadeOutAnimation, this);
Storyboard.SetTargetProperty(fadeOutAnimation, new PropertyPath(TopProperty));
fadeOutStoryboard.Children.Add(fadeOutAnimation);
imgClose.Source = new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory + "\\Images\\close.png"));
imgClose.MouseUp += imgClose_MouseUp;
}
void imgClose_MouseUp(object sender, MouseButtonEventArgs e)
{
if (!closing)
{
closing = true;
fadeOutStoryboard.Begin();
}
}
private void fadeOutStoryboard_Completed(object sender, EventArgs e)
{
Close();
}
public void Show(string title, string subTitle, string icopath)
{
tbTitle.Text = title;
tbSubTitle.Text = subTitle;
if (!File.Exists(icopath))
{
icopath = AppDomain.CurrentDomain.BaseDirectory + "Images\\ico.png";
}
imgIco.Source = new BitmapImage(new Uri(icopath));
Show();
new Timer(o =>
{
if (!closing)
{
closing = true;
Dispatcher.Invoke(new Action(fadeOutStoryboard.Begin));
}
}, null, TimeSpan.FromSeconds(3), TimeSpan.FromMilliseconds(-1));
}
}
}

View File

@@ -22,11 +22,11 @@ namespace WinAlfred.PluginLoader
ThreadPool.QueueUserWorkItem(o => plugin1.Init(new PluginInitContext()
{
Plugins = plugins,
ChangeQuery = s =>
{
window.tbQuery.Text = s;
window.ShowWinAlfred();
}
ChangeQuery = s => window.ChangeQuery(s),
CloseApp = window.CloseApp,
HideApp = window.HideApp,
ShowApp = window.ShowApp,
ShowMsg = (title,subTitle,iconPath) => window.ShowMsg(title,subTitle,iconPath)
}));
}
}

View File

@@ -192,14 +192,21 @@ namespace WinAlfred
Select(0);
}
public void AcceptSelect()
public bool AcceptSelect()
{
int index = GetCurrentSelectedResultIndex();
var resultItemControl = pnlContainer.Children[index] as ResultItem;
if (resultItemControl != null)
{
if (resultItemControl.Result.Action != null) resultItemControl.Result.Action();
if (resultItemControl.Result.Action != null)
{
resultItemControl.Result.Action();
}
return !resultItemControl.Result.DontHideWinAlfredAfterAction;
}
return true;
}
public ResultPanel()

View File

@@ -39,22 +39,50 @@
<PropertyGroup>
<ApplicationIcon>app.ico</ApplicationIcon>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x64\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>bin\x64\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<Reference Include="Accessibility" />
<Reference Include="log4net">
<HintPath>..\packages\log4net.2.0.3\lib\net35-full\log4net.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json">
<HintPath>..\packages\Newtonsoft.Json.5.0.8\lib\net35\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="Noesis.Javascript">
<HintPath>C:\Users\Scott\Desktop\x64\Noesis.Javascript.dll</HintPath>
</Reference>
<Reference Include="PresentationUI, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
<Reference Include="ReachFramework" />
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Deployment" />
<Reference Include="System.Drawing" />
<Reference Include="System.Printing" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xaml" />
<Reference Include="System.Xml" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="UIAutomationProvider" />
<Reference Include="UIAutomationTypes" />
<Reference Include="WindowsBase" />
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
@@ -71,6 +99,9 @@
<Compile Include="Helper\KeyboardHook.cs" />
<Compile Include="Helper\Log.cs" />
<Compile Include="Helper\WinAlfredException.cs" />
<Compile Include="Msg.xaml.cs">
<DependentUpon>Msg.xaml</DependentUpon>
</Compile>
<Compile Include="PluginLoader\BasePluginLoader.cs" />
<Compile Include="PluginLoader\CSharpPluginLoader.cs" />
<Compile Include="Helper\IniParser.cs" />
@@ -95,6 +126,10 @@
<DependentUpon>MainWindow.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Page Include="Msg.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="ResultPanel.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>