-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChapters.mm
208 lines (176 loc) · 7.41 KB
/
Chapters.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
//
// Chapters.mm | Substrate hooks to get the tweak integrated cleanly.
// Chapters
//
// Add some clean labels to your pages
//
// Created by _kritanta
//
//05
@import UIKit;
#include "include/SpringBoardHome.h"
#include "Chapters/CHPManager.h"
#import <objc/runtime.h>
#include "substrate.h"
@class SBRootFolderController;
@class SBRootIconListView;
@class SBHomeScreenPreviewView;
@class _UIStatusBar;
@class SBIconListView;
static void (*orig_UISB_setForegroundColor) (_UIStatusBar *, SEL, UIColor *);
static void hooked_UISB_setForegroundColor (_UIStatusBar *, SEL, UIColor *);
static struct CGPoint (*orig_12_SBRILV_originForIconAtCoordinate) (SBRootIconListView *, SEL, SBIconCoordinate, struct SBIconListLayoutMetrics);
static struct CGPoint hooked_12_SBRILV_originForIconAtCoordinate (SBRootIconListView *, SEL, SBIconCoordinate, struct SBIconListLayoutMetrics);
static void (*orig_SBILV_layoutIconsNow) (SBIconListView *, SEL);
static void hooked_SBILV_layoutIconsNow (SBIconListView *, SEL);
static struct CGPoint (*orig_SBILV_originForIconAtCoordinate$metrics) (SBIconListView *, SEL, SBIconCoordinate, struct SBIconListLayoutMetrics);
static struct CGPoint hooked_SBILV_originForIconAtCoordinate$metrics (SBIconListView *, SEL, SBIconCoordinate, struct SBIconListLayoutMetrics);
static void (*orig_SBRFC_viewWillAppear) (SBRootFolderController *, SEL, BOOL);
static void hooked_SBRFC_viewWillAppear (SBRootFolderController *, SEL, BOOL);
static void *observer = NULL;
/**
* [_UIStatusBar setForegroundColor:]
*
* iOS already has logic to detect a nice foreground color.
* I can look at directly using it later, but for now, it's nice to just
* snipe the color when apple uses it.
*
* @param color Color for the status bar foreground text and glyphs
* @return void
*/
static void hooked_UISB_setForegroundColor (_UIStatusBar *self, SEL cmd, UIColor *color)
{
[[CHPManager sharedInstance] setLabelColor:color];
orig_UISB_setForegroundColor(self, cmd, color);
[[CHPManager sharedInstance] relayoutIfNeeded];
}
/**
* -[SBRootIconListView originForIconAtCoordinate:metrics:]
*
* Handy little method for manually manipulating icon origin
*
* .iconLocation is an int on iOS 12, so this one needs its own hook
*
* @param coordinate Icon Coordinate
* @param metrics Layout Metrics
* @return CGPoint origin for icon specified
*/
static struct CGPoint hooked_12_SBRILV_originForIconAtCoordinate (SBRootIconListView *self, SEL cmd,
SBIconCoordinate coordinate, struct SBIconListLayoutMetrics metrics)
{
CGPoint o = orig_12_SBRILV_originForIconAtCoordinate(self, cmd, coordinate, metrics);
o.y += (54 + [[prefs valueForKey:@"BottomMargin"] intValue]+ [[prefs valueForKey:@"TopMargin"] intValue]);
return o;
}
/**
* -[SBIconListView layoutIconsNow]
*
* Convenience function added by apple that calls:
* [self setIconsNeedLayout];
* [self layoutIconsIfNeeded:0.0];
*
* iOS doesn't use it much, but my layout editing tweaks do, and this lets us update with them dynamically.
*
* @return void
*/
static void hooked_SBILV_layoutIconsNow (SBIconListView *self, SEL cmd)
{
orig_SBILV_layoutIconsNow(self, cmd);
if (kCFCoreFoundationVersionNumber < 1600 || [self.iconViewProvider class] == objc_getClass("SBHomeScreenPreviewView"))
{
return;
}
if ([self.iconLocation isEqualToString:@"SBIconLocationRoot"])
{
if (@available(iOS 14, *))
self.additionalLayoutInsets = UIEdgeInsetsMake((54 + [[prefs valueForKey:@"BottomMargin"] intValue]+ [[prefs valueForKey:@"TopMargin"] intValue]),0,-(54 + [[prefs valueForKey:@"BottomMargin"] intValue]+ [[prefs valueForKey:@"TopMargin"] intValue]),0);
[[CHPManager sharedInstance] reloadLabelsForRootFolderController:self.iconViewProvider.rootFolderController
index:-1];
}
}
/**
* -[SBIconListView originForIconAtCoordinate:metrics:]
*
* Handy little method for manually manipulating icon origin
*
* Should probably perform modifications to self.layout instead, but I need to figure out how to pull that
* off without screwing up layout managers.
*
* @param coordinate Icon Coordinate
* @param metrics Layout Metrics
* @return CGPoint origin for icon specified
*/
static struct CGPoint hooked_SBILV_originForIconAtCoordinate$metrics (SBIconListView *self, SEL cmd, SBIconCoordinate arg1, struct SBIconListLayoutMetrics arg2)
{
CGPoint o = orig_SBILV_originForIconAtCoordinate$metrics(self, cmd, arg1, arg2);
if ([self.iconViewProvider class] != objc_getClass("SBHomeScreenPreviewView")
&& [self.iconLocation isEqualToString:@"SBIconLocationRoot"]
&& kCFCoreFoundationVersionNumber > 1600
&& kCFCoreFoundationVersionNumber < 1700)
{
o.y += (54 + [[prefs valueForKey:@"BottomMargin"] intValue]+ [[prefs valueForKey:@"TopMargin"] intValue]);
}
return o;
}
/**
* -[SBRootFolderController viewWillAppear:]
*
* Load in our labels whenever the root folder controller appears.
*
* @param yes
*/
static void hooked_SBRFC_viewWillAppear (SBRootFolderController *self, SEL cmd, BOOL yes)
{
orig_SBRFC_viewWillAppear(self, cmd, yes);
@try
{
[[CHPManager sharedInstance] reloadLabelsForRootFolderController:self index:-1];
}
@catch (NSException *ex)
{
return;
}
}
static void preferencesChanged()
{
[[CHPManager sharedInstance] reloadLabelsForRootFolderController:[[[objc_getClass("SBIconController") sharedInstance] iconManager] rootFolderController] index:-1];
[[NSNotificationCenter defaultCenter] postNotificationName:@"HPLayoutIconViews" object:nil];
}
static __attribute__((constructor)) void ChaptersInit (int argc, char **argv, char **envp)
{
NSLog(@"Chapters: Injected");
prefs = [[NSUserDefaults alloc] initWithSuiteName:@"me.kritanta.chapters"];
if ([prefs valueForKey:@"TopMargin"] == nil)
[prefs setValue:0 forKey:@"TopMargin"];
if ([prefs valueForKey:@"BottomMargin"] == nil)
[prefs setValue:0 forKey:@"BottomMargin"];
MSHookMessageEx(objc_getClass("SBRootFolderController"),
@selector(viewWillAppear:),
(IMP) &hooked_SBRFC_viewWillAppear,
(IMP *) &orig_SBRFC_viewWillAppear);
MSHookMessageEx(objc_getClass("SBRootIconListView"),
@selector(originForIconAtCoordinate:metrics:),
(IMP) &hooked_12_SBRILV_originForIconAtCoordinate,
(IMP *) &orig_12_SBRILV_originForIconAtCoordinate);
MSHookMessageEx(objc_getClass("SBIconListView"),
@selector(layoutIconsNow),
(IMP) &hooked_SBILV_layoutIconsNow,
(IMP *) &orig_SBILV_layoutIconsNow);
if (kCFCoreFoundationVersionNumber < 1700)
MSHookMessageEx(objc_getClass("SBIconListView"),
@selector(originForIconAtCoordinate:metrics:),
(IMP) &hooked_SBILV_originForIconAtCoordinate$metrics,
(IMP *) &orig_SBILV_originForIconAtCoordinate$metrics);
MSHookMessageEx(objc_getClass("_UIStatusBar"),
@selector(setForegroundColor:),
(IMP) &hooked_UISB_setForegroundColor,
(IMP *) &orig_UISB_setForegroundColor);
CFNotificationCenterAddObserver(
CFNotificationCenterGetDarwinNotifyCenter(),
&observer,
(CFNotificationCallback)preferencesChanged,
(CFStringRef)@"me.krit.chapters/prefs",
NULL,
CFNotificationSuspensionBehaviorDeliverImmediately
);
}