-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVirtualDesktopUiSettings.cs
More file actions
executable file
·193 lines (173 loc) · 6.62 KB
/
VirtualDesktopUiSettings.cs
File metadata and controls
executable file
·193 lines (173 loc) · 6.62 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace VirtualDesktopTimecodeServer
{
public sealed class VirtualDesktopUiSettings
{
public static VirtualDesktopUiSettings Instance
{
get
{
return lazy.Value;
}
}
public double VideoPositionInSeconds
{
get
{
return _videoPositionInSeconds;
}
}
public string VideoPositionInSecondsString
{
get
{
return String
.Format("{0:0.000}", _videoPositionInSeconds)
.Replace(",", ".");
}
}
public bool IsPlaying
{
get
{
return _isPlaying;
}
}
public double PlayerSpeed
{
get
{
return _playerSpeed;
}
}
public void Update()
{
if (_videoPositionTextBlock is null)
{
_videoPositionTextBlock = GetElementByTreePath<TextBlock>(
Application.Current.MainWindow,
"Border.AdornerDecorator.ContentPresenter.Grid.Border.DockPanel.Grid.VideoPlayerPanel.Border.ContentPresenter.Grid.DockPanel.StackPanel.Grid.TextBlock",
o => o.Name == ""
);
}
if (_playButton is null)
{
_playButton = GetElementByTreePath<Button>(
Application.Current.MainWindow,
"Border.AdornerDecorator.ContentPresenter.Grid.Border.DockPanel.Grid.VideoPlayerPanel.Border.ContentPresenter.Grid.DockPanel.StackPanel.Grid.StackPanel.Grid.Button",
o => o.Name == "playButton"
);
}
if (_playerSpeedTextBlock is null)
{
_playerSpeedTextBlock = GetElementByTreePath<TextBlock>(
Application.Current.MainWindow,
"Border.AdornerDecorator.ContentPresenter.Grid.Border.DockPanel.Grid.VideoPlayerPanel.Border.ContentPresenter.Grid.DockPanel.StackPanel.Grid.StackPanel.DropDownButton.Grid.ContentControl.ContentPresenter.ContentPresenter.TextBlock",
o => true
);
}
if (_playerSpeedTextBlock is not null)
{
_playerSpeed = Convert.ToDouble(
_playerSpeedTextBlock
.Text
.ToLower()
.Replace("x", "")
);
}
if (_videoPositionTextBlock is not null)
{
SetVideoPosition(_videoPositionTextBlock.Text);
}
if (_playButton is not null)
{
_isPlaying = !_playButton.IsEnabled;
}
}
private VirtualDesktopUiSettings()
{
Update();
}
private void SetVideoPosition(string videoPosition)
{
if (_lastVideoPositionString != videoPosition)
{
_lastVideoPositionString = videoPosition;
_breakEvenTime = DateTime.Now;
}
// TODO: implement for playersped != 1.0
double interpolatedTimestamp = 0;
if (videoPosition.Split(':').Length == 3)
{
int hours = Convert.ToInt32(videoPosition.Split(':')[0]);
int minutes = Convert.ToInt32(videoPosition.Split(':')[1]);
int seconds = Convert.ToInt32(videoPosition.Split(':')[2]);
interpolatedTimestamp = (hours * 3600.0 + minutes * 60.0 + seconds) * 1000.0;
}
else if (videoPosition.Split(':').Length == 2)
{
int minutes = Convert.ToInt32(videoPosition.Split(':')[0]);
int seconds = Convert.ToInt32(videoPosition.Split(':')[1]);
interpolatedTimestamp = (minutes * 60.0 + seconds) * 1000.0;
}
else
{
return;
}
interpolatedTimestamp += ((TimeSpan)(DateTime.Now - _breakEvenTime)).TotalMilliseconds;
_videoPositionInSeconds = interpolatedTimestamp / 1000.0;
}
private static TControlType GetElementByTreePath<TControlType>(
DependencyObject parent,
String treePath,
Predicate<TControlType> predicate,
int pos = 0)
where TControlType : DependencyObject
{
var childrenCount = VisualTreeHelper.GetChildrenCount(parent);
for (var i = 0; i < childrenCount; i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
string[] elements = treePath.Split('.');
if (elements.Length > pos)
{
String searchElement = elements[pos];
if (child.ToString().Split(' ')[0].EndsWith(searchElement))
{
if (elements.Length == pos + 1)
{
if (child is TControlType castedChild && predicate(castedChild))
{
return castedChild;
}
continue;
}
var result = GetElementByTreePath<TControlType>(child, treePath, predicate, pos + 1);
if (result != null)
{
return result;
}
}
}
}
return null;
}
private TextBlock _videoPositionTextBlock = null;
private Button _playButton = null;
private TextBlock _playerSpeedTextBlock = null;
private bool _isPlaying = false;
private double _playerSpeed = 1.0;
private double _videoPositionInSeconds = 0.0;
private string _lastVideoPositionString = String.Empty;
private DateTime _breakEvenTime = DateTime.Now;
private static readonly Lazy<VirtualDesktopUiSettings> lazy =
new Lazy<VirtualDesktopUiSettings>(() => new VirtualDesktopUiSettings());
}
}