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

Use of StyleSpans.subView, and replacing StyleSpans at certain positions #301

Closed
radicaled opened this issue Apr 29, 2016 · 4 comments
Closed

Comments

@radicaled
Copy link

So, let's say I'm using a StyleClassedTextArea, and I want to change the background color of the user's current focus, based on where their caret is at the moment. I'm also doing other manipulations, so I have a function that receives a StyleSpans<Collection<String>> and is also expected to return a StyleSpans<Collection<String>> with the correct styles applied to it. This way, I can avoid multiple setStyle calls and just do it once after every processor adds or removes classes as necessary.

I'm using StyleSpans.subView(...) to scope my class additions / subtractions to a small range, but I'm not sure how to merge my changes back into the main collection after I've used mapStyles or overlay on the StyleSpans returned from the "master" StyleSpans.subView.

Here's some pseudo-code written in Kotlin if I'm not clear enough.

// Where's the caret at?
val indexRange = getCurrentParagraphBasedOnEvilWizardry(textArea.selection)
// Get all the style spans in the document
val styleSpans = textArea.getStyleSpans(0, textArea.length - 1)
// Remove old "focused" styling, because the user might not be on the same line anymore
val withoutFocus = styleSpans.mapStyles { /* removes "focused" class from each collection */ }
// OK, now I have a StyleSpan collection that no longer includes the "focused" class.
// Let's get a subview containing just the stylespans that exist in indexRange.
val subview = withoutFocus.subView(indexRange.start, indexRange.end)
// OK, now let's add the "focused" style.
val newStyle = StyleSpans.singleton(/* ... */)
val withFocusSubview = subview.overlay(newStyle) { theirs, ours -> (theirs + ours) }
// OK, now we've got a StyleSpans that all have the "focused" class in them.
// Now we need to go back to withoutFocus, and replace the stylespans contained within indexRange with the new ones.
// Nonexistant method.
return withoutFocus.replace(indexRange.start, indexRange.end, withFocusSubview); 

I'm trying to avoid multiple calls to setStyle and having other components know about the textarea.

@TomasMikula
Copy link
Member

You could do two more sub-views, before and after the "focused" sub-view, and then concat all three together.

In case you are using the SNAPSHOT version, you can style the whole paragraph using the :has-caret pseudo class, like here.

@radicaled
Copy link
Author

Thanks. The :has-caret class might be what I need, but I'm on 0.6.10 for now. Is there also a way to style the background of paragraphs themselves in SNAPSHOT?

@TomasMikula
Copy link
Member

Yes. StyledTextArea, Paragraph, etc. are parameterized by an additional type parameter, PS, representing the paragraph style.

@radicaled
Copy link
Author

Thanks (again!). Looks like I need to upgrade to the SNAPSHOT version to clean up my solutions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants