Skip to content

Commit

Permalink
more type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
totaam committed Jun 15, 2023
1 parent c1657d0 commit cb40315
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 69 deletions.
25 changes: 13 additions & 12 deletions xpra/client/gl/gtk3/gl_client_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# later version. See the file COPYING for details.

from collections import namedtuple
from typing import Type

from xpra.client.gtk3.gtk3_client_window import GTK3ClientWindow
from xpra.gtk_common.gtk_util import set_visual
Expand All @@ -24,13 +25,13 @@ class GLClientWindowBase(GTK3ClientWindow):
def __repr__(self):
return "GLClientWindow(%s : %s)" % (self.wid, self._backing)

def get_backing_class(self):
def get_backing_class(self) -> Type:
raise NotImplementedError()

def is_GL(self):
def is_GL(self) -> bool:
return True

def spinner(self, ok):
def spinner(self, ok:bool) -> None:
b = self._backing
log("spinner(%s) opengl window %s: backing=%s", ok, self.wid, b)
if not b:
Expand All @@ -42,14 +43,14 @@ def spinner(self, ok):
w, h = self.get_size()
self.repaint(0, 0, w, h)

def queue_draw_area(self, x, y, w, h):
def queue_draw_area(self, x:int, y:int, w:int, h:int) -> None:
b = self._backing
if not b:
return
rect = (x, y, w, h)
b.gl_expose_rect(rect)

def monitor_changed(self, monitor):
def monitor_changed(self, monitor) -> None:
super().monitor_changed(monitor)
da = self.drawing_area
if da and MONITOR_REINIT:
Expand All @@ -64,7 +65,7 @@ def monitor_changed(self, monitor):
self.new_backing(w, h)


def remove_backing(self):
def remove_backing(self) -> None:
b = self._backing
log("remove_backing() backing=%s", b)
if b:
Expand All @@ -79,7 +80,7 @@ def remove_backing(self):
except Exception:
log.warn("Warning: cannot remove %s", glarea, exc_info=True)

def magic_key(self, *args):
def magic_key(self, *args) -> None:
b = self._backing
if self.border:
self.border.toggle()
Expand All @@ -91,7 +92,7 @@ def magic_key(self, *args):
log("gl magic_key%s border=%s, backing=%s", args, self.border, b)


def set_alpha(self):
def set_alpha(self) -> None:
super().set_alpha()
rgb_formats = self._client_properties.setdefault("encodings.rgb_formats", [])
#gl_window_backing supports BGR(A) too:
Expand All @@ -102,16 +103,16 @@ def set_alpha(self):
#TODO: we could handle BGRX as BGRA too...
#rgb_formats.append("BGRX")

def do_configure_event(self, event):
def do_configure_event(self, event) -> None:
log("GL do_configure_event(%s)", event)
GTK3ClientWindow.do_configure_event(self, event)
self._backing.paint_screen = True

def destroy(self):
def destroy(self) -> None:
self.remove_backing()
super().destroy()

def new_backing(self, bw, bh):
def new_backing(self, bw:int, bh:int) -> None:
widget = super().new_backing(bw, bh)
if self.drawing_area:
self.remove(self.drawing_area)
Expand All @@ -128,7 +129,7 @@ def new_backing(self, bw, bh):
#maybe redundant?:
self.apply_geometry_hints(self.geometry_hints)

def draw_widget(self, widget, context):
def draw_widget(self, widget, context) -> bool:
log("draw_widget(%s, %s)", widget, context)
if not self.get_mapped():
return False
Expand Down
6 changes: 3 additions & 3 deletions xpra/client/gtk3/gtk3_client_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# Xpra is released under the terms of the GNU GPL v2, or, at your option, any
# later version. See the file COPYING for details.

from typing import Tuple, Dict
from typing import Tuple, Dict, Type
from gi.repository import Gdk, Gtk, Gio, GdkPixbuf # @UnresolvedImport

from xpra.client.gtk3.gtk_client_window_base import GTKClientWindowBase, HAS_X11_BINDINGS
Expand Down Expand Up @@ -118,11 +118,11 @@ def show_window_menu(self, *_args) -> None:
self.menu_helper.build()
self.menu_helper.popup(0, 0)

def get_backing_class(self):
def get_backing_class(self) -> Type:
raise NotImplementedError()


def xget_u32_property(self, target, name):
def xget_u32_property(self, target, name:str):
if HAS_X11_BINDINGS:
return GTKClientWindowBase.xget_u32_property(self, target, name)
#pure Gdk lookup:
Expand Down
10 changes: 5 additions & 5 deletions xpra/client/gtk3/gtk3_notifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

