-
Notifications
You must be signed in to change notification settings - Fork 99
/
WorkspaceWidget.cs
119 lines (102 loc) · 3.95 KB
/
WorkspaceWidget.cs
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
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Timers;
namespace workspacer.Bar.Widgets
{
public class WorkspaceWidget : BarWidgetBase
{
public Color WorkspaceHasFocusColor { get; set; } = Color.Red;
public Color WorkspaceEmptyColor { get; set; } = Color.Gray;
public Color WorkspaceIndicatingBackColor { get; set; } = Color.Teal;
public int BlinkPeriod { get; set; } = 1000;
private Timer _blinkTimer;
private ConcurrentDictionary<IWorkspace, bool> _blinkingWorkspaces;
public override void Initialize()
{
Context.Workspaces.WorkspaceUpdated += () => UpdateWorkspaces();
Context.Workspaces.WindowMoved += (w, o, n) => UpdateWorkspaces();
_blinkingWorkspaces = new ConcurrentDictionary<IWorkspace, bool>();
_blinkTimer = new Timer(BlinkPeriod);
_blinkTimer.Elapsed += (s, e) => BlinkIndicatingWorkspaces();
_blinkTimer.Enabled = true;
}
public override IBarWidgetPart[] GetParts()
{
var parts = new List<IBarWidgetPart>();
var workspaces = Context.WorkspaceContainer.GetWorkspaces(Context.Monitor);
int index = 0;
foreach (var workspace in workspaces)
{
parts.Add(CreatePart(workspace, index));
index++;
}
return parts.ToArray();
}
private bool WorkspaceIsIndicating(IWorkspace workspace)
{
if (workspace.IsIndicating)
{
if (_blinkingWorkspaces.ContainsKey(workspace))
{
_blinkingWorkspaces.TryGetValue(workspace, out bool value);
return value;
} else
{
_blinkingWorkspaces.TryAdd(workspace, true);
return true;
}
}
else if (_blinkingWorkspaces.ContainsKey(workspace))
{
_blinkingWorkspaces.TryRemove(workspace, out bool _);
}
return false;
}
private IBarWidgetPart CreatePart(IWorkspace workspace, int index)
{
var backColor = WorkspaceIsIndicating(workspace) ? WorkspaceIndicatingBackColor : null;
return Part(GetDisplayName(workspace, index), GetDisplayColor(workspace, index), backColor, () =>
{
Context.Workspaces.SwitchMonitorToWorkspace(Context.Monitor.Index, index);
},
FontName);
}
private void UpdateWorkspaces()
{
MarkDirty();
}
protected virtual string GetDisplayName(IWorkspace workspace, int index)
{
var monitor = Context.WorkspaceContainer.GetCurrentMonitorForWorkspace(workspace);
var visible = Context.Monitor == monitor;
return visible ? LeftPadding + workspace.Name + RightPadding : workspace.Name;
}
protected virtual Color GetDisplayColor(IWorkspace workspace, int index)
{
var monitor = Context.WorkspaceContainer.GetCurrentMonitorForWorkspace(workspace);
if (Context.Monitor == monitor)
{
return WorkspaceHasFocusColor;
}
var hasWindows = workspace.ManagedWindows.Count != 0;
return hasWindows ? null : WorkspaceEmptyColor;
}
private void BlinkIndicatingWorkspaces()
{
var workspaces = _blinkingWorkspaces.Keys;
var didFlip = false;
foreach (var workspace in workspaces)
{
if (_blinkingWorkspaces.TryGetValue(workspace, out bool value))
{
_blinkingWorkspaces.TryUpdate(workspace, !value, value);
didFlip = true;
}
}
if (didFlip)
{
MarkDirty();
}
}
}
}