Class: VimMate::FilesWindow

Inherits:
Object
  • Object
show all
Defined in:
lib/vimmatelib/files_window.rb

Overview

The window that contains the file tree

Constant Summary collapse

NAME =

Column for the file name

0
PATH =

Column for the full path of the file

1
ICON =

Column for the icon of the file

2
SORT =

Column used to sort the files

3
TYPE =

Column used to store the type of row

4
STATUS =

Column used to store the status of the file

5
VISIBLE =

Visiblity of row

6
TYPE_FILE =

Type of row: file

0
TYPE_DIRECTORY =

Type of row: directory

1
TYPE_SEPARATOR =

Type of row: separator

2

Instance Method Summary collapse

Constructor Details

#initialize(exclude_file_list = []) ⇒ FilesWindow

Create a FilesWindow



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/vimmatelib/files_window.rb', line 59

def initialize(exclude_file_list = [])
  @open_signal = Set.new
  @menu_signal = Set.new
  @expander_signal = Set.new

  @filter_string = ""
  
  # Tree Store: Filename, Full path, Icon, Sort, Type, Status
  @gtk_tree_store = Gtk::TreeStore.new(String,
                                       String,
                                       Gdk::Pixbuf,
                                       String,
                                       Fixnum,
                                       String,
                                       FalseClass)
  @gtk_tree_store.set_sort_column_id(SORT)

  # Filtered Tree Store
  @gtk_filtered_tree_model = Gtk::TreeModelFilter.new(@gtk_tree_store)
  @gtk_filtered_tree_model.set_visible_func do |model, iter|
    if @filter_string.nil? or @filter_string.empty?
      true
    else
      iter[VISIBLE]
    end
  end
  
  # Tree View
  @gtk_tree_view = Gtk::TreeView.new(@gtk_filtered_tree_model)
  @gtk_tree_view.selection.mode = Gtk::SELECTION_SINGLE
  @gtk_tree_view.headers_visible = Config[:file_headers_visible]
  @gtk_tree_view.hover_selection = Config[:file_hover_selection]

  # Double-click, Enter, Space: Signal to open the file
  @gtk_tree_view.signal_connect("row-activated") do |view, path, column|
    path = @gtk_filtered_tree_model.get_iter(path)[PATH]
    @open_signal.each do |signal|
      signal.call(path,
                  Config[:files_default_open_in_tabs] ? :tab_open : :open)
    end
  end

  # Left-click: Select and Signal to open the menu
  @gtk_tree_view.signal_connect("button_press_event") do |widget, event|
    if event.kind_of? Gdk::EventButton and event.button == 3
      path = @gtk_tree_view.get_path_at_pos(event.x, event.y)
      @gtk_tree_view.selection.select_path(path[0]) if path

      selected = @gtk_tree_view.selection.selected
      if selected
        @menu_signal.each do |signal|
          signal.call(selected[PATH])
        end
      end
    end
  end

  # Create a label to show the path of the file
  gtk_label = Gtk::Label.new
  gtk_label.ellipsize = Pango::Layout::EllipsizeMode::START

  # When a selection is changed in the tree view, we change the label
  # to show the path of the file
  @gtk_tree_view.selection.signal_connect("changed") do
    gtk_label.text = ""
    next if (selected_row = @gtk_tree_view.selection.selected).nil?
    gtk_label.text = File.join(File.dirname(selected_row[PATH]), selected_row[NAME])
  end
  
  # Same thing as Left-click, but with the keyboard
  @gtk_tree_view.signal_connect("popup_menu") do
    selected = @gtk_tree_view.selection.selected
    if selected
      @menu_signal.each do |signal|
        signal.call(selected[PATH])
      end
    end
  end

  # Separator between directories
  @gtk_tree_view.set_row_separator_func do |model, iter|
    iter[TYPE] == TYPE_SEPARATOR
  end

  # Add the columns
  column = Gtk::TreeViewColumn.new
  column.title = "Files"

  # Icon
  icon_cell_renderer = Gtk::CellRendererPixbuf.new
  column.pack_start(icon_cell_renderer, false)
  column.set_attributes(icon_cell_renderer, :pixbuf => ICON)

  # File name
  text_cell_renderer = Gtk::CellRendererText.new
  if Config[:files_use_ellipsis]
    text_cell_renderer.ellipsize = Pango::Layout::EllipsizeMode::MIDDLE
  end
  column.pack_start(text_cell_renderer, true)
  column.set_attributes(text_cell_renderer, :text => NAME)
  
  # Status
  if Config[:files_show_status]
    text_cell_renderer2 = Gtk::CellRendererText.new
    if Config[:files_use_ellipsis]
      text_cell_renderer2.ellipsize = Pango::Layout::EllipsizeMode::END
    end
    column.pack_start(text_cell_renderer2, true)
    column.set_attributes(text_cell_renderer2, :text => STATUS)
  end
  
  @gtk_tree_view.append_column(column)
  
  # Put the tree view in a scroll window
  @gtk_scrolled_window = Gtk::ScrolledWindow.new
  @gtk_scrolled_window.set_policy(Gtk::POLICY_AUTOMATIC,
                                  Gtk::POLICY_AUTOMATIC)
  @gtk_scrolled_window.add(@gtk_tree_view)
  
  # Set the default size for the file list
  @gtk_scrolled_window.set_size_request(Config[:files_opened_width], -1)

  # Create a box to filter the list
  gtk_filter_box = Gtk::HBox.new
  gtk_filter_box.pack_start(gtk_filter_button = Gtk::ToggleButton.new("Filter"), false, false)
  gtk_filter_box.pack_start(@gtk_file_filter_entry = Gtk::Entry.new, true, true)
  changed_lambda = lambda do
    if gtk_filter_button.active?
      self.filter = @gtk_file_filter_entry.text
    else
      self.clear_filter
    end
  end
  @gtk_file_filter_entry.signal_connect("changed", &changed_lambda)
  gtk_filter_button.signal_connect("toggled", &changed_lambda)
  gtk_filter_button.active = Config[:files_filter_active]
  gtk_filter_box.spacing = 10
  gtk_filter_box.border_width = 10

  # Create the file tree
  initialize_file_tree(exclude_file_list)
  
  gtk_top_box = Gtk::VBox.new
  gtk_top_box.pack_start(gtk_filter_box, false, false)
  gtk_top_box.pack_start(@gtk_scrolled_window, true, true)
  gtk_top_box.pack_start(gtk_label, false, false)

  # Create the search file list if it's enabled
  if Config[:files_use_search]
    @gtk_paned_box = Gtk::VPaned.new
    @gtk_paned_box.add(gtk_top_box)
    @gtk_paned_box.add((@search_window = SearchWindow.new(@file_tree)).gtk_window)
    @gtk_paned_box.position = Config[:files_search_separator_position]

    # Set the signals for the search window
    @search_window.add_open_signal do |path, kind|
      @open_signal.each do |signal|
        signal.call(path, kind)
      end
    end
    @search_window.add_menu_signal do |path|
      @menu_signal.each do |signal|
        signal.call(path)
      end
    end
  end

  
  @gtk_expander = Gtk::Expander.new("File list")
  @gtk_expander.expanded = Config[:files_expanded]
  if Config[:files_use_search]
    @gtk_expander.add(@gtk_paned_box)
  else
    @gtk_expander.add(gtk_top_box)
  end
  @gtk_expander.signal_connect("notify::expanded") do
    @expander_signal.each do |signal|
      signal.call(@gtk_expander.expanded?)
    end
  end

  gtk_window.border_width = 5

  @file_tree_mutex = Mutex.new
