luckymarmot/ThemeKit

Protocol extension methods not available from Objective-C

niklasberglund opened this issue · 3 comments

Had a go at trying ThemeKit out in an Objective-C project. Using it with Objective-C doesn't seem very trivial. I tried importing <macOSThemeKit/macOSThemeKit-Swift.h>, grabbing an instance of dark theme with:
TKDarkTheme *darkTheme = [TKThemeManager darkTheme]

Then I should invoke the apply method on the darkTheme object right? But TKDarkTheme don't have any apply method.

Did I misunderstand how to apply a theme?

Hi @niklasberglund!

Thank you for your feedback! No, you didn't misunderstand. The thing is, the Theme class extension that provides some convenience methods (like this) is not available from Objective-C (swift protocol is defined with @objc, but by definition, functions and properties defined on protocol extensions are not available when accessing from Objective-C).

As a workaround, and because this protocol extension is very simple, you can do what that method does on the extension, which is, setting the .theme property on TKThemeManager:

@import macOSThemeKit;

@implementation AppDelegate

- (void)applicationWillFinishLaunching:(NSNotification *)aNotification {
    TKDarkTheme *darkTheme = [TKThemeManager darkTheme];
    [[TKThemeManager sharedManager] setTheme:darkTheme];
}

@end

I'm sorry for the trouble - I need to update the docs about this. I will later dig into this to check if I can come with a better alternate implementation.
Thank you for pointing this out.

@niklasberglund just a quick follow-up:

The README.md and documentation has been updated to include Objective-C examples for the simpler and advanced usage sections.

Thanks and sorry for the inconvenience.

Perfect! Thank you. Look forward to trying it out more.