-
Notifications
You must be signed in to change notification settings - Fork 475
Expand file tree
/
Copy pathBookmark.cs
More file actions
66 lines (57 loc) · 2.24 KB
/
Copy pathBookmark.cs
File metadata and controls
66 lines (57 loc) · 2.24 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
using System;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Media;
namespace SourceGit.Views
{
public class Bookmark : Control
{
public static readonly DirectProperty<Bookmark, int> ValueProperty =
AvaloniaProperty.RegisterDirect<Bookmark, int>(
nameof(Value),
static o => o.Value,
static (o, v) => o.Value = v);
public int Value
{
get => _value;
set => SetAndRaise(ValueProperty, ref _value, value);
}
public Bookmark()
{
IsHitTestVisible = false;
}
public override void Render(DrawingContext context)
{
if (_icon == null)
LoadIcon();
var brush = Models.Bookmarks.Get(_value) ?? (this.FindResource("Brush.FG1") as IBrush);
var startX = (Bounds.Width - 12.0) * 0.5;
var startY = (Bounds.Height - 12.0) * 0.5;
using (context.PushTransform(Matrix.CreateTranslation(startX, startY)))
context.DrawGeometry(brush, null, _icon);
}
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
if (change.Property == ValueProperty)
InvalidateVisual();
else if (change.Property.Name == nameof(ActualThemeVariant) && change.NewValue != null)
InvalidateVisual();
}
private void LoadIcon()
{
var geo = this.FindResource("Icons.Bookmark") as StreamGeometry;
_icon = geo!.Clone();
var iconBounds = _icon.Bounds;
var translation = Matrix.CreateTranslation(-(Vector)iconBounds.Position);
var scale = Math.Min(12.0 / iconBounds.Width, 12.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);
}
private int _value = 0;
private Geometry _icon = null;
}
}