11using System ;
22using System . Windows ;
33using System . Windows . Input ;
4+ using DevExpress . Mvvm . Native ;
45using Application = System . Windows . Application ;
56using 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