Skip to content

Commit

Permalink
[fix #766] Add a variable (and an event) for OS appearance (change)
Browse files Browse the repository at this point in the history
  • Loading branch information
L-TChen committed Oct 6, 2019
1 parent b72b455 commit 5730262
Show file tree
Hide file tree
Showing 14 changed files with 88 additions and 1 deletion.
7 changes: 7 additions & 0 deletions runtime/doc/autocmd.txt
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,8 @@ Name triggered by ~
|EncodingChanged| after the 'encoding' option has been changed
|TermChanged| after the value of 'term' has changed
|OptionSet| after setting any option
|OSAppearanceChanged| after the variable |v:os_appearance| has changed
{only in MacVim GUI}

Startup and exit
|VimEnter| after doing all the startup stuff
Expand Down Expand Up @@ -918,6 +920,11 @@ OptionSet After setting an option. The pattern is

When using |:set| in the autocommand the event
is not triggered again.
*OSAppearanceChanged*
OSAppearanceChanged After changing the variable |v:os_appearance|.
This change happens only if the OS changes
its appearance and Vim is running with a GUI.

*QuickFixCmdPre*
QuickFixCmdPre Before a quickfix command is run (|:make|,
|:lmake|, |:grep|, |:lgrep|, |:grepadd|,
Expand Down
12 changes: 12 additions & 0 deletions runtime/doc/eval.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2033,6 +2033,18 @@ v:operator The last operator given in Normal mode. This is a single
commands.
Read-only.

*v:os_appearance* *os-appearance-variable*
v:os_appearance The current OS appearance mode. Useful if you want to change
options |background| or |colorscheme| according to the
appearance of the GUI frontend. See also |OSAppearanceChanged|.
value description ~
0 Light Mode (always 0 on unsupported platforms)
1 Dark Mode
2 High-Contrast Light Mode
3 High-Contrast Dark Mode

{only in MacVim GUI}

*v:prevcount* *prevcount-variable*
v:prevcount The count given for the last but one Normal mode command.
This is the v:count value of the previous command. Useful if
Expand Down
4 changes: 4 additions & 0 deletions runtime/doc/gui_mac.txt
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ These are the non-standard options that MacVim supports:
These are the non-standard commands that MacVim supports:
|:macaction| |:macmenu|

*macvim-autocommands*
These are the non-standard events that MacVim supports:
|OSAppearanceChanged|

*macvim-find*
Whenever you search for something in Vim (e.g. using "/"), or hit <D-e> when
you have text selected, the search query is copied to the macOS "Find
Expand Down
3 changes: 3 additions & 0 deletions runtime/doc/tags
Original file line number Diff line number Diff line change
Expand Up @@ -4996,6 +4996,7 @@ OS390-bugs os_390.txt /*OS390-bugs*
OS390-has-ebcdic os_390.txt /*OS390-has-ebcdic*
OS390-limitations os_390.txt /*OS390-limitations*
OS390-open-source os_390.txt /*OS390-open-source*
OSAppearanceChanged autocmd.txt /*OSAppearanceChanged*
OffTheSpot mbyte.txt /*OffTheSpot*
OnTheSpot mbyte.txt /*OnTheSpot*
Operator-pending intro.txt /*Operator-pending*
Expand Down Expand Up @@ -8274,6 +8275,7 @@ options.txt options.txt /*options.txt*
optwin options.txt /*optwin*
or() eval.txt /*or()*
oracle ft_sql.txt /*oracle*
os-appearance-variable eval.txt /*os-appearance-variable*
os2 os_os2.txt /*os2*
os390 os_390.txt /*os390*
os_390.txt os_390.txt /*os_390.txt*
Expand Down Expand Up @@ -9753,6 +9755,7 @@ v:option_old eval.txt /*v:option_old*
v:option_oldglobal eval.txt /*v:option_oldglobal*
v:option_oldlocal eval.txt /*v:option_oldlocal*
v:option_type eval.txt /*v:option_type*
v:os_appearance eval.txt /*v:os_appearance*
v:prevcount eval.txt /*v:prevcount*
v:profiling eval.txt /*v:profiling*
v:progname eval.txt /*v:progname*
Expand Down
14 changes: 14 additions & 0 deletions src/MacVim/MMBackend.m
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ - (void)handleFindReplace:(NSDictionary *)args;
- (void)useSelectionForFind;
- (void)handleMarkedText:(NSData *)data;
- (void)handleGesture:(NSData *)data;
- (void)handleOSAppearanceChange:(NSData *)data;
#ifdef FEAT_BEVAL
- (void)bevalCallback:(id)sender;
#endif
Expand Down Expand Up @@ -2110,6 +2111,8 @@ - (void)handleInputEvent:(int)msgid data:(NSData *)data
ASLogDebug(@"SetWindowPositionMsgID: x=%d y=%d", winposX, winposY);
} else if (GestureMsgID == msgid) {
[self handleGesture:data];
} else if (NotifyAppearanceChangeMsgID == msgid) {
[self handleOSAppearanceChange:data];
} else if (ActivatedImMsgID == msgid) {
[self setImState:YES];
} else if (DeactivatedImMsgID == msgid) {
Expand Down Expand Up @@ -3326,6 +3329,17 @@ - (void)handleGesture:(NSData *)data
}
}

- (void)handleOSAppearanceChange:(NSData *)data {
const void *bytes = [data bytes];
int flag = *((int*)bytes); bytes += sizeof(int);
// 0 : (default) The standard (light) system appearance.
// 1 : The standard dark system appearance.
// 2 : The high-contrast version of the standard light system appearance.
// 3 : The high-contrast version of the standard dark system appearance.
set_vim_var_nr(VV_OS_APPEARANCE, flag);
apply_autocmds(EVENT_OSAPPCHANGED, NULL, NULL, FALSE, curbuf);
}

#ifdef FEAT_BEVAL
- (void)bevalCallback:(id)sender
{
Expand Down
2 changes: 2 additions & 0 deletions src/MacVim/MMVimController.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@
- (void)file:(NSString *)filename draggedToTabAtIndex:(NSUInteger)tabIndex;
- (void)filesDraggedToTabBar:(NSArray *)filenames;
- (void)dropString:(NSString *)string;
- (void)appearanceChanged:(int)flag;

- (void)passArguments:(NSDictionary *)args;
- (void)sendMessage:(int)msgid data:(NSData *)data;
- (BOOL)sendMessageNow:(int)msgid data:(NSData *)data
Expand Down
11 changes: 11 additions & 0 deletions src/MacVim/MMVimController.m
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ - (id)initWithBackend:(id)backend pid:(int)processIdentifier

isInitialized = YES;

// After MMVimController's initialization is completed,
// set up the variable `v:os_appearance`.
[self appearanceChanged:getCurrentAppearance([windowController vimView].effectiveAppearance)];

return self;
}

Expand Down Expand Up @@ -358,6 +362,13 @@ - (void)dropString:(NSString *)string
}
}

- (void)appearanceChanged:(int)flag
{
[self sendMessage:NotifyAppearanceChangeMsgID
data:[NSData dataWithBytes: &flag
length:sizeof(flag)]];
}

- (void)passArguments:(NSDictionary *)args
{
if (!args) return;
Expand Down
4 changes: 4 additions & 0 deletions src/MacVim/MMVimView.m
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,10 @@ - (void)setFrame:(NSRect)frame
[self frameSizeMayHaveChanged:NO];
}

- (void)viewDidChangeEffectiveAppearance
{
[vimController appearanceChanged:getCurrentAppearance(self.effectiveAppearance)];
}
@end // MMVimView


Expand Down
1 change: 1 addition & 0 deletions src/MacVim/MacVim.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ extern const char * const MMVimMsgIDStrings[];
MSG(AddToMRUMsgID) \
MSG(BackingPropertiesChangedMsgID) \
MSG(SetBlurRadiusMsgID) \
MSG(NotifyAppearanceChangeMsgID) \
MSG(EnableLigaturesMsgID) \
MSG(DisableLigaturesMsgID) \
MSG(EnableThinStrokesMsgID) \
Expand Down
2 changes: 2 additions & 0 deletions src/MacVim/Miscellaneous.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,5 @@ NSArray *normalizeFilenames(NSArray *filenames);
BOOL shouldUseYosemiteTabBarStyle();
BOOL shouldUseMojaveTabBarStyle();
BOOL shouldUseBufferedDrawing();

int getCurrentAppearance(NSAppearance *appearance);
23 changes: 23 additions & 0 deletions src/MacVim/Miscellaneous.m
Original file line number Diff line number Diff line change
Expand Up @@ -330,3 +330,26 @@ - (NSInteger)tag
#endif
return NO;
}


int
getCurrentAppearance(NSAppearance *appearance){
int flag = 0; // for macOS 10.13 or eariler always return 0;
#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_14
if (@available(macOS 10.14, *)) {
NSAppearanceName appearanceName = [appearance bestMatchFromAppearancesWithNames:
@[NSAppearanceNameAqua
, NSAppearanceNameDarkAqua
, NSAppearanceNameAccessibilityHighContrastAqua
, NSAppearanceNameAccessibilityHighContrastDarkAqua]];
if ([appearanceName isEqualToString:NSAppearanceNameDarkAqua]) {
flag = 1;
} else if ([appearanceName isEqualToString:NSAppearanceNameAccessibilityHighContrastAqua]) {
flag = 2;
} else if ([appearanceName isEqualToString:NSAppearanceNameAccessibilityHighContrastDarkAqua]) {
flag = 3;
}
}
#endif
return flag;
}
1 change: 1 addition & 0 deletions src/autocmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ static struct event_name
{"InsertCharPre", EVENT_INSERTCHARPRE},
{"MenuPopup", EVENT_MENUPOPUP},
{"OptionSet", EVENT_OPTIONSET},
{"OSAppearanceChanged", EVENT_OSAPPCHANGED},
{"QuickFixCmdPost", EVENT_QUICKFIXCMDPOST},
{"QuickFixCmdPre", EVENT_QUICKFIXCMDPRE},
{"QuitPre", EVENT_QUITPRE},
Expand Down
1 change: 1 addition & 0 deletions src/evalvars.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ static struct vimvar
{VV_NAME("event", VAR_DICT), VV_RO},
{VV_NAME("versionlong", VAR_NUMBER), VV_RO},
{VV_NAME("echospace", VAR_NUMBER), VV_RO},
{VV_NAME("os_appearance", VAR_NUMBER), VV_RO},
};

// shorthand
Expand Down
4 changes: 3 additions & 1 deletion src/vim.h
Original file line number Diff line number Diff line change
Expand Up @@ -1322,6 +1322,7 @@ enum auto_event
EVENT_INSERTLEAVE, // when leaving Insert mode
EVENT_MENUPOPUP, // just before popup menu is displayed
EVENT_OPTIONSET, // option was set
EVENT_OSAPPCHANGED, // after changing the OS appearance
EVENT_QUICKFIXCMDPOST, // after :make, :grep etc.
EVENT_QUICKFIXCMDPRE, // before :make, :grep etc.
EVENT_QUITPRE, // before :quit
Expand Down Expand Up @@ -1992,7 +1993,8 @@ typedef int sock_T;
#define VV_EVENT 90
#define VV_VERSIONLONG 91
#define VV_ECHOSPACE 92
#define VV_LEN 93 // number of v: vars
#define VV_OS_APPEARANCE 93
#define VV_LEN 94 // number of v: vars

// used for v_number in VAR_SPECIAL
#define VVAL_FALSE 0L
Expand Down

0 comments on commit 5730262

Please sign in to comment.