File: account_control.rb

package info (click to toggle)
mikutter 5.1.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,780 kB
  • sloc: ruby: 22,912; sh: 186; makefile: 21
file content (95 lines) | stat: -rw-r--r-- 2,688 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
# -*- coding: utf-8 -*-
module ::Plugin::ChangeAccount
  class AccountControl < Gtk::TreeView
    include Gtk::TreeViewPrettyScroll
    COL_ICON = 0
    COL_NAME = 1
    COL_WORLD_NAME = 2
    COL_WORLD = 3

    type_register
    signal_new(:delete_world, GLib::Signal::RUN_FIRST, nil, nil, Array)

    def initialize(plugin)
      @plugin = plugin
      super()
      set_model(::Gtk::ListStore.new(GdkPixbuf::Pixbuf, String, String, Object))
      append_column ::Gtk::TreeViewColumn.new("", ::Gtk::CellRendererPixbuf.new, pixbuf: COL_ICON)
      append_column ::Gtk::TreeViewColumn.new("name", ::Gtk::CellRendererText.new, text: COL_NAME)
      append_column ::Gtk::TreeViewColumn.new("provider", ::Gtk::CellRendererText.new, text: COL_WORLD_NAME)
      self.set_reorderable(true)
      content_initialize
      register_signal_handlers
      event_listener_initialize
    end

    def selected_worlds
      self.selection.to_enum(:selected_each).map {|model, path, iter|
        iter[COL_WORLD]
      }
    end

    private

    def content_initialize
      Plugin.collect(:worlds).each(&method(:add_column))
    end

    def add_column(world)
      iter = model.append
      if world.respond_to?(:icon) && world.icon
        iter[COL_ICON] = world.icon.load_pixbuf(width: Gdk.scale(24), height: Gdk.scale(24)) do |loaded|
          iter[COL_ICON] = loaded unless destroyed?
        end
      end
      iter[COL_NAME] = world.title
      iter[COL_WORLD_NAME] = world.class.slug
      iter[COL_WORLD] = world
    end

    def menu_pop(widget, event)
      contextmenu = Gtk::ContextMenu.new
      contextmenu.register("削除") do
        signal_emit(:delete_world, selected_worlds)
      end
      contextmenu.popup(widget, widget)
    end

    def event_listener_initialize
      tag = @plugin.handler_tag do
        @plugin.on_world_after_created do |world|
          add_column(world)
        end
        @plugin.on_world_destroy do |world|
          _, _, iter = model.enum_for.find{ |_, _, i| i[COL_WORLD] == world }
          model.remove(iter) if iter
        end
      end
      register_detach_listener_at_destroy(tag)
    end

    def register_signal_handlers
      ssc(:button_release_event) do |widget, event|
        if (event.button == 3)
          menu_pop(self, event)
          true
        end
      end

      model.ssc(:row_deleted) do
        Plugin.call(:world_reorder, model.to_enum.map{|_m, _p, iter| iter[COL_WORLD] }.select(&:itself))
        false
      end
    end

    def register_detach_listener_at_destroy(tag)
      ssc(:destroy) do
        @plugin.detach(tag)
        false
      end
    end

    def signal_do_delete_world(worlds)
    end
  end
end