-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTweak.x
More file actions
732 lines (621 loc) · 28.1 KB
/
Tweak.x
File metadata and controls
732 lines (621 loc) · 28.1 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <objc/runtime.h>
#import <objc/message.h>
#import <notify.h>
#import "DINNotificationView.h"
#import "DINPreferences.h"
// ============================================================================
// MARK: - Private Class Interfaces
// ============================================================================
@interface NCNotificationContent : NSObject
- (NSString *)title;
- (NSString *)subtitle;
- (NSString *)message;
@end
@interface NCNotificationRequest : NSObject
- (NSString *)sectionIdentifier;
- (NSString *)notificationIdentifier;
- (NCNotificationContent *)content;
@end
@interface NCNotificationDispatcher : NSObject
- (void)postNotificationWithRequest:(NCNotificationRequest *)request;
@end
@interface SBApplication : NSObject
- (NSString *)displayName;
@end
@interface SBApplicationController : NSObject
+ (instancetype)sharedInstance;
- (SBApplication *)applicationWithBundleIdentifier:(NSString *)bundleIdentifier;
@end
@interface LSApplicationProxy : NSObject
+ (instancetype)applicationProxyForIdentifier:(NSString *)identifier;
- (NSString *)localizedName;
- (NSURL *)bundleURL;
@end
@interface SBSystemApertureContainerView : UIView
@end
// ============================================================================
// MARK: - Rate Limiting
// ============================================================================
static NSDate *sLastNotificationTime = nil;
static NSTimeInterval const kMinNotificationInterval = 1.0;
static BOOL dinShouldThrottle(void) {
if (!sLastNotificationTime) return NO;
return [[NSDate date] timeIntervalSinceDate:sLastNotificationTime] < kMinNotificationInterval;
}
// ============================================================================
// MARK: - App Icon Helper
// ============================================================================
static UIImage *dinAppIcon(NSString *bundleIdentifier) {
if (!bundleIdentifier) return nil;
// Method 1: UIImage private API (most reliable)
@try {
SEL iconSel = sel_registerName("_applicationIconImageForBundleIdentifier:format:scale:");
if ([UIImage respondsToSelector:iconSel]) {
UIImage *img = ((id (*)(Class, SEL, id, int, CGFloat))objc_msgSend)(
[UIImage class], iconSel, bundleIdentifier, 2, UIScreen.mainScreen.scale);
if (img) return img;
}
} @catch (NSException *e) {}
// Method 2: SBIconController → SBIconModel
@try {
Class iconControllerClass = objc_lookUpClass("SBIconController");
if (iconControllerClass) {
id iconController = ((id (*)(Class, SEL))objc_msgSend)(
iconControllerClass, sel_registerName("sharedInstance"));
if (iconController) {
id model = ((id (*)(id, SEL))objc_msgSend)(
iconController, sel_registerName("model"));
if (model) {
id icon = ((id (*)(id, SEL, id))objc_msgSend)(model,
sel_registerName("applicationIconForBundleIdentifier:"),
bundleIdentifier);
if (icon && [icon respondsToSelector:sel_registerName("getIconImage:")]) {
UIImage *image = ((id (*)(id, SEL, int))objc_msgSend)(
icon, sel_registerName("getIconImage:"), 2);
if (image) return image;
}
if (icon && [icon respondsToSelector:sel_registerName("generateIconImage:")]) {
UIImage *image = ((id (*)(id, SEL, int))objc_msgSend)(
icon, sel_registerName("generateIconImage:"), 2);
if (image) return image;
}
}
}
}
} @catch (NSException *e) {}
// Method 3: Load from app bundle
@try {
LSApplicationProxy *proxy = [LSApplicationProxy applicationProxyForIdentifier:bundleIdentifier];
if (proxy) {
NSURL *bundleURL = [proxy bundleURL];
if (bundleURL) {
for (NSString *iconName in @[@"AppIcon60x60@2x.png", @"AppIcon60x60@3x.png",
@"AppIcon76x76@2x.png", @"Icon-60@2x.png", @"Icon-60@3x.png"]) {
UIImage *img = [UIImage imageWithContentsOfFile:
[[bundleURL path] stringByAppendingPathComponent:iconName]];
if (img) return img;
}
}
}
} @catch (NSException *e) {}
return nil;
}
// Generate a placeholder icon with app initial
static UIImage *dinPlaceholderIcon(NSString *appName) {
CGFloat size = 56.0;
UIGraphicsBeginImageContextWithOptions(CGSizeMake(size, size), NO, 0);
[[UIColor colorWithWhite:0.3 alpha:1.0] setFill];
[[UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, size, size) cornerRadius:13] fill];
NSString *initial = (appName.length > 0) ? [appName substringToIndex:1] : @"?";
NSDictionary *attrs = @{
NSFontAttributeName: [UIFont systemFontOfSize:20 weight:UIFontWeightBold],
NSForegroundColorAttributeName: [UIColor whiteColor]
};
CGSize textSize = [initial sizeWithAttributes:attrs];
[initial drawAtPoint:CGPointMake((size - textSize.width) / 2.0,
(size - textSize.height) / 2.0)
withAttributes:attrs];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
// ============================================================================
// MARK: - Video Background Helper
// ============================================================================
static BOOL dinIsVideoFile(NSString *path) {
if (!path) return NO;
NSString *ext = [path.pathExtension lowercaseString];
return [@[@"mp4", @"mov", @"m4v", @"avi", @"mkv"] containsObject:ext];
}
@interface DINVideoBgView : UIView
@property (nonatomic, strong) AVPlayerLayer *playerLayer;
@property (nonatomic, strong) AVPlayerLooper *looper;
@property (nonatomic, strong) AVQueuePlayer *player;
@end
@implementation DINVideoBgView
- (void)layoutSubviews {
[super layoutSubviews];
self.playerLayer.frame = self.bounds;
}
- (void)dealloc {
[self.player pause];
self.player = nil;
self.looper = nil;
}
@end
static UIView *dinCreateVideoBgView(NSString *path, CGFloat opacity) {
// Ensure video playback doesn't interrupt music
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
NSURL *videoURL = [NSURL fileURLWithPath:path];
AVPlayerItem *item = [AVPlayerItem playerItemWithURL:videoURL];
AVQueuePlayer *player = [AVQueuePlayer playerWithPlayerItem:item];
player.muted = YES;
player.preventsDisplaySleepDuringVideoPlayback = NO;
AVPlayerLooper *looper = [AVPlayerLooper playerLooperWithPlayer:player
templateItem:item];
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
playerLayer.opacity = opacity;
DINVideoBgView *view = [[DINVideoBgView alloc] init];
view.translatesAutoresizingMaskIntoConstraints = NO;
view.clipsToBounds = YES;
view.player = player;
view.looper = looper;
view.playerLayer = playerLayer;
[view.layer addSublayer:playerLayer];
[player play];
return view;
}
// ============================================================================
// MARK: - Pass-through Views
// ============================================================================
@interface DINPassthroughView : UIView
@end
@implementation DINPassthroughView
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView *hitView = [super hitTest:point withEvent:event];
return (hitView == self) ? nil : hitView;
}
@end
@interface DINPassthroughViewController : UIViewController
@end
@implementation DINPassthroughViewController
- (void)loadView {
self.view = [[DINPassthroughView alloc] initWithFrame:UIScreen.mainScreen.bounds];
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.view.backgroundColor = [UIColor clearColor];
}
@end
@interface DINPassthroughWindow : UIWindow
@end
@implementation DINPassthroughWindow
- (BOOL)canBecomeKeyWindow { return NO; }
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView *hitView = [super hitTest:point withEvent:event];
if (hitView == self || hitView == self.rootViewController.view) return nil;
return hitView;
}
@end
// ============================================================================
// MARK: - Dynamic Island Overlay Manager
// ============================================================================
@interface DINOverlayManager : NSObject
@property (nonatomic, strong) DINPassthroughWindow *window;
@property (nonatomic, strong) UIView *containerView;
@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) DINNotificationView *notifView;
@property (nonatomic, strong) NSTimer *dismissTimer;
@property (nonatomic, copy) NSString *currentBundleIdentifier;
@property (nonatomic, assign) BOOL showing;
+ (instancetype)sharedInstance;
- (void)showWithTitle:(NSString *)title message:(NSString *)message
appName:(NSString *)appName icon:(UIImage *)icon
bundleIdentifier:(NSString *)bundleIdentifier;
- (void)dismiss;
- (void)openAppAndDismiss;
@end
@implementation DINOverlayManager
+ (instancetype)sharedInstance {
static DINOverlayManager *instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{ instance = [[self alloc] init]; });
return instance;
}
- (CGRect)pillFrame {
CGFloat screenWidth = UIScreen.mainScreen.bounds.size.width;
return CGRectMake((screenWidth - 126.0) / 2.0, 11.0, 126.0, 37.33);
}
- (CGRect)expandedFrameForWidth:(CGFloat)width height:(CGFloat)height {
CGFloat screenWidth = UIScreen.mainScreen.bounds.size.width;
CGFloat w = MIN(width, screenWidth - 16.0);
CGFloat h = MAX(70.0, MIN(height, 160.0));
return CGRectMake((screenWidth - w) / 2.0, 11.0, w, h);
}
- (void)ensureWindow {
if (self.window && self.window.windowScene &&
self.window.windowScene.activationState == UISceneActivationStateUnattached) {
self.window.hidden = YES;
self.window = nil;
self.containerView = nil;
}
if (self.window) return;
UIWindowScene *scene = nil;
for (UIScene *s in UIApplication.sharedApplication.connectedScenes) {
if ([s isKindOfClass:[UIWindowScene class]]) {
UIWindowScene *ws = (UIWindowScene *)s;
if (ws.activationState == UISceneActivationStateForegroundActive) {
scene = ws; break;
}
if (!scene) scene = ws;
}
}
if (scene) {
self.window = [[DINPassthroughWindow alloc] initWithWindowScene:scene];
} else {
self.window = [[DINPassthroughWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
}
self.window.windowLevel = UIWindowLevelStatusBar + 100;
self.window.backgroundColor = [UIColor clearColor];
self.window.rootViewController = [[DINPassthroughViewController alloc] init];
// Container view - starts as Dynamic Island pill shape, invisible
CGRect pill = [self pillFrame];
self.containerView = [[UIView alloc] initWithFrame:pill];
self.containerView.backgroundColor = [UIColor blackColor];
self.containerView.layer.cornerRadius = pill.size.height / 2.0;
self.containerView.layer.cornerCurve = kCACornerCurveContinuous;
self.containerView.clipsToBounds = YES;
self.containerView.alpha = 0; // Hidden at pill size, avoid corner mismatch with real DI
// Border directly on the container layer - matches shape perfectly
self.containerView.layer.borderWidth = 1.5;
self.containerView.layer.borderColor = [UIColor colorWithWhite:1.0 alpha:0.15].CGColor;
[self.window.rootViewController.view addSubview:self.containerView];
// Gestures
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]
initWithTarget:self action:@selector(dismiss)];
swipe.direction = UISwipeGestureRecognizerDirectionUp;
[self.containerView addGestureRecognizer:swipe];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(openAppAndDismiss)];
[self.containerView addGestureRecognizer:tap];
}
- (void)openAppAndDismiss {
if (self.currentBundleIdentifier.length > 0) {
SEL launchSel = sel_registerName("launchApplicationWithIdentifier:suspended:");
id app = [UIApplication sharedApplication];
if ([app respondsToSelector:launchSel]) {
((void (*)(id, SEL, id, BOOL))objc_msgSend)(app, launchSel,
self.currentBundleIdentifier, NO);
}
}
[self dismiss];
}
- (void)showWithTitle:(NSString *)title message:(NSString *)message
appName:(NSString *)appName icon:(UIImage *)icon
bundleIdentifier:(NSString *)bundleIdentifier {
self.currentBundleIdentifier = bundleIdentifier;
[self ensureWindow];
[self.dismissTimer invalidate];
self.dismissTimer = nil;
[self.notifView removeFromSuperview];
[self.bgView removeFromSuperview];
DINPreferences *prefs = [DINPreferences sharedInstance];
// Custom background (image, video, or solid color)
if (prefs.customBackgroundEnabled) {
NSString *bgPath = prefs.customBackgroundImagePath;
if (bgPath && dinIsVideoFile(bgPath)) {
self.bgView = dinCreateVideoBgView(bgPath, prefs.backgroundOpacity);
} else if (bgPath) {
self.bgView = [[UIView alloc] init];
self.bgView.translatesAutoresizingMaskIntoConstraints = NO;
UIImage *bgImage = [UIImage imageWithContentsOfFile:bgPath];
if (bgImage) {
UIImageView *iv = [[UIImageView alloc] initWithImage:bgImage];
iv.contentMode = UIViewContentModeScaleAspectFill;
iv.translatesAutoresizingMaskIntoConstraints = NO;
iv.alpha = prefs.backgroundOpacity;
[self.bgView addSubview:iv];
[NSLayoutConstraint activateConstraints:@[
[iv.topAnchor constraintEqualToAnchor:self.bgView.topAnchor],
[iv.leadingAnchor constraintEqualToAnchor:self.bgView.leadingAnchor],
[iv.trailingAnchor constraintEqualToAnchor:self.bgView.trailingAnchor],
[iv.bottomAnchor constraintEqualToAnchor:self.bgView.bottomAnchor],
]];
}
} else {
self.bgView = [[UIView alloc] init];
self.bgView.translatesAutoresizingMaskIntoConstraints = NO;
self.bgView.backgroundColor = [prefs customBackgroundColor];
}
[self.containerView addSubview:self.bgView];
[NSLayoutConstraint activateConstraints:@[
[self.bgView.topAnchor constraintEqualToAnchor:self.containerView.topAnchor],
[self.bgView.leadingAnchor constraintEqualToAnchor:self.containerView.leadingAnchor],
[self.bgView.trailingAnchor constraintEqualToAnchor:self.containerView.trailingAnchor],
[self.bgView.bottomAnchor constraintEqualToAnchor:self.containerView.bottomAnchor],
]];
}
// Get notification style
NSInteger style = prefs.notificationStyle;
self.notifView = [[DINNotificationView alloc] initWithTitle:title message:message
appName:appName icon:icon
style:(DINNotificationStyle)style];
self.notifView.translatesAutoresizingMaskIntoConstraints = NO;
self.notifView.alpha = 0;
[self.containerView addSubview:self.notifView];
// Layout dimensions per style
CGFloat expandedWidth, expandedHeight, centerYOffset, leadingPad;
switch (style) {
case 1: // Compact
expandedWidth = 300.0;
expandedHeight = 72.0;
centerYOffset = 12.0;
leadingPad = 14.0;
break;
case 2: // Minimal
expandedWidth = 200.0;
expandedHeight = 100.0;
centerYOffset = 14.0;
leadingPad = 16.0;
break;
default: // Standard
expandedWidth = 500.0; // Will be capped to screenWidth - 16
expandedHeight = 89.0;
centerYOffset = 3.0;
leadingPad = 18.0;
break;
}
[NSLayoutConstraint activateConstraints:@[
[self.notifView.centerYAnchor constraintEqualToAnchor:self.containerView.centerYAnchor constant:centerYOffset],
[self.notifView.leadingAnchor constraintEqualToAnchor:self.containerView.leadingAnchor constant:leadingPad],
[self.notifView.trailingAnchor constraintEqualToAnchor:self.containerView.trailingAnchor constant:-leadingPad],
]];
// Start from pill shape
if (!self.showing) {
CGRect pill = [self pillFrame];
self.containerView.frame = pill;
self.containerView.layer.cornerRadius = pill.size.height / 2.0;
self.containerView.alpha = 0;
}
self.window.hidden = NO;
self.showing = YES;
CGRect expandedFrame = [self expandedFrameForWidth:expandedWidth height:expandedHeight];
CGFloat expandedRadius = expandedFrame.size.height / 2.0; // Capsule shape like AirDrop
// Haptic
UIImpactFeedbackGenerator *haptic = [[UIImpactFeedbackGenerator alloc]
initWithStyle:UIImpactFeedbackStyleLight];
[haptic impactOccurred];
// Spring expand animation
[UIView animateWithDuration:0.55 delay:0
usingSpringWithDamping:0.72 initialSpringVelocity:0.8
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
self.containerView.alpha = 1.0;
self.containerView.frame = expandedFrame;
self.containerView.layer.cornerRadius = expandedRadius;
self.notifView.alpha = 1.0;
} completion:nil];
// Auto-dismiss after configured duration
double duration = [DINPreferences sharedInstance].dismissDuration;
if (duration < 1.0) duration = 1.0;
self.dismissTimer = [NSTimer scheduledTimerWithTimeInterval:duration
target:self selector:@selector(dismiss) userInfo:nil repeats:NO];
}
- (void)dismiss {
if (!self.showing) return;
[self.dismissTimer invalidate];
self.dismissTimer = nil;
CGRect pill = [self pillFrame];
CGFloat pillRadius = pill.size.height / 2.0;
// Phase 1: Fade out content smoothly (like system DI)
[UIView animateWithDuration:0.35 delay:0
options:UIViewAnimationOptionCurveEaseIn
animations:^{
self.notifView.alpha = 0;
self.bgView.alpha = 0;
} completion:nil];
// Phase 2: Shrink container back to pill and fade out
[UIView animateWithDuration:0.8 delay:0.1
usingSpringWithDamping:0.9 initialSpringVelocity:0.3
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
self.containerView.frame = pill;
self.containerView.layer.cornerRadius = pillRadius;
} completion:nil];
// Phase 3: Fade out container after shrink starts
[UIView animateWithDuration:0.4 delay:0.35
options:UIViewAnimationOptionCurveEaseIn
animations:^{
self.containerView.alpha = 0;
} completion:^(BOOL finished) {
self.window.hidden = YES;
self.showing = NO;
[self.notifView removeFromSuperview];
[self.bgView removeFromSuperview];
self.notifView = nil;
self.bgView = nil;
}];
}
@end
// ============================================================================
// MARK: - Hooks
// ============================================================================
%hook NCNotificationDispatcher
- (void)postNotificationWithRequest:(NCNotificationRequest *)request {
DINPreferences *prefs = [DINPreferences sharedInstance];
// If tweak disabled or DI notification disabled, use system notification
if (!prefs.enabled || !prefs.notificationEnabled) {
%orig;
return;
}
NCNotificationContent *content = [request content];
if (!content) { %orig; return; }
NSString *title = [content title];
NSString *message = [content message];
if (!title && !message) { %orig; return; }
if (dinShouldThrottle()) {
// Suppress entirely when throttled
return;
}
sLastNotificationTime = [NSDate date];
NSString *bundleIdentifier = [request sectionIdentifier];
NSString *appName = nil;
SBApplicationController *appController =
[objc_lookUpClass("SBApplicationController") sharedInstance];
if (appController && bundleIdentifier) {
SBApplication *app = [appController applicationWithBundleIdentifier:bundleIdentifier];
if (app) appName = [app displayName];
}
if (!appName) {
LSApplicationProxy *proxy =
[LSApplicationProxy applicationProxyForIdentifier:bundleIdentifier];
appName = [proxy localizedName];
}
UIImage *icon = dinAppIcon(bundleIdentifier);
if (!icon) icon = dinPlaceholderIcon(appName);
// Do NOT call %orig — fully suppress system notification (banner + notification center)
// Show DI overlay instead
dispatch_async(dispatch_get_main_queue(), ^{
[[DINOverlayManager sharedInstance] showWithTitle:title message:message
appName:appName icon:icon
bundleIdentifier:bundleIdentifier];
});
}
%end
// ============================================================================
// MARK: - Custom Background + Border for Real Dynamic Island Content
// ============================================================================
static void *kDINCustomBgViewKey = &kDINCustomBgViewKey;
static void *kDINBorderLayerKey = &kDINBorderLayerKey;
%hook SBSystemApertureContainerView
- (id)initWithInterfaceElementIdentifier:(id)identifier {
id result = %orig;
if (!result) return nil;
DINPreferences *prefs = [DINPreferences sharedInstance];
UIView *selfView = (UIView *)result;
if (prefs.customBackgroundEnabled) {
NSString *bgPath = prefs.customBackgroundImagePath;
UIView *customBg;
if (bgPath && dinIsVideoFile(bgPath)) {
customBg = dinCreateVideoBgView(bgPath, prefs.backgroundOpacity);
} else if (bgPath) {
customBg = [[UIView alloc] init];
customBg.translatesAutoresizingMaskIntoConstraints = NO;
customBg.clipsToBounds = YES;
UIImage *bgImage = [UIImage imageWithContentsOfFile:bgPath];
if (bgImage) {
UIImageView *iv = [[UIImageView alloc] initWithImage:bgImage];
iv.contentMode = UIViewContentModeScaleAspectFill;
iv.translatesAutoresizingMaskIntoConstraints = NO;
iv.alpha = prefs.backgroundOpacity;
[customBg addSubview:iv];
[NSLayoutConstraint activateConstraints:@[
[iv.topAnchor constraintEqualToAnchor:customBg.topAnchor],
[iv.leadingAnchor constraintEqualToAnchor:customBg.leadingAnchor],
[iv.trailingAnchor constraintEqualToAnchor:customBg.trailingAnchor],
[iv.bottomAnchor constraintEqualToAnchor:customBg.bottomAnchor],
]];
}
} else {
customBg = [[UIView alloc] init];
customBg.translatesAutoresizingMaskIntoConstraints = NO;
customBg.clipsToBounds = YES;
customBg.backgroundColor = [prefs customBackgroundColor];
}
[selfView insertSubview:customBg atIndex:0];
[NSLayoutConstraint activateConstraints:@[
[customBg.topAnchor constraintEqualToAnchor:selfView.topAnchor],
[customBg.leadingAnchor constraintEqualToAnchor:selfView.leadingAnchor],
[customBg.trailingAnchor constraintEqualToAnchor:selfView.trailingAnchor],
[customBg.bottomAnchor constraintEqualToAnchor:selfView.bottomAnchor],
]];
objc_setAssociatedObject(result, kDINCustomBgViewKey, customBg,
OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
// Create border shape layer (will be updated in layoutSubviews)
if (prefs.enabled) {
CAShapeLayer *borderLayer = [CAShapeLayer layer];
borderLayer.fillColor = [UIColor clearColor].CGColor;
borderLayer.strokeColor = [UIColor colorWithWhite:1.0 alpha:0.15].CGColor;
borderLayer.lineWidth = 1.5;
[selfView.layer addSublayer:borderLayer];
objc_setAssociatedObject(result, kDINBorderLayerKey, borderLayer,
OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
return result;
}
- (void)layoutSubviews {
%orig;
// Clip custom background to match the DI shape (pill or expanded)
UIView *customBg = objc_getAssociatedObject(self, kDINCustomBgViewKey);
if (customBg) {
if (self.layer.mask && [self.layer.mask isKindOfClass:[CAShapeLayer class]]) {
CGPathRef maskPath = ((CAShapeLayer *)self.layer.mask).path;
CAShapeLayer *bgMask = [CAShapeLayer layer];
bgMask.frame = self.bounds;
bgMask.path = maskPath;
customBg.layer.mask = bgMask;
} else {
CGFloat radius = self.layer.cornerRadius;
if (radius <= 0) radius = self.bounds.size.height / 2.0;
customBg.layer.cornerRadius = radius;
customBg.layer.cornerCurve = kCACornerCurveContinuous;
customBg.clipsToBounds = YES;
customBg.layer.mask = nil;
}
// Resume video playback if it was paused during DI state transitions
if ([customBg isKindOfClass:[DINVideoBgView class]]) {
AVQueuePlayer *player = ((DINVideoBgView *)customBg).player;
if (player && player.rate == 0) {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
[player play];
}
}
}
// Update border shape to match the actual DI shape
CAShapeLayer *borderLayer = objc_getAssociatedObject(self, kDINBorderLayerKey);
if (!borderLayer) return;
// Only show border when DI is expanded (not compact pill)
if (self.bounds.size.height < 50.0) {
borderLayer.hidden = YES;
return;
}
borderLayer.hidden = NO;
borderLayer.frame = self.bounds;
// If the view uses a mask layer, copy its path for the border
if (self.layer.mask && [self.layer.mask isKindOfClass:[CAShapeLayer class]]) {
borderLayer.path = ((CAShapeLayer *)self.layer.mask).path;
} else {
CGFloat radius = self.layer.cornerRadius;
if (radius <= 0) radius = self.bounds.size.height / 2.0;
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.bounds
cornerRadius:radius];
borderLayer.path = path.CGPath;
}
}
%end
// ============================================================================
// MARK: - Constructor
// ============================================================================
%ctor {
[[DINPreferences sharedInstance] reloadPreferences];
int token = 0;
notify_register_dispatch("com.34306.shimastyle/prefsChanged",
&token, dispatch_get_main_queue(), ^(int t) {
[[DINPreferences sharedInstance] reloadPreferences];
});
int testToken = 0;
notify_register_dispatch("com.34306.shimastyle/testNotification",
&testToken, dispatch_get_main_queue(), ^(int t) {
UIImage *icon = dinAppIcon(@"com.apple.Preferences");
if (!icon) icon = dinPlaceholderIcon(@"Settings");
[[DINOverlayManager sharedInstance] showWithTitle:@"Test Notification"
message:@"This is a test notification from ShimaStyle"
appName:@"Settings"
icon:icon
bundleIdentifier:@"com.apple.Preferences"];
});
%init;
}