Skip to content

Commit 18b5ba0

Browse files
committed
refactor(SignaturePage): Simplify the logic of the signature page and remove unnecessary functions
Remove the signature preview window and related image processing code Optimize page layout and default stroke size Add a new "Clear" string resource
1 parent 3043022 commit 18b5ba0

8 files changed

Lines changed: 74 additions & 202 deletions

File tree

MainWindow.xaml.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,6 @@ private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs
210210

211211
private void MinimizeToTray()
212212
{
213-
_notifyIcon!.ShowBalloonTip(2000, Strings.Toolbox, Strings.MinimizedToTray, ToolTipIcon.None);
214213
Hide();
215214
}
216215

Pages/HomePage.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
xmlns:local="clr-namespace:DevTools.Pages"
55
xmlns:resources="clr-namespace:DevTools.Resources"
66
Title="HomePage">
7-
<Grid Margin="20">
8-
<UniformGrid Rows="2" Columns="3" HorizontalAlignment="Center" VerticalAlignment="Center">
7+
<Grid>
8+
<UniformGrid Rows="2" Columns="3" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,0,0,150">
99
<Button x:Name="BtnMd5" Click="BtnMd5_Click" Style="{StaticResource CardButtonStyle}" Margin="8">
1010
<StackPanel Width="140" HorizontalAlignment="Center" VerticalAlignment="Center">
1111
<TextBlock FontFamily="{StaticResource FontAwesomeSolid}" Text="&#xf023;" FontSize="32" FontWeight="Bold" HorizontalAlignment="Center" TextAlignment="Center" Foreground="#FFFFFF" />

Pages/SignaturePage.xaml

Lines changed: 30 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -17,64 +17,45 @@
1717

1818
<TextBlock Text="{x:Static resources:Strings.PageSignature}" FontSize="18" Margin="0,10" />
1919

20-
<StackPanel Orientation="Horizontal" Margin="0,10">
21-
<TextBlock Text="{x:Static resources:Strings.PenSize}" FontWeight="Bold" VerticalAlignment="Center" Margin="0,0,10,0" />
22-
<Slider x:Name="PenSizeSlider"
23-
Width="200"
24-
Minimum="1"
25-
Maximum="10"
26-
Value="2"
27-
VerticalAlignment="Center" />
28-
<TextBlock x:Name="PenSizeText" Text="2" FontWeight="Bold" VerticalAlignment="Center" Margin="10,0,0,0" />
29-
</StackPanel>
30-
3120
<Border BorderBrush="#666" BorderThickness="1" CornerRadius="4" Margin="0,10">
3221
<InkCanvas x:Name="SignatureCanvas"
3322
Height="300"
3423
Background="#FFFFFF">
3524
<InkCanvas.DefaultDrawingAttributes>
36-
<DrawingAttributes x:Name="DefaultPenAttributes" Color="#000000" Width="2" />
25+
<DrawingAttributes x:Name="DefaultPenAttributes" Color="#000000" Width="7" Height="7" FitToCurve="True" />
3726
</InkCanvas.DefaultDrawingAttributes>
3827
</InkCanvas>
3928
</Border>
4029

