import { Button, Glass, Pressable, Progress, Segmented, SFSymbol, Switch, WindowDragArea } from "@effect/ui-native/kit" import * as Native from "@effect/ui-native/Native" import * as Schema from "effect/Schema" import * as UiSchema from "effect/ui/UiSchema" // Registered by each app: `AppBadge.swift` with AppKit in `native-macos`, and // with UIKit in `native`. const Badge = Native.hostedView< { readonly count: number; readonly badgeColor: string }, { readonly clear: {} } >("AppBadge") const Message = UiSchema.TaggedEnum({ Picked: { index: Schema.Number }, Stepped: { by: Schema.Number }, Added: {}, Cleared: {}, Toggled: { checked: Schema.Boolean } }) const tabs = ["All", "Open", "Done"] export const ControlsDemo = Native.component({ name: "ControlsDemo", props: Schema.Struct({ apple: Schema.Boolean }), model: Schema.Struct({ tab: Schema.Number, count: Schema.Number, done: Schema.Boolean }), message: Message }).make({ init: () => ({ tab: 0, count: 2, done: false }), update: { Picked: ($, { index }) => $.evolve({ tab: index }), Stepped: ($, { by }) => $.evolve({ count: (count) => Math.max(0, count + by) }), Added: ($) => $.evolve({ count: (count) => count + 1 }), Cleared: ($) => $.evolve({ count: 0 }), Toggled: ($, { checked }) => $.evolve({ done: checked }) }, view: (h, model) => h.view .style({ gap: 16, padding: 12 }) .children( // On macOS, drag the window by this bar. On iOS and Android it is a view. WindowDragArea(h) .style({ height: 52, justifyContent: "center" }) .children( Glass(h, { props: { cornerRadius: 22 } }) .style({ flexDirection: "row", alignItems: "center", gap: 8, padding: 6, alignSelf: "flex-start" }) .children( SFSymbol(h, { props: { name: "checklist", size: 17, weight: "medium", accessibilityLabel: "Tasks" }, fallback: h.text.children("☑") }).style({ marginLeft: 6 }), h.text.children(`${model.count} tasks`), Button(h, { props: { symbol: "plus", accessibilityLabel: "New task", tint: "rgba(115, 115, 115, 0.4)" }, onActivate: Message.Added(), fallback: Pressable(h, { onPress: Message.Added() }) .style({ paddingHorizontal: 12, paddingVertical: 6 }) .children(h.text.children("+")) }) ) ), // Android has no segmented control. Segmented(h, { props: { segments: tabs, selected: model.tab }, onChange: ({ index }) => Message.Picked({ index }) }), h.text.children(`Showing: ${tabs[model.tab]}`), Segmented(h, { props: { segments: [{ label: "Fewer", symbol: "minus" }, { label: "More", symbol: "plus" }], momentary: true, size: "small" }, onActivate: ({ index }) => Message.Stepped({ by: index === 0 ? -1 : 1 }) }).style({ width: 96 }), h.view .style({ flexDirection: "row", alignItems: "center", gap: 12 }) .children( Progress(h, { props: { value: Math.min(model.count / 10, 1), shape: "circle" } }), Progress(h, { props: { value: Math.min(model.count / 10, 1) } }) .style({ flex: 1 }) ), h.view .style({ flexDirection: "row", alignItems: "center", gap: 12 }) .children( // A checkbox on macOS, a switch on iOS and Android. Switch(h, { props: { checked: model.done, appearance: "checkbox" }, onToggle: ({ checked }) => Message.Toggled({ checked }) }), h.text.children(model.done ? "Done" : "Not done"), Button(h, { props: { label: "Clear", symbol: "trash", bezel: "plain" }, onActivate: Message.Cleared(), fallback: Pressable(h, { onPress: Message.Cleared() }).children( h.text.children("Clear") ) }) ), // The app's own view, which only iOS and macOS register. h.props.apple && Badge(h) .props({ count: model.count, badgeColor: "#2563eb" }) .on("clear", Message.Cleared()) ) })