Skip to content

fix(help panel): prevent duplicated key displays #5066

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

Conversation

TomJGooding
Copy link
Contributor

Fixes #5037

Please review the following checklist.

  • Docstrings on all new or modified functions / classes
  • Updated documentation
  • Updated CHANGELOG.md (where appropriate)

@willmcgugan
Copy link
Member

I don't think this will de-duplicate the key displays, except where we have identical keys to the same action.

i.e something like this:

BINDINGS = [
    ("foo", "do_foo"),
    ("foo", "do_foo"),
    ("bar", "do_foo")
]

Should display foo bar for the key display.

@TomJGooding
Copy link
Contributor Author

Sorry @willmcgugan, I'm not sure I understand what you mean.

Currently if you define a binding with a comma-separated list of keys along with a key_display, the help panel will duplicate that key display for each key as described in the linked issue #5037.

I don't see any issues with duplicated keys with your example bindings above, but perhaps I'm missing something?

@willmcgugan
Copy link
Member

Consider the following:

from textual.app import App, ComposeResult

from textual.binding import Binding
from textual.widgets import Footer


class HelpPanelApp(App):
    BINDINGS = [
        Binding("a", "bell", "Ring the bell", key_display="bar"),
        Binding("b,e,l", "bell", "Ring the bell", key_display="foo"),
    ]

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


HelpPanelApp().run()

This will only display a single key, when there are multiple that would apply.

A contrived example perhaps, but even more likely now we have keymaps.

@TomJGooding
Copy link
Contributor Author

Thanks Will for clarifying. I see your point about this edge case, but why would you define multiple bindings like this? The key panel also wouldn't account for these bindings having different descriptions, so why should the key display be different?

@TomJGooding
Copy link
Contributor Author

TomJGooding commented Oct 11, 2024

I think I now understand what you meant by this not de-duping the key displays, now that support for keymaps has been released.

Hopefully fixed by 9aa73f0?

from textual.app import App, ComposeResult
from textual.binding import Binding
from textual.widgets import Footer, Label


class MyApp(App[None]):
    BINDINGS = [
        Binding(
            key="i,up",
            action="increment",
            description="Increment",
            id="app.increment",
            key_display="INCORRECT",
        ),
    ]

    def compose(self) -> ComposeResult:
        yield Label("Check the footer and help panel")
        yield Footer()

    def on_mount(self) -> None:
        self.action_show_help_panel()
        self.set_keymap({"app.increment": "k,plus,j,l"})

    def get_key_display(self, binding: Binding) -> str:
        if binding.id == "app.increment":
            return "correct"
        return super().get_key_display(binding)


if __name__ == "__main__":
    app = MyApp()
    app.run()

@TomJGooding
Copy link
Contributor Author

@willmcgugan Friendly ping as currently the duplication of key displays is a bit of a wart in the help panel.

The updated test_keymap_bindings_key_display snapshot shows the difference after this fix. But please let me know if I might have still missed something.

Copy link
Member

@willmcgugan willmcgugan left a comment

Choose a reason for hiding this comment

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

Thanks. Just a suggestion, which I'm happy to leave as "your call"!

@darrenburns darrenburns merged commit a3fee68 into Textualize:main Nov 7, 2024
20 checks passed
@TomJGooding TomJGooding deleted the fix-help-panel-prevent-duplicated-key-displays branch November 7, 2024 10:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Binding.key_display and help panel interact poorly for comma-separated Binding definitions
3 participants