Skip to content

Commit a19b97b

Browse files
authored
Merge pull request #305 from gui-cs/multi-line
Add checkbox for typing newlines into GetTextDialog
2 parents cf5c192 + 42265ad commit a19b97b

2 files changed

Lines changed: 75 additions & 12 deletions

File tree

src/UI/Windows/DialogArgs.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ public class DialogArgs
3232
/// </summary>
3333
public bool MultiLine { get; set; }
3434

35+
/// <summary>
36+
/// True to make newlines toggleable (e.g. for Label).
37+
/// </summary>
38+
public bool ToggleableMultiLine { get; set; } = true;
39+
3540
/// <inheritdoc/>
3641
public override string ToString()
3742
{

src/UI/Windows/GetTextDialog.cs

Lines changed: 70 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ internal class GetTextDialog
1010
private readonly DialogArgs args;
1111
private readonly string? initialValue;
1212
private readonly Window win;
13-
private readonly TextView textField;
13+
private readonly TextView textView;
1414
private bool okClicked = false;
15+
private static CheckState lastKnownEnableNewlines = CheckState.UnChecked;
16+
private bool? multiLineChecked;
1517

1618
public GetTextDialog(DialogArgs args, string? initialValue)
1719
{
@@ -42,7 +44,7 @@ public GetTextDialog(DialogArgs args, string? initialValue)
4244

4345
this.win.Add(entryLabel);
4446

45-
this.textField = new TextView()
47+
this.textView = new TextView()
4648
{
4749
X = 1,
4850
Y = Pos.Bottom(entryLabel),
@@ -51,19 +53,29 @@ public GetTextDialog(DialogArgs args, string? initialValue)
5153
Text = this.initialValue ?? string.Empty,
5254
AllowsTab = false,
5355
};
54-
this.textField.KeyDown += this.TextField_KeyPress;
56+
this.textView.KeyDown += this.TextViewKeyPress;
5557

5658
// make it easier for user to replace this text with something else
5759
// by directly selecting it all so next keypress replaces text
58-
this.textField.SelectAll();
60+
this.textView.SelectAll();
5961

60-
this.win.Add(this.textField);
62+
this.win.Add(this.textView);
63+
64+
if (args.MultiLine)
65+
{
66+
SetupMultiLineForced();
67+
}
68+
else
69+
if (args.ToggleableMultiLine)
70+
{
71+
SetupMultiLineOptional();
72+
}
6173

6274
var btnOk = new Button()
6375
{
6476
Text = "Ok",
6577
X = 0,
66-
Y = Pos.Bottom(this.textField),
78+
Y = Pos.Bottom(this.textView),
6779
IsDefault = !this.args.MultiLine,
6880
};
6981
btnOk.Accept += (s, e) =>
@@ -75,7 +87,7 @@ public GetTextDialog(DialogArgs args, string? initialValue)
7587
{
7688
Text = "Cancel",
7789
X = Pos.Right(btnOk),
78-
Y = Pos.Bottom(this.textField),
90+
Y = Pos.Bottom(this.textView),
7991
IsDefault = false,
8092
};
8193
btnCancel.Accept += (s, e) =>
@@ -88,18 +100,59 @@ public GetTextDialog(DialogArgs args, string? initialValue)
88100
{
89101
Text = "Clear",
90102
X = Pos.Right(btnCancel),
91-
Y = Pos.Bottom(this.textField),
103+
Y = Pos.Bottom(this.textView),
92104
};
93105
btnClear.Accept += (s, e) =>
94106
{
95-
this.textField.Text = string.Empty;
107+
this.textView.Text = string.Empty;
96108
};
97109

98110
this.win.Add(btnOk);
99111
this.win.Add(btnCancel);
100112
this.win.Add(btnClear);
101113
}
102114

115+
private void SetupMultiLineForced()
116+
{
117+
var cbMultiLine = new CheckBox()
118+
{
119+
Text = "Enable Newlines",
120+
X = Pos.AnchorEnd(),
121+
CheckedState = CheckState.Checked,
122+
Enabled = false
123+
};
124+
win.Add(cbMultiLine);
125+
}
126+
127+
private void SetupMultiLineOptional()
128+
{
129+
// Initial state
130+
SetEnableNewlines();
131+
132+
var cbMultiLine = new CheckBox()
133+
{
134+
Text = "Enable Newlines",
135+
X = Pos.AnchorEnd(),
136+
CheckedState = lastKnownEnableNewlines
137+
};
138+
cbMultiLine.CheckedStateChanging += (s, e) =>
139+
{
140+
SetEnableNewlines(e.NewValue);
141+
};
142+
win.Add(cbMultiLine);
143+
}
144+
145+
private void SetEnableNewlines()
146+
{
147+
SetEnableNewlines(lastKnownEnableNewlines);
148+
}
149+
150+
private void SetEnableNewlines(CheckState newValue)
151+
{
152+
lastKnownEnableNewlines = newValue;
153+
multiLineChecked = textView.AllowsReturn = newValue == CheckState.Checked;
154+
}
155+
103156
public string? ResultText { get; set; }
104157

105158
public bool ShowDialog()
@@ -112,16 +165,21 @@ public bool ShowDialog()
112165
private void Accept()
113166
{
114167
this.okClicked = true;
115-
this.ResultText = this.textField.Text.ToString();
168+
this.ResultText = this.textView.Text.ToString();
116169
Application.RequestStop();
117170
}
118171

119-
private void TextField_KeyPress(object? sender, Key key)
172+
private void TextViewKeyPress(object? sender, Key key)
120173
{
121-
if (key == Key.Enter && !this.args.MultiLine)
174+
if (key == Key.Enter && !IsMultiLine())
122175
{
123176
this.Accept();
124177
key.Handled = true;
125178
}
126179
}
180+
181+
private bool IsMultiLine()
182+
{
183+
return this.args.MultiLine || (multiLineChecked ?? false);
184+
}
127185
}

0 commit comments

Comments
 (0)