Debian Codegraph @

plugin/quickstep/complete.rb

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 module Plugin::Quickstep
4 class Complete < Gtk::TreeView
5
6 def initialize(search_input)
7 super(gen_store)
8 append_column ::Gtk::TreeViewColumn.new("", ::Gtk::CellRendererPixbuf.new, pixbuf: Plugin::Quickstep::Store::COL_ICON)
9 @col_kind = ::Gtk::TreeViewColumn.new("", ::Gtk::CellRendererText.new, text: Plugin::Quickstep::Store::COL_KIND)
10 @col_kind.set_sizing(Gtk::TreeViewColumn::FIXED)
11 append_column @col_kind
12 text = ::Gtk::CellRendererText.new
13 text.ellipsize = :end
14 append_column ::Gtk::TreeViewColumn.new("", text, text: Plugin::Quickstep::Store::COL_TITLE)
15
16 set_enable_search(false)
17 set_headers_visible(false)
18 set_enable_grid_lines(Gtk::TreeView::GridLines::HORIZONTAL)
19 set_tooltip_column(Plugin::Quickstep::Store::COL_TITLE)
20
21 register_listeners(search_input)
22 end
23
24 private
25
26 def register_listeners(search_input)
27 search_input.ssc(:changed, &method(:input_change_event))
28 search_input.ssc(:key_press_event) { |widget, event|
29 case ::Gtk::keyname([event.keyval,event.state])
30 when 'Up', 'Control + n'
31 path = self.selection.selected.path
32 path.prev!
33 self.selection.select_path(path)
34 true
35 when 'Down', 'Control + p'
36 path = self.selection.selected.path
37 path.next!
38 self.selection.select_path(path)
39 true
40 end
41 }
42 ssc(:row_activated) do |this, path, _column|
43 iter = this.model.get_iter(path)
44 if iter
45 search_input.signal_emit(:activate)
46 end
47 end
48 end
49
50 def input_change_event(widget)
51 @col_kind.set_fixed_width(self.window.geometry[2] * 0.25)
52 tree_model = self.model = gen_store
53 Plugin.collect(:quickstep_query, widget.text.freeze, Pluggaloid::COLLECT).deach { |detected|
54 tree_model.add_model(detected)
55 }.trap { |err|
56 error err
57 }
58 false
59 end
60
61 def select_first_ifn(model, path, iter)
62 Delayer.new do
63 break if self.destroyed?
64 self.selection.select_path(path) unless self.selection.selected
65 end
66 false
67 end
68
69 def gen_store
70 store = Store.new(GdkPixbuf::Pixbuf, String, String, Object)
71 store.ssc(:row_inserted, &method(:select_first_ifn))
72 store
73 end
74 end
75
76 class Store < Gtk::ListStore
77 COL_ICON = 0
78 COL_KIND = 1
79 COL_TITLE = 2
80 COL_MODEL = 3
81
82 def add_model(model)
83 case model
84 when Diva::Model
85 force_add_model(model)
86 when Diva::URI, URI::Generic, Addressable::URI, String
87 add_uri_or_models(model)
88 end
89 end
90
91 private
92
93 def force_add_model(model)
94 iter = append
95 iter[COL_ICON] = nil # model.icon if model.respond_to?(icon)
96 iter[COL_KIND] = model.class.spec.name
97 iter[COL_TITLE] = model.title
98 iter[COL_MODEL] = model
99 end
100
101 def add_uri_or_models(uri)
102 model_slugs = Plugin.collect(:model_of_uri, uri).to_a
103 if model_slugs.empty?
104 force_add_uri(uri)
105 else
106 model_slugs.each do |model_slug|
107 Deferred.new {
108 Diva::Model(model_slug).find_by_uri(uri)
109 }.next {|model|
110 force_add_model(model) if model
111 }.trap do |err|
112 error err
113 end
114 end
115 end
116 end
117
118 def force_add_uri(uri)
119 iter = append
120 iter[COL_ICON] = nil
121 iter[COL_KIND] = Plugin[:quickstep]._('URLを開く')
122 iter[COL_TITLE] = uri.to_s
123 iter[COL_MODEL] = uri
124 end
125 end
126 end