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

fix command palette key #4890

Merged
merged 8 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- Fix crash when `validate_on` value isn't a set https://github.com/Textualize/textual/pull/4868
- Fix `Input.cursor_blink` having no effect on the blink cycle after mounting https://github.com/Textualize/textual/pull/4869
- Fix command palette key override https://github.com/Textualize/textual/pull/4890

## [0.76.0]

Expand Down
32 changes: 22 additions & 10 deletions src/textual/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,18 +367,13 @@ class MyApp(App[None]):
"""

COMMAND_PALETTE_BINDING: ClassVar[str] = "ctrl+p"
"""The key that launches the command palette (if enabled)."""
"""The key that launches the command palette (if enabled by [`App.ENABLE_COMMAND_PALETTE`][textual.app.App.ENABLE_COMMAND_PALETTE])."""

COMMAND_PALETTE_DISPLAY: ClassVar[str | None] = None
"""How the command palette key should be displayed in the footer (or `None` for default)."""

BINDINGS: ClassVar[list[BindingType]] = [
Binding("ctrl+c", "quit", "Quit", show=False, priority=True),
Binding(
COMMAND_PALETTE_BINDING,
"command_palette",
"palette",
show=False,
priority=True,
tooltip="Open command palette",
),
Binding("ctrl+c", "quit", "Quit", show=False, priority=True)
]
"""The default key bindings."""

Expand Down Expand Up @@ -650,6 +645,23 @@ def __init__(
# Size of previous inline update
self._previous_inline_height: int | None = None

if self.ENABLE_COMMAND_PALETTE:
for _key, binding in self._bindings:
if binding.action in {"command_palette", "app.command_palette"}:
break
else:
self._bindings._add_binding(
Binding(
self.COMMAND_PALETTE_BINDING,
"command_palette",
"palette",
show=False,
key_display=self.COMMAND_PALETTE_DISPLAY,
priority=True,
tooltip="Open command palette",
)
)

def validate_title(self, title: Any) -> str:
"""Make sure the title is set to a string."""
return str(title)
Expand Down
8 changes: 8 additions & 0 deletions src/textual/binding.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ def make_bindings(bindings: Iterable[BindingType]) -> Iterable[Binding]:
for binding in make_bindings(bindings or {}):
self.key_to_bindings.setdefault(binding.key, []).append(binding)

def _add_binding(self, binding: Binding) -> None:
"""Add a new binding.

Args:
binding: New Binding to add.
"""
self.key_to_bindings.setdefault(binding.key, []).append(binding)

def __iter__(self) -> Iterator[tuple[str, Binding]]:
"""Iterating produces a sequence of (KEY, BINDING) tuples."""
return iter(
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions tests/snapshot_tests/snapshot_apps/command_palette_key.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from textual.app import App, ComposeResult
from textual.widgets import Footer


class NewPaletteBindingApp(App):
COMMAND_PALETTE_BINDING = "ctrl+backslash"
COMMAND_PALETTE_DISPLAY = "ctrl+\\"
Copy link
Contributor

@TomJGooding TomJGooding Aug 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps I've misunderstood this new COMMAND_PALETTE_DISPLAY variable , but why does the snapshot show ^\ rather than ctrl+\?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is some work in #4876 that will fix that.

Copy link
Contributor

@TomJGooding TomJGooding Aug 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be worth adding that work in this PR if possible, as currently the snapshot is not really as expected?


def compose(self) -> ComposeResult:
yield Footer()


if __name__ == "__main__":
app = NewPaletteBindingApp()
app.run()
5 changes: 5 additions & 0 deletions tests/snapshot_tests/test_snapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -1438,3 +1438,8 @@ async def run_before(pilot: Pilot):
assert snap_compare(
SNAPSHOT_APPS_DIR / "command_palette_dismiss.py", run_before=run_before
)


def test_command_palette_key_change(snap_compare):
"""Regression test for https://github.com/Textualize/textual/issues/4887"""
assert snap_compare(SNAPSHOT_APPS_DIR / "command_palette_key.py")
Loading