Skip to content

Commit 56b9b83

Browse files
committed
Issue #10 - Added KeyBinding for resizing the Window
1 parent 2fd5ad8 commit 56b9b83

File tree

3 files changed

+102
-21
lines changed

3 files changed

+102
-21
lines changed

QuickNoteWidget/MainWindow.xaml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@
1313
WindowStyle="None"
1414
Title="MainWindow"
1515
Height="350"
16+
Width="600"
17+
MinHeight="200"
18+
MinWidth="200"
19+
ResizeMode="CanResize"
1620
Topmost="{Binding OnTop}"
1721
ShowInTaskbar="{Binding ShowInTaskbar}"
1822
AllowsTransparency="True"
1923
Opacity="{Binding TransparencyValue, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
2024
ContextMenu="{DynamicResource contextMenu}"
21-
Width="600">
25+
>
2226
<Window.Resources>
2327
<converter:AccentToColorConverter x:Key="AccentToColorConverter" />
2428
<converter:StringToFontStyleConverter x:Key="fontStyle" />
@@ -27,7 +31,7 @@
2731
<converter:BoolToVisibilityCollapsedConverter x:Key="BoolToVisibilityCollapsedConverter" />
2832
<Style TargetType="TextBlock">
2933
<Setter Property="Foreground"
30-
Value="{Binding SelectedAccent, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, Converter={StaticResource AccentToColorConverter}}" />
34+
Value="{Binding SelectedAccent, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, Converter={StaticResource AccentToColorConverter}}" />
3135
</Style>
3236
<ContextMenu x:Key="contextMenu">
3337
<MenuItem Header="Select All (Ctrl + A)"

QuickNoteWidget/MainWindow.xaml.cs

Lines changed: 96 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77

88
namespace QuickNoteWidget
99
{
10-
/// <summary>
11-
/// Interaction logic for MainWindow.xaml
12-
/// </summary>
1310
public partial class MainWindow : Window
1411
{
1512
private MainWindowViewModel _mainWindowViewModel;
13+
private const double FONT_MIN_SIZE = 5d;
14+
private const double FONT_MAX_SIZE = 60d;
15+
private const int WINDOW_RESIZE_FACTOR = 15;
1616

1717
public MainWindow()
1818
{
@@ -81,11 +81,35 @@ private void Info_Click(object sender, RoutedEventArgs e)
8181
_infoWindow.Show();
8282
}
8383

84+
private void Window_KeyDown(object sender, KeyEventArgs e)
85+
{
86+
bool ctrlKeyDown = GetCtrlKeyDown();
87+
bool addKeyDown = GetAddKeyDown();
88+
bool subtractKeyDown = GetSubtractKeyDown();
89+
90+
if (ctrlKeyDown && addKeyDown)
91+
UpdateFontSize(true);
92+
else if (ctrlKeyDown && subtractKeyDown)
93+
UpdateFontSize(false);
94+
}
8495

8596
private void tbxMultiLine_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
8697
{
87-
// React to ctrl + mouse wheel
88-
bool ctrlButtonPressed = (Keyboard.Modifiers == ModifierKeys.Control);
98+
bool ctrlKeyDown = GetCtrlKeyDown();
99+
bool altKeyDown = GetAltKeyDown();
100+
bool hKeyDown = GetHKeyDown(); // h - height
101+
bool wKeyDown = GetWKeyDown(); // w - Width
102+
103+
104+
HandleFontSizeChange(e, ctrlKeyDown);
105+
if (e.Handled)
106+
return;
107+
108+
HandleWindowResize(e, altKeyDown, hKeyDown, wKeyDown);
109+
110+
}
111+
private void HandleFontSizeChange(MouseWheelEventArgs e, bool ctrlButtonPressed)
112+
{
89113
if (ctrlButtonPressed)
90114
{
91115
bool increase = e.Delta > 0;
@@ -94,8 +118,49 @@ private void tbxMultiLine_PreviewMouseWheel(object sender, MouseWheelEventArgs e
94118
}
95119
}
96120

97-
private const double FONT_MIN_SIZE = 5d;
98-
private const double FONT_MAX_SIZE = 60d;
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+
}
99164

100165
private void UpdateFontSize(bool increase)
101166
{
@@ -130,16 +195,31 @@ private void TbxMultiLine_TextChanged(object sender, EventArgs e)
130195
this._mainWindowViewModel.WordCount = this.tbxMultiLine.Document.TextLength.ToString();
131196
}
132197

133-
private void Window_KeyDown(object sender, KeyEventArgs e)
198+
#region GetKeyDownMethods
199+
private static bool GetSubtractKeyDown()
134200
{
135-
bool ctrlKeyDown = (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl));
136-
bool addKeyDown = Keyboard.IsKeyDown(Key.Add);
137-
bool subtractKeyDown = Keyboard.IsKeyDown(Key.Subtract);
138-
139-
if (ctrlKeyDown && addKeyDown)
140-
UpdateFontSize(true);
141-
else if (ctrlKeyDown && subtractKeyDown)
142-
UpdateFontSize(false);
201+
return Keyboard.IsKeyDown(Key.Subtract);
202+
}
203+
private static bool GetAddKeyDown()
204+
{
205+
return Keyboard.IsKeyDown(Key.Add);
206+
}
207+
private static bool GetCtrlKeyDown()
208+
{
209+
return (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl));
210+
}
211+
private static bool GetWKeyDown()
212+
{
213+
return Keyboard.IsKeyDown(Key.W);
214+
}
215+
private static bool GetHKeyDown()
216+
{
217+
return Keyboard.IsKeyDown(Key.H);
218+
}
219+
private static bool GetAltKeyDown()
220+
{
221+
return (Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt));
143222
}
223+
#endregion GetKeyDownMethods
144224
}
145225
}

QuickNoteWidget/MainWindowViewModel.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,6 @@ private void ResetView()
205205
LoadSettings(SettingsLoadLocations.Default);
206206
}
207207

208-
209-
210-
211208
private void UpdateFontColorOnThemeSelectionChanged()
212209
{
213210
if (!String.IsNullOrEmpty(SelectedTheme))

0 commit comments

Comments
 (0)