Skip to content

Commit

Permalink
Fixing IME alignment for Input widget. TextArea remains unfixed.
Browse files Browse the repository at this point in the history
  • Loading branch information
darrenburns committed Sep 26, 2023
1 parent d766bb9 commit d727627
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/textual/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import rich.repr
from rich import terminal_theme
from rich.console import Console, RenderableType
from rich.control import Control
from rich.protocol import is_renderable
from rich.segment import Segment, Segments
from rich.traceback import Traceback
Expand Down Expand Up @@ -417,6 +418,7 @@ def __init__(
self._animator = Animator(self)
self._animate = self._animator.bind(self)
self.mouse_position = Offset(0, 0)
self.cursor_position = Offset(0, 0)

self._exception: Exception | None = None
"""The unhandled exception which is leading to the app shutting down,
Expand Down Expand Up @@ -2423,7 +2425,11 @@ def _display(self, screen: Screen, renderable: RenderableType | None) -> None:
try:
try:
if isinstance(renderable, CompositorUpdate):
cursor_x, cursor_y = self.cursor_position
terminal_sequence = renderable.render_segments(console)
terminal_sequence += Control.move_to(
cursor_x, cursor_y
).segment.text
else:
segments = console.render(renderable)
terminal_sequence = console._render_buffer(segments)
Expand All @@ -2433,7 +2439,9 @@ def _display(self, screen: Screen, renderable: RenderableType | None) -> None:
self._driver.write(terminal_sequence)
finally:
self._end_update()

self._driver.flush()

finally:
self.post_display_hook()

Expand Down
2 changes: 1 addition & 1 deletion src/textual/screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ def _reset_focus(
chosen = candidate
break

# Go with the what was found.
# Go with what was found.
self.set_focus(chosen)

def _update_focus_styles(
Expand Down
12 changes: 11 additions & 1 deletion src/textual/widgets/_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from .._segment_tools import line_crop
from ..binding import Binding, BindingType
from ..events import Blur, Focus, Mount
from ..geometry import Size
from ..geometry import Offset, Size
from ..message import Message
from ..reactive import reactive
from ..suggester import Suggester, SuggestionReady
Expand Down Expand Up @@ -254,6 +254,7 @@ def __init__(
super().__init__(name=name, id=id, classes=classes, disabled=disabled)
if value is not None:
self.value = value

self.placeholder = placeholder
self.highlighter = highlighter
self.password = password
Expand Down Expand Up @@ -327,6 +328,14 @@ def _watch_cursor_position(self) -> None:
else:
self.view_position = self.view_position

self.app.cursor_position = self.cursor_screen_offset

@property
def cursor_screen_offset(self) -> Offset:
"""The offset of the cursor of this input in screen-space. (x, y)/(column, row)"""
x, y, _width, _height = self.content_region
return Offset(x + self._cursor_offset - self.view_position, y)

async def _watch_value(self, value: str) -> None:
self._suggestion = ""
if self.suggester and value:
Expand Down Expand Up @@ -425,6 +434,7 @@ def _on_focus(self, _: Focus) -> None:
self.cursor_position = len(self.value)
if self.cursor_blink:
self.blink_timer.resume()
self.app.cursor_position = self.cursor_screen_offset

async def _on_key(self, event: events.Key) -> None:
self._cursor_visible = True
Expand Down

0 comments on commit d727627

Please sign in to comment.