1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 from skin_config import skin_config
24 from utils import eval_file, get_parent_dir, create_directory
25 import gtk
26 import os
27
29 '''
30 Dynamic color.
31 '''
32
34 '''
35 Initialize DynamicColor.
36
37 @param color: Initialize color.
38 '''
39 self.update(color)
40
42 '''
43 Update color.
44
45 @param color: Color value.
46 '''
47 self.color = color
48
50 '''
51 Get color.
52
53 @return: Return current color value.
54 '''
55 return self.color
56
58 '''
59 Dynamic alpha color.
60 '''
61
63 '''
64 Initialize DynamicAlphaColor class.
65
66 @param color_info: Color information, format as (hex_color, alpha)
67 '''
68 self.update(color_info)
69
71 '''
72 Update color_info with given value.
73 '''
74 (self.color, self.alpha) = color_info
75
77 '''
78 Get color info.
79
80 @return: Return color information, format as (hex_color, alpha)
81 '''
82 return (self.color, self.alpha)
83
85 '''
86 Get color.
87
88 @return: Return hex color string.
89 '''
90 return self.color
91
93 '''
94 Get alpha value.
95
96 @return: Return alpha value.
97 '''
98 return self.alpha
99
101 '''
102 Dynamic shadow color.
103 '''
104
106 '''
107 Initialize DynamicShadowColor class.
108
109 @param color_info: Color information, format as:
110
111 >>> [(color_position_1, (hex_color_1, color_alpha_1),
112 >>> (color_position_2, (hex_color_2, color_alpha_2),
113 >>> (color_position_3, (hex_color_3, color_alpha_3)]
114 '''
115 self.update(color_info)
116
117 - def update(self, color_info):
118 '''
119 Update color with given value.
120
121 @param color_info: Color information, format as:
122
123 >>> [(color_position_1, (hex_color_1, color_alpha_1),
124 >>> (color_position_2, (hex_color_2, color_alpha_2),
125 >>> (color_position_3, (hex_color_3, color_alpha_3)]
126 '''
127 self.color_info = color_info
128
130 '''
131 Get color information.
132
133 @return: Return color information, format as:
134
135 >>> [(color_position_1, (hex_color_1, color_alpha_1),
136 >>> (color_position_2, (hex_color_2, color_alpha_2),
137 >>> (color_position_3, (hex_color_3, color_alpha_3)]
138 '''
139 return self.color_info
140
142 '''
143 Dynamic pixbuf.
144 '''
145
147 '''
148 Initialize DynamicPixbuf class.
149
150 @param filepath: Dynamic pixbuf filepath.
151 '''
152 self.update(filepath)
153
155 '''
156 Update filepath with given value.
157
158 @param filepath: Dynamic pixbuf filepath.
159 '''
160 self.pixbuf = gtk.gdk.pixbuf_new_from_file(filepath)
161
163 '''
164 Get pixbuf.
165 '''
166 return self.pixbuf
167
169 '''
170 Theme.
171
172 @undocumented: get_ticker
173 '''
174
175 - def __init__(self,
176 system_theme_dir,
177 user_theme_dir):
178 '''
179 Initialize Theme class.
180
181 @param system_theme_dir: Default theme directory.
182 @param user_theme_dir: User's theme save directory, generic is ~/.config/project-name/theme
183 '''
184
185 self.system_theme_dir = system_theme_dir
186 self.user_theme_dir = user_theme_dir
187 self.theme_info_file = "theme.txt"
188 self.ticker = 0
189 self.pixbuf_dict = {}
190 self.color_dict = {}
191 self.alpha_color_dict = {}
192 self.shadow_color_dict = {}
193
194
195 for theme_dir in [self.system_theme_dir, self.user_theme_dir]:
196 create_directory(theme_dir)
197
199 '''
200 Load theme.
201 '''
202 self.theme_name = skin_config.theme_name
203
204
205 theme_info = eval_file(self.get_theme_file_path(self.theme_info_file))
206
207
208 for (color_name, color) in theme_info["colors"].items():
209 self.color_dict[color_name] = DynamicColor(color)
210
211
212 for (color_name, color_info) in theme_info["alpha_colors"].items():
213 self.alpha_color_dict[color_name] = DynamicAlphaColor(color_info)
214
215
216 for (color_name, color_info) in theme_info["shadow_colors"].items():
217 self.shadow_color_dict[color_name] = DynamicShadowColor(color_info)
218
219
220 skin_config.add_theme(self)
221
223 '''
224 Get theme file path with given theme name.
225
226 @return: Return filepath of theme.
227 '''
228 theme_file_dir = None
229 for theme_dir in [self.system_theme_dir, self.user_theme_dir]:
230 if os.path.exists(theme_dir):
231 if self.theme_name in os.listdir(os.path.expanduser(theme_dir)):
232 theme_file_dir = theme_dir
233 break
234
235 if theme_file_dir:
236 return os.path.join(theme_file_dir, self.theme_name, filename)
237 else:
238 return None
239
241 '''
242 Get pixbuf with given relative path.
243
244 @param path: Image relative filepath to theme.
245
246 @return: Return pixbuf with given relative path.
247 '''
248
249 if not self.pixbuf_dict.has_key(path):
250 self.pixbuf_dict[path] = DynamicPixbuf(self.get_theme_file_path("image/%s" % (path)))
251
252 return self.pixbuf_dict[path]
253
255 '''
256 Get color with given dynmaic color.
257
258 @param color_name: DynamicColor name from theme.txt.
259
260 @return: Return color with given dynamic color.
261 '''
262 return self.color_dict[color_name]
263
265 '''
266 Get color with given dynmaic alpha color.
267
268 @param color_name: DynamicAlphaColor name from theme.txt.
269
270 @return: Return color with given dynamic alpha color.
271 '''
272 return self.alpha_color_dict[color_name]
273
275 '''
276 Get color with given dynmaic shadow color.
277
278 @param color_name: DynamicShadowColor name from theme.txt.
279
280 @return: Return color with given dynamic shadow color.
281 '''
282 return self.shadow_color_dict[color_name]
283
285 '''
286 Internal function to get ticker.
287 '''
288 return self.ticker
289
291 '''
292 Change theme with given new theme name.
293
294 @param new_theme_name: New theme name.
295 '''
296
297 self.ticker += 1
298
299
300 self.theme_name = new_theme_name
301
302
303 for (path, pixbuf) in self.pixbuf_dict.items():
304 pixbuf.update(self.get_theme_file_path("image/%s" % (path)))
305
306
307 theme_info = eval_file(self.get_theme_file_path(self.theme_info_file))
308
309 for (color_name, color) in theme_info["colors"].items():
310 self.color_dict[color_name].update(color)
311
312
313 for (color_name, color_info) in theme_info["alpha_colors"].items():
314 self.alpha_color_dict[color_name].update(color_info)
315
316
317 for (color_name, color_info) in theme_info["shadow_colors"].items():
318 self.shadow_color_dict[color_name].update(color_info)
319
320
321 ui_theme = Theme(os.path.join(get_parent_dir(__file__, 2), "theme"),
322 os.path.expanduser("~/.config/deepin-ui/theme"))
323