Skip to content

Commit 7512dc1

Browse files
committed
big Line refactoring
1 parent 4705363 commit 7512dc1

13 files changed

+205
-192
lines changed

src/System.Windows.Forms.Design/src/System/ComponentModel/Design/DesignerActionPanel.CheckBoxPropertyLine.cs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,10 @@ internal sealed partial class DesignerActionPanel
1111
{
1212
private sealed class CheckBoxPropertyLine : PropertyLine
1313
{
14-
private CheckBox? _checkBox;
14+
private readonly CheckBox _checkBox;
1515

16-
public CheckBoxPropertyLine(IServiceProvider serviceProvider, DesignerActionPanel actionPanel)
16+
private CheckBoxPropertyLine(IServiceProvider serviceProvider, DesignerActionPanel actionPanel)
1717
: base(serviceProvider, actionPanel)
18-
{
19-
}
20-
21-
protected override void AddControls(List<Control> controls)
2218
{
2319
_checkBox = new CheckBox
2420
{
@@ -30,14 +26,14 @@ protected override void AddControls(List<Control> controls)
3026
};
3127
_checkBox.CheckedChanged += new EventHandler(OnCheckBoxCheckedChanged);
3228

33-
controls.Add(_checkBox);
29+
AddedControls.Add(_checkBox);
3430
}
3531

36-
public sealed override void Focus() => _checkBox!.Focus();
32+
public override void Focus() => _checkBox.Focus();
3733

3834
public override Size LayoutControls(int top, int width, bool measureOnly)
3935
{
40-
Size checkBoxPreferredSize = _checkBox!.GetPreferredSize(new Size(int.MaxValue, int.MaxValue));
36+
Size checkBoxPreferredSize = _checkBox.GetPreferredSize(new Size(int.MaxValue, int.MaxValue));
4137
if (!measureOnly)
4238
{
4339
_checkBox.Location = new Point(LineLeftMargin, top + LineVerticalPadding / 2);
@@ -49,12 +45,12 @@ public override Size LayoutControls(int top, int width, bool measureOnly)
4945

5046
private void OnCheckBoxCheckedChanged(object? sender, EventArgs e)
5147
{
52-
SetValue(_checkBox!.Checked);
48+
SetValue(_checkBox.Checked);
5349
}
5450

5551
protected override void OnPropertyTaskItemUpdated(ToolTip toolTip, ref int currentTabIndex)
5652
{
57-
_checkBox!.Text = StripAmpersands(PropertyItem!.DisplayName);
53+
_checkBox.Text = StripAmpersands(PropertyItem!.DisplayName);
5854
_checkBox.AccessibleDescription = PropertyItem.Description;
5955
_checkBox.TabIndex = currentTabIndex++;
6056

@@ -63,7 +59,17 @@ protected override void OnPropertyTaskItemUpdated(ToolTip toolTip, ref int curre
6359

6460
protected override void OnValueChanged()
6561
{
66-
_checkBox!.Checked = (bool)Value!;
62+
_checkBox.Checked = (bool)Value!;
63+
}
64+
65+
public sealed class Info(DesignerActionList list, DesignerActionItem item) : LineInfo(list, item)
66+
{
67+
public override Line CreateLine(IServiceProvider serviceProvider, DesignerActionPanel actionPanel)
68+
{
69+
return new CheckBoxPropertyLine(serviceProvider, actionPanel);
70+
}
71+
72+
public override Type LineType => typeof(CheckBoxPropertyLine);
6773
}
6874
}
6975
}

src/System.Windows.Forms.Design/src/System/ComponentModel/Design/DesignerActionPanel.EditorPropertyLine.EditorButton.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ internal sealed class EditorButton : Button
1818
{
1919
private bool _mouseOver;
2020
private bool _mouseDown;
21-
private bool _ellipsis;
2221

2322
protected override void OnMouseDown(MouseEventArgs e)
2423
{
@@ -52,16 +51,12 @@ protected override void OnMouseUp(MouseEventArgs e)
5251
}
5352
}
5453

55-
public bool Ellipsis
56-
{
57-
get => _ellipsis;
58-
set => _ellipsis = value;
59-
}
54+
public bool Ellipsis { get; set; }
6055

6156
protected override void OnPaint(PaintEventArgs e)
6257
{
6358
Graphics g = e.Graphics;
64-
if (_ellipsis)
59+
if (Ellipsis)
6560
{
6661
PushButtonState buttonState = PushButtonState.Normal;
6762
if (_mouseDown)
@@ -162,7 +157,7 @@ protected override void OnPaint(PaintEventArgs e)
162157
}
163158
finally
164159
{
165-
icon?.Dispose();
160+
icon.Dispose();
166161
}
167162
}
168163
catch

src/System.Windows.Forms.Design/src/System/ComponentModel/Design/DesignerActionPanel.EditorPropertyLine.cs

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,22 @@ internal sealed partial class DesignerActionPanel
1414
{
1515
private sealed partial class EditorPropertyLine : TextBoxPropertyLine, IWindowsFormsEditorService, IServiceProvider
1616
{
17-
private EditorButton? _button;
17+
private readonly EditorButton _button;
1818
private UITypeEditor? _editor;
1919
private bool _hasSwatch;
2020
private Image? _swatch;
2121
private FlyoutDialog? _dropDownHolder;
2222
private bool _ignoreNextSelectChange;
2323
private bool _ignoreDropDownValue;
2424

25-
public EditorPropertyLine(IServiceProvider serviceProvider, DesignerActionPanel actionPanel)
25+
private EditorPropertyLine(IServiceProvider serviceProvider, DesignerActionPanel actionPanel)
2626
: base(serviceProvider, actionPanel)
2727
{
28+
_button = new EditorButton();
29+
_button.Click += new EventHandler(OnButtonClick);
30+
_button.GotFocus += new EventHandler(OnButtonGotFocus);
31+
32+
AddedControls.Add(_button);
2833
}
2934

3035
private unsafe void ActivateDropDown()
@@ -121,17 +126,6 @@ private unsafe void ActivateDropDown()
121126
}
122127
}
123128

124-
protected override void AddControls(List<Control> controls)
125-
{
126-
base.AddControls(controls);
127-
128-
_button = new EditorButton();
129-
_button.Click += new EventHandler(OnButtonClick);
130-
_button.GotFocus += new EventHandler(OnButtonGotFocus);
131-
132-
controls.Add(_button);
133-
}
134-
135129
private void CloseDropDown()
136130
{
137131
if (_dropDownHolder is not null)
@@ -179,7 +173,7 @@ public override Size LayoutControls(int top, int width, bool measureOnly)
179173
if (!measureOnly)
180174
{
181175
int buttonHeight = EditRegionSize.Height - EditorLineButtonPadding * 2 - 1;
182-
_button!.Location = new Point(EditRegionLocation.X + EditRegionSize.Width - buttonHeight - EditorLineButtonPadding, EditRegionLocation.Y + EditorLineButtonPadding + 1);
176+
_button.Location = new Point(EditRegionLocation.X + EditRegionSize.Width - buttonHeight - EditorLineButtonPadding, EditRegionLocation.Y + EditorLineButtonPadding + 1);
183177
_button.Size = new Size(buttonHeight, buttonHeight);
184178
}
185179

@@ -193,7 +187,7 @@ private void OnButtonClick(object? sender, EventArgs e)
193187

194188
private void OnButtonGotFocus(object? sender, EventArgs e)
195189
{
196-
if (!_button!.Ellipsis)
190+
if (!_button.Ellipsis)
197191
{
198192
Focus();
199193
}
@@ -236,12 +230,12 @@ protected override void OnPropertyTaskItemUpdated(ToolTip toolTip, ref int curre
236230

237231
if (_editor is not null)
238232
{
239-
_button!.Ellipsis = (_editor.GetEditStyle(TypeDescriptorContext) == UITypeEditorEditStyle.Modal);
233+
_button.Ellipsis = (_editor.GetEditStyle(TypeDescriptorContext) == UITypeEditorEditStyle.Modal);
240234
_hasSwatch = _editor.GetPaintValueSupported(TypeDescriptorContext);
241235
}
242236
else
243237
{
244-
_button!.Ellipsis = false;
238+
_button.Ellipsis = false;
245239
}
246240

247241
if (_button.Ellipsis)
@@ -319,7 +313,7 @@ protected internal override bool ProcessDialogKey(Keys keyData)
319313
// VS is going to eat the F4 in PreProcessMessage, preventing it from ever
320314
// getting to an OnKeyDown on this control. Doing it here also allow to not
321315
// hook up to multiple events for each button.
322-
if (!_button!.Focused && !_button.Ellipsis)
316+
if (_button is { Focused: false, Ellipsis: false })
323317
{
324318
if (keyData is (Keys.Alt | Keys.Down) or (Keys.Alt | Keys.Up) or Keys.F4)
325319
{
@@ -402,11 +396,11 @@ private void ShowDropDown(Control hostedControl, Color borderColor)
402396
ActionPanel.SetDropDownActive(true);
403397
try
404398
{
405-
_dropDownHolder.ShowDropDown(_button!);
399+
_dropDownHolder.ShowDropDown(_button);
406400
}
407401
finally
408402
{
409-
_button!.ResetMouseStates();
403+
_button.ResetMouseStates();
410404
ActionPanel.SetDropDownActive(false);
411405
ActionPanel.InMethodInvoke = false;
412406
}
@@ -479,5 +473,15 @@ protected override bool ProcessDialogKey(Keys keyData)
479473
return base.ProcessDialogKey(keyData);
480474
}
481475
}
476+
477+
public new sealed class Info(DesignerActionList list, DesignerActionItem item) : LineInfo(list, item)
478+
{
479+
public override Line CreateLine(IServiceProvider serviceProvider, DesignerActionPanel actionPanel)
480+
{
481+
return new EditorPropertyLine(serviceProvider, actionPanel);
482+
}
483+
484+
public override Type LineType => typeof(EditorPropertyLine);
485+
}
482486
}
483487
}

src/System.Windows.Forms.Design/src/System/ComponentModel/Design/DesignerActionPanel.HeaderLine.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,20 @@ internal sealed partial class DesignerActionPanel
1010
{
1111
private sealed class HeaderLine : TextLine
1212
{
13-
public HeaderLine(IServiceProvider serviceProvider, DesignerActionPanel actionPanel) : base(serviceProvider, actionPanel)
13+
private HeaderLine(IServiceProvider serviceProvider, DesignerActionPanel actionPanel) : base(serviceProvider, actionPanel)
1414
{
1515
}
1616

1717
protected override Font GetFont() => new Font(ActionPanel.Font, FontStyle.Bold);
18+
19+
public new sealed class Info(DesignerActionList list, DesignerActionItem item) : LineInfo(list, item)
20+
{
21+
public override Line CreateLine(IServiceProvider serviceProvider, DesignerActionPanel actionPanel)
22+
{
23+
return new HeaderLine(serviceProvider, actionPanel);
24+
}
25+
26+
public override Type LineType => typeof(HeaderLine);
27+
}
1828
}
1929
}

src/System.Windows.Forms.Design/src/System/ComponentModel/Design/DesignerActionPanel.Line.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ internal sealed partial class DesignerActionPanel
1111
{
1212
private abstract class Line
1313
{
14-
private List<Control>? _addedControls;
14+
protected readonly List<Control> AddedControls = new();
1515

16-
public Line(IServiceProvider serviceProvider, DesignerActionPanel actionPanel)
16+
protected Line(IServiceProvider serviceProvider, DesignerActionPanel actionPanel)
1717
{
1818
ServiceProvider = serviceProvider;
1919
ActionPanel = actionPanel.OrThrowIfNull();
@@ -28,19 +28,15 @@ public abstract string FocusId
2828

2929
protected IServiceProvider ServiceProvider { get; }
3030

31-
protected abstract void AddControls(List<Control> controls);
32-
3331
internal List<Control> GetControls()
3432
{
35-
_addedControls = new List<Control>();
36-
AddControls(_addedControls);
3733
// Tag all the controls with the Line so we know who owns it
38-
foreach (Control c in _addedControls)
34+
foreach (Control c in AddedControls)
3935
{
4036
c.Tag = this;
4137
}
4238

43-
return _addedControls;
39+
return AddedControls;
4440
}
4541

4642
public abstract void Focus();
@@ -55,9 +51,8 @@ public virtual void PaintLine(Graphics g, int lineWidth, int lineHeight)
5551

5652
internal void RemoveControls(ControlCollection controls)
5753
{
58-
for (int i = 0; i < _addedControls!.Count; i++)
54+
foreach (Control c in AddedControls)
5955
{
60-
Control c = _addedControls[i];
6156
c.Tag = null;
6257
controls.Remove(c);
6358
}

src/System.Windows.Forms.Design/src/System/ComponentModel/Design/DesignerActionPanel.LineInfo.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@ namespace System.ComponentModel.Design;
66

77
internal sealed partial class DesignerActionPanel
88
{
9-
private class LineInfo
9+
private abstract class LineInfo
1010
{
11-
public readonly Line Line;
1211
public readonly DesignerActionItem? Item;
1312
public readonly DesignerActionList? List;
1413

15-
public LineInfo(DesignerActionList? list, DesignerActionItem? item, Line line)
14+
protected LineInfo(DesignerActionList? list, DesignerActionItem? item)
1615
{
17-
Debug.Assert(line is not null);
18-
Line = line;
1916
Item = item;
2017
List = list;
2118
}
19+
20+
public abstract Line CreateLine(IServiceProvider serviceProvider, DesignerActionPanel actionPanel);
21+
public abstract Type LineType { get; }
2222
}
2323
}

src/System.Windows.Forms.Design/src/System/ComponentModel/Design/DesignerActionPanel.MethodLine.cs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,8 @@ private sealed class MethodLine : Line
1414
{
1515
private DesignerActionList? _actionList;
1616
private DesignerActionMethodItem? _methodItem;
17-
private MethodItemLinkLabel? _linkLabel;
18-
public MethodLine(IServiceProvider serviceProvider, DesignerActionPanel actionPanel) : base(serviceProvider, actionPanel)
19-
{
20-
}
21-
22-
public override string FocusId
23-
{
24-
get => $"METHOD:{_actionList!.GetType().FullName}.{_methodItem!.MemberName}";
25-
}
26-
27-
protected override void AddControls(List<Control> controls)
17+
private readonly MethodItemLinkLabel _linkLabel;
18+
private MethodLine(IServiceProvider serviceProvider, DesignerActionPanel actionPanel) : base(serviceProvider, actionPanel)
2819
{
2920
_linkLabel = new MethodItemLinkLabel
3021
{
@@ -38,17 +29,19 @@ protected override void AddControls(List<Control> controls)
3829
VisitedLinkColor = ActionPanel.LinkColor
3930
};
4031
_linkLabel.LinkClicked += new LinkLabelLinkClickedEventHandler(OnLinkLabelLinkClicked);
41-
controls.Add(_linkLabel);
32+
AddedControls.Add(_linkLabel);
4233
}
4334

35+
public override string FocusId => $"METHOD:{_actionList!.GetType().FullName}.{_methodItem!.MemberName}";
36+
4437
public override void Focus()
4538
{
46-
_linkLabel!.Focus();
39+
_linkLabel.Focus();
4740
}
4841

4942
public override Size LayoutControls(int top, int width, bool measureOnly)
5043
{
51-
Size linkLabelSize = _linkLabel!.GetPreferredSize(new Size(int.MaxValue, int.MaxValue));
44+
Size linkLabelSize = _linkLabel.GetPreferredSize(new Size(int.MaxValue, int.MaxValue));
5245
if (!measureOnly)
5346
{
5447
_linkLabel.Location = new Point(LineLeftMargin, top + LineVerticalPadding / 2);
@@ -88,11 +81,11 @@ private void OnLinkLabelLinkClicked(object? sender, LinkLabelLinkClickedEventArg
8881

8982
internal override void UpdateActionItem(DesignerActionList? actionList, DesignerActionItem? actionItem, ToolTip toolTip, ref int currentTabIndex)
9083
{
91-
_actionList = actionList;
92-
_methodItem = (DesignerActionMethodItem?)actionItem;
93-
toolTip.SetToolTip(_linkLabel!, _methodItem!.Description);
94-
_linkLabel!.Text = StripAmpersands(_methodItem.DisplayName);
95-
_linkLabel.AccessibleDescription = actionItem!.Description;
84+
_actionList = actionList!;
85+
_methodItem = (DesignerActionMethodItem)actionItem!;
86+
toolTip.SetToolTip(_linkLabel, _methodItem.Description);
87+
_linkLabel.Text = StripAmpersands(_methodItem.DisplayName);
88+
_linkLabel.AccessibleDescription = _methodItem.Description;
9689
_linkLabel.TabIndex = currentTabIndex++;
9790
}
9891

@@ -114,5 +107,15 @@ protected override bool ProcessDialogKey(Keys keyData)
114107
return base.ProcessDialogKey(keyData);
115108
}
116109
}
110+
111+
public sealed class Info(DesignerActionList list, DesignerActionItem item) : LineInfo(list, item)
112+
{
113+
public override Line CreateLine(IServiceProvider serviceProvider, DesignerActionPanel actionPanel)
114+
{
115+
return new MethodLine(serviceProvider, actionPanel);
116+
}
117+
118+
public override Type LineType => typeof(MethodLine);
119+
}
117120
}
118121
}

0 commit comments

Comments
 (0)