Module: Alexandria::UI::Icons

Defined in:
lib/alexandria/ui/icons.rb

Constant Summary collapse

ICONS_DIR =
File.join(Alexandria::Config::DATA_DIR, 'icons')

Class Method Summary collapse

Class Method Details

.blank?(filename) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
93
94
95
96
97
# File 'lib/alexandria/ui/icons.rb', line 90

def self.blank?(filename)
  pixbuf = GdkPixbuf::Pixbuf.new(file: filename)
  (pixbuf.width == 1) && (pixbuf.height == 1)
rescue => err
  puts err.message
  puts err.backtrace.join("\n> ")
  true
end

.cover(library, book) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/alexandria/ui/icons.rb', line 76

def self.cover(library, book)
  begin
    return BOOK_ICON if library.nil?
    filename = library.cover(book)
    return GdkPixbuf::Pixbuf.new(file: filename) if File.exist?(filename)
  rescue => err
    # report load error; FIX should go to a Logger...
    puts err.message
    puts err.backtrace.join("\n> ")
    puts "Failed to load GdkPixbuf::Pixbuf, please ensure that from #{filename} is a valid image file"
  end
  BOOK_ICON
end

.initObject



60
61
62
# File 'lib/alexandria/ui/icons.rb', line 60

def self.init
  load_icon_images
end

.load_icon_imagesObject

loads icons from icons_dir location and gives them as uppercase constants to Alexandria::UI::Icons namespace, e.g., Icons::STAR_SET



66
67
68
69
70
71
72
73
74
# File 'lib/alexandria/ui/icons.rb', line 66

def self.load_icon_images
  Dir.entries(ICONS_DIR).each do |file|
    next unless file =~ /\.png$/ # skip non '.png' files
    # Don't use upcase and use tr instead
    # For example in Turkish the upper case of 'i' is still 'i'.
    name = File.basename(file, '.png').tr('a-z', 'A-Z')
    const_set(name, GdkPixbuf::Pixbuf.new(file: File.join(ICONS_DIR, file)))
  end
end