From 1e96a4f599b9790509dad865ea3c7b1b254fb19b Mon Sep 17 00:00:00 2001 From: Aaron <57018940+0dm@users.noreply.github.com> Date: Fri, 28 Jul 2023 15:04:10 -0400 Subject: [PATCH] feat: scrub toggle for gui (#375) * add scrub toggle + write dark_mode to env * Update config.py * Update util.py * Update config.py * address comments * Update config.py * run isort * from first * Update openadapt/config.py * Update openadapt/config.py * Update openadapt/config.py * Update openadapt/config.py * Update openadapt/config.py * Update openadapt/config.py * add env file path (also where did the toggle go??) * Update config.py * Update config.py * Update config.py * isort * Update util.py * linted --------- Co-authored-by: Richard Abrich --- openadapt/app/cards.py | 16 ++++++++++------ openadapt/app/main.py | 12 ++++++------ openadapt/app/util.py | 29 +++++++++++++++++++++++++++-- openadapt/config.py | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 14 deletions(-) diff --git a/openadapt/app/cards.py b/openadapt/app/cards.py index 8ee9a6ef5..4e500fd24 100644 --- a/openadapt/app/cards.py +++ b/openadapt/app/cards.py @@ -9,7 +9,7 @@ from nicegui import ui from openadapt.app.objects.local_file_picker import LocalFilePicker -from openadapt.app.util import set_dark, sync_switch +from openadapt.app.util import get_scrub, set_dark, set_scrub, sync_switch PROC = None @@ -21,13 +21,17 @@ def settings(dark_mode: bool) -> None: dark_mode (bool): Current dark mode setting. """ with ui.dialog() as settings, ui.card(): - s = ui.switch( - "Dark mode", - on_change=lambda: set_dark(dark_mode, s.value), + dark_switch = ui.switch( + "Dark mode", on_change=lambda: set_dark(dark_mode, dark_switch.value) ) - sync_switch(s, dark_mode) - ui.button("Close", on_click=lambda: settings.close()) + sync_switch(dark_switch, dark_mode) + + scrub_switch = ui.switch( + "Scrubbing", on_change=lambda: set_scrub(scrub_switch.value) + ) + sync_switch(scrub_switch, get_scrub()) + ui.button("Close", on_click=lambda: settings.close()) settings.open() diff --git a/openadapt/app/main.py b/openadapt/app/main.py index 3c62dd69d..b5a267083 100644 --- a/openadapt/app/main.py +++ b/openadapt/app/main.py @@ -10,11 +10,10 @@ import base64 import os -import threading from nicegui import app, ui -from openadapt import replay, visualize +from openadapt import config, replay, visualize from openadapt.app.cards import recording_prompt, select_import, settings from openadapt.app.objects.console import Console from openadapt.app.util import clear_db, on_export, on_import @@ -29,6 +28,8 @@ def run_app() -> None: app.native.start_args["debug"] = False dark = ui.dark_mode() + dark.value = config.APP_DARK_MODE + logger = None # Add logo @@ -70,10 +71,9 @@ def run_app() -> None: ) .tooltip("Record a new replay / Stop recording") ) - ui.icon("visibility", size="64px").on( - "click", - lambda: threading.Thread(target=visualize.main).start(), - ).tooltip("Visualize the latest replay") + ui.icon("visibility", size="64px").on("click", visualize.main).tooltip( + "Visualize the latest replay" + ) ui.icon("play_arrow", size="64px").on( "click", diff --git a/openadapt/app/util.py b/openadapt/app/util.py index 94d26742b..8190677f0 100644 --- a/openadapt/app/util.py +++ b/openadapt/app/util.py @@ -19,6 +19,7 @@ from nicegui import elements, ui +from openadapt import config from openadapt.app.objects import console from openadapt.scripts.reset_db import reset_db @@ -97,7 +98,29 @@ def sync_switch( switch: The switch object. prop: The property object. """ - switch.value = prop.value + switch.value = prop.value if hasattr(prop, "value") else prop + + +def set_scrub(value: bool) -> None: + """Set the scrubbing value. + + Args: + value: The value to set. + """ + if config.SCRUB_ENABLED != value: + config.persist_env("SCRUB_ENABLED", value) + config.SCRUB_ENABLED = value + ui.notify("Scrubbing enabled." if value else "Scrubbing disabled.") + ui.notify("You may need to restart the app for this to take effect.") + + +def get_scrub() -> bool: + """Get the scrubbing value. + + Returns: + bool: The scrubbing value. + """ + return config.SCRUB_ENABLED def set_dark(dark_mode: ui.dark_mode, value: bool) -> None: @@ -107,4 +130,6 @@ def set_dark(dark_mode: ui.dark_mode, value: bool) -> None: dark_mode: The dark mode object. value: The value to set. """ - dark_mode.value = value + if dark_mode.value != value: + dark_mode.value = value + config.persist_env("APP_DARK_MODE", value) diff --git a/openadapt/config.py b/openadapt/config.py index da5a99c23..7d52f8fdf 100644 --- a/openadapt/config.py +++ b/openadapt/config.py @@ -36,6 +36,8 @@ "ACTION_TEXT_SEP": "-", "ACTION_TEXT_NAME_PREFIX": "<", "ACTION_TEXT_NAME_SUFFIX": ">", + # APP CONFIGURATIONS + "APP_DARK_MODE": False, # SCRUBBING CONFIGURATIONS "SCRUB_ENABLED": False, "SCRUB_CHAR": "*", @@ -102,6 +104,8 @@ list(stop_str) for stop_str in STOP_STRS ] + SPECIAL_CHAR_STOP_SEQUENCES +ENV_FILE_PATH = ".env" + def getenv_fallback(var_name: str) -> str: """Get the value of an environment variable or fallback to the default value. @@ -128,6 +132,34 @@ def getenv_fallback(var_name: str) -> str: return rval +def persist_env(var_name: str, val: str, env_file_path: str = ENV_FILE_PATH) -> None: + """Persist an environment variable to a .env file. + + Args: + var_name (str): The name of the environment variable. + val (str): The value of the environment variable. + env_file_path (str, optional): The path to the .env file (default: ".env"). + """ + if not os.path.exists(env_file_path): + with open(env_file_path, "w") as f: + f.write(f"{var_name}={val}") + else: + # find and replace + with open(env_file_path, "r") as f: + lines = f.readlines() + for i, line in enumerate(lines): + if line.startswith(f"{var_name}="): + lines[i] = f"{var_name}={val}\n" + break + else: + # we didn't find the variable in the file, so append it + if lines[-1][-1] != "\n": + lines.append("\n") + lines.append(f"{var_name}={val}") + with open(env_file_path, "w") as f: + f.writelines(lines) + + load_dotenv() for key in _DEFAULTS: