Skip to content

Commit 350337a

Browse files
authored
Merge pull request #108 from lastunicorn/107-scrollmenu-consolewriteline-whites-text-over-the-menu
107 scrollmenu consolewriteline whites text over the menu
2 parents a526ee6 + 53933b8 commit 350337a

File tree

7 files changed

+57
-50
lines changed

7 files changed

+57
-50
lines changed

doc/changelog.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ ConsoleTools
33

44
Note: For any bug or feature request please add a new issue on GitHub: https://github.com/lastunicorn/ConsoleTools/issues/new/choose
55

6-
ver 1.3.0
6+
ver 1.2.1
77
---------
8+
- [bugfix ] ScrollMenu: Failing to leave the cursor below the menu when the menu was closing.
89

910

1011
ver 1.2.0

release/ConsoleTools.proj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<PropertyGroup>
66
<OutputPath>output</OutputPath>
77
<TempPath>temp</TempPath>
8-
<Version>1.3.0</Version>
8+
<Version>1.2.1</Version>
99
<OutputZipFileName>ConsoleTools-$(Version).zip</OutputZipFileName>
1010
<RepositoryRoot>..</RepositoryRoot>
1111
</PropertyGroup>

sources/ConsoleTools/AssemblyInfo.Shared.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,5 @@
3939
// You can specify all the values or you can default the Build and Revision Numbers
4040
// by using the '*' as shown below:
4141
// [assembly: AssemblyVersion("1.0.*")]
42-
[assembly: AssemblyVersion("1.3.0.0")]
42+
[assembly: AssemblyVersion("1.2.1.0")]
4343
//[assembly: AssemblyFileVersion("1.0.0.*")]

sources/ConsoleTools/ConsoleTools.Controls.Menus/ScrollMenu.cs

Lines changed: 51 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -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
{

sources/ConsoleTools/ConsoleTools.Core/Controls/ErasableControl.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,8 @@ namespace DustInTheWind.ConsoleTools.Controls;
2828
/// It is sometime useful for the controls that wait for an user input
2929
/// and then must get themselves out of the way.
3030
/// </summary>
31-
/// <remarks>
32-
/// In order to be able to successfully erase the control, the inheritor must
33-
/// calculate and set the <see cref="InnerSize"/> of the control until the end
34-
/// of the <see cref="BlockControl.DoDisplayContent"/> method.
35-
/// </remarks>
3631
public abstract class ErasableControl : BlockControl
3732
{
38-
/// <summary>
39-
/// Gets the size of the control after it was displayed.
40-
/// Does not include the margins
41-
/// </summary>
42-
public Size InnerSize { get; protected set; }
43-
4433
/// <summary>
4534
/// Gets or sets a value that specifies if the control is erased from the Console
4635
/// after it was displayed.
@@ -49,7 +38,6 @@ public abstract class ErasableControl : BlockControl
4938

5039
/// <summary>
5140
/// When implemented by an inheritor it displays the content of the control to the console.
52-
/// The inheritor must also calculate and set the <see cref="InnerSize"/> property.
5341
/// </summary>
5442
protected abstract override void DoDisplayContent(ControlDisplay display);
5543

sources/ConsoleTools/ConsoleTools.Demo.ScrollMenuDemo/MainMenu.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
using System;
1818
using System.Collections.Generic;
19+
using DustInTheWind.ConsoleTools.Controls;
1920
using DustInTheWind.ConsoleTools.Controls.Menus;
2021
using DustInTheWind.ConsoleTools.Controls.Menus.MenuItems;
2122
using DustInTheWind.ConsoleTools.Demo.ScrollMenuDemo.Commands;
@@ -28,8 +29,8 @@ public MainMenu(GameApplication application)
2829
{
2930
if (application == null) throw new ArgumentNullException(nameof(application));
3031

31-
EraseAfterClose = true;
3232
Margin = "0 1";
33+
HorizontalAlignment = HorizontalAlignment.Center;
3334
SelectFirstByDefault = true;
3435

3536
IEnumerable<IMenuItem> menuItems = CreateMenuItems(application);

sources/ConsoleTools/ConsoleTools.Demo.ScrollMenuDemo/Program.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ private static void Main()
3030
{
3131
DisplayApplicationHeader();
3232

33-
Console.SetWindowSize(80, 50);
34-
Console.SetBufferSize(80, 50);
35-
3633
Console.CancelKeyPress += HandleCancelKeyPress;
3734

3835
gameApplication = new GameApplication();

0 commit comments

Comments
 (0)