Skip to content

Commit 32f6fe7

Browse files
committed
代码优化
1 parent df90330 commit 32f6fe7

File tree

12 files changed

+225
-97
lines changed

12 files changed

+225
-97
lines changed

Controls/BrowserMenuItems.xaml

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@
77
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
88
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
99
mc:Ignorable="d">
10-
<Button ToolTipService.ToolTip="设置及其他 (Alt+F)" Style="{StaticResource AlternateCloseButtonStyle}">
11-
<Button.KeyboardAccelerators>
12-
<KeyboardAccelerator Key="F" Modifiers="Menu"/>
13-
</Button.KeyboardAccelerators>
14-
<FontIcon Glyph="&#xe712;" />
15-
<Button.Flyout>
10+
<local:IconButton
11+
TipText="设置及其他 (Alt+F)"
12+
IconGlyph="&#xe712;"
13+
Key="F" Modifiers="Menu">
14+
<local:IconButton.Flyout>
1615
<MenuFlyout Opening="MenuFlyout_Opening" Placement="BottomEdgeAlignedRight">
1716
<MenuFlyoutItem Text="新建标签页" Click="TryCreateNewTab">
1817
<MenuFlyoutItem.KeyboardAccelerators>
@@ -36,6 +35,44 @@
3635

3736
<MenuFlyoutSeparator />
3837

38+
<MenuFlyoutItem>
39+
<MenuFlyoutItem.Template>
40+
<ControlTemplate>
41+
<Grid>
42+
<Grid.ColumnDefinitions>
43+
<ColumnDefinition Width="*"/>
44+
<ColumnDefinition Width="Auto"/>
45+
</Grid.ColumnDefinitions>
46+
47+
<TextBlock Text="缩放" Margin="48,0,0,0" VerticalAlignment="Center"/>
48+
49+
<StackPanel Orientation="Horizontal" Grid.Column="1" HorizontalAlignment="Right">
50+
<local:IconButton
51+
TipText="缩小 (Ctrl+减号键)"
52+
IconGlyph="&#xe949;"
53+
Key="Subtract" Modifiers="Control" />
54+
55+
<TextBlock x:Name="zoomBlock" Text="100%" VerticalAlignment="Center"/>
56+
57+
<local:IconButton
58+
TipText="放大 (Ctrl+加号键)"
59+
IconGlyph="&#xe948;"
60+
Key="Add" Modifiers="Control" />
61+
62+
<Border Grid.Column="4" Background="LightGray" Width="1" Margin="0,5,0,5"/>
63+
64+
<local:IconButton
65+
TipText="进入全屏"
66+
IconGlyph="&#xe740;"
67+
Key="F11" Modifiers="None" />
68+
</StackPanel>
69+
</Grid>
70+
</ControlTemplate>
71+
</MenuFlyoutItem.Template>
72+
</MenuFlyoutItem>
73+
74+
<MenuFlyoutSeparator />
75+
3976
<MenuFlyoutItem Text="收藏夹" Click="ShowFlyout">
4077
<MenuFlyoutItem.KeyboardAccelerators>
4178
<KeyboardAccelerator Key="O" Modifiers="Control,Shift"/>
@@ -72,7 +109,7 @@
72109
<MenuFlyoutItem.KeyboardAccelerators>
73110
<KeyboardAccelerator Key="P" Modifiers="Control"/>
74111
</MenuFlyoutItem.KeyboardAccelerators>
75-
112+
76113
<MenuFlyoutItem.Icon>
77114
<FontIcon Glyph="&#xe749;"/>
78115
</MenuFlyoutItem.Icon>
@@ -104,7 +141,7 @@
104141

105142
<MenuFlyoutItem Text="关闭此软件" Click="CloseApp"/>
106143
</MenuFlyout>
107-
</Button.Flyout>
108-
</Button>
144+
</local:IconButton.Flyout>
145+
</local:IconButton>
109146

110147
</Page>

Controls/BrowserMenuItems.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ private void CloseApp(object sender, RoutedEventArgs e)
5959

