|
16 | 16 | UIMouseDragEvent,
|
17 | 17 | UIMouseEvent,
|
18 | 18 | UIMousePressEvent,
|
| 19 | + UIMouseReleaseEvent, |
19 | 20 | UIMouseScrollEvent,
|
20 | 21 | UIOnChangeEvent,
|
21 | 22 | UIOnClickEvent,
|
@@ -544,7 +545,6 @@ def __init__(
|
544 | 545 | **kwargs,
|
545 | 546 | )
|
546 | 547 |
|
547 |
| - self._active = False |
548 | 548 | self._text_color = Color.from_iterable(text_color)
|
549 | 549 |
|
550 | 550 | self.doc: AbstractDocument = pyglet.text.decode_text(text)
|
@@ -574,10 +574,17 @@ def __init__(
|
574 | 574 | bind(self, "pressed", self._apply_style)
|
575 | 575 | bind(self, "invalid", self._apply_style)
|
576 | 576 | bind(self, "disabled", self._apply_style)
|
| 577 | + bind(self, "_active", self._on_active_changed) |
577 | 578 |
|
578 | 579 | # initial style application
|
579 | 580 | self._apply_style()
|
580 | 581 |
|
| 582 | + def _on_active_changed(self): |
| 583 | + """Handle the active state change of the input |
| 584 | + text field to care about loosing active state.""" |
| 585 | + if not self._active: |
| 586 | + self.deactivate() |
| 587 | + |
581 | 588 | def _apply_style(self):
|
582 | 589 | style = self.get_current_style()
|
583 | 590 |
|
@@ -630,12 +637,25 @@ def on_event(self, event: UIEvent) -> bool | None:
|
630 | 637 |
|
631 | 638 | Text input is only active when the user clicks on the input field."""
|
632 | 639 | # If active check to deactivate
|
633 |
| - if self._active and isinstance(event, UIMousePressEvent): |
634 |
| - if self.rect.point_in_rect(event.pos): |
635 |
| - x = int(event.x - self.left - self.LAYOUT_OFFSET) |
636 |
| - y = int(event.y - self.bottom) |
637 |
| - self.caret.on_mouse_press(x, y, event.button, event.modifiers) |
638 |
| - else: |
| 640 | + if self._active and isinstance(event, UIMouseEvent): |
| 641 | + event_in_rect = self.rect.point_in_rect(event.pos) |
| 642 | + |
| 643 | + # mouse press |
| 644 | + if isinstance(event, UIMousePressEvent): |
| 645 | + # inside the input field |
| 646 | + if event_in_rect: |
| 647 | + x = int(event.x - self.left - self.LAYOUT_OFFSET) |
| 648 | + y = int(event.y - self.bottom) |
| 649 | + self.caret.on_mouse_press(x, y, event.button, event.modifiers) |
| 650 | + else: |
| 651 | + # outside the input field |
| 652 | + self.deactivate() |
| 653 | + # return unhandled to allow other widgets to activate |
| 654 | + return EVENT_UNHANDLED |
| 655 | + |
| 656 | + # mouse release outside the input field, |
| 657 | + # which could be a click on another widget, which handles the press event |
| 658 | + if isinstance(event, UIMouseReleaseEvent) and not event_in_rect: |
639 | 659 | self.deactivate()
|
640 | 660 | # return unhandled to allow other widgets to activate
|
641 | 661 | return EVENT_UNHANDLED
|
@@ -683,18 +703,20 @@ def activate(self):
|
683 | 703 | if self._active:
|
684 | 704 | return
|
685 | 705 |
|
686 |
| - self._active = True |
| 706 | + self._grap_active() # will set _active to True |
687 | 707 | self.trigger_full_render()
|
688 | 708 | self.caret.on_activate()
|
689 | 709 | self.caret.position = len(self.doc.text)
|
690 | 710 |
|
691 | 711 | def deactivate(self):
|
692 | 712 | """Programmatically deactivate the text input field."""
|
693 | 713 |
|
694 |
| - if not self._active: |
695 |
| - return |
| 714 | + if self._active: |
| 715 | + print("Release active text input field") |
| 716 | + self._release_active() # will set _active to False |
| 717 | + else: |
| 718 | + print("Text input field is not active, cannot deactivate") |
696 | 719 |
|
697 |
| - self._active = False |
698 | 720 | self.trigger_full_render()
|
699 | 721 | self.caret.on_deactivate()
|
700 | 722 |
|
|
0 commit comments