Skip to content

Commit

Permalink
Dynamically load page render/buttons from the subpackages to reduce b…
Browse files Browse the repository at this point in the history
…oilerplate code
  • Loading branch information
woodcoder committed Apr 26, 2024
1 parent f7701f5 commit 8e2763f
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 59 deletions.
2 changes: 1 addition & 1 deletion ui/book/buttons.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from ..state import state

book_buttons = {
buttons = {
'single': {
'2': state.app.user.enter_go_to_page,
'5': state.app.user.insert_bookmark,
Expand Down
2 changes: 1 addition & 1 deletion ui/bookmarks/buttons.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def close_menu():
return lambda: state.app.close_menu(True)


bookmarks_buttons = {
buttons = {
'single': {
'L': state.app.close_menu,
'2': go_to_bookmark(0),
Expand Down
46 changes: 17 additions & 29 deletions ui/buttons.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,27 @@
import signal
from datetime import datetime
from .state import state
from .library.buttons import library_buttons
from .book.buttons import book_buttons
from .go_to_page.buttons import go_to_page_buttons
from .bookmarks.buttons import bookmarks_buttons
from .system_menu.buttons import system_buttons
from .language.buttons import language_buttons

from .config_loader import import_pages

log = logging.getLogger(__name__)

page_buttons = import_pages('buttons')

bindings = {
'library': library_buttons,
'book': book_buttons,
'go_to_page': go_to_page_buttons,
'bookmarks_menu': bookmarks_buttons,
'system_menu': system_buttons,
'language': language_buttons,
'help_menu': {
'single': {
'L': state.app.close_menu,
'>': state.app.next_page,
'<': state.app.previous_page,
'R': state.app.help_menu.toggle,
},
'long': {
'L': state.app.close_menu,
'>': state.app.next_page,
'<': state.app.previous_page,
'R': state.app.help_menu.toggle,
'X': state.hardware.reset_display,
},
}
bindings = { p:p.buttons for p in page_buttons }
bindings['help_menu'] = {
'single': {
'L': state.app.close_menu,
'>': state.app.next_page,
'<': state.app.previous_page,
'R': state.app.help_menu.toggle,
},
'long': {
'L': state.app.close_menu,
'>': state.app.next_page,
'<': state.app.previous_page,
'R': state.app.help_menu.toggle,
'X': state.hardware.reset_display,
},
}

# add a signal handler to manage going to the system menu when the rear
Expand Down
17 changes: 17 additions & 0 deletions ui/config_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,20 @@ def load(config_file=config_file):
files_section['media_dir'] = os.path.expanduser(media_dir)

return config

def import_pages(module):
"""
return a dictionary of location -> module (such as buttons or view)
from a list of subpackages
"""
config = load()
ui_section = config.get('ui', {})
pages = ui_section.get('pages', [
'library',
'book',
'go_to_page',
'bookmarks_menu',
'system_menu',
'language'
])
return { p:__import__(f'.{p}.{module}') for p in pages }
27 changes: 7 additions & 20 deletions ui/display.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import logging
from .library import view as library_view
from .system_menu import view as system_menu_view
from .go_to_page import view as go_to_page_view
from .language import view as language_view
from .book import view as book_view
from .bookmarks import view as bookmarks_view
from .config_loader import import_pages


log = logging.getLogger(__name__)


page_views = import_pages('view')

class Display():
def __init__(self):
self.row = 0
Expand All @@ -20,21 +17,11 @@ def __init__(self):
async def render_to_buffer(self, state):
width, height = state.app.dimensions
location = state.app.location
page_data = None
if location == 'library':
page_data = library_view.render(width, height, state)
elif location == 'system_menu':
page_data = system_menu_view.render(width, height, state)
elif location == 'go_to_page':
page_data = go_to_page_view.render(width, height, state)
elif location == 'language':
page_data = language_view.render(width, height, state)
elif location == 'book':
page_data = await book_view.render(width, height, state)
elif location == 'bookmarks_menu':
page_data = await bookmarks_view.render(width, height, state)
if page_data:
try:
page_data = await page_views[location].render(width, height, state)
self._set_buffer(page_data)
except:
log.warn('unknown page location')

async def send_line(self, driver):
row = self.row
Expand Down
2 changes: 1 addition & 1 deletion ui/go_to_page/buttons.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def thunk():
return thunk


go_to_page_buttons = {
buttons = {
'single': {
'L': state.app.close_menu,
'1': queue_key_press(1),
Expand Down
2 changes: 1 addition & 1 deletion ui/go_to_page/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def render_help(width, height):
return tuple(data)


def render(width, height, state):
async def render(width, height, state):
if state.app.help_menu.visible:
all_lines = render_help(width, height)
num_pages = len(all_lines) // height
Expand Down
2 changes: 1 addition & 1 deletion ui/language/buttons.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def select_language(index):
return lambda: state.app.languages.select_language(index)


language_buttons = {
buttons = {
'single': {
'L': state.app.close_menu,
'2': select_language(0),
Expand Down
2 changes: 1 addition & 1 deletion ui/language/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def render_help(width, height):
return tuple(data)


def render(width, height, state):
async def render(width, height, state):
help_menu = state.app.help_menu.visible
if help_menu:
all_lines = render_help(width, height)
Expand Down
2 changes: 1 addition & 1 deletion ui/library/buttons.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def library_action(number):
return lambda: state.app.library.action(number - 1)


library_buttons = {
buttons = {
'single': {
'2': library_action(2),
'3': library_action(3),
Expand Down
2 changes: 1 addition & 1 deletion ui/library/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def render_library(width, height, state):
yield tuple()


def render(width, height, state):
async def render(width, height, state):
if state.app.help_menu.visible:
all_lines = render_help(width, height)
num_pages = len(all_lines) // height
Expand Down
2 changes: 1 addition & 1 deletion ui/system_menu/buttons.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from ..state import state

system_buttons = {
buttons = {
'single': {
'2': state.app.shutdown,
'3': state.app.go_to_language_menu,
Expand Down
2 changes: 1 addition & 1 deletion ui/system_menu/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
serial = release


def render(width, height, state):
async def render(width, height, state):
if state.app.help_menu.visible:
all_lines = render_help(width, height)
num_pages = len(all_lines) // height
Expand Down

0 comments on commit 8e2763f

Please sign in to comment.