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

Setting rich text style on empty range causes buggy behaviour on macOS (see video) #130

Open
DominikBucher12 opened this issue Jan 22, 2024 · 3 comments
Labels
bug Something isn't working macOS Affects only macOS

Comments

@DominikBucher12
Copy link
Collaborator

DominikBucher12 commented Jan 22, 2024

This is caused by different behaviours of var richTextAttributes: RichTextAttributes inside RichTextViewComponent+Attributes.swift There are two fixes I came up with:

  1. in func setStyle(_ style: RichTextStyle, to newValue: Bool) inside RichTextCoordinator, replace the function with #ifdef like this:
func setStyle(_ style: RichTextStyle, to newValue: Bool) {
        let hasStyle = textView.richTextStyles.hasStyle(style)
    
        #if macOS
        // If we set value to true, we expect the style not to contain it.
        // The other way around, this doesnt quite work on macOS since we do not count in typing attributes.
        if newValue == hasStyle && newValue == true { return }
        #else
        if newValue == hasStyle { return }
        #endif
        
        textView.setRichTextStyle(style, to: newValue)
    }

This forces to toggle style on macOS no matter what. This feels a bit like hack to me.

  1. var richTextAttributes: RichTextAttributes behaves differently on macOS, we should either return typingAttributes when the richTextAttributes are empty or treat this the same as on iOS since there is no issue.

Note: It looks like none of the fixes above 100% works :D

Screen.Recording.2024-01-22.at.7.58.38.mov
@DominikBucher12 DominikBucher12 added bug Something isn't working macOS Affects only macOS labels Jan 22, 2024
@DominikBucher12
Copy link
Collaborator Author

DominikBucher12 commented Jan 22, 2024

Btw you can reproduce this bug all way to 0.9.0 (didn't dare to go further) where highlighting of selected styles doesnt even work 😅 So it has been there for quite a while... I will fix this ASAP but I need to merge #129 and subscriptions, so patience please 🙏🏻

@danielsaidi
Copy link
Owner

Nice find! It seems to be something with the underline and strikethrough, which are handled differently than bold and italic:

func setRichTextStyle(
        _ style: RichTextStyle,
        to newValue: Bool
    ) {
        let value = newValue ? 1 : 0
        switch style {
        case .bold, .italic:
            let styles = richTextStyles
            guard shouldAddOrRemove(style, newValue, given: styles) else { return }
            guard let font = richTextFont else { return }
            guard let newFont = newFont(for: font, byToggling: style) else { return }
            setRichTextFont(newFont)
        case .underlined:
            setRichTextAttribute(.underlineStyle, to: value)
        case .strikethrough:
            setRichTextAttribute(.strikethroughStyle, to: value)
        }
    }

The code for setRichTextAttribute looks like this:

func setRichTextAttribute(
        _ attribute: RichTextAttribute,
        to value: Any
    ) {
        #if macOS
        setRichTextAttribute(attribute, to: value, at: selectedRange)
        typingAttributes[attribute] = value
        #else
        if hasSelectedRange {
            setRichTextAttribute(attribute, to: value, at: selectedRange)
        } else {
            typingAttributes[attribute] = value
        }
        #endif
    }

BUT it works the same, even if I remove the platform check:

func setRichTextAttribute(
        _ attribute: RichTextAttribute,
        to value: Any
    ) {
        if hasSelectedRange {
            setRichTextAttribute(attribute, to: value, at: selectedRange)
        } else {
            typingAttributes[attribute] = value
        }
    }

The behavior is however still the same for strikethrough and underline, while colors (which also use this), work great.

I'll push the platform check removal, to make this easier to fix later on.

@DominikBucher12
Copy link
Collaborator Author

DominikBucher12 commented Jan 22, 2024

IMHO this will affect (or has affected in past) :D even colors and everything, I had this issue before I think but with colors but I am not sure... I think this will be in setting/unsetting Attributes, but with one fix, we can rule them all :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working macOS Affects only macOS
Projects
None yet
Development

No branches or pull requests

2 participants