Skip to content

Commit 823d9c9

Browse files
committed
优化代码长度
1 parent 42030e5 commit 823d9c9

1 file changed

Lines changed: 33 additions & 108 deletions

File tree

ContextMenuManager/BluePointLilac.Controls/RComboBox.cs

Lines changed: 33 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -182,19 +182,13 @@ private void InitEvents()
182182
MouseLeave += (s, e) => { mouseOverDropDown = false; UpdateState(); };
183183
MouseMove += (s, e) => UpdateDropDownHoverState(e.Location);
184184

185-
SelectedIndexChanged += (s, e) =>
186-
{
187-
if (AutoSize) AdjustWidth();
188-
};
185+
SelectedIndexChanged += (s, e) => { if (AutoSize) AdjustWidth(); };
189186
TextChanged += (s, e) => { if (AutoSize) AdjustWidth(); };
190187

191188
DropDown += RComboBox_DropDown;
192-
DropDownClosed += (s, e) =>
193-
{
194-
originalSelectedIndex = -1;
189+
DropDownClosed += (s, e) => {
190+
originalSelectedIndex = animatedIndex = previousAnimatedIndex = -1;
195191
dropDownHwnd = IntPtr.Zero;
196-
animatedIndex = -1;
197-
previousAnimatedIndex = -1;
198192
};
199193

200194
animTimer.Tick += AnimTimer_Tick;
@@ -290,30 +284,19 @@ protected override void OnMeasureItem(MeasureItemEventArgs e)
290284
private void RComboBox_DropDown(object sender, EventArgs e)
291285
{
292286
originalSelectedIndex = SelectedIndex;
293-
var totalHeight = 0;
294-
for (var i = 0; i < Items.Count; i++)
295-
{
296-
totalHeight += DropDownItemHeight.DpiZoom();
297-
}
298-
DropDownHeight = totalHeight + 2 * SystemInformation.BorderSize.Height;
287+
DropDownHeight = Items.Count * DropDownItemHeight.DpiZoom() + 2 * SystemInformation.BorderSize.Height;
299288

300289
if (Parent.FindForm() is Form form)
301-
{
302290
form.BeginInvoke(() =>
303291
{
304-
var cbi = new COMBOBOXINFO();
305-
cbi.cbSize = Marshal.SizeOf(cbi);
292+
var cbi = new COMBOBOXINFO { cbSize = Marshal.SizeOf<COMBOBOXINFO>() };
306293
GetComboBoxInfo(Handle, ref cbi);
307294
dropDownHwnd = cbi.hwndList;
308295
var r = new RECT();
309296
GetWindowRect(cbi.hwndList, ref r);
310297
var h = CreateRoundRectRgn(0, 0, r.Right - r.Left, r.Bottom - r.Top, BorderRadius.DpiZoom(), BorderRadius.DpiZoom());
311-
if (SetWindowRgn(cbi.hwndList, h, true) == 0)
312-
{
313-
throw new Win32Exception(Marshal.GetLastWin32Error());
314-
}
298+
if (SetWindowRgn(cbi.hwndList, h, true) == 0) throw new Win32Exception(Marshal.GetLastWin32Error());
315299
});
316-
}
317300
}
318301

319302
// 自定义绘制下拉列表中的每个项。
@@ -323,11 +306,8 @@ protected override void OnDrawItem(DrawItemEventArgs e)
323306
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
324307

325308
var bounds = e.Bounds;
326-
bool isActuallySelected = (e.State & DrawItemState.Selected) == DrawItemState.Selected;
327-
if (!isActuallySelected && DroppedDown && animatedIndex == -1 && e.Index == originalSelectedIndex)
328-
{
329-
isActuallySelected = true;
330-
}
309+
bool isActuallySelected = (e.State & DrawItemState.Selected) == DrawItemState.Selected ||
310+
(DroppedDown && animatedIndex == -1 && e.Index == originalSelectedIndex);
331311

332312
var textFont = DropDownFont ?? Font;
333313
var textColor = DropDownForeColor;
@@ -407,20 +387,9 @@ internal struct COMBOBOXINFO
407387

408388

409389

410-
// 在两种颜色之间进行线性插值。
411-
// c1: 起始颜色。
412-
// c2: 结束颜色。
413-
// t: 插值因子,范围从 0.0 到 1.0。
414-
// returns: 插值后的颜色。
415-
private static Color ColorLerp(Color c1, Color c2, float t)
416-
{
417-
return Color.FromArgb(
418-
(int)(c1.A + (c2.A - c1.A) * t),
419-
(int)(c1.R + (c2.R - c1.R) * t),
420-
(int)(c1.G + (c2.G - c1.G) * t),
421-
(int)(c1.B + (c2.B - c1.B) * t)
422-
);
423-
}
390+
private static Color ColorLerp(Color c1, Color c2, float t) => Color.FromArgb(
391+
(int)(c1.A + (c2.A - c1.A) * t), (int)(c1.R + (c2.R - c1.R) * t),
392+
(int)(c1.G + (c2.G - c1.G) * t), (int)(c1.B + (c2.B - c1.B) * t));
424393

425394
// 当系统主题(深色/浅色模式)更改时,更新控件的颜色。
426395
public void UpdateColors()
@@ -444,50 +413,27 @@ public void UpdateColors()
444413
// 根据控件的焦点和鼠标悬停状态更新边框的外观。
445414
private void UpdateState()
446415
{
447-
if (focused)
448-
{
449-
targetBorder = FocusColor;
450-
targetWidth = 2f;
451-
}
452-
else if (mouseOverDropDown || ClientRectangle.Contains(PointToClient(MousePosition)))
453-
{
454-
targetBorder = HoverColor;
455-
targetWidth = 2f;
456-
}
457-
else
458-
{
459-
targetBorder = DarkModeHelper.ComboBoxBorder;
460-
targetWidth = 1.2f;
461-
}
416+
bool hover = mouseOverDropDown || ClientRectangle.Contains(PointToClient(MousePosition));
417+
targetBorder = focused ? FocusColor : (hover ? HoverColor : DarkModeHelper.ComboBoxBorder);
418+
targetWidth = focused || hover ? 2f : 1.2f;
462419
Invalidate();
463420
}
464421

465422
// 更新下拉按钮的悬停状态,并相应地更改光标。
466-
// location: 当前鼠标在控件内的坐标。
467423
private void UpdateDropDownHoverState(Point location)
468424
{
469-
var dropDownRect = GetDropDownButtonRect();
470-
var wasHovered = mouseOverDropDown;
471-
mouseOverDropDown = dropDownRect.Contains(location);
472-
if (wasHovered != mouseOverDropDown)
473-
{
474-
Cursor = mouseOverDropDown ? Cursors.Hand : Cursors.Default;
475-
UpdateState();
476-
}
425+
bool hover = GetDropDownButtonRect().Contains(location);
426+
if (mouseOverDropDown == hover) return;
427+
mouseOverDropDown = hover;
428+
Cursor = hover ? Cursors.Hand : Cursors.Default;
429+
UpdateState();
477430
}
478431

479432
// 获取下拉按钮的矩形区域。
480-
// returns: 表示下拉按钮区域的矩形。
481433
private Rectangle GetDropDownButtonRect()
482434
{
483-
var clientRect = ClientRectangle;
484-
var dropDownButtonWidth = SystemInformation.HorizontalScrollBarArrowWidth + 8.DpiZoom();
485-
var dropDownRect = new Rectangle(
486-
clientRect.Right - dropDownButtonWidth,
487-
clientRect.Top,
488-
dropDownButtonWidth,
489-
clientRect.Height);
490-
return dropDownRect;
435+
int w = SystemInformation.HorizontalScrollBarArrowWidth + 8.DpiZoom();
436+
return new Rectangle(ClientRectangle.Right - w, ClientRectangle.Top, w, ClientRectangle.Height);
491437
}
492438

493439
// 根据当前选择的项或文本内容调整控件的宽度。
@@ -525,10 +471,7 @@ protected override void OnFontChanged(EventArgs e)
525471
}
526472

