Skip to content

Commit

Permalink
Implement ui_wx.wid_transtoken
Browse files Browse the repository at this point in the history
  • Loading branch information
TeamSpen210 committed Jun 28, 2024
1 parent 4f73404 commit 231be6f
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 6 deletions.
7 changes: 5 additions & 2 deletions src/ui_tk/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ async def init_app(core_nursery: trio.Nursery) -> None:
gameMan.set_game_by_name(last_game.id)

core_nursery.start_soon(sound.sound_task)
core_nursery.start_soon(wid_transtoken.update_task)

export_trig = app.EdgeTrigger[exporting.ExportInfo]()
export_send, export_rec = trio.open_memory_channel[lifecycle.ExportResult](1)
Expand Down Expand Up @@ -142,9 +141,13 @@ async def app_main(init: Callable[[trio.Nursery], Awaitable[Any]]) -> None:
# Start some core tasks.
await nursery.start(route_callback_exceptions)
await nursery.start(display_errors)
await nursery.start(loadScreen.startup)

# Check very early before bad things happen.
await gameMan.check_app_in_game(DIALOG)

await nursery.start(loadScreen.startup)
nursery.start_soon(wid_transtoken.update_task)

# Run main app, then once completed cancel this nursery to quit all other tasks.
# It gets given the nursery to allow spawning new tasks here.
await init(nursery)
Expand Down
10 changes: 6 additions & 4 deletions src/ui_wx/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from trio_debug import Tracer
from ui_wx.dialogs import DIALOG
from ui_tk.errors import display_errors
from ui_tk import wid_transtoken, route_callback_exceptions
from ui_wx import wid_transtoken
from ui_wx.img import WX_IMG
from ui_wx import APP, MAIN_WINDOW
from config.gen_opts import GenOptions
Expand Down Expand Up @@ -58,7 +58,6 @@ async def init_app(core_nursery: trio.Nursery) -> None:
gameMan.set_game_by_name(last_game.id)

core_nursery.start_soon(sound.sound_task)
core_nursery.start_soon(wid_transtoken.update_task)

export_trig = app.EdgeTrigger[exporting.ExportInfo]()
export_send, export_rec = trio.open_memory_channel[lifecycle.ExportResult](1)
Expand Down Expand Up @@ -135,9 +134,13 @@ async def app_main(init: Callable[[trio.Nursery], Awaitable[Any]]) -> None:
# Start some core tasks.
# await nursery.start(route_callback_exceptions)
# await nursery.start(display_errors)
# await nursery.start(loadScreen.startup)

# Check very early before bad things happen.
await gameMan.check_app_in_game(DIALOG)

nursery.start_soon(wid_transtoken.update_task)
# await nursery.start(loadScreen.startup)

# Run main app, then once completed cancel this nursery to quit all other tasks.
# It gets given the nursery to allow spawning new tasks here.
await init(nursery)
Expand All @@ -163,7 +166,6 @@ def run_callback(event: wx.IdleEvent) -> None:
while queue:
queue.popleft()()


def run_sync_soon_threadsafe(func: Callable[[], Any]) -> None:
"""Run the specified func in the next loop, from other threads."""
queue.append(func)
Expand Down
81 changes: 81 additions & 0 deletions src/ui_wx/wid_transtoken.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
"""Manage applying translation tokens to Wx widgets."""
from __future__ import annotations

from contextlib import aclosing
from weakref import WeakKeyDictionary

import trio.lowlevel
import wx

from app.localisation import gradual_iter
from transtoken import TransToken, CURRENT_LANG


__all__ = [
'TransToken', 'CURRENT_LANG', # Re-exports
'set_text', 'set_win_title', 'set_menu_text',
]


# Widgets that have a SetText method.
type TextWidget = (
wx.Control
)
_control_labels: WeakKeyDictionary[TextWidget, TransToken] = WeakKeyDictionary()
_menu_labels: WeakKeyDictionary[wx.MenuItem, TransToken] = WeakKeyDictionary()
_window_titles: WeakKeyDictionary[wx.Frame, TransToken] = WeakKeyDictionary()


def set_text[Widget: TextWidget](widget: Widget, token: TransToken) -> Widget:
"""Apply a token to the specified control."""
# TODO: Should we use SetLabel here to allow mnemonics?
widget.SetLabelText(str(token))
if token.is_untranslated: # No need to have a callback for this one.
_control_labels.pop(widget, None)
else:
_control_labels[widget] = token
return widget


def set_win_title(win: wx.Frame, token: TransToken) -> None:
"""Set the title of a window to this token."""
win.SetTitle(str(token))
_window_titles[win] = token


def set_menu_text(menu: wx.MenuItem, token: TransToken) -> None:
"""Apply this text to an item on a menu."""
menu.SetItemLabel(token)
_menu_labels[menu] = token


async def update_task() -> None:
"""Apply new languages to all stored widgets."""
# Using gradual_iter() yields to the event loop in-between each conversion.
while True:
await CURRENT_LANG.wait_transition()
async with aclosing(gradual_iter(_control_labels)) as agen1:
async for control, token in agen1:
control.SetTextLabel(str(token))

await trio.lowlevel.checkpoint()

async with aclosing(gradual_iter(_menu_labels)) as agen2:
async for menu, menu_map in agen2:
for index, token in menu_map.items():
menu.entryconfigure(index, label=str(token))

await trio.lowlevel.checkpoint()

for window, token in _window_titles.items():
window.SetTitle(str(token))


def stats() -> str:
"""Output debingging statistics."""
return (
f'TransTokens:\n'
f'- Controls: {len(_control_labels)}\n'
f'- Menus: {len(_menu_labels)}\n'
f'- Windows: {len(_window_titles)}\n'
)

0 comments on commit 231be6f

Please sign in to comment.