diff --git a/core/mui/gtk_form_dsl.rb b/core/mui/gtk_form_dsl.rb index 0fadc81..5027924 100644 --- a/core/mui/gtk_form_dsl.rb +++ b/core/mui/gtk_form_dsl.rb @@ -76,8 +76,8 @@ module Gtk::FormDSL # [label] ラベル # [config] キー # [dir] 初期のディレクトリ - def fileselect(label, config, _current=Dir.pwd, dir: _current, title: label.to_s) - fsselect(label, config, dir: dir, action: Gtk::FileChooser::ACTION_OPEN, title: title) + def fileselect(label, config, _current=Dir.pwd, dir: _current, title: label.to_s, shortcuts: [], filters: {}, use_preview: false) + fsselect(label, config, dir: dir, action: Gtk::FileChooser::ACTION_OPEN, title: title, shortcuts: shortcuts, filters: filters, use_preview: use_preview) end # ファイルを選択する @@ -94,8 +94,8 @@ module Gtk::FormDSL # [label] ラベル # [config] 設定のキー # [current] 初期のディレクトリ - def dirselect(label, config, _current=Dir.pwd, dir: _current, title: label.to_s) - fsselect(label, config, dir: dir, action: Gtk::FileChooser::ACTION_SELECT_FOLDER, title: title) + def dirselect(label, config, _current=Dir.pwd, dir: _current, title: label.to_s, shortcuts: []) + fsselect(label, config, dir: dir, action: Gtk::FileChooser::ACTION_SELECT_FOLDER, title: title, shortcuts: shortcuts) end # 一行テキストボックス @@ -374,12 +374,12 @@ module Gtk::FormDSL self[config] = w.font_name } button end - def fsselect(label, config, dir: Dir.pwd, action: Gtk::FileChooser::ACTION_OPEN, title: label) + def fsselect(label, config, dir: Dir.pwd, action: Gtk::FileChooser::ACTION_OPEN, title: label, shortcuts: [], filters: {}, use_preview: false) container = input(label, config) input = container.children.last.children.first button = Gtk::Button.new(Plugin[:settings]._('参照')) container.pack_start(button, false) - button.signal_connect(:clicked, &gen_fileselect_dialog_generator(title, action, dir, config: config, &input.method(:text=))) + button.signal_connect(:clicked, &gen_fileselect_dialog_generator(title, action, dir, config: config, shortcuts: shortcuts, filters: filters, use_preview: use_preview, &input.method(:text=))) container end @@ -443,7 +443,7 @@ module Gtk::FormDSL end end - def gen_fileselect_dialog_generator(title, action, dir, config:, &result_callback) + def gen_fileselect_dialog_generator(title, action, dir, config:, shortcuts: [], filters: {}, use_preview: false, &result_callback) ->(widget) do dialog = Gtk::FileChooserDialog.new(title, widget.get_ancestor(Gtk::Window), @@ -452,6 +452,42 @@ module Gtk::FormDSL [Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL], [Gtk::Stock::OPEN, Gtk::Dialog::RESPONSE_ACCEPT]) dialog.current_folder = File.expand_path(dir) + + filters.each do |name, exts| + filter = Gtk::FileFilter.new + filter.name = name + exts.each do |ext| + filter.add_pattern("*.#{ext}") + end + dialog.add_filter(filter) + end + + shortcuts.select { |dir| + !dialog.shortcut_folders.include?(dir) + }.each { |dir| + begin + dialog.add_shortcut_folder(dir) + rescue => e + puts e + puts e.backtrace + end + } + + if use_preview + preview = Gtk::Image.new + dialog.preview_widget = preview + dialog.signal_connect("update-preview") do + begin + path = dialog.preview_filename + pixbuf = Gdk::Pixbuf.new(file: path, width: 256, height: 256) + preview.set_pixbuf(pixbuf) + dialog.set_preview_widget_active(true) + rescue => e + dialog.set_preview_widget_active(false) + end + end + end + dialog.ssc_atonce(:response, &gen_fs_dialog_response_callback(config, &result_callback)) dialog.show_all false