diff --git a/contact/ui/contact_ui.py b/contact/ui/contact_ui.py index bee8932..86f851d 100644 --- a/contact/ui/contact_ui.py +++ b/contact/ui/contact_ui.py @@ -13,7 +13,7 @@ from contact.utilities.input_handlers import get_list_input import contact.ui.default_config as config import contact.ui.dialog from contact.ui.nav_utils import move_main_highlight, draw_main_arrows, get_msg_window_lines, wrap_text -from contact.utilities.singleton import ui_state, interface_state +from contact.utilities.singleton import ui_state, interface_state, menu_state def handle_resize(stdscr: curses.window, firstrun: bool) -> None: @@ -602,6 +602,8 @@ def draw_messages_window(scroll_to_bottom: bool = False) -> None: refresh_pad(1) draw_packetlog_win() + if ui_state.current_window == 4: + menu_state.need_redraw = True def draw_node_list() -> None: diff --git a/contact/ui/control_ui.py b/contact/ui/control_ui.py index e9c584f..45cbaa1 100644 --- a/contact/ui/control_ui.py +++ b/contact/ui/control_ui.py @@ -20,9 +20,9 @@ from contact.ui.dialog import dialog from contact.ui.menus import generate_menu_from_protobuf from contact.ui.nav_utils import move_highlight, draw_arrows, update_help_window from contact.ui.user_config import json_editor -from contact.ui.ui_state import MenuState +from contact.utilities.singleton import menu_state -menu_state = MenuState() +# menu_state = MenuState() # Constants width = 80 @@ -45,7 +45,7 @@ config_folder = os.path.join(locals_dir, "node-configs") field_mapping, help_text = parse_ini_file(translation_file) -def display_menu(menu_state: MenuState) -> tuple[object, object]: # curses.window or pad types +def display_menu() -> tuple[object, object]: # curses.window or pad types min_help_window_height = 6 num_items = len(menu_state.current_menu) + (1 if menu_state.show_save_option else 0) @@ -106,7 +106,7 @@ def display_menu(menu_state: MenuState) -> tuple[object, object]: # curses.wind ) # Draw help window with dynamically updated max_help_lines - draw_help_window(start_y, start_x, menu_height, max_help_lines, transformed_path, menu_state) + draw_help_window(start_y, start_x, menu_height, max_help_lines, transformed_path) menu_win.refresh() menu_pad.refresh( @@ -132,7 +132,6 @@ def draw_help_window( menu_height: int, max_help_lines: int, transformed_path: List[str], - menu_state: MenuState, ) -> None: global help_win @@ -168,11 +167,11 @@ def settings_menu(stdscr: object, interface: object) -> None: modified_settings = {} - need_redraw = True + menu_state.need_redraw = True menu_state.show_save_option = False while True: - if need_redraw: + if menu_state.need_redraw == True: options = list(menu_state.current_menu.keys()) menu_state.show_save_option = ( @@ -185,10 +184,11 @@ def settings_menu(stdscr: object, interface: object) -> None: ) # Display the menu - menu_win, menu_pad = display_menu(menu_state) + menu_win, menu_pad = display_menu() - need_redraw = False + menu_state.need_redraw = False + menu_win.timeout(200) # wait up to 200 ms for a keypress (or less if key is pressed) # Capture user input key = menu_win.getch() @@ -224,7 +224,7 @@ def settings_menu(stdscr: object, interface: object) -> None: ) elif key == curses.KEY_RESIZE: - need_redraw = True + menu_state.need_redraw = True curses.update_lines_cols() menu_win.erase() @@ -248,7 +248,7 @@ def settings_menu(stdscr: object, interface: object) -> None: ) elif key == curses.KEY_RIGHT or key == ord("\n"): - need_redraw = True + menu_state.need_redraw = True menu_state.start_index.append(0) menu_win.erase() help_win.erase() @@ -390,7 +390,7 @@ def settings_menu(stdscr: object, interface: object) -> None: menu_state.start_index.pop() menu_state.selected_index = 4 continue - # need_redraw = True + # menu_state.need_redraw = True field_info = menu_state.current_menu.get(selected_option) if isinstance(field_info, tuple): @@ -509,7 +509,7 @@ def settings_menu(stdscr: object, interface: object) -> None: menu_state.selected_index = 0 elif key == curses.KEY_LEFT: - need_redraw = True + menu_state.need_redraw = True menu_win.erase() help_win.erase() diff --git a/contact/ui/ui_state.py b/contact/ui/ui_state.py index 80f9278..b888040 100644 --- a/contact/ui/ui_state.py +++ b/contact/ui/ui_state.py @@ -10,6 +10,7 @@ class MenuState: current_menu: Union[Dict[str, Any], List[Any], str, int] = field(default_factory=dict) menu_path: List[str] = field(default_factory=list) show_save_option: bool = False + need_redraw: bool = False @dataclass diff --git a/contact/ui/user_config.py b/contact/ui/user_config.py index cb67072..1ffc193 100644 --- a/contact/ui/user_config.py +++ b/contact/ui/user_config.py @@ -7,6 +7,7 @@ from contact.ui.colors import get_color, setup_colors, COLOR_MAP from contact.ui.default_config import format_json_single_line_arrays, loaded_config from contact.ui.nav_utils import move_highlight, draw_arrows from contact.utilities.input_handlers import get_list_input +from contact.utilities.singleton import menu_state width = 80 @@ -106,7 +107,7 @@ def edit_value(key: str, current_value: str) -> str: return user_input if user_input else current_value -def display_menu(menu_state: Any) -> tuple[Any, Any, List[str]]: +def display_menu() -> tuple[Any, Any, List[str]]: """ Render the configuration menu with a Save button directly added to the window. """ @@ -211,14 +212,14 @@ def json_editor(stdscr: curses.window, menu_state: Any) -> None: menu_state.current_menu = data # Track the current level of the menu # Render the menu - menu_win, menu_pad, options = display_menu(menu_state) - need_redraw = True + menu_win, menu_pad, options = display_menu() + menu_state.need_redraw = True while True: - if need_redraw: - menu_win, menu_pad, options = display_menu(menu_state) + if menu_state.need_redraw == True: + menu_win, menu_pad, options = display_menu() menu_win.refresh() - need_redraw = False + menu_state.need_redraw = False max_index = len(options) + (1 if menu_state.show_save_option else 0) - 1 key = menu_win.getch() @@ -248,7 +249,7 @@ def json_editor(stdscr: curses.window, menu_state: Any) -> None: elif key in (curses.KEY_RIGHT, 10, 13): # 10 = \n, 13 = carriage return - need_redraw = True + menu_state.need_redraw = True menu_win.erase() menu_win.refresh() @@ -287,7 +288,7 @@ def json_editor(stdscr: curses.window, menu_state: Any) -> None: menu_state.menu_index.pop() menu_state.start_index.pop() menu_state.current_menu[selected_key] = new_value - need_redraw = True + menu_state.need_redraw = True else: # Save button selected @@ -297,7 +298,7 @@ def json_editor(stdscr: curses.window, menu_state: Any) -> None: elif key in (27, curses.KEY_LEFT): # Escape or Left Arrow - need_redraw = True + menu_state.need_redraw = True menu_win.erase() menu_win.refresh() diff --git a/contact/utilities/singleton.py b/contact/utilities/singleton.py index 17fb99b..8fca9c3 100644 --- a/contact/utilities/singleton.py +++ b/contact/utilities/singleton.py @@ -1,5 +1,6 @@ -from contact.ui.ui_state import ChatUIState, InterfaceState, AppState +from contact.ui.ui_state import ChatUIState, InterfaceState, AppState, MenuState ui_state = ChatUIState() interface_state = InterfaceState() app_state = AppState() +menu_state = MenuState()