]> Dogcows Code - chaz/openbox/blob - scripts/focus.py
don't put windows in the focus cycling list if they are set to not show up in the...
[chaz/openbox] / scripts / focus.py
1 ###########################################################################
2 ### Functions for helping out with your window focus. ###
3 ###########################################################################
4
5 ###########################################################################
6 ### Options that affect the behavior of the focus module. ###
7 ### ###
8 # raise the window also when it is focused ###
9 cycle_raise = 1 ###
10 # raise as you cycle in stacked mode ###
11 stacked_cycle_raise = 0 ###
12 # show a pop-up list of windows while cycling ###
13 stacked_cycle_popup_list = 1 ###
14 # send focus somewhere when nothing is left with the focus, if possible ###
15 fallback = 0 ###
16 ### ###
17 ### ###
18 # Provides: ###
19 # def focus_next_stacked(data, forward=1): ###
20 # """Focus the next (or previous, with forward=0) window in a stacked ###
21 # order.""" ###
22 # def focus_prev_stacked(data): ###
23 # """Focus the previous window in a stacked order.""" ###
24 # def focus_next(data, num=1, forward=1): ###
25 # """Focus the next (or previous, with forward=0) window in a linear ###
26 # order.""" ###
27 # def focus_prev(data, num=1): ###
28 # """Focus the previous window in a linear order.""" ###
29 ### ###
30 # All of these functions call be used as callbacks for bindings ###
31 # directly. ###
32 ### ###
33 ###########################################################################
34
35 import otk
36 import ob
37
38 # maintain a list of clients, stacked in focus order
39 _clients = []
40 # maintaint he current focused window
41 _doing_stacked = 0
42
43 def _new_win(data):
44 global _clients
45 global _doing_stacked
46 global _cyc_w;
47
48 if _doing_stacked:
49 _clients.insert(_clients.index(_cyc_w), data.client.window())
50 _create_popup_list(data)
51 _hilite_popup_list(data)
52 else:
53 if not len(_clients):
54 _clients.append(data.client.window())
55 else:
56 _clients.insert(1, data.client.window()) # insert in 2nd slot
57
58 def _close_win(data):
59 global _clients
60 global _cyc_w;
61 global _doing_stacked
62
63 if not _doing_stacked:
64 # not in the middle of stacked cycling, so who cares
65 _clients.remove(data.client.window())
66 else:
67 # have to fix the cycling if we remove anything
68 win = data.client.window()
69 if _cyc_w == win:
70 _do_stacked_cycle(data, 1) # cycle off the window first, forward
71 _clients.remove(win)
72 _create_popup_list(data)
73
74 def _focused(data):
75 global _clients
76 global _doing_stacked
77 global _cyc_w
78
79 if data.client:
80 if not _doing_stacked: # only move the window when we're not cycling
81 win = data.client.window()
82 # move it to the top
83 _clients.remove(win)
84 _clients.insert(0, win)
85 else: # if we are cycling, then update our pointer
86 _cyc_w = data.client.window()
87 _hilite_popup_list(data)
88 elif fallback:
89 # pass around focus
90 desktop = ob.openbox.screen(_cyc_screen).desktop()
91 for w in _clients:
92 client = ob.openbox.findClient(w)
93 if client and (client.desktop() == desktop or
94 client.desktop() == 0xffffffff) \
95 and client.normal() and client.focus()):
96 break
97 if _doing_stacked:
98 _cyc_w = 0
99 _hilite_popup_list(data)
100
101 _cyc_mask = 0
102 _cyc_key = 0
103 _cyc_w = 0 # last window cycled to
104 _cyc_screen = 0
105
106 def _do_stacked_cycle(data, forward):
107 global _cyc_w
108 global stacked_cycle_raise
109 global _clients
110
111 clients = _clients[:] # make a copy
112
113 if not forward:
114 clients.reverse()
115
116 try:
117 i = clients.index(_cyc_w) + 1
118 except ValueError:
119 i = 1
120 clients = clients[i:] + clients[:i]
121
122 desktop = ob.openbox.screen(data.screen).desktop()
123 for w in clients:
124 client = ob.openbox.findClient(w)
125 if client and (client.desktop() == desktop or
126 client.desktop() == 0xffffffff) \
127 and client.normal() and client.focus():
128 if stacked_cycle_raise:
129 ob.openbox.screen(data.screen).raiseWindow(client)
130 return
131
132 def _focus_stacked_ungrab(data):
133 global _cyc_mask;
134 global _cyc_key;
135 global _doing_stacked;
136
137 if data.action == ob.KeyAction.Release:
138 # have all the modifiers this started with been released?
139 if not _cyc_mask & data.state:
140 _destroy_popup_list()
141 ob.kungrab()
142 ob.mungrab()
143 _doing_stacked = 0;
144 if cycle_raise:
145 client = ob.openbox.findClient(_cyc_w)
146 if client:
147 ob.openbox.screen(data.screen).raiseWindow(client)
148
149 _list_widget = 0
150 _list_labels = []
151 _list_windows = []
152
153 def _hilite_popup_list(data):
154 global _cyc_w, _doing_stacked
155 global _list_widget, _list_labels, _list_windows
156 found = 0
157
158 if not _list_widget and _doing_stacked:
159 _create_popup_list(data)
160
161 if _list_widget:
162 i = 0
163 for w in _list_windows:
164 if w == _cyc_w:
165 _list_labels[i].focus()
166 found = 1
167 else:
168 _list_labels[i].unfocus()
169 i += 1
170 if not found:
171 _create_popup_list(data)
172
173 def _destroy_popup_list():
174 global _list_widget, _list_labels, _list_windows
175 if _list_widget:
176 _list_windows = []
177 _list_labels = []
178 _list_widget = 0
179
180 def _create_popup_list(data):
181 global _list_widget, _list_labels, _list_windows, _clients
182
183 if _list_widget:
184 _destroy_popup_list()
185
186 style = ob.openbox.screen(data.screen).style()
187 _list_widget = otk.Widget(ob.openbox, style,
188 otk.Widget.Vertical, 0,
189 style.bevelWidth(), 1)
190 t = style.titlebarFocusBackground()
191 _list_widget.setTexture(t)
192
193 titles = []
194 font = style.labelFont()
195 height = font.height()
196 longest = 0
197 for c in _clients:
198 client = ob.openbox.findClient(c)
199 desktop = ob.openbox.screen(data.screen).desktop()
200 if client and not client.skipTaskbar() and \
201 ((client.desktop() == desktop or
202 client.desktop() == 0xffffffff) and \
203 client.normal() and (client.canFocus() or
204 client.focusNotify())):
205 t = client.title()
206 if len(t) > 50: # limit the length of titles
207 t = t[:24] + "..." + t[-24:]
208 titles.append(t)
209 _list_windows.append(c)
210 l = font.measureString(t)
211 if l > longest: longest = l
212 if len(titles) > 1:
213 for t in titles:
214 w = otk.FocusLabel(_list_widget)
215 w.fitSize(longest, height)
216 w.setText(t)
217 w.unfocus()
218 _list_labels.append(w)
219 _list_widget.update()
220 area = otk.display.screenInfo(data.screen).rect()
221 _list_widget.move(area.x() + (area.width() -
222 _list_widget.width()) / 2,
223 area.y() + (area.height() -
224 _list_widget.height()) / 2)
225 _list_widget.show(1)
226 else:
227 _destroy_popup_list() # nothing (or only 1) to list
228
229 def focus_next_stacked(data, forward=1):
230 """Focus the next (or previous, with forward=0) window in a stacked
231 order."""
232 global _cyc_mask
233 global _cyc_key
234 global _cyc_w
235 global _cyc_screen
236 global _doing_stacked
237
238 if _doing_stacked:
239 if _cyc_key == data.key:
240 _do_stacked_cycle(data,forward)
241 else:
242 _cyc_mask = data.state
243 _cyc_key = data.key
244 _cyc_w = 0
245 _cyc_screen = data.screen
246 _doing_stacked = 1
247
248 global stacked_cycle_popup_list
249 if stacked_cycle_popup_list:
250 _create_popup_list(data)
251
252 ob.kgrab(data.screen, _focus_stacked_ungrab)
253 # the pointer grab causes pointer events during the keyboard grab to
254 # go away, which means we don't get enter notifies when the popup
255 # disappears, screwing up the focus
256 ob.mgrab(data.screen)
257 focus_next_stacked(data, forward) # start with the first press
258
259 def focus_prev_stacked(data):
260 """Focus the previous window in a stacked order."""
261 focus_next_stacked(data, forward=0)
262
263 def focus_next(data, num=1, forward=1):
264 """Focus the next (or previous, with forward=0) window in a linear
265 order."""
266 screen = ob.openbox.screen(data.screen)
267 count = screen.clientCount()
268
269 if not count: return # no clients
270
271 target = 0
272 if data.client:
273 client_win = data.client.window()
274 found = 0
275 r = range(count)
276 if not forward:
277 r.reverse()
278 for i in r:
279 if found:
280 target = i
281 found = 2
282 break
283 elif screen.client(i).window() == client_win:
284 found = 1
285 if found == 1: # wraparound
286 if forward: target = 0
287 else: target = count - 1
288
289 t = target
290 curdesk = screen.desktop()
291 while 1:
292 client = screen.client(t)
293 if not client.skipTaskbar() and client.normal() and \
294 (client.desktop() == curdesk or
295 client.desktop() == 0xffffffff)\
296 and client.focus():
297 if cycle_raise:
298 screen.raiseWindow(client)
299 return
300 if forward:
301 t += num
302 if t >= count: t -= count
303 else:
304 t -= num
305 if t < 0: t += count
306 if t == target: return # nothing to focus
307
308 def focus_prev(data, num=1):
309 """Focus the previous window in a linear order."""
310 focus_next(data, num, forward=0)
311
312
313 ob.ebind(ob.EventAction.NewWindow, _new_win)
314 ob.ebind(ob.EventAction.CloseWindow, _close_win)
315 ob.ebind(ob.EventAction.Focus, _focused)
316
317 print "Loaded focus.py"
This page took 0.047057 seconds and 4 git commands to generate.