Skip to content

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

@radicaled

Description

@radicaled

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions