Skip to content

Commit

Permalink
Code Clean Ups; Removed redundand Code for WindowResizing;
Browse files Browse the repository at this point in the history
Added: Saving Visibility of Window resizing to the Settings;
Converted old Methods to Expressions.
  • Loading branch information
al-develop committed Oct 14, 2022
1 parent 8111cdf commit 16aba11
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 143 deletions.
25 changes: 14 additions & 11 deletions QuickNoteWidget/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@
KeyDown="Window_KeyDown"
WindowStyle="None"
Title="MainWindow"
Height="{Binding WindowHeight, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
Width="{Binding WindowWidht, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
Height="350"
Width="650"
MinHeight="200"
MinWidth="200"
ResizeMode="CanResize"
Topmost="{Binding OnTop}"
ShowInTaskbar="{Binding ShowInTaskbar}"
AllowsTransparency="True"
Opacity="{Binding TransparencyValue, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
ContextMenu="{DynamicResource contextMenu}">
ContextMenu="{DynamicResource contextMenu}"
PreviewMouseWheel="Window_PreviewMouseWheel">
<Window.Resources>
<converter:AccentToColorConverter x:Key="AccentToColorConverter" />
<converter:StringToFontStyleConverter x:Key="fontStyle" />
Expand Down Expand Up @@ -99,12 +100,12 @@
IsChecked="{Binding DisplayDetails}" />
<CheckBox Content="Show in Taskbar"
IsChecked="{Binding ShowInTaskbar}" />
<CheckBox Content="Display Window Resize"
ToolTip="Alt + H(eight) or Alt + W(idth) with Mouse Scrolling for more precise resizing."
IsChecked="{Binding IsWindowResizeBarVisible, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
</MenuItem>
<MenuItem Header="Reset Settings"
Command="{Binding ResetViewCommand}" />
<CheckBox Content="Display Window Resize"
ToolTip="Alt + H(eight) or Alt + W(idth) with Mouse Scrolling for more precise resizing."
IsChecked="{Binding IsWindowResizeBarVisible, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
<MenuItem Header="Info"
Click="Info_Click" />
</ContextMenu>
Expand Down Expand Up @@ -154,11 +155,13 @@
Margin="0,0,5,0" />
<StackPanel Orientation="Horizontal">
<Button Content="-"
Command="{Binding ReduceWindowSizeCommand}"
Width="20" />
x:Name="btnReduceWindowSize"
Width="20"
Click="btnReduceWindowSize_Click" />
<Button Content="+"
Command="{Binding IncreaseWindowSizeCommand}"
Width="20" />
x:Name="btnIncreaseWindowSize"
Width="20"
Click="btnIncreaseWindowSize_Click" />
</StackPanel>
</StackPanel>
</StatusBarItem>
Expand All @@ -182,7 +185,7 @@
<tb:TaskbarIcon IconSource=".\Clipboard.ico"
x:Name="TaskbarIcon"
Grid.RowSpan="2"
Grid.ColumnSpan="4"
Grid.ColumnSpan="4" Width="0.5" Height="0.5" VerticalAlignment="Bottom" HorizontalAlignment="Right"
ContextMenu="{DynamicResource contextMenu}" />
</Grid>
</Window>
175 changes: 90 additions & 85 deletions QuickNoteWidget/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Windows;
using System.Windows.Input;
using DevExpress.Mvvm.Native;
using Application = System.Windows.Application;
using Clipboard = System.Windows.Clipboard;

Expand All @@ -13,29 +14,23 @@ public partial class MainWindow : Window
private const double FONT_MIN_SIZE = 5d;
private const double FONT_MAX_SIZE = 60d;
private const int WINDOW_RESIZE_FACTOR = 15;
private const int MIN_WINDOW_HEIGHT = 100;
private const int MIN_WINDOW_WIDTH = 470;

public MainWindow()
{
InitializeComponent();
DataContext = _mainWindowViewModel = new MainWindowViewModel();
tbxMultiLine.Focus();
_mainWindowViewModel.ResetWindowSizeAction = new Action(ResetWindowSize);
}

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

private void contextClose_Click(object sender, RoutedEventArgs e)
{
Application.Current.Shutdown();
}

private void contextMaximize_Click(object sender, RoutedEventArgs e)
{
WindowState = WindowState.Normal;
}
private void contextMaximize_Click(object sender, RoutedEventArgs e) => WindowState = WindowState.Normal;

private void contextMinimize_Click(object sender, RoutedEventArgs e)
{
WindowState = WindowState.Minimized;
}
private void contextMinimize_Click(object sender, RoutedEventArgs e) => WindowState = WindowState.Minimized;

private void contextAdd_Click(object sender, RoutedEventArgs e)
{
Expand All @@ -54,7 +49,7 @@ private void contextCopy_Click(object sender, RoutedEventArgs e)
else
Clipboard.SetText(this.tbxMultiLine.SelectedText);
}

private void contextSelect_Click(object sender, RoutedEventArgs e)
{
if (String.IsNullOrEmpty(this.tbxMultiLine.Text))
Expand All @@ -80,6 +75,9 @@ private void Info_Click(object sender, RoutedEventArgs e)
var _infoWindow = new InfoWindow(_mainWindowViewModel.SelectedAccent ?? "Cyan");
_infoWindow.Show();
}
#endregion



