Skip to content

Commit

Permalink
Added new gui themes parameters: reset/resetall
Browse files Browse the repository at this point in the history
- Added "reset" parameter: resets the selected gui theme options to the default values.
- Added "resetall" parameter: resets all gui theme options to the default values and removes non-default themes.
  • Loading branch information
DuckBoss committed Apr 30, 2023
1 parent 3867c76 commit 80e7b5d
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 2 deletions.
30 changes: 30 additions & 0 deletions src/plugins/builtin_core/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ def themes(self, data: "Command") -> None:
# !themes.delete "theme_name" -> Deletes the specified theme, and falls back to a default theme if it was in use.
# !themes.show "theme_name" -> Shows the config data for the specified theme.
# !themes.update="theme_name" item1=value1... -> Updates a specified theme with the specified new values.
# !themes.reset "theme_name" -> Resets the specified theme back to default options.
# !themes.resetall -> Resets all the themes back to the default set of themes and options.

_parameters = self.verify_parameters(self.themes.__name__, data)
if _parameters is None:
Expand Down Expand Up @@ -173,6 +175,33 @@ def _parameter_themes_new(self, data: "Command", parameter: str) -> None:
target_users=mumble_utils.get_user_by_id(data.actor),
)

def _parameter_themes_reset(self, data: "Command", parameter: str) -> None:
_selected_theme = data.message.strip().replace(" ", "_")
if not theme_utils.reset_theme(_selected_theme):
logger.error(f"[{LogOutputIdentifiers.PLUGINS_COMMANDS}]: '{data.command}' command error: failed to reset theme.")
GUIFramework.gui(
text=f"Failed to reset theme: {_selected_theme}",
target_users=mumble_utils.get_user_by_id(data.actor),
)
return
GUIFramework.gui(
text=f"Resetted selected theme: {_selected_theme}",
target_users=mumble_utils.get_user_by_id(data.actor),
)

def _parameter_themes_resetall(self, data: "Command", parameter: str) -> None:
if not theme_utils.reset_all_themes():
logger.error(f"[{LogOutputIdentifiers.PLUGINS_COMMANDS}]: '{data.command}' command error: failed to reset all themes.")
GUIFramework.gui(
text="Failed to reset all themes.",
target_users=mumble_utils.get_user_by_id(data.actor),
)
return
GUIFramework.gui(
text="Resetted all themes to defaults.",
target_users=mumble_utils.get_user_by_id(data.actor),
)

def _parameter_themes_delete(self, data: "Command", parameter: str) -> None:
_delete_theme = data.message.strip().replace(" ", "_")
if not theme_utils.delete_theme(_delete_theme):
Expand All @@ -194,6 +223,7 @@ def _parameter_themes_update(self, data: "Command", parameter: str) -> None:
f"'{data._command}' command error: a user name was not provided.",
target_users=mumble_utils.get_user_by_id(data.actor),
)
return

_theme_name: str = parameter_split[1].strip().replace("_", " ")
if not _theme_name:
Expand Down
4 changes: 4 additions & 0 deletions src/plugins/builtin_core/utility/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class Themes:
DELETE: str = "delete"
UPDATE: str = "update"
SHOW: str = "show"
RESET: str = "reset"
RESETALL: str = "resetall"

@staticmethod
def get_definitions() -> List[str]:
Expand All @@ -55,4 +57,6 @@ def get_definitions() -> List[str]:
ParameterDefinitions.Themes.DELETE,
ParameterDefinitions.Themes.UPDATE,
ParameterDefinitions.Themes.SHOW,
ParameterDefinitions.Themes.RESET,
ParameterDefinitions.Themes.RESETALL,
]
58 changes: 56 additions & 2 deletions src/plugins/builtin_core/utility/theme_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def delete_theme(theme: str) -> bool:


def new_theme(theme: str) -> bool:
_template_theme = _get_template_theme()
_template_theme = _get_custom_theme_template()
_themes: Optional["Config"] = settings.configs.get_gui_themes()
if not _themes:
logger.error("Failed to create new gui theme: the gui themes could not be retrieved from settings.")
Expand All @@ -73,6 +73,42 @@ def new_theme(theme: str) -> bool:
return True


def reset_theme(theme: str) -> bool:
_template_theme = _get_custom_theme_template()
_themes: Optional["Config"] = settings.configs.get_gui_themes()
if not _themes:
logger.error("Failed to reset gui theme: the gui themes could not be retrieved from settings.")
return False

theme = theme.strip().replace(" ", "_")
_selected_theme = _themes.get(theme, None)
if not _selected_theme:
logger.error(f"Failed to reset gui theme: theme '{theme}' does not exist.")
return False
_selected_theme = _template_theme
_themes.update({theme: _selected_theme})

_themes.save()
logger.debug(f"Reset gui theme from template: {theme}.")

return True


def reset_all_themes() -> bool:
_template_default_themes = _get_default_themes_template()
_themes: Optional["Config"] = settings.configs.get_gui_themes()
if not _themes:
logger.error("Failed to reset gui themes: the gui themes could not be retrieved from settings.")
return False
_themes.clear()
_themes.update(_template_default_themes)

_themes.save()
logger.debug("Reset all gui themes from default themes template.")

return True


def update_theme(theme: str, items: Dict[str, Any]) -> bool:
_themes: Optional["Config"] = settings.configs.get_gui_themes()
if not _themes:
Expand All @@ -96,7 +132,7 @@ def update_theme(theme: str, items: Dict[str, Any]) -> bool:
return True


def _get_template_theme() -> "Config":
def _get_custom_theme_template() -> "Config":
_config: Optional["Config"] = settings.configs.get_mumimo_config()
if not _config:
raise PluginError("Unable to get template theme: mumimo config could not be retrieved from settings.", logger=logger)
Expand All @@ -118,6 +154,24 @@ def _get_template_theme() -> "Config":
return _template


def _get_default_themes_template() -> "Config":
_config: Optional["Config"] = settings.configs.get_mumimo_config()
if not _config:
raise PluginError("Unable to get default template themes: mumimo config could not be retrieved from settings.", logger=logger)

_plugin_path = _config.get(MumimoCfgFields.SETTINGS.PLUGINS.PLUGINS_PATH, None)
if not _plugin_path:
raise PluginError("Unable to get default template themes: mumimo config does not have a defined plugin path.")

_theme_template_path = pathlib.Path.cwd() / _plugin_path / "builtin_core/resources/gui_themes_template.toml"
_themes_template: "Config" = Config(_theme_template_path)
if _themes_template is None:
raise PluginError(f"Unable to get default template themes: default template themes file is missing. Expected path: {_theme_template_path}")
_themes_template.read()

return _themes_template


def _get_theme(theme: str) -> Optional["Config"]:
if not theme:
logger.error("Failed to get gui theme: no theme name provided.")
Expand Down

0 comments on commit 80e7b5d

Please sign in to comment.