#!/usr/bin/env python3
"""Visible GTK clipboard editor. All interaction is through Cua controls."""
import os
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk, GLib

GLib.set_prgname('lazyboy-clipboard')
number = ''.join(c for c in os.environ.get('DISPLAY', ':1') if c.isdigit()) or '1'
app = Gtk.Application(application_id='net.lazyboy.Clipboard.d' + number)
window = None
entry = None

def activate(application):
    global window, entry
    clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
    if window is None:
        window = Gtk.ApplicationWindow(application=application, title='Clipboard · LazyBoy')
        window.set_default_size(520, 120)
        # Retain clipboard ownership without leaving a window on the desktop.
        def hide(widget, _event):
            widget.hide()
            return True
        window.connect('delete-event', hide)
        box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=10)
        box.set_border_width(16)
        window.add(box)
        entry = Gtk.Entry()
        entry.connect('changed', lambda field: field.get_accessible().set_name('Clipboard text: ' + field.get_text()))
        entry.get_accessible().set_name('Clipboard text: ')
        box.pack_start(entry, True, True, 0)
        button = Gtk.Button(label='Copy')
        def copy(_):
            clipboard.set_text(entry.get_text(), -1)
        button.connect('clicked', copy)
        box.pack_start(button, False, False, 0)
    entry.set_text(clipboard.wait_for_text() or '')
    window.show_all()
    window.present()

app.connect('activate', activate)
app.run([])
