Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464// Building blocks shared by the widget layouts. They mirror the webmail pieces// the mockups copied: list rows, avatars, pills, event chips, buttons.// react-native-android-widget turns these into RemoteViews, so only its own// components (FlexWidget, TextWidget, SvgWidget, OverlapWidget) can appear.
import React from 'react';import { FlexWidget, ImageWidget, OverlapWidget, SvgWidget, TextWidget, type ColorProp, type FlexWidgetStyle, type TextWidgetStyle,} from 'react-native-android-widget';import { chipBackground, normalizeHex, type WidgetPalette } from './theme';import { filledIcon, icon, type FilledIconName, type IconName } from './icons';import type { WidgetClick } from './clicks';
/** Corner radius of the widget surface; `widget_radius` in android/app/src/main/res/values/dimens.xml. */export const WIDGET_RADIUS = 16;
/** * The widget's background. Its edge and the clip to rounded corners come from * the root layout (android/app/src/main/res/layout/rn_widget.xml), which has the * launcher's real size; this bitmap is drawn at the reported one and can be cut off. */export function Surface({ p, children, style, click,}: { p: WidgetPalette; children?: React.ReactNode; style?: FlexWidgetStyle; click?: WidgetClick;}) { return ( <FlexWidget {...click} style={{ height: 'match_parent', width: 'match_parent', backgroundColor: p.bg, borderRadius: WIDGET_RADIUS, overflow: 'hidden', ...style, }} > {children} </FlexWidget> );}
export function Txt({ text, color, size = 14, weight = '400', lines = 1, style, click,}: { text: string; color: ColorProp; size?: number; weight?: TextWidgetStyle['fontWeight']; lines?: number; style?: TextWidgetStyle; click?: WidgetClick;}) { return ( <TextWidget {...click} text={text} maxLines={lines} truncate="END" style={{ color, fontSize: size, fontWeight: weight, ...style }} /> );}
export function Icon({ name, color, size = 16, style, click,}: { name: IconName; color: string; size?: number; style?: FlexWidgetStyle; click?: WidgetClick;}) { return <SvgWidget {...click} svg={icon(name, color)} style={{ width: size, height: size, ...style }} />;}
export function FilledIcon({ name, color, size = 14 }: { name: FilledIconName; color: string; size?: number }) { return <SvgWidget svg={filledIcon(name, color)} style={{ width: size, height: size }} />;}
export function Spacer({ size, horizontal }: { size?: number; horizontal?: boolean }) { if (size === undefined) return <FlexWidget style={{ flex: 1 }} />; return <FlexWidget style={horizontal ? { width: size, height: 1 } : { height: size, width: 1 }} />;}
export function Dot({ color, size = 8 }: { color: ColorProp; size?: number }) { return <FlexWidget style={{ width: size, height: size, borderRadius: size / 2, backgroundColor: color }} />;}
export function Divider({ p }: { p: WidgetPalette }) { return <FlexWidget style={{ height: 1, width: 'match_parent', backgroundColor: p.border }} />;}
/** * The webmail's sender avatar: the contact photo or the sender's logo (on * white) when the snapshot has one (./avatar-images.ts), else the initials on * the hashed avatar colour. The initials are drawn underneath, so a picture * that does not draw still leaves them. */export function Avatar({ initials, color, size = 32, ring, image,}: { initials: string; color: string; size?: number; /** Draws a ring in this colour, for stacked avatars. */ ring?: ColorProp; image?: { src: string; logo: boolean } | null;}) { const face = ( <FlexWidget style={{ width: size, height: size, borderRadius: size / 2, backgroundColor: normalizeHex(color), justifyContent: 'center', alignItems: 'center', ...(ring ? { borderWidth: 2, borderColor: ring } : {}), }} > <TextWidget text={initials} allowFontScaling={false} style={{ color: '#ffffff', fontSize: Math.round(size * 0.38), fontWeight: '500' }} /> </FlexWidget> ); if (!image) return face; // Inside the ring, when there is one. const inner = ring ? size - 4 : size; return ( <OverlapWidget style={{ width: size, height: size }}> {face} <FlexWidget style={{ width: size, height: size, borderRadius: size / 2, justifyContent: 'center', alignItems: 'center', ...(ring ? { backgroundColor: ring } : {}), }} > <FlexWidget style={{ width: inner, height: inner, borderRadius: inner / 2, backgroundColor: image.logo ? '#ffffff' : normalizeHex(color) }}> <ImageWidget image={image.src as `data:image${string}`} imageWidth={inner} imageHeight={inner} radius={inner / 2} resizeMode="cover" /> </FlexWidget> </FlexWidget> </OverlapWidget> );}
/** `rounded-full bg-muted px-2 py-0.5 text-[11px] font-medium` */export function Pill({ text, bg, color, size = 11,}: { text: string; bg: ColorProp; color: ColorProp; size?: number;}) { return ( <FlexWidget style={{ backgroundColor: bg, borderRadius: 999, paddingHorizontal: 8, paddingVertical: 2 }}> <TextWidget text={text} maxLines={1} style={{ color, fontSize: size, fontWeight: '500' }} /> </FlexWidget> );}
/** Month/week-view event chip: calendar colour at ~14%, 3dp left edge, text in the colour. */export function EventChip({ title, time, color, height = 24, style, click,}: { title: string; time?: string; color: string; height?: number; style?: FlexWidgetStyle; click?: WidgetClick;}) { const hex = normalizeHex(color); return ( <FlexWidget {...click} style={{ flexDirection: 'row', alignItems: 'center', height, width: 'match_parent', backgroundColor: chipBackground(hex), borderLeftWidth: 3, borderLeftColor: hex, borderRadius: 3, paddingLeft: 6, paddingRight: 4, ...style, }} > {time ? ( <TextWidget text={`${time} `} maxLines={1} style={{ color: `${hex}cc` as ColorProp, fontSize: 12, fontWeight: '500' }} /> ) : null} <TextWidget text={title} maxLines={1} truncate="END" style={{ color: hex, fontSize: 12, fontWeight: '500' }} /> </FlexWidget> );}
/** The 4dp colour bar the agenda puts between the time and the title. */export function ColorBar({ color, height = 36 }: { color: string; height?: number }) { return <FlexWidget style={{ width: 4, height, borderRadius: 2, backgroundColor: normalizeHex(color) }} />;}
export function Button({ p, label, iconName, variant = 'outline', click, height = 32, flex, slop = 0,}: { p: WidgetPalette; label?: string; iconName?: IconName; variant?: 'primary' | 'outline'; click?: WidgetClick; height?: number; flex?: number; /** * Transparent tap area above and below the drawn button, so a 32dp button * still takes a 48dp tap. Take the same amount off the spacing around it. */ slop?: number | { top: number; bottom: number };}) { const primary = variant === 'primary'; const [slopTop, slopBottom] = typeof slop === 'number' ? [slop, slop] : [slop.top, slop.bottom]; const fg = primary ? p.primaryFg : p.fg; const face = ( <FlexWidget {...(slop ? {} : click)} style={{ flexDirection: 'row', justifyContent: 'center', alignItems: 'center', height, ...(slop && flex ? { width: 'match_parent' as const } : flex ? { flex } : { paddingHorizontal: label ? 12 : 0, width: label ? 'wrap_content' : height }), borderRadius: 6, backgroundColor: primary ? p.primary : p.bg, ...(primary ? {} : { borderWidth: 1, borderColor: p.input }), }} > {iconName ? <Icon name={iconName} color={fg} size={15} /> : null} {iconName && label ? <Spacer size={6} horizontal /> : null} {label ? <TextWidget text={label} maxLines={1} style={{ color: fg, fontSize: 13, fontWeight: '500' }} /> : null} </FlexWidget> ); if (!slop) return face; return ( <FlexWidget {...click} style={{ paddingTop: slopTop, paddingBottom: slopBottom, ...(flex ? { flex } : { width: label ? 'wrap_content' : height }), }} > {face} </FlexWidget> );}
/** Round primary action (compose, new event), the webmail's floating button. */export function Fab({ p, iconName, size = 32, click, slop = 0,}: { p: WidgetPalette; iconName: IconName; size?: number; click?: WidgetClick; /** Transparent tap area around the button (see Button). */ slop?: number;}) { const face = ( <FlexWidget {...(slop ? {} : click)} style={{ width: size, height: size, borderRadius: size / 2, backgroundColor: p.primary, justifyContent: 'center', alignItems: 'center', }} > <Icon name={iconName} color={p.primaryFg} size={Math.round(size / 2)} /> </FlexWidget> ); if (!slop) return face; return ( <FlexWidget {...click} style={{ padding: slop, justifyContent: 'center', alignItems: 'center' }}> {face} </FlexWidget> );}
/** Icon-only ghost button (search in a header). */export function GhostIcon({ p, iconName, click, size = 32,}: { p: WidgetPalette; iconName: IconName; click?: WidgetClick; size?: number;}) { return ( <FlexWidget {...click} style={{ width: size, height: size, justifyContent: 'center', alignItems: 'center' }}> <Icon name={iconName} color={p.muted} size={18} /> </FlexWidget> );}
/** Widget header bar: icon, title, optional count, trailing actions, bottom rule. */export function Header({ p, iconName, iconColor, title, count, trailing, height = 40, click,}: { p: WidgetPalette; iconName?: IconName; iconColor?: string; title: string; count?: string; trailing?: React.ReactNode; height?: number; click?: WidgetClick;}) { return ( <FlexWidget style={{ width: 'match_parent' }}> <FlexWidget {...click} style={{ flexDirection: 'row', alignItems: 'center', width: 'match_parent', height, paddingLeft: 14, paddingRight: 8, }} > {iconName ? <Icon name={iconName} color={iconColor ?? p.fg} size={16} /> : null} {iconName ? <Spacer size={8} horizontal /> : null} {/* The title gives way (truncates) before the actions on the right. */} <FlexWidget style={{ flex: 1, flexDirection: 'row', alignItems: 'center' }}> <Txt text={title} color={p.fg} size={14} weight="600" /> {count ? <Spacer size={6} horizontal /> : null} {count ? <Txt text={count} color={p.muted} size={12} /> : null} </FlexWidget> {trailing} </FlexWidget> <Divider p={p} /> </FlexWidget> );}
/** Uppercase section label (`text-xs font-medium uppercase tracking-wider`). */export function SectionLabel({ p, text }: { p: WidgetPalette; text: string }) { return ( <TextWidget text={text.toUpperCase()} maxLines={1} style={{ color: p.muted, fontSize: 11, fontWeight: '500', letterSpacing: 0.05, paddingHorizontal: 14, paddingTop: 8, paddingBottom: 4 }} /> );}
/** Centred message for empty, signed-out and error states. */export function Placeholder({ p, iconName, title, body, click,}: { p: WidgetPalette; iconName: IconName; title: string; body?: string; click?: WidgetClick;}) { return ( <FlexWidget {...click} style={{ flex: 1, width: 'match_parent', justifyContent: 'center', alignItems: 'center', padding: 12 }} > <Icon name={iconName} color={p.muted} size={22} /> <Spacer size={6} /> <TextWidget text={title} maxLines={2} style={{ color: p.fg, fontSize: 13, fontWeight: '500', textAlign: 'center' }} /> {body ? ( <TextWidget text={body} maxLines={3} style={{ color: p.muted, fontSize: 12, textAlign: 'center', marginTop: 2 }} /> ) : null} </FlexWidget> );}
/** Wraps children so an absolutely placed dot/badge can sit on top (FrameLayout). */export function Overlay({ children, style }: { children?: React.ReactNode; style?: FlexWidgetStyle }) { return <OverlapWidget style={style}>{children}</OverlapWidget>;}