

"""
Bug when a window:

1. Is in focus.
2. Receives a focus-out event
3. Hides itself
4. Would get back focus due to the event in (2) if (3) didn't happen.

Steps to reproduce:

1. Run this script, which has two windows W and Y
2. Move Y window to the left
3. Click inside Y to show W again
4. Click inside W and it prints a message from its button-press-event
5. W has focus, now click the Y window's close button
6. W is not hidden and does not respond to click events
"""

import signal
import gi
gi.require_version("Gtk", "3.0")

from gi.repository import Gtk, GLib

def focus_out(widget, event):
    print("Focus out event:", event)
    widget.hide()
    #GLib.idle_add(lambda: widget.hide())

def main():
    w = Gtk.Window.new(Gtk.WindowType.TOPLEVEL)
    y = Gtk.Window.new(Gtk.WindowType.TOPLEVEL)
    w.set_title("W: Click Me to print()")
    w.resize(600, 400)
    y.set_title("Y: Click Me to Show W")
    y.resize(600, 400)
    w.present()
    y.present()
    w.connect("focus-out-event", focus_out)
    w.connect("button-press-event", lambda *args: print("Clicked W"))
    y.connect("button-press-event", lambda *args: w.show())

    signal.signal(signal.SIGINT, lambda *args: Gtk.main_quit())
    Gtk.main()

main()