end

Instance Method Details

#add_expander_signal(&block) ⇒ Object

Add a block that will be called when the user choose to expand or close the expander. The block takes one argument: if the expander is opened or closed



356
357
358
# File 'lib/vimmatelib/files_window.rb', line 356

def add_expander_signal(&block)
  @expander_signal << block
end

#add_menu_signal(&block) ⇒ Object

Add a block that will be called when the user choose to open the menu. The block takes one argument: the path to the file to open.



349
350
351
# File 'lib/vimmatelib/files_window.rb', line 349

def add_menu_signal(&block)
  @menu_signal << block
end

#add_open_signal(&block) ⇒ Object

Add a block that will be called when the user choose to open a file The block take two argument: the path to the file to open, and a symbol to indicate the kind: :open, :split_open, :tab_open



343
344
345
# File 'lib/vimmatelib/files_window.rb', line 343

def add_open_signal(&block)
  @open_signal << block
end

#add_path(path) ⇒ Object

Recursively add a path at the root of the tree



246
247
248
249
250
251
# File 'lib/vimmatelib/files_window.rb', line 246

def add_path(path)
  @file_tree_mutex.synchronize do
    @file_tree.add_path(path)
  end
  self
end

#clear_filterObject

Clear the filter



312
313
314
315
316
317
# File 'lib/vimmatelib/files_window.rb', line 312

