-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathStatusbarControl.cs
More file actions
81 lines (67 loc) · 2.48 KB
/
Copy pathStatusbarControl.cs
File metadata and controls
81 lines (67 loc) · 2.48 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
using Microsoft.VisualStudio.Shell;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace WakaTime.ExtensionUtils
{
internal class StatusbarControl : TextBlock
{
private const string Icon = "🕑";
private const string DefaultDashboardUrl = "https://wakatime.com/";
private readonly Brush _normalBackground = new SolidColorBrush(Colors.Transparent);
private readonly Brush _hoverBackground = new SolidColorBrush(Colors.White) { Opacity = 0.2 };
private readonly string _dashboardUrl;
public StatusbarControl(string apiUrl)
{
_dashboardUrl = GetDashboardUrl(apiUrl);
Text = Icon;
Foreground = new SolidColorBrush(Colors.White);
Background = _normalBackground;
VerticalAlignment = VerticalAlignment.Center;
Margin = new Thickness(7, 0, 7, 0);
Padding = new Thickness(7, 0, 7, 0);
MouseEnter += (s, e) =>
{
Cursor = Cursors.Hand;
Background = _hoverBackground;
};
MouseLeave += (s, e) =>
{
Cursor = Cursors.Arrow;
Background = _normalBackground;
};
MouseLeftButtonUp += (s, e) =>
{
// Open WakaTime in browser
System.Diagnostics.Process.Start(_dashboardUrl);
};
}
private static string GetDashboardUrl(string apiUrl)
{
if (string.IsNullOrWhiteSpace(apiUrl))
return DefaultDashboardUrl;
if (!Uri.TryCreate(apiUrl, UriKind.Absolute, out var apiUri))
return DefaultDashboardUrl;
var host = apiUri.Host.StartsWith("api.", StringComparison.OrdinalIgnoreCase)
? apiUri.Host.Substring(4)
: apiUri.Host;
var dashboardUriBuilder = new UriBuilder(apiUri.Scheme, host, apiUri.IsDefaultPort ? -1 : apiUri.Port)
{
Path = "/"
};
return dashboardUriBuilder.Uri.AbsoluteUri;
}
public void SetText(string text)
{
ThreadHelper.ThrowIfNotOnUIThread();
Text = string.IsNullOrEmpty(text) ? Icon : $"{Icon} {text}";
}
public void SetToolTip(string toolTip)
{
ThreadHelper.ThrowIfNotOnUIThread();
ToolTip = toolTip;
}
}
}