class TkComponent::BrowserComponent

Attributes

data_source[RW]
selected_path[RW]

Public Class Methods

new(options = {}) click to toggle source
Calls superclass method TkComponent::Base::new
# 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

Public Instance Methods

generate_from_level(container, start_index) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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_selection() click to toggle source
# File lib/tk_component/components/browser_component.rb, line 27
def show_current_selection
  @trees.each { |t| t.tk_item.scroll_to_selection }
end