Răsfoiți Sursa

added ability to change palettes

Daniel Sheffield 3 ani în urmă
părinte
comite
723ac8bd01
2 a modificat fișierele cu 50 adăugiri și 5 ștergeri
  1. 22 1
      app/activities/__init__.py
  2. 28 4
      grocery_transactions.py

+ 22 - 1
app/activities/__init__.py

@@ -4,11 +4,31 @@
 # All rights reserved
 #
 # THIS SOFTWARE IS PROVIDED AS IS WITHOUT WARRANTY
+from typing import Iterator
 import urwid
-def show_or_exit(key):
+from urwid.display_common import BaseScreen
+
+def show_or_exit(key,
+    screen: BaseScreen = None, palettes: Iterator = None
+):
+
     if isinstance(key, tuple):
         return
     
+    if key == 'ctrl home':
+        
+        if screen is None or palettes is None:
+            return
+        else:
+            try:
+                p = next(palettes)
+                screen.register_palette(p)
+                screen.clear()
+            except StopIteration:
+                pass
+            finally:
+                return
+
     if key in ('esc',):
         raise urwid.ExitMainLoop()
 
@@ -43,3 +63,4 @@ class ActivityManager(object):
         if self.app is None:
             return None
         return self.app.original_widget
+

+ 28 - 4
grocery_transactions.py

@@ -10,6 +10,7 @@ import itertools
 import sys
 from typing import Union
 import urwid
+from urwid import raw_display
 from app.db_utils import (
     QueryManager,
     get_insert_product_statement
@@ -40,7 +41,7 @@ except:
     print('Failed to set up db connection. Entering Mock mode')
     from mock import *
 
-palette = [
+dark_palette = [
     ("popup_focus", "light red", "light gray"),
     ('popup', 'light red', 'dark gray'),
     ('banner', 'light gray', 'dark red'),
@@ -48,6 +49,14 @@ palette = [
     ('bg', 'light red', 'black'),
 ]
 
+light_palette = [
+    ("popup_focus", "dark blue", "light gray"),
+    ('popup', 'light blue', 'dark gray'),
+    ('banner', 'light gray', 'dark red'),
+    ('streak', 'white', 'dark gray'),
+    ('bg', 'white', 'dark blue'),
+]
+
 grid_layout = [
     [ 'ts',          'store',    ],
     [ 'organic',     'product',  ],
@@ -209,15 +218,30 @@ activity_manager.create(TransactionEditor, 'transaction',
     query_manager, cols, grid_layout, side_pane, bottom_pane,
     lambda: _save_and_clear_callback(activity_manager),
     lambda widget, name, data: _autocomplete_callback(activity_manager, query_manager, widget, name, data),
-    lambda name, value: _apply_choice_callback(activity_manager, 'transaction', name, value))
+    lambda name, value: _apply_choice_callback(activity_manager, 'transaction', name, value),
+)
 
 app = None
+def iter_palettes():
+    palettes = [dark_palette, light_palette]
+    while True:
+        p = palettes.pop(0)
+        palettes.append(p)
+        yield p
+
+palettes = iter_palettes()
+palette = next(palettes)
+
 try:
     app = GroceryTransactionEditor(activity_manager, cur, log)
-
-    loop = urwid.MainLoop(app, palette, unhandled_input=show_or_exit, pop_ups=True)
+    screen = raw_display.Screen()
+    loop = urwid.MainLoop(app, palette, screen=screen,
+        unhandled_input=lambda k: show_or_exit(k, screen=screen, palettes=palettes),
+        pop_ups=True
+    )
 
     loop.run()
+   
 finally:
     if app is not None:
         app.close()