Class: Fidgit::FileBrowser

Inherits:
Composite show all
Defined in:
lib/fidgit/elements/file_browser.rb

Constant Summary collapse

VALID_TYPES =
[:open, :save]

Constants inherited from Composite

Composite::DEBUG_BORDER_COLOR

Constants inherited from Element

Element::DEFAULT_SCHEMA_FILE, Element::VALID_ALIGN_H, Element::VALID_ALIGN_V

Instance Attribute Summary collapse

Attributes inherited from Packer

#spacing_h, #spacing_v

Attributes inherited from Element

#align_h, #align_v, #background_color, #border_thickness, #font, #padding_bottom, #padding_left, #padding_right, #padding_top, #parent, #tip, #z

Instance Method Summary collapse

Methods inherited from Container

#add, #button, #clear, #color_picker, #color_well, #combo_box, #file_browser, #grid, #group, #hit_element, #horizontal, #image_frame, #insert, #label, #list, #radio_button, #remove, #scroll_area, #scroll_window, #slider, #text_area, #to_s, #toggle_button, #update, #vertical, #write_tree, #x=, #y=

Methods inherited from Element

#default, #drag?, #draw, #draw_frame, #draw_rect, #enabled=, #enabled?, #height, #height=, #hit?, #max_height, #max_width, #min_height, #min_width, new, original_new, #outer_height, #outer_width, #recalc, schema, #to_s, #update, #width, #width=, #with, #x, #x=, #y, #y=

Methods included from Event

#events, included, new_event_handlers, #publish, #subscribe, #unsubscribe

Constructor Details

#initialize(type, options = {}) ⇒ FileBrowser

Returns a new instance of FileBrowser.

Parameters:

  • type (Symbol)

    One of :open, :save

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :base_directory (String) — default: ''

    Outermost directory that the browser will see.

  • :directory (String) — default: current working directory

    .

  • :file_name (String) — default: ''

    Initially selected file in the directory.

  • :pattern (String) — default: '*.*'
  • :show_extension (Boolean) — default: true

Raises:

  • (ArgumentError)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
# File 'lib/fidgit/elements/file_browser.rb', line 26

def initialize(type, options = {})
  options = {
    base_directory: '',
    directory: Dir.pwd,
    file_name: '',
    pattern: default(:pattern),
    show_extension: default(:show_extension),
    width: 400,
    save_text: "Save",
    open_text: "Open",
    cancel_text: "Cancel",
  }.merge! options

  @type = type
  raise ArgumentError, "type must be one of #{VALID_TYPES}, not #{@type}" unless VALID_TYPES.include? @type

  @pattern = options[:pattern]
  @show_extension = options[:show_extension]
  @base_directory = options[:base_directory].chomp File::SEPARATOR

  @directories = options[:directory].sub(/^#{@base_directory}/, '').split(File::SEPARATOR)
  if @directories.first == ''
    @directories[0] = File::SEPARATOR
  end

  super options

  vertical do
    @nav_buttons = horizontal padding: 0, spacing: 2

    @scroll_window = scroll_window(height: 250, width: options[:width]) do
      @files_list = list(width: options[:width]) do
        subscribe :changed do |sender, file_path|
          if file_path
            file_name = File.basename file_path
            if File.directory? file_path
              @directories.push file_name
              create_nav_buttons
              update_files_list
            else
              @file_name_text.text = file_name
            end
          end
        end
      end
    end

    @file_name_text = text_area(text: options[:file_name], max_height: font.height * 1.5, width: options[:width], border_thickness: 1)

    create_nav_buttons

    horizontal align: :center, padding: 0 do
      @action_button = button(options[:"#{type}_text"]) do
        publish :selected, @type, file_path
      end

      button(options[:cancel_text]) do
        publish :selected, :cancel, file_path
      end
    end

    # Ensure that the open/save button is enabled only when the path is sensible.
    @file_name_text.subscribe :changed do |sender, text|
      @action_button.enabled = case @type
      when :open
        File.exists? file_path and not File.directory? file_path
      when :save
        not text.empty?
      end
    end

    update_files_list
  end
end

Instance Attribute Details

#base_directoryObject (readonly)

Returns the value of attribute base_directory.



9
10
11
# File 'lib/fidgit/elements/file_browser.rb', line 9

def base_directory
  @base_directory
end

#patternObject (readonly)

Returns the value of attribute pattern.



9
10
11
# File 'lib/fidgit/elements/file_browser.rb', line 9

def pattern
  @pattern
end

Instance Method Details

#directoryObject



12
13
14
15
16
# File 'lib/fidgit/elements/file_browser.rb', line 12

def directory
  dir = File.join(*@directories)
  dir = File.join(@base_directory, dir) unless @base_directory.empty?
  dir
end

#file_nameObject



17
# File 'lib/fidgit/elements/file_browser.rb', line 17

def file_name; @file_name_text.text; end

#file_pathObject



18
# File 'lib/fidgit/elements/file_browser.rb', line 18

def file_path; File.join(directory, file_name); end

#show_extension?Boolean

Returns:

  • (Boolean)


11
# File 'lib/fidgit/elements/file_browser.rb', line 11

def show_extension?; @show_extension; end