Class: Teek::UI::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/teek/ui/image.rb

Overview

A DSL-declared image, backed by teek core's Photo (which owns the underlying Tk image's GC lifetime - see its own docs).

Declared via WidgetDSL#image at build time - its Tcl image name is allocated purely in Ruby, no interpreter needed yet (same shape as Var's own Tcl variable name), so it can be captured as a widget's image: option (or a later handle.configure(image: ...) value) before realize even happens. The real Photo - and the actual file load - only exists at #realize.

#to_s returns the Tcl name (matching Photo's own convention), so passing an Image directly as an image: option value works through teek's ordinary option-value serialization - no special-casing needed anywhere in the widget DSL for it.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, path, opts) ⇒ Image

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Image.



26
27
28
29
30
31
# File 'lib/teek/ui/image.rb', line 26

def initialize(name, path, opts)
  @name = name
  @path = path
  @opts = opts
  @photo = nil
end

Instance Attribute Details

#nameString (readonly)

Returns the Tcl image name.

Returns:

  • (String)

    the Tcl image name



23
24
25
# File 'lib/teek/ui/image.rb', line 23

def name
  @name
end

Instance Method Details

#photoTeek::Photo

Returns the live, GC-owned Tk photo image, loaded from the file path this was declared with.

Returns:

  • (Teek::Photo)

    the live, GC-owned Tk photo image, loaded from the file path this was declared with

Raises:



36
37
38
# File 'lib/teek/ui/image.rb', line 36

def photo
  @photo or raise NotRealizedError
end

#realize(app) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Loads the backing Photo from this image's file path. Called once by Session#realize, before the widget tree realizes, so any widget's image: option already resolves to a real, loaded image by the time it's created.



45
46
47
# File 'lib/teek/ui/image.rb', line 45

def realize(app)
  @photo = Teek::Photo.new(app, name: @name, file: @path, **@opts)
end

#to_sString

Returns the Tcl image name - lets this be passed directly as a widget's image: option, at build time or via a later handle.configure(image: ...), the same way Photo itself already can be.

Returns:

  • (String)

    the Tcl image name - lets this be passed directly as a widget's image: option, at build time or via a later handle.configure(image: ...), the same way Photo itself already can be.



53
54
55
# File 'lib/teek/ui/image.rb', line 53

def to_s
  @name
end