summaryrefslogtreecommitdiffstats
path: root/examples/full-screen/simple-demos/focus.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-15 16:35:31 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-15 16:35:31 +0000
commit4f1a3b5f9ad05aa7b08715d48909a2b06ee2fcb1 (patch)
treee5dee7be2f0d963da4faad6517278d03783e3adc /examples/full-screen/simple-demos/focus.py
parentInitial commit. (diff)
downloadprompt-toolkit-4f1a3b5f9ad05aa7b08715d48909a2b06ee2fcb1.tar.xz
prompt-toolkit-4f1a3b5f9ad05aa7b08715d48909a2b06ee2fcb1.zip
Adding upstream version 3.0.43.upstream/3.0.43
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'examples/full-screen/simple-demos/focus.py')
-rwxr-xr-xexamples/full-screen/simple-demos/focus.py98
1 files changed, 98 insertions, 0 deletions
diff --git a/examples/full-screen/simple-demos/focus.py b/examples/full-screen/simple-demos/focus.py
new file mode 100755
index 0000000..9fe9b8f
--- /dev/null
+++ b/examples/full-screen/simple-demos/focus.py
@@ -0,0 +1,98 @@
+#!/usr/bin/env python
+"""
+Demonstration of how to programmatically focus a certain widget.
+"""
+from prompt_toolkit.application import Application
+from prompt_toolkit.buffer import Buffer
+from prompt_toolkit.document import Document
+from prompt_toolkit.key_binding import KeyBindings
+from prompt_toolkit.layout.containers import HSplit, VSplit, Window
+from prompt_toolkit.layout.controls import BufferControl, FormattedTextControl
+from prompt_toolkit.layout.layout import Layout
+
+# 1. The layout
+top_text = (
+ "Focus example.\n"
+ "[q] Quit [a] Focus left top [b] Right top [c] Left bottom [d] Right bottom."
+)
+
+LIPSUM = """Lorem ipsum dolor sit amet, consectetur adipiscing elit.
+Maecenas quis interdum enim. Nam viverra, mauris et blandit malesuada, ante est
+bibendum mauris, ac dignissim dui tellus quis ligula. Aenean condimentum leo at
+dignissim placerat. In vel dictum ex, vulputate accumsan mi. Donec ut quam
+placerat massa tempor elementum. Sed tristique mauris ac suscipit euismod. Ut
+tempus vehicula augue non venenatis. Mauris aliquam velit turpis, nec congue
+risus aliquam sit amet. Pellentesque blandit scelerisque felis, faucibus
+consequat ante. Curabitur tempor tortor a imperdiet tincidunt. Nam sed justo
+sit amet odio bibendum congue. Quisque varius ligula nec ligula gravida, sed
+convallis augue faucibus. Nunc ornare pharetra bibendum. Praesent blandit ex
+quis sodales maximus. """
+
+
+left_top = Window(BufferControl(Buffer(document=Document(LIPSUM))))
+left_bottom = Window(BufferControl(Buffer(document=Document(LIPSUM))))
+right_top = Window(BufferControl(Buffer(document=Document(LIPSUM))))
+right_bottom = Window(BufferControl(Buffer(document=Document(LIPSUM))))
+
+
+body = HSplit(
+ [
+ Window(FormattedTextControl(top_text), height=2, style="reverse"),
+ Window(height=1, char="-"), # Horizontal line in the middle.
+ VSplit([left_top, Window(width=1, char="|"), right_top]),
+ Window(height=1, char="-"), # Horizontal line in the middle.
+ VSplit([left_bottom, Window(width=1, char="|"), right_bottom]),
+ ]
+)
+
+
+# 2. Key bindings
+kb = KeyBindings()
+
+
+@kb.add("q")
+def _(event):
+ "Quit application."
+ event.app.exit()
+
+
+@kb.add("a")
+def _(event):
+ event.app.layout.focus(left_top)
+
+
+@kb.add("b")
+def _(event):
+ event.app.layout.focus(right_top)
+
+
+@kb.add("c")
+def _(event):
+ event.app.layout.focus(left_bottom)
+
+
+@kb.add("d")
+def _(event):
+ event.app.layout.focus(right_bottom)
+
+
+@kb.add("tab")
+def _(event):
+ event.app.layout.focus_next()
+
+
+@kb.add("s-tab")
+def _(event):
+ event.app.layout.focus_previous()
+
+
+# 3. The `Application`
+application = Application(layout=Layout(body), key_bindings=kb, full_screen=True)
+
+
+def run():
+ application.run()
+
+
+if __name__ == "__main__":
+ run()