41-
<StackPanel Orientation="Horizontal" Margin="0,10" HorizontalAlignment="Center">
42-
<Button Click="Clear_Click" Width="40" Height="32" Margin="5,0" ToolTip="{x:Static resources:Strings.ClearAll}">
43-
<TextBlock FontFamily="{StaticResource FontAwesomeSolid}" Text="&#xf2ed;" FontSize="16" FontWeight="Bold" Foreground="#FFFFFF" />
44-
</Button>
45-
<Button Click="Generate_Click" Width="40" Height="32" Margin="5,0" ToolTip="{x:Static resources:Strings.Generate}">
46-
<TextBlock FontFamily="{StaticResource FontAwesomeSolid}" Text="&#xf02a;" FontSize="16" FontWeight="Bold" Foreground="#FFFFFF" />
47-
</Button>
48-
<Button x:Name="SaveImageButton"
49-
Click="SaveImage_Click"
50-
Width="40"
51-
Height="32"
52-
Margin="5,0"
53-
Visibility="Collapsed"
54-
ToolTip="{x:Static resources:Strings.SaveImage}">
55-
<TextBlock FontFamily="{StaticResource FontAwesomeSolid}" Text="&#xf0c7;" FontSize="16" FontWeight="Bold" Foreground="#FFFFFF" />
56-
</Button>
57-
</StackPanel>
58-
59-
<!-- Image preview -->
60-
<Border x:Name="ImageBorder"
61-
CornerRadius="8"
62-
BorderBrush="#4A4A4A"
63-
BorderThickness="1"
64-
Cursor="Hand"
65-
MouseLeftButtonUp="ImageBorder_MouseLeftButtonUp"
66-
ToolTip="{x:Static resources:Strings.ClickToPreview}"
67-
Visibility="Collapsed"
68-
Margin="0,10">
69-
<Image x:Name="ResultImage" Height="200" Stretch="Uniform" />
70-
</Border>
71-
72-
<StackPanel Orientation="Horizontal" Margin="0,8">
73-
<TextBlock Text="{x:Static resources:Strings.ImageSize}" FontWeight="Bold" />
74-
<TextBlock x:Name="ImageDimensionsText" Text="-" />
75-
<TextBlock Text="{x:Static resources:Strings.Size}" FontWeight="Bold" Margin="12,0,0,0" />
76-
<TextBlock x:Name="ImageSizeText" Text="-" />
77-
</StackPanel>
30+
<Grid Margin="0,10">
31+
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
32+
<TextBlock Text="{x:Static resources:Strings.PenSize}" FontWeight="Bold" VerticalAlignment="Center" Margin="0,0,10,0" />
33+
<Slider x:Name="PenSizeSlider"
34+
Width="100"
35+
Minimum="1"
36+
Maximum="10"
37+
Value="7"
38+
VerticalAlignment="Center" />
39+
<TextBlock x:Name="PenSizeText" Text="7" FontWeight="Bold" VerticalAlignment="Center" Margin="10,0,0,0" />
40+
</StackPanel>
41+
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
42+
<Button Click="Convert_Click" Width="40" Height="32" Margin="5,0" ToolTip="{x:Static resources:Strings.Convert}">
43+
<TextBlock FontFamily="{StaticResource FontAwesomeSolid}" Text="&#xf362;" FontSize="16" FontWeight="Bold" Foreground="#FFFFFF" />
44+
</Button>
45+
<Button Click="Clear_Click" Width="40" Height="32" Margin="5,0" ToolTip="{x:Static resources:Strings.ClearAll}">
46+
<TextBlock FontFamily="{StaticResource FontAwesomeSolid}" Text="&#xf2ed;" FontSize="16" FontWeight="Bold" Foreground="#FFFFFF" />
47+
</Button>
48+
<Button x:Name="SaveImageButton"
49+
Click="SaveImage_Click"
50+
Width="40"
51+
Height="32"
52+
Margin="5,0"
53+
Visibility="Collapsed"
54+
ToolTip="{x:Static resources:Strings.SaveImage}">
55+
<TextBlock FontFamily="{StaticResource FontAwesomeSolid}" Text="&#xf0c7;" FontSize="16" FontWeight="Bold" Foreground="#FFFFFF" />
56+
</Button>
57+
</StackPanel>
58+
</Grid>
7859

7960
<StackPanel Orientation="Horizontal" Margin="0,6">
8061
<TextBlock Text="{x:Static resources:Strings.Base64}" FontWeight="Bold" VerticalAlignment="Center" />

Pages/SignaturePage.xaml.cs

