Class: VER::Theme
- Inherits:
-
Struct
- Object
- Struct
- VER::Theme
- Defined in:
- lib/ver/theme.rb
Constant Summary collapse
- CACHE =
{}
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
- .list ⇒ Object
- .load(filename) ⇒ 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
Constructor Details
#initialize(colors = {}, &block) ⇒ Theme
Returns a new instance of Theme.
48 49 50 51 |
# File 'lib/ver/theme.rb', line 48 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 else # 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}.json") 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 |
.list ⇒ Object
5 6 7 |
# File 'lib/ver/theme.rb', line 5 def self.list VER.loadpath.map{|path| Dir[(path/'theme/*.json').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 json = JSON.load(File.read(filename.to_s)) uuid = json['uuid'] CACHE[uuid] ||= create(uuid, json) 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
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/ver/theme.rb', line 131 def apply_default_on() default.each do |key, value| case key.downcase when 'background' .configure background: value when 'caret' .configure insertbackground: value when 'foreground', 'fg' .configure foreground: value when 'invisibles' # TODO # widget.configure key => value when 'linehighlight' # TODO # widget.configure key => value when 'selection' .configure selectbackground: value else warn key => value .configure(key => value) end end end |
#create_tags_on(widget) ⇒ Object
155 156 157 158 159 |
# File 'lib/ver/theme.rb', line 155 def () colors.each do |name, | .tag_configure(name.to_s, ) end end |
#delete_tags_on(widget) ⇒ Object
167 168 169 170 171 |
# File 'lib/ver/theme.rb', line 167 def () colors.each do |name, option| .tag_delete(name.to_s) rescue nil end end |
#fontstyle_as_font(style) ⇒ Object
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/ver/theme.rb', line 90 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
58 59 60 61 62 63 64 65 |
# File 'lib/ver/theme.rb', line 58 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
101 102 103 |
# File 'lib/ver/theme.rb', line 101 def normalize(keyname) keyname.tr(' ', '-') end |
#remove_tags_on(widget, from, to) ⇒ Object
161 162 163 164 165 |
# File 'lib/ver/theme.rb', line 161 def (, from, to) colors.each do |name, | .tag_remove(name.to_s, from, to) rescue nil end end |
#sanitize_settings(given_settings) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/ver/theme.rb', line 71 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{6})/ settings[key] = $1 elsif key.downcase == 'fontstyle' settings['font'] = fontstyle_as_font(value) else settings[key] = value end end settings end |
#set(match, options) ⇒ Object
53 54 55 56 |
# File 'lib/ver/theme.rb', line 53 def set(match, ) match = normalize(match) colors[match] = (colors[match] || {}).merge(sanitize_settings()) end |