code in events

This commit is contained in:
Will McGugan
2022-09-26 10:41:36 +01:00
parent 3f0955cbe5
commit 28d395f774
26 changed files with 126 additions and 17 deletions

View File

@@ -8,3 +8,7 @@ The `Blur` event is sent to a widget when it loses focus.
## Attributes ## Attributes
_No other attributes_ _No other attributes_
## Code
::: textual.events.Blur

View File

@@ -19,3 +19,7 @@ The `Click` event is sent to a widget when the user clicks a mouse button.
| `ctrl` | bool | Ctrl key pressed if True | | `ctrl` | bool | Ctrl key pressed if True |
| `screen_x` | int | Mouse x coordinate relative to the screen | | `screen_x` | int | Mouse x coordinate relative to the screen |
| `screen_y` | int | Mouse y coordinate relative to the screen | | `screen_y` | int | Mouse y coordinate relative to the screen |
## Code
::: textual.events.Click

View File

@@ -8,3 +8,7 @@ The `DescendantBlur` event is sent to a widget when one of its children loses fo
## Attributes ## Attributes
_No other attributes_ _No other attributes_
## Code
::: textual.events.DescendantBlur

View File

@@ -8,3 +8,7 @@ The `DescendantFocus` event is sent to a widget when one of its descendants rece
## Attributes ## Attributes
_No other attributes_ _No other attributes_
## Code
::: textual.events.DescendantFocus

View File

@@ -8,3 +8,7 @@ The `Enter` event is sent to a widget when the mouse pointer first moves over a
## Attributes ## Attributes
_No other attributes_ _No other attributes_
## Code
::: textual.events.Enter

View File

@@ -8,3 +8,7 @@ The `Focus` event is sent to a widget when it receives input focus.
## Attributes ## Attributes
_No other attributes_ _No other attributes_
## Code
::: textual.events.Focus

View File

@@ -8,3 +8,7 @@ The `Hide` event is sent to a widget when it is hidden from view.
## Attributes ## Attributes
_No additional attributes_ _No additional attributes_
## Code
::: textual.events.Hide

View File

@@ -11,3 +11,7 @@ The `Key` event is sent to a widget when the user presses a key on the keyboard.
| --------- | ----------- | ----------------------------------------------------------- | | --------- | ----------- | ----------------------------------------------------------- |
| `key` | str | Name of the key that was pressed. | | `key` | str | Name of the key that was pressed. |
| `char` | str or None | The character that was pressed, or None it isn't printable. | | `char` | str or None | The character that was pressed, or None it isn't printable. |
## Code
::: textual.events.Key

View File

@@ -8,3 +8,7 @@ The `Leave` event is sent to a widget when the mouse pointer moves off a widget.
## Attributes ## Attributes
_No other attributes_ _No other attributes_
## Code
::: textual.events.Leave

View File

@@ -10,3 +10,7 @@ The load event is typically used to do any setup actions required by the app tha
## Attributes ## Attributes
_No additional attributes_ _No additional attributes_
## Code
::: textual.events.Load

View File

@@ -10,3 +10,7 @@ The mount event is typically used to set the initial state of a widget or to add
## Attributes ## Attributes
_No additional attributes_ _No additional attributes_
## Code
::: textual.events.Mount

View File

@@ -10,3 +10,7 @@ The `MouseCapture` event is sent to a widget when it is capturing mouse events f
| attribute | type | purpose | | attribute | type | purpose |
| ---------------- | ------ | --------------------------------------------- | | ---------------- | ------ | --------------------------------------------- |
| `mouse_position` | Offset | Mouse coordinates when the mouse was captured | | `mouse_position` | Offset | Mouse coordinates when the mouse was captured |
## Code
::: textual.events.MouseCapture

View File

@@ -19,3 +19,7 @@ The `MouseDown` event is sent to a widget when a mouse button is pressed.
| `ctrl` | bool | Ctrl key pressed if True | | `ctrl` | bool | Ctrl key pressed if True |
| `screen_x` | int | Mouse x coordinate relative to the screen | | `screen_x` | int | Mouse x coordinate relative to the screen |
| `screen_y` | int | Mouse y coordinate relative to the screen | | `screen_y` | int | Mouse y coordinate relative to the screen |
## Code
::: textual.events.MouseDown

View File

@@ -19,3 +19,7 @@ The `MouseMove` event is sent to a widget when the mouse pointer is moved over a
| `ctrl` | bool | Ctrl key pressed if True | | `ctrl` | bool | Ctrl key pressed if True |
| `screen_x` | int | Mouse x coordinate relative to the screen | | `screen_x` | int | Mouse x coordinate relative to the screen |
| `screen_y` | int | Mouse y coordinate relative to the screen | | `screen_y` | int | Mouse y coordinate relative to the screen |
## Code
::: textual.events.MouseMove