6060
private void ScreenClip(object sender, RoutedEventArgs e)
6161
{
62-
WebView2 webView2 = (App.GetWindowForElement(this).SelectedItem as WebViewPage).webView2;
62+
WebView2 webView2 = App.GetWebView2(this);
6363
webView2.Focus(FocusState.Programmatic);
6464

6565
List<INPUT> inputs =

Controls/IconButton.cs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using Microsoft.UI.Xaml;
2+
using Microsoft.UI.Xaml.Controls;
3+
using Windows.System;
4+
5+
namespace Edge
6+
{
7+
public sealed partial class IconButton : Button
8+
{
9+
public event RoutedEventHandler ButtonClicked;
10+
11+
public IconButton()
12+
{
13+
this.DefaultStyleKey = typeof(IconButton);
14+
}
15+
16+
protected override void OnApplyTemplate()
17+
{
18+
base.OnApplyTemplate();
19+
Button IconButton = GetTemplateChild("IconButton") as Button;
20+
IconButton.Click += IconButton_Click;
21+
}
22+
23+
private void IconButton_Click(object sender, RoutedEventArgs e)
24+
{
25+
ButtonClicked?.Invoke(sender, e);
26+
}
27+
28+
public static readonly DependencyProperty TipTextProperty = DependencyProperty.Register(
29+
nameof(TipText),
30+
typeof(string),
31+
typeof(IconButton),
32+
new PropertyMetadata(null));
33+
34+
public string TipText
35+
{
36+
get => (string)GetValue(TipTextProperty);
37+
set => SetValue(TipTextProperty, value);
38+
}
39+
40+
public static readonly DependencyProperty IconGlyphProperty = DependencyProperty.Register(
41+
nameof(IconGlyph),
42+
typeof(string),
43+
typeof(IconButton),
44+
new PropertyMetadata(null));
45+
46+
public string IconGlyph
47+
{
48+
get => (string)GetValue(IconGlyphProperty);
49+
set => SetValue(IconGlyphProperty, value);
50+
}
51+
52+
public static readonly DependencyProperty KeyProperty = DependencyProperty.Register(
53+
nameof(Key),
54+
typeof(VirtualKey),
55+
typeof(IconButton),
56+
new PropertyMetadata(null));
57+
58+
public VirtualKey Key
59+
{
60+
get => (VirtualKey)GetValue(KeyProperty);
61+
set => SetValue(KeyProperty, value);
62+
}
63+
64+
public static readonly DependencyProperty ModifiersProperty = DependencyProperty.Register(
65+
nameof(Modifiers),
66+
typeof(VirtualKeyModifiers),
67+
typeof(IconButton),
68+
new PropertyMetadata(null));
69+
70+
public VirtualKeyModifiers Modifiers
71+
{
72+
get => (VirtualKeyModifiers)GetValue(ModifiersProperty);
73+
set => SetValue(ModifiersProperty, value);
74+
}
75+
}
76+
}

Controls/ToolBar.xaml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
<local:ToolButton
1313
x:Name="FavoriteButton"
1414
Title="收藏夹" PlaceholderText="搜索收藏夹"
15-
ButtonTip="收藏夹 (Ctrl+Shift+O)" FontIconGlyph="&#xe728;">
15+
TipText="收藏夹 (Ctrl+Shift+O)" IconGlyph="&#xe728;">
1616
<local:FavoriteList x:Name="FavoriteList"/>
1717
</local:ToolButton>
1818

1919
<local:ToolButton
2020
x:Name="HistoryButton"
2121
Title="历史记录" PlaceholderText="搜索历史记录"
22-
ButtonTip="历史记录 (Ctrl+H)" FontIconGlyph="&#xe81c;"
22+
TipText="历史记录 (Ctrl+H)" IconGlyph="&#xe81c;"
2323
TextChanged="SearchHistory">
2424
<local:ToolButton.KeyboardAccelerators>
2525
<KeyboardAccelerator Key="H" Modifiers="Control"/>
@@ -66,7 +66,7 @@
6666
<local:ToolButton
6767
x:Name="DownloadButton"
6868
Title="下载" PlaceholderText="搜索下载文件"
69-
ButtonTip="下载 (Ctrl+J)" FontIconGlyph="&#xe896;"
69+
TipText="下载 (Ctrl+J)" IconGlyph="&#xe896;"
7070
TextChanged="SearchDownload">
7171
<local:ToolButton.KeyboardAccelerators>
7272
<KeyboardAccelerator Key="J" Modifiers="Control"/>
@@ -88,11 +88,11 @@
8888
<TextBlock Text="{x:Bind Information, Mode=TwoWay}" TextTrimming="CharacterEllipsis"/>
8989
</StackPanel>
9090

91-
<Button Click="RemoveDownloadItem" Grid.Column="1" HorizontalAlignment="Right">
92-
<Button.Content>
93-
<FontIcon Glyph="&#xe711;"/>
94-
</Button.Content>
95-
</Button>
91+
<local:IconButton
92+
TipText="删除"
93+
IconGlyph="&#xe711;"
94+
Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Center"
95+
ButtonClicked="RemoveDownloadItem" />
9696
</Grid>
9797
</DataTemplate>
9898
</ListView.ItemTemplate>

Controls/ToolButton.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,28 @@ private void SearchTextBox_TextChanged(object sender, TextChangedEventArgs e)
3131
TextChanged?.Invoke(sender, e);
3232
}
3333

