Skip to content

Commit 16aba11

Browse files
committed
Code Clean Ups; Removed redundand Code for WindowResizing;
Added: Saving Visibility of Window resizing to the Settings; Converted old Methods to Expressions.
1 parent 8111cdf commit 16aba11

File tree

4 files changed

+123
-143
lines changed

4 files changed

+123
-143
lines changed

QuickNoteWidget/MainWindow.xaml

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,17 @@
1212
KeyDown="Window_KeyDown"
1313
WindowStyle="None"
1414
Title="MainWindow"
15-
Height="{Binding WindowHeight, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
16-
Width="{Binding WindowWidht, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
15+
Height="350"
16+
Width="650"
1717
MinHeight="200"
1818
MinWidth="200"
1919
ResizeMode="CanResize"
2020
Topmost="{Binding OnTop}"
2121
ShowInTaskbar="{Binding ShowInTaskbar}"
2222
AllowsTransparency="True"
2323
Opacity="{Binding TransparencyValue, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
24-
ContextMenu="{DynamicResource contextMenu}">
24+
ContextMenu="{DynamicResource contextMenu}"
25+
PreviewMouseWheel="Window_PreviewMouseWheel">
2526
<Window.Resources>
2627
<converter:AccentToColorConverter x:Key="AccentToColorConverter" />
2728
<converter:StringToFontStyleConverter x:Key="fontStyle" />
@@ -99,12 +100,12 @@
99100
IsChecked="{Binding DisplayDetails}" />
100101
<CheckBox Content="Show in Taskbar"
101102
IsChecked="{Binding ShowInTaskbar}" />
103+
<CheckBox Content="Display Window Resize"
104+
ToolTip="Alt + H(eight) or Alt + W(idth) with Mouse Scrolling for more precise resizing."
105+
IsChecked="{Binding IsWindowResizeBarVisible, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
102106
</MenuItem>
103107
<MenuItem Header="Reset Settings"
104108
Command="{Binding ResetViewCommand}" />
105-
<CheckBox Content="Display Window Resize"
106-
ToolTip="Alt + H(eight) or Alt + W(idth) with Mouse Scrolling for more precise resizing."
107-
IsChecked="{Binding IsWindowResizeBarVisible, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
108109
<MenuItem Header="Info"
109110
Click="Info_Click" />
110111
</ContextMenu>
@@ -154,11 +155,13 @@
154155
Margin="0,0,5,0" />
155156
<StackPanel Orientation="Horizontal">
156157
<Button Content="-"
157-
Command="{Binding ReduceWindowSizeCommand}"
158-
Width="20" />
158+
x:Name="btnReduceWindowSize"
159+
Width="20"
160+
Click="btnReduceWindowSize_Click" />
159161
<Button Content="+"
160-
Command="{Binding IncreaseWindowSizeCommand}"
161-
Width="20" />
162+
x:Name="btnIncreaseWindowSize"
163+
Width="20"
164+
Click="btnIncreaseWindowSize_Click" />
162165
</StackPanel>
163166
</StackPanel>
164167
</StatusBarItem>
@@ -182,7 +185,7 @@
182185
<tb:TaskbarIcon IconSource=".\Clipboard.ico"
183186
x:Name="TaskbarIcon"
184187
Grid.RowSpan="2"
185-
Grid.ColumnSpan="4"
188+
Grid.ColumnSpan="4" Width="0.5" Height="0.5" VerticalAlignment="Bottom" HorizontalAlignment="Right"
186189
ContextMenu="{DynamicResource contextMenu}" />
187190
</Grid>
188191
</Window>

QuickNoteWidget/MainWindow.xaml.cs

Lines changed: 90 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Windows;
33
using System.Windows.Input;
4+
using DevExpress.Mvvm.Native;
45
using Application = System.Windows.Application;
56
using Clipboard = System.Windows.Clipboard;
67

@@ -13,29 +14,23 @@ public partial class MainWindow : Window
1314
private const double FONT_MIN_SIZE = 5d;
1415
private const double FONT_MAX_SIZE = 60d;
1516
private const int WINDOW_RESIZE_FACTOR = 15;
17+
private const int MIN_WINDOW_HEIGHT = 100;
18+
private const int MIN_WINDOW_WIDTH = 470;
1619

1720
public MainWindow()
1821
{
1922
InitializeComponent();
2023
DataContext = _mainWindowViewModel = new MainWindowViewModel();
2124
tbxMultiLine.Focus();
25+
_mainWindowViewModel.ResetWindowSizeAction = new Action(ResetWindowSize);
2226
}
2327

