-
Notifications
You must be signed in to change notification settings - Fork 100
/
TitleWidget.cs
149 lines (128 loc) · 5.48 KB
/
TitleWidget.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
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
using System;
using System.Collections.Generic;
using System.Linq;
namespace workspacer.Bar.Widgets
{
public class TitleWidget : BarWidgetBase
{
#region Properties
public Color WindowHasFocusColor { get; set; } = Color.Yellow;
public bool IsShortTitle { get; set; } = false;
public int? MaxTitleLength { get; set; } = null;
public bool ShowAllWindowTitles { get; set; } = false;
public string TitlePreamble { get; set; } = null;
public string TitlePostamble { get; set; } = null;
public string NoWindowMessage { get; set; } = "No Windows";
public Func<IWindow, Action> TitlePartClicked = ClickAction;
public Func<IWindow, object> OrderWindowsBy = (window) => 0;
#endregion
public override void Initialize()
{
Context.Workspaces.WindowAdded += RefreshAdd;
Context.Workspaces.WindowRemoved += RefreshRemove;
Context.Workspaces.WindowUpdated += RefreshUpdated;
Context.Workspaces.FocusedMonitorUpdated += RefreshFocusedMonitor;
}
#region Get Windows
private IEnumerable<IWindow> GetWindows(bool filterOnTitleFilled = true)
{
var currentWorkspace = Context.WorkspaceContainer.GetWorkspaceForMonitor(Context.Monitor);
return currentWorkspace.ManagedWindows.Where(window => !filterOnTitleFilled || !string.IsNullOrEmpty(window.Title));
}
private IWindow GetWindow()
{
var currentWorkspace = Context.WorkspaceContainer.GetWorkspaceForMonitor(Context.Monitor);
return currentWorkspace.FocusedWindow ??
currentWorkspace.LastFocusedWindow ??
currentWorkspace.ManagedWindows.FirstOrDefault();
}
#endregion
#region Events
private void RefreshRemove(IWindow window, IWorkspace workspace)
{
var currentWorkspace = Context.WorkspaceContainer.GetWorkspaceForMonitor(Context.Monitor);
if (workspace == currentWorkspace && !string.IsNullOrEmpty(window.Title) && !GetWindows().Contains(window))
{
MarkDirty();
}
}
private void RefreshAdd(IWindow window, IWorkspace workspace)
{
var currentWorkspace = Context.WorkspaceContainer.GetWorkspaceForMonitor(Context.Monitor);
if (workspace == currentWorkspace && !string.IsNullOrEmpty(window.Title) && GetWindows().Contains(window))
{
MarkDirty();
}
}
private void RefreshUpdated(IWindow window, IWorkspace workspace)
{
var currentWorkspace = Context.WorkspaceContainer.GetWorkspaceForMonitor(Context.Monitor);
if (workspace == currentWorkspace && !string.IsNullOrEmpty(window.Title) && GetWindows().Contains(window))
{
MarkDirty();
}
}
private void RefreshFocusedMonitor()
{
MarkDirty();
}
private static Action ClickAction(IWindow window)
{
return new Action(() =>
{
if (window == null)
{
return;
}
window.ShowInCurrentState();
window.BringToTop();
window.Focus();
});
}
#endregion
#region Title Generation
public override IBarWidgetPart[] GetParts()
{
var windows = ShowAllWindowTitles ? GetWindows() : new[] { GetWindow() };
if (windows == null || !windows.Any())
{
return Parts(Part(NoWindowMessage, null, fontname: FontName));
}
return windows.OrderByDescending(OrderWindowsBy).Select(w => CreateTitlePart(w, WindowHasFocusColor, FontName, IsShortTitle, MaxTitleLength, TitlePartClicked)).ToArray();
}
private IBarWidgetPart CreateTitlePart(IWindow window, Color windowHasFocusColor, string fontName, bool isShortTitle = false, int? maxTitleLength = null, Func<IWindow, Action> clickAction = null)
{
var windowTitle = window.Title;
if (isShortTitle)
{
windowTitle = GetShortTitle(windowTitle);
}
if (maxTitleLength.HasValue)
{
windowTitle = GetTrimmedTitle(windowTitle, maxTitleLength);
}
windowTitle = string.Format("{0}{1}{2}", string.IsNullOrEmpty(TitlePreamble) ? '[' : TitlePreamble, windowTitle, string.IsNullOrEmpty(TitlePostamble) ? ']' : TitlePostamble);
return Part(windowTitle, window.IsFocused ? windowHasFocusColor : null, fontname: fontName, partClicked: clickAction != null ? clickAction(window) : null);
}
#endregion
#region Title Formating
public static string GetShortTitle(string title)
{
var parts = title.Split(new char[] { '-', '—', '|' }, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length == 0)
{
return title.Trim();
}
return parts.Last().Trim();
}
public static string GetTrimmedTitle(string title, int? maxTitleLength = null)
{
if (!maxTitleLength.HasValue || title.Length <= maxTitleLength.Value)
{
return title;
}
return title.Remove(maxTitleLength.Value, title.Length - maxTitleLength.Value) + "...";
}
#endregion
}
}