Skip to content

Commit

Permalink
ui: add missing type annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
ilius committed Jan 15, 2025
1 parent 338e126 commit 4599602
Show file tree
Hide file tree
Showing 23 changed files with 563 additions and 453 deletions.
2 changes: 1 addition & 1 deletion pyglossary/ui/argparse_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import logging


def defineFlags(parser: argparse.ArgumentParser, config: dict[str, Any]):
def defineFlags(parser: argparse.ArgumentParser, config: dict[str, Any]) -> None:
defaultHasColor = config.get(
"color.enable.cmd.windows" if os.sep == "\\" else "color.enable.cmd.unix",
True,
Expand Down
2 changes: 2 additions & 0 deletions pyglossary/ui/gtk3_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
# do not sort these imports!
from gi.repository import Gtk as gtk # noqa: I001
from gi.repository import Gdk as gdk # noqa: I001

__all__ = ["gdk", "gtk"]
6 changes: 3 additions & 3 deletions pyglossary/ui/gtk3_utils/about.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def newTabWidgetTextView(
text: str,
wrap: bool = False,
justification: gtk.Justification | None = None,
):
) -> gtk.ScrolledWindow:
tv = gtk.TextView()
tv.set_editable(False)
tv.set_can_focus(False)
Expand All @@ -104,7 +104,7 @@ def newTabLabelWidget(
text: str,
# wrap: bool = False,
# justification: "gtk.Justification | None" = None,
):
) -> gtk.ScrolledWindow:
box = VBox()
box.set_border_width(10)
label = gtk.Label()
Expand All @@ -127,7 +127,7 @@ def newTabLabelWidget(
return swin

@staticmethod
def newTabTitle(title: str, icon: str):
def newTabTitle(title: str, icon: str) -> gtk.Box:
box = gtk.Box(orientation=gtk.Orientation.VERTICAL)
if icon:
box.pack_start(imageFromFile(icon), False, False, 5)
Expand Down
8 changes: 5 additions & 3 deletions pyglossary/ui/gtk3_utils/dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,27 @@
# GNU General Public License for more details.


from collections.abc import Callable

from gi.repository import Gdk as gdk
from gi.repository import Gtk as gtk

__all__ = ["MyDialog"]


class MyDialog:
def startWaiting(self):
def startWaiting(self) -> None:
self.queue_draw()
self.vbox.set_sensitive(False)
self.get_window().set_cursor(gdk.Cursor.new(gdk.CursorType.WATCH))
while gtk.events_pending():
gtk.main_iteration_do(False)

def endWaiting(self):
def endWaiting(self) -> None:
self.get_window().set_cursor(gdk.Cursor.new(gdk.CursorType.LEFT_PTR))
self.vbox.set_sensitive(True)

def waitingDo(self, func, *args, **kwargs):
def waitingDo(self, func: Callable, *args, **kwargs) -> None: # noqa: ANN002
self.startWaiting()
try:
func(*args, **kwargs)
Expand Down
10 changes: 8 additions & 2 deletions pyglossary/ui/gtk3_utils/resize_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,20 @@

from __future__ import annotations

from typing import Any

from . import gdk, gtk
from .utils import imageFromFile

__all__ = ["ResizeButton"]


class ResizeButton(gtk.EventBox):
def __init__(self, win, edge=gdk.WindowEdge.SOUTH_EAST) -> None:
def __init__(
self,
win: gtk.Window,
edge: gdk.WindowEdge = gdk.WindowEdge.SOUTH_EAST,
) -> None:
gtk.EventBox.__init__(self)
self.win = win
self.edge = edge
Expand All @@ -35,7 +41,7 @@ def __init__(self, win, edge=gdk.WindowEdge.SOUTH_EAST) -> None:
self.add(self.image)
self.connect("button-press-event", self.buttonPress)

def buttonPress(self, _obj, gevent):
def buttonPress(self, _obj: Any, gevent: gdk.ButtonEvent) -> None:
self.win.begin_resize_drag(
self.edge,
gevent.button,
Expand Down
76 changes: 38 additions & 38 deletions pyglossary/ui/gtk3_utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@

import logging
from os.path import isabs, join
from typing import TYPE_CHECKING

from gi.repository import Pango as pango

from pyglossary.core import appResDir

from . import gdk, gtk

if TYPE_CHECKING:
from collections.abc import Callable

__all__ = [
"HBox",
"VBox",
Expand All @@ -41,25 +45,20 @@
log = logging.getLogger("pyglossary")


def VBox(**kwargs):
def VBox(**kwargs) -> gtk.Box:
return gtk.Box(orientation=gtk.Orientation.VERTICAL, **kwargs)


def HBox(**kwargs):
def HBox(**kwargs) -> gtk.Box:
return gtk.Box(orientation=gtk.Orientation.HORIZONTAL, **kwargs)


def set_tooltip(widget, text):
try:
widget.set_tooltip_text(text) # PyGTK 2.12 or above
except AttributeError:
try:
widget.set_tooltip(gtk.Tooltips(), text)
except Exception:
log.exception("")
# TODO: remove
def set_tooltip(widget, text) -> None: # noqa: ANN001
widget.set_tooltip_text(text)


def imageFromFile(path): # the file must exist
def imageFromFile(path: str) -> gtk.Image: # the file must exist
if not isabs(path):
path = join(appResDir, path)
im = gtk.Image()
Expand All @@ -70,7 +69,7 @@ def imageFromFile(path): # the file must exist
return im


def imageFromIconName(iconName: str, size: int, nonStock=False) -> gtk.Image:
def imageFromIconName(iconName: str, size: int, nonStock: bool = False) -> gtk.Image:
# So gtk.Image.new_from_stock is deprecated
# And the doc says we should use gtk.Image.new_from_icon_name
# which does NOT have the same functionality!
Expand All @@ -87,18 +86,20 @@ def imageFromIconName(iconName: str, size: int, nonStock=False) -> gtk.Image:
return gtk.Image.new_from_icon_name(iconName, size)


def rgba_parse(colorStr):
def rgba_parse(colorStr: str) -> gdk.RGBA:
rgba = gdk.RGBA()
if not rgba.parse(colorStr):
raise ValueError(f"bad color string {colorStr!r}")
return rgba


def color_parse(colorStr):
return rgba_parse(colorStr).to_color()


def pack(box, child, expand=False, fill=False, padding=0):
def pack(
box: gtk.Box | gtk.CellLayout,
child: gtk.Widget | gtk.CellRenderer,
expand: bool = False,
fill: bool = False,
padding: int = 0,
) -> None:
if isinstance(box, gtk.Box):
box.pack_start(child, expand, fill, padding)
elif isinstance(box, gtk.CellLayout):
Expand All @@ -108,31 +109,30 @@ def pack(box, child, expand=False, fill=False, padding=0):


def dialog_add_button(
dialog,
_iconName,
label,
resId,
onClicked=None,
tooltip="",
):
dialog: gtk.Dialog,
_iconName: str, # TODO: remove
label: str,
resId: int,
onClicked: Callable | None = None,
tooltip: str = "",
) -> None:
b = dialog.add_button(label, resId)
if onClicked:
b.connect("clicked", onClicked)
if tooltip:
set_tooltip(b, tooltip)
return b


def showMsg( # noqa: PLR0913
msg,
iconName="",
parent=None,
transient_for=None,
title="",
borderWidth=10,
iconSize=gtk.IconSize.DIALOG,
selectable=False,
):
msg: str,
iconName: str = "",
parent: gtk.Widget | None = None,
transient_for: gtk.Widget | None = None,
title: str = "",
borderWidth: int = 10,
iconSize: gtk.IconSize = gtk.IconSize.DIALOG,
selectable: bool = False,
) -> None:
win = gtk.Dialog(
parent=parent,
transient_for=transient_for,
Expand Down Expand Up @@ -167,19 +167,19 @@ def showMsg( # noqa: PLR0913
win.destroy()


def showError(msg, **kwargs):
def showError(msg, **kwargs) -> None: # noqa: ANN001
# gtk-dialog-error is deprecated since version 3.10:
# Use named icon “dialog-error”.
showMsg(msg, iconName="gtk-dialog-error", **kwargs)


def showWarning(msg, **kwargs):
def showWarning(msg, **kwargs) -> None: # noqa: ANN001
# gtk-dialog-warning is deprecated since version 3.10:
# Use named icon “dialog-warning”.
showMsg(msg, iconName="gtk-dialog-warning", **kwargs)


def showInfo(msg, **kwargs):
def showInfo(msg, **kwargs) -> None: # noqa: ANN001
# gtk-dialog-info is deprecated since version 3.10:
# Use named icon “dialog-information”.
showMsg(msg, iconName="gtk-dialog-info", **kwargs)
6 changes: 3 additions & 3 deletions pyglossary/ui/gtk4_utils/about.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def newTabWidgetTextView(
text: str,
wrap: bool = False,
justification: gtk.Justification | None = None,
):
) -> gtk.ScrolledWindow:
tv = gtk.TextView()
if wrap:
tv.set_wrap_mode(gtk.WrapMode.WORD)
Expand All @@ -128,7 +128,7 @@ def newTabLabelWidget(
text: str,
# wrap: bool = False,
# justification: "gtk.Justification | None" = None,
):
) -> gtk.ScrolledWindow:
box = VBox()
# box.set_border_width(10)
label = gtk.Label()
Expand All @@ -151,5 +151,5 @@ def newTabLabelWidget(
return swin

@staticmethod
def newTabTitle(title: str, icon: str):
def newTabTitle(title: str, icon: str) -> AboutTabTitleBox:
return AboutTabTitleBox(title, icon)
8 changes: 5 additions & 3 deletions pyglossary/ui/gtk4_utils/dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,25 @@
# GNU General Public License for more details.


from collections.abc import Callable

from gi.repository import Gdk as gdk

from .utils import gtk_event_iteration_loop


class MyDialog:
def startWaiting(self):
def startWaiting(self) -> None:
self.queue_draw()
self.vbox.set_sensitive(False)
self.get_window().set_cursor(gdk.Cursor.new(gdk.CursorType.WATCH))
gtk_event_iteration_loop()

def endWaiting(self):
def endWaiting(self) -> None:
self.get_window().set_cursor(gdk.Cursor.new(gdk.CursorType.LEFT_PTR))
self.vbox.set_sensitive(True)

def waitingDo(self, func, *args, **kwargs):
def waitingDo(self, func: Callable, *args, **kwargs) -> None: # noqa: ANN002
self.startWaiting()
try:
func(*args, **kwargs)
Expand Down
8 changes: 6 additions & 2 deletions pyglossary/ui/gtk4_utils/resize_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@


class ResizeButton(gtk.Box):
def __init__(self, win, edge=gdk.SurfaceEdge.SOUTH_EAST) -> None:
def __init__(
self,
win: gtk.Window,
edge: gdk.SurfaceEdge = gdk.SurfaceEdge.SOUTH_EAST,
) -> None:
gtk.Box.__init__(self)
self.win = win
self.edge = edge
Expand All @@ -35,7 +39,7 @@ def __init__(self, win, edge=gdk.SurfaceEdge.SOUTH_EAST) -> None:
gesture.connect("pressed", self.buttonPress)
self.add_controller(gesture)

def buttonPress(self, gesture, button, x, y):
def buttonPress(self, gesture: gtk.EventController, button, x, y) -> None: # noqa: ANN001
# Gesture is subclass of EventController
pass # FIXME
# self.win.begin_resize(
Expand Down
Loading

0 comments on commit 4599602

Please sign in to comment.