28+
#region Context Menu Events
29+
private void contextClose_Click(object sender, RoutedEventArgs e) => Application.Current.Shutdown();
2430

25-
private void contextClose_Click(object sender, RoutedEventArgs e)
26-
{
27-
Application.Current.Shutdown();
28-
}
29-
30-
private void contextMaximize_Click(object sender, RoutedEventArgs e)
31-
{
32-
WindowState = WindowState.Normal;
33-
}
31+
private void contextMaximize_Click(object sender, RoutedEventArgs e) => WindowState = WindowState.Normal;
3432

35-
private void contextMinimize_Click(object sender, RoutedEventArgs e)
36-
{
37-
WindowState = WindowState.Minimized;
38-
}
33+
private void contextMinimize_Click(object sender, RoutedEventArgs e) => WindowState = WindowState.Minimized;
3934

4035
private void contextAdd_Click(object sender, RoutedEventArgs e)
4136
{
@@ -54,7 +49,7 @@ private void contextCopy_Click(object sender, RoutedEventArgs e)
5449
else
5550
Clipboard.SetText(this.tbxMultiLine.SelectedText);
5651
}
57-
52+
5853
private void contextSelect_Click(object sender, RoutedEventArgs e)
5954
{
6055
if (String.IsNullOrEmpty(this.tbxMultiLine.Text))
@@ -80,6 +75,9 @@ private void Info_Click(object sender, RoutedEventArgs e)
8075
var _infoWindow = new InfoWindow(_mainWindowViewModel.SelectedAccent ?? "Cyan");
8176
_infoWindow.Show();
8277
}
78+
#endregion
79+
80+
8381

8482
private void Window_KeyDown(object sender, KeyEventArgs e)
8583
{
@@ -96,18 +94,12 @@ private void Window_KeyDown(object sender, KeyEventArgs e)
9694
private void tbxMultiLine_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
9795
{
9896
bool ctrlKeyDown = GetCtrlKeyDown();
99-
bool altKeyDown = GetAltKeyDown();
100-
bool hKeyDown = GetHKeyDown(); // h - height
101-
bool wKeyDown = GetWKeyDown(); // w - Width
102-
10397

10498
HandleFontSizeChange(e, ctrlKeyDown);
10599
if (e.Handled)
106100
return;
107-
108-
HandleWindowResize(e, altKeyDown, hKeyDown, wKeyDown);
109-
110101
}
102+
111103
private void HandleFontSizeChange(MouseWheelEventArgs e, bool ctrlButtonPressed)
112104
{
113105
if (ctrlButtonPressed)
@@ -118,50 +110,6 @@ private void HandleFontSizeChange(MouseWheelEventArgs e, bool ctrlButtonPressed)
118110
}
119111
}
120112

121-
private void HandleWindowResize(MouseWheelEventArgs e, bool altKeyDown, bool hKeyDown, bool wKeyDown)
122-
{
123-
if (altKeyDown && hKeyDown)
124-
{
125-
bool increase = e.Delta > 0;
126-
double currentSize = this.Height;
127-
if (increase)
128-
{
129-
double newHeight = currentSize + WINDOW_RESIZE_FACTOR;
130-
this.Height = newHeight;
131-
}
132-
else if(this.Height > 100)
133-
{
134-
double newHeight = currentSize - WINDOW_RESIZE_FACTOR;
135-
this.Height = newHeight;
136-
}
137-
else
138-
{
139-
return;
140-
}
141-
142-
}
143-
else if (altKeyDown && wKeyDown)
144-
{
145-
bool increase = e.Delta > 0;
146-
double currentSize = this.Width;
147-
if (increase)
148-
{
149-
double newWidth = currentSize + WINDOW_RESIZE_FACTOR;
150-
this.Width = newWidth;
151-
}
152-
else if (this.Width > 100)
153-
{
154-
double newWidth = currentSize - WINDOW_RESIZE_FACTOR;
155-
this.Width = newWidth;
156-
}
157-
else
158-
{
159-
return;
160-
}
161-
}
162-
e.Handled = true;
163-
}
164-
165113
private void UpdateFontSize(bool increase)
166114
{
167115
double currentSize = tbxMultiLine.FontSize;
@@ -185,41 +133,98 @@ private void UpdateFontSize(bool increase)
185133
}
186134

187135