Lines changed: 5 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,18 @@
33
using System.Windows;
44
using System.Windows.Controls;
55
using System.Windows.Ink;
6-
using System.Windows.Input;
76
using System.Windows.Media;
87
using System.Windows.Media.Imaging;
9-
using System.Windows.Media.Effects;
108
using Microsoft.Win32;
119
using DevTools.Helpers;
1210
using DevTools.Resources;
13-
using Button = System.Windows.Controls.Button;
1411
using MessageBox = System.Windows.MessageBox;
1512
using SaveFileDialog = Microsoft.Win32.SaveFileDialog;
16-
using Cursors = System.Windows.Input.Cursors;
17-
using Orientation = System.Windows.Controls.Orientation;
18-
using HorizontalAlignment = System.Windows.HorizontalAlignment;
19-
using Image = System.Windows.Controls.Image;
20-
using Color = System.Windows.Media.Color;
21-
using Brushes = System.Windows.Media.Brushes;
22-
using Application = System.Windows.Application;
23-
using FontFamily = System.Windows.Media.FontFamily;
24-
using Point = System.Windows.Point;
2513

2614
namespace DevTools.Pages
2715
{
2816
public partial class SignaturePage : Page
2917
{
30-
private byte[]? _currentImageBytes;
3118
private BitmapSource? _currentImage;
3219

3320
public SignaturePage()
@@ -45,27 +32,18 @@ private void PenSizeSlider_ValueChanged(object sender, RoutedPropertyChangedEven
4532
{
4633
PenSizeText.Text = e.NewValue.ToString("F0");
4734
DefaultPenAttributes.Width = e.NewValue;
35+
DefaultPenAttributes.Height = e.NewValue;
4836
}
4937

5038
private void Clear_Click(object sender, RoutedEventArgs e)
5139
{
5240
SignatureCanvas.Strokes.Clear();
53-
ClearResult();
54-
}
55-
56-
private void ClearResult()
57-
{
5841
Base64Text.Text = string.Empty;
59-
ImageDimensionsText.Text = "-";
60-
ImageSizeText.Text = "-";
61-
_currentImageBytes = null;
6242
_currentImage = null;
63-
ResultImage.Source = null;
6443
SaveImageButton.Visibility = Visibility.Collapsed;
65-
ImageBorder.Visibility = Visibility.Collapsed;
6644
}
6745

68-
private void Generate_Click(object sender, RoutedEventArgs e)
46+
private void Convert_Click(object sender, RoutedEventArgs e)
6947
{
7048
if (SignatureCanvas.Strokes.Count == 0)
7149
{
@@ -99,17 +77,10 @@ private void Generate_Click(object sender, RoutedEventArgs e)
9977

10078
using var stream = new MemoryStream();
10179
encoder.Save(stream);
102-
_currentImageBytes = stream.ToArray();
80+
var imageBytes = stream.ToArray();
10381

104-
var base64 = Convert.ToBase64String(_currentImageBytes);
105-
Base64Text.Text = base64;
106-
107-
ImageDimensionsText.Text = $"{bitmap.PixelWidth} x {bitmap.PixelHeight}";
108-
ImageSizeText.Text = FormatBytes(_currentImageBytes.Length);
109-
110-
ResultImage.Source = _currentImage;
82+
Base64Text.Text = Convert.ToBase64String(imageBytes);
11183
SaveImageButton.Visibility = Visibility.Visible;
112-
ImageBorder.Visibility = Visibility.Visible;
11384
}
11485
catch (Exception ex)
11586
{
@@ -136,109 +107,6 @@ private void SaveImage_Click(object sender, RoutedEventArgs e)
136107
return;
137108
}
138109

139-
SaveImage(_currentImage);
140-
}
141-
142-
private void ImageBorder_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
143-
{
144-
if (_currentImage == null)
145-
{
146-
MessageBox.Show(Strings.NoImageToPreview, Strings.Info, MessageBoxButton.OK, MessageBoxImage.Information);
147-
return;
148-
}
149-
150-
var previewWindow = new Window
151-
{
152-
Title = $"{Strings.ImagePreview} - {_currentImage.PixelWidth} x {_currentImage.PixelHeight}",
153-
Width = Math.Min(_currentImage.PixelWidth + 40, SystemParameters.WorkArea.Width * 0.9),
154-
Height = Math.Min(_currentImage.PixelHeight + 100, SystemParameters.WorkArea.Height * 0.9),
155-
WindowStartupLocation = WindowStartupLocation.CenterScreen,
156-
Background = new SolidColorBrush(Color.FromRgb(18, 18, 18))
157-
};
158-
159-
var grid = new Grid();
160-
grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
161-
grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
162-
163-
var toolbar = new StackPanel
164-
{
165-
Orientation = Orientation.Horizontal,
166-
HorizontalAlignment = HorizontalAlignment.Right,
167-
Margin = new Thickness(10)
168-
};
169-
170-
var saveButton = CreateIconButton();
171-
saveButton.Click += (s, args) => SaveImage(_currentImage);
172-
toolbar.Children.Add(saveButton);
173-
174-
grid.Children.Add(toolbar);
175-
Grid.SetRow(toolbar, 0);
176-
177-
var image = new Image
178-
{
179-
Source = _currentImage,
180-
Stretch = Stretch.Uniform,
181-
Margin = new Thickness(10)
182-
};
183-
184-
grid.Children.Add(image);
185-
Grid.SetRow(image, 1);
186-
187-
previewWindow.Content = grid;
188-
previewWindow.ShowDialog();
189-
}
190-
191-
private Button CreateIconButton()
192-
{
193-
var textBlock = new TextBlock
194-
{
195-
FontFamily = (FontFamily)Application.Current.FindResource("FontAwesomeSolid"),
196-
Text = "\uf0c7",
197-
FontSize = 18,
198-
Foreground = Brushes.White,
199-
RenderTransformOrigin = new Point(0.5, 0.5)
200-
};
201-
202-
var button = new Button
203-
{
204-
Content = textBlock,
205-
Width = 40,
206-
Height = 40,
207-
Cursor = System.Windows.Input.Cursors.Hand,
208-
Background = new SolidColorBrush(Color.FromRgb(74, 74, 74)),
209-
BorderThickness = new Thickness(0),
210-
ToolTip = Strings.SaveImage
211-
};
212-
213-
var template = new ControlTemplate(typeof(Button));
214-
var borderFactory = new FrameworkElementFactory(typeof(Border));
215-
borderFactory.Name = "border";
216-
borderFactory.SetValue(Border.BackgroundProperty, new TemplateBindingExtension(Button.BackgroundProperty));
217-
borderFactory.SetValue(Border.CornerRadiusProperty, new CornerRadius(20));
218-
borderFactory.SetValue(Border.SnapsToDevicePixelsProperty, true);
219-
220-
var contentFactory = new FrameworkElementFactory(typeof(ContentPresenter));
221-
contentFactory.SetValue(ContentPresenter.HorizontalAlignmentProperty, HorizontalAlignment.Center);
222-
contentFactory.SetValue(ContentPresenter.VerticalAlignmentProperty, VerticalAlignment.Center);
223-
224-
borderFactory.AppendChild(contentFactory);
225-
template.VisualTree = borderFactory;
226-
227-
var hoverTrigger = new Trigger { Property = Button.IsMouseOverProperty, Value = true };
228-
hoverTrigger.Setters.Add(new Setter(Border.BackgroundProperty, new SolidColorBrush(Color.FromRgb(90, 90, 90)), "border"));
229-
hoverTrigger.Setters.Add(new Setter(Border.EffectProperty, new DropShadowEffect { Color = Color.FromArgb(102, 0, 0, 0), BlurRadius = 12, ShadowDepth = 2 }, "border"));
230-
template.Triggers.Add(hoverTrigger);
231-
232-
var pressedTrigger = new Trigger { Property = Button.IsPressedProperty, Value = true };
233-
pressedTrigger.Setters.Add(new Setter(Border.BackgroundProperty, new SolidColorBrush(Color.FromRgb(106, 106, 106)), "border"));
234-
template.Triggers.Add(pressedTrigger);
235-
236-
button.Template = template;
237-
return button;
238-
}
239-
240-
private void SaveImage(BitmapSource image)
241-
{
242110
var dialog = new SaveFileDialog
243111
{
244112
Filter = "PNG Image|*.png|JPEG Image|*.jpg|Bitmap Image|*.bmp|GIF Image|*.gif|All Files|*.*",
@@ -258,7 +126,7 @@ private void SaveImage(BitmapSource image)
258126
_ => new PngBitmapEncoder()
259127
};
260128

261-
encoder.Frames.Add(BitmapFrame.Create(image));
129+
encoder.Frames.Add(BitmapFrame.Create(_currentImage));
262130

263131
using var stream = new FileStream(dialog.FileName, FileMode.Create);
264132
encoder.Save(stream);
@@ -271,18 +139,5 @@ private void SaveImage(BitmapSource image)
271139
}
272140
}
273141
}
274-
275-
private string FormatBytes(long bytes)
276-
{
277-
string[] sizes = { "B", "KB", "MB", "GB" };
278-
double len = bytes;
279-
int order = 0;
280-
while (len >= 1024 && order < sizes.Length - 1)
281-
{
282-
order++;
283-
len = len / 1024;
284-
}
285-
return $"{len:0.##} {sizes[order]}";
286-
}
287142
}
288143
}

