Class: MittensUi::Image

Inherits:
Core
  • Object
show all
Defined in:
lib/mittens_ui/image.rb

Overview

An image widget that displays a static image or animated GIF. Wraps Gtk::Image. Click events are handled via Gtk::GestureClick.

Examples:

Basic image

img = MittensUi::Image.new('./assets/logo.png', width: 200, height: 200)

With click handler

img = MittensUi::Image.new('./assets/logo.png')
img.click { puts 'image clicked!' }

Animated GIF

img = MittensUi::Image.new('./assets/animation.gif')

Instance Attribute Summary collapse

Attributes inherited from Core

#core_widget

Instance Method Summary collapse

Methods inherited from Core

#hidden?, #hide, #keyboard_shortcut, #remove, #remove_keyboard_shortcut, #render, #shortcuts, #show

Methods included from Helpers

#icon_map, #list_system_icons, #set_margin_from_opts_for

Constructor Details

#initialize(path, options = {}) ⇒ Image

Creates a new Image widget.

Parameters:

  • path (String)

    path to the image file

  • options (Hash) (defaults to: {})

    configuration options

Options Hash (options):

  • :tooltip_text (String) — default: ''

    tooltip shown on hover

  • :width (Integer) — default: 80

    image width in pixels

  • :height (Integer) — default: 80

    image height in pixels

  • :width (Symbol) — default: :full

    column width in the layout grid

  • :defer_render (Boolean) — default: false

    skip auto-rendering into layout



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/mittens_ui/image.rb', line 30

def initialize(path, options = {})
  @path        = path.strip
  tooltip_text = options[:tooltip_text] || ''
  @img_width   = options[:width]        || 80
  @img_height  = options[:height]       || 80

  if @path.include?('.gif')
    init_gif
  else
    init_static_image
  end

  @image.tooltip_text = tooltip_text

  # GTK4: use a Picture widget for better size control if available,
  # otherwise wrap in a fixed-size container
  @container = Gtk::Box.new(:vertical, 0)
  @container.set_size_request(@img_width, @img_height)
  @container.halign = :start
  @container.valign = :start
  @container.append(@image)

  super(@container, options)
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



19
20
21
# File 'lib/mittens_ui/image.rb', line 19

def path
  @path
end

Instance Method Details

#click { ... } ⇒ void

This method returns an undefined value.

Connects a block to the click event.

Examples:

img.click { puts 'clicked!' }

Yields:

  • called when the image is clicked



61
62
63
64
65
# File 'lib/mittens_ui/image.rb', line 61

def click
  gesture = Gtk::GestureClick.new
  gesture.signal_connect('pressed') { yield }
  @container.add_controller(gesture)
end