| 1 |
|
| 2 |
|
| 3 |
require_relative 'edit_window' |
| 4 |
require_relative 'extract_tab_list' |
| 5 |
|
| 6 |
Plugin.create :extract_gtk do |
| 7 |
settings _('抽出タブ') do |
| 8 |
tablist = Plugin::ExtractGtk::ExtractTabList.new(Plugin[:extract]) |
| 9 |
tablist.hexpand = true |
| 10 |
tablist.vexpand = true |
| 11 |
|
| 12 |
btn_add = Gtk::Button.new(stock_id: Gtk::Stock::ADD) |
| 13 |
btn_edit = Gtk::Button.new(stock_id: Gtk::Stock::EDIT) |
| 14 |
btn_delete = Gtk::Button.new(stock_id: Gtk::Stock::DELETE) |
| 15 |
btn_add.ssc(:clicked) do |
| 16 |
Plugin.call(:extract_tab_open_create_dialog, toplevel) |
| 17 |
true |
| 18 |
end |
| 19 |
btn_edit.ssc(:clicked) do |
| 20 |
slug = tablist.selected_slug |
| 21 |
Plugin.call(:extract_open_edit_dialog, slug) if slug |
| 22 |
true |
| 23 |
end |
| 24 |
btn_delete.ssc(:clicked) do |
| 25 |
slug = tablist.selected_slug |
| 26 |
Plugin.call(:extract_tab_delete_with_confirm, toplevel, slug) if slug |
| 27 |
true |
| 28 |
end |
| 29 |
|
| 30 |
grid = Gtk::Grid.new |
| 31 |
grid.column_spacing = 6 |
| 32 |
grid << Gtk::ScrolledWindow.new.add(tablist) |
| 33 |
grid << Gtk::Grid.new.tap do |subgrid| |
| 34 |
subgrid.orientation = :vertical |
| 35 |
subgrid.row_spacing = 6 |
| 36 |
subgrid << btn_add << btn_edit << btn_delete |
| 37 |
end |
| 38 |
|
| 39 |
add grid |
| 40 |
|
| 41 |
add_tab_observer = on_extract_tab_create(&tablist.method(:add_record)) |
| 42 |
update_tab_observer = on_extract_tab_update(&tablist.method(:update_record)) |
| 43 |
delete_tab_observer = on_extract_tab_delete(&tablist.method(:remove_record)) |
| 44 |
tablist.ssc(:destroy) do |
| 45 |
detach add_tab_observer |
| 46 |
detach update_tab_observer |
| 47 |
detach delete_tab_observer |
| 48 |
end |
| 49 |
end |
| 50 |
|
| 51 |
on_extract_tab_delete_with_confirm do |window, slug| |
| 52 |
extract = Plugin[:extract].extract_tabs[slug] |
| 53 |
extract or next |
| 54 |
|
| 55 |
message = _('本当に抽出タブ「%{name}」を削除しますか?') % { name: extract.name } |
| 56 |
|
| 57 |
dialog = Gtk::MessageDialog.new(parent: window, |
| 58 |
type: :question, |
| 59 |
buttons: :none, |
| 60 |
message:) |
| 61 |
dialog.add_button Gtk::Stock::CANCEL, :reject |
| 62 |
btn_remove = dialog.add_button Gtk::Stock::REMOVE, :accept |
| 63 |
btn_remove.style_context.add_class 'destructive-action' |
| 64 |
case dialog.run |
| 65 |
when Gtk::ResponseType::ACCEPT |
| 66 |
Plugin.call(:extract_tab_delete, slug) |
| 67 |
end |
| 68 |
dialog.destroy |
| 69 |
end |
| 70 |
|
| 71 |
on_extract_tab_open_create_dialog do |window| |
| 72 |
dialog = Gtk::Dialog.new(title: _('抽出タブを作成 - %{mikutter}') % { mikutter: Environment::NAME }, parent: window) |
| 73 |
dialog.add_button(Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_REJECT) |
| 74 |
dialog.add_button(Gtk::Stock::OK, Gtk::Dialog::RESPONSE_ACCEPT).tap do |button| |
| 75 |
button.style_context.add_class('suggested-action') |
| 76 |
end |
| 77 |
prompt = Gtk::Entry.new |
| 78 |
prompt.hexpand = true |
| 79 |
box = Gtk::Box.new(:horizontal, 8).tap do |subbox| |
| 80 |
subbox.pack_start(Gtk::Label.new(_('名前')), expand: false) |
| 81 |
subbox.add(prompt) |
| 82 |
subbox.show_all |
| 83 |
end |
| 84 |
dialog.child.add(box) |
| 85 |
|
| 86 |
case dialog.run |
| 87 |
when Gtk::ResponseType::ACCEPT |
| 88 |
Plugin.call(:extract_tab_create, Plugin::Extract::Setting.new(name: prompt.text)) |
| 89 |
end |
| 90 |
dialog.destroy |
| 91 |
end |
| 92 |
|
| 93 |
on_extract_open_edit_dialog do |extract_slug| |
| 94 |
window = Plugin::ExtractGtk::EditWindow.new(Plugin[:extract].extract_tabs[extract_slug], self) |
| 95 |
event = on_extract_tab_update do |setting| |
| 96 |
if extract_slug == setting.slug && !window.destroyed? |
| 97 |
window.refresh_title |
| 98 |
end |
| 99 |
end |
| 100 |
window.ssc(:destroy) do |
| 101 |
event.detach |
| 102 |
false |
| 103 |
end |
| 104 |
end |
| 105 |
end |