-
Notifications
You must be signed in to change notification settings - Fork 5k
feat(mobile): customizable sent message colors #8155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
abcdmku
wants to merge
9
commits into
pingdotgg:main
Choose a base branch
from
abcdmku:feat/mobile-sent-message-colors
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
cbad33f
feat(mobile): customize sent message bubble and text colors
abcdmku 8142f1d
fix(mobile): keep the custom message color swatch reachable
abcdmku 095539d
fix(mobile): accept pasted hex colors with surrounding whitespace
abcdmku d5819c3
feat(mobile): add a color picker for custom message colors
abcdmku f2c617a
fix(mobile): keep cleared message colors cleared across saves
abcdmku 6c36388
perf(mobile): drive color picker thumbs on the UI thread
abcdmku b3212db
fix(mobile): let vertical drags work on the color picker pad
abcdmku 07c2e80
fix(mobile): keep the color picker exact on typed hex values
abcdmku 4d02613
fix(mobile): keep fenced code readable and picker commits race-free
abcdmku File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
221 changes: 221 additions & 0 deletions
221
apps/mobile/src/features/settings/appearance/components/HsvColorPicker.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,221 @@ | ||
| import { useEffect, useId, useMemo, useRef, useState } from "react"; | ||
| import { View } from "react-native"; | ||
| import { Gesture, GestureDetector } from "react-native-gesture-handler"; | ||
| import Animated, { runOnJS, useAnimatedStyle, useSharedValue } from "react-native-reanimated"; | ||
| import Svg, { Defs, LinearGradient, Rect, Stop } from "react-native-svg"; | ||
|
|
||
| import { hexToHsv, hsvToHex, type HsvColor } from "../../../../lib/mobileTheme"; | ||
|
|
||
| const THUMB_SIZE = 24; | ||
| const HUE_STOPS: ReadonlyArray<{ readonly offset: number; readonly color: string }> = [ | ||
| { offset: 0, color: "#ff0000" }, | ||
| { offset: 1 / 6, color: "#ffff00" }, | ||
| { offset: 2 / 6, color: "#00ff00" }, | ||
| { offset: 3 / 6, color: "#00ffff" }, | ||
| { offset: 4 / 6, color: "#0000ff" }, | ||
| { offset: 5 / 6, color: "#ff00ff" }, | ||
| { offset: 1, color: "#ff0000" }, | ||
| ]; | ||
|
|
||
| function clamp01(value: number): number { | ||
| "worklet"; | ||
| return Math.min(1, Math.max(0, value)); | ||
| } | ||
|
|
||
| function quantize(color: HsvColor): HsvColor { | ||
| return { | ||
| h: Math.round(color.h), | ||
| s: Math.round(color.s * 100) / 100, | ||
| v: Math.round(color.v * 100) / 100, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Saturation/brightness pad plus hue slider. Thumbs track on the UI thread | ||
| * via shared values; the color is committed to React state and preferences | ||
| * once on release, so dragging never re-renders the SVG surfaces. | ||
| */ | ||
| export function HsvColorPicker(props: { | ||
| readonly disabled?: boolean; | ||
| readonly value: string; | ||
| readonly onChange: (hex: string) => void; | ||
| }) { | ||
| const idPrefix = useId().replaceAll(":", ""); | ||
| const [hsv, setHsv] = useState<HsvColor>( | ||
| () => hexToHsv(props.value) ?? { h: 210, s: 0.8, v: 0.9 }, | ||
| ); | ||
| const [syncedValue, setSyncedValue] = useState(props.value); | ||
|
|
||
| // Adopt external changes (hex field, preset then custom) without fighting | ||
| // our own commits, which round-trip to the same hex. Adopt the parsed HSV | ||
| // exactly — quantizing here would drift the color off the typed hex. | ||
| if (props.value !== syncedValue) { | ||
| setSyncedValue(props.value); | ||
| const parsed = hexToHsv(props.value); | ||
| if (parsed !== null && hsvToHex(hsv) !== props.value) setHsv(parsed); | ||
| } | ||
|
|
||
| const saturation = useSharedValue(hsv.s); | ||
| const brightness = useSharedValue(hsv.v); | ||
| const huePosition = useSharedValue(hsv.h / 360); | ||
| const padWidth = useSharedValue(0); | ||
| const padHeight = useSharedValue(0); | ||
| const hueWidth = useSharedValue(0); | ||
|
|
||
| useEffect(() => { | ||
| saturation.value = hsv.s; | ||
| brightness.value = hsv.v; | ||
| huePosition.value = hsv.h / 360; | ||
| }, [brightness, hsv, huePosition, saturation]); | ||
|
|
||
| const latest = useRef({ onChange: props.onChange }); | ||
| latest.current = { onChange: props.onChange }; | ||
|
|
||
| // Commits carry a full HSV snapshot taken from the shared values at gesture | ||
| // time, so a queued callback cannot mix stale state with newer selections. | ||
| const commitColor = (huePos: number, s: number, v: number) => { | ||
| const next = quantize({ h: huePos * 360, s, v }); | ||
| setHsv(next); | ||
| latest.current.onChange(hsvToHex(next)); | ||
| }; | ||
| const commitRef = useRef(commitColor); | ||
| commitRef.current = commitColor; | ||
| const runCommit = useMemo( | ||
| () => (huePos: number, s: number, v: number) => commitRef.current(huePos, s, v), | ||
| [], | ||
| ); | ||
|
|
||
| const padGesture = useMemo(() => { | ||
| const track = (x: number, y: number) => { | ||
| "worklet"; | ||
| if (padWidth.value <= 0 || padHeight.value <= 0) return; | ||
| saturation.value = clamp01(x / padWidth.value); | ||
| brightness.value = clamp01(1 - y / padHeight.value); | ||
| }; | ||
| // Axis-independent activation: brightness-only drags are vertical, so the | ||
| // pad must win over the surrounding ScrollView in both directions. | ||
| const pan = Gesture.Pan() | ||
| .enabled(!props.disabled) | ||
| .minDistance(6) | ||
| .onUpdate((event) => track(event.x, event.y)) | ||
| .onEnd(() => { | ||
| runOnJS(runCommit)(huePosition.value, saturation.value, brightness.value); | ||
| }); | ||
| const tap = Gesture.Tap() | ||
| .enabled(!props.disabled) | ||
| .onEnd((event) => { | ||
| track(event.x, event.y); | ||
| runOnJS(runCommit)(huePosition.value, saturation.value, brightness.value); | ||
| }); | ||
| return Gesture.Race(pan, tap); | ||
| }, [brightness, huePosition, padHeight, padWidth, props.disabled, runCommit, saturation]); | ||
|
|
||
| const hueGesture = useMemo(() => { | ||
| const track = (x: number) => { | ||
| "worklet"; | ||
| if (hueWidth.value <= 0) return; | ||
| huePosition.value = clamp01(x / hueWidth.value); | ||
| }; | ||
| const pan = Gesture.Pan() | ||
| .enabled(!props.disabled) | ||
| .activeOffsetX([-6, 6]) | ||
| .failOffsetY([-12, 12]) | ||
| .onUpdate((event) => track(event.x)) | ||
| .onEnd(() => { | ||
| runOnJS(runCommit)(huePosition.value, saturation.value, brightness.value); | ||
| }); | ||
| const tap = Gesture.Tap() | ||
| .enabled(!props.disabled) | ||
| .onEnd((event) => { | ||
| track(event.x); | ||
| runOnJS(runCommit)(huePosition.value, saturation.value, brightness.value); | ||
| }); | ||
| return Gesture.Race(pan, tap); | ||
| }, [brightness, hueWidth, huePosition, props.disabled, runCommit, saturation]); | ||
|
|
||
| const padThumbStyle = useAnimatedStyle(() => ({ | ||
| transform: [ | ||
| { translateX: saturation.value * padWidth.value - THUMB_SIZE / 2 }, | ||
| { translateY: (1 - brightness.value) * padHeight.value - THUMB_SIZE / 2 }, | ||
| ], | ||
| })); | ||
| const hueThumbStyle = useAnimatedStyle(() => ({ | ||
| transform: [{ translateX: huePosition.value * hueWidth.value - THUMB_SIZE / 2 }], | ||
| })); | ||
|
|
||
| const hueColor = hsvToHex({ h: hsv.h, s: 1, v: 1 }); | ||
| const currentColor = hsvToHex(hsv); | ||
| const thumbStyle = { | ||
| borderColor: "#ffffff", | ||
| borderWidth: 2, | ||
| height: THUMB_SIZE, | ||
| shadowColor: "#000000", | ||
| shadowOffset: { height: 1, width: 0 }, | ||
| shadowOpacity: 0.25, | ||
| shadowRadius: 2, | ||
| width: THUMB_SIZE, | ||
| } as const; | ||
|
|
||
| return ( | ||
| <View className={props.disabled ? "gap-3 opacity-[0.45]" : "gap-3"}> | ||
| <GestureDetector gesture={padGesture}> | ||
| <View | ||
| accessibilityLabel="Saturation and brightness" | ||
| className="h-40 overflow-hidden rounded-2xl border border-border" | ||
| onLayout={(event) => { | ||
| padWidth.value = event.nativeEvent.layout.width; | ||
| padHeight.value = event.nativeEvent.layout.height; | ||
| }} | ||
| > | ||
| <Svg height="100%" width="100%"> | ||
| <Defs> | ||
| <LinearGradient id={`${idPrefix}-s`} x1="0" x2="1" y1="0" y2="0"> | ||
| <Stop offset="0" stopColor="#ffffff" stopOpacity={1} /> | ||
| <Stop offset="1" stopColor="#ffffff" stopOpacity={0} /> | ||
| </LinearGradient> | ||
| <LinearGradient id={`${idPrefix}-v`} x1="0" x2="0" y1="0" y2="1"> | ||
| <Stop offset="0" stopColor="#000000" stopOpacity={0} /> | ||
| <Stop offset="1" stopColor="#000000" stopOpacity={1} /> | ||
| </LinearGradient> | ||
| </Defs> | ||
| <Rect fill={hueColor} height="100%" width="100%" /> | ||
| <Rect fill={`url(#${idPrefix}-s)`} height="100%" width="100%" /> | ||
| <Rect fill={`url(#${idPrefix}-v)`} height="100%" width="100%" /> | ||
| </Svg> | ||
| <Animated.View | ||
| className="absolute left-0 top-0 rounded-full" | ||
| pointerEvents="none" | ||
| style={[{ ...thumbStyle, backgroundColor: currentColor }, padThumbStyle]} | ||
| /> | ||
| </View> | ||
| </GestureDetector> | ||
| <GestureDetector gesture={hueGesture}> | ||
| <View | ||
| accessibilityLabel="Hue" | ||
| className="h-11 justify-center" | ||
| onLayout={(event) => { | ||
| hueWidth.value = event.nativeEvent.layout.width; | ||
| }} | ||
| > | ||
| <View className="h-3 overflow-hidden rounded-full"> | ||
| <Svg height="100%" width="100%"> | ||
| <Defs> | ||
| <LinearGradient id={`${idPrefix}-hue`} x1="0" x2="1" y1="0" y2="0"> | ||
| {HUE_STOPS.map((stop) => ( | ||
| <Stop key={stop.offset} offset={stop.offset} stopColor={stop.color} /> | ||
| ))} | ||
| </LinearGradient> | ||
| </Defs> | ||
| <Rect fill={`url(#${idPrefix}-hue)`} height="100%" width="100%" /> | ||
| </Svg> | ||
| </View> | ||
| <Animated.View | ||
| className="absolute left-0 rounded-full" | ||
| pointerEvents="none" | ||
| style={[{ ...thumbStyle, backgroundColor: hueColor }, hueThumbStyle]} | ||
| /> | ||
| </View> | ||
| </GestureDetector> | ||
| </View> | ||
| ); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.