File: shortcutkey_listview.rb

package info (click to toggle)
mikutter 5.0.4%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 9,700 kB
  • sloc: ruby: 21,307; sh: 181; makefile: 19
file content (362 lines) | stat: -rw-r--r-- 11,357 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# -*- coding: utf-8 -*-

module Plugin::Shortcutkey
  class ShortcutKeyListView < ::Gtk::CRUD

    COLUMN_KEYBIND = 0
    COLUMN_COMMAND_ICON = 1
    COLUMN_COMMAND = 2
    COLUMN_SLUG = 3
    COLUMN_WORLD_FACE = 4
    COLUMN_WORLD = 5
    COLUMN_ID = 6

    attr_accessor :filter_entry

    def initialize(plugin)
      type_strict plugin => Plugin
      @plugin = plugin
      super()
      set_model(Gtk::TreeModelFilter.new(model))
      model.set_visible_func do |model, iter|
        if defined?(@filter_entry) and @filter_entry
          query = @filter_entry.text
          Enumerator.new {|y|
            y << iter[COLUMN_KEYBIND] << iter[COLUMN_COMMAND] << iter[COLUMN_SLUG]
            iter[COLUMN_WORLD].yield_self do |w|
              y << w.uri << w.title if w.is_a? Diva::Model
            end
          }.any? {|x| x.to_s.include?(query) }
        else
          true
        end
      end
      commands = Plugin.filtering(:command, Hash.new).first
      worlds = world_selections(key: ->(k) { k.uri.to_s })
      shortcutkeys.each do |id, behavior|
        slug = behavior[:slug]
        iter = model.model.append
        iter[COLUMN_ID] = id
        iter[COLUMN_KEYBIND] = behavior[:key]
        iter[COLUMN_COMMAND] = behavior[:name]
        iter[COLUMN_SLUG] = slug
        iter[COLUMN_WORLD] = worlds[behavior[:world]]
        update_iter(iter, force: false)
        commands.dig(slug, :icon)&.yield_self {|icon|
          icon.is_a?(Proc) ? icon.call(nil) : icon
        }&.yield_self {|icon|
          Diva::Model(:photo)[icon]
        }&.yield_self do |icon|
          iter[COLUMN_COMMAND_ICON] = icon.load_pixbuf(width: Gdk.scale(16), height: Gdk.scale(16)) do |pixbuf|
            iter[COLUMN_COMMAND_ICON] = pixbuf if not destroyed?
          end
        end
      end
    end

    def column_schemer
      [{ kind:   :text,
         widget: :keyconfig,
         type:   String,
         label:  @plugin._('キーバインド')
       },
       [{ kind:  :pixbuf,
          type:  GdkPixbuf::Pixbuf,
          label: @plugin._('機能名')
        },
        { kind:   :text,
          type:   String,
          expand: true
        }
       ],
       { kind:   :text,
         widget: :chooseone,
         args:   command_dict_slug_to_name,
         type:   Symbol
       },
       { kind:     :text,
         label:    @plugin._('アカウント'),
         type:     String
       },
       { widget:   :chooseone,
         args:     worlds_dict_slug_to_name,
         type:     Object        # World Model or :current
       },
       { type: Integer
       },
      ].freeze
    end

    def command_dict_slug_to_name
      @command_dict_slug_to_name ||=
        Plugin.filtering(:command, Hash.new)
          .first
          .values
          .to_h {|x| [x[:slug], x[:name]] }
    end

    def worlds_dict_slug_to_name
      Plugin.collect(:worlds).to_h {|x| [x.uri, x.title] }
    end

    def shortcutkeys
      UserConfig[:shortcutkey_keybinds] || {}.freeze
    end

    def new_serial
      @new_serial ||= (shortcutkeys.keys.max || 0)
      @new_serial += 1 end

    def on_created(iter)
      iter[COLUMN_ID] = new_serial
      merge_key_bind(iter)
    end

    def on_updated(iter)
      merge_key_bind(iter)
    end

    def on_deleted(iter)
      bind = shortcutkeys.dup
      bind.delete(iter[COLUMN_ID].to_i)
      UserConfig[:shortcutkey_keybinds] = bind
    end

    def popup_input_window(defaults = [])
      values = defaults.dup
      result = nil
      defaults.freeze
      window = KeyConfigWindow.new(@plugin._("設定 - %{software_name}") % {software_name: Environment::NAME})
      window.transient_for = toplevel
      window.modal = true
      window.destroy_with_parent = true
      btn_ok = ::Gtk::Button.new(label: @plugin._("OK"))
      btn_cancel = ::Gtk::Button.new(label: @plugin._("キャンセル"))
      box = ::Gtk::ButtonBox.new :horizontal
      box.layout_style = :end
      box.spacing = 6
      box << btn_cancel << btn_ok
      grid = ::Gtk::Grid.new
      grid.orientation = :vertical
      grid.row_spacing = 12
      grid.margin = 12
      grid << (::Gtk::Grid.new.tap do |grid|
        grid.orientation = :vertical
        grid.row_spacing = 12
        c_box = command_box(values)
        c_box.expand = true
        grid << key_box(values) << world_box(values) << c_box
      end) << box

      window << grid
      window.show_all

      window.ssc(:destroy) { ::Gtk::main_quit }
      btn_cancel.ssc(:clicked) { window.destroy }
      btn_ok.ssc(:clicked) {
        error = catch(:validate) {
          throw :validate, @plugin._("キーバインドを選択してください") unless (values[COLUMN_KEYBIND] && values[COLUMN_KEYBIND] != "")
          throw :validate, @plugin._("コマンドを選択してください") unless command_dict_slug_to_name.has_key?(values[COLUMN_SLUG]&.to_sym)
          result = values
          window.destroy
          nil
        }
        if error
          dialog = ::Gtk::MessageDialog.new(parent: window,
                                            flags: ::Gtk::DialogFlags::DESTROY_WITH_PARENT,
                                            type: ::Gtk::MessageType::WARNING,
                                            buttons: ::Gtk::ButtonsType::OK,
                                            message: error)
          dialog.run
          dialog.destroy end }
      ::Gtk::main
      result end

    private

    def update_iter(iter, force: false)
      if force
        iter[COLUMN_COMMAND] = name_of(iter)
      else
        iter[COLUMN_COMMAND] ||= name_of(iter)
      end
      iter[COLUMN_WORLD_FACE] =
        if iter[COLUMN_WORLD]
          iter[COLUMN_WORLD].title
        else
          @plugin._('カレントアカウント')
        end
    end

    def merge_key_bind(iter)
      update_iter(iter, force: true)
      UserConfig[:shortcutkey_keybinds] = shortcutkeys.merge(
        iter[COLUMN_ID].to_i => bind_of(iter)
      )
    end

    def bind_of(iter)
      {
        key: -iter[COLUMN_KEYBIND].to_s,
        name: -iter[COLUMN_COMMAND].to_s,
        slug: iter[COLUMN_SLUG].to_sym,
        world:
          if iter[COLUMN_WORLD]
            iter[COLUMN_WORLD].uri&.to_s
          end
      }
    end

    def name_of(iter)
      name = Plugin.filtering(:command, Hash.new).first[iter[COLUMN_SLUG].to_sym][:name]
      name = name.call(nil) if name.is_a? Proc
      name
    end

    def key_box(results)
      container = ::Gtk::Box.new(:horizontal, 0)
      button = ::Gtk::KeyConfig.new(@plugin._('キーバインド'), results[COLUMN_KEYBIND])
      button.width_request = HYDE
      button.change_hook = lambda { |new| results[COLUMN_KEYBIND] = new }
      container.pack_start(::Gtk::Label.new(@plugin._('キーバインド')), expand: false, fill: true, padding: 0)
      container.pack_start(Gtk::Alignment.new(1.0, 0.5, 0, 0).add(button), expand: true, fill: true, padding: 0) end

    def world_box(results)
      faces = world_selections(value: :title)
      Mtk.chooseone(
        ->(new) {
          case new
          when :current
            results[COLUMN_WORLD_FACE] = @plugin._('カレントアカウント')
            results[COLUMN_WORLD] = nil
          when nil
            results[COLUMN_WORLD]
          else
            results[COLUMN_WORLD_FACE] = faces[new]
            results[COLUMN_WORLD] = new
          end
        },
        @plugin._('アカウント'),
        faces
      )
    end

    def command_box(results)
      treeview = CommandList.new(@plugin, results)

      sw = ::Gtk::ScrolledWindow.new
      sw.expand = true
      sw.set_policy :never, :automatic
      sw << treeview

      filter_entry = treeview.filter_entry = Gtk::Entry.new
      filter_entry.primary_icon_pixbuf = Skin[:search].pixbuf(width: 24, height: 24)
      filter_entry.ssc(:changed) {
        treeview.model.refilter
        false }

      grid = ::Gtk::Grid.new
      grid.orientation = :vertical
      grid.row_spacing = 12
      grid << filter_entry << sw
    end

    def world_selections(key: :itself, value: :itself)
      {
        current: @plugin._('カレントアカウント'),
        **Plugin.collect(:worlds).to_h do |w|
          [key.to_proc.call(w), value.to_proc.call(w)]
        end
      }
    end

    class KeyConfigWindow < ::Gtk::Window
      def initialize(*args)
        super
        set_size_request(640, 480)
        self.window_position = ::Gtk::WindowPosition::CENTER
      end
    end

    class CommandList < ::Gtk::TreeView
      include Gtk::TreeViewPrettyScroll

      COL_ICON = 0
      COL_NAME = 1
      COL_SLUG = 2

      attr_accessor :filter_entry

      def initialize(plugin, results)
        type_strict plugin => Plugin
        @plugin = plugin
        super(::Gtk::TreeModelFilter.new(::Gtk::TreeStore.new(::GdkPixbuf::Pixbuf, String, Symbol)))
        model.set_visible_func { |model, iter|
          if defined?(@filter_entry) and @filter_entry
            iter_match(iter, @filter_entry.text)
          else
            true
          end
        }
        append_column ::Gtk::TreeViewColumn.new("", ::Gtk::CellRendererPixbuf.new, pixbuf: COL_ICON)
        append_column ::Gtk::TreeViewColumn.new(@plugin._("コマンド名"), ::Gtk::CellRendererText.new, text: COL_NAME)
        append_column ::Gtk::TreeViewColumn.new(@plugin._("スラッグ"), ::Gtk::CellRendererText.new, text: COL_SLUG)
        parents = Hash.new { |h, k| # role => TreeIter
          h[k] = iter = model.model.append(nil)
          iter[COL_NAME] = k.to_s
          iter
        }
        Plugin.filtering(:command, Hash.new).first.map { |slug, command|
          iter = model.model.append(parents[command[:role]])
          icon = icon_model(command[:icon])
          if icon
            iter[COL_ICON] = icon.load_pixbuf(width: Gdk.scale(16), height: Gdk.scale(16)) do |pixbuf|
              iter[COL_ICON] = pixbuf if not destroyed?
            end
          end
          name = command[:name]
          name = name.call(nil) if name.is_a? Proc
          iter[COL_NAME] = name
          iter[COL_SLUG] = slug
          if results[Plugin::Shortcutkey::ShortcutKeyListView::COLUMN_SLUG].to_s == slug.to_s
            expand_row(iter.parent.path, true)
            selection.select_iter(iter)
          end
        }
        ssc(:cursor_changed) do
          iter = selection.selected
          if iter
            results[Plugin::Shortcutkey::ShortcutKeyListView::COLUMN_COMMAND] = iter[COL_NAME]
            results[Plugin::Shortcutkey::ShortcutKeyListView::COLUMN_SLUG] = iter[COL_SLUG]
          end
          false
        end
        selected = selection.selected
        if selected
          scroll_to_cell(selected.path, nil, false, 0.5, 0)
        end
      end

      private

      def icon_model(icon)
        case icon
        when Proc
          icon_model(icon.call(nil))
        when Diva::Model
          icon
        when String, URI, Addressable::URI, Diva::URI
          Plugin.collect(:photo_filter, icon, Pluggaloid::COLLECT).first
        end
      end

      def iter_match(iter, text)
        if [COL_NAME, COL_SLUG].any? { |column| iter[column].to_s.include?(text) }
          true
        elsif iter.has_child?
          iter.n_children.times.any? { |i| iter_match(iter.nth_child(i), text) }
        end
      end
    end
  end
end