Class: VER::Theme
Constant Summary collapse
- CACHE =
{}
- R4G4B4 =
'#%04x%04x%04x'
Instance Attribute Summary collapse
-
#colors ⇒ Object
Returns the value of attribute colors.
-
#default ⇒ Object
Returns the value of attribute default.
-
#name ⇒ Object
Returns the value of attribute name.
-
#uuid ⇒ Object
Returns the value of attribute uuid.
Class Method Summary collapse
- .create(uuid, hash) ⇒ Object
- .find(theme_name) ⇒ Object
- .find_and_load(theme_name) ⇒ Object
- .invert_rgb(color) ⇒ Object
- .list ⇒ Object
- .load(filename) ⇒ Object
- .tm_color_to_tk_color(color) ⇒ Object
Instance Method Summary collapse
-
#apply_default_on(widget) ⇒ Object
-background or -bg, background, Background -borderwidth or -bd, borderWidth, BorderWidth -cursor, cursor, Cursor -font, font, Font -foreground or -fg, foreground, Foreground.
- #create_tags_on(widget) ⇒ Object
- #delete_tags_on(widget) ⇒ Object
- #fontstyle_as_font(style) ⇒ Object
- #get(name) ⇒ Object
-
#initialize(colors = {}, &block) ⇒ Theme
constructor
A new instance of Theme.
- #normalize(keyname) ⇒ Object
- #remove_tags_on(widget, from, to) ⇒ Object
- #sanitize_settings(given_settings) ⇒ Object
- #set(match, options) ⇒ Object
Methods inherited from Struct
Constructor Details
#initialize(colors = {}, &block) ⇒ Theme
Returns a new instance of Theme.
65 66 67 68 |
# File 'lib/ver/theme.rb', line 65 def initialize(colors = {}, &block) self.colors = colors instance_eval(&block) if block_given? end |
Instance Attribute Details
#colors ⇒ Object
Returns the value of attribute colors
2 3 4 |
# File 'lib/ver/theme.rb', line 2 def colors @colors end |
#default ⇒ Object
Returns the value of attribute default
2 3 4 |
# File 'lib/ver/theme.rb', line 2 def default @default end |
#name ⇒ Object
Returns the value of attribute name
2 3 4 |
# File 'lib/ver/theme.rb', line 2 def name @name end |
#uuid ⇒ Object
Returns the value of attribute uuid
2 3 4 |
# File 'lib/ver/theme.rb', line 2 def uuid @uuid end |
Class Method Details
.create(uuid, hash) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/ver/theme.rb', line 22 def self.create(uuid, hash) instance = new instance.name = hash[:name] instance.uuid = uuid hash[:settings].each do |setting| next unless settings = setting[:settings] if scope_names = setting[:scope] # specific settings scope_names.split(/\s*,\s*/).each do |scope_name| instance.set(scope_name, settings) end elsif !settings.empty? # general settings instance.default = settings end end return instance end |
.find(theme_name) ⇒ Object
9 10 11 |
# File 'lib/ver/theme.rb', line 9 def self.find(theme_name) VER.find_in_loadpath("theme/#{theme_name}.rb") end |
.find_and_load(theme_name) ⇒ Object
44 45 46 |
# File 'lib/ver/theme.rb', line 44 def self.find_and_load(theme_name) load(find(theme_name)) end |
.invert_rgb(color) ⇒ Object
60 61 62 63 |
# File 'lib/ver/theme.rb', line 60 def self.invert_rgb(color) xcolor = FFI::Tk::get_color(Tk.interp, color) R4G4B4 % [0xffff - xcolor.red, 0xffff - xcolor.green, 0xffff - xcolor.blue] end |
.list ⇒ Object
5 6 7 |
# File 'lib/ver/theme.rb', line 5 def self.list VER.loadpath.map{|path| Dir[(path/'theme/*.rb').to_s] }.flatten end |
.load(filename) ⇒ Object
13 14 15 16 17 18 19 20 |
# File 'lib/ver/theme.rb', line 13 def self.load(filename) raise(ArgumentError, "No path to theme file given") unless filename hash = eval(File.read(filename.to_s)) uuid = hash[:uuid] CACHE[uuid] ||= create(uuid, hash) end |
.tm_color_to_tk_color(color) ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/ver/theme.rb', line 50 def self.tm_color_to_tk_color(color) case color when /^(#\h{6})\h{2}$/, /^(#\h{6})$/,/^(#\h{3})\h$/ color = $1 end xcolor = FFI::Tk::get_color(Tk.interp, color) R4G4B4 % [xcolor.red, xcolor.green, xcolor.blue] end |
Instance Method Details
#apply_default_on(widget) ⇒ Object
-background or -bg, background, Background -borderwidth or -bd, borderWidth, BorderWidth -cursor, cursor, Cursor -font, font, Font -foreground or -fg, foreground, Foreground
-highlightbackground, highlightBackground, HighlightBackground -highlightcolor, highlightColor, HighlightColor -highlightthickness, highlightThickness, HighlightThickness
-insertbackground, insertBackground, Foreground -insertborderwidth, insertBorderWidth, BorderWidth
-insertofftime, insertOffTime, OffTime -insertontime, insertOnTime, OnTime
-selectbackground, selectBackground, Foreground -selectborderwidth, selectBorderWidth, BorderWidth -selectforeground, selectForeground, Background
-inactiveselectbackground, inactiveSelectBackground, Foreground
-spacing1, spacing1, Spacing1 -spacing2, spacing2, Spacing2 -spacing3, spacing3, Spacing3
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/ver/theme.rb', line 148 def apply_default_on() config = {} default.each do |key, value| case key.downcase when :background, :bg config[:background] = value when :caret config[:insertbackground] = value when :foreground, :fg config[:foreground] = value when :invisibles, :linehighlight # TODO # widget.configure key => value when :selection config[:selectbackground] = value config[:selectforeground] = Theme.invert_rgb(value) else warn key => value config[key] = value end end .default_theme_config = config end |
#create_tags_on(widget) ⇒ Object
174 175 176 177 178 |
# File 'lib/ver/theme.rb', line 174 def () colors.each do |name, | .tag_configure(name.to_s, ) end end |
#delete_tags_on(widget) ⇒ Object
190 191 192 193 194 |
# File 'lib/ver/theme.rb', line 190 def () colors.each do |name, option| .tag_delete(name.to_s) rescue nil end end |
#fontstyle_as_font(style) ⇒ Object
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/ver/theme.rb', line 107 def fontstyle_as_font(style) = Font. [:slant] = :italic if style =~ /\bitalic\b/ [:underline] = true if style =~ /\bunderline\b/ [:overstrike] = true if style =~ /\boverstrike\b/ [:weight] = :bold if style =~ /\bbold\b/ Font[] end |
#get(name) ⇒ Object
75 76 77 78 79 80 81 82 |
# File 'lib/ver/theme.rb', line 75 def get(name) name = normalize(name) colors.each do |syntax_name, | return syntax_name if name.start_with?(syntax_name) end nil end |
#normalize(keyname) ⇒ Object
118 119 120 |
# File 'lib/ver/theme.rb', line 118 def normalize(keyname) keyname.tr(' ', '-') end |
#remove_tags_on(widget, from, to) ⇒ Object
180 181 182 183 184 185 186 187 188 |
# File 'lib/ver/theme.rb', line 180 def (, from, to) = .tag_names(from) & .tag_names(to) colors.each do |name, | name = name.to_s next if .include?(name) .tag_remove(name, from, to) rescue nil end end |
#sanitize_settings(given_settings) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/ver/theme.rb', line 88 def sanitize_settings(given_settings) settings = given_settings.dup settings.keys.each do |key| next unless value = settings.delete(key) next if value.empty? if value =~ /^#\h+$/ settings[key] = Theme.tm_color_to_tk_color(value) elsif key.downcase == :fontstyle settings[:font] = fontstyle_as_font(value) else settings[key] = value end end settings end |
#set(match, options) ⇒ Object
70 71 72 73 |
# File 'lib/ver/theme.rb', line 70 def set(match, ) match = normalize(match) colors[match] = (colors[match] || {}).merge(sanitize_settings()) end |