Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import FontUtils from '@styles/utils/FontUtils';
import variables from '@styles/variables';

import type {NativeBottomTabIcon, NativeBottomTabNavigationOptions} from '@react-navigation/bottom-tabs/unstable';
import type {SkCanvas, SkImage, SkParagraph, SkPath} from '@shopify/react-native-skia';
import type {SkCanvas, SkImage, SkParagraph, SkPath, SkTypefaceFontProvider} from '@shopify/react-native-skia';

import {BlendMode, ClipOp, FillType, FilterMode, FontWeight, ImageFormat, MipmapMode, Skia, TextAlign} from '@shopify/react-native-skia';
import {PixelRatio} from 'react-native';
Expand All @@ -19,6 +19,12 @@ type TabIconLayout = {
avatarSize: number;
/** Whether the label is drawn under the glyph or beside it, as UIKit lays out a tab in landscape. */
direction: 'stacked' | 'inline';
/** Radius of the emoji status badge, sized with the avatar so it stays clear of the status dot. */
emojiStatusRadius: number;
/** Size of the emoji drawn in that badge. */
emojiStatusFontSize: number;
/** How far that badge sits past the bottom-right corner of the avatar's box. The canvas grows to fit it. */
emojiStatusOffset: number;
};

type TabIconLabel = {
Expand All @@ -32,12 +38,21 @@ type TabIconLabel = {
maxWidth: number;
};

type TabIconEmojiStatus = {
emoji: string;
backgroundColor: string;
/** The custom emoji font, given only for a private use area emoji, which the system fonts can't draw. */
fontManager?: SkTypefaceFontProvider;
};

type TabIconParams = {
/** The glyph drawn unless an avatar is given. */
name: NativeTabName;
color: string;
avatar?: TabAvatarImage;
dotColor?: string;
/** The user's status emoji, badged on the account icon as the JS tab bar does. */
emojiStatus?: TabIconEmojiStatus;
label?: TabIconLabel;
};

Expand Down Expand Up @@ -135,6 +150,34 @@ function drawStatusDot(canvas: SkCanvas, right: number, top: number, scale: numb
paint.dispose();
}

/** Drawn at the bottom-right of the badge box, opposite the status dot, and as far past it as the layout's offset. */
function drawEmojiStatus(canvas: SkCanvas, layout: TabIconLayout, right: number, bottom: number, scale: number, {emoji, backgroundColor, fontManager}: TabIconEmojiStatus) {
const radius = layout.emojiStatusRadius * scale;
const offset = layout.emojiStatusOffset * scale;
const centerX = right + offset - radius;
const centerY = bottom + offset - radius;
const paint = Skia.Paint();
paint.setAntiAlias(true);
paint.setBlendMode(BlendMode.Clear);
canvas.drawCircle(centerX, centerY, radius + variables.nativeTabIconDotCutout * scale, paint);
paint.setBlendMode(BlendMode.SrcOver);
paint.setColor(Skia.Color(backgroundColor));
canvas.drawCircle(centerX, centerY, radius, paint);
paint.dispose();

// A paragraph, so the emoji falls back to the system's color emoji font. A custom emoji font is tried first.
const paragraph = Skia.ParagraphBuilder.Make({maxLines: 1, textAlign: TextAlign.Center}, fontManager)
.pushStyle({
fontFamilies: fontManager ? [FontUtils.fontFamily.single.CUSTOM_EMOJI_FONT.fontFamily] : undefined,
fontSize: layout.emojiStatusFontSize * scale,
})
.addText(emoji)
.build();
paragraph.layout(radius * 2);
paragraph.paint(canvas, centerX - radius, centerY - paragraph.getHeight() / 2);
paragraph.dispose();
}

type TabIconFrame = {
canvasWidth: number;
canvasHeight: number;
Expand All @@ -146,15 +189,25 @@ type TabIconFrame = {

/**
* With a label, every tab gives its glyph or avatar a box as big as the avatar, so glyphs and the avatar share a centre
* and every label starts at the same place: under that box, or beside it in landscape.
* and every label starts at the same place: under that box, or beside it in landscape. The canvas also grows by the
* overhang of anything drawn past the content's bottom-right corner, on both sides where the bar centres the content.
*/
function getTabIconFrame(layout: TabIconLayout, contentSize: number, labelGap: number, labelWidth: number, labelHeight: number, hasLabel: boolean, scale: number): TabIconFrame {
function getTabIconFrame(
layout: TabIconLayout,
contentSize: number,
overhang: number,
labelGap: number,
labelWidth: number,
labelHeight: number,
hasLabel: boolean,
scale: number,
): TabIconFrame {
const boxSize = hasLabel ? Math.max(layout.glyphSize, layout.avatarSize) * scale : contentSize;

if (layout.direction === 'inline') {
const canvasHeight = Math.ceil(Math.max(boxSize, labelHeight));
const canvasHeight = Math.ceil(Math.max(boxSize + 2 * overhang, labelHeight));
return {
canvasWidth: Math.ceil(boxSize + (hasLabel ? labelGap + labelWidth : 0)),
canvasWidth: Math.ceil(boxSize + (hasLabel ? labelGap + labelWidth : overhang)),
canvasHeight,
contentLeft: (boxSize - contentSize) / 2,
contentTop: (canvasHeight - contentSize) / 2,
Expand All @@ -163,31 +216,35 @@ function getTabIconFrame(layout: TabIconLayout, contentSize: number, labelGap: n
};
}

const canvasWidth = Math.ceil(Math.max(contentSize, labelWidth));
const canvasWidth = Math.ceil(Math.max(contentSize + 2 * overhang, labelWidth));
const labelTop = (boxSize + layout.glyphSize * scale) / 2 + labelGap;
// Under a label, the overhang reaches into the gap above it, so only an unlabelled icon grows in height.
const contentTop = (boxSize - contentSize) / 2 + (hasLabel ? 0 : overhang);
return {
canvasWidth,
canvasHeight: Math.ceil(hasLabel ? labelTop + labelHeight : boxSize),
canvasHeight: Math.ceil(hasLabel ? Math.max(labelTop + labelHeight, contentTop + contentSize + overhang) : boxSize + 2 * overhang),
contentLeft: (canvasWidth - contentSize) / 2,
contentTop: (boxSize - contentSize) / 2,
contentTop,
labelLeft: (canvasWidth - labelWidth) / 2,
labelTop,
};
}

function drawTabIcon(layout: TabIconLayout, {name, color, avatar, dotColor, label}: TabIconParams): NativeBottomTabIcon | undefined {
function drawTabIcon(layout: TabIconLayout, {name, color, avatar, dotColor, emojiStatus, label}: TabIconParams): NativeBottomTabIcon | undefined {
const scale = PixelRatio.get();
const contentSize = (avatar ? layout.avatarSize : layout.glyphSize) * scale;
// With an emoji, the dot and badge sit on an avatar-sized box even over the glyph, because the badge is sized to clear the dot on the avatar.
const badgeBoxSize = avatar || emojiStatus ? layout.avatarSize * scale : contentSize;
const overhang = emojiStatus ? layout.emojiStatusOffset * scale : 0;
const paragraph = label ? makeLabelParagraph(label, scale) : undefined;
const {canvasWidth, canvasHeight, contentLeft, contentTop, labelLeft, labelTop} = getTabIconFrame(
layout,
contentSize,
(label?.gap ?? 0) * scale,
paragraph?.getLongestLine() ?? 0,
paragraph?.getHeight() ?? 0,
!!label,
scale,
);
const {
canvasWidth,
canvasHeight,
contentLeft: badgeBoxLeft,
contentTop: badgeBoxTop,
labelLeft,
labelTop,
} = getTabIconFrame(layout, badgeBoxSize, overhang, (label?.gap ?? 0) * scale, paragraph?.getLongestLine() ?? 0, paragraph?.getHeight() ?? 0, !!label, scale);

const surface = Skia.Surface.Make(canvasWidth, canvasHeight);
if (!surface) {
Expand All @@ -196,13 +253,18 @@ function drawTabIcon(layout: TabIconLayout, {name, color, avatar, dotColor, labe
}

const canvas = surface.getCanvas();
const contentLeft = badgeBoxLeft + (badgeBoxSize - contentSize) / 2;
const contentTop = badgeBoxTop + (badgeBoxSize - contentSize) / 2;
if (avatar) {
drawCircularImage(canvas, avatar.image, contentLeft, contentTop, contentSize);
} else {
drawGlyph(canvas, name, contentLeft, contentTop, contentSize, color);
}
if (dotColor) {
drawStatusDot(canvas, contentLeft + contentSize, contentTop, scale, dotColor);
drawStatusDot(canvas, badgeBoxLeft + badgeBoxSize, badgeBoxTop, scale, dotColor);
}
if (emojiStatus) {
drawEmojiStatus(canvas, layout, badgeBoxLeft + badgeBoxSize, badgeBoxTop + badgeBoxSize, scale, emojiStatus);
}
if (paragraph) {
paragraph.paint(canvas, labelLeft, labelTop);
Expand All @@ -227,9 +289,11 @@ function drawTabIcon(layout: TabIconLayout, {name, color, avatar, dotColor, labe
* stays the same. A status, theme or language change redraws only the icons it reaches.
*/
function getTabIcon(layout: TabIconLayout, params: TabIconParams): NativeBottomTabIcon | undefined {
const {name, color, avatar, dotColor, label} = params;
const {name, color, avatar, dotColor, emojiStatus, label} = params;
// The label's font is fixed per platform, so it is left out of the key.
const key = [layout.direction, name, avatar?.uri ?? color, dotColor, label?.text, label?.color, label?.isBold, label?.maxWidth].join('|');
const key = [layout.direction, name, avatar?.uri ?? color, dotColor, emojiStatus?.emoji, emojiStatus?.backgroundColor, label?.text, label?.color, label?.isBold, label?.maxWidth].join(
'|',
);
if (iconCache.has(key)) {
return iconCache.get(key);
}
Expand All @@ -249,4 +313,4 @@ function getTabBarIcon(layout: TabIconLayout, getParams: (isSelected: boolean) =
}

export default getTabBarIcon;
export type {TabIconLayout};
export type {TabIconLayout, TabIconEmojiStatus};
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {NativeTabName} from '@libs/Navigation/AppNavigator/Navigators/Nativ
import getTabBarIcon from '@libs/Navigation/AppNavigator/Navigators/NativeTabNavigator/tabIconRasterizer';
import type {TabIconLayout} from '@libs/Navigation/AppNavigator/Navigators/NativeTabNavigator/tabIconRasterizer';
import useTabAvatarImage from '@libs/Navigation/AppNavigator/Navigators/NativeTabNavigator/useTabAvatarImage';
import useTabEmojiStatus from '@libs/Navigation/AppNavigator/Navigators/NativeTabNavigator/useTabEmojiStatus';

import variables from '@styles/variables';

Expand All @@ -25,12 +26,17 @@ const TAB_ICON_LAYOUT: TabIconLayout = {
glyphSize: variables.iconBottomBar,
avatarSize: variables.iconBottomBar,
direction: 'stacked',
emojiStatusRadius: variables.nativeTabIconEmojiStatusRadius,
emojiStatusFontSize: variables.nativeTabIconEmojiStatusFontSize,
// Material scales an icon down to fit its slot, so a badge past the avatar would shrink the whole icon.
emojiStatusOffset: 0,
};

function useNativeTabBarOptions({shouldShowNativeTabBar, isAccountAvatarShown, dotColors, tabLabels}: NativeTabBarOptionsParams) {
const theme = useTheme();
const styles = useThemeStyles();
const avatar = useTabAvatarImage(isAccountAvatarShown);
const emojiStatus = useTabEmojiStatus();

// The tint only reaches the labels, since the icons arrive already recolored (react-native-screens patch 004).
const screenOptions: NativeBottomTabNavigationOptions = {
Expand All @@ -51,6 +57,7 @@ function useNativeTabBarOptions({shouldShowNativeTabBar, isAccountAvatarShown, d
color: isSelected ? theme.iconMenu : theme.icon,
avatar: name === NAVIGATORS.SETTINGS_SPLIT_NAVIGATOR ? avatar : undefined,
dotColor: dotColors[name],
emojiStatus: name === NAVIGATORS.SETTINGS_SPLIT_NAVIGATOR ? emojiStatus : undefined,
})),
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {NativeTabName} from '@libs/Navigation/AppNavigator/Navigators/Nativ
import getTabBarIcon from '@libs/Navigation/AppNavigator/Navigators/NativeTabNavigator/tabIconRasterizer';
import type {TabIconLayout} from '@libs/Navigation/AppNavigator/Navigators/NativeTabNavigator/tabIconRasterizer';
import useTabAvatarImage from '@libs/Navigation/AppNavigator/Navigators/NativeTabNavigator/useTabAvatarImage';
import useTabEmojiStatus from '@libs/Navigation/AppNavigator/Navigators/NativeTabNavigator/useTabEmojiStatus';

import {fontScale} from '@styles/typography';
import variables from '@styles/variables';
Expand All @@ -24,13 +25,19 @@ const TAB_ICON_LAYOUT: TabIconLayout = {
// A circle reads smaller than a glyph of the same box, so the avatar is drawn a little larger than the glyphs.
avatarSize: variables.avatarNativeTabBarIOS,
direction: 'stacked',
emojiStatusRadius: variables.nativeTabIconEmojiStatusRadius,
emojiStatusFontSize: variables.nativeTabIconEmojiStatusFontSize,
emojiStatusOffset: variables.nativeTabIconEmojiStatusOffset,
};

/** A phone in landscape gives the bar too little height for a stacked tab, so UIKit lays its own tabs out this way. */
const LANDSCAPE_TAB_ICON_LAYOUT: TabIconLayout = {
glyphSize: variables.iconNativeTabBarIOSLandscape,
avatarSize: variables.avatarNativeTabBarIOSLandscape,
direction: 'inline',
emojiStatusRadius: variables.nativeTabIconEmojiStatusRadiusLandscape,
emojiStatusFontSize: variables.nativeTabIconEmojiStatusFontSizeLandscape,
emojiStatusOffset: variables.nativeTabIconEmojiStatusOffset,
};

/**
Expand All @@ -41,6 +48,7 @@ const LANDSCAPE_TAB_ICON_LAYOUT: TabIconLayout = {
function useNativeTabBarOptions({shouldShowNativeTabBar, isAccountAvatarShown, dotColors, tabLabels}: NativeTabBarOptionsParams) {
const theme = useTheme();
const avatar = useTabAvatarImage(isAccountAvatarShown);
const emojiStatus = useTabEmojiStatus();
const {windowWidth} = useWindowDimensions();
const {isInLandscapeMode} = useResponsiveLayout();
const tabIconLayout = isInLandscapeMode ? LANDSCAPE_TAB_ICON_LAYOUT : TAB_ICON_LAYOUT;
Expand Down Expand Up @@ -69,6 +77,7 @@ function useNativeTabBarOptions({shouldShowNativeTabBar, isAccountAvatarShown, d
color: isSelected ? theme.iconMenu : theme.icon,
avatar: name === NAVIGATORS.SETTINGS_SPLIT_NAVIGATOR ? avatar : undefined,
dotColor: dotColors[name],
emojiStatus: name === NAVIGATORS.SETTINGS_SPLIT_NAVIGATOR ? emojiStatus : undefined,
label: {
text: tabLabels[name],
color: isSelected ? theme.text : theme.textSupporting,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails';
import useOnyx from '@hooks/useOnyx';
import useTheme from '@hooks/useTheme';

import {containsCustomEmoji} from '@libs/CustomEmojiUtils';

import FontUtils from '@styles/utils/FontUtils';

import ONYXKEYS from '@src/ONYXKEYS';

import type {SkTypefaceFontProvider} from '@shopify/react-native-skia';

import {delegateEmailSelector} from '@selectors/Account';
import {Skia} from '@shopify/react-native-skia';
import {useEffect, useState} from 'react';
import {Image} from 'react-native';

import type {TabIconEmojiStatus} from './tabIconRasterizer';

// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- require() of a bundled asset is untyped. Metro resolves it to an asset ID.
const CUSTOM_EMOJI_FONT = require('@assets/fonts/native/CustomEmojiNativeFont.ttf') as number;

/** Held outside any component, so the font is read from disk once and a remounted navigator reuses it. */
let loadedCustomEmojiFontManager: SkTypefaceFontProvider | undefined;

/**
* The current user's status emoji for the account tab icon, as the JS tab bar badges it: not while acting as a
* delegate, because the delegate avatar takes its place there. Skia's system fonts have no glyph for our private use
* area emojis, so those wait for the custom emoji font to load rather than draw a blank badge.
*/
function useTabEmojiStatus(): TabIconEmojiStatus | undefined {
const theme = useTheme();
const [delegateEmail] = useOnyx(ONYXKEYS.ACCOUNT, {selector: delegateEmailSelector});
const emojiCode = useCurrentUserPersonalDetails().status?.emojiCode;
const isCustomEmoji = containsCustomEmoji(emojiCode);
const [customEmojiFontManager, setCustomEmojiFontManager] = useState(loadedCustomEmojiFontManager);

useEffect(() => {
if (!isCustomEmoji || customEmojiFontManager) {
return;
}
let isActive = true;
Skia.Data.fromURI(Image.resolveAssetSource(CUSTOM_EMOJI_FONT).uri)
.then((data) => {
const typeface = Skia.Typeface.MakeFreeTypeFaceFromData(data);
data.dispose();
if (!typeface) {
return;
}
const fontManager = Skia.TypefaceFontProvider.Make();
fontManager.registerFont(typeface, FontUtils.fontFamily.single.CUSTOM_EMOJI_FONT.fontFamily);
loadedCustomEmojiFontManager = fontManager;
if (isActive) {
setCustomEmojiFontManager(fontManager);
}
})
// A failed load keeps the badge off.
.catch(() => undefined);
return () => {
isActive = false;
};
}, [isCustomEmoji, customEmojiFontManager]);

if (!emojiCode || delegateEmail || (isCustomEmoji && !customEmojiFontManager)) {
return undefined;
}
return {emoji: emojiCode, backgroundColor: theme.border, fontManager: isCustomEmoji ? customEmojiFontManager : undefined};
}

export default useTabEmojiStatus;
10 changes: 10 additions & 0 deletions src/styles/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,16 @@ export default {
nativeTabIconDotRadius: 4,
/** Width of the ring cut out of a native tab bar icon around its status dot, as the JS tab bar's dot border does. */
nativeTabIconDotCutout: 2,
/** Radius of the emoji status badge drawn into the account icon of a native tab bar. */
nativeTabIconEmojiStatusRadius: 6,
/** Size of the emoji drawn in that badge. */
nativeTabIconEmojiStatusFontSize: 8,
/** Radius of that badge on the smaller landscape avatar of the iOS native tab bar, where a full-size badge would run into the status dot. */
nativeTabIconEmojiStatusRadiusLandscape: 5,
/** Size of the emoji drawn in that landscape badge. */
nativeTabIconEmojiStatusFontSizeLandscape: 7,
/** How far the emoji status badge of an iOS native tab bar icon sits past the bottom-right corner of the avatar, as the JS badge hangs off its avatar. */
nativeTabIconEmojiStatusOffset: 2,
/** Gap between the glyph and the label drawn under it, or beside it in landscape, in an iOS native tab bar icon. */
nativeTabIconLabelGap: 6,
/** Space between the screen edge and the first tab of the iOS 26 floating tab bar. */
Expand Down
Loading
Loading