class GTK3_Notifier(NotifierBase):

def show_notify(self, dbus_id, tray, nid,
app_name, replaces_nid, app_icon,
summary, body, actions, hints, timeout, icon):
def show_notify(self, dbus_id, tray, nid:int,
app_name:str, replaces_nid:int, app_icon,
summary:str, body:str, actions, hints, timeout:int, icon):
if not self.dbus_check(dbus_id):
return
icon_string = self.get_icon_string(nid, app_icon, icon)
Expand All @@ -28,9 +28,9 @@ def closed(*_args):
n.show()


def close_notify(self, nid):
def close_notify(self, nid:int) -> None:
self.clean_notification(nid)

def cleanup(self):
def cleanup(self) -> None:
Notify.uninit()
super().cleanup()
28 changes: 15 additions & 13 deletions xpra/client/gui/client_widget_base.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
# This file is part of Xpra.
# Copyright (C) 2011 Serviware (Arthur Huillet, <ahuillet@serviware.com>)
# Copyright (C) 2010-2016 Antoine Martin <antoine@xpra.org>
# Copyright (C) 2010-2023 Antoine Martin <antoine@xpra.org>
# Copyright (C) 2008, 2010 Nathaniel Smith <njs@pobox.com>
# Xpra is released under the terms of the GNU GPL v2, or, at your option, any
# later version. See the file COPYING for details.

#pretend to draw the windows, but don't actually do anything
from typing import Dict, Any, Type

from xpra.util import envbool
from xpra.log import Logger

log = Logger("window")
#pretend to draw the windows, but don't actually do anything:
USE_FAKE_BACKING = envbool("XPRA_USE_FAKE_BACKING", False)


class ClientWidgetBase:

def __init__(self, client, watcher_pid, wid, has_alpha):
def __init__(self, client, watcher_pid:int, wid:int, has_alpha:bool):
self.wid = wid
self.watcher_pid = watcher_pid
#gobject-like scheduler:
Expand All @@ -32,7 +34,7 @@ def __init__(self, client, watcher_pid, wid, has_alpha):
self._backing = None
self.pixel_depth = 24

