| 1 |
|
| 2 |
module ::Plugin::ChangeAccount |
| 3 |
class AccountControl < Gtk::TreeView |
| 4 |
include Gtk::TreeViewPrettyScroll |
| 5 |
COL_ICON = 0 |
| 6 |
COL_NAME = 1 |
| 7 |
COL_WORLD_NAME = 2 |
| 8 |
COL_WORLD = 3 |
| 9 |
|
| 10 |
type_register |
| 11 |
signal_new(:delete_world, GLib::Signal::RUN_FIRST, nil, nil, Array) |
| 12 |
|
| 13 |
def initialize(plugin) |
| 14 |
@plugin = plugin |
| 15 |
super() |
| 16 |
set_model(::Gtk::ListStore.new(GdkPixbuf::Pixbuf, String, String, Object)) |
| 17 |
append_column ::Gtk::TreeViewColumn.new("", ::Gtk::CellRendererPixbuf.new, pixbuf: COL_ICON) |
| 18 |
append_column ::Gtk::TreeViewColumn.new("name", ::Gtk::CellRendererText.new, text: COL_NAME) |
| 19 |
append_column ::Gtk::TreeViewColumn.new("provider", ::Gtk::CellRendererText.new, text: COL_WORLD_NAME) |
| 20 |
self.set_reorderable(true) |
| 21 |
content_initialize |
| 22 |
register_signal_handlers |
| 23 |
event_listener_initialize |
| 24 |
end |
| 25 |
|
| 26 |
def selected_worlds |
| 27 |
self.selection.to_enum(:selected_each).map {|model, path, iter| |
| 28 |
iter[COL_WORLD] |
| 29 |
} |
| 30 |
end |
| 31 |
|
| 32 |
private |
| 33 |
|
| 34 |
def content_initialize |
| 35 |
Plugin.collect(:worlds).each(&method(:add_column)) |
| 36 |
end |
| 37 |
|
| 38 |
def add_column(world) |
| 39 |
iter = model.append |
| 40 |
if world.respond_to?(:icon) && world.icon |
| 41 |
iter[COL_ICON] = world.icon.load_pixbuf(width: Gdk.scale(24), height: Gdk.scale(24)) do |loaded| |
| 42 |
iter[COL_ICON] = loaded unless destroyed? |
| 43 |
end |
| 44 |
end |
| 45 |
iter[COL_NAME] = world.title |
| 46 |
iter[COL_WORLD_NAME] = world.class.slug |
| 47 |
iter[COL_WORLD] = world |
| 48 |
end |
| 49 |
|
| 50 |
def menu_pop(widget, event) |
| 51 |
contextmenu = Gtk::ContextMenu.new |
| 52 |
contextmenu.register("削除") do |
| 53 |
signal_emit(:delete_world, selected_worlds) |
| 54 |
end |
| 55 |
contextmenu.popup(widget, widget) |
| 56 |
end |
| 57 |
|
| 58 |
def event_listener_initialize |
| 59 |
tag = @plugin.handler_tag do |
| 60 |
@plugin.on_world_after_created do |world| |
| 61 |
add_column(world) |
| 62 |
end |
| 63 |
@plugin.on_world_destroy do |world| |
| 64 |
_, _, iter = model.enum_for.find{ |_, _, i| i[COL_WORLD] == world } |
| 65 |
model.remove(iter) if iter |
| 66 |
end |
| 67 |
end |
| 68 |
register_detach_listener_at_destroy(tag) |
| 69 |
end |
| 70 |
|
| 71 |
def register_signal_handlers |
| 72 |
ssc(:button_release_event) do |widget, event| |
| 73 |
if (event.button == 3) |
| 74 |
menu_pop(self, event) |
| 75 |
true |
| 76 |
end |
| 77 |
end |
| 78 |
|
| 79 |
model.ssc(:row_deleted) do |
| 80 |
Plugin.call(:world_reorder, model.to_enum.map{|_m, _p, iter| iter[COL_WORLD] }.select(&:itself)) |
| 81 |
false |
| 82 |
end |
| 83 |
end |
| 84 |
|
| 85 |
def register_detach_listener_at_destroy(tag) |
| 86 |
ssc(:destroy) do |
| 87 |
@plugin.detach(tag) |
| 88 |
false |
| 89 |
end |
| 90 |
end |
| 91 |
|
| 92 |
def signal_do_delete_world(worlds) |
| 93 |
end |
| 94 |
end |
| 95 |
end |