Class: Glimmer::LibUI::ControlProxy::TextProxy

Inherits:
Glimmer::LibUI::ControlProxy show all
Includes:
Parent, PerfectShaped, Transformable
Defined in:
lib/glimmer/libui/control_proxy/text_proxy.rb

Overview

Proxy for LibUI text objects

Follows the Proxy Design Pattern

Constant Summary

Constants inherited from Glimmer::LibUI::ControlProxy

BOOLEAN_PROPERTIES, KEYWORD_ALIASES, STRING_PROPERTIES, TransformProxy

Instance Attribute Summary

Attributes inherited from Glimmer::LibUI::ControlProxy

#args, #block, #content_added, #custom_control, #keyword, #libui, #options, #parent_custom_control, #parent_proxy, #slot

Instance Method Summary collapse

Methods included from PerfectShaped

#bounding_box, #contain?, #include?, #move, #move_by

Methods included from Parent

#children, #post_initialize_child

Methods included from Transformable

#apply_transform, #post_initialize_child, #transform, #undo_transform

Methods inherited from Glimmer::LibUI::ControlProxy

#append_properties, #append_property, #bind_content, constant_symbol, #content, control_proxies, control_proxy_class, create, #custom_listener_name_aliases, #custom_listener_names, #default_destroy, #deregister_all_custom_listeners, #deregister_custom_listeners, descendant_keyword_constant_map, #destroy_child, #enabled, exists?, #handle_custom_listener, #has_custom_listener?, image_proxies, keyword, #libui_api_keyword, #listeners, #listeners_for, main_window_proxy, map_descendant_keyword_constants_for, menu_proxies, #method_missing, new_control, #notify_custom_listeners, #post_initialize_child, reset_descendant_keyword_constant_map, #respond_to?, #respond_to_libui?, #send_to_libui, #visible, #window_proxy

Methods included from DataBindable

#data_bind, #data_bind_read, #data_bind_write, #data_binding_model_attribute_observer_registrations

Constructor Details

#initialize(keyword, parent, args, &block) ⇒ TextProxy

Returns a new instance of TextProxy.



38
39
40
41
42
43
44
# File 'lib/glimmer/libui/control_proxy/text_proxy.rb', line 38

def initialize(keyword, parent, args, &block)
  @keyword = keyword
  @parent_proxy = parent
  @args = args
  @block = block
  post_add_content if @block.nil?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Glimmer::LibUI::ControlProxy

Instance Method Details

#align(value = nil) ⇒ Object Also known as: align=, set_align



140
141
142
143
144
145
146
147
# File 'lib/glimmer/libui/control_proxy/text_proxy.rb', line 140

def align(value = nil)
  if value.nil?
    @align
  else
    @align = value
    redraw
  end
end

#area_proxyObject



160
161
162
163
# File 'lib/glimmer/libui/control_proxy/text_proxy.rb', line 160

def area_proxy
  # TODO eventually reuse this method from Shape
  find_parent_in_ancestors { |parent| parent.nil? || parent.is_a?(ControlProxy::AreaProxy) }
end

#attributed_stringObject



105
106
107
# File 'lib/glimmer/libui/control_proxy/text_proxy.rb', line 105

def attributed_string
  @attributed_string ||= reset_attributed_string
end

#calculate_extentsObject



224
225
226
227
228
229
230
231
232
# File 'lib/glimmer/libui/control_proxy/text_proxy.rb', line 224

def calculate_extents
  # TODO fix implementation once libui binding project responds about this
  # as it always returns 0,0 right now
  extent_width = Fiddle::Pointer.malloc(Fiddle::SIZEOF_DOUBLE*8)
  extent_height = Fiddle::Pointer.malloc(Fiddle::SIZEOF_DOUBLE*8)
  ::LibUI.draw_text_layout_extents(@libui, extent_width, extent_height)
  @extent_width = extent_width[0, Fiddle::SIZEOF_DOUBLE*8].unpack1('i')
  @extent_height = extent_height[0, Fiddle::SIZEOF_DOUBLE*8].unpack1('i')
end

#can_handle_listener?(listener_name) ⇒ Boolean

Returns:

  • (Boolean)


176
177
178
# File 'lib/glimmer/libui/control_proxy/text_proxy.rb', line 176

def can_handle_listener?(listener_name)
  area_proxy&.can_handle_listener?(listener_name)
end

#default_font(value = nil) ⇒ Object Also known as: default_font=, set_default_font



113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/glimmer/libui/control_proxy/text_proxy.rb', line 113

def default_font(value = nil)
  if value.nil?
    @default_font ||= {
      family: 'Helvetica',
      size: 12.0,
      weight: :normal,
      italic: :normal,
      stretch: :normal,
    }
  else
    @default_font = value
    redraw
  end
end

#default_font_descriptorObject



130
131
132
133
134
135
136
137
138
# File 'lib/glimmer/libui/control_proxy/text_proxy.rb', line 130

def default_font_descriptor
  @default_font_descriptor ||= ::LibUI::FFI::FontDescriptor.malloc
  @default_font_descriptor.Family = default_font[:family] || 'Helvetica'
  @default_font_descriptor.Size = default_font[:size] || 12.0
  @default_font_descriptor.Weight = Glimmer::LibUI.enum_symbol_to_value(:text_weight, default_font[:weight], default_symbol: :normal)
  @default_font_descriptor.Italic = Glimmer::LibUI.enum_symbol_to_value(:text_italic, default_font[:italic], default_symbol: :normal)
  @default_font_descriptor.Stretch = Glimmer::LibUI.enum_symbol_to_value(:text_stretch, default_font[:stretch], default_symbol: :normal)
  @default_font_descriptor
end

#destroyObject



