Skip to content

Commit

Permalink
refactor: avoid loading kivy when constants are imported
Browse files Browse the repository at this point in the history
  • Loading branch information
sassanh committed Sep 20, 2024
1 parent 3a2b00e commit 719bc18
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 89 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Version 0.13.2

- refactor: avoid loading kivy when constants are imported

## Version 0.13.1

- feat: widen the notification text horizontally if it doesn't have any actions on the left
Expand Down
75 changes: 39 additions & 36 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "ubo-gui"
version = "0.13.1"
version = "0.13.2"
description = "GUI sdk for Ubo Pod"
authors = ["Sassan Haradji <[email protected]>"]
license = "Apache-2.0"
Expand All @@ -9,7 +9,7 @@ packages = [{ include = "ubo_gui" }]
include = ['ubo_gui/assets/fonts/*']

[tool.poetry.dependencies]
headless-kivy = "^0.9.7"
headless-kivy = "^0.9.8"
python = "^3.11"
python-immutable = "^1.0.2"
qrcode = "^7.4.2"
Expand Down
50 changes: 36 additions & 14 deletions ubo_gui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,44 @@

from pathlib import Path

from kivy.factory import Factory

__import__('ubo_gui.constants')
__import__('ubo_gui.menu.constants')

ROOT_PATH = Path(__file__).parent
ASSETS_PATH = ROOT_PATH.joinpath('assets')
FONTS_PATH = ASSETS_PATH.joinpath('fonts')


Factory.register('AnimatedSlider', module='ubo_gui.animated_slider')
Factory.register('GaugeWidget', module='ubo_gui.gauge')
Factory.register('ItemWidget', module='ubo_gui.menu.widgets.item_widget')
Factory.register('MenuWidget', module='ubo_gui.menu')
Factory.register('NotificationWidget', module='ubo_gui.notification')
Factory.register('ProgressRingWidget', module='ubo_gui.progress_ring')
Factory.register('PromptWidget', module='ubo_gui.prompt')
Factory.register('QRCodeWidget', module='ubo_gui.qrcode')
Factory.register('VolumeWidget', module='ubo_gui.volume')
def setup() -> None:
"""Register various widgets using Kivy Factory and sets up the constants."""
from kivy.factory import Factory
from kivy.lang.builder import Builder

from ubo_gui import constants
from ubo_gui.menu import constants as menu_constants

Builder.load_string(
f"""
#:set UBO_GUI_PRIMARY_COLOR '{constants.PRIMARY_COLOR}'
#:set UBO_GUI_SECONDARY_COLOR '{constants.SECONDARY_COLOR}'
#:set UBO_GUI_SECONDARY_COLOR_LIGHT '{constants.SECONDARY_COLOR_LIGHT}'
#:set UBO_GUI_TEXT_COLOR '{constants.TEXT_COLOR}'
#:set UBO_GUI_INFO_COLOR '{constants.INFO_COLOR}'
#:set UBO_GUI_DANGER_COLOR '{constants.DANGER_COLOR}'
#:set UBO_GUI_WARNING_COLOR '{constants.WARNING_COLOR}'
#:set UBO_GUI_SUCCESS_COLOR '{constants.SUCCESS_COLOR}'
#:set UBO_GUI_PAGE_SIZE {menu_constants.PAGE_SIZE}
#:set UBO_GUI_SHORT_WIDTH {menu_constants.SHORT_WIDTH}
#:set UBO_GUI_MENU_ITEM_HEIGHT {menu_constants.MENU_ITEM_HEIGHT}
#:set UBO_GUI_MENU_ITEM_GAP {menu_constants.MENU_ITEM_GAP}
""",
)

