Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add toggled to command #41

Merged
merged 1 commit into from
Jul 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/app_model/backends/qt/_qaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ def __init__(
self.triggered.connect(self._on_triggered)

def _on_triggered(self, checked: bool) -> None:
self._app.commands.execute_command(self._command_id)
# execute_command returns a Future, for the sake of eventually being
# asynchronous without breaking the API. For now, we call result()
# to raise any exceptions.
self._app.commands.execute_command(self._command_id).result()


class QCommandRuleAction(QCommandAction):
Expand Down Expand Up @@ -80,10 +83,13 @@ def __init__(
self.setToolTip(command_rule.tooltip)
if command_rule.status_tip:
self.setStatusTip(command_rule.status_tip)
self.setCheckable(command_rule.toggled is not None)

def update_from_context(self, ctx: Mapping[str, object]) -> None:
"""Update the enabled state of this menu item from `ctx`."""
self.setEnabled(expr.eval(ctx) if (expr := self._cmd_rule.enablement) else True)
if expr := self._cmd_rule.toggled:
self.setChecked(expr.eval(ctx))


class QMenuItemAction(QCommandRuleAction):
Expand Down
5 changes: 5 additions & 0 deletions src/app_model/types/_command_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ class CommandRule(_StrictModel):
"the UI. Menus pick either `title` or `short_title` depending on the context "
"in which they show commands.",
)
toggled: Optional[expressions.Expr] = Field(
None,
description="(Optional) Condition under which the command should appear "
"in any GUI representation (like a menu).",
)

def _as_command_rule(self) -> "CommandRule":
"""Simplify (subclasses) to a plain CommandRule."""
Expand Down
8 changes: 8 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Commands:
REDO = "redo"
COPY = "copy"
PASTE = "paste"
TOGGLE_THING = "toggle_thing"
OPEN_FROM_A = "open.from_a"
OPEN_FROM_B = "open.from_b"
UNIMPORTABLE = "unimportable"
Expand Down Expand Up @@ -195,6 +196,13 @@ def build_app(name: str = "complete_test_app") -> FullApp:
title="Will raise an error",
callback=_raise_an_error,
),
Action(
id=Commands.TOGGLE_THING,
title="Toggle Thing",
callback=lambda: None,
menus=[{"id": Menus.HELP}],
toggled="thing_toggled",
),
]
for action in actions:
app.register_action(action)
Expand Down
13 changes: 13 additions & 0 deletions tests/test_qt/test_qmenu.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,16 @@ def test_cache_action(full_app: "FullApp") -> None:
a1 = QMenuItemAction(action, full_app)
a2 = QMenuItemAction(action, full_app)
assert a1 is a2


def test_toggled_menu_item(qtbot: "QtBot", full_app: "FullApp") -> None:
app = full_app
menu = QModelMenu(app.Menus.HELP, app)
qtbot.addWidget(menu)

menu.update_from_context({"thing_toggled": True})
action = menu.findAction(app.Commands.TOGGLE_THING)
assert action.isChecked()

menu.update_from_context({"thing_toggled": False})
assert not action.isChecked()