-
I'm looking for a preferred way to implement focus travelsal in Markdown widget so that:
In my code below I accomplished kind of the desired behaviour, but there are two questions:
markdown.py from textual.app import App, ComposeResult
from textual.widgets import Markdown
from textual.containers import Horizontal
from textual import events
EXAMPLE_MARKDOWN = """\
# Markdown Document
This is an example of Textual's `Markdown` widget.
## Features
Markdown syntax and extensions are supported.
- Typography *emphasis*, **strong**, `inline code` etc.
- Headers
- Lists (bullet and ordered)
- Syntax highlighted code blocks
- Tables!
"""
class MyMarkdown(Markdown):
can_focus = True
def on_key(self, event: events.Key) -> None:
if event.key == "enter":
# Enable "children" focus mode
self.can_focus_children = True
for child in self.children:
child.can_focus = True
self.children[0].focus()
elif event.key == "escape":
# Restore "normal" focus mode
self.can_focus_children = False
for child in self.children:
child.can_focus = False
self.focus()
class MarkdownExampleApp(App):
CSS_PATH = "markdown.tcss"
def compose(self) -> ComposeResult:
with Horizontal():
yield MyMarkdown(EXAMPLE_MARKDOWN)
yield MyMarkdown(EXAMPLE_MARKDOWN)
if __name__ == "__main__":
app = MarkdownExampleApp()
app.run() markdown.tcss Markdown {
width: 50%;
margin: 1 1 1 1;
}
Markdown:focus {
background: $accent;
}
MarkdownH1:focus {
background: $accent;
}
MarkdownParagraph:focus {
background: $accent;
}
MarkdownH2:focus {
background: $accent;
}
MarkdownBulletList:focus {
background: $accent;
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 8 replies
-
I'd suggest using Bindings rather than handling the key events. The problem is that Tab is a priority binding in Textual to focus the next widget on the screen. You would need to override this by adding a new priority binding in your custom widget. Perhaps something like this: from textual.app import App, ComposeResult
from textual.binding import Binding
from textual.containers import Horizontal
from textual.widgets import Markdown
EXAMPLE_MARKDOWN = """\
# Markdown Document
This is an example of Textual's `Markdown` widget.
## Features
Markdown syntax and extensions are supported.
- Typography *emphasis*, **strong**, `inline code` etc.
- Headers
- Lists (bullet and ordered)
- Syntax highlighted code blocks
- Tables!
"""
class MyMarkdown(Markdown, can_focus=True, can_focus_children=False):
BINDINGS = [
Binding("enter", "enable_focus_children"),
Binding("escape", "disable_focus_children"),
Binding("tab", "focus_next", priority=True),
]
def action_enable_focus_children(self) -> None:
self.can_focus_children = True
for child in self.children:
child.can_focus = True
self.children[0].focus()
def action_disable_focus_children(self) -> None:
self.can_focus_children = False
for child in self.children:
child.can_focus = False
self.focus()
def action_focus_next(self) -> None:
if self.can_focus_children:
self.screen.focus_next("MyMarkdown *")
else:
self.screen.focus_next()
class MarkdownExampleApp(App):
CSS_PATH = "markdown.tcss"
def compose(self) -> ComposeResult:
with Horizontal():
yield MyMarkdown(EXAMPLE_MARKDOWN)
yield MyMarkdown(EXAMPLE_MARKDOWN)
if __name__ == "__main__":
app = MarkdownExampleApp()
app.run() |
Beta Was this translation helpful? Give feedback.
-
Yes, that's neat! When having also code block in markdown content focus-metaclass doesn't hightlight MarkdownFence widget. If I change focus-metaclass to use some sort of border then focus effect is visible but that makes markdown child widgets jump around a bit once focused. Any idea how to apply $accent background also to widget that displays the code part so that focus travelsal within markdown widget is consisten? (Code attach as a file and renamed to .md as I couldn't figure out how to post markdown in markdown in github issue) |
Beta Was this translation helpful? Give feedback.
Yes, you're totally right. That was my mistake missing that binding. So for now the code is the following.
How would the final iteration look like utilizing the fix for Textualize/rich#3295?