File: opacity.py

package info (click to toggle)
python-plwm 2.6a%2B20080530-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,052 kB
  • ctags: 1,781
  • sloc: python: 7,574; ansic: 1,075; xml: 109; makefile: 83
file content (111 lines) | stat: -rw-r--r-- 3,126 bytes parent folder | download | duplicates (3)
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
#
# opacity.py -- set window opacity based on focus
#
#	Copyright (C) 2008  David H. Bronke <whitelynx@gmail.com>
#
#	This program is free software; you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation; either version 2 of the License, or
#	(at your option) any later version.
#
#	This program is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.
#
#	You should have received a copy of the GNU General Public License
#	along with this program; if not, write to the Free Software
#	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import wmanager, wmevents, cfilter
from Xlib import X, Xatom

# Client mixin
class TransparentClient:
	"""Client mixin. Sets opacity according to focus and client filters.

	Class members:
		opacity_(un)focused_default - sets the default opacity for (un)focused
			windows.
		opacity_(un)focused_clients - sets the opacity for specific windows,
			specified by client filters, depending on focus.
		opacity_ignore_clients - client filter which specifies clients to ignore.
			These clients will not have the _NET_WM_WINDOW_OPACITY property set.

	You can also set a client's opacity directly, using the opacity_set() method.
	"""

	opacity_focused_default = 1.0
	opacity_unfocused_default = 0.7

	opacity_focused_clients = ()
	opacity_unfocused_clients = ()

	opacity_ignore_clients = cfilter.false

	def __client_init__(self):
		self.opacity_focused = None
		self.opacity_unfocused = None

		self._NET_WM_WINDOW_OPACITY = self.wm.display.intern_atom(
			'_NET_WM_WINDOW_OPACITY'
		)

		if not self.opacity_ignore_clients(self):
			for filter, opacity in self.opacity_focused_clients:
				if filter(self):
					self.opacity_focused = opacity
					break
			else:
				self.opacity_focused = self.opacity_focused_default

			for filter, opacity in self.opacity_unfocused_clients:
				if filter(self):
					self.opacity_unfocused = opacity
					break
			else:
				self.opacity_unfocused = self.opacity_unfocused_default

			if self.focused:
				self.opacity_get_focus(None)
			else:
				self.opacity_lose_focus(None)

			# Set up dispatchers for the opacity changes
			self.dispatch.add_handler(
				wmevents.ClientFocusIn,
				self.opacity_get_focus
			)
			self.dispatch.add_handler(
				wmevents.ClientFocusOut,
				self.opacity_lose_focus
			)

	def opacity_get_focus(self, event):
		if self.opacity_focused is not None:
			self.opacity_set(self.opacity_focused)

	def opacity_lose_focus(self, event):
		if self.opacity_unfocused is not None:
			self.opacity_set(self.opacity_unfocused)

	def opacity_set(self, percent):
		opacity = int(percent * 0xFFFFFFFF)

		w = self.window
		while 1:
			r = w.query_tree()
			if r.parent == r.root:
				break
			w = r.parent

		w.change_property(
			self._NET_WM_WINDOW_OPACITY,
			Xatom.CARDINAL,
			32,
			[opacity],
			X.PropModeReplace
		)

		self.wm.display.sync()