Skip to content

Commit d7e188e

Browse files
committed
搜索框修改为 AutoSuggestBox
1 parent 6ff9c8b commit d7e188e

File tree

7 files changed

+74
-164
lines changed

7 files changed

+74
-164
lines changed

App.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public static MainWindow GetWindowForElement(UIElement element)
6565
public static WebView2 GetWebView2(UIElement element)
6666
{
6767
WebViewPage page = GetWindowForElement(element).SelectedItem as WebViewPage;
68-
return page.webView2;
68+
return page.WebView2;
6969
}
7070

7171
protected override void OnLaunched(LaunchActivatedEventArgs args)

Controls/WebSearch.xaml

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,10 @@
88
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
99
mc:Ignorable="d">
1010

11-
<StackPanel>
12-
<TextBox
13-
x:Name="SearchBox"
14-
PlaceholderText="搜索或输入 Web 地址"
15-
GotFocus="SearchBox_GotFocus"
16-
LostFocus="SearchBox_LostFocus"
17-
KeyDown="TextBox_KeyDown"
18-
TextChanged="OnTextChanged"
19-
DragOver="SearchBox_DragOver"
20-
Drop="SearchBox_Drop"/>
21-
<Popup
22-
x:Name="SuggestionPopup">
23-
<ListView
24-
x:Name="listView"
25-
Background="{ThemeResource CardBackgroundFillColorSecondaryBrush}"
26-
BorderBrush="{ThemeResource ButtonBorderThemeBrush}"
27-
BorderThickness="{ThemeResource ButtonBorderThemeThickness}"
28-
CornerRadius="{ThemeResource ControlCornerRadius}"
29-
IsItemClickEnabled="True"
30-
ItemClick="SuggestItemClick"/>
31-
</Popup>
32-
</StackPanel>
11+
<AutoSuggestBox
12+
x:Name="searchBox"
13+
PlaceholderText="搜索或输入 Web 地址"
14+
TextChanged="AutoSuggestBox_TextChanged"
15+
SuggestionChosen="AutoSuggestBox_SuggestionChosen"
16+
QuerySubmitted="AutoSuggestBox_QuerySubmitted" />
3317
</UserControl>

Controls/WebSearch.xaml.cs

Lines changed: 31 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,23 @@
1-
using Microsoft.UI.Xaml;
21
using Microsoft.UI.Xaml.Controls;
3-
using Microsoft.UI.Xaml.Input;
42
using System;
53
using System.IO;
64
using System.Linq;
7-
using Windows.ApplicationModel.DataTransfer;
8-
using Windows.Storage;
95

106
namespace Edge
117
{
128
public sealed partial class WebSearch : UserControl
139
{
1410
public string Text
1511
{
16-
get => SearchBox.Text;
17-
set => SearchBox.Text = value;
12+
get => searchBox.Text;
13+
set => searchBox.Text = value;
1814
}
1915

2016
public WebSearch()
2117
{
2218
this.InitializeComponent();
2319
}
2420

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-
8321
public static void StartSearch(string text, MainWindow mainWindow)
8422
{
8523
UriType uriType = text.DetectUri();
@@ -126,36 +64,51 @@ public static void Navigate(string site, MainWindow mainWindow)
12664
Uri uri = new(site);
12765
if ((mainWindow.TabView.SelectedItem != null) && (mainWindow.SelectedItem is WebViewPage webviewPage))
12866
{
129-
webviewPage.webView2.Source = uri;
67+
webviewPage.WebView2.Source = uri;
13068
}
13169
else
13270
{
13371
mainWindow.AddNewTab(new WebViewPage(uri));
13472
}
13573
}
13674

137-
private void SearchBox_DragOver(object sender, DragEventArgs e)
75+
private void AutoSuggestBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
13876
{
139-
if (e.DataView.Contains(StandardDataFormats.StorageItems))
77+
if (args.Reason == AutoSuggestionBoxTextChangeReason.UserInput)
14078
{
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+
}
14285
}
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)
14495
{
145-
e.AcceptedOperation = DataPackageOperation.None;
96+
sender.Text = item;
97+
return;
14698
}
99+
100+
string prefix = text[..lastIndex];
101+
sender.Text = $"{prefix} {item}";
147102
}
148103

