Skip to content

Commit 6c9df2c

Browse files
authored
Merge pull request #5445 from gui-cs/copilot/remove-dialog-setstyle
Fixes #5444 - Remove Dialog.SetStyle; set scheme and arrangement in constructor
2 parents 01a447a + d24bf12 commit 6c9df2c

4 files changed

Lines changed: 47 additions & 43 deletions

File tree

Terminal.Gui/Views/Dialog.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Terminal.Gui.Views;
1313
/// </para>
1414
/// <para>
1515
/// By default, <see cref="Dialog"/> is centered with <see cref="Dim.Auto"/> sizing and uses the
16-
/// <see cref="Schemes.Dialog"/> color scheme when running.
16+
/// <see cref="Schemes.Dialog"/> color scheme (applied at construction time, regardless of running state).
1717
/// </para>
1818
/// <para>
1919
/// To run modally, pass the dialog to <see cref="IApplication.Run(IRunnable, Func{Exception, bool})"/>.

Terminal.Gui/Views/DialogTResult.cs

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Terminal.Gui.Views;
1212
/// <remarks>
1313
/// <para>
1414
/// By default, <see cref="Dialog{TResult}"/> is centered with <see cref="Dim.Auto"/> sizing and uses the
15-
/// <see cref="Schemes.Dialog"/> color scheme when running.
15+
/// <see cref="Schemes.Dialog"/> color scheme (applied at construction time, regardless of running state).
1616
/// </para>
1717
/// <para>
1818
/// To run modally, pass the dialog to <see cref="IApplication.Run(IRunnable, Func{Exception, bool})"/>.
@@ -99,6 +99,8 @@ public Dialog ()
9999

100100
SchemeName = SchemeManager.SchemesToSchemeName (Schemes.Dialog);
101101

102+
Arrangement |= ViewArrangement.Movable | ViewArrangement.Resizable | ViewArrangement.Overlapped;
103+
102104
CommandsToBubbleUp = [Command.Accept];
103105

104106
_buttonContainer = new View
@@ -117,7 +119,6 @@ public Dialog ()
117119
Padding.GetOrCreateView ();
118120
Padding.View?.Add (_buttonContainer);
119121
UpdateSizes ();
120-
SetStyle ();
121122
}
122123

123124
/// <inheritdoc/>
@@ -334,40 +335,6 @@ public Button [] Buttons
334335
}
335336
}
336337

337-
/// <inheritdoc/>
338-
protected override void OnIsRunningChanged (bool newIsModal)
339-
{
340-
base.OnIsRunningChanged (newIsModal);
341-
342-
if (newIsModal)
343-
{
344-
SetStyle ();
345-
}
346-
}
347-
348-
private void SetStyle ()
349-
{
350-
if (IsRunning)
351-
{
352-
// When running, restore to Dialog scheme only if it was set to Base by SetStyle
353-
// (i.e., the scheme was not explicitly overridden before running, e.g. by
354-
// MessageBox.ErrorQuery which sets SchemeName = "Error" before calling app.Run).
355-
if (SchemeName == SchemeManager.SchemesToSchemeName (Schemes.Base))
356-
{
357-
SchemeName = SchemeManager.SchemesToSchemeName (Schemes.Dialog);
358-
}
359-
360-
Arrangement |= ViewArrangement.Movable | ViewArrangement.Resizable | ViewArrangement.Overlapped;
361-
}
362-
else
363-
{
364-
SchemeName = SchemeManager.SchemesToSchemeName (Schemes.Base);
365-
366-
// strip out movable and resizable
367-
Arrangement &= ~(ViewArrangement.Movable | ViewArrangement.Resizable);
368-
}
369-
}
370-
371338
// Dialogs are Modal and Focus is indicated by their Border. _drawingText ensures the
372339
// Text of the dialog (e.g. for a MessageBox) is always drawn using the Normal Attribute
373340
// instead of the Focus attribute.

