From 4f23e7570c7d519b6de841abeece7dd170b987d0 Mon Sep 17 00:00:00 2001 From: SpookyDervish <78246495+SpookyDervish@users.noreply.github.com> Date: Thu, 30 Oct 2025 07:20:01 +1100 Subject: [PATCH] actually load the settings when you open the app --- main.py | 24 +++++++++++++++++++++++- settings.py | 12 +++--------- settings_store.py | 16 ++++++++++++++-- 3 files changed, 40 insertions(+), 12 deletions(-) diff --git a/main.py b/main.py index 6cf4397..ce52228 100644 --- a/main.py +++ b/main.py @@ -11,6 +11,7 @@ from pathlib import Path from assets.theme_mappings import theme_mappings from plugin_loader import PluginLoader from settings import SettingsScreen +from settings_store import ConfigHandler from watchdog.observers import Observer from watchdog.events import FileSystemEventHandler @@ -43,6 +44,7 @@ class Berry(App): def __init__(self, path: str): super().__init__() self.path = path + self.config_handler = ConfigHandler(self) def compose(self) -> ComposeResult: yield Header() @@ -80,7 +82,25 @@ class Berry(App): self.push_screen(SettingsScreen()) def get_system_commands(self, screen): - yield from super().get_system_commands(screen) + yield SystemCommand( + "Quit the application", + "Quit the application as soon as possible", + self.action_quit, + ) + + if screen.query("HelpPanel"): + yield SystemCommand( + "Hide keys and help panel", + "Hide the keys and widget help panel", + self.action_hide_help_panel, + ) + else: + yield SystemCommand( + "Show keys and help panel", + "Show help for the focused widget and a summary of available keys", + self.action_show_help_panel, + ) + yield SystemCommand("Settings", "Open the settings menu", self.action_settings) def on_input_submitted(self, event: Input.Submitted): @@ -358,6 +378,8 @@ class Berry(App): #else: # self.query_one("#terminal").start() + self.config_handler.apply_settings() + if __name__ == "__main__": app = Berry("./") app.run() \ No newline at end of file diff --git a/settings.py b/settings.py index c51836b..37de181 100644 --- a/settings.py +++ b/settings.py @@ -3,10 +3,6 @@ from textual.widgets import Label, Select, TabbedContent, TabPane, Switch from textual.containers import Vertical, HorizontalGroup, VerticalGroup from textual.binding import Binding -from settings_store import ConfigHandler - -import os - class SettingsScreen(ModalScreen): border_title = "Settings" @@ -58,7 +54,6 @@ class SettingsScreen(ModalScreen): def __init__(self): super().__init__() - self.config_handler = ConfigHandler() def action_close(self): self.dismiss() @@ -66,13 +61,12 @@ class SettingsScreen(ModalScreen): def on_switch_changed(self, event: Switch.Changed): if event.switch.id == "word-wrap": self.app.query_one("#code-editor").soft_wrap = event.value - self.config_handler.set("editor", "word_wrap", str(int(event.value))) - self.notify(str(int(event.value))) + self.app.config_handler.set("editor", "word_wrap", str(int(event.value))) def on_select_changed(self, event: Select.Changed): if event.select.id == "colour-theme": self.app.theme = event.value - self.config_handler.set("appearance", "colour_theme", str(event.value)) + self.app.config_handler.set("appearance", "colour_theme", str(event.value)) def compose(self): with Vertical(id="window") as window: @@ -97,7 +91,7 @@ class SettingsScreen(ModalScreen): with VerticalGroup(): yield Label("Word Wrap", classes="setting-name") yield Label("Enable word wrap in the code editor.", classes="setting-desc") - yield Switch(value=bool(int(self.config_handler.get("editor", "word_wrap"))), id="word-wrap") + yield Switch(value=bool(int(self.app.config_handler.get("editor", "word_wrap"))), id="word-wrap") with TabPane("Plugins"): with HorizontalGroup(classes="setting"): with VerticalGroup(): diff --git a/settings_store.py b/settings_store.py index 8b649fc..fdfdb03 100644 --- a/settings_store.py +++ b/settings_store.py @@ -1,10 +1,12 @@ +from textual.app import App from pathlib import Path import os import configparser class ConfigHandler: - def __init__(self): + def __init__(self, app: App): + self.app: App = app self.config: configparser.ConfigParser = configparser.ConfigParser() self.config_dir: str = self.ensure_hidden_config_dir() @@ -20,7 +22,17 @@ class ConfigHandler: return self.config.get(section, option) def set(self, section: str, option: str, new_value: str): - return self.config.set(section, option, new_value) + self.config.set(section, option, new_value) + self.write_settings() + + def apply_settings(self): + self.app.query_one("#code-editor").soft_wrap = bool(int(self.get("editor", "word_wrap"))) + self.app.theme = self.get("appearance", "colour_theme") + self.app.notify(self.get("appearance", "colour_theme")) + + def write_settings(self): + with open(self.config_dir / "config.ini", "w") as configfile: + self.config.write(configfile) def load_settings(self): if os.path.isfile(self.config_dir / "config.ini"):