Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Intercepting Paste Events #1177

Closed
Alch-Emi opened this issue Dec 31, 2021 · 4 comments · Fixed by #1350
Closed

Intercepting Paste Events #1177

Alch-Emi opened this issue Dec 31, 2021 · 4 comments · Fixed by #1350
Labels
feature New feature or request question Further information is requested

Comments

@Alch-Emi
Copy link
Contributor

I'm trying to write an app which includes custom behavior for pasting, and especially multi-line pasting. It's fairly easy to listen for a Ctr+V event, check if a relevant input is selected, grab the clipboard, and then run the custom behavior.

However, by the time the message reaches my code, the UI will have already filled the widget with the text clipboard. I can work around this by just guessing the length of the pasted text (i think it's just the text length - the # of newlines) and truncating the text in the UI, but this feels a little messy.

It would be nice to be able to intercept a paste event, maybe by setting a property on the TextInput component that produces a Message instead of pasting text, or just disabling pasting into the TextInput.

@brightly-salty
Copy link

Just a possible solution, not a permanent one: you could copy the code of this file and remove the linked lines to create a widget which doesn't handle pasting, or just insert your custom logic into that area

keyboard::KeyCode::V => {
if self.state.keyboard_modifiers.command() {
let content = match self.state.is_pasting.take() {
Some(content) => content,
None => {
let content: String = clipboard
.read()
.unwrap_or(String::new())
.chars()
.filter(|c| !c.is_control())
.collect();
Value::new(&content)
}
};
let mut editor = Editor::new(
&mut self.value,
&mut self.state.cursor,
);
editor.paste(content.clone());
let message = (self.on_change)(editor.contents());
shell.publish(message);
self.state.is_pasting = Some(content);
} else {
self.state.is_pasting = None;
}
}

@Alch-Emi
Copy link
Contributor Author

Alch-Emi commented Dec 31, 2021 via email

@hecrj
Copy link
Member

hecrj commented Jan 3, 2022

We could extend the TextInput widget with additional methods to override paste behavior.

Could you elaborate a bit on your use case? Do you want to simply perform a transformation on the pasted text?

@hecrj hecrj added feature New feature or request question Further information is requested labels Jan 3, 2022
@Alch-Emi
Copy link
Contributor Author

Sorry I didn't get back to this sooner! The project I'm working on is displaying several lines of text in a context where it makes sense to use individual, single-line text inputs. However, I'd like the user to be able to do a multi-line paste, so I'm trying to fudge it a little bit by intercepting the paste and breaking up the text between lines.

That said, I could definitely see this being used in other usecases where transformations might need to be applied as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants