Files
Terminal.Gui/Examples/UICatalog/Scenarios/MessageBoxes.cs
Tig 0f72cf8a74 Fixes #4425 - ApplicationImpl internal (#4426)
* Pulled from v2_release

* Refactor migration guide for Terminal.Gui v2

Restructured and expanded the migration guide to provide a comprehensive resource for transitioning from Terminal.Gui v1 to v2. Key updates include:

- Added a Table of Contents for easier navigation.
- Summarized major architectural changes in v2, including the instance-based application model, IRunnable architecture, and 24-bit TrueColor support.
- Updated examples to reflect new patterns, such as initializers replacing constructors and explicit disposal using `IDisposable`.
- Documented changes to the layout system, including the removal of `Absolute`/`Computed` styles and the introduction of `Viewport`.
- Standardized event patterns to use `object sender, EventArgs args`.
- Detailed updates to the Keyboard, Mouse, and Navigation APIs, including configurable key bindings and viewport-relative mouse coordinates.
- Replaced legacy components like `ScrollView` and `ContextMenu` with built-in scrolling and `PopoverMenu`.
- Clarified disposal rules and introduced best practices for resource management.
- Provided a complete migration example and a summary of breaking changes.

This update aims to simplify the migration process by addressing breaking changes, introducing new features, and aligning with modern .NET conventions.

* Refactor to use Application.Instance for lifecycle management

Replaced all occurrences of `ApplicationImpl.Instance` with the new `Application.Instance` property across the codebase to align with the updated application lifecycle model.

Encapsulated the `ApplicationImpl` class by making it `internal`, ensuring it is no longer directly accessible outside its assembly. Introduced the `[Obsolete]` `Application.Instance` property as a backward-compatible singleton for the legacy static `Application` model, while encouraging the use of `Application.Create()` for new code.

Updated `MessageBox` methods to use `Application.Instance` for consistent modal dialog management. Improved documentation to reflect these changes and emphasize the transition to the instance-based application model.

Performed code cleanup in multiple classes to ensure consistency and maintainability. These changes maintain backward compatibility while preparing the codebase for the eventual removal of the legacy `ApplicationImpl` class.

* Fix doc bug

* - Removed obsolete `.cd` class diagram files.
- Introduced `IRunnable` interface for decoupling component execution.
- Added fluent API for running dialogs and retrieving results.
- Enhanced `View` with `App` and `Driver` properties for better decoupling.
- Improved testability with support for mock and real applications.
- Implemented `IDisposable` for proper resource cleanup.
- Replaced `RunnableSessionStack` with `SessionStack` for session management.
- Updated driver architecture to align with the new model.
- Scoped `IKeyboard` to application contexts for modularity.
- Updated documentation with migration strategies and best practices.

These changes modernize the library, improve maintainability, and align with current development practices.
2025-12-01 14:40:31 -07:00

