enhance: ColorPicker performance improvement

This commit is contained in:
leo 2024-09-11 10:34:27 +08:00
parent f4f0fe788b
commit 0c31c982be
No known key found for this signature in database

View file

@ -94,26 +94,24 @@ namespace SourceGit.Views
// Color table. // Color table.
{ {
// Colors
for (int i = 0; i < 6; i++) for (int i = 0; i < 6; i++)
{ {
for (int j = 0; j < 8; j++) for (int j = 0; j < 8; j++)
{ context.FillRectangle(new SolidColorBrush(COLOR_TABLE[i, j]), new Rect(j * 32, i * 32, 32, 32));
var idx = i * 8 + j;
var x = j * 32.0;
var y = i * 32.0;
context.FillRectangle(new SolidColorBrush(COLOR_TABLE[i, j]), new Rect(x, y, 32, 32));
if (idx == _hightlightedTableElement)
context.DrawRectangle(new Pen(Brushes.White, 2), new Rect(x + 2, y + 2, 28, 28));
}
} }
// Borders
var border = this.FindResource("Brush.Border0") as IBrush; var border = this.FindResource("Brush.Border0") as IBrush;
var pen = new Pen(border, 0.4); var pen = new Pen(border, 0.4);
for (int i = 1; i < 6; i++) for (int i = 1; i < 6; i++)
context.DrawLine(pen, new Point(0, i * 32), new Point(256, i * 32)); context.DrawLine(pen, new Point(0, i * 32), new Point(256, i * 32));
for (int j = 1; j < 8; j++) for (int j = 1; j < 8; j++)
context.DrawLine(pen, new Point(j * 32, 0), new Point(j * 32, 192)); context.DrawLine(pen, new Point(j * 32, 0), new Point(j * 32, 192));
// Selected
if (_hightlightedTableRect is { } rect)
context.DrawRectangle(new Pen(Brushes.White, 2), rect);
} }
// Palette picker // Palette picker
@ -142,6 +140,12 @@ namespace SourceGit.Views
} }
} }
protected override void OnDataContextChanged(EventArgs e)
{
base.OnDataContextChanged(e);
_hightlightedTableRect = null;
}
protected override Size MeasureOverride(Size availableSize) protected override Size MeasureOverride(Size availableSize)
{ {
return new Size(256, 256); return new Size(256, 256);
@ -154,12 +158,12 @@ namespace SourceGit.Views
var p = e.GetPosition(this); var p = e.GetPosition(this);
if (_colorTableRect.Contains(p)) if (_colorTableRect.Contains(p))
{ {
int col = (int)Math.Floor(p.X / 32.0); var col = (int)Math.Floor(p.X / 32.0);
int row = (int)Math.Floor(p.Y / 32.0); var row = (int)Math.Floor(p.Y / 32.0);
int idx = row * 8 + col; var rect = new Rect(col * 32 + 2, row * 32 + 2, 28, 28);
if (_hightlightedTableElement != idx) if (!rect.Equals(_hightlightedTableRect))
{ {
_hightlightedTableElement = idx; _hightlightedTableRect = rect;
SetCurrentValue(ValueProperty, COLOR_TABLE[row, col].ToUInt32()); SetCurrentValue(ValueProperty, COLOR_TABLE[row, col].ToUInt32());
} }
@ -168,32 +172,32 @@ namespace SourceGit.Views
if (_darkestRect.Rect.Contains(p)) if (_darkestRect.Rect.Contains(p))
{ {
_hightlightedTableElement = -1; _hightlightedTableRect = null;
SetCurrentValue(ValueProperty, _darkestColor.ToUInt32()); SetCurrentValue(ValueProperty, _darkestColor.ToUInt32());
} }
else if (_darkerRect.Contains(p)) else if (_darkerRect.Contains(p))
{ {
_hightlightedTableElement = -1; _hightlightedTableRect = null;
SetCurrentValue(ValueProperty, _darkerColor.ToUInt32()); SetCurrentValue(ValueProperty, _darkerColor.ToUInt32());
} }
else if (_darkRect.Contains(p)) else if (_darkRect.Contains(p))
{ {
_hightlightedTableElement = -1; _hightlightedTableRect = null;
SetCurrentValue(ValueProperty, _darkColor.ToUInt32()); SetCurrentValue(ValueProperty, _darkColor.ToUInt32());
} }
else if (_lightRect.Contains(p)) else if (_lightRect.Contains(p))
{ {
_hightlightedTableElement = -1; _hightlightedTableRect = null;
SetCurrentValue(ValueProperty, _lightColor.ToUInt32()); SetCurrentValue(ValueProperty, _lightColor.ToUInt32());
} }
else if (_lighterRect.Contains(p)) else if (_lighterRect.Contains(p))
{ {
_hightlightedTableElement = -1; _hightlightedTableRect = null;
SetCurrentValue(ValueProperty, _lighterColor.ToUInt32()); SetCurrentValue(ValueProperty, _lighterColor.ToUInt32());
} }
else if (_lightestRect.Rect.Contains(p)) else if (_lightestRect.Rect.Contains(p))
{ {
_hightlightedTableElement = -1; _hightlightedTableRect = null;
SetCurrentValue(ValueProperty, _lightestColor.ToUInt32()); SetCurrentValue(ValueProperty, _lightestColor.ToUInt32());
} }
} }
@ -230,8 +234,7 @@ namespace SourceGit.Views
private Rect _lightRect = new Rect(160, 200, 32, 32); private Rect _lightRect = new Rect(160, 200, 32, 32);
private Rect _lighterRect = new Rect(192, 200, 32, 32); private Rect _lighterRect = new Rect(192, 200, 32, 32);
private RoundedRect _lightestRect = new RoundedRect(new Rect(224, 200, 32, 32), new CornerRadius(0, 4, 4, 0)); private RoundedRect _lightestRect = new RoundedRect(new Rect(224, 200, 32, 32), new CornerRadius(0, 4, 4, 0));
private Rect? _hightlightedTableRect = null;
private int _hightlightedTableElement = -1;
private Color _darkestColor; private Color _darkestColor;
private Color _darkerColor; private Color _darkerColor;