Class: TkComponent::Base

Inherits:
Object
  • Object
show all
Includes:
BasicComponent
Defined in:
lib/tk_component/base/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BasicComponent

#focus

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.



15
16
17
18
19
# File 'lib/tk_component/base/base.rb', line 15

def initialize(options = {})
  @parent = options[:parent]
  @parent_node = options[:parent_node]
  @children = []
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



10
11
12
# File 'lib/tk_component/base/base.rb', line 10

def children
  @children
end

#nodeObject

Returns the value of attribute node.



11
12
13
# File 'lib/tk_component/base/base.rb', line 11

def node
  @node
end

#parentObject

Returns the value of attribute parent.



8
9
10
# File 'lib/tk_component/base/base.rb', line 8

def parent
  @parent
end

#parent_nodeObject

Returns the value of attribute parent_node.



9
10
11
# File 'lib/tk_component/base/base.rb', line 9

def parent_node
  @parent_node
end

#tk_itemObject

Returns the value of attribute tk_item.



7
8
9
# File 'lib/tk_component/base/base.rb', line 7

def tk_item
  @tk_item
end

Instance Method Details

#add_child(child) ⇒ Object



118
119
120
121
# File 'lib/tk_component/base/base.rb', line 118

def add_child(child)
  binding.pry if children.nil?
  children << child
end

#build(parent_component) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/tk_component/base/base.rb', line 51

def build(parent_component)
  @node.build(@parent_node, parent_component)
  component_did_build
  children.each do |c|
    c.build(self)
    TkGrid.columnconfigure c.parent_node.tk_item.native_item, 0, weight: 1
    TkGrid.rowconfigure c.parent_node.tk_item.native_item, 0, weight: 1
    TkGrid.columnconfigure c.node.tk_item.native_item, 0, weight: 1
    TkGrid.rowconfigure c.node.tk_item.native_item, 0, weight: 1
  end
end

#component_did_buildObject



115
116
# File 'lib/tk_component/base/base.rb', line 115

def component_did_build
end

#emit(event_name) ⇒ Object



111
112
113
# File 'lib/tk_component/base/base.rb', line 111

def emit(event_name)
  TkComponent::Builder::Event.emit(event_name, parent_node.native_item, self.object_id)
end

#generate(parent_component) ⇒ Object



25
26
27
28
29
# File 'lib/tk_component/base/base.rb', line 25

def generate(parent_component)
  parse_component(parent_component) do |p|
    render(p, parent_component)
  end
end

#nameObject



107
108
109
# File 'lib/tk_component/base/base.rb', line 107

def name
  self.class.name
end

#parse_component(parent_component) {|@node| ... } ⇒ Object

Yields:



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

def parse_component(parent_component)
  raise "You need to provide a block" unless block_given?
  @node = Builder::Node.new(:top)
  yield(@node)
  binding.pry if @node.sub_nodes.size != 1
  raise "Components need to have a single root node" unless @node.sub_nodes.size == 1
  @node.prepare_option_events(self)
  @node.prepare_grid
  @node = @node.sub_nodes.first # Get rid of the dummy top node
end

#parse_nodes(parent_node, options = {}) {|parent_node| ... } ⇒ Object

Yields:



42
43
44
45
46
47
48
49
# File 'lib/tk_component/base/base.rb', line 42

def parse_nodes(parent_node, options = {})
  old_sub_nodes = parent_node.sub_nodes.dup
  yield(parent_node)
  new_sub_nodes = parent_node.sub_nodes - old_sub_nodes
  new_sub_nodes.each { |n| n.prepare_option_events(self) }
  parent_node.prepare_grid
  new_sub_nodes
end

#rebuild(old_node) ⇒ Object



103
104
105
# File 'lib/tk_component/base/base.rb', line 103

def rebuild(old_node)
  build(parent)
end

#regenerateObject



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/tk_component/base/base.rb', line 63

def regenerate
  old_node = @node
  old_children = @children
  @children = []
  generate(parent)
  rebuild(old_node)
  @children.each do |c|
    c.generate(self)
    c.build(self)
  end
end

#regenerate_after_node(node, parent_node, &block) ⇒ Object



79
80
81
82
# File 'lib/tk_component/base/base.rb', line 79

def regenerate_after_node(node, parent_node, &block)
  return if parent_node.sub_nodes.index(node).nil?
  regenerate_from_index(parent_node, parent_node.sub_nodes.index(node) + 1, &block)
end

#regenerate_from_index(parent_node, index, &block) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/tk_component/base/base.rb', line 84

def regenerate_from_index(parent_node, index, &block)
  old_children = @children.dup
  to_remove = parent_node.sub_nodes.slice!(index..-1)
  to_remove.each do |n|
    n.remove
  end
  new_sub_nodes = parse_nodes(parent_node, &block)
  new_children = @children - old_children
  new_sub_nodes.each do |n|
    n.build(parent_node, self)
  end
  new_children.each do |c|
    c.generate(self)
    c.build(self)
  end
  parent_node.apply_grid
  parent_node.built
end

#regenerate_from_node(node, parent_node, &block) ⇒ Object



75
76
77
# File 'lib/tk_component/base/base.rb', line 75

def regenerate_from_node(node, parent_node, &block)
  regenerate_from_index(parent_node, parent_node.sub_nodes.index(node), &block)
end

#render(p, parent_component) ⇒ Object



21
22
23
# File 'lib/tk_component/base/base.rb', line 21

def render(p, parent_component)
  raise "Component #{self.class.to_s} needs to have a 'render' method"
end