Class: TkComponent::Builder::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/tk_component/builder/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Node

Returns a new instance of Node.



31
32
33
34
35
36
37
38
39
40
# File 'lib/tk_component/builder/node.rb', line 31

def initialize(name, options = {})
  @name = name
  @options = options.with_indifferent_access
  @sub_nodes = []
  @grid = {}.with_indifferent_access
  @weights = {}.with_indifferent_access
  @grid_map = GridMap.new
  @event_handlers = []
  @tk_item = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



131
132
133
134
135
136
137
138
139
# File 'lib/tk_component/builder/node.rb', line 131

def method_missing(method_name, *args, &block)
  if method_name.to_s.match(/(.*)\?/)
    return name.to_s == $1
  end
  if TOKENS.include?(method_name.to_s)
    return node_from_command(method_name, *args, &block)
  end
  super
end

Instance Attribute Details

#event_handlersObject

Returns the value of attribute event_handlers.



18
19
20
# File 'lib/tk_component/builder/node.rb', line 18

def event_handlers
  @event_handlers
end

#gridObject

Returns the value of attribute grid.



15
16
17
# File 'lib/tk_component/builder/node.rb', line 15

def grid
  @grid
end

#grid_mapObject

Returns the value of attribute grid_map.



17
18
19
# File 'lib/tk_component/builder/node.rb', line 17

def grid_map
  @grid_map
end

#nameObject

Returns the value of attribute name.



12
13
14
# File 'lib/tk_component/builder/node.rb', line 12

def name
  @name
end

#optionsObject

Returns the value of attribute options.



13
14
15
# File 'lib/tk_component/builder/node.rb', line 13

def options
  @options
end

#sub_nodesObject

Returns the value of attribute sub_nodes.



14
15
16
# File 'lib/tk_component/builder/node.rb', line 14

def sub_nodes
  @sub_nodes
end

#tk_itemObject

Returns the value of attribute tk_item.



19
20
21
# File 'lib/tk_component/builder/node.rb', line 19

def tk_item
  @tk_item
end

#weightsObject

Returns the value of attribute weights.



16
17
18
# File 'lib/tk_component/builder/node.rb', line 16

def weights
  @weights
end

Instance Method Details

#add_event_handler(name, lambda, options = {}) ⇒ Object



58
59
60
# File 'lib/tk_component/builder/node.rb', line 58

def add_event_handler(name, lambda, options = {})
  event_handlers << EventHandler.new(name, lambda, options)
end

#add_node(node) ⇒ Object



159
160
161
# File 'lib/tk_component/builder/node.rb', line 159

def add_node(node)
  sub_nodes << node
end

#apply_gridObject



73
74
75
# File 'lib/tk_component/builder/node.rb', line 73

def apply_grid
  self.tk_item.apply_internal_grid(grid_map)
end

#build(parent_node, parent_component) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/tk_component/builder/node.rb', line 62

def build(parent_node, parent_component)
  parent_item = parent_node.present? ? parent_node.tk_item : parent_component.tk_item
  self.tk_item = TkItem.create(parent_item, name, options, grid, event_handlers)
  parent_component.tk_item = self.tk_item if parent_component.tk_item.nil?
  sub_nodes.each do |n|
    n.build(self, parent_component)
  end
  apply_grid
  built
end

#builtObject



77
78
79
# File 'lib/tk_component/builder/node.rb', line 77

def built
  self.tk_item.built
end

#going_down?Boolean

Returns:

  • (Boolean)


155
156
157
# File 'lib/tk_component/builder/node.rb', line 155

def going_down?
  vframe?
end

#insert_component(component_class, parent_component, options = {}, &block) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/tk_component/builder/node.rb', line 49

def insert_component(component_class, parent_component, options = {}, &block)
  layout_options = options.slice(*LAYOUT_OPTIONS)
  c_node = node_from_command(:frame, layout_options, &block)
  comp = component_class.new(options.merge(parent: parent_component, parent_node: c_node))
  comp.generate(parent_component)
  parent_component.add_child(comp)
  comp
end

#node_from_command(method_name, *args, &block) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/tk_component/builder/node.rb', line 141

def node_from_command(method_name, *args, &block)
  if method_name.to_s == 'on_event'
    args[0] = "<#{args[0]}>"
    add_event_handler(*args)
  elsif method_name.to_s.match(/^on_(.*)/)
    add_event_handler($1, *args)
  else
    builder = self.class.new(method_name, *args)
    yield(builder) if block_given?
    add_node(builder)
    return builder
  end
end

#prepare_gridObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/tk_component/builder/node.rb', line 97

def prepare_grid
  self.grid_map = GridMap.new
  return unless self.sub_nodes.any?
  current_row = -1
  current_col = -1
  final_sub_nodes = []
  going_down = going_down?
  while (n = sub_nodes.shift) do
    if n.row?
      current_row += 1
      current_col = 0
      sub_nodes.unshift(*n.sub_nodes)
    else
      # Set the initial row and cols if no row was specified
      current_row = 0 if current_row < 0
      current_col = 0 if current_col < 0
      current_row, current_col = grid_map.get_next_cell(current_row, current_col, going_down)
      binding.pry if n.options.nil?
      n.grid = {}.with_indifferent_access if n.grid.nil?
      n.grid.merge!(n.options.extract!(:column, :row, :rowspan, :columnspan, :sticky))
      n.grid.merge!(column: current_col, row: current_row)
      rowspan = n.grid[:rowspan] || 1
      columnspan = n.grid[:columnspan] || 1
      grid_map.fill(current_row, current_col, rowspan, columnspan, true)
      n.weights = {}.with_indifferent_access if n.weights.nil?
      n.weights.merge!(n.options.extract!(:x_flex, :y_flex))
      grid_map.set_weights(current_row, current_col, n.weights)
      n.prepare_grid
      final_sub_nodes << n
    end
  end
  self.sub_nodes = final_sub_nodes
end

#prepare_option_events(component) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/tk_component/builder/node.rb', line 88

def prepare_option_events(component)
  option_events = options.extract!(*EVENT_CMDS)
  option_events.each do |k, v|
    event_proc = v.is_a?(Proc) ? v : proc { |e| component.public_send(v, e) }
    node_from_command(k, event_proc)
  end
  sub_nodes.each { |n| n.prepare_option_events(component) }
end

#rebuiltObject



81
82
# File 'lib/tk_component/builder/node.rb', line 81

def rebuilt
end

#removeObject



84
85
86
# File 'lib/tk_component/builder/node.rb', line 84

def remove
  self.tk_item.remove
end

#short(level = 0) ⇒ Object



42
43
44
45
46
47
# File 'lib/tk_component/builder/node.rb', line 42

def short(level = 0)
  @sub_nodes.each do |n|
    n.short(level + 4)
  end
  nil
end