Class: TkComponent::BrowserComponent

Inherits:
Base
  • Object
show all
Defined in:
lib/tk_component/components/browser_component.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#children, #node, #parent, #parent_node, #tk_item

Instance Method Summary collapse

Methods inherited from Base

#add_child, #build, #component_did_build, #emit, #generate, #name, #parse_component, #parse_nodes, #rebuild, #regenerate, #regenerate_after_node, #regenerate_from_index, #regenerate_from_node

Methods included from BasicComponent

#focus

Constructor Details

#initialize(options = {}) ⇒ BrowserComponent

Returns a new instance of BrowserComponent.



7
8
9
10
11
12
13
14
# File 'lib/tk_component/components/browser_component.rb', line 7

def initialize(options = {})
  super
  @data_source = options[:data_source]
  @selected_path = options[:selected_path] || []
  @paned = !!options[:paned]
  @trees_container = nil
  @trees = []
end

Instance Attribute Details

#data_sourceObject

Returns the value of attribute data_source.



4
5
6
# File 'lib/tk_component/components/browser_component.rb', line 4

def data_source
  @data_source
end

#selected_pathObject

Returns the value of attribute selected_path.



5
6
7
# File 'lib/tk_component/components/browser_component.rb', line 5

def selected_path
  @selected_path
end

Instance Method Details

#generate_from_level(container, start_index) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/tk_component/components/browser_component.rb', line 31

def generate_from_level(container, start_index)
  (start_index..selected_path.size).each do |idx|
    next_in_path = selected_path[idx]
    partial_path = selected_path.slice(0, idx)
    items = data_source.items_for_path(partial_path)
    title = items.nil? ? '' : data_source.title_for_path(partial_path, items)
    @trees << container.tree(sticky: 'nsew', x_flex: 1, y_flex: 1,
                             scrollers: 'y', heading: title) do |t|
      items&.each do |item|
        t.tree_node(at: 'end', text: item, selected: item == next_in_path)
      end
      t.on_select ->(e) { select_item(e.sender, idx) }
    end
  end
end

#render(p, parent_component) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/tk_component/components/browser_component.rb', line 16

def render(p, parent_component)
  partial_path = []
  @trees = []
  p.vframe(sticky: 'nsew', x_flex: 1, y_flex: 1) do |vf|
    command = @paned ? :hpaned : :hframe
    @trees_container = vf.send(command, sticky: 'nsew', x_flex: 1, y_flex: 1) do |f|
      generate_from_level(f, 0)
    end
  end
end

#select_item(sender, index) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/tk_component/components/browser_component.rb', line 47

def select_item(sender, index)
  # If we don't do this update sometimes the selection is not
  # updated until some later event, like a mouse drag
  Tk.update
  item = sender.native_item.selection&.first.text.to_s
  return if selected_path[index] == item
  selected_path[index] = item
  selected_path.slice!(index + 1..-1) if index < selected_path.size - 1
  @trees.slice!(index + 1..-1)
  regenerate_after_node(@trees[index], @trees_container) do |container|
    generate_from_level(container, index + 1)
  end
  Tk.update
  emit('PathChanged')
  Tk.update
end

#show_current_selectionObject



27
28
29
# File 'lib/tk_component/components/browser_component.rb', line 27

def show_current_selection
  @trees.each { |t| t.tk_item.scroll_to_selection }
end