#!/bin/env python3

import sys
from gi import require_version
require_version("Gdk", "3.0")
require_version("Gtk", "3.0")
from gi.repository import Gdk, Gio, Gtk
 
class MyWindow(Gtk.ApplicationWindow):
  def __init__(self, app):
    Gtk.Window.__init__(self, application=app)

    title = "App"

    self.set_default_size(800, 600)
    header = Gtk.HeaderBar()
    self.set_position(Gtk.WindowPosition.CENTER)
    settings = self.get_settings()
    settings.set_property("gtk-application-prefer-dark-theme", False)
 
    self.set_title(title)
    header.set_title(title)
    header.set_show_close_button(True)

    self.label = Gtk.Label.new("Press F11 to fullscreen\n" +
                               "Press ESC to close")
    self.label.set_justify(Gtk.Justification.CENTER)
    self.add(self.label)

    self.set_titlebar(header)
    self.show_all()
    self.connect("key-press-event", self.onKeyPress)

  def onKeyPress(self, widget, event):
    if event.keyval in [65474, 65480]:
      self.label.set_label("Press ESC to unfullscreen\n" +
                           "Double press ESC to reproduce bug")
      self.fullscreen()
    elif event.keyval == 65307:
      if self.get_window().get_state() & Gdk.WindowState.FULLSCREEN:
        self.label.set_label("Press F11 to fullscreen\n" +
                             "Press ESC to close")
        self.unfullscreen()
      else:
        self.close()

class MyApplication(Gtk.Application):
  def __init__(self):
    Gtk.Application.__init__(self)

  def do_activate(self):
    win = MyWindow(self)
    win.show_all()

  def do_startup (self):
    Gtk.Application.do_startup(self)

app = MyApplication()
exit_status = app.run(sys.argv)
sys.exit(exit_status)
