SourceCodeSyntaxHighlight icon indicating copy to clipboard operation
SourceCodeSyntaxHighlight copied to clipboard

Bug with constantly reverting back to light mode quicklook, even though OSX is in dark

Open seaders opened this issue 9 months ago • 10 comments

I think this bug is best shown with a screenrecord:

https://github.com/user-attachments/assets/3da38837-a13e-4ea9-8883-ec369e044787

You can see that it's showing up as very bright light, despite me being in dark mode. When I then change to OSX's light mode, it changes to dark for a second (I think incorrectly?), and then back to light. Then I switch back to dark mode, and it does nothing. Then I go light mode, dark mode again, and it shows as dark mode, properly now, but when I go to another file, it's back in light mode again.

I looked in the code, as per the advice here https://developer.apple.com/documentation/uikit/supporting-dark-mode-in-your-interface which had this snippet

    observation = NSApp.observe(\.effectiveAppearance) { (app, _) in
        app.effectiveAppearance.performAsCurrentDrawingAppearance {
            // Invoke your non-view code that needs to be aware of the
            // change in appearance.
        }
    }

But the only reference to effectiveAppearance in the code is here, https://github.com/sbarex/SourceCodeSyntaxHighlight/blob/d8460c64c06de3db0fcc5e18255276a41d6601c0/QLExtension/PreviewViewController.swift#L85

    /// Check if the appearance is Dark
    lazy var isDarkMode: Bool = {
        // fixme: nell'estensione non sempre restituisce il valore aggiornato!
        if let appearance = self.view.effectiveAppearance.bestMatch(from: [.darkAqua, .aqua]) {
            return appearance == .darkAqua
        }
        return false
    }()

Which seems to be where the problem is. The fixme there is translated as

in the extension it does not always return the updated value

But yeah, for my experience, it's not a sometimes issue, this is never right, for me, when in dark mode.

seaders avatar Apr 11 '25 12:04 seaders

I seem to be having the same issue too.

AWH1122 avatar Apr 24 '25 09:04 AWH1122

Try using the HTML rendering engine. The RTF on the dark theme has some bugs, macOS automatically changes the background. In a future release I think I will remove the choice and leave only the HTML engine.

sbarex avatar Apr 24 '25 09:04 sbarex

This is the quicklook view I get when I switch to the HTML engine

Image

Which, for browsing through files, I'd absolutely hate to be forced into only having that @sbarex . For quickly scanning through files, the RTF view is essential IMO, even with the light mode issue / other bugs.

seaders avatar Apr 24 '25 09:04 seaders

this is unfortunately another limitation of macos, in gallery mode the html preview only shows the enlarged icon of the file (while the rtf preview shows the formatted text). Try to see in the quicklook window how it looks (press the space button)

sbarex avatar Apr 24 '25 10:04 sbarex

Yeah no, that part is completely fine - including the dark mode issue. The popup view works 100% with HTML, but just checking now and the RTF issue seems to be backwards?

Image

Btw, I'm more than happy to spend a bit of time investigating this issue myself, I just wasn't totally sure how I go about installing the service locally from the XCode project. I was in there poking around the other day, but just don't have any of that specific MacOS app dev experience, on the services-ish side of things.

seaders avatar Apr 24 '25 10:04 seaders

I have not found any Apple documentation regarding RTF codes for managing dark mode. Theoretically it is possible to create RTF files that automatically adapt to the light/dark theme.

By the way, the creation of the RTF output is managed by the external Highlight engine, so it is not easy to intervene. I remember that even just setting the background color was not a simple thing years ago. However, as long as it is not clear which RTF codes are needed, the problem of how to implement the corrections does not arise.

Some ideas can be found in:

  • https://eclecticlight.co/2018/12/10/rendering-rich-text-in-dark-mode/
  • https://eclecticlight.co/2018/11/14/making-documents-look-good-in-both-appearance-modes/
  • https://eclecticlight.co/2022/08/03/how-rich-text-can-vanish-in-quicklook/

sbarex avatar Apr 24 '25 10:04 sbarex

Okay so, I'm not sure exactly what's happening, but I did notice that, no matter what I did, it wasn't actually showing the light scheme, while I'm in dark mode settings. It was showing kinda an inverse of what I had chosen for the dark view, so... I chose a light view in the dark area and...

Image

I now have it working exactly as I want, and am getting more light mode jump scares. I don't REALLY care if this is a bug with this, or OSX, it now is working absolutely perfectly for me.

If it is a bug, I hope this "solution" (hack) helps get to the cause of it, but yeah, I'm delighted now.

seaders avatar Jun 19 '25 09:06 seaders

When doing this do you know what theme is actually being used to provide the colours for the highlighting?

AWH1122 avatar Jun 19 '25 10:06 AWH1122

When doing this do you know what theme is actually being used to provide the colours for the highlighting?

I honestly don't know exactly what's going on, but it's like a "darked" or some ways inverted version of the light mode selected for dark, chosen by checking "Show only light modes" in the dark area,

Image

seaders avatar Jun 19 '25 10:06 seaders

No worries! seems to get the job done, nice to have a way to use RTF over HTML in dark mode.

AWH1122 avatar Jun 19 '25 10:06 AWH1122