View File

@@ -8,5 +8,9 @@ The `MouseRelease` event is sent to a widget when it is no longer receiving mous
## Attributes ## Attributes
| attribute | type | purpose | | attribute | type | purpose |
| ---------------- | ------ | -------------------------------------------- | | ---------------- | ------ | --------------------------------------------- |
| `mouse_position` | Offset | Mouse coordinates when the mouse was released | | `mouse_position` | Offset | Mouse coordinates when the mouse was released |
## Code
::: textual.events.MouseRelease

View File

@@ -8,6 +8,10 @@ The `MouseScrollDown` event is sent to a widget when the scroll wheel (or trackp
## Attributes ## Attributes
| attribute | type | purpose | | attribute | type | purpose |
|-----------|------|----------------------------------------| | --------- | ---- | -------------------------------------- |
| `x` | int | Mouse x coordinate, relative to Widget | | `x` | int | Mouse x coordinate, relative to Widget |
| `y` | int | Mouse y coordinate, relative to Widget | | `y` | int | Mouse y coordinate, relative to Widget |
## Code
::: textual.events.MouseScrollDown

View File

@@ -11,3 +11,7 @@ The `MouseScrollUp` event is sent to a widget when the scroll wheel (or trackpad
| --------- | ---- | -------------------------------------- | | --------- | ---- | -------------------------------------- |
| `x` | int | Mouse x coordinate, relative to Widget | | `x` | int | Mouse x coordinate, relative to Widget |
| `y` | int | Mouse y coordinate, relative to Widget | | `y` | int | Mouse y coordinate, relative to Widget |
## Code
::: textual.events.MouseScrollUp

View File

@@ -19,3 +19,7 @@ The `MouseUp` event is sent to a widget when the user releases a mouse button.
| `ctrl` | bool | Ctrl key pressed if True | | `ctrl` | bool | Ctrl key pressed if True |
| `screen_x` | int | Mouse x coordinate relative to the screen | | `screen_x` | int | Mouse x coordinate relative to the screen |
| `screen_y` | int | Mouse y coordinate relative to the screen | | `screen_y` | int | Mouse y coordinate relative to the screen |
## Code
::: textual.events.MouseUp

View File

@@ -10,3 +10,7 @@ The `Paste` event is sent to a widget when the user pastes text.
| attribute | type | purpose | | attribute | type | purpose |
| --------- | ---- | ------------------------ | | --------- | ---- | ------------------------ |
| `text` | str | The text that was pasted | | `text` | str | The text that was pasted |
## Code
::: textual.events.Paste

View File

@@ -8,7 +8,11 @@ The `Resize` event is sent to a widget when its size changes and when it is firs
## Attributes ## Attributes
| attribute | type | purpose | | attribute | type | purpose |
| ---------------- | ---- | ------------------------------------------------- | | ---------------- | ---- | ------------------------------------------------ |
| `size` | Size | The new size of the Widget | | `size` | Size | The new size of the Widget |
| `virtual_size` | Size | The virtual size (scrollable area) of the Widget | | `virtual_size` | Size | The virtual size (scrollable area) of the Widget |
| `container_size` | Size | The size of the container (parent widget) | | `container_size` | Size | The size of the container (parent widget) |
## Code
::: textual.events.Resize

View File

@@ -8,3 +8,7 @@ The `ScreenResume` event is sent to a **Screen** when it becomes current.
## Attributes ## Attributes
_No other attributes_ _No other attributes_
## Code
::: textual.events.ScreenResume

View File

@@ -8,3 +8,7 @@ The `ScreenSuspend` event is sent to a **Screen** when it is replaced by another
## Attributes ## Attributes
_No other attributes_ _No other attributes_
## Code
::: textual.events.ScreenSuspend

View File

@@ -8,3 +8,7 @@ The `Show` event is sent to a widget when it becomes visible.
## Attributes ## Attributes
_No additional attributes_ _No additional attributes_
## Code
::: textual.events.Show

View File

@@ -1,6 +1,5 @@
from textual.app import App, ComposeResult
from textual import events from textual import events
from textual.app import App, ComposeResult
from textual.layout import Container from textual.layout import Container
from textual.widgets import Static, TextLog from textual.widgets import Static, TextLog

View File

@@ -29,15 +29,15 @@ Note the key event handler on the app which logs all key events. if you press an
There are two main attributes on a key event. The `key` attribute is the _name_ of the key which may be a single character, or a longer identifier. Textual insures that the `key` attribute could always be used in a method name. There are two main attributes on a key event. The `key` attribute is the _name_ of the key which may be a single character, or a longer identifier. Textual insures that the `key` attribute could always be used in a method name.
Key events also contain a `char` attribute which contains a printable character. Key events also contain a `char` attribute which contains single character if it is printable, or ``None`` if it is not printable (like a function key which has no corresponding character).
To illustrate the difference, try `key01.py` with the space key. You should see something like the following: To illustrate the difference between `key` ad `char`, try `key01.py` with the space key. You should see something like the following:
```{.textual path="docs/examples/guide/input/key01.py", press="space,_"} ```{.textual path="docs/examples/guide/input/key01.py", press="space,_"}
``` ```
The `key` attribute contains the word "space" while the `char` attribute contains a literal space. Note that he `key` attribute contains the word "space" while the `char` attribute contains a literal space.
### Key methods ### Key methods
@@ -53,7 +53,7 @@ Note the addition of a `key_space` method which is called in response to the spa
!!! note !!! note
Consider key methods to be a convenience for experimenting with Textual features. In nearly all cases, key [bindings](#bindings) and actions are referable. Consider key methods to be a convenience for experimenting with Textual features. In nearly all cases, key [bindings](#bindings) and [actions](../guide/actions.md) are preferable.
## Input focus ## Input focus
@@ -84,7 +84,7 @@ The app splits the screen in to quarters, with a TextLog widget in each quarter.
the `:focus` CSS pseudo-selector can be used to apply a style to the focused widget. the `:focus` CSS pseudo-selector can be used to apply a style to the focused widget.
You can also move focus by pressing the ++tab++ key which moves the focus to the next widget. Pressing ++shift+tab++ moves the focus in the opposite direction. You can move focus by pressing the ++tab++ key to focus the next widget. Pressing ++shift+tab++ moves the focus in the opposite direction.
### Controlling focus ### Controlling focus
@@ -119,7 +119,7 @@ The following example binds the keys ++r++, ++g++, and ++b++ to an action which
```{.textual path="docs/examples/guide/input/binding01.py", press="r,g,b,b"} ```{.textual path="docs/examples/guide/input/binding01.py", press="r,g,b,b"}
``` ```
Note how the footer displays bindings and makes them clickable, as an alternative to pressing the key. Note how the footer displays bindings and makes them clickable.
### Binding class ### Binding class
@@ -156,7 +156,7 @@ The following example shows mouse movements being used to _attach_ a widget to t
=== "mouse01.py" === "mouse01.py"
```python title="mouse01.py" hl_lines="12-14" ```python title="mouse01.py" hl_lines="11-13"
--8<-- "docs/examples/guide/input/mouse01.py" --8<-- "docs/examples/guide/input/mouse01.py"
``` ```
@@ -182,6 +182,10 @@ Call [release_mouse][textual.widget.Widget.release_mouse] to restore the default
Textual will send a [MouseCapture](../events/mouse_capture.md) event when the mouse is captured, and a [MouseRelease](../events/mouse_release.md) event when it is released. Textual will send a [MouseCapture](../events/mouse_capture.md) event when the mouse is captured, and a [MouseRelease](../events/mouse_release.md) event when it is released.
### Enter and Leave events
Textual will send a [Enter](../events/enter.md) event to a widget when the mouse cursor first moves over it, and a [Leave](../events/leave) event when the cursor moves off a widget.
### Click events ### Click events
There are three events associated with clicking a button on your mouse. When the button is initially pressed, Textual sends a [MouseDown](../events/mouse_down.md) event, followed by [MouseUp](../events/mouse_up.md) when the button is released. Textual then sends a final [Click](../events/mouse_click.md) event. There are three events associated with clicking a button on your mouse. When the button is initially pressed, Textual sends a [MouseDown](../events/mouse_down.md) event, followed by [MouseUp](../events/mouse_up.md) when the button is released. Textual then sends a final [Click](../events/mouse_click.md) event.

View File

@@ -318,17 +318,31 @@ class MouseEvent(InputEvent, bubble=True):
@property @property
def offset(self) -> Offset: def offset(self) -> Offset:
"""The mouse coordinate as an offset.""" """The mouse coordinate as an offset.
Returns:
Offset: Mouse coordinate.
"""
return Offset(self.x, self.y) return Offset(self.x, self.y)
@property @property
def screen_offset(self) -> Offset: def screen_offset(self) -> Offset:
"""Mouse coordinate relative to the screen.""" """Mouse coordinate relative to the screen.
Returns:
Offset: Mouse coordinate.
"""
return Offset(self.screen_x, self.screen_y) return Offset(self.screen_x, self.screen_y)
@property @property
def delta(self) -> Offset: def delta(self) -> Offset:
"""Mouse coordinate delta (change since last event).""" """Mouse coordinate delta (change since last event).
Returns:
Offset: Mouse coordinate.
"""
return Offset(self.delta_x, self.delta_y) return Offset(self.delta_x, self.delta_y)
@property @property