beeware / toga

A Python native, OS native GUI toolkit.
https://toga.readthedocs.io/en/latest/
BSD 3-Clause "New" or "Revised" License
4.36k stars 673 forks source link

MultiLineTextInput widget persistently takes on rich formatting on a paste operation #2957

Open kcoombs opened 5 hours ago

kcoombs commented 5 hours ago

Describe the bug

When pasting rich-formatted text into a MultiLineTextInput widget, the widget takes on the rich formatting.

Steps to reproduce

  1. Launch a Toga application that includes a MultiLineTextInput widget. e.g., from the Toga repository, examples/multilinetextinput.
  2. Copy rich-formatted text from another application, e.g., MS Word, TextEdit, a web browser.
  3. Paste into the MultiLineTextInput widget.
  4. Rich-formatting is preserved in the MultiLineTextInput widget. The formatting persists even when clearing the value (e.g., "clear" button in the Toga multilinetextinput example).

Expected behavior

While rich formatting may be desirable in some situations, I'd expect the default behavior to be displaying only plain text, like a TextInput widget.

Screenshots

Before paste:

Screenshot 2024-11-15 at 18 19 16

Rich text (TextEdit):

Screenshot 2024-11-15 at 18 21 04

After paste:

Screenshot 2024-11-15 at 18 21 22

After clear:

Screenshot 2024-11-15 at 18 21 29

Environment

Logs

Additional context

No response

freakboy3742 commented 5 hours ago

Confirming this definitely isn't the intended behavior. I'm guessing we've missed an option when configuring the native text widgets.

Not sure if this also affects GTK, iOS or Android; seems highly plausible it might be an issue on GTK.

kcoombs commented 1 hour ago

As least so far as MacOS goes, this seems to prevent MultiLineTextInput from taking rich-formatted text on a paste:

diff --git a/cocoa/src/toga_cocoa/widgets/multilinetextinput.py b/cocoa/src/toga_cocoa/widgets/multilinetextinput.py
index c651fa8e4..e908ecb6d 100644
--- a/cocoa/src/toga_cocoa/widgets/multilinetextinput.py
+++ b/cocoa/src/toga_cocoa/widgets/multilinetextinput.py
@@ -36,6 +36,7 @@ class MultilineTextInput(Widget):

         # Create the actual text widget
         self.native_text = TogaTextView.alloc().init()
+        self.native_text.setRichText_(False)
         self.native_text.interface = self.interface
         self.native_text.delegate = self.native_text

See https://developer.apple.com/documentation/appkit/nstextview/1449538-richtext.