Class: Mireru::Widget::Image

Inherits:
Gtk::Image
  • Object
show all
Defined in:
lib/mireru/widget/image.rb

Instance Method Summary collapse

Constructor Details

#initialize(file, width, height) ⇒ Image

Returns a new instance of Image.



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/mireru/widget/image.rb', line 22

def initialize(file, width, height)
  super()
  @pixbuf = Gdk::PixbufAnimation.new(file)
  if @pixbuf.static_image?
    @pixbuf = @pixbuf.static_image
    if @pixbuf.width > width || @pixbuf.height > height
      scale_preserving_aspect_ratio(width, height)
    end
    self.pixbuf = @pixbuf
  else
    self.pixbuf_animation = @pixbuf
  end
end

Instance Method Details

#scale_preserving_aspect_ratio(width, height) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/mireru/widget/image.rb', line 36

def scale_preserving_aspect_ratio(width, height)
  if @pixbuf.is_a?(Gdk::PixbufAnimation)
    @pixbuf = @pixbuf.static_image
  end
  x_ratio = width.to_f / @pixbuf.width
  y_ratio = height.to_f / @pixbuf.height
  ratio = [x_ratio, y_ratio].min
  self.pixbuf = @pixbuf.scale(@pixbuf.width * ratio,
                              @pixbuf.height * ratio)
end