Implement WebSearch feature.
This commit is contained in:
parent
76fa97e0a0
commit
495e0ce89b
|
@ -47,9 +47,21 @@ namespace Wox.Infrastructure
|
|||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
//on-op, keep default storage
|
||||
LoadDefaultUserSetting();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LoadDefaultUserSetting();
|
||||
}
|
||||
}
|
||||
|
||||
private static void LoadDefaultUserSetting()
|
||||
{
|
||||
//default setting
|
||||
Instance.UserSetting.Theme = "Default";
|
||||
Instance.UserSetting.ReplaceWinR = true;
|
||||
Instance.UserSetting.WebSearches = Instance.UserSetting.LoadDefaultWebSearches();
|
||||
}
|
||||
|
||||
public static CommonStorage Instance
|
||||
|
@ -70,6 +82,5 @@ namespace Wox.Infrastructure
|
|||
return storage;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -9,15 +9,7 @@ namespace Wox.Infrastructure.UserSettings
|
|||
public bool ReplaceWinR { get; set; }
|
||||
public List<WebSearch> WebSearches { get; set; }
|
||||
|
||||
public UserSetting()
|
||||
{
|
||||
//default setting
|
||||
Theme = "Default";
|
||||
ReplaceWinR = true;
|
||||
WebSearches = LoadDefaultWebSearches();
|
||||
}
|
||||
|
||||
private List<WebSearch> LoadDefaultWebSearches()
|
||||
public List<WebSearch> LoadDefaultWebSearches()
|
||||
{
|
||||
List<WebSearch> webSearches = new List<WebSearch>();
|
||||
|
||||
|
|
|
@ -34,11 +34,10 @@ namespace Wox.Plugin.System
|
|||
}
|
||||
}
|
||||
|
||||
results.AddRange(CommonStorage.Instance.UserSetting.WebSearches.Where(o => o.ActionWord.StartsWith(query.RawQuery)).Select(n => new Result()
|
||||
results.AddRange(CommonStorage.Instance.UserSetting.WebSearches.Where(o => o.ActionWord.StartsWith(query.RawQuery) && o.Enabled).Select(n => new Result()
|
||||
{
|
||||
|
||||
Title = n.ActionWord,
|
||||
SubTitle = string.Format("Activate {0} plugin", n.ActionWord),
|
||||
SubTitle = string.Format("Activate {0} web search", n.ActionWord),
|
||||
Score = 50,
|
||||
IcoPath = "Images/work.png",
|
||||
Action = () => changeQuery(n.ActionWord + " "),
|
||||
|
|
|
@ -17,14 +17,14 @@ namespace Wox.Plugin.System
|
|||
if (string.IsNullOrEmpty(query.ActionName)) return results;
|
||||
|
||||
WebSearch webSearch =
|
||||
CommonStorage.Instance.UserSetting.WebSearches.FirstOrDefault(o => o.ActionWord == query.ActionName);
|
||||
CommonStorage.Instance.UserSetting.WebSearches.FirstOrDefault(o => o.ActionWord == query.ActionName && o.Enabled);
|
||||
|
||||
if (webSearch != null)
|
||||
{
|
||||
string keyword = query.ActionParameters.Count > 0 ? query.RawQuery.Substring(query.RawQuery.IndexOf(' ') + 1) : "";
|
||||
results.Add(new Result()
|
||||
{
|
||||
Title = string.Format("Search {0} for {1}", webSearch.Title, keyword),
|
||||
Title = string.Format("Search {0} for \"{1}\"", webSearch.Title, keyword),
|
||||
IcoPath = webSearch.IconPath,
|
||||
Action = () => Process.Start(webSearch.Url.Replace("{q}", keyword))
|
||||
});
|
||||
|
|
|
@ -44,18 +44,15 @@
|
|||
</DataTemplate>
|
||||
</GridViewColumn.CellTemplate>
|
||||
</GridViewColumn>
|
||||
<GridViewColumn Header="Enable" Width="50" >
|
||||
<GridViewColumn.CellTemplate>
|
||||
<DataTemplate>
|
||||
<TextBlock Text="{Binding Path=Enabled}"></TextBlock>
|
||||
</DataTemplate>
|
||||
</GridViewColumn.CellTemplate>
|
||||
</GridViewColumn>
|
||||
</GridView.Columns>
|
||||
</GridView>
|
||||
</ListView.View>
|
||||
</ListView>
|
||||
<Button Grid.Row="1" HorizontalAlignment="Right" Click="ButtonBase_OnClick" Width="100" Margin="10">Add</Button>
|
||||
<StackPanel Grid.Row="1" HorizontalAlignment="Right" Orientation="Horizontal">
|
||||
<Button x:Name="btnDeleteWebSearch" Click="btnDeleteWebSearch_OnClick" Width="100" Margin="10">Delete</Button>
|
||||
<Button x:Name="btnEditWebSearch" Click="btnEditWebSearch_OnClick" Width="100" Margin="10">Edit</Button>
|
||||
<Button x:Name="btnAddWebSearch" Click="btnAddWebSearch_OnClick" Width="100" Margin="10">Add</Button>
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
</TabItem>
|
||||
</TabControl>
|
||||
|
|
|
@ -5,6 +5,7 @@ using System.Windows;
|
|||
using System.Windows.Controls;
|
||||
using Wox.Helper;
|
||||
using Wox.Infrastructure;
|
||||
using Wox.Infrastructure.UserSettings;
|
||||
|
||||
namespace Wox
|
||||
{
|
||||
|
@ -42,6 +43,11 @@ namespace Wox
|
|||
webSearchView.ItemsSource = CommonStorage.Instance.UserSetting.WebSearches;
|
||||
}
|
||||
|
||||
public void ReloadWebSearchView()
|
||||
{
|
||||
webSearchView.Items.Refresh();
|
||||
}
|
||||
|
||||
private List<string> LoadAvailableThemes()
|
||||
{
|
||||
string themePath = Directory.GetCurrentDirectory() + "\\Themes\\";
|
||||
|
@ -56,10 +62,41 @@ namespace Wox
|
|||
CommonStorage.Instance.Save();
|
||||
}
|
||||
|
||||
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
|
||||
private void btnAddWebSearch_OnClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
WebSearchSetting webSearch = new WebSearchSetting();
|
||||
WebSearchSetting webSearch = new WebSearchSetting(this);
|
||||
webSearch.Show();
|
||||
}
|
||||
|
||||
private void btnDeleteWebSearch_OnClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
WebSearch seletedWebSearch = webSearchView.SelectedItem as WebSearch;
|
||||
if (seletedWebSearch != null &&
|
||||
MessageBox.Show("Are your sure to delete " + seletedWebSearch.Title, "Delete WebSearch",
|
||||
MessageBoxButton.YesNo) == MessageBoxResult.Yes)
|
||||
{
|
||||
CommonStorage.Instance.UserSetting.WebSearches.Remove(seletedWebSearch);
|
||||
webSearchView.Items.Refresh();
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("Please select a web search");
|
||||
}
|
||||
}
|
||||
|
||||
private void btnEditWebSearch_OnClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
WebSearch seletedWebSearch = webSearchView.SelectedItem as WebSearch;
|
||||
if (seletedWebSearch != null)
|
||||
{
|
||||
WebSearchSetting webSearch = new WebSearchSetting(this);
|
||||
webSearch.Show();
|
||||
webSearch.UpdateItem(seletedWebSearch);
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("Please select a web search");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
<RowDefinition></RowDefinition>
|
||||
<RowDefinition></RowDefinition>
|
||||
<RowDefinition></RowDefinition>
|
||||
<RowDefinition></RowDefinition>
|
||||
</Grid.RowDefinitions>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="120"></ColumnDefinition>
|
||||
|
@ -25,9 +26,12 @@
|
|||
<TextBlock Margin="10" FontSize="14" Grid.Row="2" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Right">ActionWord:</TextBlock>
|
||||
<TextBox x:Name="tbActionword" Margin="10" Grid.Row="2" Width="400" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left"></TextBox>
|
||||
|
||||
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Grid.Row="8" Grid.Column="1">
|
||||
<TextBlock Margin="10" FontSize="14" Grid.Row="3" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Right">Enable:</TextBlock>
|
||||
<CheckBox x:Name="cbEnable" IsChecked="True" Margin="10" Grid.Row="3" Grid.Column="1" VerticalAlignment="Center"></CheckBox>
|
||||
|
||||
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Grid.Row="4" Grid.Column="1">
|
||||
<Button x:Name="btnCancel" Click="BtnCancel_OnClick" Margin="10 0 10 0" Width="80" Height="25">Cancel</Button>
|
||||
<Button x:Name="btnAdd" Margin="10 0 10 0" Width="80" Height="25" Click="btnAdd_OnClick">Add</Button>
|
||||
<Button x:Name="btnAdd" Margin="10 0 10 0" Width="80" Height="25" Click="btnAdd_OnClick"><TextBlock x:Name="lblAdd">Add</TextBlock></Button>
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
</Window>
|
||||
|
|
|
@ -6,6 +6,7 @@ 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.Imaging;
|
||||
|
@ -13,16 +14,40 @@ using System.Windows.Shapes;
|
|||
using Wox.Helper;
|
||||
using Wox.Infrastructure;
|
||||
using Wox.Infrastructure.UserSettings;
|
||||
using MessageBox = System.Windows.MessageBox;
|
||||
|
||||
namespace Wox
|
||||
{
|
||||
public partial class WebSearchSetting : Window
|
||||
{
|
||||
public WebSearchSetting()
|
||||
private SettingWidow settingWidow;
|
||||
private bool update;
|
||||
private WebSearch updateWebSearch;
|
||||
|
||||
public WebSearchSetting(SettingWidow settingWidow)
|
||||
{
|
||||
this.settingWidow = settingWidow;
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
public void UpdateItem(WebSearch webSearch)
|
||||
{
|
||||
updateWebSearch = CommonStorage.Instance.UserSetting.WebSearches.FirstOrDefault(o => o == webSearch);
|
||||
if (updateWebSearch == null || string.IsNullOrEmpty(updateWebSearch.Url))
|
||||
{
|
||||
MessageBox.Show("Invalid web search");
|
||||
Close();
|
||||
return;
|
||||
}
|
||||
|
||||
update = true;
|
||||
lblAdd.Text = "Update";
|
||||
cbEnable.IsChecked = webSearch.Enabled;
|
||||
tbTitle.Text = webSearch.Title;
|
||||
tbUrl.Text = webSearch.Url;
|
||||
tbActionword.Text = webSearch.ActionWord;
|
||||
}
|
||||
|
||||
private void BtnCancel_OnClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
Close();
|
||||
|
@ -50,22 +75,37 @@ namespace Wox
|
|||
MessageBox.Show("Please input ActionWord field");
|
||||
return;
|
||||
}
|
||||
if (CommonStorage.Instance.UserSetting.WebSearches.Exists(o => o.ActionWord == action))
|
||||
{
|
||||
MessageBox.Show("ActionWord has existed, please input a new one.");
|
||||
return;
|
||||
}
|
||||
|
||||
CommonStorage.Instance.UserSetting.WebSearches.Add(new WebSearch()
|
||||
|
||||
if (!update)
|
||||
{
|
||||
ActionWord = action,
|
||||
Enabled = true,
|
||||
IconPath="",
|
||||
Url = url,
|
||||
Title = title
|
||||
});
|
||||
if (CommonStorage.Instance.UserSetting.WebSearches.Exists(o => o.ActionWord == action))
|
||||
{
|
||||
MessageBox.Show("ActionWord has existed, please input a new one.");
|
||||
return;
|
||||
}
|
||||
CommonStorage.Instance.UserSetting.WebSearches.Add(new WebSearch()
|
||||
{
|
||||
ActionWord = action,
|
||||
Enabled = cbEnable.IsChecked ?? false,
|
||||
IconPath = "",
|
||||
Url = url,
|
||||
Title = title
|
||||
});
|
||||
MessageBox.Show(string.Format("Add {0} web search successfully!", title));
|
||||
}
|
||||
else
|
||||
{
|
||||
updateWebSearch.ActionWord = action;
|
||||
updateWebSearch.IconPath = "";
|
||||
updateWebSearch.Enabled = cbEnable.IsChecked ?? false;
|
||||
updateWebSearch.Url = url;
|
||||
updateWebSearch.Title= title;
|
||||
MessageBox.Show(string.Format("Update {0} web search successfully!", title));
|
||||
}
|
||||
CommonStorage.Instance.Save();
|
||||
MessageBox.Show("Succeed!");
|
||||
settingWidow.ReloadWebSearchView();
|
||||
Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue