Skip to content

Commit 2d882e7

Browse files
committed
feat(mobile): choose reply emphasis weight per font
1 parent c8fd694 commit 2d882e7

10 files changed

Lines changed: 199 additions & 22 deletions

File tree

‎apps/mobile/modules/t3-native-controls/ios/T3InstalledFonts.swift‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import UIKit
33

44
@MainActor
55
enum T3InstalledFonts {
6-
static func describe(_ font: UIFont, name: String? = nil) -> [String: String] {
6+
static func describe(_ font: UIFont, name: String? = nil) -> [String: Any] {
77
func variant(_ weight: UIFont.Weight) -> String {
88
let descriptor = UIFontDescriptor(fontAttributes: [
99
.family: font.familyName,
@@ -12,16 +12,27 @@ enum T3InstalledFonts {
1212
let candidate = UIFont(descriptor: descriptor, size: font.pointSize)
1313
return candidate.familyName == font.familyName ? candidate.fontName : font.fontName
1414
}
15+
// CoreText reports medium as 0.20 while UIKit requests 0.23.
16+
var emphasisFaces: [String: String] = [:]
17+
for (key, weight) in [("500", UIFont.Weight.medium), ("600", .semibold), ("700", .bold)] {
18+
let name = variant(weight)
19+
guard let candidate = UIFont(name: name, size: font.pointSize),
20+
let traits = candidate.fontDescriptor.object(forKey: .traits) as? [UIFontDescriptor.TraitKey: Any],
21+
let actual = traits[.weight] as? NSNumber,
22+
abs(actual.doubleValue - Double(weight.rawValue)) < 0.04 else { continue }
23+
emphasisFaces[key] = name
24+
}
1525
return [
1626
"name": name ?? font.fontName,
1727
"label": font.familyName,
1828
"regular": font.fontName,
1929
"medium": variant(.medium),
2030
"bold": variant(.bold),
31+
"emphasisFaces": emphasisFaces,
2132
]
2233
}
2334

24-
static func resolve(_ names: [String], completion: @escaping ([[String: String]]) -> Void) {
35+
static func resolve(_ names: [String], completion: @escaping ([[String: Any]]) -> Void) {
2536
guard !names.isEmpty else { completion([]); return }
2637
// Provider fonts must be requested again for each process. Persist names,
2738
// not file URLs: iOS owns the files and the picker grants family access.
@@ -39,11 +50,11 @@ enum T3InstalledFonts {
3950

4051
@MainActor
4152
final class T3FontPickerPresentation: NSObject, UIFontPickerViewControllerDelegate, UIAdaptivePresentationControllerDelegate {
42-
private let completion: ([String: String]?) -> Void
53+
private let completion: ([String: Any]?) -> Void
4354
private var picker: UIFontPickerViewController?
4455
private var finished = false
4556

46-
init(completion: @escaping ([String: String]?) -> Void) {
57+
init(completion: @escaping ([String: Any]?) -> Void) {
4758
self.completion = completion
4859
}
4960

@@ -79,7 +90,7 @@ final class T3FontPickerPresentation: NSObject, UIFontPickerViewControllerDelega
7990
picker.dismiss(animated: true) { self.finish(nil) }
8091
}
8192

82-
private func finish(_ result: [String: String]?) {
93+
private func finish(_ result: [String: Any]?) {
8394
guard !finished else { return }
8495
finished = true
8596
picker?.delegate = nil

‎apps/mobile/src/features/files/FileMarkdownPreview.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ interface MarkdownPreviewStyles {
3232
}
3333

3434
export function useMarkdownPreviewStyles(): MarkdownPreviewStyles {
35-
const { fonts, replyFontSize } = useAppearancePreferences();
35+
const { fonts, replyFontSize, replyEmphasisFontFamily } = useAppearancePreferences();
3636
const nativeMarkdownTypography = resolveNativeMarkdownTypography(replyFontSize);
3737
const theme = useUniwindTheme();
3838
const body = theme["--color-md-body"];
@@ -64,7 +64,7 @@ export function useMarkdownPreviewStyles(): MarkdownPreviewStyles {
6464
codeFontFamily: fonts.code.regular,
6565
fontFamily: regularFontFamily,
6666
headingFontFamily: boldFontFamily,
67-
boldFontFamily,
67+
boldFontFamily: replyEmphasisFontFamily,
6868
},
6969
};
7070
}

‎apps/mobile/src/features/settings/appearance/AppearancePreferencesProvider.tsx‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ import {
2525
import { mobilePreferencesAtom, updateMobilePreferencesAtom } from "../../../state/preferences";
2626
import {
2727
normalizeFontSelections,
28+
normalizeReplyEmphasisWeights,
29+
resolveReplyEmphasis,
30+
type ReplyEmphasisWeight,
2831
resolveMobileFonts,
2932
type MobileFontRole,
3033
type MobileFontSelections,
@@ -59,6 +62,9 @@ interface AppearancePreferencesContextValue {
5962
readonly installedFonts: ReadonlyMap<string, InstalledMobileFont>;
6063
readonly unavailableFonts: readonly string[];
6164
readonly replyFontSize: number;
65+
readonly replyEmphasisWeight: ReplyEmphasisWeight | undefined;
66+
readonly replyEmphasisFontFamily: string;
67+
readonly setReplyEmphasisWeight: (weight: ReplyEmphasisWeight | undefined) => void;
6268
readonly composerFontSize: number;
6369
readonly customReplyFontSize: boolean;
6470
readonly customComposerFontSize: boolean;
@@ -202,6 +208,10 @@ export function AppearancePreferencesProvider(props: { readonly children: ReactN
202208
() => resolveMobileFonts(fontSelections, installedFonts, Platform.OS),
203209
[fontSelections, installedFonts],
204210
);
211+
const replyEmphasisWeight = normalizeReplyEmphasisWeights(
212+
storedPreferences?.replyEmphasisWeights,
213+
)[fonts.reply.regular];
214+
const replyEmphasisFontFamily = resolveReplyEmphasis(fonts.reply, replyEmphasisWeight);
205215
const fontVariables = useMemo(
206216
() => ({
207217
...themeVariables,
@@ -366,6 +376,19 @@ export function AppearancePreferencesProvider(props: { readonly children: ReactN
366376
},
367377
[savePreferences],
368378
);
379+
const setReplyEmphasisWeight = useCallback(
380+
(weight: ReplyEmphasisWeight | undefined) => {
381+
savePreferences({
382+
transform: (current) => {
383+
const next = { ...normalizeReplyEmphasisWeights(current.replyEmphasisWeights) };
384+
if (weight === undefined) delete next[fonts.reply.regular];
385+
else next[fonts.reply.regular] = weight;
386+
return { replyEmphasisWeights: next };
387+
},
388+
});
389+
},
390+
[fonts.reply.regular, savePreferences],
391+
);
369392
const setReplyFontSize = useCallback(
370393
(size: number | null) =>
371394
updatePreferences({ replyFontSize: size === null ? null : normalizeBaseFontSize(size) }),
@@ -385,6 +408,9 @@ export function AppearancePreferencesProvider(props: { readonly children: ReactN
385408
installedFonts,
386409
unavailableFonts,
387410
replyFontSize,
411+
replyEmphasisWeight,
412+
replyEmphasisFontFamily,
413+
setReplyEmphasisWeight,
388414
composerFontSize,
389415
customReplyFontSize,
390416
customComposerFontSize,
@@ -418,6 +444,9 @@ export function AppearancePreferencesProvider(props: { readonly children: ReactN
418444
installedFonts,
419445
unavailableFonts,
420446
replyFontSize,
447+
replyEmphasisWeight,
448+
replyEmphasisFontFamily,
449+
setReplyEmphasisWeight,
421450
composerFontSize,
422451
customReplyFontSize,
423452
customComposerFontSize,

‎apps/mobile/src/features/settings/appearance/sections/FontAppearanceSection.tsx‎

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { updateMobilePreferencesAtom } from "../../../../state/preferences";
1313
import { canPickInstalledFont, pickInstalledFont } from "../../../../native/installed-fonts";
1414
import {
1515
BUILT_IN_FONT_OPTIONS,
16+
REPLY_EMPHASIS_OPTIONS,
1617
DEFAULT_FONT_SELECTIONS,
1718
type MobileFontRole,
1819
} from "../../../../lib/mobileFonts";
@@ -33,6 +34,9 @@ export function FontAppearanceSection() {
3334
unavailableFonts,
3435
fonts,
3536
replyFontSize,
37+
replyEmphasisWeight,
38+
replyEmphasisFontFamily,
39+
setReplyEmphasisWeight,
3640
composerFontSize,
3741
customReplyFontSize,
3842
customComposerFontSize,
@@ -76,11 +80,11 @@ export function FontAppearanceSection() {
7680
lineHeight: Math.round(replyFontSize * 1.45),
7781
}}
7882
>
79-
A reply in your selected font.
83+
A reply in your selected font.{" "}
84+
<Text style={{ fontFamily: replyEmphasisFontFamily }}>Emphasized text.</Text>
8085
</Text>
8186
<Text className="text-xs text-foreground-muted">
82-
Defaults follow the interface. Choose an installed font to use its matching styles. These
83-
choices stay on this device.
87+
Defaults follow the interface. These choices stay on this device.
8488
</Text>
8589
</View>
8690
{ROLES.map(({ role, label }) => (
@@ -122,6 +126,45 @@ export function FontAppearanceSection() {
122126
</View>
123127
</Menu>
124128
))}
129+
<Menu
130+
accessibilityLabel="Reply emphasis weight"
131+
accessibilityRole="button"
132+
actions={[
133+
{
134+
id: "default",
135+
title: "Default",
136+
state: replyEmphasisWeight === undefined ? "on" : "off",
137+
attributes: { disabled: !isReady || busy },
138+
},
139+
...REPLY_EMPHASIS_OPTIONS.map((option) => ({
140+
id: option.id,
141+
title: option.label,
142+
state: replyEmphasisWeight === option.id ? ("on" as const) : ("off" as const),
143+
attributes: { disabled: !isReady || busy || !fonts.reply.emphasisFaces?.[option.id] },
144+
})),
145+
]}
146+
onPressAction={({ nativeEvent }) =>
147+
setReplyEmphasisWeight(
148+
REPLY_EMPHASIS_OPTIONS.find((option) => option.id === nativeEvent.event)?.id,
149+
)
150+
}
151+
>
152+
<View>
153+
<ListRowLayout
154+
icon="textformat.size"
155+
label="Reply emphasis weight"
156+
detail={
157+
REPLY_EMPHASIS_OPTIONS.find((option) => option.id === replyEmphasisWeight)?.label ??
158+
"Default"
159+
}
160+
/>
161+
</View>
162+
</Menu>
163+
<View className="px-4 pb-2">
164+
<Text className="text-xs text-foreground-muted">
165+
Emphasis is remembered for this reply font. Unavailable weights are disabled.
166+
</Text>
167+
</View>
125168
<SettingsSwitchRow
126169
icon="textformat.size"
127170
label="Custom reply size"
@@ -175,6 +218,7 @@ export function FontAppearanceSection() {
175218
onPress={() =>
176219
void savePreferences({
177220
fontSelections: DEFAULT_FONT_SELECTIONS,
221+
replyEmphasisWeights: {},
178222
replyFontSize: null,
179223
composerFontSize: null,
180224
}).catch(report)

‎apps/mobile/src/features/threads/ThreadFeed.tsx‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,7 @@ const AssistantMarkdownContent = memo(function AssistantMarkdownContent(props: {
904904
});
905905

906906
function useMarkdownStyles(): MarkdownStyleSets {
907-
const { appearance, fonts, replyFontSize } = useAppearancePreferences();
907+
const { appearance, fonts, replyFontSize, replyEmphasisFontFamily } = useAppearancePreferences();
908908
const replyTypography = useMemo(
909909
() => resolveNativeMarkdownTypography(replyFontSize),
910910
[replyFontSize],
@@ -980,12 +980,13 @@ function useMarkdownStyles(): MarkdownStyleSets {
980980
codeFontFamily: fonts.code.regular,
981981
fontFamily: fonts.reply.regular,
982982
headingFontFamily: fonts.reply.bold,
983-
boldFontFamily: fonts.reply.bold,
983+
boldFontFamily: replyEmphasisFontFamily,
984984
},
985985
},
986986
};
987987
}, [
988988
fonts.reply,
989+
replyEmphasisFontFamily,
989990
fonts.code.regular,
990991
replyTypography,
991992
boldFontFamily,
@@ -2059,12 +2060,12 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
20592060
const userScrollSettleTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
20602061
const headerMaterialVisibleRef = useRef(false);
20612062
const { width: windowWidth, fontScale } = useWindowDimensions();
2062-
const { appearance, fonts, replyFontSize } = useAppearancePreferences();
2063+
const { appearance, fonts, replyFontSize, replyEmphasisFontFamily } = useAppearancePreferences();
20632064
const workRowSizing = deriveThreadWorkLogSizing({
20642065
baseFontSize: appearance.baseFontSize,
20652066
fontScale,
20662067
});
2067-
const textLayoutKey = `${workRowSizing.textSizeKey}:${replyFontSize}:${fonts.interface.regular}:${fonts.interface.bold}:${fonts.reply.regular}:${fonts.reply.bold}:${fonts.code.regular}`;
2068+
const textLayoutKey = `${workRowSizing.textSizeKey}:${replyFontSize}:${fonts.interface.regular}:${fonts.interface.bold}:${fonts.reply.regular}:${fonts.reply.bold}:${replyEmphasisFontFamily}:${fonts.code.regular}`;
20682069
const previousTextSize = useRef(textLayoutKey);
20692070
useLayoutEffect(() => {
20702071
if (previousTextSize.current === textLayoutKey) {

‎apps/mobile/src/lib/fontFamilies.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ export const FONT_FAMILIES = {
22
regular: "DMSans-Regular",
33
medium: "DMSans-Medium",
44
bold: "DMSans-Bold",
5+
emphasisFaces: { "500": "DMSans-Medium", "700": "DMSans-Bold" },
56
} as const;

‎apps/mobile/src/lib/mobileFonts.test.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { describe, expect, it } from "vite-plus/test";
22
import {
33
DEFAULT_FONT_SELECTIONS,
44
normalizeFontSelections,
5+
normalizeReplyEmphasisWeights,
6+
resolveReplyEmphasis,
57
resolveMobileFonts,
68
} from "./mobileFonts";
79

@@ -11,6 +13,11 @@ const serif = {
1113
regular: "ExampleSerif-Regular",
1214
medium: "ExampleSerif-Medium",
1315
bold: "ExampleSerif-Bold",
16+
emphasisFaces: {
17+
"500": "ExampleSerif-Medium",
18+
"600": "ExampleSerif-Semibold",
19+
"700": "ExampleSerif-Bold",
20+
},
1421
};
1522

1623
describe("installed mobile fonts", () => {
@@ -75,3 +82,39 @@ describe("installed mobile fonts", () => {
7582
expect(fonts.code.regular).toBe("monospace");
7683
});
7784
});
85+
86+
describe("reply emphasis", () => {
87+
it("preserves the default bold face and only changes emphasis for a selected weight", () => {
88+
const standard = resolveMobileFonts(DEFAULT_FONT_SELECTIONS, new Map(), "ios");
89+
expect(resolveReplyEmphasis(standard.reply)).toBe("DMSans-Bold");
90+
expect(resolveReplyEmphasis(standard.reply, "500")).toBe("DMSans-Medium");
91+
});
92+
93+
it("selects a real installed semibold face and falls back when that weight is unavailable", () => {
94+
expect(resolveReplyEmphasis(serif, "600")).toBe("ExampleSerif-Semibold");
95+
expect(
96+
resolveReplyEmphasis(
97+
{
98+
regular: "Example-Regular",
99+
medium: "Example-Regular",
100+
bold: "Example-Bold",
101+
emphasisFaces: { "700": "Example-Bold" },
102+
},
103+
"600",
104+
),
105+
).toBe("Example-Bold");
106+
});
107+
108+
it("remembers different weights per font and drops invalid saved values", () => {
109+
const saved = normalizeReplyEmphasisWeights({
110+
"FirstSerif-Regular": "700",
111+
"SecondSerif-Regular": "600",
112+
invalid: 600,
113+
bad: "900",
114+
"": "500",
115+
});
116+
expect(saved).toEqual({ "FirstSerif-Regular": "700", "SecondSerif-Regular": "600" });
117+
expect(normalizeReplyEmphasisWeights(null)).toEqual({});
118+
expect(resolveReplyEmphasis(serif, saved["SecondSerif-Regular"])).toBe("ExampleSerif-Semibold");
119+
});
120+
});

0 commit comments

Comments
 (0)