Debian Codegraph @

plugin/intent_selector/listview.rb

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 module Plugin::IntentSelector
4 class IntentSelectorListView < ::Gtk::CRUD
5 COLUMN_INTENT_LABEL = 0
6 COLUMN_MODEL_LABEL = 1
7 COLUMN_STRING = 2
8 COLUMN_INTENT = 3
9 COLUMN_MODEL = 4
10 COLUMN_UUID = 5
11
12 def initialize
13 super
14 intents = intent_catalog
15 models = model_catalog
16 UserConfig[:intent_selector_rules].each do |record|
17 iter = model.model.append
18 iter[COLUMN_INTENT] = record[:intent]
19 iter[COLUMN_INTENT_LABEL] = intents[record[:intent].to_sym]
20 iter[COLUMN_MODEL] = record[:model]
21 iter[COLUMN_MODEL_LABEL] = models[record[:model].to_s]
22 iter[COLUMN_STRING] = record[:str]
23 iter[COLUMN_UUID] = record[:uuid]
24 end
25 end
26
27 def initialize_model
28 set_model(Gtk::TreeModelFilter.new(Gtk::ListStore.new(*column_schemer.flatten.map{|x| x[:type]})))
29 model.set_visible_func{ |model, iter|
30 if defined?(@filter_entry) and @filter_entry
31 [COLUMN_INTENT, COLUMN_INTENT_LABEL, COLUMN_MODEL, COLUMN_MODEL_LABEL, COLUMN_STRING].any?{ |column| iter[column].to_s.include?(@filter_entry.text) }
32 else
33 true end }
34 end
35
36 def column_schemer
37 [{kind: :text, type: Symbol, expand: true, label: _('開く方法')},
38 {kind: :text, type: String, expand: true, label: _('対象')},
39 {kind: :text, type: String, widget: :input, expand: true, label: _('条件')},
40 {type: Symbol, widget: :chooseone, args: [intent_catalog], label: _('開く方法')},
41 {type: String, widget: :chooseone, args: [model_catalog], label: _('対象')},
42 {type: String},
43 ].freeze
44 end
45
46 def on_created(iter)
47 iter[COLUMN_UUID] = SecureRandom.uuid
48 iter[COLUMN_INTENT_LABEL] = intent_catalog[iter[COLUMN_INTENT].to_sym]
49 iter[COLUMN_MODEL_LABEL] = model_catalog[iter[COLUMN_MODEL].to_s]
50 UserConfig[:intent_selector_rules] += [{
51 intent: iter[COLUMN_INTENT].to_sym,
52 model: iter[COLUMN_MODEL],
53 str: iter[COLUMN_STRING],
54 rule: 'start',
55 uuid: iter[COLUMN_UUID]
56 }]
57 end
58
59 def on_updated(iter)
60 iter[COLUMN_INTENT_LABEL] = intent_catalog[iter[COLUMN_INTENT].to_sym]
61 iter[COLUMN_MODEL_LABEL] = model_catalog[iter[COLUMN_MODEL].to_s]
62 UserConfig[:intent_selector_rules] = UserConfig[:intent_selector_rules].map do |record|
63 if record[:uuid] == iter[COLUMN_UUID]
64 record.merge(
65 intent: iter[COLUMN_INTENT].to_sym,
66 model: iter[COLUMN_MODEL],
67 str: iter[COLUMN_STRING])
68 else
69 record
70 end
71 end
72 end
73
74 def on_deleted(iter)
75 UserConfig[:intent_selector_rules] = UserConfig[:intent_selector_rules].reject do |record|
76 record[:uuid] == iter[COLUMN_UUID]
77 end
78 end
79
80 def filter_entry
81 @filter_entry ||= Gtk::Entry.new.tap do |entry|
82 entry.primary_icon_pixbuf = Skin[:search].pixbuf(width: 24, height: 24)
83 entry.ssc(:changed, self, &gen_refilter)
84 end
85 end
86
87 private
88
89 def gen_refilter
90 proc do
91 model.refilter
92 end
93 end
94
95 def _(str)
96 Plugin[:intent_selector]._(str)
97 end
98
99 def intent_catalog
100 Plugin.filtering(:intent_all, []).first.to_h { |i| [i.slug, i.label] }
101 end
102
103 def model_catalog
104 Plugin.filtering(:retrievers, []).first.to_h { |s|[s[:slug].to_s,s[:name]] }.merge('' => _('(未定義)'))
105 end
106
107 end
108 end