def get_info(self):
def get_info(self) -> Dict[str,Any]:
info = {
"has-alpha" : self._has_alpha,
"window-alpha" : self._window_alpha,
Expand All @@ -43,7 +45,7 @@ def get_info(self):
info["backing"] = b.get_info()
return info

def make_new_backing(self, backing_class, ww, wh, bw, bh):
def make_new_backing(self, backing_class:Type, ww:int, wh:int, bw:int, bh:int):
#size of the backing (same as server window source):
bw = max(1, bw)
bh = max(1, bh)
Expand All @@ -66,31 +68,31 @@ def make_new_backing(self, backing_class, ww, wh, bw, bh):
backing.init(ww, wh, bw, bh)
return backing

def freeze(self):
def freeze(self) -> None:
"""
Subclasses can suspend screen updates and free some resources
"""

def unfreeze(self):
def unfreeze(self) -> None:
"""
Subclasses may resume normal operation that were suspended by freeze()
"""


def workspace_changed(self): # pragma: no cover
def workspace_changed(self) -> None: # pragma: no cover
pass

def set_cursor_data(self, cursor_data): # pragma: no cover
def set_cursor_data(self, cursor_data) -> None: # pragma: no cover
pass

def new_backing(self, _w, _h): # pragma: no cover
def new_backing(self, w:int, h:int): # pragma: no cover
raise NotImplementedError

def is_OR(self): # pragma: no cover
def is_OR(self) -> bool: # pragma: no cover
return False

def is_tray(self): # pragma: no cover
def is_tray(self) -> bool: # pragma: no cover
return False

def is_GL(self): # pragma: no cover
def is_GL(self) -> bool: # pragma: no cover
return False
4 changes: 2 additions & 2 deletions xpra/client/gui/client_window_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def cp(self, x, y) -> Tuple[int,int]:
return self.cx(x), self.cy(y)


def new_backing(self, bw, bh):
def new_backing(self, bw:int, bh:int):
backing_class = self.get_backing_class()
log("new_backing(%s, %s) backing_class=%s", bw, bh, backing_class)
assert backing_class is not None
Expand All @@ -223,7 +223,7 @@ def destroy(self) -> None:
self._backing = None


def setup_window(self, bw, bh) -> None:
def setup_window(self, bw:int, bh:int) -> None:
self.new_backing(bw, bh)
#tell the server about the encoding capabilities of this backing instance:
#but don't bother if they're the same as what we sent as defaults
Expand Down
34 changes: 18 additions & 16 deletions xpra/client/gui/tray_base.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# This file is part of Xpra.
# Copyright (C) 2010 Nathaniel Smith <njs@pobox.com>
# Copyright (C) 2011-2022 Antoine Martin <antoine@xpra.org>
# Copyright (C) 2011-2023 Antoine Martin <antoine@xpra.org>
# Xpra is released under the terms of the GNU GPL v2, or, at your option, any
# later version. See the file COPYING for details.

from typing import Tuple, Callable
from time import monotonic
from collections import deque

Expand All @@ -18,7 +19,8 @@ class TrayBase:
Utility superclass for all tray implementations
"""

def __init__(self, _client, app_id, menu, tooltip, icon_filename, size_changed_cb, click_cb, mouseover_cb, exit_cb):
def __init__(self, _client, app_id, menu, tooltip:str, icon_filename:str,
size_changed_cb:Callable, click_cb:Callable, mouseover_cb:Callable, exit_cb:Callable):
#we don't keep a reference to client,
#because calling functions on the client directly should be discouraged
self.app_id = app_id
Expand All @@ -39,24 +41,24 @@ def __init__(self, _client, app_id, menu, tooltip, icon_filename, size_changed_c
def __repr__(self):
return f"Tray({self.app_id}:{self.tooltip})"

def cleanup(self):
def cleanup(self) -> None:
if self.tray_widget:
self.hide()
self.tray_widget = None

def ready(self):
def ready(self) -> None:
"""
This is called when we have finished the startup sequence.
The MacOS dock overrides this method.
"""

def show(self):
def show(self) -> None:
raise NotImplementedError

def hide(self):
def hide(self) -> None:
raise NotImplementedError

def get_screen(self):
def get_screen(self) -> int:
return -1

def get_orientation(self):
Expand All @@ -65,48 +67,48 @@ def get_orientation(self):
def get_geometry(self):
raise NotImplementedError

def get_size(self):
def get_size(self) -> Tuple[int,int]:
g = self.get_geometry()
if g is None:
return None
return g[2:4]

def set_tooltip(self, tooltip=None):
def set_tooltip(self, tooltip:str="") -> None:
self.tooltip = tooltip
raise NotImplementedError

def set_blinking(self, on):
def set_blinking(self, on:bool) -> None:
raise NotImplementedError


def set_icon_from_data(self, pixels, has_alpha, w, h, rowstride, options=None):
def set_icon_from_data(self, pixels, has_alpha:bool, w:int, h:int, rowstride:int, options=None):
raise NotImplementedError

def get_icon_filename(self, basename=None):
def get_icon_filename(self, basename="") -> str:
name = basename or self.default_icon_filename
f = get_icon_filename(name, self.default_icon_extension)
if not f:
log.error(f"Error: cannot find icon {name!r}")
return f

def set_icon(self, basename=None):
def set_icon(self, basename="") -> None:
filename = self.get_icon_filename(basename)
if not filename:
return
log(f"set_icon({basename}) using filename={filename!r}")
self.set_icon_from_file(filename)

def set_icon_from_file(self, filename):
def set_icon_from_file(self, filename:str) -> None:
log(f"set_icon_from_file({filename}) tray_widget={self.tray_widget}")
if not self.tray_widget:
return
self.do_set_icon_from_file(filename)
self.icon_timestamp = monotonic()

def do_set_icon_from_file(self, filename):
def do_set_icon_from_file(self, filename:str) -> None:
raise NotImplementedError

def recalculate_geometry(self, x, y, width, height):
def recalculate_geometry(self, x:int, y:int, width:int, height:int) -> None:
log("recalculate_geometry%s guess=%s, tray event locations: %s",
(x, y, width, height), self.geometry_guess, len(self.tray_event_locations))
if x is None or y is None:
Expand Down
4 changes: 2 additions & 2 deletions xpra/client/gui/window_backing_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from time import monotonic
from threading import Lock
from collections import deque
from typing import Dict, Any, Tuple
from typing import Dict, Any, Tuple, List, Callable
from gi.repository import GLib # @UnresolvedImport

from xpra.net.mmap_pipe import mmap_read
Expand Down Expand Up @@ -83,7 +83,7 @@ def load_video_decoders():
return VIDEO_DECODERS


def fire_paint_callbacks(callbacks, success=True, message=""):
def fire_paint_callbacks(callbacks:List[Callable], success=True, message=""):
for x in callbacks:
try:
x(success, message)
Expand Down
Loading

0 comments on commit cb40315

Please sign in to comment.