@@ -34,9 +34,16 @@ public class ScrollMenu : ErasableControl, IRepeatableSupport
34
34
private const HorizontalAlignment DefaultHorizontalAlignment = HorizontalAlignment . Center ;
35
35
private readonly MenuItemCollection menuItems = new ( ) ;
36
36
private bool closeWasRequested ;
37
- private Location menuLocation ;
38
37
private Location itemsLocation ;
39
38
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
+
40
47
/// <summary>
41
48
/// Gets the item that is currently selected.
42
49
/// </summary>
@@ -71,7 +78,7 @@ public class ScrollMenu : ErasableControl, IRepeatableSupport
71
78
public bool KeepHighlightingOnClose { get ; set ; }
72
79
73
80
/// <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.
75
82
/// When reaching the first item go to the last item.
76
83
/// When reaching the last item go to the first item.
77
84
/// Default value: <c>true</c>
@@ -84,7 +91,6 @@ public bool AllowWrapAround
84
91
85
92
/// <summary>
86
93
/// 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.
88
94
/// </summary>
89
95
public event EventHandler Closed ;
90
96
@@ -160,8 +166,6 @@ protected override void OnBeforeDisplay()
160
166
throw new ApplicationException ( "There are no menu items to be displayed." ) ;
161
167
162
168
closeWasRequested = false ;
163
- //InnerSize = Size.Empty;
164
- menuLocation = Location . Origin ;
165
169
itemsLocation = Location . Origin ;
166
170
167
171
//for (int i = 0; i < InnerSize.Height; i++)
@@ -182,18 +186,16 @@ protected override void DoDisplayContent(ControlDisplay display)
182
186
183
187
try
184
188
{
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 ) ;
189
191
190
192
itemsLocation = CalculateMenuLocation ( ) ;
191
193
192
- foreach ( IMenuItem menuItem in menuItems )
193
- {
194
- if ( ! menuItem . IsVisible )
195
- continue ;
194
+ IEnumerable < IMenuItem > visibleMenuItems = menuItems
195
+ . Where ( x => x . IsVisible ) ;
196
196
197
+ foreach ( IMenuItem menuItem in visibleMenuItems )
198
+ {
197
199
int left = itemsLocation . Left ;
198
200
int top = Console . CursorTop ;
199
201
@@ -205,6 +207,8 @@ protected override void DoDisplayContent(ControlDisplay display)
205
207
Console . WriteLine ( ) ;
206
208
}
207
209
210
+ itemsLocation = new Location ( itemsLocation . Left , Console . CursorTop - itemsSize . Height ) ;
211
+
208
212
if ( SelectFirstByDefault )
209
213
menuItems . SelectFirst ( ) ;
210
214
@@ -217,7 +221,7 @@ protected override void DoDisplayContent(ControlDisplay display)
217
221
218
222
menuItems . CurrentIndexChanged -= HandleCurrentIndexChanged ;
219
223
220
- int lastMenuLine = menuLocation . Top + InnerSize . Height - 1 ;
224
+ int lastMenuLine = itemsLocation . Top + itemsSize . Height - 1 ;
221
225
Console . SetCursorPosition ( 0 , lastMenuLine ) ;
222
226
Console . WriteLine ( ) ;
223
227
}
@@ -234,32 +238,44 @@ private void HandleCurrentIndexChanged(object sender, CurrentIndexChangedEventAr
234
238
235
239
private Location CalculateMenuLocation ( )
236
240
{
237
- HorizontalAlignment calcualtedHorizontalAlignment = CalcualteHorizontalAlignment ( ) ;
241
+ HorizontalAlignment calculatedHorizontalAlignment = CalculateHorizontalAlignment ( ) ;
238
242
239
243
int menuTop = Console . CursorTop ;
240
244
241
- switch ( calcualtedHorizontalAlignment )
245
+ switch ( calculatedHorizontalAlignment )
242
246
{
243
247
default :
244
248
return new Location ( 0 , menuTop ) ;
245
249
246
250
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
+ }
248
255
249
256
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
+ }
251
261
}
252
262
}
253
263
254
264
private Size CalculateItemsSize ( )
255
265
{
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 ++ ;
258
275
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
+ }
263
279
264
280
return new Size ( menuWidth , menuHeight ) ;
265
281
}
@@ -276,21 +292,21 @@ private void DrawMenuItem(int index)
276
292
277
293
Console . SetCursorPosition ( left , top ) ;
278
294
279
- Size menuItemSize = new ( InnerSize . Width , 1 ) ;
295
+ Size menuItemSize = new ( itemsSize . Width , 1 ) ;
280
296
bool isHighlighted = menuItemToDraw == menuItems . CurrentItem ;
281
297
282
298
menuItemToDraw . Display ( menuItemSize , isHighlighted ) ;
283
299
}
284
300
}
285
301
286
- private HorizontalAlignment CalcualteHorizontalAlignment ( )
302
+ private HorizontalAlignment CalculateHorizontalAlignment ( )
287
303
{
288
- HorizontalAlignment calcualtedHorizontalAlignment = HorizontalAlignment ;
304
+ HorizontalAlignment calculatedHorizontalAlignment = HorizontalAlignment ;
289
305
290
- if ( calcualtedHorizontalAlignment == HorizontalAlignment . Default )
291
- calcualtedHorizontalAlignment = DefaultHorizontalAlignment ;
306
+ if ( calculatedHorizontalAlignment == HorizontalAlignment . Default )
307
+ calculatedHorizontalAlignment = DefaultHorizontalAlignment ;
292
308
293
- return calcualtedHorizontalAlignment ;
309
+ return calculatedHorizontalAlignment ;
294
310
}
295
311
296
312
private void ReadUserSelection ( )
@@ -363,11 +379,15 @@ protected override void OnAfterDisplay()
363
379
{
364
380
base . OnAfterDisplay ( ) ;
365
381
382
+ //int lastMenuLine = itemsLocation.Top + menuSize.Height - 1;
383
+ //Console.SetCursorPosition(0, lastMenuLine);
384
+ //Console.WriteLine();
385
+
366
386
SelectedItem ? . Command ? . Execute ( ) ;
367
387
}
368
388
369
389
/// <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.
371
391
/// </summary>
372
392
public void RequestClose ( )
373
393
{
0 commit comments