-
Notifications
You must be signed in to change notification settings - Fork 0
/
tint.mm
212 lines (185 loc) · 7.96 KB
/
tint.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#import <AppKit/AppKit.h>
#include "tint.h"
#define CONTENT_EFFECT_VIEW_TAG 100022
#define INSPECTOR_EFFECT_VIEW_TAG 100023
#define TITLEBAR_BACKGROUND_VIEW_TAG 100024
#define TITLEBAR_EFFECT_VIEW_TAG 100025
@interface DCTaggedVisualEffectView : NSVisualEffectView
@property(readwrite) NSInteger tag;
@end
@implementation DCTaggedVisualEffectView
@synthesize tag = _tag;
@end
@interface DCTaggedView : NSView
@property(readwrite) NSInteger tag;
@end
@implementation DCTaggedView
@synthesize tag = _tag;
@end
napi_value setWindowLayout(napi_env env, napi_callback_info info) {
napi_status status;
size_t argc = 4;
napi_value args[4];
status = napi_get_cb_info(env, info, &argc, args, 0, 0);
if (status != napi_ok) {
napi_throw_error(env, NULL, "setWindowLayout(): failed to get arguments");
return NULL;
} else if (argc < 3) {
napi_throw_error(env, NULL, "setWindowLayout(): wrong number of arguments");
return NULL;
}
void *windowBuffer;
size_t windowBufferLength;
status = napi_get_buffer_info(env, args[0], &windowBuffer, &windowBufferLength);
if (status != napi_ok) {
napi_throw_error(env, NULL, "setWindowLayout(): cannot read window handle");
return NULL;
} else if (windowBufferLength == 0) {
napi_throw_error(env, NULL, "setWindowLayout(): empty window handle");
return NULL;
}
NSView *mainWindowView = *static_cast<NSView **>(windowBuffer);
if (![mainWindowView respondsToSelector:@selector(window)] || mainWindowView.window == nil) {
napi_throw_error(env, NULL, "setWindowLayout(): NSView doesn't contain window");
return NULL;
}
int sidebarWidth;
status = napi_get_value_int32(env, args[1], &sidebarWidth);
if (status != napi_ok) {
napi_throw_error(env, NULL, "setWindowLayout(): cannot read sidebarWidth from args");
return NULL;
}
int titlebarHeight;
status = napi_get_value_int32(env, args[2], &titlebarHeight);
if (status != napi_ok) {
napi_throw_error(env, NULL, "setWindowLayout(): cannot read titlebarHeight from args");
return NULL;
}
int titlebarMarginRight = 0;
if (argc >= 4) {
status = napi_get_value_int32(env, args[3], &titlebarMarginRight);
if (status != napi_ok) {
napi_throw_error(env, NULL, "setWindowLayout(): cannot read titlebarMarginRight from args");
return NULL;
}
}
NSWindow *window = mainWindowView.window;
NSView *view = window.contentView.subviews[1];
DCTaggedVisualEffectView *contentEffectView =
(DCTaggedVisualEffectView *)[view viewWithTag:CONTENT_EFFECT_VIEW_TAG];
if (contentEffectView) {
contentEffectView.frame =
CGRectMake(sidebarWidth, 0, [view frame].size.width - sidebarWidth - titlebarMarginRight,
[view frame].size.height - titlebarHeight);
} else {
contentEffectView = [[DCTaggedVisualEffectView alloc]
initWithFrame:CGRectMake(sidebarWidth, 0,
[view frame].size.width - sidebarWidth - titlebarMarginRight,
[view frame].size.height - titlebarHeight)];
[contentEffectView setBlendingMode:NSVisualEffectBlendingModeBehindWindow];
[contentEffectView setMaterial:NSVisualEffectMaterialWindowBackground];
[contentEffectView setTag:CONTENT_EFFECT_VIEW_TAG];
if ([[view subviews] count] > 0) {
[view addSubview:contentEffectView
positioned:NSWindowAbove
relativeTo:[[view subviews] objectAtIndex:0]];
} else {
[view addSubview:contentEffectView];
}
contentEffectView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
}
DCTaggedVisualEffectView *inspectorEffectView =
(DCTaggedVisualEffectView *)[view viewWithTag:INSPECTOR_EFFECT_VIEW_TAG];
if (inspectorEffectView) {
inspectorEffectView.frame = CGRectMake([view frame].size.width - titlebarMarginRight, 0,
titlebarMarginRight, [view frame].size.height);
} else {
inspectorEffectView = [[DCTaggedVisualEffectView alloc]
initWithFrame:CGRectMake([view frame].size.width - titlebarMarginRight, 0, titlebarMarginRight,
[view frame].size.height)];
[inspectorEffectView setBlendingMode:NSVisualEffectBlendingModeBehindWindow];
[inspectorEffectView setMaterial:NSVisualEffectMaterialWindowBackground];
[inspectorEffectView setTag:INSPECTOR_EFFECT_VIEW_TAG];
[view addSubview:inspectorEffectView positioned:NSWindowAbove relativeTo:contentEffectView];
inspectorEffectView.autoresizingMask = NSViewHeightSizable | NSViewMinXMargin;
}
DCTaggedView *titlebarBackgroundView = (DCTaggedView *)[view viewWithTag:TITLEBAR_BACKGROUND_VIEW_TAG];
if (titlebarBackgroundView) {
titlebarBackgroundView.frame =
CGRectMake(sidebarWidth, [view frame].size.height - titlebarHeight,
[view frame].size.width - sidebarWidth - titlebarMarginRight, titlebarHeight);
} else {
titlebarBackgroundView = [[DCTaggedView alloc]
initWithFrame:CGRectMake(sidebarWidth, [view frame].size.height - titlebarHeight,
[view frame].size.width - sidebarWidth - titlebarMarginRight,
titlebarHeight)];
[titlebarBackgroundView setTag:TITLEBAR_BACKGROUND_VIEW_TAG];
[view addSubview:titlebarBackgroundView positioned:NSWindowAbove relativeTo:inspectorEffectView];
titlebarBackgroundView.autoresizingMask = NSViewWidthSizable | NSViewMinYMargin;
NSBox *box = [[NSBox alloc] initWithFrame:CGRectMake(0, 0, [titlebarBackgroundView frame].size.width,
[titlebarBackgroundView frame].size.height)];
[box setBackgroundColor:[NSColor textBackgroundColor]];
[box setBoxType:NSBoxCustom];
[box setBorderType:NSNoBorder];
[titlebarBackgroundView addSubview:box];
box.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
}
DCTaggedVisualEffectView *titlebarEffectView =
(DCTaggedVisualEffectView *)[view viewWithTag:TITLEBAR_EFFECT_VIEW_TAG];
if (titlebarEffectView) {
titlebarEffectView.frame =
CGRectMake(sidebarWidth, [view frame].size.height - titlebarHeight,
[view frame].size.width - sidebarWidth - titlebarMarginRight, titlebarHeight);
} else {
titlebarEffectView = [[DCTaggedVisualEffectView alloc]
initWithFrame:CGRectMake(sidebarWidth, [view frame].size.height - titlebarHeight,
[view frame].size.width - sidebarWidth - titlebarMarginRight,
titlebarHeight)];
[titlebarEffectView setBlendingMode:NSVisualEffectBlendingModeWithinWindow];
[titlebarEffectView setMaterial:NSVisualEffectMaterialTitlebar];
[titlebarEffectView setTag:TITLEBAR_EFFECT_VIEW_TAG];
[view addSubview:titlebarEffectView positioned:NSWindowAbove relativeTo:titlebarBackgroundView];
titlebarEffectView.autoresizingMask = NSViewWidthSizable | NSViewMinYMargin;
}
return NULL;
}
napi_value setWindowAnimationBehavior(napi_env env, napi_callback_info info) {
napi_status status;
size_t argc = 2;
napi_value args[2];
status = napi_get_cb_info(env, info, &argc, args, 0, 0);
if (status != napi_ok) {
napi_throw_error(env, NULL, "setWindowAnimationBehavior(): failed to get arguments");
return NULL;
} else if (argc < 2) {
napi_throw_error(env, NULL, "setWindowAnimationBehavior(): wrong number of arguments");
return NULL;
}
void *windowBuffer;
size_t windowBufferLength;
status = napi_get_buffer_info(env, args[0], &windowBuffer, &windowBufferLength);
if (status != napi_ok) {
napi_throw_error(env, NULL, "setWindowAnimationBehavior(): cannot read window handle");
return NULL;
} else if (windowBufferLength == 0) {
napi_throw_error(env, NULL, "setWindowAnimationBehavior(): empty window handle");
return NULL;
}
bool isDocument;
status = napi_get_value_bool(env, args[1], &isDocument);
if (status != napi_ok) {
napi_throw_error(env, NULL, "setWindowAnimationBehavior(): cannot read sidebarWidth from args");
return NULL;
}
NSView *mainWindowView = *static_cast<NSView **>(windowBuffer);
if (![mainWindowView respondsToSelector:@selector(window)] || mainWindowView.window == nil) {
napi_throw_error(env, NULL, "setWindowAnimationBehavior(): NSView doesn't contain window");
return NULL;
}
NSWindow *window = mainWindowView.window;
if (window) {
window.animationBehavior =
isDocument ? NSWindowAnimationBehaviorDocumentWindow : NSWindowAnimationBehaviorDefault;
}
return NULL;
}