]> Dogcows Code - chaz/openbox/blob - scripts/focus.py
2302036a45822ec73440c9ed1377ecb45511b2ce
[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 cycle_popup_list = 1
14 # send focus somewhere when nothing is left with the focus, if possible ###
15 fallback = 0 ###
16 ### ###
17 ###########################################################################
18
19 import otk
20 import ob
21
22 # maintain a list of clients, stacked in focus order
23 _clients = []
24 # maintaint he current focused window
25 _doing_stacked = 0
26
27 def _new_win(data):
28 global _clients
29 global _doing_stacked
30 global _cyc_w;
31
32 if _doing_stacked:
33 _clients.insert(_clients.index(_cyc_w), data.client.window())
34 else:
35 if not len(_clients):
36 _clients.append(data.client.window())
37 else:
38 _clients.insert(1, data.client.window()) # insert in 2nd slot
39
40 def _close_win(data):
41 global _clients
42 global _cyc_w;
43 global _doing_stacked
44
45 if not _doing_stacked:
46 # not in the middle of stacked cycling, so who cares
47 _clients.remove(data.client.window())
48 else:
49 # have to fix the cycling if we remove anything
50 win = data.client.window()
51 if _cyc_w == win:
52 _do_stacked_cycle(data) # cycle off the window first
53 _clients.remove(win)
54
55 def _focused(data):
56 global _clients
57 global _doing_stacked
58 global _cyc_w
59
60 if data.client:
61 if not _doing_stacked: # only move the window when we're not cycling
62 win = data.client.window()
63 # move it to the top
64 _clients.remove(win)
65 _clients.insert(0, win)
66 else: # if we are cycling, then update our pointer
67 _cyc_w = data.client.window()
68 global _list_widget, _list_labels, _list_windows
69 if _list_widget:
70 i = 0
71 for w in _list_windows:
72 if w == _cyc_w: _list_labels[i].focus()
73 else: _list_labels[i].unfocus()
74 i += 1
75 elif fallback:
76 # pass around focus
77 desktop = ob.openbox.screen(_cyc_screen).desktop()
78 for w in _clients:
79 client = ob.openbox.findClient(w)
80 if client and (client.desktop() == desktop and \
81 client.normal() and client.focus()):
82 break
83
84 _cyc_mask = 0
85 _cyc_key = 0
86 _cyc_w = 0 # last window cycled to
87 _cyc_screen = 0
88
89 def _do_stacked_cycle(data, forward):
90 global _cyc_w
91 global stacked_cycle_raise
92 global _clients
93
94 clients = _clients[:] # make a copy
95
96 if not forward:
97 clients.reverse()
98
99 try:
100 i = clients.index(_cyc_w) + 1
101 except ValueError:
102 i = 1
103 clients = clients[i:] + clients[:i]
104
105 desktop = ob.openbox.screen(data.screen).desktop()
106 for w in clients:
107 client = ob.openbox.findClient(w)
108 if client and (client.desktop() == desktop and \
109 client.normal() and client.focus()):
110 if stacked_cycle_raise:
111 ob.openbox.screen(data.screen).raiseWindow(client)
112 return
113
114 def _focus_stacked_ungrab(data):
115 global _cyc_mask;
116 global _cyc_key;
117 global _doing_stacked;
118
119 if data.action == ob.KeyAction.Release:
120 # have all the modifiers this started with been released?
121 if not _cyc_mask & data.state:
122 ob.kungrab() # ungrab ourself
123 _doing_stacked = 0;
124 if cycle_raise:
125 client = ob.openbox.findClient(_cyc_w)
126 if client:
127 ob.openbox.screen(data.screen).raiseWindow(client)
128 global _list_widget, _list_labels, _list_windows
129 if _list_widget:
130 _list_windows = []
131 _list_labels = []
132 _list_widget = 0
133
134 _list_widget = 0
135 _list_labels = []
136 _list_windows = []
137
138 def focus_next_stacked(data, forward=1):
139 """Focus the next (or previous, with forward=0) window in a stacked
140 order."""
141 global _cyc_mask
142 global _cyc_key
143 global _cyc_w
144 global _cyc_screen
145 global _doing_stacked
146
147 if _doing_stacked:
148 if _cyc_key == data.key:
149 _do_stacked_cycle(data,forward)
150 else:
151 _cyc_mask = data.state
152 _cyc_key = data.key
153 _cyc_w = 0
154 _cyc_screen = data.screen
155 _doing_stacked = 1
156
157 global cycle_popup_list
158 if cycle_popup_list:
159 global _list_widget, _list_labels
160 if not _list_widget: # make the widget list
161 style = ob.openbox.screen(data.screen).style()
162 _list_widget = otk.Widget(ob.openbox, style,
163 otk.Widget.Vertical, 0,
164 style.bevelWidth(), 1)
165 t = style.titlebarFocusBackground()
166 _list_widget.setTexture(t)
167
168 titles = []
169 font = style.labelFont()
170 height = font.height()
171 longest = 0
172 for c in _clients:
173 client = ob.openbox.findClient(c)
174 screen = ob.openbox.screen(data.screen)
175 desktop = screen.desktop()
176 if client and (client.desktop() == desktop and \
177 client.normal()):
178 t = client.title()
179 if len(t) > 50: # limit the length of titles
180 t = t[:24] + "..." + t[-24:]
181 titles.append(t)
182 _list_windows.append(c)
183 l = font.measureString(t) + 10 # add margin
184 if l > longest: longest = l
185 if len(titles):
186 for t in titles:
187 w = otk.FocusLabel(_list_widget)
188 w.resize(longest, height)
189 w.setText(t)
190 w.unfocus()
191 _list_labels.append(w)
192 _list_labels[0].focus()
193 _list_widget.update()
194 area = screen.area()
195 _list_widget.move(area.x() + (area.width() -
196 _list_widget.width()) / 2,
197 area.y() + (area.height() -
198 _list_widget.height()) / 2)
199 _list_widget.show(1)
200 else:
201 _list_widget = 0 #nothing to list
202
203 ob.kgrab(data.screen, _focus_stacked_ungrab)
204 focus_next_stacked(data, forward) # start with the first press
205
206 def focus_prev_stacked(data):
207 """Focus the previous window in a stacked order."""
208 focus_next_stacked(data, forward=0)
209
210 def focus_next(data, num=1, forward=1):
211 """Focus the next (or previous, with forward=0) window in a linear
212 order."""
213 screen = ob.openbox.screen(data.screen)
214 count = screen.clientCount()
215
216 if not count: return # no clients
217
218 target = 0
219 if data.client:
220 client_win = data.client.window()
221 found = 0
222 r = range(count)
223 if not forward:
224 r.reverse()
225 for i in r:
226 if found:
227 target = i
228 found = 2
229 break
230 elif screen.client(i).window() == client_win:
231 found = 1
232 if found == 1: # wraparound
233 if forward: target = 0
234 else: target = count - 1
235
236 t = target
237 curdesk = screen.desktop()
238 while 1:
239 client = screen.client(t)
240 if client.normal() and \
241 (client.desktop() == curdesk or client.desktop() == 0xffffffff)\
242 and client.focus():
243 if cycle_raise:
244 screen.raiseWindow(client)
245 return
246 if forward:
247 t += num
248 if t >= count: t -= count
249 else:
250 t -= num
251 if t < 0: t += count
252 if t == target: return # nothing to focus
253
254 def focus_prev(data, num=1):
255 """Focus the previous window in a linear order."""
256 focus_next(data, num, forward=0)
257
258
259 ob.ebind(ob.EventAction.NewWindow, _new_win)
260 ob.ebind(ob.EventAction.CloseWindow, _close_win)
261 ob.ebind(ob.EventAction.Focus, _focused)
262
263 print "Loaded focus.py"
This page took 0.051246 seconds and 4 git commands to generate.