149-
private async void SearchBox_Drop(object sender, DragEventArgs e)
104+
private void AutoSuggestBox_QuerySubmitted(AutoSuggestBox sender, AutoSuggestBoxQuerySubmittedEventArgs args)
150105
{
151-
if (e.DataView.Contains(StandardDataFormats.StorageItems))
106+
if (args.ChosenSuggestion == null)
152107
{
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);
159112
}
160113
}
161114
}

MainWindow.xaml

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,12 @@
138138
<KeyboardAccelerator Key="T" Modifiers="Control,Shift"/>
139139
</MenuFlyoutItem.KeyboardAccelerators>
140140
</MenuFlyoutItem>
141+
142+
<MenuFlyoutItem Text="将所有标签页添加到收藏夹">
143+
<MenuFlyoutItem.KeyboardAccelerators>
144+
<KeyboardAccelerator Key="D" Modifiers="Control,Shift"/>
145+
</MenuFlyoutItem.KeyboardAccelerators>
146+
</MenuFlyoutItem>
141147
</MenuFlyout>
142148

143149
<MenuFlyout
@@ -148,7 +154,6 @@
148154
Click="OnRestoreClicked"
149155
IsEnabled="{x:Bind IsWindowMaximized, Mode=OneWay}"
150156
Text="还原">
151-
152157
<MenuFlyoutItem.Icon>
153158
<FontIcon Glyph="&#xE923;" />
154159
</MenuFlyoutItem.Icon>
@@ -178,18 +183,40 @@
178183
Click="OnMaximizeClicked"
179184
IsEnabled="{x:Bind IsWindowMaximized.Equals(x:False), Mode=OneWay}"
180185
Text="最大化">
181-
182186
<MenuFlyoutItem.Icon>
183187
<FontIcon Glyph="&#xE922;" />
184188
</MenuFlyoutItem.Icon>
185189
</MenuFlyoutItem>
186190

187-
<MenuFlyoutSeparator Width="200" />
191+
<MenuFlyoutSeparator />
192+
193+
<MenuFlyoutItem
194+
Text="新建标签页">
195+
<MenuFlyoutItem.KeyboardAccelerators>
196+
<KeyboardAccelerator Key="T" Modifiers="Control" />
197+
</MenuFlyoutItem.KeyboardAccelerators>
198+
</MenuFlyoutItem>
199+
200+
<MenuFlyoutItem
201+
Click="OpenClosedTab"
202+
Text="重新打开关闭的标签页">
203+
<MenuFlyoutItem.KeyboardAccelerators>
204+
<KeyboardAccelerator Key="T" Modifiers="Control,Shift" />
205+
</MenuFlyoutItem.KeyboardAccelerators>
206+
</MenuFlyoutItem>
207+
208+
<MenuFlyoutItem
209+
Text="将打开的页面添加到收藏夹">
210+
<MenuFlyoutItem.KeyboardAccelerators>
211+
<KeyboardAccelerator Key="D" Modifiers="Control,Shift" />
212+
</MenuFlyoutItem.KeyboardAccelerators>
213+
</MenuFlyoutItem>
214+
215+
<MenuFlyoutSeparator />
188216

189217
<MenuFlyoutItem
190218
Click="OnCloseClicked"
191219
Text="关闭">
192-
193220
<MenuFlyoutItem.Icon>
194221
<FontIcon Glyph="&#xE8BB;" />
195222
</MenuFlyoutItem.Icon>

MainWindow.xaml.cs

Lines changed: 3 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,11 @@ public MainWindow()
4747
this.SetThemeColor();
4848

4949
overlappedPresenter = AppWindow.Presenter as OverlappedPresenter;
50-
5150
IsWindowMaximized = overlappedPresenter.State == OverlappedPresenterState.Maximized;
5251

53-
HWND hWND = (HWND)this.GetWindowHandle();
54-
55-
// 挂载相应的事件
5652
AppWindow.Changed += OnAppWindowChanged;
5753
mainWindowSubClassProc = new SUBCLASSPROC(MainWindowSubClassProc);
58-
PInvoke.SetWindowSubclass(hWND, mainWindowSubClassProc, 0, 0);
54+
PInvoke.SetWindowSubclass(new(this.GetWindowHandle()), mainWindowSubClassProc, 0, 0);
5955
}
6056