def clear_filter
  @filter_string = ""
  @gtk_filtered_tree_model.refilter
  @gtk_tree_view.collapse_all if Config[:files_auto_expand_on_filter]
  filter
end

#expand_first_rowObject

Expand the first row of the file tree



335
336
337
338
# File 'lib/vimmatelib/files_window.rb', line 335

def expand_first_row
  @gtk_tree_view.collapse_all
  @gtk_tree_view.expand_row(Gtk::TreePath.new("0"), false)
end

#filterObject

Get the filter: files must contain this string



265
266
267
# File 'lib/vimmatelib/files_window.rb', line 265

def filter
  @filter_string
end

#filter=(filter) ⇒ Object

Set a filter: files must contain this string



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/vimmatelib/files_window.rb', line 270

def filter=(filter)
  if filter.empty?
    clear_filter
    return
  end
  @filter_string = filter
  
  # Filter tree view so only directories and separators with matching
  # elements are set visible
  visible_path = Hash.new(false)
  
  @gtk_tree_store.each do |model,path,iter|
    if iter[NAME] and iter[TYPE] == TYPE_FILE
      if iter[VISIBLE] = iter[NAME].index(@filter_string)
        begin
          visible_path[path.to_s] = true
        end while path.up!
      end
    else
      iter[VISIBLE] = true
      if iter[TYPE] == TYPE_SEPARATOR
        visible_path[path.to_s] = true
      end
    end
  end

  @gtk_tree_store.each do |model,path,iter|
    if not visible_path[path.to_s]
      iter[VISIBLE] = false
      if iter[TYPE] == TYPE_DIRECTORY and Config[:file_directory_separator]
        if iter.next!
          iter[VISIBLE] = false
        end
      end
    end
  end

  @gtk_filtered_tree_model.refilter
  @gtk_tree_view.expand_all if Config[:files_auto_expand_on_filter]
end

#focus_file_filterObject

Set the focus to the file filter



320
321
322
# File 'lib/vimmatelib/files_window.rb', line 320

def focus_file_filter
  @gtk_file_filter_entry.has_focus = true if @gtk_file_filter_entry
end

#focus_file_listObject

Set the focus to the file list



325
326
327
# File 'lib/vimmatelib/files_window.rb', line 325

def focus_file_list
  @gtk_tree_view.has_focus = true if @gtk_tree_view
end

#focus_file_searchObject

Set the focus to the search file list



330
331
332
# File 'lib/vimmatelib/files_window.rb', line 330

def focus_file_search
  @search_window.focus_file_search if @search_window
end

#gtk_windowObject

The “window” for this object



254
255
256
# File 'lib/vimmatelib/files_window.rb', line 254

def gtk_window
  @gtk_expander
end

#initial_add(&block) ⇒ Object

Indicates that the initial file adding is going on. The timer to refresh the list is started after the initial add.



362
363
364
365
366
367
368
369
# File 'lib/vimmatelib/files_window.rb', line 362

def initial_add(&block)
  @file_tree.initial_add(&block)
  # Launch a timer to refresh the file list
  Gtk.timeout_add(Config[:files_refresh_interval] * 1000) do 
    do_refresh
    true
  end
end

#refreshObject

Refresh the file list



259
260
261
262
# File 'lib/vimmatelib/files_window.rb', line 259

def refresh
  do_refresh
  self
end