Resources/Strings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public static string Get(string key)
2525
public static string Copy => Get("Copy");
2626
public static string Generate => Get("Generate");
2727
public static string ClearAll => Get("ClearAll");
28+
public static string Clear => Get("Clear");
2829
public static string ShowOnly => Get("ShowOnly");
2930
public static string Save => Get("Save");
3031
public static string Delete => Get("Delete");

Resources/Strings.en-US.resx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@
7373
<data name="ClearAll" xml:space="preserve">
7474
<value>Clear All</value>
7575
</data>
76+
<data name="Clear" xml:space="preserve">
77+
<value>Clear</value>
78+
</data>
7679
<data name="ShowOnly" xml:space="preserve">
7780
<value>Show Only</value>
7881
</data>
@@ -304,4 +307,13 @@
304307
<data name="SignatureGenerateFailed" xml:space="preserve">
305308
<value>Failed to generate signature</value>
306309
</data>
310+
<data name="NoImageToPreview" xml:space="preserve">
311+
<value>No image to preview</value>
312+
</data>
313+
<data name="ClickToPreview" xml:space="preserve">
314+
<value>Click to preview</value>
315+
</data>
316+
<data name="ImageSaved" xml:space="preserve">
317+
<value>Image saved</value>
318+
</data>
307319
</root>

Resources/Strings.resx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@
7373
<data name="ClearAll" xml:space="preserve">
7474
<value>清除全部</value>
7575
</data>
76+
<data name="Clear" xml:space="preserve">
77+
<value>清除</value>
78+
</data>
7679
<data name="ShowOnly" xml:space="preserve">
7780
<value>仅显示</value>
7881
</data>
@@ -262,4 +265,13 @@
262265
<data name="SignatureGenerateFailed" xml:space="preserve">
263266
<value>生成签名失败</value>
264267
</data>
268+
<data name="NoImageToPreview" xml:space="preserve">
269+
<value>没有可预览的图片</value>
270+
</data>
271+
<data name="ClickToPreview" xml:space="preserve">
272+
<value>点击预览</value>
273+
</data>
274+
<data name="ImageSaved" xml:space="preserve">
275+
<value>图片已保存</value>
276+
</data>
265277
</root>

0 commit comments

Comments
 (0)