Skip to content

Commit

Permalink
feat: scrub toggle for gui (#375)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
0dm and abrichr authored Jul 28, 2023
1 parent e1f6e5e commit 1e96a4f
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 14 deletions.
16 changes: 10 additions & 6 deletions openadapt/app/cards.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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()


Expand Down
12 changes: 6 additions & 6 deletions openadapt/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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",
Expand Down
29 changes: 27 additions & 2 deletions openadapt/app/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand All @@ -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)
32 changes: 32 additions & 0 deletions openadapt/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": "*",
Expand Down Expand Up @@ -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.
Expand All @@ -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:
Expand Down

0 comments on commit 1e96a4f

Please sign in to comment.