| 1 |
|
| 2 |
|
| 3 |
class Plugin::Gtk3::WorldShifter < Gtk::EventBox |
| 4 |
UserConfig[:gtk_accountbox_geometry] ||= 32 |
| 5 |
|
| 6 |
def initialize |
| 7 |
super |
| 8 |
ssc(:button_press_event) do |_this, event| |
| 9 |
open_menu event if event.button <= 3 |
| 10 |
false |
| 11 |
end |
| 12 |
ssc_atonce(:realize) do |
| 13 |
refresh |
| 14 |
end |
| 15 |
pluggaloid_event_listener |
| 16 |
end |
| 17 |
|
| 18 |
def refresh |
| 19 |
destroy_menu |
| 20 |
put_face |
| 21 |
modify_face |
| 22 |
end |
| 23 |
|
| 24 |
def open_menu(event) |
| 25 |
@menu ||= Gtk::Menu.new.tap do |menu| |
| 26 |
Plugin.collect(:worlds).each do |world| |
| 27 |
item = Gtk::ImageMenuItem.new(label: world.title, use_underline: false) |
| 28 |
item.set_image Gtk::WebIcon.new(world.icon, UserConfig[:gtk_accountbox_geometry], UserConfig[:gtk_accountbox_geometry]) |
| 29 |
item.always_show_image = true |
| 30 |
item.ssc(:activate) do |_w| |
| 31 |
Plugin.call(:world_change_current, world) |
| 32 |
@face.tooltip_text = world.title |
| 33 |
false |
| 34 |
end |
| 35 |
menu.append item |
| 36 |
end |
| 37 |
menu.append Gtk::SeparatorMenuItem.new |
| 38 |
item = Gtk::ImageMenuItem.new(label: Plugin[:gtk3]._('Worldを追加'), use_underline: false) |
| 39 |
item.set_image Gtk::WebIcon.new(Skin[:add], UserConfig[:gtk_accountbox_geometry], UserConfig[:gtk_accountbox_geometry]) |
| 40 |
item.always_show_image = true |
| 41 |
item.ssc(:activate) do |_w| |
| 42 |
Plugin.call(:request_world_add) |
| 43 |
false |
| 44 |
end |
| 45 |
menu.append item |
| 46 |
end |
| 47 |
@menu.show_all.popup(nil, nil, event.button, event.time) |
| 48 |
end |
| 49 |
|
| 50 |
def destroy_menu |
| 51 |
@menu&.destroy |
| 52 |
@menu = nil |
| 53 |
end |
| 54 |
|
| 55 |
def pluggaloid_event_listener |
| 56 |
tag = Plugin[:gtk3].handler_tag(:world_shifter) do |
| 57 |
Plugin[:gtk3].on_world_change_current { refresh } |
| 58 |
Plugin[:gtk3].on_userconfig_modify do |key, _newval| |
| 59 |
refresh if key == :world_shifter_visibility |
| 60 |
end |
| 61 |
Plugin[:gtk3].on_world_reordered do |_worlds| |
| 62 |
refresh |
| 63 |
end |
| 64 |
Plugin[:gtk3].on_world_after_created do |_world| |
| 65 |
refresh |
| 66 |
end |
| 67 |
Plugin[:gtk3].on_world_destroy do |_world| |
| 68 |
refresh |
| 69 |
end |
| 70 |
end |
| 71 |
ssc(:destroy) do |
| 72 |
Plugin[:gtk3].detach(tag) |
| 73 |
end |
| 74 |
end |
| 75 |
|
| 76 |
def put_face |
| 77 |
if visible? |
| 78 |
add_face_widget_ifn |
| 79 |
else |
| 80 |
remove_face_widget_ifn |
| 81 |
end |
| 82 |
end |
| 83 |
|
| 84 |
def visible? |
| 85 |
case UserConfig[:world_shifter_visibility] |
| 86 |
when :always |
| 87 |
true |
| 88 |
when :auto |
| 89 |
Plugin.collect(:worlds).take(2).size > 1 |
| 90 |
else |
| 91 |
false |
| 92 |
end |
| 93 |
end |
| 94 |
|
| 95 |
def modify_face |
| 96 |
if @face |
| 97 |
world, = Plugin.filtering(:world_current, nil) |
| 98 |
transaction = @world_transaction = SecureRandom.uuid |
| 99 |
rect = { |
| 100 |
width: Gdk.scale(UserConfig[:gtk_accountbox_geometry]), |
| 101 |
height: Gdk.scale(UserConfig[:gtk_accountbox_geometry]) |
| 102 |
} |
| 103 |
@face.pixbuf = world&.icon&.load_pixbuf(**rect) do |pixbuf| |
| 104 |
if transaction == @world_transaction |
| 105 |
@face.pixbuf = pixbuf |
| 106 |
end |
| 107 |
end || Skin[:add].pixbuf(**rect) |
| 108 |
end |
| 109 |
end |
| 110 |
|
| 111 |
def add_face_widget_ifn |
| 112 |
unless @face |
| 113 |
size = Gdk.scale UserConfig[:gtk_accountbox_geometry] |
| 114 |
pb = Skin[:loading].pixbuf width: size, height: size |
| 115 |
@face = Gtk::Image.new pixbuf: pb |
| 116 |
add(@face).show_all |
| 117 |
end |
| 118 |
world, = Plugin.filtering(:world_current, nil) |
| 119 |
if world |
| 120 |
@face.tooltip_text = world.title |
| 121 |
end |
| 122 |
end |
| 123 |
|
| 124 |
def remove_face_widget_ifn |
| 125 |
if @face |
| 126 |
remove(@face) |
| 127 |
@face.destroy |
| 128 |
@face = nil |
| 129 |
end |
| 130 |
end |
| 131 |
end |