Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V2 View cleanup #2428

Merged
merged 3 commits into from
Mar 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions Terminal.Gui/Core/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -951,13 +951,10 @@ public static RunState Begin (Toplevel toplevel)

var rs = new RunState (toplevel);

if (toplevel is ISupportInitializeNotification initializableNotification &&
!initializableNotification.IsInitialized) {
initializableNotification.BeginInit ();
initializableNotification.EndInit ();
} else if (toplevel is ISupportInitialize initializable) {
initializable.BeginInit ();
initializable.EndInit ();
// View implements ISupportInitializeNotification which is derived from ISupportInitialize
if (!toplevel.IsInitialized) {
toplevel.BeginInit ();
toplevel.EndInit ();
}

lock (toplevels) {
Expand Down Expand Up @@ -1219,7 +1216,7 @@ public static void RunMainLoopIteration (ref RunState state, bool wait, ref bool
MdiTop?.OnDeactivate (state.Toplevel);
state.Toplevel = Current;
MdiTop?.OnActivate (state.Toplevel);
Top.SetChildNeedsDisplay ();
Top.SetSubViewNeedsDisplay ();
Refresh ();
}
if (Driver.EnsureCursorVisibility ()) {
Expand Down Expand Up @@ -1283,7 +1280,7 @@ static bool MdiChildNeedsDisplay ()

foreach (var top in toplevels) {
if (top != Current && top.Visible && (!top.NeedDisplay.IsEmpty || top.ChildNeedsDisplay || top.LayoutNeeded)) {
MdiTop.SetChildNeedsDisplay ();
MdiTop.SetSubViewNeedsDisplay ();
return true;
}
}
Expand Down
29 changes: 11 additions & 18 deletions Terminal.Gui/Core/Toplevel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,6 @@ namespace Terminal.Gui {
/// and run (e.g. <see cref="Dialog"/>s. To run a Toplevel, create the <see cref="Toplevel"/> and
/// call <see cref="Application.Run(Toplevel, Func{Exception, bool})"/>.
/// </para>
/// <para>
/// Toplevels can also opt-in to more sophisticated initialization
/// by implementing <see cref="ISupportInitialize"/>. When they do
/// so, the <see cref="ISupportInitialize.BeginInit"/> and
/// <see cref="ISupportInitialize.EndInit"/> methods will be called
/// before running the view.
/// If first-run-only initialization is preferred, the <see cref="ISupportInitializeNotification"/>
/// can be implemented too, in which case the <see cref="ISupportInitialize"/>
/// methods will only be called if <see cref="ISupportInitializeNotification.IsInitialized"/>
/// is <see langword="false"/>. This allows proper <see cref="View"/> inheritance hierarchies
/// to override base class layout code optimally by doing so only on first run,
/// instead of on every run.
/// </para>
/// </remarks>
public class Toplevel : View {
/// <summary>
Expand Down Expand Up @@ -147,7 +134,7 @@ internal virtual void OnAllChildClosed ()
internal virtual void OnChildClosed (Toplevel top)
{
if (IsMdiContainer) {
SetChildNeedsDisplay ();
SetSubViewNeedsDisplay ();
}
ChildClosed?.Invoke (top);
}
Expand Down Expand Up @@ -221,6 +208,10 @@ void Initialize ()
{
ColorScheme = Colors.TopLevel;

// TODO: v2 - ALL Views (Responders??!?!) should support the commands related to
// - Focus
// Move the appropriate AddCommand calls to `Responder`

// Things this view knows how to do
AddCommand (Command.QuitToplevel, () => { QuitToplevel (); return true; });
AddCommand (Command.Suspend, () => { Driver.Suspend (); ; return true; });
Expand Down Expand Up @@ -370,7 +361,7 @@ public bool IsMdiChild {

/// <summary>
/// <see langword="true"/> if was already loaded by the <see cref="Application.Begin(Toplevel)"/>
/// <see langword="false"/>, otherwise. This is used to avoid the <see cref="View.NeedDisplay"/>
/// <see langword="false"/>, otherwise. This is used to avoid the <see cref="View._needsDisplay"/>
/// having wrong values while this was not yet loaded.
/// </summary>
public bool IsLoaded { get; private set; }
Expand Down Expand Up @@ -608,7 +599,7 @@ internal void RemoveMenuStatusBar (View view)
}

internal View EnsureVisibleBounds (Toplevel top, int x, int y,
out int nx, out int ny, out View mb, out View sb)
out int nx, out int ny, out MenuBar mb, out StatusBar sb)
{
int l;
View superView;
Expand Down Expand Up @@ -681,13 +672,14 @@ internal void PositionToplevels ()
}

/// <summary>
/// Adjusts the location and size of <paramref name="top"/> within this Toplevel.
/// Virtual method enabling implementation of specific positions for inherited <see cref="Toplevel"/> views.
/// </summary>
/// <param name="top">The toplevel.</param>
/// <param name="top">The Toplevel to adjust.</param>
public virtual void PositionToplevel (Toplevel top)
{
var superView = EnsureVisibleBounds (top, top.Frame.X, top.Frame.Y,
out int nx, out int ny, out _, out View sb);
out int nx, out int ny, out _, out StatusBar sb);
bool layoutSubviews = false;
if ((top?.SuperView != null || (top != Application.Top && top.Modal)
|| (top?.SuperView == null && top.IsMdiChild))
Expand All @@ -703,6 +695,7 @@ public virtual void PositionToplevel (Toplevel top)
}
}

// TODO: v2 - This is a hack to get the StatusBar to be positioned correctly.
if (sb != null && ny + top.Frame.Height != superView.Frame.Height - (sb.Visible ? 1 : 0)
&& top.Height is Dim.DimFill && -top.Height.Anchor (0) < 1) {

Expand Down
Loading