Files
cthulhu/tests/gtk_messaging.py
2026-10-27 00:40:49 -04:00

71 lines
2.3 KiB
Python

#!/usr/bin/env python3
"""Private transcript provider matching legacy Pidgin's accessible hierarchy."""
import ctypes
import os
import sys
import gi
gi.require_version("Gtk", "3.0")
gi.require_version("Atk", "1.0")
gi.require_version("GdkX11", "3.0")
from gi.repository import Atk, GdkX11, GLib, Gtk
if ctypes.CDLL(None).prctl(15, sys.argv[1].encode(), 0, 0, 0) != 0:
raise RuntimeError("Cannot set fixture process identity")
chatWindow = Gtk.Window(title="Conversation fixture")
notebook = Gtk.Notebook()
transcript = Gtk.TextView(editable=False)
scroll = Gtk.ScrolledWindow()
scroll.add(transcript)
notebook.append_page(scroll, Gtk.Label(label="Conversation"))
chatWindow.add(notebook)
chatWindow.show_all()
buffer = transcript.get_buffer()
typingTag = buffer.create_tag("typing", scale=0.8)
weightTag = buffer.create_tag("typing-weight", weight=300)
foreground = Gtk.Window(title="Foreground fixture")
box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
editor = Gtk.Entry()
marker = Gtk.Label(label="Marker")
marker.get_accessible().set_role(Atk.Role.ALERT)
box.add(editor)
box.add(marker)
foreground.add(box)
foreground.show_all()
pendingInput = ""
def receive_command(source, condition):
global pendingInput
content = os.read(sys.stdin.fileno(), 4096)
if not content:
Gtk.main_quit()
return False
pendingInput += content.decode()
while "\n" in pendingInput:
command, pendingInput = pendingInput.split("\n", 1)
if command == "message":
buffer.insert(buffer.get_end_iter(), "Username: Hello\n")
elif command == "typing":
buffer.insert_with_tags(buffer.get_end_iter(), "Username is typing\n", typingTag)
elif command == "typing-weight":
buffer.insert_with_tags(buffer.get_end_iter(), "Username is typing\n", weightTag)
elif command == "edit":
editor.set_text("Typed input")
elif command == "barrier":
marker.set_text("Messaging barrier")
elif command == "destroy":
transcript.destroy()
elif command == "quit":
Gtk.main_quit()
return False
return True
GLib.io_add_watch(sys.stdin, GLib.IO_IN | GLib.IO_HUP, receive_command)
GLib.idle_add(lambda: print("ready", GdkX11.X11Window.get_xid(foreground.get_window()), flush=True) or False)
Gtk.main()