-
Notifications
You must be signed in to change notification settings - Fork 3
/
IPDFMacSheet.m
250 lines (190 loc) · 6.98 KB
/
IPDFMacSheet.m
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
//
// IPDFMacSheet.m
// InstaPDF for Mac
//
// Created by mmackh on 13.10.19.
// Copyright © 2019 mackh ag. All rights reserved.
//
#import "IPDFMacSheet.h"
@interface NSWindow_Catalyst : NSObject
- (void)beginSheet:(id)sheet completionHandler:(id)completionHandler;
@property id contentView;
@property CGSize contentSize;
@end
@interface IPDFMacSheetToolbarItemState : NSObject
@property (nonatomic,weak) NSToolbarItem *toolbarItem;
@property (nonatomic) BOOL enabled;
@property (nonatomic) BOOL autovalidates;
@end @implementation IPDFMacSheetToolbarItemState @end
@interface IPDFMacSheet ()
@property (nonatomic) UIView *backgroundView;
@end
@interface IPDFMacSheet ()
+ (void)setDisableToolbar:(BOOL)toolbar;
@end
@implementation IPDFMacSheet
+ (UIWindow *)targetWindow
{
UIWindow *targetWindow = nil;
for (UIWindow *window in [UIApplication sharedApplication].windows)
{
if (window.isKeyWindow)
{
targetWindow = window;
break;
}
}
return targetWindow;
}
+ (instancetype)showSheetWithSizeHandler:(CGSize(^)(CGRect windowBounds))sizeHandler viewConfigurationHandler:(void(^)(UIView *contentView))viewConfigurationHandler
{
UIWindow *targetWindow = [self targetWindow];
IPDFMacSheet *sheet = [[self class] new];
UIEdgeInsets safeAreaInsets = targetWindow.safeAreaInsets;
CGRect windowBounds = targetWindow.frame;
windowBounds.origin.y = safeAreaInsets.top;
windowBounds.size.height -= safeAreaInsets.top;
CGRect backgroundViewFrame = windowBounds;
IPDFMacSheet *possibleParentSheet = [IPDFMacSheet currentSheet];
if (possibleParentSheet)
{
// Avoid compounding white background that turns more opaque the more sheets are stacked
backgroundViewFrame = possibleParentSheet.frame;
}
sheet.backgroundView = [[UIView alloc] initWithFrame:backgroundViewFrame];
sheet.backgroundView.backgroundColor = [UIColor clearColor];
sheet.backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[targetWindow addSubview:sheet.backgroundView];
sheet.sizeHandler = sizeHandler;
CGSize sheetSize = sizeHandler(windowBounds);
CGRect targetSheetFrame = windowBounds;
targetSheetFrame.size = sheetSize;
targetSheetFrame.origin.x = windowBounds.size.width / 2 - sheetSize.width/2;
targetSheetFrame.origin.y = -targetSheetFrame.size.height;
sheet.frame = targetSheetFrame;
sheet.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
targetSheetFrame.origin.y = safeAreaInsets.top - 1;
UIVisualEffectView *backgroundBlurView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleSystemThickMaterial]];
backgroundBlurView.frame = sheet.bounds;
backgroundBlurView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[sheet addSubview:backgroundBlurView];
sheet.layer.borderColor = [UIColor colorWithRed:0.74 green:0.74 blue:0.74 alpha:1.00].CGColor;
sheet.layer.borderWidth = 1.0;
sheet.layer.shadowColor = [UIColor colorWithWhite:0.3 alpha:1.0].CGColor;
sheet.layer.shadowRadius = 8;
sheet.layer.shadowOffset = CGSizeZero;
sheet.layer.shadowOpacity = 0.5;
[targetWindow addSubview:sheet];
if (viewConfigurationHandler) viewConfigurationHandler(sheet);
[UIView animateWithDuration:0.35 animations:^
{
sheet.backgroundView.backgroundColor = [[UIColor systemBackgroundColor] colorWithAlphaComponent:0.15];
sheet.frame = targetSheetFrame;
}];
if ([IPDFMacSheet currentSheets].count == 1)
{
[IPDFMacSheet setDisableToolbar:YES];
}
return sheet;
}
+ (instancetype)currentSheet
{
return [IPDFMacSheet currentSheets].lastObject;
}
+ (NSArray *)currentSheets
{
NSMutableArray *sheetsMutable = [NSMutableArray new];
UIWindow *targetWindow = [self targetWindow];
for (UIView *subview in targetWindow.subviews)
{
if ([subview isKindOfClass:[IPDFMacSheet class]])
{
[sheetsMutable addObject:subview];
}
}
return sheetsMutable;
}
- (void)dismiss
{
[self dismissWithCompletionHandler:nil];
}
- (void)dismissWithCompletionHandler:(void (^)(void))completionHandler
{
self.sizeHandler = nil;
CGRect targetSheetFrame = self.frame;
targetSheetFrame.origin.y = -targetSheetFrame.size.height;
[UIView animateWithDuration:0.4 animations:^
{
self.frame = targetSheetFrame;
self.backgroundView.backgroundColor = [UIColor clearColor];
}
completion:^(BOOL finished)
{
if (completionHandler) completionHandler();
if ([IPDFMacSheet currentSheets].count == 1)
{
[IPDFMacSheet setDisableToolbar:NO];
toolbarItemStates = nil;
}
for (UIView *subview in self.subviews) {
[subview removeFromSuperview];
}
[self.backgroundView removeFromSuperview];
[self removeFromSuperview];
}];
}
- (void)layoutSubviews
{
[super layoutSubviews];
if (!self.sizeHandler) return;
CGSize targetSize = self.sizeHandler(self.superview.bounds);
CGRect targetFrame = self.frame;
targetFrame.origin.x = self.superview.bounds.size.width / 2 - targetSize.width/2;
targetFrame.size = targetSize;
self.frame = targetFrame;
}
+ (BOOL)dismissableWithESCKey
{
return YES;
}
static NSMutableArray<IPDFMacSheetToolbarItemState *> *toolbarItemStates;
+ (void)changeStateForToolbarItem:(NSToolbarItem *)toolbarItem stateAfterDismissal:(BOOL)stateAfterDismissal
{
for (IPDFMacSheetToolbarItemState *state in toolbarItemStates)
{
if (state.toolbarItem != toolbarItem) continue;
state.enabled = stateAfterDismissal;
}
}
+ (void)setDisableToolbar:(BOOL)disableToolbar
{
if (!toolbarItemStates)
{
toolbarItemStates = [NSMutableArray new];
}
UIWindowScene *scene = [IPDFMacSheet currentSheet].window.windowScene;
[scene.titlebar.toolbar setDisplayMode:NSToolbarDisplayModeIconAndLabel];
if (!disableToolbar)
{
for (IPDFMacSheetToolbarItemState *state in toolbarItemStates)
{
NSToolbarItem *item = state.toolbarItem;
item.autovalidates = state.autovalidates;
item.enabled = state.enabled;
}
return;
}
for (NSToolbarItem *toolbarItem in scene.titlebar.toolbar.items)
{
BOOL autovalidates = NO;
BOOL enabled = NO;
IPDFMacSheetToolbarItemState *state = [IPDFMacSheetToolbarItemState new];
state.autovalidates = toolbarItem.autovalidates;
state.enabled = toolbarItem.enabled;
state.toolbarItem = toolbarItem;
[toolbarItemStates addObject:state];
toolbarItem.autovalidates = autovalidates;
toolbarItem.enabled = enabled;
}
}
@end