Class: VER::Theme

Inherits:
Struct
  • Object
show all
Defined in:
lib/ver/theme.rb

Constant Summary collapse

CACHE =
{}
R4G4B4 =
'#%04x%04x%04x'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Struct

new

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

#colorsObject

Returns the value of attribute colors

Returns:

  • (Object)

    the current value of colors



2
3
4
# File 'lib/ver/theme.rb', line 2

def colors
  @colors
end

#defaultObject

Returns the value of attribute default

Returns:

  • (Object)

    the current value of default



2
3
4
# File 'lib/ver/theme.rb', line 2

def default
  @default
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



2
3
4
# File 'lib/ver/theme.rb', line 2

def name
  @name
end

#uuidObject

Returns the value of attribute uuid

Returns:

  • (Object)

    the current value of 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

.listObject



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

Raises:

  • (ArgumentError)


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(widget)
  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

  widget.default_theme_config = config
end

#create_tags_on(widget) ⇒ Object



174
175
176
177
178
# File 'lib/ver/theme.rb', line 174

def create_tags_on(widget)
  colors.each do |name, options|
    widget.tag_configure(name.to_s, options)
  end
end

#delete_tags_on(widget) ⇒ Object



190
191
192
193
194
# File 'lib/ver/theme.rb', line 190

def delete_tags_on(widget)
  colors.each do |name, option|
    widget.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)
  options = Font.default_options

  options[:slant]      = :italic if style =~ /\bitalic\b/
  options[:underline]  = true    if style =~ /\bunderline\b/
  options[:overstrike] = true    if style =~ /\boverstrike\b/
  options[:weight]     = :bold   if style =~ /\bbold\b/

  Font[options]
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, options|
    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 remove_tags_on(widget, from, to)
  outer_tags = widget.tag_names(from) & widget.tag_names(to)

  colors.each do |name, options|
    name = name.to_s
    next if outer_tags.include?(name)
    widget.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, options)
  match = normalize(match)
  colors[match] = (colors[match] || {}).merge(sanitize_settings(options))
end