IDisposable POC

This commit is contained in:
Charlie Kindel
2020-06-14 21:54:15 -07:00
parent af88f610b0
commit e040382315
8 changed files with 132 additions and 114 deletions

View File

@@ -15,6 +15,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace Terminal.Gui {
/// <summary>
@@ -28,7 +29,14 @@ namespace Terminal.Gui {
/// For debug purposes to verify objects are being disposed properly
/// </summary>
public bool WasDisposed = false;
public int DisposedCount = 0;
/// <summary>
/// For debug purposes
/// </summary>
public static List<Responder> Instances = new List<Responder> ();
/// <summary>
/// For debug purposes
/// </summary>
public Responder ()
{
Instances.Add (this);
@@ -232,7 +240,11 @@ namespace Terminal.Gui {
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose (disposing: true);
GC.SuppressFinalize (this);
#if DEBUG
WasDisposed = true;
Debug.Assert (DisposedCount == 0);
DisposedCount++;
#endif
}
}
}

View File

@@ -344,13 +344,13 @@ namespace Terminal.Gui {
var uncheckedChar = Driver.UnSelected;
if (item.CheckType.HasFlag (MenuItemCheckStyle.Checked)) {
checkChar = Driver.Checked;
checkChar = Driver.Checked;
uncheckedChar = Driver.UnChecked;
}
// Support Checked even though CHeckType wasn't set
if (item.Checked) {
textToDraw = ustring.Make(new Rune [] { checkChar, ' ' }) + item.Title;
textToDraw = ustring.Make (new Rune [] { checkChar, ' ' }) + item.Title;
} else if (item.CheckType.HasFlag (MenuItemCheckStyle.Checked) ||
item.CheckType.HasFlag (MenuItemCheckStyle.Radio)) {
textToDraw = ustring.Make (new Rune [] { uncheckedChar, ' ' }) + item.Title;
@@ -795,8 +795,10 @@ namespace Terminal.Gui {
lastFocused = lastFocused ?? SuperView.MostFocused;
if (openSubMenu != null)
CloseMenu (false, true);
if (openMenu != null)
if (openMenu != null) {
SuperView.Remove (openMenu);
openMenu.Dispose ();
}
for (int i = 0; i < index; i++)
pos += Menus [i].Title.Length + 2;
@@ -869,11 +871,13 @@ namespace Terminal.Gui {
OnMenuClosing ();
switch (isSubMenu) {
case false:
if (openMenu != null)
if (openMenu != null) {
SuperView.Remove (openMenu);
}
SetNeedsDisplay ();
if (previousFocused != null && openMenu != null && previousFocused.ToString () != openCurrentMenu.ToString ())
previousFocused?.SuperView?.SetFocus (previousFocused);
openMenu.Dispose ();
openMenu = null;
if (lastFocused is Menu) {
lastFocused = null;
@@ -916,6 +920,7 @@ namespace Terminal.Gui {
if (openSubMenu != null) {
SuperView.Remove (openSubMenu [i]);
openSubMenu.Remove (openSubMenu [i]);
openSubMenu [i].Dispose ();
}
RemoveSubMenu (i);
}
@@ -950,6 +955,7 @@ namespace Terminal.Gui {
if (openSubMenu != null) {
foreach (var item in openSubMenu) {
SuperView.Remove (item);
item.Dispose ();
}
}
}

View File

@@ -646,10 +646,17 @@ namespace Terminal.Gui {
return true;
}
///<inheritdoc/>
protected override void Dispose (bool disposing)
{
vertical?.Dispose ();
horizontal?.Dispose ();
if (!showVerticalScrollIndicator) {
// It was not added to SuperView, so it won't get disposed automatically
vertical?.Dispose ();
}
if (!showHorizontalScrollIndicator) {
// It was not added to SuperView, so it won't get disposed automatically
horizontal?.Dispose ();
}
base.Dispose (disposing);
}
}

View File

@@ -75,8 +75,6 @@ namespace Terminal.Gui {
/// </summary>
public StatusBar () : this (items: new StatusItem [] { }) { }
Action<Application.ResizedEventArgs> cachedResizedHandler;
/// <summary>
/// Initializes a new instance of the <see cref="StatusBar"/> class with the specified set of <see cref="StatusItem"/>s.
/// The <see cref="StatusBar"/> will be drawn on the lowest line of the terminal or <see cref="View.SuperView"/> (if not null).
@@ -94,7 +92,12 @@ namespace Terminal.Gui {
Width = Dim.Fill ();
Height = 1;
cachedResizedHandler = (action) => {
Application.Resized += Application_Resized ();
}
private Action<Application.ResizedEventArgs> Application_Resized ()
{
return delegate {
X = 0;
Height = 1;
if (SuperView == null || SuperView == Application.Top) {
@@ -103,8 +106,6 @@ namespace Terminal.Gui {
//Y = Pos.Bottom (SuperView);
}
};
Application.Resized += cachedResizedHandler;
}
Attribute ToggleScheme (Attribute scheme)
@@ -204,7 +205,7 @@ namespace Terminal.Gui {
{
if (!disposedValue) {
if (disposing) {
Application.Resized -= cachedResizedHandler;
Application.Resized -= Application_Resized ();
}
disposedValue = true;
}

View File

@@ -439,7 +439,11 @@ namespace Terminal.Gui {
/// <param name="message">The message.</param>
public FileDialog (ustring title, ustring prompt, ustring nameFieldLabel, ustring message) : base (title, Driver.Cols - 20, Driver.Rows - 5, null)
{
this.message = new Label (Rect.Empty, "MESSAGE" + message);
this.message = new Label (message) {
X = 1,
Y = 0,
};
Add (this.message);
var msgLines = Label.MeasureLines (message, Driver.Cols - 20);
dirLabel = new Label ("Directory: ") {
@@ -509,6 +513,12 @@ namespace Terminal.Gui {
//SetFocus (nameEntry);
}
//protected override void Dispose (bool disposing)
//{
// message?.Dispose ();
// base.Dispose (disposing);
//}
/// <summary>
/// Gets or sets the prompt label for the <see cref="Button"/> displayed to the user
/// </summary>

View File

@@ -1,6 +1,7 @@
using NStack;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
@@ -334,6 +335,7 @@ namespace UICatalog {
// Remove existing class, if any
if (view != null) {
_hostPane.Remove (view);
view.Dispose ();
_hostPane.Clear ();
}
}

View File

@@ -67,7 +67,6 @@ namespace UICatalog {
public override void Run ()
{
base.Run ();
_charMap.Dispose ();
}
}
@@ -128,11 +127,6 @@ namespace UICatalog {
}
}
}
protected override void Dispose (bool disposing)
{
base.Dispose (disposing);
}
#else
public override void OnDrawContent (Rect viewport)
{

View File

@@ -112,101 +112,6 @@ namespace UICatalog {
Application.UseSystemConsole = false;
Application.Init ();
if (_menu == null) {
Setup ();
}
_top = Application.Top;
_top.KeyDown += KeyDownHandler;
_top.Add (_menu);
_top.Add (_leftPane);
_top.Add (_rightPane);
_top.Add (_statusBar);
_top.Ready += () => {
if (_runningScenario != null) {
_top.SetFocus (_rightPane);
_runningScenario = null;
}
};
Application.Run (_top, true);
Application.Shutdown ();
return _runningScenario;
}
static MenuItem [] CreateDiagnosticMenuItems ()
{
MenuItem CheckedMenuMenuItem (ustring menuItem, Action action, Func<bool> checkFunction)
{
var mi = new MenuItem ();
mi.Title = menuItem;
mi.CheckType |= MenuItemCheckStyle.Checked;
mi.Checked = checkFunction ();
mi.Action = () => {
action?.Invoke ();
mi.Title = menuItem;
mi.Checked = checkFunction ();
};
return mi;
}
return new MenuItem [] {
CheckedMenuMenuItem ("Use _System Console",
() => {
_useSystemConsole = !_useSystemConsole;
},
() => _useSystemConsole),
CheckedMenuMenuItem ("Diagnostics: _Frame Padding",
() => {
ConsoleDriver.Diagnostics ^= ConsoleDriver.DiagnosticFlags.FramePadding;
_top.SetNeedsDisplay ();
},
() => (ConsoleDriver.Diagnostics & ConsoleDriver.DiagnosticFlags.FramePadding) == ConsoleDriver.DiagnosticFlags.FramePadding),
CheckedMenuMenuItem ("Diagnostics: Frame _Ruler",
() => {
ConsoleDriver.Diagnostics ^= ConsoleDriver.DiagnosticFlags.FrameRuler;
_top.SetNeedsDisplay ();
},
() => (ConsoleDriver.Diagnostics & ConsoleDriver.DiagnosticFlags.FrameRuler) == ConsoleDriver.DiagnosticFlags.FrameRuler),
};
}
static void SetColorScheme ()
{
_leftPane.ColorScheme = _baseColorScheme;
_rightPane.ColorScheme = _baseColorScheme;
_top?.SetNeedsDisplay ();
}
static ColorScheme _baseColorScheme;
static MenuItem [] CreateColorSchemeMenuItems ()
{
List<MenuItem> menuItems = new List<MenuItem> ();
foreach (var sc in Colors.ColorSchemes) {
var item = new MenuItem ();
item.Title = sc.Key;
item.CheckType |= MenuItemCheckStyle.Radio;
item.Checked = sc.Value == _baseColorScheme;
item.Action += () => {
_baseColorScheme = sc.Value;
SetColorScheme ();
foreach (var menuItem in menuItems) {
menuItem.Checked = menuItem.Title.Equals (sc.Key) && sc.Value == _baseColorScheme;
}
};
menuItems.Add (item);
}
return menuItems.ToArray ();
}
/// <summary>
/// Create all controls. This gets called once and the controls remain with their state between Sceanrio runs.
/// </summary>
private static void Setup ()
{
// Set this here because not initilzied until driver is loaded
_baseColorScheme = Colors.Base;
@@ -296,6 +201,87 @@ namespace UICatalog {
});
SetColorScheme ();
_top = Application.Top;
_top.KeyDown += KeyDownHandler;
_top.Add (_menu);
_top.Add (_leftPane);
_top.Add (_rightPane);
_top.Add (_statusBar);
_top.Ready += () => {
if (_runningScenario != null) {
_top.SetFocus (_rightPane);
_runningScenario = null;
}
};
Application.Run (_top, true);
Application.Shutdown ();
return _runningScenario;
}
static MenuItem [] CreateDiagnosticMenuItems ()
{
MenuItem CheckedMenuMenuItem (ustring menuItem, Action action, Func<bool> checkFunction)
{
var mi = new MenuItem ();
mi.Title = menuItem;
mi.CheckType |= MenuItemCheckStyle.Checked;
mi.Checked = checkFunction ();
mi.Action = () => {
action?.Invoke ();
mi.Title = menuItem;
mi.Checked = checkFunction ();
};
return mi;
}
return new MenuItem [] {
CheckedMenuMenuItem ("Use _System Console",
() => {
_useSystemConsole = !_useSystemConsole;
},
() => _useSystemConsole),
CheckedMenuMenuItem ("Diagnostics: _Frame Padding",
() => {
ConsoleDriver.Diagnostics ^= ConsoleDriver.DiagnosticFlags.FramePadding;
_top.SetNeedsDisplay ();
},
() => (ConsoleDriver.Diagnostics & ConsoleDriver.DiagnosticFlags.FramePadding) == ConsoleDriver.DiagnosticFlags.FramePadding),
CheckedMenuMenuItem ("Diagnostics: Frame _Ruler",
() => {
ConsoleDriver.Diagnostics ^= ConsoleDriver.DiagnosticFlags.FrameRuler;
_top.SetNeedsDisplay ();
},
() => (ConsoleDriver.Diagnostics & ConsoleDriver.DiagnosticFlags.FrameRuler) == ConsoleDriver.DiagnosticFlags.FrameRuler),
};
}
static void SetColorScheme ()
{
_leftPane.ColorScheme = _baseColorScheme;
_rightPane.ColorScheme = _baseColorScheme;
_top?.SetNeedsDisplay ();
}
static ColorScheme _baseColorScheme;
static MenuItem [] CreateColorSchemeMenuItems ()
{
List<MenuItem> menuItems = new List<MenuItem> ();
foreach (var sc in Colors.ColorSchemes) {
var item = new MenuItem ();
item.Title = sc.Key;
item.CheckType |= MenuItemCheckStyle.Radio;
item.Checked = sc.Value == _baseColorScheme;
item.Action += () => {
_baseColorScheme = sc.Value;
SetColorScheme ();
foreach (var menuItem in menuItems) {
menuItem.Checked = menuItem.Title.Equals (sc.Key) && sc.Value == _baseColorScheme;
}
};
menuItems.Add (item);
}
return menuItems.ToArray ();
}
private static void _scenarioListView_OpenSelectedItem (EventArgs e)