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

Set appropriate colours for Multiline Entry control on dark mode (Darwin) #481

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
26 changes: 24 additions & 2 deletions darwin/multilineentry.m
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// 8 december 2015
#import "uipriv_darwin.h"
#include "util.h"

// NSTextView has no intrinsic content size by default, which wreaks havoc on a pure-Auto Layout system
// we'll have to take over to get it to work
// see also http://stackoverflow.com/questions/24210153/nstextview-not-properly-resizing-with-auto-layout and http://stackoverflow.com/questions/11237622/using-autolayout-with-expanding-nstextviews
@interface intrinsicSizeTextView : NSTextView {
@interface intrinsicSizeTextView : NSTextView<NSTextViewDelegate> {
uiMultilineEntry *libui_e;
}
- (id)initWithFrame:(NSRect)r e:(uiMultilineEntry *)e;
Expand All @@ -25,11 +26,21 @@ @implementation intrinsicSizeTextView
- (id)initWithFrame:(NSRect)r e:(uiMultilineEntry *)e
{
self = [super initWithFrame:r];
self.delegate = self;
if (self)
self->libui_e = e;
return self;
}

// [e->tv setTextColor:[NSColor textColor]] in finishMultilineentry
// does not work due to an cocoa bug which stops the text
// from changing pre control initialization so we resort
// to changing the text on the delegate method instead
- (void)textDidChange:(NSNotification *)aNotification {
NSTextView *tv = (NSTextView *)[aNotification object];
[tv setTextColor:[NSColor textColor]];
}

- (NSSize)intrinsicContentSize
{
NSTextContainer *textContainer;
Expand Down Expand Up @@ -130,7 +141,6 @@ void uiMultilineEntrySetReadOnly(uiMultilineEntry *e, int readonly)

// NSText properties:
// this is what Interface Builder sets the background color to
[e->tv setBackgroundColor:[NSColor colorWithCalibratedWhite:1.0 alpha:1.0]];
[e->tv setDrawsBackground:YES];
[e->tv setEditable:YES];
[e->tv setSelectable:YES];
Expand All @@ -139,6 +149,13 @@ void uiMultilineEntrySetReadOnly(uiMultilineEntry *e, int readonly)
[e->tv setImportsGraphics:NO];
[e->tv setUsesFontPanel:NO];
[e->tv setRulerVisible:NO];

if (isDarkMode()) {
e->tv.backgroundColor = NSColor.textBackgroundColor;
} else {
[e->tv setBackgroundColor:[NSColor colorWithCalibratedWhite:1.0 alpha:1.0]];
}

// we'll handle font last
// while setAlignment: has been around since 10.0, the named constant "NSTextAlignmentNatural" seems to have only been introduced in 10.11
#define ourNSTextAlignmentNatural 4
Expand Down Expand Up @@ -217,6 +234,11 @@ void uiMultilineEntrySetReadOnly(uiMultilineEntry *e, int readonly)
p.VScroll = YES;
e->sv = uiprivMkScrollView(&p, &(e->d));

if (isDarkMode()) {
e->sv.drawsBackground= YES;
e->sv.backgroundColor = NSColor.textBackgroundColor;
}

uiMultilineEntryOnChanged(e, defaultOnChanged, NULL);

return e;
Expand Down
2 changes: 2 additions & 0 deletions darwin/util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

bool isDarkMode();
6 changes: 6 additions & 0 deletions darwin/util.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@ void uiprivDisableAutocorrect(NSTextView *tv)
[tv setAutomaticLinkDetectionEnabled:NO];
[tv setSmartInsertDeleteEnabled:NO];
}

bool isDarkMode()
{
NSString *osxMode = [[NSUserDefaults standardUserDefaults] stringForKey:@"AppleInterfaceStyle"];
return [osxMode isEqualToString:@"Dark"];
}