Skip to content

Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.

C

fix: AirPods gestures stop listening on Cancel and hide Forget until learned

My-Name-Is-Jeff083bbaa0b5f3
diff --git a/docs/tweaks.md b/docs/tweaks.md
index d155042e..65076d76 100644
--- a/docs/tweaks.md
+++ b/docs/tweaks.md
@@ -187,7 +187,8 @@ Shared:
Spotify's collection platform (addURL:showUIConfirmation:completion:), and a shake, which skips; a
tone played through Spotify's playback session confirms each, whatever the Ring/Silent switch says.
Learning one's own nod and shake sets the thresholds, each kept only if the detector, set to it,
- finds the gesture in what was recorded. The one CMHeadphoneMotionManager is lent to other features
+ finds the gesture in what was recorded; its Cancel stops listening at once, and Forget shows only
+ while something is learned. The one CMHeadphoneMotionManager is lent to other features
(SGHeadMotionListen: Sing's spatial voice), and runs for them with the switch off, the detector then
unfed. The detector is
tested on the Mac against harness/head-gestures/, the hook and the page in the simulator against its sim/
diff --git a/harness/head-gestures/README.md b/harness/head-gestures/README.md
index 4e287ff2..a0f5ebab 100644
--- a/harness/head-gestures/README.md
+++ b/harness/head-gestures/README.md
@@ -21,9 +21,10 @@ The real `HeadGestures.x` (through Logos), its settings page and the detector, w
platform stood in for and `CMHeadphoneMotionManager`'s start and stop taken over, so made-up motion reaches the
handler on the manager's own queue. It checks that nothing listens until the switch is on, a double nod adds the
playing track with Spotify's toast, a shake skips, an episode is not saved, pausing stops, learning listens while
-paused and stores what it learned, learning with no motion says so, a learning alert's Cancel keeps the nod
-that was stored (and a second Learn meanwhile says it is still listening), and Forget asks before it clears
-both, and says nothing is learned when nothing is.
+paused and stores what it learned, learning with no motion says so, a learning alert's Cancel ends the
+listening at once (but for another feature's, through SGHeadMotionListen) and keeps the nod that was stored, a
+second Learn straight after starts at once, Forget shows only while something is learned, and asks before it
+clears both.
THEOS=$HOME/theos sim/build.sh && xcrun simctl install <udid> build/sim/HeadGesturesSim.app
xcrun simctl launch <udid> com.vitrine.headgesturessim # then read the [harness] lines from the log
@@ -31,3 +32,5 @@ both, and says nothing is learned when nothing is.
2026-10-05, iPhone 17 Pro on iOS 27.0: all 15 checks pass, three runs in a row, with the cue's tones played
through AVAudioPlayer (the harness checks the calls, not the sound). With Cancel and Forget, all 24 pass; a simulator
slow enough to hold the alert's presentation for seconds makes the timed steps fail.
+Since Cancel ends learning at once and Forget shows only while something is learned: all 30 pass, three runs in a
+row, on the same simulator.
diff --git a/harness/head-gestures/sim/main.m b/harness/head-gestures/sim/main.m
index f4821fba..80684403 100644
--- a/harness/head-gestures/sim/main.m
+++ b/harness/head-gestures/sim/main.m
@@ -200,6 +200,7 @@ static NSInteger sg_nodBefore;
},
^{
check(sg_active, @"switch on, playing: listening");
+ check([self.table numberOfRowsInSection:1] == 2, @"nothing learned: no Forget row");
doubleNod(0.35, 0.4);
},
^{
@@ -239,6 +240,7 @@ static NSInteger sg_nodBefore;
check([[self alertTitle] isEqualToString:@"Learned"], [NSString stringWithFormat:@"after 4 s it says %@", [self alertTitle]]);
check(SGInt(SGKeyHeadNod, 0) > 0, [NSString stringWithFormat:@"stored nod %ld", (long)SGInt(SGKeyHeadNod, 0)]);
check(!sg_active, @"learning over, paused: stopped");
+ check([self.table numberOfRowsInSection:1] == 3, @"a nod learned: the Forget row shows");
[self dismissAlert];
[self.table reloadData];
[self setPlaying:YES];
@@ -260,8 +262,9 @@ static NSInteger sg_nodBefore;
[self.table reloadData];
},
^{
- // Learning the nod again, cancelled while a nod comes in.
+ // Learning the nod again, cancelled while a nod comes in, with another feature listening to the motion.
sg_nodBefore = SGInt(SGKeyHeadNod, 0);
+ SGHeadMotionListen(@"harness", ^(CMDeviceMotion *motion) {});
[self.table.delegate tableView:self.table didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
},
^{
@@ -271,16 +274,21 @@ static NSInteger sg_nodBefore;
},
^{
check([self alertTitle] == nil, [NSString stringWithFormat:@"Cancel takes the alert away: %@", [self alertTitle]]);
+ check(SGInt(SGKeyHeadNod, 0) == sg_nodBefore, [NSString stringWithFormat:@"a cancelled try keeps the nod %ld, stored %ld", (long)sg_nodBefore, (long)SGInt(SGKeyHeadNod, 0)]);
+ check(sg_active, @"cancelled, paused, another feature listening: the motion runs on for it");
+ SGHeadMotionListen(@"harness", nil);
+ check(!sg_active, @"that feature gone too: stopped");
[self.table.delegate tableView:self.table didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
},
^{
- check([[self alertTitle] isEqualToString:@"Still listening"], [NSString stringWithFormat:@"learning again before the cancelled one ends says %@", [self alertTitle]]);
- [self dismissAlert];
+ check([[self alertTitle] isEqualToString:@"Nod twice"], [NSString stringWithFormat:@"learning again straight after Cancel asks: %@", [self alertTitle]]);
+ check(sg_active, @"learning again: listening");
+ [self press:UIAlertActionStyleCancel];
},
- ^{}, ^{},
+ ^{}, ^{}, ^{}, ^{},
^{
- check([self alertTitle] == nil, [NSString stringWithFormat:@"a cancelled try says nothing when it ends: %@", [self alertTitle]]);
- check(SGInt(SGKeyHeadNod, 0) == sg_nodBefore, [NSString stringWithFormat:@"a cancelled try keeps the nod %ld, stored %ld", (long)sg_nodBefore, (long)SGInt(SGKeyHeadNod, 0)]);
+ check([self alertTitle] == nil, [NSString stringWithFormat:@"the cancelled tries say nothing when their 4 s end: %@", [self alertTitle]]);
+ check(SGInt(SGKeyHeadNod, 0) == sg_nodBefore, [NSString stringWithFormat:@"and store nothing: nod %ld", (long)SGInt(SGKeyHeadNod, 0)]);
check(!sg_active, @"cancelled learning over, paused: stopped");
[self.table.delegate tableView:self.table didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:1]];
},
@@ -290,11 +298,7 @@ static NSInteger sg_nodBefore;
},
^{
check(SGInt(SGKeyHeadNod, 0) == 0 && SGInt(SGKeyHeadShake, 0) == 0, @"Forget clears both");
- [self.table.delegate tableView:self.table didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:1]];
- },
- ^{
- check([[self alertTitle] isEqualToString:@"Nothing learned yet"], [NSString stringWithFormat:@"Forget with nothing learned says %@", [self alertTitle]]);
- [self dismissAlert];
+ check([self.table numberOfRowsInSection:1] == 2, @"nothing learned again: the Forget row goes");
},
^{
NSLog(@"[harness] %@", failures ? [NSString stringWithFormat:@"%d failed", failures] : @"all passed");
diff --git a/tweak/Sources/Settings/SGModPage.h b/tweak/Sources/Settings/SGModPage.h
index 039daf58..fd33f529 100644
--- a/tweak/Sources/Settings/SGModPage.h
+++ b/tweak/Sources/Settings/SGModPage.h
@@ -26,8 +26,9 @@
@property (nonatomic) BOOL glows;
// An ⓘ button beside the row's switch, whose tap reads this out under the row's title.
@property (nonatomic, copy) NSString *info;
-// The row shows only while this answers YES, asked again whenever a switch on the page is flipped or the
-// page comes back from a choice's list: the row fades in or out where it sits. Nil shows it always.
+// The row shows only while this answers YES, asked again whenever a switch on the page is flipped, the
+// page comes back from a choice's list or it is told to (-refreshVisibility): the row fades in or out where
+// it sits. Nil shows it always.
@property (nonatomic, copy) BOOL (^visible)(void);
// A choice row's: a line under each name in its list, in the same order, and what runs once one is stored.
@property (nonatomic, copy) NSArray<NSString *> *choiceNotes;
@@ -49,6 +50,8 @@
@interface SGModPage : SGPage
- (instancetype)initWithTitle:(NSString *)title intro:(NSString *)intro sections:(NSArray<SGModSection *> *)sections footer:(NSString *)footer;
+// Asks every row's `visible` again, after something other than a switch on the page changed what it reads.
+- (void)refreshVisibility;
@end
// The intro of every page whose switches the hooks read at launch.
diff --git a/tweak/Sources/Settings/SGModPage.m b/tweak/Sources/Settings/SGModPage.m
index 81238574..a99dfd36 100644
--- a/tweak/Sources/Settings/SGModPage.m
+++ b/tweak/Sources/Settings/SGModPage.m
@@ -474,6 +474,10 @@ static const CGFloat kSliderTop = 12, kSliderLine = 18, kSliderSubtitle = 14, kS
return YES;
}
+- (void)refreshVisibility {
+ [self showRowsThen:nil];
+}
+
// The row a switch or an ⓘ belongs to, by the cell it sits in: rows coming and going move the rows under
// them, so a position remembered when the cell was made may be stale.
- (NSIndexPath *)pathOf:(UIView *)control {
diff --git a/tweak/Sources/Shared/HeadGestures/HeadGestures.h b/tweak/Sources/Shared/HeadGestures/HeadGestures.h
index 9ac28283..68e181d3 100644
--- a/tweak/Sources/Shared/HeadGestures/HeadGestures.h
+++ b/tweak/Sources/Shared/HeadGestures/HeadGestures.h
@@ -32,6 +32,9 @@ BOOL SGHeadGesturesAvailable(void);
// Listens for `seconds` whatever plays and answers the threshold learned for `axis` (0 when the motion held
// no gesture) and how many samples came (0 when no headphones sent any, or motion is not allowed).
void SGHeadGesturesLearn(SGHeadAxis axis, double seconds, void (^done)(double threshold, NSInteger samples));
+// Ends the learning that listens now, at once: nothing is stored, its done is never called, and the motion stops
+// unless the gestures or another feature want it. A new learning can start straight after.
+void SGHeadGesturesCancelLearn(void);
UIViewController *SGHeadGesturesSettingsPage(void);
diff --git a/tweak/Sources/Shared/HeadGestures/HeadGestures.x b/tweak/Sources/Shared/HeadGestures/HeadGestures.x
index 9a3fa867..9b70277c 100644
--- a/tweak/Sources/Shared/HeadGestures/HeadGestures.x
+++ b/tweak/Sources/Shared/HeadGestures/HeadGestures.x
@@ -40,6 +40,7 @@ static NSOperationQueue *sg_queue; // serial: the detector, the learning buffe
static SGHeadDetector sg_detector;
static NSMutableData *sg_learned; // learning's samples, three doubles each
static BOOL sg_learning;
+static NSUInteger sg_learnTry; // which learning is current: a cancelled one's timer finds it moved on
static BOOL sg_listening; // started with the handler, by updateListening
static BOOL sg_detecting; // the gestures want the motion: learning, or the switch on and Spotify playing
static BOOL sg_queueDetecting; // sg_detecting, on sg_queue
@@ -313,12 +314,14 @@ void SGHeadGesturesLearn(SGHeadAxis axis, double seconds, void (^done)(double th
return;
}
sg_learning = YES;
+ NSUInteger ticket = ++sg_learnTry;
manager();
// The check after learning runs with the other gesture as it is set now, at a sensitivity of 100%.
double other = axis == SGHeadAxisPitch ? learnedOr(SGKeyHeadShake, SGHeadDefaultShake) : learnedOr(SGKeyHeadNod, SGHeadDefaultNod);
[sg_queue addOperationWithBlock:^{ sg_learned = [NSMutableData data]; }];
SGHeadGesturesSettingsChanged();
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(seconds * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
+ if (ticket != sg_learnTry) return;
[sg_queue addOperationWithBlock:^{
NSData *data = sg_learned;
sg_learned = nil;
@@ -335,6 +338,7 @@ void SGHeadGesturesLearn(SGHeadAxis axis, double seconds, void (^done)(double th
free(pitch);
free(yaw);
dispatch_async(dispatch_get_main_queue(), ^{
+ if (ticket != sg_learnTry) return;
SGLog(@"head gestures: learned %.2f rad/s for %@ from %d samples", learned, axis == SGHeadAxisPitch ? @"nod" : @"shake", count);
if (learned > 0) SGSetInt(axis == SGHeadAxisPitch ? SGKeyHeadNod : SGKeyHeadShake, lround(learned * kMilli));
sg_learning = NO;
@@ -345,6 +349,15 @@ void SGHeadGesturesLearn(SGHeadAxis axis, double seconds, void (^done)(double th
});
}
+void SGHeadGesturesCancelLearn(void) {
+ if (!sg_learning) return;
+ sg_learning = NO;
+ sg_learnTry++;
+ [sg_queue addOperationWithBlock:^{ sg_learned = nil; }];
+ SGLog(@"head gestures: learning cancelled");
+ SGHeadGesturesSettingsChanged();
+}
+
// Playing or not, from the one player hook every feature shares.
@interface SGHeadGesturesObserver : NSObject <SGPlayerStateObserver>
@end
diff --git a/tweak/Sources/Shared/HeadGestures/HeadGesturesSettings.m b/tweak/Sources/Shared/HeadGestures/HeadGesturesSettings.m
index e97c14ab..3f4bd4b8 100644
--- a/tweak/Sources/Shared/HeadGestures/HeadGesturesSettings.m
+++ b/tweak/Sources/Shared/HeadGestures/HeadGesturesSettings.m
@@ -18,38 +18,30 @@ static void say(NSString *title, NSString *message) {
[SGTopController() presentViewController:alert animated:YES completion:nil];
}
-// Set while learning listens, a cancelled try too: SGHeadGesturesLearn runs its seconds out either way.
-static BOOL sg_listening;
+static BOOL anythingLearned(void) {
+ return SGInt(SGKeyHeadNod, 0) > 0 || SGInt(SGKeyHeadShake, 0) > 0;
+}
-static void learn(SGHeadAxis axis) {
+// `done` runs once something may be stored, before the alert that says what: the page shows Forget then.
+static void learn(SGHeadAxis axis, void (^done)(void)) {
if (!SGHeadGesturesAvailable()) {
say(@"No head tracking", @"This iPhone cannot read the motion of headphones.");
return;
}
- if (sg_listening) {
- say(@"Still listening", @"The last try is finishing. Try again in a few seconds.");
- return;
- }
BOOL nod = axis == SGHeadAxisPitch;
- NSString *key = nod ? SGKeyHeadNod : SGKeyHeadShake;
- // SGHeadGesturesLearn stores what it found itself, so a cancelled try puts the old size back.
- NSInteger before = SGInt(key, 0);
+ // Cancel tapped while the alert was still coming in: learning never starts.
__block BOOL cancelled = NO;
UIAlertController *asking = [UIAlertController alertControllerWithTitle:nod ? @"Nod twice" : @"Shake your head"
message:nod ? @"Now, the way you would to like a song." : @"Now, the way you would to skip a song."
preferredStyle:UIAlertControllerStyleAlert];
- [asking addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { cancelled = YES; }]];
+ [asking addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
+ cancelled = YES;
+ SGHeadGesturesCancelLearn();
+ }]];
[SGTopController() presentViewController:asking animated:YES completion:^{
- sg_listening = YES;
+ if (cancelled) return;
SGHeadGesturesLearn(axis, kLearnSeconds, ^(double threshold, NSInteger samples) {
- sg_listening = NO;
- if (cancelled) {
- if (SGInt(key, 0) != before) {
- SGSetInt(key, before);
- SGHeadGesturesSettingsChanged();
- }
- return;
- }
+ done();
[asking dismissViewControllerAnimated:YES completion:^{
if (samples == 0)
say(@"No motion came", @"Put in AirPods that track head motion (AirPods Pro, AirPods 3 or later, "
@@ -64,18 +56,15 @@ static void learn(SGHeadAxis axis) {
}];
}
-// Both learned sizes back to the defaults, asked first. With nothing learned there is nothing to ask.
-static void forget(void) {
- if (SGInt(SGKeyHeadNod, 0) <= 0 && SGInt(SGKeyHeadShake, 0) <= 0) {
- say(@"Nothing learned yet", @"Your nod and your shake use the default sizes.");
- return;
- }
+// Both learned sizes back to the defaults, asked first; `done` runs once they are.
+static void forget(void (^done)(void)) {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Forget your nod and shake?"
message:@"Both go back to the default sizes. You can learn them again." preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"Forget" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
SGSetInt(SGKeyHeadNod, 0);
SGSetInt(SGKeyHeadShake, 0);
SGHeadGesturesSettingsChanged();
+ done();
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]];
[SGTopController() presentViewController:alert animated:YES completion:nil];
@@ -96,19 +85,23 @@ UIViewController *SGHeadGesturesSettingsPage(void) {
^NSString *(double value) { return [NSString stringWithFormat:@"%ld%%", lround(value)]; });
sensitivity.visible = on;
- SGModRow *nod = SGStatActionRow(@"Learn your nod", nil, ^NSString *{ return learnedValue(SGKeyHeadNod); }, ^{ learn(SGHeadAxisPitch); });
- SGModRow *shake = SGStatActionRow(@"Learn your shake", nil, ^NSString *{ return learnedValue(SGKeyHeadShake); }, ^{ learn(SGHeadAxisYaw); });
- // Shown whenever the switch is on: SGModPage asks `visible` again only when a switch flips or the page
- // comes back, not after an alert, so a row hidden until something is learned would stay hidden.
- SGModRow *forgetRow = SGActionRow(@"Forget what it learned", nil, ^{ forget(); });
+ // Learning and forgetting change what Forget's row reads, which no switch on the page tells it.
+ __block __weak SGModPage *page;
+ void (^refresh)(void) = ^{ [page refreshVisibility]; };
+ SGModRow *nod = SGStatActionRow(@"Learn your nod", nil, ^NSString *{ return learnedValue(SGKeyHeadNod); }, ^{ learn(SGHeadAxisPitch, refresh); });
+ SGModRow *shake = SGStatActionRow(@"Learn your shake", nil, ^NSString *{ return learnedValue(SGKeyHeadShake); }, ^{ learn(SGHeadAxisYaw, refresh); });
+ nod.visible = shake.visible = on;
+ SGModRow *forgetRow = SGActionRow(@"Forget what it learned", nil, ^{ forget(refresh); });
forgetRow.color = SGRed();
- for (SGModRow *row in @[nod, shake, forgetRow]) row.visible = on;
+ forgetRow.visible = ^BOOL { return on() && anythingLearned(); };
- return [[SGModPage alloc] initWithTitle:@"AirPods gestures" intro:nil sections:@[
+ SGModPage *shown = [[SGModPage alloc] initWithTitle:@"AirPods gestures" intro:nil sections:@[
SGSection(nil, @[SGWithSymbol(toggle, @"airpods.pro"), sensitivity]),
SGNotedSection(@"Learn", @[nod, shake, forgetRow],
@"Learning listens for a few seconds and sets how big a move counts, from yours. The gestures work while "
"Spotify plays through headphones that track head motion. A tone in the music confirms each one, a rising pair when "
"it worked and a low one when it did not, with the iPhone on silent too."),
] footer:nil];
+ page = shown;
+ return shown;
}