Factory.register('AnimatedSlider', module='ubo_gui.animated_slider')
Factory.register('GaugeWidget', module='ubo_gui.gauge')
Factory.register('ItemWidget', module='ubo_gui.menu.widgets.item_widget')
Factory.register('MenuWidget', module='ubo_gui.menu')
Factory.register('NotificationWidget', module='ubo_gui.notification')
Factory.register('ProgressRingWidget', module='ubo_gui.progress_ring')
Factory.register('PromptWidget', module='ubo_gui.prompt')
Factory.register('QRCodeWidget', module='ubo_gui.qrcode')
Factory.register('VolumeWidget', module='ubo_gui.volume')
16 changes: 0 additions & 16 deletions ubo_gui/constants.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""Module to store constants used in the application."""

from kivy.lang.builder import Builder

PRIMARY_COLOR = '#68B7FF'
SECONDARY_COLOR = '#363F4B'
SECONDARY_COLOR_LIGHT = '#ABA7A7'
Expand All @@ -11,17 +9,3 @@
DANGER_COLOR = '#FF3F51'
WARNING_COLOR = '#FFC107'
SUCCESS_COLOR = '#03F7AE'

Builder.load_string(
f"""
#:set UBO_GUI_PRIMARY_COLOR '{PRIMARY_COLOR}'
#:set UBO_GUI_SECONDARY_COLOR '{SECONDARY_COLOR}'
#:set UBO_GUI_SECONDARY_COLOR_LIGHT '{SECONDARY_COLOR_LIGHT}'
#:set UBO_GUI_TEXT_COLOR '{TEXT_COLOR}'
#:set UBO_GUI_INFO_COLOR '{INFO_COLOR}'
#:set UBO_GUI_DANGER_COLOR '{DANGER_COLOR}'
#:set UBO_GUI_WARNING_COLOR '{WARNING_COLOR}'
#:set UBO_GUI_SUCCESS_COLOR '{SUCCESS_COLOR}'
""",
)
15 changes: 1 addition & 14 deletions ubo_gui/menu/constants.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@
"""Module for defining constants used in the `Menu` widget."""

from kivy.lang.builder import Builder

from ubo_gui.page import PAGE_MAX_ITEMS

PAGE_SIZE = PAGE_MAX_ITEMS
PAGE_SIZE = 3 # PAGE_MAX_ITEMS
SHORT_WIDTH = 46
MENU_ITEM_HEIGHT = 52
MENU_ITEM_GAP = 7

Builder.load_string(
f"""
#:set UBO_GUI_PAGE_SIZE {PAGE_SIZE}
#:set UBO_GUI_SHORT_WIDTH {SHORT_WIDTH}
#:set UBO_GUI_MENU_ITEM_HEIGHT {MENU_ITEM_HEIGHT}
#:set UBO_GUI_MENU_ITEM_GAP {MENU_ITEM_GAP}
""",
)
10 changes: 7 additions & 3 deletions ubo_gui/menu/menu_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,14 +218,18 @@ def select_action_item(self: MenuWidget, item: ActionItem) -> None:
result = item.action()
if not result:
return
if isinstance(result, type) and issubclass(result, PageWidget):
self.open_application(result())
if isinstance(result, type):
if issubclass(result, PageWidget):
self.open_application(result())
else:
msg = f'Unsupported returned value by `ActionItem`: {result}'
raise TypeError(msg)
elif isinstance(result, PageWidget):
self.open_application(result)
elif isinstance(result, Menu) or callable(result):
self.open_menu(result)
else:
msg = f'Unsupported returned value by `ActionItem`: {type(result)}'
msg = f'Unsupported returned value by `ActionItem`: {result}'
raise TypeError(msg)

def select_application_item(self: MenuWidget, item: ApplicationItem) -> None:
Expand Down
9 changes: 5 additions & 4 deletions ubo_gui/prompt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import pathlib
import warnings
from abc import ABC, abstractmethod
from abc import ABC, ABCMeta, abstractmethod
from typing import TYPE_CHECKING, Sequence

from kivy.lang.builder import Builder
Expand All @@ -17,22 +17,23 @@
ColorProperty,
StringProperty,
)
from kivy.uix.widget import WidgetMetaclass

from ubo_gui.constants import DANGER_COLOR, SUCCESS_COLOR
from ubo_gui.menu.types import ActionItem
from ubo_gui.page import PageWidget

if TYPE_CHECKING:
from ubo_gui.menu.types import Item, Menu
from ubo_gui.page import PageWidget

PROMPT_OPTIONS = 2


class PromptWidgetMetaClass(type(ABC), type(PageWidget)):
class PromptWidgetMetaClass(ABCMeta, WidgetMetaclass):
"""Metaclass merging `ABC` and `PageWidget` for `PromptWidget` class."""


class PromptWidget(PageWidget, ABC, metaclass=PromptWidgetMetaClass):
class PromptWidget(ABC, metaclass=PromptWidgetMetaClass):
"""A widget that renders a prompt."""

icon: str = StringProperty()
Expand Down

0 comments on commit 719bc18

Please sign in to comment.