Class: Glimmer::LibUI::ControlProxy::TableProxy

Inherits:
Glimmer::LibUI::ControlProxy show all
Includes:
FiddleConsumer
Defined in:
lib/glimmer/libui/control_proxy/table_proxy.rb

Overview

Proxy for LibUI table objects

Follows the Proxy Design Pattern

Constant Summary collapse

CUSTOM_LISTENER_NAMES =
['on_changed', 'on_edited']

Constants inherited from Glimmer::LibUI::ControlProxy

BOOLEAN_PROPERTIES, KEYWORD_ALIASES, STRING_PROPERTIES, TransformProxy

Instance Attribute Summary collapse

Attributes inherited from Glimmer::LibUI::ControlProxy

#args, #block, #content_added, #keyword, #libui, #parent_proxy

Instance Method Summary collapse

Methods included from FiddleConsumer

#fiddle_closure_block_caller

Methods inherited from Glimmer::LibUI::ControlProxy

#append_properties, #append_property, #can_handle_listener?, constant_symbol, #content, control_proxies, 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, #handle_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, reset_descendant_keyword_constant_map, #respond_to?, #respond_to_libui?, #send_to_libui, #visible, widget_proxy_class, #window_proxy

Methods included from DataBindable

#data_bind, #data_binding_model_attribute_observer_registrations

Constructor Details

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



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/glimmer/libui/control_proxy/table_proxy.rb', line 43

def initialize(keyword, parent, args, &block)
  @keyword = keyword
  @parent_proxy = parent
  @args = args
  @block = block
  @enabled = true
  @columns = []
  @cell_rows = []
  window_proxy.on_destroy do
    # the following unless condition is an exceptional condition stumbled upon that fails freeing the table model
    ::LibUI.free_table_model(@model) unless @destroyed && parent_proxy.is_a?(Box)
  end
end

Dynamic Method Handling

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

Instance Attribute Details

#column_attributesObject (readonly)

Returns the value of attribute column_attributes.



41
42
43
# File 'lib/glimmer/libui/control_proxy/table_proxy.rb', line 41

def column_attributes
  @column_attributes
end

#columnsObject (readonly)

Returns the value of attribute columns.



41
42
43
# File 'lib/glimmer/libui/control_proxy/table_proxy.rb', line 41

def columns
  @columns
end

#modelObject (readonly)

Returns the value of attribute model.



41
42
43
# File 'lib/glimmer/libui/control_proxy/table_proxy.rb', line 41

def model
  @model
end

#model_handlerObject (readonly)

Returns the value of attribute model_handler.



41
42
43
# File 'lib/glimmer/libui/control_proxy/table_proxy.rb', line 41

def model_handler
  @model_handler
end

#table_paramsObject (readonly)

Returns the value of attribute table_params.



41
42
43
# File 'lib/glimmer/libui/control_proxy/table_proxy.rb', line 41

def table_params
  @table_params
end

Instance Method Details

#array_deep_clone(array_or_object) ⇒ Object



173
174
175
176
177
178
179
180
181
# File 'lib/glimmer/libui/control_proxy/table_proxy.rb', line 173

def array_deep_clone(array_or_object)
  if array_or_object.is_a?(Array)
    array_or_object.map do |element|
      array_deep_clone(element)
    end
  else
    array_or_object.clone
  end
end

#cell_rows(rows = nil) ⇒ Object Also known as: cell_rows=, set_cell_rows



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/glimmer/libui/control_proxy/table_proxy.rb', line 82

def cell_rows(rows = nil)
  if rows.nil?
    @cell_rows
  else
    if rows != @cell_rows
      @cell_rows = rows
      @cell_rows = @cell_rows.to_a if @cell_rows.is_a?(Enumerator)
      @last_cell_rows ||= array_deep_clone(@cell_rows)
      @cell_rows_observer ||= Glimmer::DataBinding::Observer.proc do |new_cell_rows|
        if @cell_rows.size < @last_cell_rows.size && @last_cell_rows.include_all?(*@cell_rows)
          @last_cell_rows.array_diff_indexes(@cell_rows).reverse.each do |row|
            ::LibUI.table_model_row_deleted(model, row) if model && row
            notify_custom_listeners('on_changed', row, :deleted, @last_cell_rows[row])
          end
        elsif @cell_rows.size > @last_cell_rows.size && @cell_rows.include_all?(*@last_cell_rows)
          @cell_rows.array_diff_indexes(@last_cell_rows).each do |row|
            ::LibUI.table_model_row_inserted(model, row) if model && row
            notify_custom_listeners('on_changed', row, :inserted, @cell_rows[row])
          end
        else
          @cell_rows.each_with_index do |new_row_data, row|
            if new_row_data != @last_cell_rows[row]
              ::LibUI.table_model_row_changed(model, row) if model && row
              notify_custom_listeners('on_changed', row, :changed, @cell_rows[row])
            end
          end
        end
        @last_last_cell_rows = array_deep_clone(@last_cell_rows)
        @last_cell_rows = array_deep_clone(@cell_rows)
      end.tap do |cell_rows_observer|
        cell_rows_observer.observe(self, :cell_rows, recursive: true, ignore_frozen: true)
      end
    end
    @cell_rows
  end
