Class: Fidgit::TextLine

Inherits:
Element show all
Defined in:
lib/fidgit/elements/text_line.rb

Overview

Used internally by the label.

Direct Known Subclasses

ToolTip

Constant Summary

VALID_JUSTIFICATION =
[:left, :right, :center]

Constants inherited from Element

Element::DEFAULT_SCHEMA_FILE, Element::VALID_ALIGN_H, Element::VALID_ALIGN_V

Instance Attribute Summary (collapse)

Attributes inherited from Element

#align_h, #align_v, #background_color, #border_thickness, #font, #padding_bottom, #padding_left, #padding_right, #padding_top, #parent, #tip, #z

Instance Method Summary (collapse)

Methods inherited from Element

#default, #drag?, #draw, #draw_frame, #draw_rect, #enabled=, #enabled?, #height, #height=, #hit?, #max_height, #max_width, #min_height, new, original_new, #outer_height, #outer_width, #recalc, schema, #update, #width, #width=, #with, #x, #x=, #y, #y=

Methods included from Event

#events, included, new_event_handlers, #publish, #subscribe, #unsubscribe

Constructor Details

- (TextLine) initialize(text, options = {})

Returns a new instance of TextLine

Options Hash (options):

  • :justify (:left, :right, :center) — default: :left

    Text justification.



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/fidgit/elements/text_line.rb', line 37

def initialize(text, options = {})
  options = {
      color: default(:color),
      justify: default(:justify),
  }.merge! options

  super(options)

  self.justify = options[:justify]
  self.color = options[:color]
  self.text = text
end

Instance Attribute Details

- (Object) color

Returns the value of attribute color



6
7
8
# File 'lib/fidgit/elements/text_line.rb', line 6

def color
  @color
end

- (Object) justify

Returns the value of attribute justify



6
7
8
# File 'lib/fidgit/elements/text_line.rb', line 6

def justify
  @justify
end

Instance Method Details

- (Object) draw_foreground



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fidgit/elements/text_line.rb', line 50

def draw_foreground
  case @justify
    when :left
      rel_x = 0.0
      draw_x = x + padding_left

    when :right
      rel_x = 1.0
      draw_x = x + rect.width - padding_right

    when :center
      rel_x = 0.5
      draw_x = (x + padding_left) + (rect.width - padding_right - padding_left) / 2.0
  end

  font.draw_rel(@text, draw_x, y + padding_top, z, rel_x, 0, 1, 1, color)
end

- (Object) min_width



68
69
70
71
72
73
74
# File 'lib/fidgit/elements/text_line.rb', line 68

def min_width
  if @text.empty?
    [padding_left + padding_right, super].max
  else
    [padding_left + font.text_width(@text) + padding_right, super].max
  end
end

- (Object) text



16
# File 'lib/fidgit/elements/text_line.rb', line 16

def text; @text.dup; end

- (Object) text=(text)

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
25
# File 'lib/fidgit/elements/text_line.rb', line 18

def text=(text)
  raise ArgumentError.new("Text must be a String") unless text.respond_to? :to_s

  @text = text.to_s.dup

  recalc
  text
end

- (Object) to_s



88
89
90
# File 'lib/fidgit/elements/text_line.rb', line 88

def to_s
  "#{super} '#{@text}'"
end