-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtabbed.py
305 lines (253 loc) · 9.28 KB
/
tabbed.py
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
"""Proof-of-concept for a Tabbed layout in Qtile.
See GitHub page for more information: https://github.com/hanschen/qtile_tabbed
Created by Hans Chen (contact@hanschen.org).
"""
from libqtile import hook
from libqtile.layout.base import Layout, _ClientList, _SimpleLayoutBase
def count_windows(group, include_floating=True):
count = 0
for window in group.windows:
if include_floating or not window.floating:
count += 1
return count
class Tab:
"""A tab representing a window."""
def __init__(self, window):
self.window = window
self._left = 0
self._right = 0
def button_press(self, x, y):
del y
if self._left <= x < self._right:
return self
def draw(self, layout, left):
if not layout.group.screen:
return
layout._layout.font_size = layout.fontsize
layout._layout.text = self.window.name
if self.window is layout.clients.current_client:
fg = layout.active_fg
bg = layout.active_bg
elif self.window.urgent:
fg = layout.urgent_fg
bg = layout.urgent_bg
else:
fg = layout.inactive_fg
bg = layout.inactive_bg
ntabs = len(layout.clients)
width = layout.group.screen.width / ntabs
layout._layout.width = width
layout._layout.colour = fg
# get a text frame from the above
framed = layout._layout.framed(
border_width=0,
border_color=bg,
pad_x=0,
pad_y=layout.padding_y,
)
# draw the text frame at the given point
framed.draw_fill(left, 0, rounded=layout.rounded_tabs)
self._left = left
self._right = left + framed.width
left += framed.width + layout.hspace
return left
class ClientList(_ClientList):
"""Similar to libqtile.layout.base._ClientList, but allows wraping when
shuffling windows.
"""
def shuffle_up(self, maintain_index=True):
"""
Shuffle the list. The current client swaps position with its
predecessor. If maintain_index is True the current_index is adjusted,
such that the same client stays current and goes up in list.
"""
idx = self._current_idx
if idx > 0:
self.clients[idx], self.clients[idx - 1] = self[idx - 1], self[idx]
if maintain_index:
self.current_index -= 1
else:
self.clients.append(self.clients.pop(0))
if maintain_index:
self.current_index = len(self.clients) - 1
def shuffle_down(self, maintain_index=True):
"""
Shuffle the list. The current client swaps position with its successor.
If maintain_index is True the current_index is adjusted,
such that the same client stays current and goes down in list.
"""
idx = self._current_idx
if idx + 1 < len(self.clients):
self.clients[idx], self.clients[idx + 1] = self[idx + 1], self[idx]
if maintain_index:
self.current_index += 1
else:
self.clients.insert(0, self.clients.pop(-1))
if maintain_index:
self.current_index = 0
class Tabbed(_SimpleLayoutBase):
"""Tabbed layout
A simple layout that displays one window at a time, similar to the Max
layout. The major difference from Max is that Tabbed will show a tab bar
with all windows if there are more than one or two windows in the layout,
depending on your settings.
"""
defaults = [
("bg_color", "000000", "Background color of tab bar"),
("active_fg", "ffffff", "Foreground color of active tab"),
("active_bg", "000080", "Background color of active tab"),
("urgent_fg", "ffffff", "Foreground color of urgent tab"),
("urgent_bg", "ff0000", "Background color of urgent tab"),
("inactive_fg", "ffffff", "Foreground color of inactive tab"),
("inactive_bg", "606060", "Background color of inactive tab"),
("rounded_tabs", False, "Draw tabs rounded"),
("padding_y", 2, "Top and bottom padding for tab label"),
("hspace", 2, "Space between tabs"),
("font", "sans", "Font"),
("fontsize", 14, "Font pixel size"),
("fontshadow", None, "Font shadow color, default is None (no shadow)"),
("bar_height", 24, "Height of tab bar"),
("place_bottom", False, "Place tab bar at the bottom instead of top"),
("show_single_tab", True, "Show tabs if there is only a single tab"),
]
def __init__(self, **config):
_SimpleLayoutBase.__init__(self, **config)
self.clients = ClientList()
self.add_defaults(Tabbed.defaults)
self._drawer = None
self._panel = None
self._tabs = {}
def add(self, client):
tab = Tab(client)
self._tabs[client] = tab
return super().add(client, 1)
def clone(self, group):
c = Layout.clone(self, group)
c.clients = ClientList()
return c
def configure(self, client, screen_rect):
if self.clients and client is self.clients.current_client:
client.place(
screen_rect.x,
screen_rect.y,
screen_rect.width,
screen_rect.height,
0,
None,
)
client.unhide()
else:
client.hide()
cmd_previous = _SimpleLayoutBase.previous
cmd_next = _SimpleLayoutBase.next
cmd_up = cmd_previous
cmd_down = cmd_next
cmd_left = cmd_previous
cmd_right = cmd_next
def cmd_shuffle_down(self):
self.clients.shuffle_down()
self.draw_panel()
def cmd_shuffle_up(self):
self.clients.shuffle_up()
self.draw_panel()
cmd_shuffle_left = cmd_shuffle_up
cmd_shuffle_right = cmd_shuffle_down
def draw_panel(self, *args):
del args
if not self._panel:
return
self._drawer.clear(self.bg_color)
left = 0
for client in self.clients:
left = self._tabs[client].draw(self, left)
self._drawer.draw(height=self.bar_height)
def finalize(self):
if self._panel:
self._panel.kill()
Layout.finalize(self)
if self._drawer is not None:
self._drawer.finalize()
def hide(self):
if self._panel:
self._panel.hide()
def layout(self, windows, screen_rect):
if not self._show_tabs():
body = screen_rect
if self._panel:
self._panel.hide()
else:
if self.place_bottom:
body, panel = screen_rect.vsplit(screen_rect.height - self.bar_height)
else:
panel, body = screen_rect.vsplit(self.bar_height)
self._resize_panel(panel)
if self._panel:
self._panel.unhide()
Layout.layout(self, windows, body)
def process_button_click(self, x, y, button):
if button == 4:
self.cmd_up()
elif button == 5:
self.cmd_down()
else:
for client in self.clients:
tab = self._tabs[client].button_press(x, y)
if tab:
self.group.focus(tab.window, False)
def remove(self, win):
super().remove(win)
self._tabs.pop(win)
self.draw_panel()
def show(self, screen_rect):
if not self._panel:
self._create_panel(screen_rect)
if not self._show_tabs():
return
if self.place_bottom:
_, panel = screen_rect.vsplit(screen_rect.height - self.bar_height)
else:
panel, _ = screen_rect.vsplit(self.bar_height)
self._resize_panel(panel)
self._panel.unhide()
def _create_drawer(self, screen_rect):
if self._drawer is None:
self._drawer = self._panel.create_drawer(
screen_rect.width,
self.bar_height,
)
else:
self._drawer.width = screen_rect.width
self._drawer.clear(self.bg_color)
self._layout = self._drawer.textlayout(
"", "#ffffff", self.font, self.fontsize, self.fontshadow, wrap=False
)
def _create_panel(self, screen_rect):
self._panel = self.group.qtile.core.create_internal(
screen_rect.x,
screen_rect.y,
screen_rect.width,
self.bar_height,
)
self._create_drawer(screen_rect)
self._panel.process_window_expose = self.draw_panel
self._panel.process_button_click = self.process_button_click
hook.subscribe.client_name_updated(self.draw_panel)
hook.subscribe.focus_change(self.draw_panel)
def _resize_panel(self, screen_rect):
if self._panel:
self._panel.place(
screen_rect.x,
screen_rect.y,
screen_rect.width,
screen_rect.height,
0,
None,
)
self._create_drawer(screen_rect)
self.draw_panel()
def _show_tabs(self):
nwindows = count_windows(self.group, include_floating=False)
if self.show_single_tab:
return nwindows > 0
else:
return nwindows > 1