end

#data_bind_read(property, model_binding) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/glimmer/libui/control_proxy/table_proxy.rb', line 143

def data_bind_read(property, model_binding)
  if model_binding.binding_options[:column_attributes].is_a?(Array)
    @column_attributes = model_binding.binding_options[:column_attributes]
  else
    column_attribute_mapping = model_binding.binding_options[:column_attributes].is_a?(Hash) ? model_binding.binding_options[:column_attributes] : {}
    @column_attributes = columns.select {|column| column.is_a?(Column)}.map(&:name).map {|column_name| column_attribute_mapping[column_name] || column_name.underscore}
  end
  model_attribute_observer = model_attribute_observer_registration = nil
  model_attribute_observer = Glimmer::DataBinding::Observer.proc do
    new_value = model_binding.evaluate_property
    new_value = new_value.to_a if new_value.is_a?(Enumerator)
    if model_binding.binding_options[:column_attributes] || (!new_value.empty? && !new_value.first.is_a?(Array))
      @model_attribute_array_observer_registration&.deregister
      @model_attribute_array_observer_registration = model_attribute_observer.observe(new_value, @column_attributes, ignore_frozen: true)
      model_attribute_observer.add_dependent(model_attribute_observer_registration => @model_attribute_array_observer_registration)
    end
    # TODO look if multiple notifications are happening as a result of observing array and observing model binding
    send("#{property}=", new_value) unless @last_cell_rows == new_value
  end
  model_attribute_observer_registration = model_attribute_observer.observe(model_binding)
  model_attribute_observer.call # initial update
  data_binding_model_attribute_observer_registrations << model_attribute_observer_registration
  model_attribute_observer
end

#data_bind_write(property, model_binding) ⇒ Object



168
169
170
171
# File 'lib/glimmer/libui/control_proxy/table_proxy.rb', line 168

def data_bind_write(property, model_binding)
  # TODO ensure writing is happening to models if rows are not arrays
  handle_listener('on_edited') { model_binding.call(cell_rows) } if property == 'cell_rows'
end

#destroyObject



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

def destroy
  super
  @cell_rows_observer&.unobserve(self, :cell_rows, recursive: true)
  @destroyed = true
end

#editable(value = nil) ⇒ Object Also known as: editable=, set_editable, editable?



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

def editable(value = nil)
  if value.nil?
    @editable
  else
    @editable = !!value
  end
end

#expand(cell_rows) ⇒ Object



125
126
127
128
129
130
# File 'lib/glimmer/libui/control_proxy/table_proxy.rb', line 125

def expand(cell_rows)
  cell_rows.to_a.map do |row|
    row = @column_attributes.map {|attribute| row.send(attribute) } if @column_attributes&.any? && !row.is_a?(Array)
    row.flatten(1)
  end
end

#expanded_cell_rowsObject



121
122
123
# File 'lib/glimmer/libui/control_proxy/table_proxy.rb', line 121

def expanded_cell_rows
  expand(cell_rows)
end

#post_add_contentObject



57
58
59
60
# File 'lib/glimmer/libui/control_proxy/table_proxy.rb', line 57

def post_add_content
  build_control unless @content_added
  super
end

#post_initialize_child(child) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/glimmer/libui/control_proxy/table_proxy.rb', line 62

def post_initialize_child(child)
  @columns << child
  # add extra complementary columns (:text, :color) if it is a dual/triple column (i.e. ImageTextColumnProxy or CheckboxTextColumnProxy
  case child
  when Column::ImageTextColumnProxy, Column::CheckboxTextColumnProxy
    @columns << :text
  when Column::TextColorColumnProxy
    @columns << :color
  when Column::CheckboxTextColorColumnProxy, Column::ImageTextColorColumnProxy
    @columns << :text
    @columns << :color
  end
end