Class: Canopus::IconTheme

Inherits:
Object
  • Object
show all
Defined in:
lib/canopus/icon_theme.rb,
sig/workspace_services.rbs

Overview

Theme JSON maps file/directory/expanded_directory and extensions to local SVGs.

Constant Summary collapse

FILE =

Returns:

  • (String)
'<svg viewBox="0 0 16 16"><path d="M3 1h6l4 4v10H3z M9 1v4h4" fill="none" stroke="currentColor" stroke-linejoin="round"/></svg>'
FOLDER =

Returns:

  • (String)
'<svg viewBox="0 0 16 16"><path d="M1 4V2h5l2 2h7v10H1z" fill="none" stroke="currentColor" stroke-linejoin="round"/></svg>'

Instance Method Summary collapse

Constructor Details

#initialize(path = nil) ⇒ IconTheme

Returns a new instance of IconTheme.

Parameters:

  • path (String, nil) (defaults to: nil)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/canopus/icon_theme.rb', line 8

def initialize(path = nil)
  @icons, @extensions = {}, {}
  if path
    raise Error, "icon theme exceeds 256 KiB" if File.size(path) > 262_144
    document = Kochab.parse(File.read(path))
    raise Error, "invalid icon theme" unless document.valid? && document.value.is_a?(Hash)
    @root, @definition = File.realpath(File.dirname(path)), document.value
    @extensions = @definition.fetch("extensions", {})
    raise Error, "icon extensions must be a map with at most 256 entries" unless @extensions.is_a?(Hash) && @extensions.length <= 256
    paths = @definition.values_at("file", "directory", "expanded_directory").compact + @extensions.values
    paths.uniq.each { |value| load_icon(value) }
  else
    @definition = {}
  end
  @icons[:file] = Zaniah::SVG.new(FILE)
  @icons[:directory] = Zaniah::SVG.new(FOLDER)
end

Instance Method Details

#texture(path, directory: false, expanded: false, size: 16, scale: 1, color: "#999") ⇒ Zaniah::GPU::Texture

Parameters:

  • path (String)
  • directory: (Boolean) (defaults to: false)
  • expanded: (Boolean) (defaults to: false)
  • size: (Numeric) (defaults to: 16)
  • scale: (Numeric) (defaults to: 1)
  • color: (String) (defaults to: "#999")

Returns:

  • (Zaniah::GPU::Texture)


25
26
27
28
29
30
31
32
33
# File 'lib/canopus/icon_theme.rb', line 25

def texture(path, directory: false, expanded: false, size: 16, scale: 1, color: "#999")
  key = if directory
    @definition[expanded ? "expanded_directory" : "directory"] || @definition["directory"] || :directory
  else
    @extensions[File.extname(path)] || @extensions[File.basename(path)] || @definition["file"] || :file
  end
  pixels = (size * scale).ceil
  @icons.fetch(key).texture(width: pixels, height: pixels, color: color)
end