349 lines
11 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
namespace UICatalog.Scenarios;
[ScenarioMetadata ("MessageBoxes", "Demonstrates how to use the MessageBox class.")]
[ScenarioCategory ("Controls")]
[ScenarioCategory ("Dialogs")]
public class MessageBoxes : Scenario
{
public override void Main ()
{
Application.Init ();
Window app = new ()
{
Title = GetQuitKeyAndName (),
};
var frame = new FrameView
{
X = Pos.Center (),
Y = 1,
Width = Dim.Percent (75),
Height = Dim.Auto (DimAutoStyle.Content),
Title = "MessageBox Options"
};
app.Add (frame);
// TODO: Use Pos.Align her to demo aligning labels and fields
var label = new Label { X = 0, Y = 0, Width = 15, TextAlignment = Alignment.End, Text = "W_idth:" };
frame.Add (label);
var widthEdit = new TextField
{
X = Pos.Right (label) + 1,
Y = Pos.Top (label),
Width = 5,
Height = 1,
Text = "0"
};
frame.Add (widthEdit);
label = new ()
{
X = 0,
Y = Pos.Bottom (label),
Width = Dim.Width (label),
Height = 1,
TextAlignment = Alignment.End,
Text = "_Height:"
};
frame.Add (label);
var heightEdit = new TextField
{
X = Pos.Right (label) + 1,
Y = Pos.Top (label),
Width = 5,
Height = 1,
Text = "0"
};
frame.Add (heightEdit);
frame.Add (
new Label
{
X = Pos.Right (widthEdit) + 2,
Y = Pos.Top (widthEdit),
Text = $"If width is 0, the dimension will be greater than {MessageBox.DefaultMinimumWidth}%."
}
);
frame.Add (
new Label
{
X = Pos.Right (heightEdit) + 2,
Y = Pos.Top (heightEdit),
Text = $"If height is 0, the dimension will be greater than {MessageBox.DefaultMinimumHeight}%."
}
);
label = new ()
{
X = 0,
Y = Pos.Bottom (label),
Width = Dim.Width (label),
Height = 1,
TextAlignment = Alignment.End,
Text = "_Title:"
};
frame.Add (label);
var titleEdit = new TextField
{
X = Pos.Right (label) + 1,
Y = Pos.Top (label),
Width = Dim.Fill (),
Height = 1,
Text = "The title"
};
frame.Add (titleEdit);
label = new ()
{
X = 0,
Y = Pos.Bottom (label),
Width = Dim.Width (label),
Height = 1,
TextAlignment = Alignment.End,
Text = "_Message:"
};
frame.Add (label);
var messageEdit = new TextView
{
Text = "Message line 1.\nMessage line two. This is a really long line to force wordwrap. It needs to be long for it to work.",
X = Pos.Right (label) + 1,
Y = Pos.Top (label),
Width = Dim.Fill (),
Height = 5
};
frame.Add (messageEdit);
label = new ()
{
X = 0,
Y = Pos.Bottom (messageEdit),
Width = Dim.Width (label),
Height = 1,
TextAlignment = Alignment.End,
Text = "_Num Buttons:"
};
frame.Add (label);
var numButtonsEdit = new TextField
{
X = Pos.Right (label) + 1,
Y = Pos.Top (label),
Width = 5,
Height = 1,
Text = "3"
};
frame.Add (numButtonsEdit);
label = new ()
{
X = 0,
Y = Pos.Bottom (label),
Width = Dim.Width (label),
Height = 1,
TextAlignment = Alignment.End,
Text = "_Default Button:"
};
frame.Add (label);
var defaultButtonEdit = new TextField
{
X = Pos.Right (label) + 1,
Y = Pos.Top (label),
Width = 5,
Height = 1,
Text = "0"
};
frame.Add (defaultButtonEdit);
label = new ()
{
X = 0,
Y = Pos.Bottom (label),
Width = Dim.Width (label),
Height = 1,
TextAlignment = Alignment.End,
Text = "St_yle:"
};
frame.Add (label);
var styleOptionSelector = new OptionSelector ()
{
X = Pos.Right (label) + 1,
Y = Pos.Top (label),
Labels = ["_Query", "_Error"],
Title = "Sty_le"
};
frame.Add (styleOptionSelector);
label = new ()
{
X = 0,
Y = Pos.Bottom (styleOptionSelector),
Width = Dim.Width (label),
Height = 1,
TextAlignment = Alignment.End,
Text = "Wra_p:"
};
var ckbWrapMessage = new CheckBox
{
X = Pos.Right (label) + 1, Y = Pos.Bottom (styleOptionSelector),
CheckedState = CheckState.Checked,
Text = "_Wrap Message",
};
frame.Add (label, ckbWrapMessage);
frame.ValidatePosDim = true;
label = new ()
{
X = Pos.Center (), Y = Pos.Bottom (frame) + 2, TextAlignment = Alignment.End, Text = "Button Pressed:"
};
app.Add (label);
var buttonPressedLabel = new Label
{
X = Pos.Center (),
Y = Pos.Bottom (label) + 1,
SchemeName = "Error",
TextAlignment = Alignment.Center,
Text = " "
};
var showMessageBoxButton = new Button
{
X = Pos.Center (), Y = Pos.Bottom (frame) + 2, IsDefault = true, Text = "_Show MessageBox"
};
app.Accepting += (s, e) =>
{
try
{
int width = int.Parse (widthEdit.Text);
int height = int.Parse (heightEdit.Text);
int numButtons = int.Parse (numButtonsEdit.Text);
int defaultButton = int.Parse (defaultButtonEdit.Text);
List<string> btns = new ();
for (var i = 0; i < numButtons; i++)
{
btns.Add ($"_{NumberToWords.Convert (i)}");
}
if (styleOptionSelector.Value == 0)
{
buttonPressedLabel.Text =
$"{MessageBox.Query (
Application.Instance, width,
height,
titleEdit.Text,
messageEdit.Text,
defaultButton,
ckbWrapMessage.CheckedState == CheckState.Checked,
btns.ToArray ()
)}";
}
else
{
buttonPressedLabel.Text =
$"{MessageBox.ErrorQuery (Application.Instance,
width,
height,
titleEdit.Text,
messageEdit.Text,
defaultButton,
ckbWrapMessage.CheckedState == CheckState.Checked,
btns.ToArray ()
)}";
}
}
catch (FormatException)
{
buttonPressedLabel.Text = "Invalid Options";
}
e.Handled = true;
};
app.Add (showMessageBoxButton);
app.Add (buttonPressedLabel);
Application.Run (app);
app.Dispose ();
Application.Shutdown ();
}
public override List<Key> GetDemoKeyStrokes ()
{
var keys = new List<Key> ();
keys.Add (Key.S.WithAlt);
keys.Add (Key.Esc);
keys.Add (Key.E.WithAlt);
keys.Add (Key.S.WithAlt);
keys.Add (Key.Esc);
keys.Add (Key.N.WithAlt);
keys.Add (Key.D5);
keys.Add (Key.S.WithAlt);
keys.Add (Key.Enter);
keys.Add (Key.T.WithAlt);
keys.Add (Key.T.WithCtrl);
keys.AddRange (GetKeysFromText ("This is a really long title"));
keys.Add (Key.M.WithAlt);
keys.Add (Key.T.WithCtrl);
keys.AddRange (GetKeysFromText ("This is a long,\nmulti-line message.\nThis is a test of the emergency\nbroadcast\nsystem."));
keys.Add (Key.S.WithAlt);
for (int i = 0; i < 10; i++)
{
keys.Add (Key.Tab);
}
keys.Add (Key.Enter);
keys.Add (Key.W.WithAlt);
keys.Add (Key.S.WithAlt);
for (int i = 0; i < 10; i++)
{
keys.Add (Key.Tab);
}
keys.Add (Key.Enter);
return keys;
}
List<Key> GetKeysFromText (string text)
{
List<Key> keys = new ();
foreach (var r in text)
{
keys.Add (r);
}
return keys;
}
}