actually load the settings when you open the app

This commit is contained in:
SpookyDervish
2025-10-30 07:20:01 +11:00
parent 7bc671ef63
commit 4f23e7570c
3 changed files with 40 additions and 12 deletions

24
main.py
View File

@@ -11,6 +11,7 @@ from pathlib import Path
from assets.theme_mappings import theme_mappings from assets.theme_mappings import theme_mappings
from plugin_loader import PluginLoader from plugin_loader import PluginLoader
from settings import SettingsScreen from settings import SettingsScreen
from settings_store import ConfigHandler
from watchdog.observers import Observer from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler from watchdog.events import FileSystemEventHandler
@@ -43,6 +44,7 @@ class Berry(App):
def __init__(self, path: str): def __init__(self, path: str):
super().__init__() super().__init__()
self.path = path self.path = path
self.config_handler = ConfigHandler(self)
def compose(self) -> ComposeResult: def compose(self) -> ComposeResult:
yield Header() yield Header()
@@ -80,7 +82,25 @@ class Berry(App):
self.push_screen(SettingsScreen()) self.push_screen(SettingsScreen())
def get_system_commands(self, screen): 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) yield SystemCommand("Settings", "Open the settings menu", self.action_settings)
def on_input_submitted(self, event: Input.Submitted): def on_input_submitted(self, event: Input.Submitted):
@@ -358,6 +378,8 @@ class Berry(App):
#else: #else:
# self.query_one("#terminal").start() # self.query_one("#terminal").start()
self.config_handler.apply_settings()
if __name__ == "__main__": if __name__ == "__main__":
app = Berry("./") app = Berry("./")
app.run() app.run()

View File

@@ -3,10 +3,6 @@ from textual.widgets import Label, Select, TabbedContent, TabPane, Switch
from textual.containers import Vertical, HorizontalGroup, VerticalGroup from textual.containers import Vertical, HorizontalGroup, VerticalGroup
from textual.binding import Binding from textual.binding import Binding
from settings_store import ConfigHandler
import os
class SettingsScreen(ModalScreen): class SettingsScreen(ModalScreen):
border_title = "Settings" border_title = "Settings"
@@ -58,7 +54,6 @@ class SettingsScreen(ModalScreen):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
self.config_handler = ConfigHandler()
def action_close(self): def action_close(self):
self.dismiss() self.dismiss()
@@ -66,13 +61,12 @@ class SettingsScreen(ModalScreen):
def on_switch_changed(self, event: Switch.Changed): def on_switch_changed(self, event: Switch.Changed):
if event.switch.id == "word-wrap": if event.switch.id == "word-wrap":
self.app.query_one("#code-editor").soft_wrap = event.value self.app.query_one("#code-editor").soft_wrap = event.value
self.config_handler.set("editor", "word_wrap", str(int(event.value))) self.app.config_handler.set("editor", "word_wrap", str(int(event.value)))
self.notify(str(int(event.value)))
def on_select_changed(self, event: Select.Changed): def on_select_changed(self, event: Select.Changed):
if event.select.id == "colour-theme": if event.select.id == "colour-theme":
self.app.theme = event.value 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): def compose(self):
with Vertical(id="window") as window: with Vertical(id="window") as window:
@@ -97,7 +91,7 @@ class SettingsScreen(ModalScreen):
with VerticalGroup(): with VerticalGroup():
yield Label("Word Wrap", classes="setting-name") yield Label("Word Wrap", classes="setting-name")
yield Label("Enable word wrap in the code editor.", classes="setting-desc") 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 TabPane("Plugins"):
with HorizontalGroup(classes="setting"): with HorizontalGroup(classes="setting"):
with VerticalGroup(): with VerticalGroup():

View File

@@ -1,10 +1,12 @@
from textual.app import App
from pathlib import Path from pathlib import Path
import os import os
import configparser import configparser
class ConfigHandler: class ConfigHandler:
def __init__(self): def __init__(self, app: App):
self.app: App = app
self.config: configparser.ConfigParser = configparser.ConfigParser() self.config: configparser.ConfigParser = configparser.ConfigParser()
self.config_dir: str = self.ensure_hidden_config_dir() self.config_dir: str = self.ensure_hidden_config_dir()
@@ -20,7 +22,17 @@ class ConfigHandler:
return self.config.get(section, option) return self.config.get(section, option)
def set(self, section: str, option: str, new_value: str): 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): def load_settings(self):
if os.path.isfile(self.config_dir / "config.ini"): if os.path.isfile(self.config_dir / "config.ini"):