63
64
65
66
67
68
# File 'lib/glimmer/libui/control_proxy/text_proxy.rb', line 63

def destroy
  return if ControlProxy.main_window_proxy&.destroying?
  deregister_all_custom_listeners
  @parent_proxy&.children&.delete(self)
  ControlProxy.control_proxies.delete(self)
end

#draw(area_draw_params) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/glimmer/libui/control_proxy/text_proxy.rb', line 54

def draw(area_draw_params)
  reset_attributed_string
  children.dup.each {|child| child.draw(area_draw_params)}
  build_control
  ::LibUI.draw_text(area_draw_params[:context], @libui, x, y)
  ::LibUI.draw_free_text_layout(@libui)
  ::LibUI.free_attributed_string(@attributed_string)
end

#draw_text_layout_paramsObject



151
152
153
154
155
156
157
158
# File 'lib/glimmer/libui/control_proxy/text_proxy.rb', line 151

def draw_text_layout_params
  @draw_text_layout_params ||= ::LibUI::FFI::DrawTextLayoutParams.malloc
  @draw_text_layout_params.String = attributed_string
  @draw_text_layout_params.DefaultFont = default_font_descriptor
  @draw_text_layout_params.Width = width
  @draw_text_layout_params.Align = Glimmer::LibUI.enum_symbol_to_value(:draw_text_align, align, default_symbol: :left)
  @draw_text_layout_params
end

#extent_heightObject



211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/glimmer/libui/control_proxy/text_proxy.rb', line 211

def extent_height
  if @extent_height.to_f > 0
    @extent_height
  else # TODO auto calculate extents
    children_max_size = children.map(&:font).map {|font| font[:size] if font.respond_to?(:[]) }.compact.max
    if children_max_size.to_f > 0
      children_max_size
    else
      @default_font[:size]
    end
  end
end

#extent_widthObject



203
204
205
206
207
208
209
# File 'lib/glimmer/libui/control_proxy/text_proxy.rb', line 203

def extent_width
  if @extent_width.to_f > 0
    @extent_width
  else # TODO auto calculate extents
    width
  end
end

#find_parent_in_ancestors(&condition) ⇒ Object



165
166
167
168
169
170
171
172
173
174
# File 'lib/glimmer/libui/control_proxy/text_proxy.rb', line 165

def find_parent_in_ancestors(&condition)
  # TODO eventually reuse this method from Shape
  found = self
  until condition.call(found)
    # TODO in the future, support nesting under composite shape where there is parent instead of parent_proxy
#             found = found.respond_to?(:parent_proxy) ? found.parent_proxy : found.parent
    found = found.parent_proxy
  end
  found
end

#handle_listener(listener_name, &listener) ⇒ Object



180
181
182
183
184
# File 'lib/glimmer/libui/control_proxy/text_proxy.rb', line 180

def handle_listener(listener_name, &listener)
  area_proxy.handle_listener(listener_name) do |event|
    listener.call(event) if include?(event[:x], event[:y])
  end
end

#perfect_shapeObject



186
187
188
189
190
191
192
193
194
# File 'lib/glimmer/libui/control_proxy/text_proxy.rb', line 186

def perfect_shape
  require 'perfect-shape'
  the_perfect_shape_dependencies = perfect_shape_dependencies
  if the_perfect_shape_dependencies != @perfect_shape_dependencies
    absolute_x, absolute_y, width, height = @perfect_shape_dependencies = the_perfect_shape_dependencies
    @perfect_shape = PerfectShape::Rectangle.new(x: absolute_x, y: absolute_y, width: width, height: height)
  end
  @perfect_shape
end

#perfect_shape_dependenciesObject



196
197
198
199
200
201
# File 'lib/glimmer/libui/control_proxy/text_proxy.rb', line 196

def perfect_shape_dependencies
  # TODO support absolute_x and absolute_y with relative positioning in the future
  absolute_x = x
  absolute_y = y
  [absolute_x, absolute_y, extent_width, extent_height]
end

#post_add_contentObject



46
47
48
49
50
51
52
# File 'lib/glimmer/libui/control_proxy/text_proxy.rb', line 46

def post_add_content
  super
  if @parent_proxy.nil? && AreaProxy.current_area_draw_params
    draw(AreaProxy.current_area_draw_params)
    destroy
  end
end

#redrawObject



70
71
72
# File 'lib/glimmer/libui/control_proxy/text_proxy.rb', line 70

def redraw
  @parent_proxy&.queue_redraw_all
end

#reset_attributed_stringObject



109
110
111
# File 'lib/glimmer/libui/control_proxy/text_proxy.rb', line 109

def reset_attributed_string
  @attributed_string = ::LibUI.new_attributed_string('')
end

#width(value = nil) ⇒ Object Also known as: width=, set_width



94
95
96
97
98
99
100
101
# File 'lib/glimmer/libui/control_proxy/text_proxy.rb', line 94

def width(value = nil)
  if value.nil?
    @width ||= args[2] || (AreaProxy.current_area_draw_params && (AreaProxy.current_area_draw_params[:area_width] - 2*x))
  else
    @width = value
    redraw
  end
end

#x(value = nil) ⇒ Object Also known as: x=, set_x



74
75
76
77
78
79
80
# File 'lib/glimmer/libui/control_proxy/text_proxy.rb', line 74

def x(value = nil)
  if value.nil?
    @x ||= args[0] || 0
  else
    @x = value
  end
end

#y(value = nil) ⇒ Object Also known as: y=, set_y



84
85
86
87
88
89
90
# File 'lib/glimmer/libui/control_proxy/text_proxy.rb', line 84

def y(value = nil)
  if value.nil?
    @y ||= args[1] || 0
  else
    @y = value
  end
end