~orax/+junk/kupfer_themes

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
__kupfer_name__ = _("Crunch Theme")
__kupfer_sources__ = ()
__description__ = _("Use a Crunchbang Style theme")
__version__ = "1.0"
__author__ = "Jean-Philippe 'Orax' Roemer"

import os
import gtk

from kupfer import config
from kupfer import plugin_support

"""
Kupfer's UI can be themed by using the normal GtkRc style language
Theming can change colors and some pre-defined parameters, but
not the layout.

See also Documentation/GTKTheming.rst
      or https://github.com/engla/kupfer/blob/master/Documentation/GTKTheming.rst

For general information about GTK+ styles,
please see http://live.gnome.org/GnomeArt/Tutorials/GtkThemes

"""
CRUNCH_STYLE = """
style "crunch"
{
        ## Main Window
        KupferWindow :: corner-radius = 0
        KupferWindow :: opacity = 90
        KupferWindow :: decorated = 0
        KupferWindow :: border-width = 4

        ## Selected Item in Main Window
        MatchView :: corner-radius = 5
        MatchView :: opacity = 90

        ## Search List
        Search :: list-opacity = 93
        Search :: list-length = 200

        ## bg: background color
        bg[NORMAL] = "#424242"
        bg[SELECTED] = "#353535"
        bg[ACTIVE] = "#222"
        bg[PRELIGHT] = "#222"
        bg[INSENSITIVE] = "#333"

        ## fg: foreground text color
        fg[NORMAL] = "#DDD"
        fg[SELECTED] = "#EEE"
        fg[ACTIVE] = "#EEE"
        fg[PRELIGHT] = "#EEE"
        fg[INSENSITIVE] = "#DDD"

        ## text: text color in input widget and treeview
        text[NORMAL] = "#EEE"
        text[SELECTED] = "#EEE"
        text[ACTIVE] = "#EEE"

        ## base: background color in input widget and treeview
        base[NORMAL] = "#777"
        base[SELECTED] = "#100"
        base[ACTIVE] = "#112"
}

## The main window is kupfer
widget "kupfer" style "crunch"
widget "kupfer.*" style "crunch"

## The result list is kupfer-list
widget "kupfer-list.*" style "crunch"
"""

all_styles = {
	'default': None,
	'crunch': CRUNCH_STYLE,
}

__kupfer_settings__ = plugin_support.PluginSettings(
		{
			"key": "theme",
			"label": _("Theme:"),
			"type": str,
			"value": 'default',
			"alternatives": all_styles.keys(),
		},
	)

def cache_filename():
	return os.path.join(config.get_cache_home(), __name__)

def re_read_theme():
	## force re-read theme
	## FIXME: re-read on all screens
	settings = gtk.settings_get_default()
	gtk.rc_reparse_all_for_settings(settings, True)

def initialize_plugin(name):
	"""
	Theme changes are only reversible if we add
	and remove gtkrc files.
	"""
	use_theme(all_styles.get(__kupfer_settings__['theme']))
	__kupfer_settings__.connect_settings_changed_cb(on_change_theme)

def on_change_theme(sender, key, value):
	if key == 'theme':
		use_theme(all_styles.get(__kupfer_settings__[key]))

def use_theme(style_str):
	"""
	Use the GTK+ style in @style_str,
	or unset if it is None
	"""
	filename = cache_filename()
	if style_str is None:
		filename = cache_filename()
		gtk.rc_set_default_files([f for f in gtk.rc_get_default_files()
		                          if f != filename])
	else:
		with open(filename, "wb") as rcfile:
			rcfile.write(style_str)
		gtk.rc_add_default_file(filename)
	re_read_theme()

def finalize_plugin(name):
	use_theme(None)
	re_read_theme()
	## remove cache file
	filename = cache_filename()
	assert ("kupfer" in filename)
	try:
		os.unlink(filename)
	except OSError:
		pass