File: quickstep.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 (100 lines) | stat: -rw-r--r-- 2,927 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
# -*- coding: utf-8 -*-
require_relative 'complete'
require_relative 'model/command'
Plugin.create(:quickstep) do
  command(:quickstep,
          name: _('Quick Step'),
          condition: lambda { |opt| true },
          icon: Skin[:search],
          visible: true,
          role: :window) do |opt|
    if tab?(:quickstep)
      Plugin::GUI::Tab.instance(:quickstep).active!
      next
    end

    tab(:quickstep, _('QuickStep 検索')) do
      set_icon Skin[:search]
      set_deletable true
      temporary_tab true
      grid = Gtk::Grid.new
      grid.orientation = :vertical
      grid.hexpand = true
      put_widget grid
      nativewidget grid
      active!
    end
  end

  intent Plugin::Quickstep::Command, label: _('mikutterコマンド') do |intent_token|
    window = Plugin::GUI::Window.active
    command = intent_token.model
    world, = Plugin.filtering(:world_current, nil)
    event = Plugin::GUI::Event.new(event: :quickstep,
                                   widget: window,
                                   world: world,
                                   messages: [])
    command[:exec].call(event) if command[:condition] === event
  end

  # URLっぽい文字列なら、それに対してintentを発行する候補を出す
  filter_quickstep_query do |query, yielder|
    if URI::DEFAULT_PARSER.make_regexp.match?(query)
      yielder << Diva::URI!(query)
    end
    [query, yielder]
  end

  filter_quickstep_query do |query, yielder|
    if !query.empty?
      commands, = Plugin.filtering(:command, Hash.new)
      commands.each do |command_slug, options|
        if options[:role] == :window and (options[:name].include?(query) or command_slug.to_s.include?(query))
          yielder << Plugin::Quickstep::Command.new(options.merge(slug: command_slug))
        end
      end
    end
    [query, yielder]
  end

  def put_widget(grid)
    search = Gtk::Entry.new
    search.hexpand = true
    complete = Plugin::Quickstep::Complete.new(search)
    complete.valign = :fill
    complete.vexpand = true
    search.ssc(:activate, &gen_search_activate_callback(complete))
    search.ssc(:realize, &gen_search_realize_callback)
    search.ssc(:key_press_event, &gen_common_shortcutkey_callback)
    complete.ssc(:key_press_event, &gen_common_shortcutkey_callback)

    grid << search << complete

    complete
  end

  def gen_search_activate_callback(complete)
    ->(_) do
      tab(:quickstep).destroy
      selected = complete.selection.selected
      Plugin.call(:open, selected[Plugin::Quickstep::Store::COL_MODEL]) if selected
    end
  end

  def gen_search_realize_callback
    ->(this) do
      this.get_ancestor(Gtk::Window).set_focus(this)
      false
    end
  end

  def gen_common_shortcutkey_callback
    ->(widget, event) do
      case ::Gtk::keyname([event.keyval, event.state])
      when 'Escape'
        tab(:quickstep).destroy
        true
      end
    end
  end
end