|
1 | | -using Microsoft.UI.Xaml; |
2 | 1 | using Microsoft.UI.Xaml.Controls; |
3 | | -using Microsoft.UI.Xaml.Input; |
4 | 2 | using System; |
5 | 3 | using System.IO; |
6 | 4 | using System.Linq; |
7 | | -using Windows.ApplicationModel.DataTransfer; |
8 | | -using Windows.Storage; |
9 | 5 |
|
10 | 6 | namespace Edge |
11 | 7 | { |
12 | 8 | public sealed partial class WebSearch : UserControl |
13 | 9 | { |
14 | 10 | public string Text |
15 | 11 | { |
16 | | - get => SearchBox.Text; |
17 | | - set => SearchBox.Text = value; |
| 12 | + get => searchBox.Text; |
| 13 | + set => searchBox.Text = value; |
18 | 14 | } |
19 | 15 |
|
20 | 16 | public WebSearch() |
21 | 17 | { |
22 | 18 | this.InitializeComponent(); |
23 | 19 | } |
24 | 20 |
|
25 | | - private void SearchBox_GotFocus(object sender, RoutedEventArgs e) |
26 | | - { |
27 | | - showPopup(true); |
28 | | - } |
29 | | - |
30 | | - private void SearchBox_LostFocus(object sender, RoutedEventArgs e) |
31 | | - { |
32 | | - DispatcherQueue.TryEnqueue(() => |
33 | | - { |
34 | | - if (SuggestionPopup.IsOpen) |
35 | | - { |
36 | | - showPopup(false); |
37 | | - } |
38 | | - }); |
39 | | - } |
40 | | - |
41 | | - private void showPopup(bool visible) |
42 | | - { |
43 | | - SuggestionPopup.IsOpen = visible; |
44 | | - listView.Width = SearchBox.ActualWidth; |
45 | | - } |
46 | | - |
47 | | - private void OnTextChanged(object sender, TextChangedEventArgs e) |
48 | | - { |
49 | | - string text = (sender as TextBox).Text; |
50 | | - string lastWord = text.Split(' ')[^1].ToLower(); |
51 | | - if (!string.IsNullOrEmpty(lastWord)) |
52 | | - { |
53 | | - var result = App.searchEngine.SearchWords(lastWord).Take(10).ToList(); |
54 | | - listView.ItemsSource = result; |
55 | | - } |
56 | | - } |
57 | | - |
58 | | - private void TextBox_KeyDown(object sender, KeyRoutedEventArgs e) |
59 | | - { |
60 | | - if (e.Key == Windows.System.VirtualKey.Enter) |
61 | | - { |
62 | | - MainWindow mainWindow = App.GetWindowForElement(this); |
63 | | - StartSearch(SearchBox.Text, mainWindow); |
64 | | - SearchBox.Text = string.Empty; |
65 | | - } |
66 | | - } |
67 | | - |
68 | | - private void SuggestItemClick(object sender, ItemClickEventArgs e) |
69 | | - { |
70 | | - string item = e.ClickedItem as string; |
71 | | - string text = SearchBox.Text; |
72 | | - int lastIndex = text.LastIndexOf(' '); |
73 | | - if (lastIndex == -1) |
74 | | - { |
75 | | - SearchBox.Text = item; |
76 | | - return; |
77 | | - } |
78 | | - |
79 | | - string prefix = text[..lastIndex]; |
80 | | - SearchBox.Text = $"{prefix} {item}"; |
81 | | - } |
82 | | - |
83 | 21 | public static void StartSearch(string text, MainWindow mainWindow) |
84 | 22 | { |
85 | 23 | UriType uriType = text.DetectUri(); |
@@ -126,36 +64,51 @@ public static void Navigate(string site, MainWindow mainWindow) |
126 | 64 | Uri uri = new(site); |
127 | 65 | if ((mainWindow.TabView.SelectedItem != null) && (mainWindow.SelectedItem is WebViewPage webviewPage)) |
128 | 66 | { |
129 | | - webviewPage.webView2.Source = uri; |
| 67 | + webviewPage.WebView2.Source = uri; |
130 | 68 | } |
131 | 69 | else |
132 | 70 | { |
133 | 71 | mainWindow.AddNewTab(new WebViewPage(uri)); |
134 | 72 | } |
135 | 73 | } |
136 | 74 |
|
137 | | - private void SearchBox_DragOver(object sender, DragEventArgs e) |
| 75 | + private void AutoSuggestBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args) |
138 | 76 | { |
139 | | - if (e.DataView.Contains(StandardDataFormats.StorageItems)) |
| 77 | + if (args.Reason == AutoSuggestionBoxTextChangeReason.UserInput) |
140 | 78 | { |
141 | | - e.AcceptedOperation = DataPackageOperation.Copy; |
| 79 | + string text = sender.Text; |
| 80 | + string lastWord = text.Split(' ')[^1].ToLower(); |
| 81 | + if (!string.IsNullOrEmpty(lastWord)) |
| 82 | + { |
| 83 | + sender.ItemsSource = App.searchEngine.SearchWords(lastWord); |
| 84 | + } |
142 | 85 | } |
143 | | - else |
| 86 | + } |
| 87 | + |
| 88 | + private void AutoSuggestBox_SuggestionChosen(AutoSuggestBox sender, AutoSuggestBoxSuggestionChosenEventArgs args) |
| 89 | + { |
| 90 | + string text = sender.Text; |
| 91 | + string item = args.SelectedItem as string; |
| 92 | + |
| 93 | + int lastIndex = text.LastIndexOf(' '); |
| 94 | + if (lastIndex == -1) |
144 | 95 | { |
145 | | - e.AcceptedOperation = DataPackageOperation.None; |
| 96 | + sender.Text = item; |
| 97 | + return; |
146 | 98 | } |
| 99 | + |
| 100 | + string prefix = text[..lastIndex]; |
| 101 | + sender.Text = $"{prefix} {item}"; |
147 | 102 | } |
148 | 103 |
|
149 | | - private async void SearchBox_Drop(object sender, DragEventArgs e) |
| 104 | + private void AutoSuggestBox_QuerySubmitted(AutoSuggestBox sender, AutoSuggestBoxQuerySubmittedEventArgs args) |
150 | 105 | { |
151 | | - if (e.DataView.Contains(StandardDataFormats.StorageItems)) |
| 106 | + if (args.ChosenSuggestion == null) |
152 | 107 | { |
153 | | - var items = await e.DataView.GetStorageItemsAsync(); |
154 | | - var file = items.OfType<StorageFile>().FirstOrDefault(); |
155 | | - if (file != null) |
156 | | - { |
157 | | - SearchBox.Text = file.Path; |
158 | | - } |
| 108 | + string text = args.QueryText; |
| 109 | + sender.Text = string.Empty; |
| 110 | + MainWindow mainWindow = App.GetWindowForElement(this); |
| 111 | + StartSearch(text, mainWindow); |
159 | 112 | } |
160 | 113 | } |
161 | 114 | } |
|
0 commit comments