@@ -34,9 +34,16 @@ public class ScrollMenu : ErasableControl, IRepeatableSupport
3434 private const HorizontalAlignment DefaultHorizontalAlignment = HorizontalAlignment . Center ;
3535 private readonly MenuItemCollection menuItems = new ( ) ;
3636 private bool closeWasRequested ;
37- private Location menuLocation ;
3837 private Location itemsLocation ;
3938
39+ /// <summary>
40+ /// The size of the control after it was displayed.
41+ /// Does not include the margins
42+ /// </summary>
43+ private Size menuSize ;
44+
45+ private Size itemsSize ;
46+
4047 /// <summary>
4148 /// Gets the item that is currently selected.
4249 /// </summary>
@@ -71,7 +78,7 @@ public class ScrollMenu : ErasableControl, IRepeatableSupport
7178 public bool KeepHighlightingOnClose { get ; set ; }
7279
7380 /// <summary>
74- /// Gets or sets a vlue that specifies if circular selection is allowed.
81+ /// Gets or sets a value that specifies if circular selection is allowed.
7582 /// When reaching the first item go to the last item.
7683 /// When reaching the last item go to the first item.
7784 /// Default value: <c>true</c>
@@ -84,7 +91,6 @@ public bool AllowWrapAround
8491
8592 /// <summary>
8693 /// Event raised when the current instance cannot be displayed anymore and it is in the "Closed" state.
87- /// The <see cref="ControlRepeater"/> must also end its display loop.
8894 /// </summary>
8995 public event EventHandler Closed ;
9096
@@ -160,8 +166,6 @@ protected override void OnBeforeDisplay()
160166 throw new ApplicationException ( "There are no menu items to be displayed." ) ;
161167
162168 closeWasRequested = false ;
163- //InnerSize = Size.Empty;
164- menuLocation = Location . Origin ;
165169 itemsLocation = Location . Origin ;
166170
167171 //for (int i = 0; i < InnerSize.Height; i++)
@@ -182,18 +186,16 @@ protected override void DoDisplayContent(ControlDisplay display)
182186
183187 try
184188 {
185- menuLocation = CalculateMenuLocation ( ) ;
186-
187- Size itemsSize = CalculateItemsSize ( ) ;
188- //InnerSize = new Size(itemsSize.Width, InnerSize.Height + itemsSize.Height);
189+ itemsSize = CalculateItemsSize ( ) ;
190+ menuSize = itemsSize + new Size ( Padding . Left + Padding . Right , Padding . Top + Padding . Bottom ) ;
189191
190192 itemsLocation = CalculateMenuLocation ( ) ;
191193
192- foreach ( IMenuItem menuItem in menuItems )
193- {
194- if ( ! menuItem . IsVisible )
195- continue ;
194+ IEnumerable < IMenuItem > visibleMenuItems = menuItems
195+ . Where ( x => x . IsVisible ) ;
196196
197+ foreach ( IMenuItem menuItem in visibleMenuItems )
198+ {
197199 int left = itemsLocation . Left ;
198200 int top = Console . CursorTop ;
199201
@@ -205,6 +207,8 @@ protected override void DoDisplayContent(ControlDisplay display)
205207 Console . WriteLine ( ) ;
206208 }
207209
210+ itemsLocation = new Location ( itemsLocation . Left , Console . CursorTop - itemsSize . Height ) ;
211+
208212 if ( SelectFirstByDefault )
209213 menuItems . SelectFirst ( ) ;
210214
@@ -217,7 +221,7 @@ protected override void DoDisplayContent(ControlDisplay display)
217221
218222 menuItems . CurrentIndexChanged -= HandleCurrentIndexChanged ;
219223
220- int lastMenuLine = menuLocation . Top + InnerSize . Height - 1 ;
224+ int lastMenuLine = itemsLocation . Top + itemsSize . Height - 1 ;
221225 Console . SetCursorPosition ( 0 , lastMenuLine ) ;
222226 Console . WriteLine ( ) ;
223227 }
@@ -234,32 +238,44 @@ private void HandleCurrentIndexChanged(object sender, CurrentIndexChangedEventAr
234238
235239 private Location CalculateMenuLocation ( )
236240 {
237- HorizontalAlignment calcualtedHorizontalAlignment = CalcualteHorizontalAlignment ( ) ;
241+ HorizontalAlignment calculatedHorizontalAlignment = CalculateHorizontalAlignment ( ) ;
238242
239243 int menuTop = Console . CursorTop ;
240244
241- switch ( calcualtedHorizontalAlignment )
245+ switch ( calculatedHorizontalAlignment )
242246 {
243247 default :
244248 return new Location ( 0 , menuTop ) ;
245249
246250 case HorizontalAlignment . Center :
247- return new Location ( ( Console . BufferWidth - InnerSize . Width ) / 2 , menuTop ) ;
251+ {
252+ int menuLeft = ( Console . BufferWidth - menuSize . Width ) / 2 ;
253+ return new Location ( menuLeft , menuTop ) ;
254+ }
248255
249256 case HorizontalAlignment . Right :
250- return new Location ( Console . BufferWidth - InnerSize . Width , menuTop ) ;
257+ {
258+ int menuLeft = Console . BufferWidth - menuSize . Width ;
259+ return new Location ( menuLeft , menuTop ) ;
260+ }
251261 }
252262 }
253263
254264 private Size CalculateItemsSize ( )
255265 {
256- int menuHeight = menuItems
257- . Count ( x => x . IsVisible ) ;
266+ IEnumerable < IMenuItem > visibleMenuItems = menuItems
267+ . Where ( x => x . IsVisible ) ;
268+
269+ int menuHeight = 0 ;
270+ int menuWidth = 0 ;
271+
272+ foreach ( IMenuItem menuItem in visibleMenuItems )
273+ {
274+ menuHeight ++ ;
258275
259- int menuWidth = menuItems
260- . Where ( x => x . IsVisible )
261- . Select ( x => x . Size )
262- . Max ( x => x . Width ) ;
276+ if ( menuItem . Size . Width > menuWidth )
277+ menuWidth = menuItem . Size . Width ;
278+ }
263279
264280 return new Size ( menuWidth , menuHeight ) ;
265281 }
@@ -276,21 +292,21 @@ private void DrawMenuItem(int index)
276292
277293 Console . SetCursorPosition ( left , top ) ;
278294
279- Size menuItemSize = new ( InnerSize . Width , 1 ) ;
295+ Size menuItemSize = new ( itemsSize . Width , 1 ) ;
280296 bool isHighlighted = menuItemToDraw == menuItems . CurrentItem ;
281297
282298 menuItemToDraw . Display ( menuItemSize , isHighlighted ) ;
283299 }
284300 }
285301
286- private HorizontalAlignment CalcualteHorizontalAlignment ( )
302+ private HorizontalAlignment CalculateHorizontalAlignment ( )
287303 {
288- HorizontalAlignment calcualtedHorizontalAlignment = HorizontalAlignment ;
304+ HorizontalAlignment calculatedHorizontalAlignment = HorizontalAlignment ;
289305
290- if ( calcualtedHorizontalAlignment == HorizontalAlignment . Default )
291- calcualtedHorizontalAlignment = DefaultHorizontalAlignment ;
306+ if ( calculatedHorizontalAlignment == HorizontalAlignment . Default )
307+ calculatedHorizontalAlignment = DefaultHorizontalAlignment ;
292308
293- return calcualtedHorizontalAlignment ;
309+ return calculatedHorizontalAlignment ;
294310 }
295311
296312 private void ReadUserSelection ( )
@@ -363,11 +379,15 @@ protected override void OnAfterDisplay()
363379 {
364380 base . OnAfterDisplay ( ) ;
365381
382+ //int lastMenuLine = itemsLocation.Top + menuSize.Height - 1;
383+ //Console.SetCursorPosition(0, lastMenuLine);
384+ //Console.WriteLine();
385+
366386 SelectedItem ? . Command ? . Execute ( ) ;
367387 }
368388
369389 /// <summary>
370- /// The <see cref="ControlRepeater"/> calls this method to announce the control that it should end its process.
390+ /// Call this method to announce the control that it should end its process.
371391 /// </summary>
372392 public void RequestClose ( )
373393 {
0 commit comments