Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/shell/WindowManager.py
blob: 765b2bef4499572f8c89556230b75688d8f80488 (plain)
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
import pygtk
pygtk.require('2.0')
import gtk
import gobject

SM_SPACE_PROPORTIONAL = 0
SM_STEP = 1

SLIDING_TIMEOUT = 50
SLIDING_MODE = SM_SPACE_PROPORTIONAL

#SLIDING_TIMEOUT = 10
#SLIDING_MODE = SM_STEP
#SLIDING_STEP = 0.05

class WindowManager:
	__managers_list = []

	CENTER = 0
	LEFT = 1
	RIGHT = 2
	TOP = 3
	BOTTOM = 4
	
	ABSOLUTE = 0
	SCREEN_RELATIVE = 1
	
	def __init__(self, window):
		self._window = window
		self._sliding_pos = 0

		WindowManager.__managers_list.append(self)
		
		window.connect("key-press-event", self.__key_press_event_cb)

	def __key_press_event_cb(self, window, event):
		manager = None

		if event.keyval == gtk.keysyms.Left and \
		   event.state & gtk.gdk.CONTROL_MASK:
			for wm in WindowManager.__managers_list:
				if wm._position == WindowManager.LEFT:
					manager = wm

		if event.keyval == gtk.keysyms.Up and \
		   event.state & gtk.gdk.CONTROL_MASK:
			for wm in WindowManager.__managers_list:
				if wm._position == WindowManager.TOP:
					manager = wm

		if manager and manager._window.get_property('visible'):
			manager.slide_window_out()
		elif manager:
			manager.slide_window_in()
	
	def set_width(self, width, width_type):
		self._width = width
		self._width_type = width_type
				
	def set_height(self, height, height_type):
		self._height = height
		self._height_type = height_type
	
	def set_position(self, position):
		self._position = position

	def _calc_size_and_position(self):
		screen_width = self._window.get_screen().get_width()
		screen_height = self._window.get_screen().get_height()
		
		if self._width_type is WindowManager.ABSOLUTE:
			width = self._width			
		elif self._width_type is WindowManager.SCREEN_RELATIVE:
			width = int(screen_width * self._width)

		if self._height_type is WindowManager.ABSOLUTE:
			height = self._height			
		elif self._height_type is WindowManager.SCREEN_RELATIVE:
			height = int(screen_height * self._height)
			
		if self._position is WindowManager.CENTER:
			self._x = int((screen_width - width) / 2)
			self._y = int((screen_height - height) / 2)
		elif self._position is WindowManager.LEFT:
			self._x = - int((1.0 - self._sliding_pos) * width)
			self._y = int((screen_height - height) / 2)
		elif self._position is WindowManager.TOP:
			self._x = int((screen_width - width) / 2)
			self._y = - int((1.0 - self._sliding_pos) * height)
		
		self._real_width = width
		self._real_height = height
	
	def _update_size_and_position(self):
		self._calc_size_and_position()
		self._window.move(self._x, self._y)
		self._window.resize(self._real_width, self._real_height)

	def _update_position(self):
		self._calc_size_and_position()
		self._window.move(self._x, self._y)

	def __slide_in_timeout_cb(self):
		if self._sliding_pos == 0:
			self._window.show()

		if SLIDING_MODE == SM_SPACE_PROPORTIONAL:
			space_to_go = 1.0 - self._sliding_pos
			self._sliding_pos += (space_to_go / 2)
		else:
			self._sliding_pos += SLIDING_STEP

		if self._sliding_pos > .999:
			self._sliding_pos = 1.0

		self._update_position()

		if self._sliding_pos == 1.0:
			return False
		else:
			return True

	def __slide_out_timeout_cb(self):
		self._window.show()

		if SLIDING_MODE == SM_SPACE_PROPORTIONAL:
			space_to_go = self._sliding_pos
			self._sliding_pos -= (space_to_go / 2)
		else:
			self._sliding_pos -= SLIDING_STEP

		if self._sliding_pos < .001:
			self._sliding_pos = 0

		self._update_position()

		if self._sliding_pos == 0:
			self._window.hide()
			return False
		else:
			return True

	def slide_window_in(self):
		self._sliding_pos = 0
		gobject.timeout_add(SLIDING_TIMEOUT, self.__slide_in_timeout_cb)
		
	def slide_window_out(self):
		self._sliding_pos = 1.0
		gobject.timeout_add(SLIDING_TIMEOUT, self.__slide_out_timeout_cb)
			
	def show(self):
		self._window.show()

	def update(self):
		self._update_size_and_position()
	
	def manage(self):
		self._update_size_and_position()