70 lines
2.3 KiB
Python
70 lines
2.3 KiB
Python
#!/usr/bin/env python3
|
|
"""Private GTK controls for current-line AT-SPI checks."""
|
|
import json
|
|
import sys
|
|
|
|
import gi
|
|
gi.require_version("Gtk", "3.0")
|
|
from gi.repository import GLib, Gtk
|
|
|
|
window = Gtk.Window(title="Current line fixture")
|
|
box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
|
|
window.add(box)
|
|
entry = Gtk.Entry()
|
|
entry.get_accessible().set_name("Entry")
|
|
textView = Gtk.TextView()
|
|
textView.get_accessible().set_name("Text")
|
|
password = Gtk.Entry()
|
|
password.set_visibility(False)
|
|
password.get_accessible().set_name("Password")
|
|
button = Gtk.Button(label="Example button")
|
|
controls = {"entry": entry, "text": textView, "password": password, "button": button}
|
|
for control in controls.values():
|
|
box.pack_start(control, True, True, 0)
|
|
window.show_all()
|
|
|
|
|
|
def command_ready(source, condition):
|
|
line = sys.stdin.readline()
|
|
if not line:
|
|
Gtk.main_quit()
|
|
return False
|
|
command = json.loads(line)
|
|
control = controls[command["control"]]
|
|
if command["action"] == "focus":
|
|
if isinstance(control, Gtk.Entry):
|
|
control.set_text(command["text"])
|
|
control.set_position(command["offset"])
|
|
elif isinstance(control, Gtk.TextView):
|
|
buffer = control.get_buffer()
|
|
buffer.set_text(command["text"])
|
|
buffer.place_cursor(buffer.get_iter_at_offset(command["offset"]))
|
|
window.present()
|
|
control.grab_focus()
|
|
|
|
def finish_focus():
|
|
if isinstance(control, Gtk.Entry):
|
|
control.set_position(command["offset"])
|
|
elif isinstance(control, Gtk.TextView):
|
|
buffer = control.get_buffer()
|
|
buffer.place_cursor(buffer.get_iter_at_offset(command["offset"]))
|
|
print("ready", flush=True)
|
|
return False
|
|
|
|
GLib.timeout_add(50, finish_focus)
|
|
return True
|
|
elif command["action"] == "caret":
|
|
if isinstance(control, Gtk.Entry):
|
|
offset = control.get_position()
|
|
else:
|
|
buffer = control.get_buffer()
|
|
offset = buffer.get_iter_at_mark(buffer.get_insert()).get_offset()
|
|
print(offset, flush=True)
|
|
print("ready", flush=True)
|
|
return True
|
|
|
|
|
|
GLib.io_add_watch(sys.stdin, GLib.IO_IN | GLib.IO_HUP, command_ready)
|
|
print("ready", flush=True)
|
|
Gtk.main()
|