mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-16 11:48:06 +01:00
Implement WebSearch feature.
This commit is contained in:
@@ -47,9 +47,21 @@ namespace Wox.Infrastructure
|
|||||||
}
|
}
|
||||||
catch (Exception e)
|
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
|
public static CommonStorage Instance
|
||||||
@@ -70,6 +82,5 @@ namespace Wox.Infrastructure
|
|||||||
return storage;
|
return storage;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -9,15 +9,7 @@ namespace Wox.Infrastructure.UserSettings
|
|||||||
public bool ReplaceWinR { get; set; }
|
public bool ReplaceWinR { get; set; }
|
||||||
public List<WebSearch> WebSearches { get; set; }
|
public List<WebSearch> WebSearches { get; set; }
|
||||||
|
|
||||||
public UserSetting()
|
public List<WebSearch> LoadDefaultWebSearches()
|
||||||
{
|
|
||||||
//default setting
|
|
||||||
Theme = "Default";
|
|
||||||
ReplaceWinR = true;
|
|
||||||
WebSearches = LoadDefaultWebSearches();
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<WebSearch> LoadDefaultWebSearches()
|
|
||||||
{
|
{
|
||||||
List<WebSearch> webSearches = new List<WebSearch>();
|
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,
|
Title = n.ActionWord,
|
||||||
SubTitle = string.Format("Activate {0} plugin", n.ActionWord),
|
SubTitle = string.Format("Activate {0} web search", n.ActionWord),
|
||||||
Score = 50,
|
Score = 50,
|
||||||
IcoPath = "Images/work.png",
|
IcoPath = "Images/work.png",
|
||||||
Action = () => changeQuery(n.ActionWord + " "),
|
Action = () => changeQuery(n.ActionWord + " "),
|
||||||
|
|||||||
@@ -17,14 +17,14 @@ namespace Wox.Plugin.System
|
|||||||
if (string.IsNullOrEmpty(query.ActionName)) return results;
|
if (string.IsNullOrEmpty(query.ActionName)) return results;
|
||||||
|
|
||||||
WebSearch webSearch =
|
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)
|
if (webSearch != null)
|
||||||
{
|
{
|
||||||
string keyword = query.ActionParameters.Count > 0 ? query.RawQuery.Substring(query.RawQuery.IndexOf(' ') + 1) : "";
|
string keyword = query.ActionParameters.Count > 0 ? query.RawQuery.Substring(query.RawQuery.IndexOf(' ') + 1) : "";
|
||||||
results.Add(new Result()
|
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,
|
IcoPath = webSearch.IconPath,
|
||||||
Action = () => Process.Start(webSearch.Url.Replace("{q}", keyword))
|
Action = () => Process.Start(webSearch.Url.Replace("{q}", keyword))
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -44,18 +44,15 @@
|
|||||||
</DataTemplate>
|
</DataTemplate>
|
||||||
</GridViewColumn.CellTemplate>
|
</GridViewColumn.CellTemplate>
|
||||||
</GridViewColumn>
|
</GridViewColumn>
|
||||||
<GridViewColumn Header="Enable" Width="50" >
|
|
||||||
<GridViewColumn.CellTemplate>
|
|
||||||
<DataTemplate>
|
|
||||||
<TextBlock Text="{Binding Path=Enabled}"></TextBlock>
|
|
||||||
</DataTemplate>
|
|
||||||
</GridViewColumn.CellTemplate>
|
|
||||||
</GridViewColumn>
|
|
||||||
</GridView.Columns>
|
</GridView.Columns>
|
||||||
</GridView>
|
</GridView>
|
||||||
</ListView.View>
|
</ListView.View>
|
||||||
</ListView>
|
</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>
|
</Grid>
|
||||||
</TabItem>
|
</TabItem>
|
||||||
</TabControl>
|
</TabControl>
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ using System.Windows;
|
|||||||
using System.Windows.Controls;
|
using System.Windows.Controls;
|
||||||
using Wox.Helper;
|
using Wox.Helper;
|
||||||
using Wox.Infrastructure;
|
using Wox.Infrastructure;
|
||||||
|
using Wox.Infrastructure.UserSettings;
|
||||||
|
|
||||||
namespace Wox
|
namespace Wox
|
||||||
{
|
{
|
||||||
@@ -42,6 +43,11 @@ namespace Wox
|
|||||||
webSearchView.ItemsSource = CommonStorage.Instance.UserSetting.WebSearches;
|
webSearchView.ItemsSource = CommonStorage.Instance.UserSetting.WebSearches;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void ReloadWebSearchView()
|
||||||
|
{
|
||||||
|
webSearchView.Items.Refresh();
|
||||||
|
}
|
||||||
|
|
||||||
private List<string> LoadAvailableThemes()
|
private List<string> LoadAvailableThemes()
|
||||||
{
|
{
|
||||||
string themePath = Directory.GetCurrentDirectory() + "\\Themes\\";
|
string themePath = Directory.GetCurrentDirectory() + "\\Themes\\";
|
||||||
@@ -56,10 +62,41 @@ namespace Wox
|
|||||||
CommonStorage.Instance.Save();
|
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();
|
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>
|
||||||
<RowDefinition></RowDefinition>
|
<RowDefinition></RowDefinition>
|
||||||
|
<RowDefinition></RowDefinition>
|
||||||
</Grid.RowDefinitions>
|
</Grid.RowDefinitions>
|
||||||
<Grid.ColumnDefinitions>
|
<Grid.ColumnDefinitions>
|
||||||
<ColumnDefinition Width="120"></ColumnDefinition>
|
<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>
|
<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>
|
<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="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>
|
</StackPanel>
|
||||||
</Grid>
|
</Grid>
|
||||||
</Window>
|
</Window>
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ using System.Windows;
|
|||||||
using System.Windows.Controls;
|
using System.Windows.Controls;
|
||||||
using System.Windows.Data;
|
using System.Windows.Data;
|
||||||
using System.Windows.Documents;
|
using System.Windows.Documents;
|
||||||
|
using System.Windows.Forms;
|
||||||
using System.Windows.Input;
|
using System.Windows.Input;
|
||||||
using System.Windows.Media;
|
using System.Windows.Media;
|
||||||
using System.Windows.Media.Imaging;
|
using System.Windows.Media.Imaging;
|
||||||
@@ -13,16 +14,40 @@ using System.Windows.Shapes;
|
|||||||
using Wox.Helper;
|
using Wox.Helper;
|
||||||
using Wox.Infrastructure;
|
using Wox.Infrastructure;
|
||||||
using Wox.Infrastructure.UserSettings;
|
using Wox.Infrastructure.UserSettings;
|
||||||
|
using MessageBox = System.Windows.MessageBox;
|
||||||
|
|
||||||
namespace Wox
|
namespace Wox
|
||||||
{
|
{
|
||||||
public partial class WebSearchSetting : Window
|
public partial class WebSearchSetting : Window
|
||||||
{
|
{
|
||||||
public WebSearchSetting()
|
private SettingWidow settingWidow;
|
||||||
|
private bool update;
|
||||||
|
private WebSearch updateWebSearch;
|
||||||
|
|
||||||
|
public WebSearchSetting(SettingWidow settingWidow)
|
||||||
{
|
{
|
||||||
|
this.settingWidow = settingWidow;
|
||||||
InitializeComponent();
|
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)
|
private void BtnCancel_OnClick(object sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
Close();
|
Close();
|
||||||
@@ -50,22 +75,37 @@ namespace Wox
|
|||||||
MessageBox.Show("Please input ActionWord field");
|
MessageBox.Show("Please input ActionWord field");
|
||||||
return;
|
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,
|
if (CommonStorage.Instance.UserSetting.WebSearches.Exists(o => o.ActionWord == action))
|
||||||
Enabled = true,
|
{
|
||||||
IconPath="",
|
MessageBox.Show("ActionWord has existed, please input a new one.");
|
||||||
Url = url,
|
return;
|
||||||
Title = title
|
}
|
||||||
});
|
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();
|
CommonStorage.Instance.Save();
|
||||||
MessageBox.Show("Succeed!");
|
settingWidow.ReloadWebSearchView();
|
||||||
|
Close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user