Skip to content

Commit

Permalink
feat(multi_export, export_config, settings): save and load last state…
Browse files Browse the repository at this point in the history
… in multi-export (fixes #521)
  • Loading branch information
actionless committed Jun 13, 2024
1 parent d776948 commit 446357f
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 42 deletions.
76 changes: 43 additions & 33 deletions oomox_gui/export_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,16 @@

class ExportConfig(CommonOomoxConfig):

def __init__(self, config_name: str, default_config: dict[str, "Any"] | None = None) -> None:
def __init__(
self,
config_name: str,
default_config: dict[str, "Any"] | None = None,
override_config: dict[str, "Any"] | None = None,
) -> None:
super().__init__(
config_name=config_name,
default_config=default_config,
override_config=override_config,
config_dir=USER_EXPORT_CONFIG_DIR,
force_reload=True,
)
Expand Down Expand Up @@ -345,6 +351,7 @@ class ExportDialogWithOptions(FileBasedExportDialog):
OPTIONS: ExportDialogWithOptionsOptions = ExportDialogWithOptionsOptions()

config_name: str
export_config: ExportConfig

@g_abstractproperty # type: ignore[no-redef]
def config_name(self) -> None:
Expand All @@ -362,34 +369,8 @@ def callback(widget: Gtk.Entry) -> None:
self.export_config[option_id] = widget.get_text()
return callback

def __init__(
self,
transient_for: Gtk.Window,
colorscheme: "ThemeT",
theme_name: str,
export_options: "dict[str, Any] | None" = None,
override_options: dict[str, "Any"] | None = None,
headline: str | None = None,
**kwargs: "Any",
) -> None:
self.option_widgets: dict[str, Gtk.Widget] = {}
export_options = override_options or export_options or {}
super().__init__(
transient_for=transient_for, colorscheme=colorscheme, theme_name=theme_name,
headline=headline or translate("Theme Export Options"),
**kwargs,
)
self.label.hide()

self.export_config = ExportConfig(
config_name=self.config_name,
default_config={
option_name: option["default"]
for option_name, option in export_options.items()
},
)

for option_name, option in export_options.items():
def load_state_from_config(self) -> None:
for option_name, option in self.export_options.items():
value = self.export_config[option_name]
value_widget: Gtk.Widget
if isinstance(value, bool):
Expand Down Expand Up @@ -421,6 +402,37 @@ def __init__(
raise NotImplementedError
self.options_box.add(value_widget)

def __init__(
self,
transient_for: Gtk.Window,
colorscheme: "ThemeT",
theme_name: str,
export_options: "dict[str, Any] | None" = None,
override_options: dict[str, "Any"] | None = None,
override_config: dict[str, "Any"] | None = None,
headline: str | None = None,
**kwargs: "Any",
) -> None:
self.option_widgets: dict[str, Gtk.Widget] = {}
self.export_options = override_options or export_options or {}
super().__init__(
transient_for=transient_for, colorscheme=colorscheme, theme_name=theme_name,
headline=headline or translate("Theme Export Options"),
**kwargs,
)
self.label.hide()

self.export_config = ExportConfig(
config_name=self.config_name,
default_config={
option_name: option["default"]
for option_name, option in self.export_options.items()
},
override_config=override_config,
)

self.load_state_from_config()

self.box.add(self.options_box)
self.options_box.show_all()
self.box.add(self.apply_button)
Expand Down Expand Up @@ -491,12 +503,10 @@ def do_export(self) -> None:
export_path = os.path.expanduser(
self.option_widgets[self.OPTIONS.DEFAULT_PATH].get_text(), # type: ignore[attr-defined]
)
new_destination_dir, _theme_name = export_path.rsplit("/", 1)

super().do_export()

new_destination_dir, _theme_name = export_path.rstrip("/").rsplit("/", 1)
self.export_config[self.OPTIONS.DEFAULT_PATH] = new_destination_dir
self.export_config.save()
super().do_export()


class CommonGtkThemeExportDialogOptions(DialogWithExportPathOptions):
Expand Down
35 changes: 27 additions & 8 deletions oomox_gui/multi_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

from gi.repository import Gio, Gtk

from .export_common import ExportDialog
from .config import USER_EXPORT_CONFIG_DIR
from .export_common import ExportDialogWithOptions
from .gtk_helpers import (
ImageButton,
ImageMenuButton,
Expand All @@ -11,6 +12,7 @@
from .i18n import translate
from .plugin_api import OomoxExportPlugin, OomoxIconsPlugin, OomoxThemePlugin
from .plugin_loader import PluginLoader
from .settings import CommonOomoxConfig

if TYPE_CHECKING:
from collections.abc import Callable
Expand All @@ -29,15 +31,17 @@ class ExportWrapper(Gtk.Box):

def __init__(
self,
name: str,
plugin: OomoxExportPlugin | OomoxThemePlugin | OomoxIconsPlugin,
export_dialog: ExportDialog,
export_dialog: ExportDialogWithOptions,
remove_callback: "Callable[[Any], None]",
) -> None:
super().__init__( # type: ignore[misc]
self, # type: ignore[arg-type]
orientation=Gtk.Orientation.VERTICAL,
spacing=5,
)
self.name = name
self.header = Gtk.Box(
orientation=Gtk.Orientation.HORIZONTAL,
spacing=5,
Expand Down Expand Up @@ -72,7 +76,7 @@ class MultiExportDialog(BaseClass):

added_plugins: list[ExportWrapper]

def __init__(
def __init__( # pylint: disable=too-many-locals
self,
transient_for: Gtk.Window,
colorscheme: "ThemeT",
Expand All @@ -88,7 +92,6 @@ def __init__(
self.colorscheme_name = theme_name
self.set_default_size(width, height)

# self.box = self.get_content_area()
self.box = Gtk.Box(
orientation=Gtk.Orientation.VERTICAL, spacing=DEFAULT_PADDING,
)
Expand Down Expand Up @@ -161,27 +164,40 @@ def __init__(
))

self.box.pack_start(add_export_target_button, False, False, 0)
# self.box.set_center_widget(self.scroll)
self.box.pack_start(self.scroll, True, True, 0)
self.box.pack_end(export_all_button, False, False, 0)

self.show_all()

self.config = CommonOomoxConfig(
config_dir=USER_EXPORT_CONFIG_DIR,
config_name="multi_export",
force_reload=True,
)
for plugin_name, plugin_config in self.config.config.items():
self.add_export_target(plugin_name, plugin_config)

def _on_remove_export_target(self, export: ExportWrapper) -> None:
self.added_plugins.remove(export)
self.background.remove(export)

def add_export_target(self, export_plugin_name: str) -> None:
plugin = self.plugins[export_plugin_name]
def add_export_target(
self,
export_plugin_name: str,
default_config: dict[str, "Any"] | None = None,
) -> None:
plugin = self.plugins.get(export_plugin_name)
if not plugin:
raise TypeError
return
export = ExportWrapper(
name=export_plugin_name,
plugin=plugin,
export_dialog=plugin.export_dialog(
transient_for=self,
theme_name=self.colorscheme_name,
colorscheme=self.colorscheme,
base_class=Gtk.Box, # type: ignore[arg-type]
override_config=default_config,
),
remove_callback=lambda _x: self._on_remove_export_target(export),
)
Expand All @@ -195,5 +211,8 @@ def _on_add_export_target(self, action: Gio.SimpleAction, _param: "Any" = None)
self.add_export_target(export_plugin_name)

def _on_export_all(self, _button: Gtk.Button) -> None:
self.config.config = {}
for export in self.added_plugins:
export.export_dialog.do_export()
self.config.config[export.name] = export.export_dialog.export_config.config
self.config.save()
3 changes: 2 additions & 1 deletion oomox_gui/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def __init__(
config_dir: str,
config_name: str,
default_config: dict[str, "Any"] | None = None,
override_config: dict[str, "Any"] | None = None,
force_reload: bool = False,
) -> None:
self.name = config_name
Expand All @@ -34,7 +35,7 @@ def __init__(
f"{self.name}.json",
)
self.default_config = default_config or {}
self.config = self.load(
self.config = override_config or self.load(
default_config=self.default_config,
config_path=self.config_path,
force_reload=force_reload,
Expand Down

0 comments on commit 446357f

Please sign in to comment.