Class: Shoes::Swt::TextBlock

Inherits:
Object
  • Object
show all
Includes:
Common::Clickable, Common::Remove, Common::Translate, Common::Visibility
Defined in:
shoes-swt/lib/shoes/swt/text_block.rb,
shoes-swt/lib/shoes/swt/text_block/fitter.rb,
shoes-swt/lib/shoes/swt/text_block/painter.rb,
shoes-swt/lib/shoes/swt/text_block/text_segment.rb,
shoes-swt/lib/shoes/swt/text_block/cursor_painter.rb,
shoes-swt/lib/shoes/swt/text_block/centered_text_segment.rb,
shoes-swt/lib/shoes/swt/text_block/text_segment_collection.rb

Direct Known Subclasses

Banner, Caption, Inscription, Para, Subtitle, Tagline, Title

Defined Under Namespace

Classes: CenteredTextSegment, CursorPainter, Fitter, Painter, TextSegment, TextSegmentCollection

Constant Summary collapse

DEFAULT_SPACING =
4
NEXT_ELEMENT_OFFSET =
1

Instance Attribute Summary collapse

Attributes included from Common::Clickable

#pass_coordinates

Instance Method Summary collapse

Methods included from Common::Visibility

#hidden?, #hidden_from_view?, #hide, #outside_parent_view?, #show, #toggle, #visible?

Methods included from Common::Translate

#clear_translate, #translate_left, #translate_top

Methods included from Common::Clickable

#click, #pass_coordinates?, #register_click, #release

Constructor Details

#initialize(dsl, app) ⇒ TextBlock

Returns a new instance of TextBlock.



18
19
20
21
22
23
24
# File 'shoes-swt/lib/shoes/swt/text_block.rb', line 18

def initialize(dsl, app)
  @dsl      = dsl
  @app      = app
  @segments = TextSegmentCollection.new(@dsl, [], default_text_styles)
  @painter  = Painter.new @dsl
  @app.add_paint_listener @painter
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



15
16
17
# File 'shoes-swt/lib/shoes/swt/text_block.rb', line 15

def app
  @app
end

#dslObject (readonly)

Returns the value of attribute dsl.



15
16
17
# File 'shoes-swt/lib/shoes/swt/text_block.rb', line 15

def dsl
  @dsl
end

#segmentsObject

Returns the value of attribute segments.



16
17
18
# File 'shoes-swt/lib/shoes/swt/text_block.rb', line 16

def segments
  @segments
end

Instance Method Details

#adjust_current_position(current_position) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'shoes-swt/lib/shoes/swt/text_block.rb', line 69

def adjust_current_position(current_position)
  current_position.y = @dsl.absolute_bottom + NEXT_ELEMENT_OFFSET

  last_segment = segments.last

  return if !last_segment || @bumped_to_next_line

  # Not quite sure why this is necessary. Could be a problem in some
  # other part of positioning, or something about how text layouts
  # actually draw themselves.
  current_position.x -= 1

  current_position.y -= last_segment.last_line_height
end

#bump_absolutes_to_next_lineObject



112
113
114
115
# File 'shoes-swt/lib/shoes/swt/text_block.rb', line 112

def bump_absolutes_to_next_line
  @dsl.absolute_right = @dsl.parent.absolute_left
  @bumped_to_next_line = true
end

#contents_alignment(current_position) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'shoes-swt/lib/shoes/swt/text_block.rb', line 40

def contents_alignment(current_position)
  dispose_existing_segments
  raw_segments = Fitter.new(self, current_position).fit_it_in

  if raw_segments&.any?
    @segments = TextSegmentCollection.new(@dsl, raw_segments, default_text_styles)
    set_absolutes_on_dsl(current_position)
    set_calculated_sizes
  else
    @segments = TextSegmentCollection.new(@dsl, [], default_text_styles)
  end
end

#default_text_stylesObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'shoes-swt/lib/shoes/swt/text_block.rb', line 53

def default_text_styles
  style = @dsl.style

  {
    fg: style[:fg],
    bg: style[:bg],
    strikecolor: style[:strikecolor],
    undercolor: style[:undercolor],
    font_detail: {
      name: @dsl.font,
      size: @dsl.size,
      styles: [::Swt::SWT::NORMAL]
    }
  }
end

#disposeObject



26
27
28
# File 'shoes-swt/lib/shoes/swt/text_block.rb', line 26

def dispose
  dispose_existing_segments
end

#in_bounds?(x, y) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
# File 'shoes-swt/lib/shoes/swt/text_block.rb', line 34

def in_bounds?(x, y)
  segments.any? do |segment|
    segment.in_bounds?(x, y)
  end
end

#removeObject



124
125
126
127
# File 'shoes-swt/lib/shoes/swt/text_block.rb', line 124

def remove
  super
  clear_contents
end

#replace(*_) ⇒ Object



129
130
131
# File 'shoes-swt/lib/shoes/swt/text_block.rb', line 129

def replace(*_)
  clear_contents
end

#set_absolutes(starting_left, starting_top) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'shoes-swt/lib/shoes/swt/text_block.rb', line 98

def set_absolutes(starting_left, starting_top)
  last_segment = segments.last

  # If we have an explicit width, use that.
  # If not, take our trailing line's width.
  width_to_offset = @dsl.element_width || last_segment.last_line_width

  @dsl.absolute_right  = starting_left + width_to_offset +
                         margin_right - NEXT_ELEMENT_OFFSET

  @dsl.absolute_bottom = starting_top + last_segment.height +
                         margin_top + margin_bottom - NEXT_ELEMENT_OFFSET
end

#set_absolutes_on_dsl(current_position) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'shoes-swt/lib/shoes/swt/text_block.rb', line 84

def set_absolutes_on_dsl(current_position)
  if segments.one?
    set_absolutes(@dsl.absolute_left, @dsl.absolute_top)
  else
    set_absolutes(@dsl.parent.absolute_left, current_position.next_line_start)
  end

  if trailing_newline?
    bump_absolutes_to_next_line
  else
    @bumped_to_next_line = false
  end
end

#set_calculated_sizesObject



117
118
119
120
121
122
# File 'shoes-swt/lib/shoes/swt/text_block.rb', line 117

def set_calculated_sizes
  @dsl.calculated_width  = segments.last.width
  @dsl.calculated_height = segments.inject(0) do |total, segment|
    total + segment.bounds.height
  end
end

#update_positionObject

has a painter, nothing to do



31
32
# File 'shoes-swt/lib/shoes/swt/text_block.rb', line 31

def update_position
end