Is there a way to specify a target widget in a binding tuple? #2207
-
I searched past issues and didn't find anything that would allow this. The idea is that you could include a selector string in the binding tuple that would allow you to define bindings for widgets that are not stored as properties of the App object. One way this could work is by including a css selector in the binding tuple. If it was included in the "action" element of the binding tuple:
would be equivalent to:
I think this could be implemented by changing line 2068 in src/textual/app.py from
to
Another option would be the inclusion of an optional fourth element of the binding tuple containing the css selector. I don't know if some other changes would be necessary. I apologize if there is some other intended method of achieving the same results. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
As you've already identified, no, there is no support for that in the binding facility as it stands. I suppose my first question is: what exactly is it you're wanting to do that would require such support? In the example you've given above it looks like you want to add vi-like navigation to Normally, if I want to extend the bindings for a particular widget I inherit from it and add my own bindings. By way of illustration, someone asked a wee back how they could do similar for the from pathlib import Path
from textual.app import App, ComposeResult
from textual.widgets import Header, Footer, MarkdownViewer
from textual.binding import Binding
class ViLikeMarkdownViewer( MarkdownViewer ):
BINDINGS = [
Binding( "j", "up", "", show=False ),
Binding( "k", "down", "", show=False ),
]
def action_up( self ) -> None:
self.scroll_up()
def action_down( self ) -> None:
self.scroll_down()
class ViLikeMarkdownApp( App[ None ] ):
def compose( self ) -> ComposeResult:
yield Header()
yield ViLikeMarkdownViewer( Path( "../textual/README.md" ).read_text() )
yield Footer()
if __name__ == "__main__":
ViLikeMarkdownApp().run() So you should be able to make a Longer-term, on our roadmap, we do have a plan to make bindings more configurable such that different binding "themes" could be used. |
Beta Was this translation helpful? Give feedback.
As you've already identified, no, there is no support for that in the binding facility as it stands. I suppose my first question is: what exactly is it you're wanting to do that would require such support? In the example you've given above it looks like you want to add vi-like navigation to
TextLog
, is that the case?Normally, if I want to extend the bindings for a particular widget I inherit from it and add my own bindings. By way of illustration, someone asked a wee back how they could do similar for the
MarkdownViewer
and I gave this as an example: