mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-06 03:07:04 +02:00
code refactoring and add web searches setting window.
This commit is contained in:
85
WinAlfred/Helper/CommonStorage.cs
Normal file
85
WinAlfred/Helper/CommonStorage.cs
Normal file
@@ -0,0 +1,85 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.Serialization.Formatters.Binary;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using Newtonsoft.Json;
|
||||
using WinAlfred.Models;
|
||||
|
||||
namespace WinAlfred.Helper
|
||||
{
|
||||
[Serializable]
|
||||
public class CommonStorage
|
||||
{
|
||||
private static string configPath = Directory.GetCurrentDirectory() + "\\data.json";
|
||||
private static object locker = new object();
|
||||
private static CommonStorage storage;
|
||||
|
||||
public UserSetting UserSetting { get; set; }
|
||||
public UserSelectedRecords UserSelectedRecords { get; set; }
|
||||
|
||||
private CommonStorage()
|
||||
{
|
||||
//default setting
|
||||
UserSetting = new UserSetting
|
||||
{
|
||||
Theme = "Default",
|
||||
ReplaceWinR = true,
|
||||
WebSearches = new List<WebSearch>()
|
||||
};
|
||||
|
||||
UserSelectedRecords = new UserSelectedRecords();
|
||||
}
|
||||
|
||||
public void Save()
|
||||
{
|
||||
lock (locker)
|
||||
{
|
||||
//json is a good choise, readable and flexiable
|
||||
string json = JsonConvert.SerializeObject(storage, Formatting.Indented);
|
||||
File.WriteAllText(configPath, json);
|
||||
}
|
||||
}
|
||||
|
||||
private static void Load()
|
||||
{
|
||||
if (!File.Exists(configPath))
|
||||
{
|
||||
File.Create(configPath).Close();
|
||||
}
|
||||
string json = File.ReadAllText(configPath);
|
||||
if (!string.IsNullOrEmpty(json))
|
||||
{
|
||||
try
|
||||
{
|
||||
storage = JsonConvert.DeserializeObject<CommonStorage>(json);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
//on-op, keep default storage
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static CommonStorage Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (storage == null)
|
||||
{
|
||||
lock (locker)
|
||||
{
|
||||
if (storage == null)
|
||||
{
|
||||
storage = new CommonStorage();
|
||||
Load();
|
||||
}
|
||||
}
|
||||
}
|
||||
return storage;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,343 +0,0 @@
|
||||
/// KEYBOARD.CS
|
||||
/// (c) 2006 by Emma Burrows
|
||||
/// This file contains the following items:
|
||||
/// - KeyboardHook: class to enable low-level keyboard hook using
|
||||
/// the Windows API.
|
||||
/// - KeyboardHookEventHandler: delegate to handle the KeyIntercepted
|
||||
/// event raised by the KeyboardHook class.
|
||||
/// - KeyboardHookEventArgs: EventArgs class to contain the information
|
||||
/// returned by the KeyIntercepted event.
|
||||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace WinAlfred.Helper
|
||||
{
|
||||
/// <summary>
|
||||
/// Low-level keyboard intercept class to trap and suppress system keys.
|
||||
/// </summary>
|
||||
public class GlobalKeyboardHook : IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// Parameters accepted by the KeyboardHook constructor.
|
||||
/// </summary>
|
||||
public enum Parameters
|
||||
{
|
||||
None,
|
||||
AllowAltTab,
|
||||
AllowWindowsKey,
|
||||
AllowAltTabAndWindows,
|
||||
PassAllKeysToNextApp
|
||||
}
|
||||
|
||||
//Internal parameters
|
||||
private bool PassAllKeysToNextApp = false;
|
||||
private bool AllowAltTab = false;
|
||||
private bool AllowWindowsKey = false;
|
||||
|
||||
//Keyboard API constants
|
||||
private const int WH_KEYBOARD_LL = 13;
|
||||
private const int WM_KEYUP = 0x0101;
|
||||
private const int WM_SYSKEYUP = 0x0105;
|
||||
|
||||
//Modifier key constants
|
||||
private const int VK_SHIFT = 0x10;
|
||||
private const int VK_CONTROL = 0x11;
|
||||
private const int VK_MENU = 0x12;
|
||||
private const int VK_CAPITAL = 0x14;
|
||||
|
||||
//Variables used in the call to SetWindowsHookEx
|
||||
private HookHandlerDelegate proc;
|
||||
private IntPtr hookID = IntPtr.Zero;
|
||||
internal delegate IntPtr HookHandlerDelegate(
|
||||
int nCode, IntPtr wParam, ref KBDLLHOOKSTRUCT lParam);
|
||||
|
||||
/// <summary>
|
||||
/// Event triggered when a keystroke is intercepted by the
|
||||
/// low-level hook.
|
||||
/// </summary>
|
||||
public event KeyboardHookEventHandler KeyIntercepted;
|
||||
|
||||
// Structure returned by the hook whenever a key is pressed
|
||||
internal struct KBDLLHOOKSTRUCT
|
||||
{
|
||||
public int vkCode;
|
||||
int scanCode;
|
||||
public int flags;
|
||||
int time;
|
||||
int dwExtraInfo;
|
||||
}
|
||||
|
||||
#region Constructors
|
||||
/// <summary>
|
||||
/// Sets up a keyboard hook to trap all keystrokes without
|
||||
/// passing any to other applications.
|
||||
/// </summary>
|
||||
public GlobalKeyboardHook()
|
||||
{
|
||||
proc = new HookHandlerDelegate(HookCallback);
|
||||
using (Process curProcess = Process.GetCurrentProcess())
|
||||
using (ProcessModule curModule = curProcess.MainModule)
|
||||
{
|
||||
hookID = NativeMethods.SetWindowsHookEx(WH_KEYBOARD_LL, proc,
|
||||
NativeMethods.GetModuleHandle(curModule.ModuleName), 0);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets up a keyboard hook with custom parameters.
|
||||
/// </summary>
|
||||
/// <param name="param">A valid name from the Parameter enum; otherwise, the
|
||||
/// default parameter Parameter.None will be used.</param>
|
||||
public GlobalKeyboardHook(string param)
|
||||
: this()
|
||||
{
|
||||
if (!String.IsNullOrEmpty(param) && Enum.IsDefined(typeof(Parameters), param))
|
||||
{
|
||||
SetParameters((Parameters)Enum.Parse(typeof(Parameters), param));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets up a keyboard hook with custom parameters.
|
||||
/// </summary>
|
||||
/// <param name="param">A value from the Parameters enum.</param>
|
||||
public GlobalKeyboardHook(Parameters param)
|
||||
: this()
|
||||
{
|
||||
SetParameters(param);
|
||||
}
|
||||
|
||||
private void SetParameters(Parameters param)
|
||||
{
|
||||
switch (param)
|
||||
{
|
||||
case Parameters.None:
|
||||
break;
|
||||
case Parameters.AllowAltTab:
|
||||
AllowAltTab = true;
|
||||
break;
|
||||
case Parameters.AllowWindowsKey:
|
||||
AllowWindowsKey = true;
|
||||
break;
|
||||
case Parameters.AllowAltTabAndWindows:
|
||||
AllowAltTab = true;
|
||||
AllowWindowsKey = true;
|
||||
break;
|
||||
case Parameters.PassAllKeysToNextApp:
|
||||
PassAllKeysToNextApp = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Check Modifier keys
|
||||
/// <summary>
|
||||
/// Checks whether Alt, Shift, Control or CapsLock
|
||||
/// is enabled at the same time as another key.
|
||||
/// Modify the relevant sections and return type
|
||||
/// depending on what you want to do with modifier keys.
|
||||
/// </summary>
|
||||
private void CheckModifiers()
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
if ((NativeMethods.GetKeyState(VK_CAPITAL) & 0x0001) != 0)
|
||||
{
|
||||
//CAPSLOCK is ON
|
||||
sb.AppendLine("Capslock is enabled.");
|
||||
}
|
||||
|
||||
if ((NativeMethods.GetKeyState(VK_SHIFT) & 0x8000) != 0)
|
||||
{
|
||||
//SHIFT is pressed
|
||||
sb.AppendLine("Shift is pressed.");
|
||||
}
|
||||
if ((NativeMethods.GetKeyState(VK_CONTROL) & 0x8000) != 0)
|
||||
{
|
||||
//CONTROL is pressed
|
||||
sb.AppendLine("Control is pressed.");
|
||||
}
|
||||
if ((NativeMethods.GetKeyState(VK_MENU) & 0x8000) != 0)
|
||||
{
|
||||
//ALT is pressed
|
||||
sb.AppendLine("Alt is pressed.");
|
||||
}
|
||||
Console.WriteLine(sb.ToString());
|
||||
}
|
||||
#endregion Check Modifier keys
|
||||
|
||||
#region Hook Callback Method
|
||||
/// <summary>
|
||||
/// Processes the key event captured by the hook.
|
||||
/// </summary>
|
||||
private IntPtr HookCallback(
|
||||
int nCode, IntPtr wParam, ref KBDLLHOOKSTRUCT lParam)
|
||||
{
|
||||
bool AllowKey = PassAllKeysToNextApp;
|
||||
|
||||
//Filter wParam for KeyUp events only
|
||||
if (nCode >= 0)
|
||||
{
|
||||
if (wParam == (IntPtr)WM_KEYUP || wParam == (IntPtr)WM_SYSKEYUP)
|
||||
{
|
||||
|
||||
// Check for modifier keys, but only if the key being
|
||||
// currently processed isn't a modifier key (in other
|
||||
// words, CheckModifiers will only run if Ctrl, Shift,
|
||||
// CapsLock or Alt are active at the same time as
|
||||
// another key)
|
||||
if (!(lParam.vkCode >= 160 && lParam.vkCode <= 164))
|
||||
{
|
||||
CheckModifiers();
|
||||
}
|
||||
|
||||
// Check for key combinations that are allowed to
|
||||
// get through to Windows
|
||||
//
|
||||
// Ctrl+Esc or Windows key
|
||||
if (AllowWindowsKey)
|
||||
{
|
||||
switch (lParam.flags)
|
||||
{
|
||||
//Ctrl+Esc
|
||||
case 0:
|
||||
if (lParam.vkCode == 27)
|
||||
AllowKey = true;
|
||||
break;
|
||||
|
||||
//Windows keys
|
||||
case 1:
|
||||
if ((lParam.vkCode == 91) || (lParam.vkCode == 92))
|
||||
AllowKey = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Alt+Tab
|
||||
if (AllowAltTab)
|
||||
{
|
||||
if ((lParam.flags == 32) && (lParam.vkCode == 9))
|
||||
AllowKey = true;
|
||||
}
|
||||
|
||||
OnKeyIntercepted(new KeyboardHookEventArgs(lParam.vkCode, AllowKey));
|
||||
}
|
||||
|
||||
//If this key is being suppressed, return a dummy value
|
||||
if (AllowKey == false)
|
||||
return (System.IntPtr)1;
|
||||
}
|
||||
//Pass key to next application
|
||||
return NativeMethods.CallNextHookEx(hookID, nCode, wParam, ref lParam);
|
||||
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Event Handling
|
||||
/// <summary>
|
||||
/// Raises the KeyIntercepted event.
|
||||
/// </summary>
|
||||
/// <param name="e">An instance of KeyboardHookEventArgs</param>
|
||||
public void OnKeyIntercepted(KeyboardHookEventArgs e)
|
||||
{
|
||||
if (KeyIntercepted != null)
|
||||
KeyIntercepted(e);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Delegate for KeyboardHook event handling.
|
||||
/// </summary>
|
||||
/// <param name="e">An instance of InterceptKeysEventArgs.</param>
|
||||
public delegate void KeyboardHookEventHandler(KeyboardHookEventArgs e);
|
||||
|
||||
/// <summary>
|
||||
/// Event arguments for the KeyboardHook class's KeyIntercepted event.
|
||||
/// </summary>
|
||||
public class KeyboardHookEventArgs : System.EventArgs
|
||||
{
|
||||
|
||||
private string keyName;
|
||||
private int keyCode;
|
||||
private bool passThrough;
|
||||
|
||||
/// <summary>
|
||||
/// The name of the key that was pressed.
|
||||
/// </summary>
|
||||
public string KeyName
|
||||
{
|
||||
get { return keyName; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The virtual key code of the key that was pressed.
|
||||
/// </summary>
|
||||
public int KeyCode
|
||||
{
|
||||
get { return keyCode; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// True if this key combination was passed to other applications,
|
||||
/// false if it was trapped.
|
||||
/// </summary>
|
||||
public bool PassThrough
|
||||
{
|
||||
get { return passThrough; }
|
||||
}
|
||||
|
||||
public KeyboardHookEventArgs(int evtKeyCode, bool evtPassThrough)
|
||||
{
|
||||
keyName = ((Keys)evtKeyCode).ToString();
|
||||
keyCode = evtKeyCode;
|
||||
passThrough = evtPassThrough;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region IDisposable Members
|
||||
/// <summary>
|
||||
/// Releases the keyboard hook.
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
NativeMethods.UnhookWindowsHookEx(hookID);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Native methods
|
||||
|
||||
[ComVisible(false),
|
||||
System.Security.SuppressUnmanagedCodeSecurity()]
|
||||
internal class NativeMethods
|
||||
{
|
||||
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
||||
public static extern IntPtr GetModuleHandle(string lpModuleName);
|
||||
|
||||
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
||||
public static extern IntPtr SetWindowsHookEx(int idHook,
|
||||
HookHandlerDelegate lpfn, IntPtr hMod, uint dwThreadId);
|
||||
|
||||
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
public static extern bool UnhookWindowsHookEx(IntPtr hhk);
|
||||
|
||||
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
||||
public static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
|
||||
IntPtr wParam, ref KBDLLHOOKSTRUCT lParam);
|
||||
|
||||
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]
|
||||
public static extern short GetKeyState(int keyCode);
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,87 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.Serialization.Formatters.Binary;
|
||||
using System.Windows.Documents;
|
||||
using WinAlfred.Plugin;
|
||||
|
||||
namespace WinAlfred.Helper
|
||||
{
|
||||
public class SelectedRecords
|
||||
{
|
||||
private int hasAddedCount = 0;
|
||||
private Dictionary<string, int> dict = new Dictionary<string, int>();
|
||||
private string filePath = Directory.GetCurrentDirectory() + "\\selectedRecords.dat";
|
||||
private static readonly SelectedRecords instance = new SelectedRecords();
|
||||
|
||||
private SelectedRecords()
|
||||
{
|
||||
LoadSelectedRecords();
|
||||
}
|
||||
|
||||
public static SelectedRecords Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
|
||||
private void LoadSelectedRecords()
|
||||
{
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
|
||||
BinaryFormatter b = new BinaryFormatter();
|
||||
dict = (Dictionary<string, int>)b.Deserialize(fileStream);
|
||||
fileStream.Close();
|
||||
}
|
||||
|
||||
if (dict.Count > 1000)
|
||||
{
|
||||
List<string> onlyOnceKeys = (from c in dict where c.Value == 1 select c.Key).ToList();
|
||||
foreach (string onlyOnceKey in onlyOnceKeys)
|
||||
{
|
||||
dict.Remove(onlyOnceKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void AddSelect(Result result)
|
||||
{
|
||||
hasAddedCount++;
|
||||
if (hasAddedCount == 10)
|
||||
{
|
||||
SaveSelectedRecords();
|
||||
hasAddedCount = 0;
|
||||
}
|
||||
|
||||
if (dict.ContainsKey(result.ToString()))
|
||||
{
|
||||
dict[result.ToString()] += 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
dict.Add(result.ToString(), 1);
|
||||
}
|
||||
}
|
||||
|
||||
public int GetSelectedCount(Result result)
|
||||
{
|
||||
if (dict.ContainsKey(result.ToString()))
|
||||
{
|
||||
return dict[result.ToString()];
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
private void SaveSelectedRecords()
|
||||
{
|
||||
FileStream fileStream = new FileStream("selectedRecords.dat", FileMode.Create);
|
||||
BinaryFormatter b = new BinaryFormatter();
|
||||
b.Serialize(fileStream, dict);
|
||||
fileStream.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace WinAlfred.Helper
|
||||
{
|
||||
public class Settings
|
||||
{
|
||||
private string configPath = Directory.GetCurrentDirectory() + "\\config.ini";
|
||||
private static readonly Settings settings = new Settings();
|
||||
IniParser parser;
|
||||
|
||||
public string Theme { get; set; }
|
||||
public bool ReplaceWinR { get; set; }
|
||||
|
||||
private Settings()
|
||||
{
|
||||
if (!File.Exists(configPath)) File.Create(configPath);
|
||||
parser = new IniParser(configPath);
|
||||
LoadSettings();
|
||||
}
|
||||
|
||||
private void LoadSettings()
|
||||
{
|
||||
Theme = parser.GetSetting("ui", "theme");
|
||||
|
||||
string replaceWinRStr = parser.GetSetting("hotkey", "replaceWinR");
|
||||
bool replace = true;
|
||||
if (bool.TryParse(replaceWinRStr, out replace))
|
||||
{
|
||||
ReplaceWinR = replace;
|
||||
}
|
||||
}
|
||||
|
||||
public void SaveSettings()
|
||||
{
|
||||
parser.AddSetting("ui", "theme", Theme);
|
||||
parser.AddSetting("hotkey", "replaceWinR", ReplaceWinR.ToString());
|
||||
parser.SaveSettings();
|
||||
}
|
||||
|
||||
public static Settings Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
return settings;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading;
|
||||
@@ -41,15 +42,13 @@ namespace WinAlfred
|
||||
resultCtrl.resultItemChangedEvent += resultCtrl_resultItemChangedEvent;
|
||||
ThreadPool.SetMaxThreads(30, 10);
|
||||
InitProgressbarAnimation();
|
||||
|
||||
try
|
||||
{
|
||||
ChangeStyles(Settings.Instance.Theme);
|
||||
|
||||
SetTheme(CommonStorage.Instance.UserSetting.Theme);
|
||||
}
|
||||
catch (System.IO.IOException)
|
||||
catch (IOException)
|
||||
{
|
||||
ChangeStyles(Settings.Instance.Theme = "Default");
|
||||
SetTheme(CommonStorage.Instance.UserSetting.Theme = "Default");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -229,7 +228,7 @@ namespace WinAlfred
|
||||
|
||||
private bool KListener_hookedKeyboardCallback(KeyEvent keyevent, int vkcode, SpecialKeyState state)
|
||||
{
|
||||
if (Settings.Instance.ReplaceWinR)
|
||||
if (CommonStorage.Instance.UserSetting.ReplaceWinR)
|
||||
{
|
||||
//todo:need refatoring. move those codes to CMD file or expose events
|
||||
if (keyevent == KeyEvent.WM_KEYDOWN && vkcode == (int)Keys.R && state.WinPressed)
|
||||
@@ -290,7 +289,7 @@ namespace WinAlfred
|
||||
Result result = resultCtrl.AcceptSelect();
|
||||
if (result != null)
|
||||
{
|
||||
SelectedRecords.Instance.AddSelect(result);
|
||||
CommonStorage.Instance.UserSelectedRecords.Add(result);
|
||||
if (!result.DontHideWinAlfredAfterSelect)
|
||||
{
|
||||
HideWinAlfred();
|
||||
@@ -307,7 +306,7 @@ namespace WinAlfred
|
||||
//todo:this should be opened to users, it's their choise to use it or not in thier workflows
|
||||
list.ForEach(o =>
|
||||
{
|
||||
if (o.AutoAjustScore) o.Score += SelectedRecords.Instance.GetSelectedCount(o);
|
||||
if (o.AutoAjustScore) o.Score += CommonStorage.Instance.UserSelectedRecords.GetSelectedCount(o);
|
||||
});
|
||||
resultCtrl.Dispatcher.Invoke(new Action(() =>
|
||||
{
|
||||
@@ -317,7 +316,7 @@ namespace WinAlfred
|
||||
}
|
||||
}
|
||||
|
||||
public void ChangeStyles(string themeName)
|
||||
public void SetTheme(string themeName)
|
||||
{
|
||||
ResourceDictionary dict = new ResourceDictionary
|
||||
{
|
||||
@@ -367,6 +366,5 @@ namespace WinAlfred
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
47
WinAlfred/Models/UserSelectedRecords.cs
Normal file
47
WinAlfred/Models/UserSelectedRecords.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.Serialization.Formatters.Binary;
|
||||
using WinAlfred.Helper;
|
||||
using WinAlfred.Plugin;
|
||||
|
||||
namespace WinAlfred.Models
|
||||
{
|
||||
[Serializable]
|
||||
public class UserSelectedRecords
|
||||
{
|
||||
private static int hasAddedCount = 0;
|
||||
|
||||
public Dictionary<string,int> Records = new Dictionary<string, int>();
|
||||
|
||||
public void Add(Result result)
|
||||
{
|
||||
if (Records.ContainsKey(result.ToString()))
|
||||
{
|
||||
Records[result.ToString()] += 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
Records.Add(result.ToString(), 1);
|
||||
}
|
||||
|
||||
//hasAddedCount++;
|
||||
//if (hasAddedCount == 10)
|
||||
//{
|
||||
// hasAddedCount = 0;
|
||||
//}
|
||||
CommonStorage.Instance.Save();
|
||||
|
||||
}
|
||||
|
||||
public int GetSelectedCount(Result result)
|
||||
{
|
||||
if (Records.ContainsKey(result.ToString()))
|
||||
{
|
||||
return Records[result.ToString()];
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
14
WinAlfred/Models/UserSetting.cs
Normal file
14
WinAlfred/Models/UserSetting.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace WinAlfred.Models
|
||||
{
|
||||
public class UserSetting
|
||||
{
|
||||
public string Theme { get; set; }
|
||||
public bool ReplaceWinR { get; set; }
|
||||
public List<WebSearch> WebSearches { get; set; }
|
||||
}
|
||||
}
|
||||
10
WinAlfred/Models/WebSearch.cs
Normal file
10
WinAlfred/Models/WebSearch.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace WinAlfred.Models
|
||||
{
|
||||
public class WebSearch
|
||||
{
|
||||
public string Keyword { get; set; }
|
||||
public string IconPath { get; set; }
|
||||
public string Website { get; set; }
|
||||
public string Enabled { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -80,7 +80,7 @@ namespace WinAlfred
|
||||
AddHandler(MouseLeftButtonUpEvent, new RoutedEventHandler((o, e) =>
|
||||
{
|
||||
Result.Action();
|
||||
SelectedRecords.Instance.AddSelect(result);
|
||||
CommonStorage.Instance.UserSelectedRecords.Add(result);
|
||||
if (!result.DontHideWinAlfredAfterSelect)
|
||||
{
|
||||
App.Window.HideApp();
|
||||
|
||||
@@ -6,15 +6,57 @@
|
||||
ResizeMode="NoResize"
|
||||
WindowStartupLocation="CenterScreen"
|
||||
Height="407.447" Width="843.989">
|
||||
<StackPanel Orientation="Vertical" Margin="10">
|
||||
<StackPanel Orientation="Horizontal">
|
||||
<TextBlock Text="Theme:" />
|
||||
<ComboBox x:Name="themeComboBox" SelectionChanged="ThemeComboBox_OnSelectionChanged" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120"/>
|
||||
</StackPanel>
|
||||
<StackPanel Orientation="Horizontal">
|
||||
<CheckBox x:Name="cbReplaceWinR" />
|
||||
<TextBlock Text="Replace Win+R:" />
|
||||
</StackPanel>
|
||||
</StackPanel>
|
||||
|
||||
<TabControl Height="auto" >
|
||||
<TabItem Header="Basic">
|
||||
<StackPanel Orientation="Vertical" Margin="10">
|
||||
<StackPanel Orientation="Horizontal">
|
||||
<TextBlock Text="Theme:" />
|
||||
<ComboBox x:Name="themeComboBox" SelectionChanged="ThemeComboBox_OnSelectionChanged" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120"/>
|
||||
</StackPanel>
|
||||
<StackPanel Orientation="Horizontal">
|
||||
<CheckBox x:Name="cbReplaceWinR" />
|
||||
<TextBlock Text="Replace Win+R:" />
|
||||
</StackPanel>
|
||||
</StackPanel>
|
||||
</TabItem>
|
||||
<TabItem Header="Web Search">
|
||||
<Grid>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="*"></RowDefinition>
|
||||
<RowDefinition Height="50"></RowDefinition>
|
||||
</Grid.RowDefinitions>
|
||||
<ListView Grid.Row="0">
|
||||
<ListView.View>
|
||||
<GridView>
|
||||
<GridView.Columns>
|
||||
<GridViewColumn Header="Keyword" Width="180">
|
||||
<GridViewColumn.CellTemplate>
|
||||
<DataTemplate>
|
||||
<TextBlock Text="{Binding Path=OPERATOR}"></TextBlock>
|
||||
</DataTemplate>
|
||||
</GridViewColumn.CellTemplate>
|
||||
</GridViewColumn>
|
||||
<GridViewColumn Header="Website" Width="500">
|
||||
<GridViewColumn.CellTemplate>
|
||||
<DataTemplate>
|
||||
<TextBlock Text="{Binding Path=DDATE}"></TextBlock>
|
||||
</DataTemplate>
|
||||
</GridViewColumn.CellTemplate>
|
||||
</GridViewColumn>
|
||||
<GridViewColumn Header="Enable" Width="50" >
|
||||
<GridViewColumn.CellTemplate>
|
||||
<DataTemplate>
|
||||
<TextBlock Text="{Binding Path=CONTENT}"></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>
|
||||
</Grid>
|
||||
</TabItem>
|
||||
</TabControl>
|
||||
</Window>
|
||||
|
||||
@@ -18,13 +18,13 @@ namespace WinAlfred
|
||||
Loaded += Setting_Loaded;
|
||||
cbReplaceWinR.Checked += (o, e) =>
|
||||
{
|
||||
Settings.Instance.ReplaceWinR = true;
|
||||
Settings.Instance.SaveSettings();
|
||||
CommonStorage.Instance.UserSetting.ReplaceWinR = true;
|
||||
CommonStorage.Instance.Save();
|
||||
};
|
||||
cbReplaceWinR.Unchecked += (o, e) =>
|
||||
{
|
||||
Settings.Instance.ReplaceWinR = false;
|
||||
Settings.Instance.SaveSettings();
|
||||
CommonStorage.Instance.UserSetting.ReplaceWinR = false;
|
||||
CommonStorage.Instance.Save();
|
||||
};
|
||||
}
|
||||
|
||||
@@ -36,8 +36,8 @@ namespace WinAlfred
|
||||
themeComboBox.Items.Add(themeName);
|
||||
}
|
||||
|
||||
themeComboBox.SelectedItem = Settings.Instance.Theme;
|
||||
cbReplaceWinR.IsChecked = Settings.Instance.ReplaceWinR;
|
||||
themeComboBox.SelectedItem = CommonStorage.Instance.UserSetting.Theme;
|
||||
cbReplaceWinR.IsChecked = CommonStorage.Instance.UserSetting.ReplaceWinR;
|
||||
}
|
||||
|
||||
private List<string> LoadAvailableThemes()
|
||||
@@ -49,9 +49,15 @@ namespace WinAlfred
|
||||
private void ThemeComboBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
|
||||
{
|
||||
string themeName = themeComboBox.SelectedItem.ToString();
|
||||
mainWindow.ChangeStyles(themeName);
|
||||
Settings.Instance.Theme = themeName;
|
||||
Settings.Instance.SaveSettings();
|
||||
mainWindow.SetTheme(themeName);
|
||||
CommonStorage.Instance.UserSetting.Theme = themeName;
|
||||
CommonStorage.Instance.Save();
|
||||
}
|
||||
|
||||
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
WebSearchSetting webSearch = new WebSearchSetting();
|
||||
webSearch.Show();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
48
WinAlfred/WebSearchSetting.xaml
Normal file
48
WinAlfred/WebSearchSetting.xaml
Normal file
@@ -0,0 +1,48 @@
|
||||
<Window x:Class="WinAlfred.WebSearchSetting"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
Icon="Images\app.png"
|
||||
ResizeMode="NoResize"
|
||||
WindowStartupLocation="CenterScreen"
|
||||
Title="WebSearchSetting" Height="400" Width="674.766">
|
||||
<Grid>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition></RowDefinition>
|
||||
<RowDefinition Height="40"></RowDefinition>
|
||||
<RowDefinition></RowDefinition>
|
||||
<RowDefinition Height="24"></RowDefinition>
|
||||
<RowDefinition></RowDefinition>
|
||||
<RowDefinition Height="24"></RowDefinition>
|
||||
<RowDefinition></RowDefinition>
|
||||
<RowDefinition Height="24"></RowDefinition>
|
||||
<RowDefinition></RowDefinition>
|
||||
</Grid.RowDefinitions>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="120"></ColumnDefinition>
|
||||
<ColumnDefinition></ColumnDefinition>
|
||||
</Grid.ColumnDefinitions>
|
||||
<TextBlock Margin="10" FontSize="14" Grid.Row="0" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Right">Search URL:</TextBlock>
|
||||
<TextBox Margin="10" Grid.Row="0" Width="400" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left"></TextBox>
|
||||
<TextBlock Margin="10 0 10 0" Foreground="Gray" VerticalAlignment="Top" Grid.Row="1" Grid.Column="1" TextWrapping="Wrap" HorizontalAlignment="Left">Perform a search on a website and copy the result URL. Replace your search term with {query} in curly brackets</TextBlock>
|
||||
|
||||
<TextBlock Margin="10" FontSize="14" Grid.Row="2" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Right">Title:</TextBlock>
|
||||
<TextBox Margin="10" Grid.Row="2" Width="400" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left"></TextBox>
|
||||
<TextBlock Margin="10 0 10 0" Foreground="Gray" VerticalAlignment="Top" Grid.Row="3" Grid.Column="1" TextWrapping="Wrap" HorizontalAlignment="Left">What to show in the WinAlfred. The search query is automatically appended for clarity.</TextBlock>
|
||||
|
||||
<TextBlock Margin="10" FontSize="14" Grid.Row="4" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Right">Keyword:</TextBlock>
|
||||
<TextBox Margin="10" Grid.Row="4" Width="400" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left"></TextBox>
|
||||
<TextBlock Margin="10 0 10 0" Foreground="Gray" VerticalAlignment="Top" Grid.Row="5" Grid.Column="1" TextWrapping="Wrap" HorizontalAlignment="Left">What you type to use this shortcut.</TextBlock>
|
||||
|
||||
<TextBlock Margin="10" FontSize="14" Grid.Row="6" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Right">Validation:</TextBlock>
|
||||
<StackPanel Grid.Row="6" Grid.Column="1" VerticalAlignment="Center" Orientation="Horizontal">
|
||||
<TextBox Margin="10" Width="400" HorizontalAlignment="Left"></TextBox>
|
||||
<Button x:Name="btnValidation" Width="60" Height="24">Test</Button>
|
||||
</StackPanel>
|
||||
<TextBlock Margin="10 0 10 0" Foreground="Gray" VerticalAlignment="Top" Grid.Row="7" Grid.Column="1" TextWrapping="Wrap" HorizontalAlignment="Left">Type some text and click test to check it works.</TextBlock>
|
||||
|
||||
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Grid.Row="8" Grid.Column="1">
|
||||
<Button x:Name="btnCancel" Click="BtnCancel_OnClick" Margin="10 0 10 0" Width="80" Height="25">Cancel</Button>
|
||||
<Button Margin="10 0 10 0" Width="80" Height="25">Add</Button>
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
</Window>
|
||||
28
WinAlfred/WebSearchSetting.xaml.cs
Normal file
28
WinAlfred/WebSearchSetting.xaml.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Documents;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
using System.Windows.Shapes;
|
||||
|
||||
namespace WinAlfred
|
||||
{
|
||||
public partial class WebSearchSetting : Window
|
||||
{
|
||||
public WebSearchSetting()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private void BtnCancel_OnClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -41,7 +41,7 @@
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<UseVSHostingProcess>false</UseVSHostingProcess>
|
||||
<UseVSHostingProcess>true</UseVSHostingProcess>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
@@ -85,6 +85,7 @@
|
||||
<Reference Include="Newtonsoft.Json">
|
||||
<HintPath>..\packages\Newtonsoft.Json.5.0.8\lib\net35\Newtonsoft.Json.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="PresentationUI, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
|
||||
<Reference Include="ReachFramework" />
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Data" />
|
||||
@@ -116,10 +117,11 @@
|
||||
<Compile Include="Commands\SystemCommand.cs" />
|
||||
<Compile Include="DispatcherExtensions.cs" />
|
||||
<Compile Include="Helper\DwmDropShadow.cs" />
|
||||
<Compile Include="Helper\GlobalKeyboardHook.cs" />
|
||||
<Compile Include="Helper\KeyboardHook.cs" />
|
||||
<Compile Include="Helper\Log.cs" />
|
||||
<Compile Include="Helper\Settings.cs" />
|
||||
<Compile Include="Helper\CommonStorage.cs" />
|
||||
<Compile Include="Models\UserSetting.cs" />
|
||||
<Compile Include="Models\WebSearch.cs" />
|
||||
<Compile Include="Helper\WinAlfredException.cs" />
|
||||
<Compile Include="Helper\KeyboardListener.cs" />
|
||||
<Compile Include="Msg.xaml.cs">
|
||||
@@ -138,10 +140,13 @@
|
||||
<Compile Include="ResultItem.xaml.cs">
|
||||
<DependentUpon>ResultItem.xaml</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Helper\SelectedRecords.cs" />
|
||||
<Compile Include="Models\UserSelectedRecords.cs" />
|
||||
<Compile Include="SettingWindow.xaml.cs">
|
||||
<DependentUpon>SettingWindow.xaml</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="WebSearchSetting.xaml.cs">
|
||||
<DependentUpon>WebSearchSetting.xaml</DependentUpon>
|
||||
</Compile>
|
||||
<Page Include="MainWindow.xaml">
|
||||
<Generator>MSBuild:Compile</Generator>
|
||||
<SubType>Designer</SubType>
|
||||
@@ -178,6 +183,10 @@
|
||||
<Generator>MSBuild:Compile</Generator>
|
||||
<SubType>Designer</SubType>
|
||||
</Page>
|
||||
<Page Include="WebSearchSetting.xaml">
|
||||
<SubType>Designer</SubType>
|
||||
<Generator>MSBuild:Compile</Generator>
|
||||
</Page>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Properties\AssemblyInfo.cs">
|
||||
|
||||
Reference in New Issue
Block a user