Skip to content

Commit

Permalink
Feat: Allow late ctx menu registration (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
mahaloz authored Nov 11, 2024
1 parent 48536ee commit a1fc519
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
2 changes: 1 addition & 1 deletion libbs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "2.5.0"
__version__ = "2.6.0"


import logging
Expand Down
44 changes: 26 additions & 18 deletions libbs/api/decompiler_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def __init__(
self._headless_dec_path = Path(headless_dec_path) if headless_dec_path else None
self._binary_path = Path(binary_path) if binary_path else None
self._init_plugin = init_plugin
self._unparsed_gui_ctx_actions = gui_ctx_menu_actions or {}
self._unparsed_gui_ctx_actions: dict[str, tuple[str, Callable]] = gui_ctx_menu_actions or {}
# (category, name, action_string, callback_func)
self._gui_ctx_menu_actions = []
self._plugin_name = plugin_name
Expand Down Expand Up @@ -136,23 +136,8 @@ def _init_gui_components(self, *args, **kwargs):
if self._init_plugin:
self.gui_plugin = self._init_gui_plugin(*args, **kwargs)

# parse all context menu actions
for combined_name, items in self._unparsed_gui_ctx_actions.items():
slashes = list(re.finditer("/", combined_name))
if not slashes:
category = ""
name = combined_name
else:
last_slash = slashes[-1]
category = combined_name[:last_slash.start()]
name = combined_name[last_slash.start()+1:]

self._gui_ctx_menu_actions.append((category, name,) + items)

# register all context menu actions
for action in self._gui_ctx_menu_actions:
category, name, action_string, callback_func = action
self.gui_register_ctx_menu(name, action_string, callback_func, category=category)
# parse & register all context menu actions
self.gui_register_ctx_menu_many(self._unparsed_gui_ctx_actions)

def _init_gui_plugin(self, *args, **kwargs):
return None
Expand Down Expand Up @@ -226,6 +211,29 @@ def gui_popup_text(self, text: str, title: str = "Plugin Message") -> bool:
from libbs.ui.utils import gui_popup_text
return gui_popup_text(text, title=title)

@staticmethod
def _parse_ctx_menu_actions(actions: dict[str, tuple[str, Callable]]) -> List[Tuple[str, str, str, Callable]]:
gui_ctx_menu_actions = []
for combined_name, items in actions.items():
slashes = list(re.finditer("/", combined_name))
if not slashes:
category = ""
name = combined_name
else:
last_slash = slashes[-1]
category = combined_name[:last_slash.start()]
name = combined_name[last_slash.start()+1:]

gui_ctx_menu_actions.append((category, name,) + items)

return gui_ctx_menu_actions

def gui_register_ctx_menu_many(self, actions: dict[str, tuple[str, Callable]]):
parsed_actions = self._parse_ctx_menu_actions(actions)
for action in parsed_actions:
category, name, action_string, callback_func = action
self.gui_register_ctx_menu(name, action_string, callback_func, category=category)

#
# Override Mandatory API
#
Expand Down

0 comments on commit a1fc519

Please sign in to comment.