527473
// 处理主题更改事件,更新控件颜色。
528-
private void OnThemeChanged(object sender, EventArgs e)
529-
{
530-
UpdateColors();
531-
}
474+
private void OnThemeChanged(object sender, EventArgs e) => UpdateColors();
532475

533476
// 释放资源。
534477
protected override void Dispose(bool disposing)
@@ -571,41 +514,23 @@ protected override void OnPaint(PaintEventArgs e)
571514
TextFormatFlags.VerticalCenter | TextFormatFlags.Left | TextFormatFlags.EndEllipsis | TextFormatFlags.NoPadding);
572515

573516
// 绘制箭头
574-
var dropDownRect = GetDropDownButtonRect();
575-
var currentArrowColor = mouseOverDropDown || focused ? FocusColor : ArrowColor;
576-
577-
var middle = new Point(
578-
dropDownRect.Left + dropDownRect.Width / 2,
579-
dropDownRect.Top + dropDownRect.Height / 2
580-
);
581-
582-
var arrowSize = 6.DpiZoom();
583-
var arrowPoints = new Point[]
584-
{
585-
new(middle.X - arrowSize, middle.Y - arrowSize / 2),
586-
new(middle.X + arrowSize, middle.Y - arrowSize / 2),
587-
new(middle.X, middle.Y + arrowSize / 2)
588-
};
589-
590-
using var brush2 = new SolidBrush(currentArrowColor);
591-
e.Graphics.FillPolygon(brush2, arrowPoints);
517+
var btnRect = GetDropDownButtonRect();
518+
var center = new Point(btnRect.Left + btnRect.Width / 2, btnRect.Top + btnRect.Height / 2);
519+
int s = 6.DpiZoom();
520+
using var arrowBrush = new SolidBrush(mouseOverDropDown || focused ? FocusColor : ArrowColor);
521+
e.Graphics.FillPolygon(arrowBrush, new Point[] {
522+
new(center.X - s, center.Y - s / 2),
523+
new(center.X + s, center.Y - s / 2),
524+
new(center.X, center.Y + s / 2)
525+
});
592526
}
593527

594528
// 在 UI 线程上安全地执行操作。
595-
// action: 要执行的操作。
596529
private void SafeInvoke(Action action)
597530
{
598531
if (IsHandleCreated)
599-
{
600-
if (InvokeRequired)
601-
{
602-
Invoke(action);
603-
}
604-
else
605-
{
606-
action();
607-
}
608-
}
532+
if (InvokeRequired) Invoke(action);
533+
else action();
609534
}
610535
}
611536

0 commit comments

Comments
 (0)