File: gtk_keyconfig.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 (64 lines) | stat: -rw-r--r-- 1,754 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
# -*- coding: utf-8 -*-
require 'mui/gtk_extension'

require 'gtk3'

class Gtk::KeyConfig < Gtk::Button

  attr_accessor :change_hook, :title, :keycode

  def initialize(title, default_key='', *args)
    @title = title
    self.keycode = default_key.to_s
    @change_hook = nil
    super(*args)
    self.add(buttonlabel)
    self.ssc(:clicked, &method(:clicked_event))
  end

  def buttonlabel
    @buttonlabel ||= Gtk::Label.new(keycode)
  end

  private

  def clicked_event(event)
    box = Gtk::Box.new(:vertical)
    label = Gtk::Label.new
    button = Gtk::Button.new
    dialog = Gtk::Dialog.new(title: title, parent: self.get_ancestor(Gtk::Window),
                             flags: Gtk::DialogFlags::MODAL,
                             buttons: [[Gtk::Stock::OK, Gtk::ResponseType::OK]])
    label.text = keycode
    box.border_width = 20
    button.add(label)
    box.pack_start(Gtk::Label.new('下のボタンをクリックして、割り当てたいキーを押してください。'))
    box.pack_start(button)
    button.signal_connect(:key_press_event, &key_set(label))
    button.signal_connect(:button_press_event, &button_set(label))
    dialog.child.add(box)
    dialog.show_all
    dialog.run
    dialog.destroy
    true
  end

  def key_set(label)
    ->(widget, event) do
      self.keycode = Gtk.keyname([event.keyval, event.state])
      buttonlabel.text = label.text = keycode
      self.change_hook.call(keycode) if self.change_hook
      true
    end
  end

  def button_set(label)
    ->(widget, event) do
      self.keycode = Gtk.buttonname([event.event_type, event.button, event.state])
      buttonlabel.text = label.text = keycode
      self.change_hook.call(keycode) if self.change_hook
      true
    end
  end

end