6157
public void AddHomePage()
@@ -123,25 +119,15 @@ private void OnRestoreClicked(object sender, RoutedEventArgs args)
123119
/// </summary>
124120
private void OnMoveClicked(object sender, RoutedEventArgs args)
125121
{
126-
MenuFlyoutItem menuItem = sender as MenuFlyoutItem;
127-
if (menuItem.Tag is not null)
128-
{
129-
((MenuFlyout)menuItem.Tag).Hide();
130-
PInvoke.SendMessage((HWND)this.GetWindowHandle(), PInvoke.WM_SYSCOMMAND, 0xF010, 0);
131-
}
122+
PInvoke.SendMessage((HWND)this.GetWindowHandle(), PInvoke.WM_SYSCOMMAND, 0xF010, 0);
132123
}
133124

134125
/// <summary>
135126
/// 窗口大小
136127
/// </summary>
137128
private void OnSizeClicked(object sender, RoutedEventArgs args)
138129
{
139-
MenuFlyoutItem menuItem = sender as MenuFlyoutItem;
140-
if (menuItem.Tag is not null)
141-
{
142-
((MenuFlyout)menuItem.Tag).Hide();
143-
PInvoke.SendMessage((HWND)this.GetWindowHandle(), PInvoke.WM_SYSCOMMAND, 0xF000, 0);
144-
}
130+
PInvoke.SendMessage((HWND)this.GetWindowHandle(), PInvoke.WM_SYSCOMMAND, 0xF000, 0);
145131
}
146132

147133
/// <summary>
@@ -175,43 +161,6 @@ private LRESULT MainWindowSubClassProc(HWND hWnd, uint Msg, WPARAM wParam, LPARA
175161
{
176162
switch (Msg)
177163
{
178-
// 选择窗口右键菜单的条目时接收到的消息
179-
case PInvoke.WM_SYSCOMMAND:
180-
{
181-
nuint sysCommand = wParam.Value & 0xFFF0;
182-
183-
if (sysCommand == PInvoke.SC_MOUSEMENU)
184-
{
185-
FlyoutShowOptions options = new()
186-
{
187-
Position = new Point(0, 15),
188-
ShowMode = FlyoutShowMode.Standard,
189-
};
190-
TitlebarMenuFlyout.ShowAt(null, options);
191-
return (LRESULT)0;
192-
}
193-
else if (sysCommand == PInvoke.SC_KEYMENU)
194-
{
195-
FlyoutShowOptions options = new()
196-
{
197-
Position = new Point(0, 45),
198-
ShowMode = FlyoutShowMode.Standard,
199-
};
200-
TitlebarMenuFlyout.ShowAt(null, options);
201-
return (LRESULT)0;
202-
}
203-
break;
204-
}
205-
case PInvoke.WM_NCLBUTTONDOWN:
206-
{
207-
if (TitlebarMenuFlyout.IsOpen)
208-
{
209-
TitlebarMenuFlyout.Hide();
210-
}
211-
break;
212-
}
213-
214-
// 当用户按下鼠标右键并释放时,光标位于窗口的非工作区内的消息
215164
case PInvoke.WM_NCRBUTTONUP:
216165
{
217166
if (Content is not null && Content.XamlRoot is not null)

NativeMethods.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,4 @@ IFileSaveDialog
1111
SHCreateItemFromParsingName
1212

1313
WM_SYSCOMMAND
14-
WM_NCLBUTTONDOWN
15-
WM_NCRBUTTONUP
16-
SC_MOUSEMENU
17-
SC_KEYMENU
14+
WM_NCRBUTTONUP

Pages/WebViewPage.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ public void ShowFlyout(string name)
318318
toolBar.ShowFlyout(name);
319319
}
320320

321-
public WebView2 webView2 => EdgeWebViewEngine;
321+
public WebView2 WebView2 => EdgeWebViewEngine;
322322

323323
private void FavoriteStateChanged(object sender, RoutedEventArgs e)
324324
{

0 commit comments

Comments
 (0)