private void Window_KeyDown(object sender, KeyEventArgs e)
{
Expand All @@ -96,18 +94,12 @@ private void Window_KeyDown(object sender, KeyEventArgs e)
private void tbxMultiLine_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
bool ctrlKeyDown = GetCtrlKeyDown();
bool altKeyDown = GetAltKeyDown();
bool hKeyDown = GetHKeyDown(); // h - height
bool wKeyDown = GetWKeyDown(); // w - Width


HandleFontSizeChange(e, ctrlKeyDown);
if (e.Handled)
return;

HandleWindowResize(e, altKeyDown, hKeyDown, wKeyDown);

}

private void HandleFontSizeChange(MouseWheelEventArgs e, bool ctrlButtonPressed)
{
if (ctrlButtonPressed)
Expand All @@ -118,50 +110,6 @@ private void HandleFontSizeChange(MouseWheelEventArgs e, bool ctrlButtonPressed)
}
}

private void HandleWindowResize(MouseWheelEventArgs e, bool altKeyDown, bool hKeyDown, bool wKeyDown)
{
if (altKeyDown && hKeyDown)
{
bool increase = e.Delta > 0;
double currentSize = this.Height;
if (increase)
{
double newHeight = currentSize + WINDOW_RESIZE_FACTOR;
this.Height = newHeight;
}
else if(this.Height > 100)
{
double newHeight = currentSize - WINDOW_RESIZE_FACTOR;
this.Height = newHeight;
}
else
{
return;
}

}
else if (altKeyDown && wKeyDown)
{
bool increase = e.Delta > 0;
double currentSize = this.Width;
if (increase)
{
double newWidth = currentSize + WINDOW_RESIZE_FACTOR;
this.Width = newWidth;
}
else if (this.Width > 100)
{
double newWidth = currentSize - WINDOW_RESIZE_FACTOR;
this.Width = newWidth;
}
else
{
return;
}
}
e.Handled = true;
}

private void UpdateFontSize(bool increase)
{
double currentSize = tbxMultiLine.FontSize;
Expand All @@ -185,41 +133,98 @@ private void UpdateFontSize(bool increase)
}


/// <summary>
/// The "Document" Property of the Avalon TextEdit does not notify back to the VM
/// Therefore, it's necessary to catch the Textchanged event in order to update the WordCount
/// </summary>
private void TbxMultiLine_TextChanged(object sender, EventArgs e)


private void Window_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
if(_mainWindowViewModel != null)
this._mainWindowViewModel.WordCount = this.tbxMultiLine.Document.TextLength.ToString();
bool altKeyDown = GetAltKeyDown();
bool hKeyDown = GetHKeyDown(); // h - height
bool wKeyDown = GetWKeyDown(); // w - Width

// mouse wheel up equals e.Delta higher than 0
// mouse wheel down equals e.Delta smaller than 0
bool increase = e.Delta > 0;

HandleWindowResize(increase, altKeyDown, hKeyDown, wKeyDown);
}

#region GetKeyDownMethods
private static bool GetSubtractKeyDown()
private void HandleWindowResize(bool increase, bool altKeyDown, bool hKeyDown, bool wKeyDown)
{
return Keyboard.IsKeyDown(Key.Subtract);
if (altKeyDown && hKeyDown)
ResizeHeight(increase);
else if (altKeyDown && wKeyDown)
ResizeWidth(increase);
}
private static bool GetAddKeyDown()


private void ResizeHeight(bool increase)
{
return Keyboard.IsKeyDown(Key.Add);
if (increase)
IncreaseHeight();
else if (this.Height > MIN_WINDOW_HEIGHT)
DecreaseHeight();
}
private static bool GetCtrlKeyDown()

private void IncreaseHeight() => this.Height += WINDOW_RESIZE_FACTOR;


private void DecreaseHeight() => this.Height -= WINDOW_RESIZE_FACTOR;




private void ResizeWidth(bool increase)
{
return (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl));
if (increase)
IncreaseWidth();
else if (this.Width > MIN_WINDOW_WIDTH)
DecreaseWidth();
}
private static bool GetWKeyDown()

private void IncreaseWidth() => this.Width += WINDOW_RESIZE_FACTOR;

private void DecreaseWidth() => this.Width -= WINDOW_RESIZE_FACTOR;

private void btnIncreaseWindowSize_Click(object sender, RoutedEventArgs e)
{
return Keyboard.IsKeyDown(Key.W);
ResizeHeight(increase: true);
ResizeWidth(increase: true);
}
private static bool GetHKeyDown()

private void btnReduceWindowSize_Click(object sender, RoutedEventArgs e)
{
ResizeHeight(increase: false);
ResizeWidth(increase: false);
}




/// <summary>
/// The "Document" Property of the Avalon TextEdit does not notify back to the VM
/// Therefore, it's necessary to catch the Textchanged event in order to update the WordCount
/// </summary>
private void TbxMultiLine_TextChanged(object sender, EventArgs e)
{
return Keyboard.IsKeyDown(Key.H);
if (_mainWindowViewModel != null)
this._mainWindowViewModel.WordCount = this.tbxMultiLine.Document.TextLength.ToString();
}
private static bool GetAltKeyDown()


private void ResetWindowSize()
{
return (Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt));
this.Width = 650;
this.Height = 350;
}


#region GetKeyDownMethods
private static bool GetSubtractKeyDown() => Keyboard.IsKeyDown(Key.Subtract);
private static bool GetAddKeyDown() => Keyboard.IsKeyDown(Key.Add);
private static bool GetCtrlKeyDown() => (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl));
private static bool GetWKeyDown() => Keyboard.IsKeyDown(Key.W);
private static bool GetHKeyDown() => Keyboard.IsKeyDown(Key.H);
private static bool GetAltKeyDown() => (Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt));
#endregion GetKeyDownMethods
}
}
Loading

0 comments on commit 16aba11

Please sign in to comment.