Class: Fidgit::FileBrowser
- Defined in:
- lib/fidgit/elements/file_browser.rb
Constant Summary collapse
- VALID_TYPES =
[:open, :save]
Constants inherited from Composite
Constants inherited from Element
Element::DEFAULT_SCHEMA_FILE, Element::VALID_ALIGN_H, Element::VALID_ALIGN_V
Instance Attribute Summary collapse
-
#base_directory ⇒ Object
readonly
Returns the value of attribute base_directory.
-
#pattern ⇒ Object
readonly
Returns the value of attribute pattern.
Attributes inherited from Packer
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
- #directory ⇒ Object
- #file_name ⇒ Object
- #file_path ⇒ Object
-
#initialize(type, options = {}) ⇒ FileBrowser
constructor
A new instance of FileBrowser.
- #show_extension? ⇒ Boolean
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.
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, = {}) = { 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! @type = type raise ArgumentError, "type must be one of #{VALID_TYPES}, not #{@type}" unless VALID_TYPES.include? @type @pattern = [:pattern] @show_extension = [:show_extension] @base_directory = [:base_directory].chomp File::SEPARATOR @directories = [:directory].sub(/^#{@base_directory}/, '').split(File::SEPARATOR) if @directories.first == '' @directories[0] = File::SEPARATOR end super vertical do @nav_buttons = horizontal padding: 0, spacing: 2 @scroll_window = scroll_window(height: 250, width: [:width]) do @files_list = list(width: [: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 update_files_list else @file_name_text.text = file_name end end end end end @file_name_text = text_area(text: [:file_name], max_height: font.height * 1.5, width: [:width], border_thickness: 1) horizontal align: :center, padding: 0 do @action_button = ([:"#{type}_text"]) do publish :selected, @type, file_path end ([: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_directory ⇒ Object (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 |
#pattern ⇒ Object (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
#directory ⇒ Object
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_name ⇒ Object
17 |
# File 'lib/fidgit/elements/file_browser.rb', line 17 def file_name; @file_name_text.text; end |
#file_path ⇒ Object
18 |
# File 'lib/fidgit/elements/file_browser.rb', line 18 def file_path; File.join(directory, file_name); end |
#show_extension? ⇒ Boolean
11 |
# File 'lib/fidgit/elements/file_browser.rb', line 11 def show_extension?; @show_extension; end |