188-
/// <summary>
189-
/// The "Document" Property of the Avalon TextEdit does not notify back to the VM
190-
/// Therefore, it's necessary to catch the Textchanged event in order to update the WordCount
191-
/// </summary>
192-
private void TbxMultiLine_TextChanged(object sender, EventArgs e)
136+
137+
138+
private void Window_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
193139
{
194-
if(_mainWindowViewModel != null)
195-
this._mainWindowViewModel.WordCount = this.tbxMultiLine.Document.TextLength.ToString();
140+
bool altKeyDown = GetAltKeyDown();
141+
bool hKeyDown = GetHKeyDown(); // h - height
142+
bool wKeyDown = GetWKeyDown(); // w - Width
143+
144+
// mouse wheel up equals e.Delta higher than 0
145+
// mouse wheel down equals e.Delta smaller than 0
146+
bool increase = e.Delta > 0;
147+
148+
HandleWindowResize(increase, altKeyDown, hKeyDown, wKeyDown);
196149
}
197150

198-
#region GetKeyDownMethods
199-
private static bool GetSubtractKeyDown()
151+
private void HandleWindowResize(bool increase, bool altKeyDown, bool hKeyDown, bool wKeyDown)
200152
{
201-
return Keyboard.IsKeyDown(Key.Subtract);
153+
if (altKeyDown && hKeyDown)
154+
ResizeHeight(increase);
155+
else if (altKeyDown && wKeyDown)
156+
ResizeWidth(increase);
202157
}
203-
private static bool GetAddKeyDown()
158+
159+
160+
private void ResizeHeight(bool increase)
204161
{
205-
return Keyboard.IsKeyDown(Key.Add);
162+
if (increase)
163+
IncreaseHeight();
164+
else if (this.Height > MIN_WINDOW_HEIGHT)
165+
DecreaseHeight();
206166
}
207-
private static bool GetCtrlKeyDown()
167+
168+
private void IncreaseHeight() => this.Height += WINDOW_RESIZE_FACTOR;
169+
170+
171+
private void DecreaseHeight() => this.Height -= WINDOW_RESIZE_FACTOR;
172+
173+
174+
175+
176+
private void ResizeWidth(bool increase)
208177
{
209-
return (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl));
178+
if (increase)
179+
IncreaseWidth();
180+
else if (this.Width > MIN_WINDOW_WIDTH)
181+
DecreaseWidth();
210182
}
211-
private static bool GetWKeyDown()
183+
184+
private void IncreaseWidth() => this.Width += WINDOW_RESIZE_FACTOR;
185+
186+
private void DecreaseWidth() => this.Width -= WINDOW_RESIZE_FACTOR;
187+
188+
private void btnIncreaseWindowSize_Click(object sender, RoutedEventArgs e)
212189
{
213-
return Keyboard.IsKeyDown(Key.W);
190+
ResizeHeight(increase: true);
191+
ResizeWidth(increase: true);
214192
}
215-
private static bool GetHKeyDown()
193+
194+
private void btnReduceWindowSize_Click(object sender, RoutedEventArgs e)
195+
{
196+
ResizeHeight(increase: false);
197+
ResizeWidth(increase: false);
198+
}
199+
200+
201+
202+
203+
/// <summary>
204+
/// The "Document" Property of the Avalon TextEdit does not notify back to the VM
205+
/// Therefore, it's necessary to catch the Textchanged event in order to update the WordCount
206+
/// </summary>
207+
private void TbxMultiLine_TextChanged(object sender, EventArgs e)
216208
{
217-
return Keyboard.IsKeyDown(Key.H);
209+
if (_mainWindowViewModel != null)
210+
this._mainWindowViewModel.WordCount = this.tbxMultiLine.Document.TextLength.ToString();
218211
}
219-
private static bool GetAltKeyDown()
212+
213+
214+
private void ResetWindowSize()
220215
{
221-
return (Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt));
216+
this.Width = 650;
217+
this.Height = 350;
222218
}
219+
220+
221+
#region GetKeyDownMethods
222+
private static bool GetSubtractKeyDown() => Keyboard.IsKeyDown(Key.Subtract);
223+
private static bool GetAddKeyDown() => Keyboard.IsKeyDown(Key.Add);
224+
private static bool GetCtrlKeyDown() => (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl));
225+
private static bool GetWKeyDown() => Keyboard.IsKeyDown(Key.W);
226+
private static bool GetHKeyDown() => Keyboard.IsKeyDown(Key.H);
227+
private static bool GetAltKeyDown() => (Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt));
223228
#endregion GetKeyDownMethods
224229
}
225230
}

0 commit comments

Comments
 (0)