Skip to content

Commit 0b679a4

Browse files
authored
Merge pull request #929 from L-TChen/dark-event
[fix #766] Add an event for OS appearance change
2 parents b72b455 + 5730262 commit 0b679a4

14 files changed

Lines changed: 88 additions & 1 deletion

runtime/doc/autocmd.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,8 @@ Name triggered by ~
290290
|EncodingChanged| after the 'encoding' option has been changed
291291
|TermChanged| after the value of 'term' has changed
292292
|OptionSet| after setting any option
293+
|OSAppearanceChanged| after the variable |v:os_appearance| has changed
294+
{only in MacVim GUI}
293295

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

919921
When using |:set| in the autocommand the event
920922
is not triggered again.
923+
*OSAppearanceChanged*
924+
OSAppearanceChanged After changing the variable |v:os_appearance|.
925+
This change happens only if the OS changes
926+
its appearance and Vim is running with a GUI.
927+
921928
*QuickFixCmdPre*
922929
QuickFixCmdPre Before a quickfix command is run (|:make|,
923930
|:lmake|, |:grep|, |:lgrep|, |:grepadd|,

runtime/doc/eval.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2033,6 +2033,18 @@ v:operator The last operator given in Normal mode. This is a single
20332033
commands.
20342034
Read-only.
20352035

2036+
*v:os_appearance* *os-appearance-variable*
2037+
v:os_appearance The current OS appearance mode. Useful if you want to change
2038+
options |background| or |colorscheme| according to the
2039+
appearance of the GUI frontend. See also |OSAppearanceChanged|.
2040+
value description ~
2041+
0 Light Mode (always 0 on unsupported platforms)
2042+
1 Dark Mode
2043+
2 High-Contrast Light Mode
2044+
3 High-Contrast Dark Mode
2045+
2046+
{only in MacVim GUI}
2047+
20362048
*v:prevcount* *prevcount-variable*
20372049
v:prevcount The count given for the last but one Normal mode command.
20382050
This is the v:count value of the previous command. Useful if

runtime/doc/gui_mac.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ These are the non-standard options that MacVim supports:
121121
These are the non-standard commands that MacVim supports:
122122
|:macaction| |:macmenu|
123123

124+
*macvim-autocommands*
125+
These are the non-standard events that MacVim supports:
126+
|OSAppearanceChanged|
127+
124128
*macvim-find*
125129
Whenever you search for something in Vim (e.g. using "/"), or hit <D-e> when
126130
you have text selected, the search query is copied to the macOS "Find

runtime/doc/tags

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4996,6 +4996,7 @@ OS390-bugs os_390.txt /*OS390-bugs*
49964996
OS390-has-ebcdic os_390.txt /*OS390-has-ebcdic*
49974997
OS390-limitations os_390.txt /*OS390-limitations*
49984998
OS390-open-source os_390.txt /*OS390-open-source*
4999+
OSAppearanceChanged autocmd.txt /*OSAppearanceChanged*
49995000
OffTheSpot mbyte.txt /*OffTheSpot*
50005001
OnTheSpot mbyte.txt /*OnTheSpot*
50015002
Operator-pending intro.txt /*Operator-pending*
@@ -8274,6 +8275,7 @@ options.txt options.txt /*options.txt*
82748275
optwin options.txt /*optwin*
82758276
or() eval.txt /*or()*
82768277
oracle ft_sql.txt /*oracle*
8278+
os-appearance-variable eval.txt /*os-appearance-variable*
82778279
os2 os_os2.txt /*os2*
82788280
os390 os_390.txt /*os390*
82798281
os_390.txt os_390.txt /*os_390.txt*
@@ -9753,6 +9755,7 @@ v:option_old eval.txt /*v:option_old*
97539755
v:option_oldglobal eval.txt /*v:option_oldglobal*
97549756
v:option_oldlocal eval.txt /*v:option_oldlocal*
97559757
v:option_type eval.txt /*v:option_type*
9758+
v:os_appearance eval.txt /*v:os_appearance*
97569759
v:prevcount eval.txt /*v:prevcount*
97579760
v:profiling eval.txt /*v:profiling*
97589761
v:progname eval.txt /*v:progname*

src/MacVim/MMBackend.m

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ - (void)handleFindReplace:(NSDictionary *)args;
198198
- (void)useSelectionForFind;
199199
- (void)handleMarkedText:(NSData *)data;
200200
- (void)handleGesture:(NSData *)data;
201+
- (void)handleOSAppearanceChange:(NSData *)data;
201202
#ifdef FEAT_BEVAL
202203
- (void)bevalCallback:(id)sender;
203204
#endif
@@ -2110,6 +2111,8 @@ - (void)handleInputEvent:(int)msgid data:(NSData *)data
21102111
ASLogDebug(@"SetWindowPositionMsgID: x=%d y=%d", winposX, winposY);
21112112
} else if (GestureMsgID == msgid) {
21122113
[self handleGesture:data];
2114+
} else if (NotifyAppearanceChangeMsgID == msgid) {
2115+
[self handleOSAppearanceChange:data];
21132116
} else if (ActivatedImMsgID == msgid) {
21142117
[self setImState:YES];
21152118
} else if (DeactivatedImMsgID == msgid) {
@@ -3326,6 +3329,17 @@ - (void)handleGesture:(NSData *)data
33263329
}
33273330
}
33283331

3332+
- (void)handleOSAppearanceChange:(NSData *)data {
3333+
const void *bytes = [data bytes];
3334+
int flag = *((int*)bytes); bytes += sizeof(int);
3335+
// 0 : (default) The standard (light) system appearance.
3336+
// 1 : The standard dark system appearance.
3337+
// 2 : The high-contrast version of the standard light system appearance.
3338+
// 3 : The high-contrast version of the standard dark system appearance.
3339+
set_vim_var_nr(VV_OS_APPEARANCE, flag);
3340+
apply_autocmds(EVENT_OSAPPCHANGED, NULL, NULL, FALSE, curbuf);
3341+
}
3342+
33293343
#ifdef FEAT_BEVAL
33303344
- (void)bevalCallback:(id)sender
33313345
{

src/MacVim/MMVimController.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@
6767
- (void)file:(NSString *)filename draggedToTabAtIndex:(NSUInteger)tabIndex;
6868
- (void)filesDraggedToTabBar:(NSArray *)filenames;
6969
- (void)dropString:(NSString *)string;
70+
- (void)appearanceChanged:(int)flag;
71+
7072
- (void)passArguments:(NSDictionary *)args;
7173
- (void)sendMessage:(int)msgid data:(NSData *)data;
7274
- (BOOL)sendMessageNow:(int)msgid data:(NSData *)data

src/MacVim/MMVimController.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ - (id)initWithBackend:(id)backend pid:(int)processIdentifier
176176

177177
isInitialized = YES;
178178

179+
// After MMVimController's initialization is completed,
180+
// set up the variable `v:os_appearance`.
181+
[self appearanceChanged:getCurrentAppearance([windowController vimView].effectiveAppearance)];
182+
179183
return self;
180184
}
181185

@@ -358,6 +362,13 @@ - (void)dropString:(NSString *)string
358362
}
359363
}
360364

365+
- (void)appearanceChanged:(int)flag
366+
{
367+
[self sendMessage:NotifyAppearanceChangeMsgID
368+
data:[NSData dataWithBytes: &flag
369+
length:sizeof(flag)]];
370+
}
371+
361372
- (void)passArguments:(NSDictionary *)args
362373
{
363374
if (!args) return;

src/MacVim/MMVimView.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,10 @@ - (void)setFrame:(NSRect)frame
664664
[self frameSizeMayHaveChanged:NO];
665665
}
666666

667+
- (void)viewDidChangeEffectiveAppearance
668+
{
669+
[vimController appearanceChanged:getCurrentAppearance(self.effectiveAppearance)];
670+
}
667671
@end // MMVimView
668672

669673

src/MacVim/MacVim.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ extern const char * const MMVimMsgIDStrings[];
261261
MSG(AddToMRUMsgID) \
262262
MSG(BackingPropertiesChangedMsgID) \
263263
MSG(SetBlurRadiusMsgID) \
264+
MSG(NotifyAppearanceChangeMsgID) \
264265
MSG(EnableLigaturesMsgID) \
265266
MSG(DisableLigaturesMsgID) \
266267
MSG(EnableThinStrokesMsgID) \

src/MacVim/Miscellaneous.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,5 @@ NSArray *normalizeFilenames(NSArray *filenames);
160160
BOOL shouldUseYosemiteTabBarStyle();
161161
BOOL shouldUseMojaveTabBarStyle();
162162
BOOL shouldUseBufferedDrawing();
163+
164+
int getCurrentAppearance(NSAppearance *appearance);

0 commit comments

Comments
 (0)