Class: FileTreePanel

Inherits:
Object
  • Object
show all
Defined in:
lib/vimamsa/gui_file_panel.rb

Constant Summary collapse

COL_LABEL =
0
COL_BUF_ID =

0 = folder row (not selectable)

1

Instance Method Summary collapse

Constructor Details

#initializeFileTreePanel

Returns a new instance of FileTreePanel.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/vimamsa/gui_file_panel.rb', line 5

def initialize
  @store = Gtk::TreeStore.new(String, Integer)
  @tree = Gtk::TreeView.new(@store)
  @tree.headers_visible = false
  @tree.activate_on_single_click = true

  renderer = Gtk::CellRendererText.new
  renderer.ellipsize = Pango::EllipsizeMode::START
  col = Gtk::TreeViewColumn.new("", renderer, text: COL_LABEL)
  col.expand = true
  @tree.append_column(col)

  @tree.signal_connect("row-activated") do |tv, path, _col|
    iter = @store.get_iter(path)
    next if iter.nil?
    buf_id = iter[COL_BUF_ID]
    next if buf_id.nil? || buf_id == 0
    vma.buffers.set_current_buffer(buf_id)
  end

  @context_menu = nil
  rightclick = Gtk::GestureClick.new
  rightclick.button = 3
  @tree.add_controller(rightclick)
  rightclick.signal_connect("pressed") do |gesture, n_press, x, y|
    result = @tree.get_path_at_pos(x.to_i, y.to_i)
    next unless result
    iter = @store.get_iter(result[0])
    next unless iter
    buf_id = iter[COL_BUF_ID]
    next unless buf_id && buf_id != 0
    @context_buf_id = buf_id
    show_context_menu(x, y)
  end

  @sw = Gtk::ScrolledWindow.new
  @sw.set_policy(:never, :automatic)
  @sw.set_child(@tree)
  @sw.set_size_request(180, -1)
  @sw.vexpand = true
end

Instance Method Details

#init_context_menuObject



51
52
53
54
55
56
57
58
# File 'lib/vimamsa/gui_file_panel.rb', line 51

def init_context_menu
  act = Gio::SimpleAction.new("fp_close_buffer")
  vma.gui.app.add_action(act)
  act.signal_connect("activate") { vma.buffers.close_buffer(@context_buf_id) if @context_buf_id }
  @context_menu = Gtk::PopoverMenu.new
  @context_menu.set_parent(@tree)
  @context_menu.has_arrow = false
end

#refreshObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/vimamsa/gui_file_panel.rb', line 69

def refresh
  @store.clear
  bh = {}
  vma.buffers.list.each do |b|
    dname = b.fname ? File.dirname(b.fname) : "*"
    bname = b.fname ? File.basename(b.fname) : (b.list_str || "(untitled)")
    bh[dname] ||= []
    bh[dname] << { bname: bname, buf: b }
  end

  bh.keys.sort.each do |dname|
    dir_iter = @store.append(nil)
    dir_iter[COL_LABEL] = "📂 #{tilde_path(dname)}"
    dir_iter[COL_BUF_ID] = 0
    bh[dname].sort_by { |x| x[:bname] }.each do |bnfo|
      active_mark = bnfo[:buf].is_active? ? "● " : "  "
      file_iter = @store.append(dir_iter)
      file_iter[COL_LABEL] = "#{active_mark}#{bnfo[:bname]}"
      file_iter[COL_BUF_ID] = bnfo[:buf].id
    end
  end

  @tree.expand_all
end

#show_context_menu(x, y) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/vimamsa/gui_file_panel.rb', line 60

def show_context_menu(x, y)
  init_context_menu if @context_menu.nil?
  menu = Gio::Menu.new
  menu.append("Close file", "app.fp_close_buffer")
  @context_menu.set_menu_model(menu)
  @context_menu.set_pointing_to(Gdk::Rectangle.new(x.to_i, y.to_i, 1, 1))
  @context_menu.popup
end

#widgetObject



47
48
49
# File 'lib/vimamsa/gui_file_panel.rb', line 47

def widget
  @sw
end