34-
public static readonly DependencyProperty FontIconGlyphProperty = DependencyProperty.Register(
35-
nameof(FontIconGlyph),
34+
public static readonly DependencyProperty IconGlyphProperty = DependencyProperty.Register(
35+
nameof(IconGlyph),
3636
typeof(string),
3737
typeof(ToolButton),
3838
new PropertyMetadata(null));
3939

40-
public string FontIconGlyph
40+
public string IconGlyph
4141
{
42-
get => (string)GetValue(FontIconGlyphProperty);
43-
set => SetValue(FontIconGlyphProperty, value);
42+
get => (string)GetValue(IconGlyphProperty);
43+
set => SetValue(IconGlyphProperty, value);
4444
}
4545

46-
public static readonly DependencyProperty ButtonTipProperty = DependencyProperty.Register(
47-
nameof(ButtonTip),
46+
public static readonly DependencyProperty TipTextProperty = DependencyProperty.Register(
47+
nameof(TipText),
4848
typeof(string),
4949
typeof(ToolButton),
5050
new PropertyMetadata(null));
5151

52-
public string ButtonTip
52+
public string TipText
5353
{
54-
get => (string)GetValue(ButtonTipProperty);
55-
set => SetValue(ButtonTipProperty, value);
54+
get => (string)GetValue(TipTextProperty);
55+
set => SetValue(TipTextProperty, value);
5656
}
5757

5858
public static readonly DependencyProperty TitleProperty = DependencyProperty.Register(

MainWindow.xaml

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,15 @@
3131

3232
<TextBlock Text="Edge 工作区" VerticalAlignment="Center" FontWeight="Bold"/>
3333

34-
<Button
35-
Style="{StaticResource AlternateCloseButtonStyle}"
34+
<local:IconButton
35+
TipText="创建新工作区"
36+
IconGlyph="&#xecc8;"
3637
HorizontalAlignment="Right"
37-
ToolTipService.ToolTip="创建新工作区" Grid.Column="1">
38-
<FontIcon Glyph="&#xecc8;"/>
39-
</Button>
40-
41-
<Button
42-
Style="{StaticResource AlternateCloseButtonStyle}"
43-
ToolTipService.ToolTip="创建新工作区" Grid.Column="2">
44-
<FontIcon Glyph="&#xe712;" />
45-
</Button>
38+
Grid.Column="1" />
39+
<local:IconButton
40+
TipText="更多选项"
41+
IconGlyph="&#xe712;"
42+
Grid.Column="2" />
4643
</Grid>
4744

4845
<TextBlock TextWrapping="WrapWholeWords">

MainWindow.xaml.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,17 @@ private LRESULT MainWindowSubClassProc(HWND hWnd, uint Msg, WPARAM wParam, LPARA
216216
{
217217
if (Content is not null && Content.XamlRoot is not null)
218218
{
219-
System.Drawing.Point point = new((short)(lParam.Value & 0xFFFF), (short)((lParam.Value >> 16) & 0xFFFF));
219+
int x = (short)(lParam.Value & 0xFFFF);
220+
int y = (short)((lParam.Value >> 16) & 0xFFFF);
221+
System.Drawing.Point point = new(x, y);
220222
PInvoke.ScreenToClient(hWnd, ref point);
221223

224+
uint dpi = PInvoke.GetDpiForWindow(hWnd);
225+
float scalingFactor = dpi / 96.0f;
226+
227+
point.X = (int)(point.X / scalingFactor);
228+
point.Y = (int)(point.Y / scalingFactor);
229+
222230
FlyoutShowOptions options = new()
223231
{
224232
ShowMode = FlyoutShowMode.Standard,

NativeMethods.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ SendMessage
33
DefSubclassProc
44
SendInput
55
ScreenToClient
6+
GetDpiForWindow
67

78
CoCreateInstance
89
FileSaveDialog

Pages/ImageViewer.xaml

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,21 @@
2929
</StackPanel>
3030
</Button>
3131

32-
<Button Click="ImageRotateRequest" Style="{StaticResource AlternateCloseButtonStyle}">
33-
<FontIcon Glyph="&#xe7ad;"/>
34-
</Button>
35-
36-
<Button Click="ImageDeleteRequest" Style="{StaticResource AlternateCloseButtonStyle}">
37-
<FontIcon Glyph="&#xe74d;"/>
38-
</Button>
39-
40-
<Button Click="PrintImageRequest" Style="{StaticResource AlternateCloseButtonStyle}">
41-
<FontIcon Glyph="&#xe749;"/>
42-
</Button>
32+
<local:IconButton
33+
TipText="旋转 (Ctrl+R)"
34+
IconGlyph="&#xe7ad;"
35+
Key="R" Modifiers="Control"
36+
ButtonClicked="ImageRotateRequest" />
37+
<local:IconButton
38+
TipText="删除 (Del)"
39+
IconGlyph="&#xe74d;"
40+
Key="Delete" Modifiers="None"
41+
ButtonClicked="ImageDeleteRequest" />
42+
<local:IconButton
43+
TipText="打印 (Ctrl+P)"
44+
IconGlyph="&#xe749;"
45+
Key="P" Modifiers="Control"
46+
ButtonClicked="PrintImageRequest" />
4347

4448
</StackPanel>
4549

0 commit comments

Comments
 (0)