Skip to content

Commit

Permalink
android scroll performance fixes pt. 1 (#6196)
Browse files Browse the repository at this point in the history
  • Loading branch information
haileyok authored Nov 10, 2024
1 parent 3aeee1e commit 89c9331
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 62 deletions.
53 changes: 25 additions & 28 deletions src/alf/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,35 +103,32 @@ export function ThemeProvider({
})
}, [])

return (
<Context.Provider
value={React.useMemo<Alf>(
() => ({
themes,
themeName: themeName,
theme: themes[themeName],
fonts: {
scale: fontScale,
scaleMultiplier: fontScaleMultiplier,
family: fontFamily,
setFontScale: setFontScaleAndPersist,
setFontFamily: setFontFamilyAndPersist,
},
flags: {},
}),
[
themeName,
themes,
fontScale,
setFontScaleAndPersist,
fontFamily,
setFontFamilyAndPersist,
fontScaleMultiplier,
],
)}>
{children}
</Context.Provider>
const value = React.useMemo<Alf>(
() => ({
themes,
themeName: themeName,
theme: themes[themeName],
fonts: {
scale: fontScale,
scaleMultiplier: fontScaleMultiplier,
family: fontFamily,
setFontScale: setFontScaleAndPersist,
setFontFamily: setFontFamilyAndPersist,
},
flags: {},
}),
[
themeName,
themes,
fontScale,
setFontScaleAndPersist,
fontFamily,
setFontFamilyAndPersist,
fontScaleMultiplier,
],
)

return <Context.Provider value={value}>{children}</Context.Provider>
}

export function useAlf() {
Expand Down
67 changes: 33 additions & 34 deletions src/view/com/util/text/Text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {UITextView} from 'react-native-uitextview'
import {lh, s} from '#/lib/styles'
import {TypographyVariant, useTheme} from '#/lib/ThemeContext'
import {logger} from '#/logger'
import {isIOS} from '#/platform/detection'
import {isIOS, isWeb} from '#/platform/detection'
import {applyFonts, useAlf} from '#/alf'
import {
childHasEmoji,
Expand Down Expand Up @@ -44,8 +44,6 @@ export function Text({
...props
}: React.PropsWithChildren<CustomTextProps>) {
const theme = useTheme()
const typography = theme.typography[type]
const lineHeightStyle = lineHeight ? lh(theme, type, lineHeight) : undefined
const {fonts} = useAlf()

if (IS_DEV) {
Expand All @@ -60,7 +58,10 @@ export function Text({
}
}

if (selectable && isIOS) {
const textProps = React.useMemo(() => {
const typography = theme.typography[type]
const lineHeightStyle = lineHeight ? lh(theme, type, lineHeight) : undefined

const flattened = StyleSheet.flatten([
s.black,
typography,
Expand All @@ -74,49 +75,47 @@ export function Text({
// @ts-ignore
if (flattened.fontSize) {
// @ts-ignore
flattened.fontSize = flattened.fontSize * fonts.scaleMultiplier
flattened.fontSize = Math.round(
// @ts-ignore
flattened.fontSize * fonts.scaleMultiplier,
)
}

const shared = {
uiTextView: true,
return {
uiTextView: selectable && isIOS,
selectable,
style: flattened,
dataSet: isWeb
? Object.assign({tooltip: title}, dataSet || {})
: undefined,
...props,
}
}, [
dataSet,
fonts.family,
fonts.scaleMultiplier,
lineHeight,
props,
selectable,
style,
theme,
title,
type,
])

if (selectable && isIOS) {
return (
<UITextView {...shared}>
{isIOS && emoji ? renderChildrenWithEmoji(children, shared) : children}
<UITextView {...textProps}>
{isIOS && emoji
? renderChildrenWithEmoji(children, textProps)
: children}
</UITextView>
)
}

const flattened = StyleSheet.flatten([
s.black,
typography,
lineHeightStyle,
style,
])

applyFonts(flattened, fonts.family)

// should always be defined on `typography`
// @ts-ignore
if (flattened.fontSize) {
// @ts-ignore
flattened.fontSize = flattened.fontSize * fonts.scaleMultiplier
}

const shared = {
selectable,
style: flattened,
dataSet: Object.assign({tooltip: title}, dataSet || {}),
...props,
}

return (
<RNText {...shared}>
{isIOS && emoji ? renderChildrenWithEmoji(children, shared) : children}
<RNText {...textProps}>
{isIOS && emoji ? renderChildrenWithEmoji(children, textProps) : children}
</RNText>
)
}

0 comments on commit 89c9331

Please sign in to comment.