-
-
Notifications
You must be signed in to change notification settings - Fork 679
Expand file tree
/
Copy pathCompositionColorSource.cs
More file actions
91 lines (77 loc) · 2.56 KB
/
Copy pathCompositionColorSource.cs
File metadata and controls
91 lines (77 loc) · 2.56 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
//
// Copyright (c) Fela Ameghino 2015-2026
//
// Distributed under the GNU General Public License v3.0. (See accompanying
// file LICENSE or copy at https://www.gnu.org/licenses/gpl-3.0.txt)
//
using Telegram.Common;
using Telegram.Navigation;
using Windows.UI;
using Windows.UI.Composition;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
namespace Telegram.Composition
{
public partial class CompositionColorSource
{
private readonly CompositionColorBrush _brush;
private SolidColorBrush _newValue;
private long _token;
public static implicit operator CompositionColorBrush(CompositionColorSource d) => d._brush;
public CompositionColorSource(Brush value, bool connected)
{
if (value is SolidColorBrush newValue)
{
if (connected && _token == 0)
{
newValue.RegisterColorChangedCallback(OnColorChanged, ref _token);
}
_newValue = newValue;
_brush = BootStrapper.Current.Compositor.CreateColorBrush(newValue.Color);
}
else
{
_brush = BootStrapper.Current.Compositor.CreateColorBrush(Colors.Black);
}
}
public void PropertyChanged(SolidColorBrush newValue, bool connected)
{
if (_newValue != null && _token != 0)
{
_newValue.UnregisterPropertyChangedCallback(SolidColorBrush.ColorProperty, _token);
_token = 0;
}
if (newValue == null || _brush == null)
{
return;
}
_newValue = newValue;
_brush.Color = newValue.Color;
if (connected)
{
_newValue.RegisterColorChangedCallback(OnColorChanged, ref _token);
}
}
private void OnColorChanged(DependencyObject sender, DependencyProperty dp)
{
var newValue = sender as SolidColorBrush;
if (newValue == null || _brush == null)
{
return;
}
_brush.Color = newValue.Color;
}
public void Register()
{
if (_token == 0 && _brush != null)
{
_newValue?.RegisterColorChangedCallback(OnColorChanged, ref _token);
OnColorChanged(_newValue, SolidColorBrush.ColorProperty);
}
}
public void Unregister()
{
_newValue?.UnregisterColorChangedCallback(ref _token);
}
}
}