File: complete.rb

package info (click to toggle)
mikutter 4.1.3%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,260 kB
  • sloc: ruby: 20,126; sh: 183; makefile: 19
file content (124 lines) | stat: -rw-r--r-- 3,523 bytes parent folder | download
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# -*- coding: utf-8 -*-

module Plugin::Quickstep
  class Complete < Gtk::TreeView

    def initialize(search_input)
      super(gen_store)
      append_column ::Gtk::TreeViewColumn.new("", ::Gtk::CellRendererPixbuf.new, pixbuf: Plugin::Quickstep::Store::COL_ICON)
      @col_kind = ::Gtk::TreeViewColumn.new("", ::Gtk::CellRendererText.new, text: Plugin::Quickstep::Store::COL_KIND)
      @col_kind.set_sizing(Gtk::TreeViewColumn::FIXED)
      append_column @col_kind
      append_column ::Gtk::TreeViewColumn.new("", ::Gtk::CellRendererText.new, text: Plugin::Quickstep::Store::COL_TITLE)

      set_enable_search(false)
      set_headers_visible(false)
      set_enable_grid_lines(Gtk::TreeView::GridLines::HORIZONTAL)
      set_tooltip_column(Plugin::Quickstep::Store::COL_TITLE)

      register_listeners(search_input)
    end

    private

    def register_listeners(search_input)
      search_input.ssc(:changed, &method(:input_change_event))
      search_input.ssc(:key_press_event) { |widget, event|
        case ::Gtk::keyname([event.keyval,event.state])
        when 'Up', 'Control + n'
          path = self.selection.selected.path
          path.prev!
          self.selection.select_path(path)
          true
        when 'Down', 'Control + p'
          path = self.selection.selected.path
          path.next!
          self.selection.select_path(path)
          true
        end
      }
      ssc(:row_activated) do |this, path, _column|
        iter = this.model.get_iter(path)
        if iter
          search_input.signal_emit(:activate)
        end
      end
    end

    def input_change_event(widget)
      @col_kind.set_fixed_width(self.window.geometry[2] * 0.25)
      tree_model = self.model = gen_store
      Plugin.collect(:quickstep_query, widget.text.freeze, Pluggaloid::COLLECT).deach { |detected|
        tree_model.add_model(detected)
      }.trap { |err|
        error err
      }
      false
    end

    def select_first_ifn(model, path, iter)
      Delayer.new do
        break if self.destroyed?
        self.selection.select_path(path) unless self.selection.selected
      end
      false
    end

    def gen_store
      store = Store.new(GdkPixbuf::Pixbuf, String, String, Object)
      store.ssc(:row_inserted, &method(:select_first_ifn))
      store
    end
  end

  class Store < Gtk::ListStore
    COL_ICON  = 0
    COL_KIND  = 1
    COL_TITLE = 2
    COL_MODEL = 3

    def add_model(model)
      case model
      when Diva::Model
        force_add_model(model)
      when Diva::URI, URI::Generic, Addressable::URI, String
        add_uri_or_models(model)
      end
    end

    private

    def force_add_model(model)
      iter = append
      iter[COL_ICON] = nil # model.icon if model.respond_to?(icon)
      iter[COL_KIND] = model.class.spec.name
      iter[COL_TITLE] = model.title
      iter[COL_MODEL] = model
    end

    def add_uri_or_models(uri)
      model_slugs = Plugin.collect(:model_of_uri, uri).to_a
      if model_slugs.empty?
        force_add_uri(uri)
      else
        model_slugs.each do |model_slug|
          Deferred.new {
            Diva::Model(model_slug).find_by_uri(uri)
          }.next {|model|
            force_add_model(model) if model
          }.trap do |err|
            error err
          end
        end
      end
    end

    def force_add_uri(uri)
      iter = append
      iter[COL_ICON] = nil
      iter[COL_KIND] = Plugin[:quickstep]._('URLを開く')
      iter[COL_TITLE] = uri.to_s
      iter[COL_MODEL] = uri
    end
  end
end