-
Notifications
You must be signed in to change notification settings - Fork 475
Expand file tree
/
Copy pathBookmarkSelector.cs
More file actions
106 lines (90 loc) · 3.65 KB
/
Copy pathBookmarkSelector.cs
File metadata and controls
106 lines (90 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System;
using System.Collections.Generic;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Media;
namespace SourceGit.Views
{
public class BookmarkSelector : Control
{
public static readonly DirectProperty<BookmarkSelector, int> BookmarkProperty =
AvaloniaProperty.RegisterDirect<BookmarkSelector, int>(
nameof(Bookmark),
static o => o.Bookmark,
static (o, v) => o.Bookmark = v);
public int Bookmark
{
get => _bookmark;
set => SetAndRaise(BookmarkProperty, ref _bookmark, value);
}
public BookmarkSelector()
{
var geo = Application.Current!.FindResource("Icons.Bookmark") as StreamGeometry;
_icon = geo!.Clone();
var iconBounds = _icon.Bounds;
var translation = Matrix.CreateTranslation(-(Vector)iconBounds.Position);
var scale = Math.Min(14.0 / iconBounds.Width, 14.0 / iconBounds.Height);
var transform = translation * Matrix.CreateScale(scale, scale);
if (_icon.Transform == null || _icon.Transform.Value == Matrix.Identity)
_icon.Transform = new MatrixTransform(transform);
else
_icon.Transform = new MatrixTransform(_icon.Transform.Value * transform);
var x = 2.0;
for (var i = 0; i < Models.Bookmarks.Brushes.Length; i++)
{
var hitBox = new Rect(x - 2.5, 2.5, 18, 20);
_hitBoxes.Add(hitBox);
x += 26;
}
}
public override void Render(DrawingContext context)
{
// Just enable clicking anywhere in the control.
context.FillRectangle(Brushes.Transparent, new Rect(0, 0, Bounds.Width, Bounds.Height));
var defaultBrush = this.FindResource("Brush.FG1") as IBrush;
var selectedBorder = new Pen(new SolidColorBrush((Color)this.FindResource("SystemAccentColor")!));
for (var i = 0; i < _hitBoxes.Count; i++)
{
var hitBox = _hitBoxes[i];
if (i == _bookmark)
context.DrawRectangle(selectedBorder, hitBox, 3);
var bursh = Models.Bookmarks.Get(i) ?? defaultBrush;
using (context.PushTransform(Matrix.CreateTranslation(hitBox.X + 3, 5)))
context.DrawGeometry(bursh, null, _icon);
}
}
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
if (change.Property == BookmarkProperty)
InvalidateVisual();
else if (change.Property.Name == nameof(ActualThemeVariant) && change.NewValue != null)
InvalidateVisual();
}
protected override void OnPointerPressed(PointerPressedEventArgs e)
{
base.OnPointerPressed(e);
if (e.GetCurrentPoint(this).Properties.IsLeftButtonPressed)
{
var pos = e.GetPosition(this);
for (var i = 0; i < _hitBoxes.Count; i++)
{
if (_hitBoxes[i].Contains(pos))
{
Bookmark = i;
break;
}
}
e.Handled = true;
}
}
protected override Size MeasureOverride(Size availableSize)
{
return new Size(8 * 14 + 7 * 12 + 4, 24);
}
private int _bookmark = 0;
private Geometry _icon = null;
private List<Rect> _hitBoxes = [];
}
}