Browse Source

:bulb: 优化搜索功能

liufei 3 years ago
parent
commit
a0ba6d4ecd
5 changed files with 89 additions and 33 deletions
  1. 14 0
      Constant/SearchType.cs
  2. 25 0
      Converts/SearchTypeConvert.cs
  3. 3 0
      GeekDesk.csproj
  4. 4 5
      MainWindow.xaml
  5. 43 28
      MainWindow.xaml.cs

+ 14 - 0
Constant/SearchType.cs

@@ -0,0 +1,14 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace GeekDesk.Constant
+{
+    public enum SearchType
+    {
+        HOT_KEY = 0, //快捷键
+        KEY_DOWN = 1, //按下即搜
+    }
+}

+ 25 - 0
Converts/SearchTypeConvert.cs

@@ -0,0 +1,25 @@
+using GeekDesk.Constant;
+using System;
+using System.Globalization;
+using System.Windows.Data;
+
+namespace GeekDesk.Converts
+{
+    public class SearchTypeConvert : IValueConverter
+    {
+        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
+        {
+            return (SearchType)value == (SearchType)int.Parse(parameter.ToString());
+        }
+
+        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
+        {
+            bool isChecked = (bool)value;
+            if (!isChecked)
+            {
+                return null;
+            }
+            return (SearchType)int.Parse(parameter.ToString());
+        }
+    }
+}

+ 3 - 0
GeekDesk.csproj

@@ -147,6 +147,7 @@
     <Compile Include="Constant\CommonEnum.cs" />
     <Compile Include="Constant\IconStartType.cs" />
     <Compile Include="Constant\RunTimeStatus.cs" />
+    <Compile Include="Constant\SearchType.cs" />
     <Compile Include="Constant\SortType.cs" />
     <Compile Include="Constant\TodoTaskExecType.cs" />
     <Compile Include="Constant\BGStyle.cs" />
@@ -222,6 +223,7 @@
     </Compile>
     <Compile Include="Converts\CountGreZero2BoolConvert.cs" />
     <Compile Include="Converts\Count2VisibleConvert.cs" />
+    <Compile Include="Converts\SearchTypeConvert.cs" />
     <Compile Include="Converts\StringAppendConvert.cs" />
     <Compile Include="Converts\DoubleToGridLength.cs" />
     <Compile Include="Converts\MenuInfoConvert.cs" />
@@ -248,6 +250,7 @@
     <Compile Include="Util\CommonCode.cs" />
     <Compile Include="Util\FileIcon.cs" />
     <Compile Include="Util\FileUtil.cs" />
+    <Compile Include="Util\KeyUtil.cs" />
     <Compile Include="Util\LogUtil.cs" />
     <Compile Include="Util\MarginHide.cs" />
     <Compile Include="Util\HttpUtil.cs" />

+ 4 - 5
MainWindow.xaml

@@ -29,7 +29,7 @@
         <WindowChrome CaptionHeight="0" ResizeBorderThickness="15"/>
     </WindowChrome.WindowChrome>
     <Window.Resources>
-        <RoutedUICommand x:Key="SearchItem" Text="SearchItem"/>
+        <RoutedUICommand x:Key="SearchHotKeyDown" Text="SearchHotKeyDown"/>
 
         <cvt:MenuWidthConvert x:Key="MenuWidthConvert"/>
         <cvt:OpcityConvert x:Key="OpcityConvert"/>
@@ -41,10 +41,10 @@
 
     </Window.Resources>
     <Window.InputBindings>
-        <KeyBinding Gesture="Ctrl+F" Key="F"  Command="{StaticResource SearchItem}"/>
+        <KeyBinding Gesture="Ctrl+F" Key="F"  Command="{StaticResource SearchHotKeyDown}"/>
     </Window.InputBindings>
     <Window.CommandBindings>
-        <CommandBinding Command="{StaticResource SearchItem}" CanExecute="SearchItem"/>
+        <CommandBinding Command="{StaticResource SearchHotKeyDown}" CanExecute="SearchHotKeyDown"/>
         <!--<CommandBinding Command="ChangeImgBG" Executed="ChangeImgBG_Executed" CanExecute="ChangeImgBG_CanExecute" />-->
 
     </Window.CommandBindings>
@@ -132,11 +132,10 @@
                     <!--搜索输入框-->
                     <TextBox Panel.ZIndex="2" Grid.Row="0" Grid.Column="1" 
                              x:Name="SearchBox"
-                             Visibility="Collapsed"
+                             Width="0"
                              Margin="-100,5,0,0"
                              Height="32"
                              FontSize="16"
-                             Width="400"
                              BorderThickness="0"
                              TextChanged="SearchBox_TextChanged"
                              />

+ 43 - 28
MainWindow.xaml.cs

@@ -59,11 +59,19 @@ namespace GeekDesk
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
-        private void SearchItem(object sender, CanExecuteRoutedEventArgs e)
+        private void SearchHotKeyDown(object sender, CanExecuteRoutedEventArgs e)
+        {
+            if (appData.AppConfig.SearchType == SearchType.HOT_KEY)
+            {
+                ShowSearchBox();
+            }
+        }
+
+        private void ShowSearchBox()
         {
             RunTimeStatus.SEARCH_BOX_SHOW = true;
             RightCard.VisibilitySearchCard(Visibility.Visible);
-            SearchBox.Visibility = Visibility.Visible;
+            SearchBox.Width = 400;
             SearchBox.Focus();
         }
         /// <summary>
