Repository navigation
Expand file tree
/
Copy pathCSLModuleIcon.m
More file actions
285 lines (262 loc) · 14 KB
/
Copy pathCSLModuleIcon.m
File metadata and controls
285 lines (262 loc) · 14 KB
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#import "CSLModuleIcon.h"
#import "CSLDiagnostics.h"
#import "CSLShortcutIcon.h"
#import "CSLSlotPreferences.h"
static NSString *const CSLModuleIconIdentifierKey = @"identifier";
static NSString *const CSLModuleIconVietnameseNameKey = @"vi";
static NSString *const CSLModuleIconEnglishNameKey = @"en";
NSArray<NSDictionary<NSString *, NSString *> *> *CSLModuleIconCatalog(void) {
static NSArray<NSDictionary<NSString *, NSString *> *> *catalog;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
catalog = @[
@{CSLModuleIconIdentifierKey: @"sparkles", CSLModuleIconVietnameseNameKey: @"Lấp lánh", CSLModuleIconEnglishNameKey: @"Sparkles"},
@{CSLModuleIconIdentifierKey: @"bookmark", CSLModuleIconVietnameseNameKey: @"Dấu trang", CSLModuleIconEnglishNameKey: @"Bookmark"},
@{CSLModuleIconIdentifierKey: @"phone", CSLModuleIconVietnameseNameKey: @"Điện thoại", CSLModuleIconEnglishNameKey: @"Phone"},
@{CSLModuleIconIdentifierKey: @"camera", CSLModuleIconVietnameseNameKey: @"Máy ảnh", CSLModuleIconEnglishNameKey: @"Camera"},
@{CSLModuleIconIdentifierKey: @"chat", CSLModuleIconVietnameseNameKey: @"Trò chuyện", CSLModuleIconEnglishNameKey: @"Chat"},
@{CSLModuleIconIdentifierKey: @"chats", CSLModuleIconVietnameseNameKey: @"Hội thoại", CSLModuleIconEnglishNameKey: @"Chats"},
@{CSLModuleIconIdentifierKey: @"message", CSLModuleIconVietnameseNameKey: @"Tin nhắn", CSLModuleIconEnglishNameKey: @"Message"},
@{CSLModuleIconIdentifierKey: @"crown", CSLModuleIconVietnameseNameKey: @"Vương miện", CSLModuleIconEnglishNameKey: @"Crown"},
@{CSLModuleIconIdentifierKey: @"trash", CSLModuleIconVietnameseNameKey: @"Thùng rác", CSLModuleIconEnglishNameKey: @"Trash"},
@{CSLModuleIconIdentifierKey: @"compass", CSLModuleIconVietnameseNameKey: @"La bàn", CSLModuleIconEnglishNameKey: @"Compass"},
@{CSLModuleIconIdentifierKey: @"note", CSLModuleIconVietnameseNameKey: @"Ghi chú", CSLModuleIconEnglishNameKey: @"Note"},
@{CSLModuleIconIdentifierKey: @"diamond", CSLModuleIconVietnameseNameKey: @"Kim cương", CSLModuleIconEnglishNameKey: @"Diamond"},
@{CSLModuleIconIdentifierKey: @"edit", CSLModuleIconVietnameseNameKey: @"Chỉnh sửa", CSLModuleIconEnglishNameKey: @"Edit"},
@{CSLModuleIconIdentifierKey: @"wallet", CSLModuleIconVietnameseNameKey: @"Ví", CSLModuleIconEnglishNameKey: @"Wallet"},
@{CSLModuleIconIdentifierKey: @"document", CSLModuleIconVietnameseNameKey: @"Tài liệu", CSLModuleIconEnglishNameKey: @"Document"},
@{CSLModuleIconIdentifierKey: @"flag", CSLModuleIconVietnameseNameKey: @"Lá cờ", CSLModuleIconEnglishNameKey: @"Flag"},
@{CSLModuleIconIdentifierKey: @"photo", CSLModuleIconVietnameseNameKey: @"Hình ảnh", CSLModuleIconEnglishNameKey: @"Photo"},
@{CSLModuleIconIdentifierKey: @"gift", CSLModuleIconVietnameseNameKey: @"Quà tặng", CSLModuleIconEnglishNameKey: @"Gift"},
@{CSLModuleIconIdentifierKey: @"help", CSLModuleIconVietnameseNameKey: @"Trợ giúp", CSLModuleIconEnglishNameKey: @"Help"},
@{CSLModuleIconIdentifierKey: @"home", CSLModuleIconVietnameseNameKey: @"Nhà", CSLModuleIconEnglishNameKey: @"Home"},
@{CSLModuleIconIdentifierKey: @"translate", CSLModuleIconVietnameseNameKey: @"Dịch", CSLModuleIconEnglishNameKey: @"Translate"},
@{CSLModuleIconIdentifierKey: @"heart", CSLModuleIconVietnameseNameKey: @"Trái tim", CSLModuleIconEnglishNameKey: @"Heart"},
@{CSLModuleIconIdentifierKey: @"lock", CSLModuleIconVietnameseNameKey: @"Khóa", CSLModuleIconEnglishNameKey: @"Lock"},
@{CSLModuleIconIdentifierKey: @"unlock", CSLModuleIconVietnameseNameKey: @"Mở khóa", CSLModuleIconEnglishNameKey: @"Unlock"},
@{CSLModuleIconIdentifierKey: @"mail", CSLModuleIconVietnameseNameKey: @"Thư", CSLModuleIconEnglishNameKey: @"Mail"},
@{CSLModuleIconIdentifierKey: @"megaphone", CSLModuleIconVietnameseNameKey: @"Loa thông báo", CSLModuleIconEnglishNameKey: @"Megaphone"},
@{CSLModuleIconIdentifierKey: @"microphone", CSLModuleIconVietnameseNameKey: @"Micrô", CSLModuleIconEnglishNameKey: @"Microphone"},
@{CSLModuleIconIdentifierKey: @"music", CSLModuleIconVietnameseNameKey: @"Âm nhạc", CSLModuleIconEnglishNameKey: @"Music"},
@{CSLModuleIconIdentifierKey: @"location", CSLModuleIconVietnameseNameKey: @"Vị trí", CSLModuleIconEnglishNameKey: @"Location"},
@{CSLModuleIconIdentifierKey: @"play", CSLModuleIconVietnameseNameKey: @"Phát", CSLModuleIconEnglishNameKey: @"Play"},
@{CSLModuleIconIdentifierKey: @"briefcase", CSLModuleIconVietnameseNameKey: @"Công việc", CSLModuleIconEnglishNameKey: @"Briefcase"},
@{CSLModuleIconIdentifierKey: @"book", CSLModuleIconVietnameseNameKey: @"Sách", CSLModuleIconEnglishNameKey: @"Book"},
@{CSLModuleIconIdentifierKey: @"trash-slash", CSLModuleIconVietnameseNameKey: @"Không xóa", CSLModuleIconEnglishNameKey: @"No Trash"},
@{CSLModuleIconIdentifierKey: @"search", CSLModuleIconVietnameseNameKey: @"Tìm kiếm", CSLModuleIconEnglishNameKey: @"Search"},
@{CSLModuleIconIdentifierKey: @"send", CSLModuleIconVietnameseNameKey: @"Gửi", CSLModuleIconEnglishNameKey: @"Send"},
@{CSLModuleIconIdentifierKey: @"settings", CSLModuleIconVietnameseNameKey: @"Cài đặt", CSLModuleIconEnglishNameKey: @"Settings"},
@{CSLModuleIconIdentifierKey: @"star", CSLModuleIconVietnameseNameKey: @"Ngôi sao", CSLModuleIconEnglishNameKey: @"Star"},
@{CSLModuleIconIdentifierKey: @"tag", CSLModuleIconVietnameseNameKey: @"Thẻ", CSLModuleIconEnglishNameKey: @"Tag"},
@{CSLModuleIconIdentifierKey: @"shirt", CSLModuleIconVietnameseNameKey: @"Áo", CSLModuleIconEnglishNameKey: @"Shirt"},
@{CSLModuleIconIdentifierKey: @"fire", CSLModuleIconVietnameseNameKey: @"Ngọn lửa", CSLModuleIconEnglishNameKey: @"Fire"},
@{CSLModuleIconIdentifierKey: @"video", CSLModuleIconVietnameseNameKey: @"Video", CSLModuleIconEnglishNameKey: @"Video"},
@{CSLModuleIconIdentifierKey: @"video-slash", CSLModuleIconVietnameseNameKey: @"Tắt video", CSLModuleIconEnglishNameKey: @"Video Off"},
@{CSLModuleIconIdentifierKey: @"eye", CSLModuleIconVietnameseNameKey: @"Mắt", CSLModuleIconEnglishNameKey: @"Eye"},
@{CSLModuleIconIdentifierKey: @"hourglass", CSLModuleIconVietnameseNameKey: @"Đồng hồ cát", CSLModuleIconEnglishNameKey: @"Hourglass"},
];
});
return catalog;
}
static NSDictionary<NSString *, NSString *> *CSLModuleIconEntry(
NSString *identifier
) {
if (identifier.length == 0) {
return nil;
}
for (NSDictionary<NSString *, NSString *> *entry in CSLModuleIconCatalog()) {
if ([entry[CSLModuleIconIdentifierKey] isEqualToString:identifier]) {
return entry;
}
}
return nil;
}
BOOL CSLModuleIconIdentifierIsValid(NSString *identifier) {
return CSLModuleIconEntry(identifier) != nil;
}
NSString *CSLModuleIconDisplayName(NSString *identifier) {
NSDictionary<NSString *, NSString *> *entry = CSLModuleIconEntry(identifier);
if (entry == nil) {
return nil;
}
return CSLUsesVietnamese()
? entry[CSLModuleIconVietnameseNameKey]
: entry[CSLModuleIconEnglishNameKey];
}
static NSBundle *CSLModuleIconResourceBundle(void) {
Class ownerClass = NSClassFromString(@"CCShortcutLauncherProvider");
if (ownerClass == Nil) {
ownerClass = NSClassFromString(@"CSLRootListController");
}
if (ownerClass == Nil) {
ownerClass = NSClassFromString(@"CSLShortcutOrderController");
}
return ownerClass != Nil ? [NSBundle bundleForClass:ownerClass] : NSBundle.mainBundle;
}
static UIImage *CSLDefaultGridIcon(CGSize size) {
CGFloat pointSize = MAX(14.0, MIN(size.width, size.height) * 0.72);
UIImageSymbolConfiguration *configuration =
[UIImageSymbolConfiguration configurationWithPointSize:pointSize
weight:UIImageSymbolWeightRegular];
return [UIImage systemImageNamed:@"square.grid.2x2"
withConfiguration:configuration];
}
static UIImage *CSLScaledModuleIcon(UIImage *source, CGSize size) {
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
[source drawInRect:CGRectMake(0.0, 0.0, size.width, size.height)];
UIImage *scaled = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaled;
}
/// Produces real RGBA pixels with the source alpha preserved. UIKit's tinting
/// implementation is used before scaling; filling a UIGraphics context with
/// SourceIn proved unreliable in Preferences and could turn the whole canvas
/// into an opaque square.
static UIImage *CSLTintedModuleIcon(UIImage *source, CGSize size, UIColor *color) {
UIImage *tintedSource = [source imageWithTintColor:color
renderingMode:UIImageRenderingModeAlwaysOriginal];
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
CGRect bounds = CGRectMake(0.0, 0.0, size.width, size.height);
[tintedSource drawInRect:bounds];
UIImage *tinted = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return [tinted imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
/// CCSupport calls LICreateIconForImage on provider icons. A glyph-only image
/// has too little opaque artwork for that app-icon compositor and can turn
/// into a plain black tile. Give it a complete, static-colour tile instead.
static UIImage *CSLControlCenterSettingsTile(UIImage *glyph, CGSize size) {
if (glyph == nil || size.width <= 0.0 || size.height <= 0.0) {
return nil;
}
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
CGRect bounds = CGRectMake(0.0, 0.0, size.width, size.height);
CGFloat cornerRadius = MIN(size.width, size.height) * 0.22;
UIBezierPath *background =
[UIBezierPath bezierPathWithRoundedRect:bounds cornerRadius:cornerRadius];
[[UIColor colorWithRed:88.0 / 255.0
green:86.0 / 255.0
blue:214.0 / 255.0
alpha:1.0] setFill];
[background fill];
CGFloat inset = MIN(size.width, size.height) * 0.18;
CGRect glyphRect = CGRectInset(bounds, inset, inset);
UIImage *whiteGlyph = CSLTintedModuleIcon(
glyph,
glyphRect.size,
UIColor.whiteColor
);
[whiteGlyph drawInRect:glyphRect];
UIImage *tile = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return [tile imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
UIImage *CSLModulePackIcon(
NSString *identifier,
CGSize size,
UIColor *tintColor,
BOOL originalRendering
) {
if (!CSLModuleIconIdentifierIsValid(identifier)) {
return nil;
}
NSString *resourceName = [NSString stringWithFormat:@"CSLModuleIcon.%@.png", identifier];
NSBundle *bundle = CSLModuleIconResourceBundle();
UIImage *source = [UIImage imageNamed:resourceName
inBundle:bundle
compatibleWithTraitCollection:nil];
if (source == nil) {
NSLog(@"[CCShortcutLauncher][Icon] MODULE_PACK_RESOURCE_MISSING identifier=%@ bundle=%@",
identifier,
bundle.bundlePath);
return nil;
}
CSLIconDiagnosticLog(
@"[CCShortcutLauncher][Icon] MODULE_PACK_RENDERED identifier=%@ bundle=%@ size=%.0fx%.0f original=%d",
identifier,
bundle.bundlePath,
size.width,
size.height,
originalRendering
);
if (!originalRendering) {
return [CSLScaledModuleIcon(source, size)
imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
}
return CSLTintedModuleIcon(
source,
size,
tintColor ?: [UIColor colorWithWhite:0.58 alpha:1.0]
);
}
UIImage *CSLModuleTemplateGlyphForSlot(
NSUInteger slot,
NSArray<NSDictionary<NSString *, id> *> *entries,
CGSize size
) {
if (entries.count == 1) {
UIImage *shortcutGlyph = CSLShortcutGlyphTemplateImageForEntry(
entries.firstObject,
size
);
if (shortcutGlyph != nil) {
return shortcutGlyph;
}
} else if (entries.count > 1) {
UIImage *selected = CSLModulePackIcon(
CSLSlotIconName(slot),
size,
nil,
NO
);
if (selected != nil) {
return selected;
}
}
return [CSLDefaultGridIcon(size) imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
}
UIImage *CSLModuleSettingsIconForSlot(
NSUInteger slot,
NSArray<NSDictionary<NSString *, id> *> *entries,
CGSize size
) {
CSLIconDiagnosticLog(
@"[CCShortcutLauncher][Icon] SETTINGS_ROUTE slot=%lu entries=%lu selected=%@",
(unsigned long)slot,
(unsigned long)entries.count,
CSLSlotIconName(slot) ?: @"default"
);
return CSLModuleControlCenterSettingsIconForSlot(slot, entries, size);
}
UIImage *CSLModuleControlCenterSettingsIconForSlot(
NSUInteger slot,
NSArray<NSDictionary<NSString *, id> *> *entries,
CGSize size
) {
if (entries.count == 1) {
UIImage *shortcutIcon = CSLShortcutIconImageForEntry(entries.firstObject, size);
if (shortcutIcon != nil) {
return shortcutIcon;
}
}
UIImage *glyph = nil;
if (entries.count > 1) {
glyph = CSLModulePackIcon(
CSLSlotIconName(slot),
size,
nil,
NO
);
}
if (glyph == nil) {
glyph = [CSLDefaultGridIcon(size)
imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
}
CSLIconDiagnosticLog(
@"[CCShortcutLauncher][Icon] CONTROL_CENTER_TILE slot=%lu entries=%lu selected=%@",
(unsigned long)slot,
(unsigned long)entries.count,
CSLSlotIconName(slot) ?: @"default"
);
return CSLControlCenterSettingsTile(glyph, size);
}