Skip to content

Commit

Permalink
add functionality of lazy focus loss to avoid close on short-term losses
Browse files Browse the repository at this point in the history
  • Loading branch information
katoquro committed Sep 11, 2021
1 parent 11e88c9 commit e28c0d8
Showing 1 changed file with 37 additions and 6 deletions.
43 changes: 37 additions & 6 deletions guake/guake_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import uuid

from pathlib import Path
from time import sleep
from threading import Thread
from urllib.parse import quote_plus
from xml.sax.saxutils import escape as xml_escape

Expand Down Expand Up @@ -204,6 +206,7 @@ def load_schema():
self.display_tab_names = 0

self.window.connect("focus-out-event", self.on_window_losefocus)
self.window.connect("focus-in-event", self.on_window_takefocus)

# Handling the delete-event of the main window to avoid
# problems when closing it.
Expand Down Expand Up @@ -260,6 +263,9 @@ def window_event(*args):
filename,
)

# usage of this property should be replaced with settigs option
self.stub_for_postponed_losefocus_property = True

log.info("Guake initialized")

def get_notebook(self):
Expand Down Expand Up @@ -478,12 +484,37 @@ def on_window_losefocus(self, window, event):
if not HidePrevention(self.window).may_hide():
return

value = self.settings.general.get_boolean("window-losefocus")
visible = window.get_property("visible")
self.losefocus_time = get_server_time(self.window)
if visible and value:
log.info("Hiding on focus lose")
self.hide()
def hide_window_callback():
value = self.settings.general.get_boolean("window-losefocus")
visible = window.get_property("visible")
self.losefocus_time = get_server_time(self.window)
if visible and value:
log.info("Hiding on focus lose")
self.hide()
return False

def loosefocus_callback(sleep_time):
sleep(sleep_time)

if self.window.get_property("has-toplevel-focus") and (self.takefocus_time - self.lazy_losefocus_time) > 0:
log.debug("Short term losefocus detected. Skip the hidding")
return

if self.window.get_property("visible"):
from gi.repository import GLib
GLib.idle_add(hide_window_callback)

if self.stub_for_postponed_losefocus_property:
self.lazy_losefocus_time = get_server_time(self.window)
thread = Thread(target = loosefocus_callback, args = (0.3,))
thread.daemon = True
thread.start()
log.debug("Lazy losefocus check at %s", self.lazy_losefocus_time)
else:
hide_window_callback(-1)

def on_window_takefocus(self, window, event):
self.takefocus_time = get_server_time(self.window)

def show_menu(self, status_icon, button, activate_time):
"""Show the tray icon menu."""
Expand Down

0 comments on commit e28c0d8

Please sign in to comment.