@@ -73,6 +81,14 @@ namespace GeekDesk
         /// <param name="e"></param>
         private void SearchBox_TextChanged(object sender, TextChangedEventArgs e)
         {
+            if (!RunTimeStatus.SEARCH_BOX_SHOW && appData.AppConfig.SearchType != SearchType.KEY_DOWN)
+            {
+                SearchBox.Text = "";
+                return;
+            }
+
+            if (!RunTimeStatus.SEARCH_BOX_SHOW) ShowSearchBox();
+
             string inputText = SearchBox.Text.ToLower();
             RightCard.VerticalUFG.Visibility = Visibility.Collapsed;
             if (!string.IsNullOrEmpty(inputText))
@@ -102,7 +118,8 @@ namespace GeekDesk
         public void HidedSearchBox()
         {
             RunTimeStatus.SEARCH_BOX_SHOW = false;
-            SearchBox.Visibility = Visibility.Collapsed;
+            SearchBox.Width = 0;
+            App.DoEvents();
             SearchIconList.IconList.Clear();
             RightCard.VisibilitySearchCard(Visibility.Collapsed);
             SearchBox.Text = "";
@@ -190,9 +207,8 @@ namespace GeekDesk
         {
             try
             {
-                if (appData.AppConfig.HotkeyModifiers != 0)
+                if (appData.AppConfig.HotkeyModifiers != GlobalHotKey.HotkeyModifiers.None)
                 {
-
                     hotKeyId = GlobalHotKey.RegisterHotKey(appData.AppConfig.HotkeyModifiers, appData.AppConfig.Hotkey, () =>
                     {
                         if (MotionControl.hotkeyFinished)
@@ -211,6 +227,8 @@ namespace GeekDesk
                     {
                         HandyControl.Controls.Growl.Success("GeekDesk快捷键注册成功(" + appData.AppConfig.HotkeyStr + ")!", "HotKeyGrowl");
                     }
+                } else
+                {
                 }
             }
             catch (Exception)
@@ -237,7 +255,7 @@ namespace GeekDesk
             try
             {
 
-                if (appData.AppConfig.ToDoHotkeyModifiers != 0)
+                if (appData.AppConfig.HotkeyModifiers != GlobalHotKey.HotkeyModifiers.None)
                 {
                     //加载完毕注册热键
                     toDoHotKeyId = GlobalHotKey.RegisterHotKey(appData.AppConfig.ToDoHotkeyModifiers, appData.AppConfig.ToDoHotkey, () =>
@@ -274,7 +292,7 @@ namespace GeekDesk
         {
             try
             {
-                if (appData.AppConfig.ColorPickerHotkeyModifiers != 0)
+                if (appData.AppConfig.HotkeyModifiers != GlobalHotKey.HotkeyModifiers.None)
                 {
                     //加载完毕注册热键
                     colorPickerHotKeyId = GlobalHotKey.RegisterHotKey(appData.AppConfig.ColorPickerHotkeyModifiers, appData.AppConfig.ColorPickerHotkey, () =>
@@ -363,14 +381,7 @@ namespace GeekDesk
         /// <param name="e"></param>
         private void CloseButtonClick(object sender, RoutedEventArgs e)
         {
-            if (appData.AppConfig.AppAnimation)
-            {
-                FadeStoryBoard(0, (int)CommonEnum.WINDOW_ANIMATION_TIME, Visibility.Collapsed);
-            }
-            else
-            {
-                this.Visibility = Visibility.Collapsed;
-            }
+            HideApp();
         }
 
 
@@ -422,7 +433,7 @@ namespace GeekDesk
 
             FadeStoryBoard(1, (int)CommonEnum.WINDOW_ANIMATION_TIME, Visibility.Visible);
 
-            Keyboard.Focus(mainWindow.EmptyTextBox);
+            Keyboard.Focus(mainWindow.SearchBox);
         }
 
         public static void HideApp()
@@ -432,17 +443,7 @@ namespace GeekDesk
                 if (RunTimeStatus.SEARCH_BOX_SHOW)
                 {
                     mainWindow.HidedSearchBox();
-
-                    Thread t = new Thread(() =>
-                    {
-                        Thread.Sleep(100);
-                        App.Current.Dispatcher.BeginInvoke(new Action(() =>
-                        {
-                            FadeStoryBoard(0, (int)CommonEnum.WINDOW_ANIMATION_TIME, Visibility.Collapsed);
-                        }));
-                    });
-                    t.IsBackground = true;
-                    t.Start();
+                    FadeStoryBoard(0, (int)CommonEnum.WINDOW_ANIMATION_TIME, Visibility.Collapsed);
                 }
                 else
                 {
@@ -656,12 +657,26 @@ namespace GeekDesk
 
         public void OnKeyDown(object sender, KeyEventArgs e)
         {
+            char c = (char)e.Key;
+
             if (e.Key == Key.Escape)
             {
                 HideApp();
             }
+            //else if (
+            //    appData.AppConfig.SearchType == SearchType.KEY_DOWN &&
+            //    (
+            //        (e.Key >= Key.D0 && e.Key <= Key.Z) 
+            //        || (e.Key >= Key.NumPad0 && e.Key < Key.NumPad9)
+            //        )
+            //    )
+            //{
+            //    ShowSearchBox();
+            //}
         }
 
+
+
         /// <summary>
         /// 为了让修改菜单的textBox失去焦点
         /// </summary>
@@ -669,7 +684,7 @@ namespace GeekDesk
         /// <param name="e"></param>
         private void MainWindow_MouseDown(object sender, MouseButtonEventArgs e)
         {
-            EmptyTextBox.Focus();
+            SearchBox.Focus();
         }
 
         /// <summary>