Tests/UnitTestsParallelizable/Views/DialogTests.Generic.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ public void GenericInherits_Properties ()
700700
Assert.Equal (AlignmentModes.StartToEnd | AlignmentModes.AddSpaceBetweenItems, dialog.ButtonAlignmentModes);
701701
Assert.Equal (LineStyle.Heavy, dialog.BorderStyle);
702702
Assert.Equal (ShadowStyles.Transparent, dialog.ShadowStyle);
703-
Assert.Equal (ViewArrangement.Overlapped, dialog.Arrangement);
703+
Assert.Equal (ViewArrangement.Movable | ViewArrangement.Resizable | ViewArrangement.Overlapped, dialog.Arrangement);
704704

705705
dialog.Dispose ();
706706
}

Tests/UnitTestsParallelizable/Views/DialogTests.cs

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public void Constructor_Initializes_DefaultValues ()
163163
Assert.Empty (dialog.Buttons);
164164
Assert.Null (dialog.Result);
165165
Assert.True (dialog.Canceled); // Canceled is true when Result is null
166-
Assert.Equal (ViewArrangement.Overlapped, dialog.Arrangement);
166+
Assert.Equal (ViewArrangement.Movable | ViewArrangement.Resizable | ViewArrangement.Overlapped, dialog.Arrangement);
167167

168168
dialog.Dispose ();
169169
}
@@ -195,7 +195,7 @@ public void Arrangement_Default ()
195195
{
196196
Dialog dialog = new ();
197197

198-
Assert.Equal (ViewArrangement.Overlapped, dialog.Arrangement);
198+
Assert.Equal (ViewArrangement.Movable | ViewArrangement.Resizable | ViewArrangement.Overlapped, dialog.Arrangement);
199199

200200
dialog.Dispose ();
201201
}
@@ -415,12 +415,12 @@ public void ShadowStyle_Can_Be_Changed ()
415415

416416
// Copilot
417417
[Fact]
418-
public void SchemeName_IsBase_WhenNotRunning ()
418+
public void SchemeName_IsDialog_WhenNotRunning ()
419419
{
420-
// When a Dialog is not running, it should use the Base scheme (not Dialog)
420+
// A Dialog uses the Dialog scheme by default, set in the constructor
421421
Dialog dialog = new ();
422422

423-
Assert.Equal (SchemeManager.SchemesToSchemeName (Schemes.Base), dialog.SchemeName);
423+
Assert.Equal (SchemeManager.SchemesToSchemeName (Schemes.Dialog), dialog.SchemeName);
424424

425425
dialog.Dispose ();
426426
}
@@ -452,6 +452,43 @@ void AppOnIteration (object? sender, EventArgs<IApplication?> e)
452452
}
453453
}
454454

455+
// Copilot
456+
[Fact]
457+
public void Custom_SchemeName_And_Arrangement_Are_Not_Overwritten_By_Run ()
458+
{
459+
using IApplication app = Application.Create ();
460+
app.Init (DriverRegistry.Names.ANSI);
461+
462+
using Dialog dialog = new ();
463+
string customSchemeName = SchemeManager.SchemesToSchemeName (Schemes.Base)!;
464+
const ViewArrangement customArrangement = ViewArrangement.Overlapped;
465+
466+
dialog.SchemeName = customSchemeName;
467+
dialog.Arrangement = customArrangement;
468+
469+
string? schemeNameWhileRunning = null;
470+
ViewArrangement arrangementWhileRunning = default;
471+
472+
app.Iteration += AppOnIteration;
473+
app.Run (dialog);
474+
app.Iteration -= AppOnIteration;
475+
476+
Assert.Equal (customSchemeName, schemeNameWhileRunning);
477+
Assert.Equal (customArrangement, arrangementWhileRunning);
478+
Assert.Equal (customSchemeName, dialog.SchemeName);
479+
Assert.Equal (customArrangement, dialog.Arrangement);
480+
481+
return;
482+
483+
void AppOnIteration (object? sender, EventArgs<IApplication?> e)
484+
{
485+
schemeNameWhileRunning = dialog.SchemeName;
486+
arrangementWhileRunning = dialog.Arrangement;
487+
app.Iteration -= AppOnIteration;
488+
app.RequestStop ();
489+
}
490+
}
491+
455492
[Fact]
456493
public void Text_Property ()
457494
{

0 commit comments

Comments
 (0)