package scoop.examples.ui.shadcn import scoop.graphics.* import scoop.ui.* import scoop.ui.component.* import scoop.ui.component.column as makeColumn import scoop.ui.component.flex as makeFlex import scoop.ui.component.row as makeRow import scoop.ui.component.wrap as makeWrap import scoop.ui.component.frame as makeFrame import scoop.ui.component.scrollArea as makeScrollArea import scoop.ui.style.* import scoop.window.SoftwareKeyboardState import scoop.window.WindowMetrics import scoop.window.softwareKeyboard /** A gallery connection owns the models and theme shared by all its pages. */ public class GalleryScene public constructor() { private val model = GalleryModel() public val theme: ThemeSource get() = model.source public fun root(scope: ComponentScope): Component = Gallery(model, scope) } private enum GalleryDestination { Initial, Catalog, Detail } internal class Gallery(private val model: GalleryModel, scope: ComponentScope) : Component(scope) { private val keyboardInset = state(0.0) private val destination = state(GalleryDestination.Initial) private val appearance = state(false) public override fun onMount() { val window = scope.nativeWindow updateKeyboard(dockedInset(window.metrics, window.softwareKeyboard)) scope .observeWindow(window) { event -> when (event) { case GeometryChanged(val metrics) -> updateKeyboard(dockedInset(Some(metrics), window.softwareKeyboard)) case SoftwareKeyboardChanged(val keyboard) -> updateKeyboard(dockedInset(window.metrics, keyboard)) else -> {} } } } private fun updateKeyboard(inset: Double) { if (keyboardInset.value != inset) keyboardInset.value = inset } private fun selectEntry(entry: Demo) { if (entry == Demo.AppWorkspace || entry == Demo.Sidebar) { val width = scope.nativeWindow.metrics?.logicalSize?.width if (compactGallery(Constraints(maxWidth = width))) { if (entry == Demo.AppWorkspace) model.appWorkspace.navigation.value = false else model.sidebars.open.value = false } } model.selected.value = entry destination.value = GalleryDestination.Detail appearance.value = false } public override fun render(): View { val model = this.model val entries = model.entries() val current = model.current(entries) val route = destination.value val inDetail = route == GalleryDestination.Detail && current != None val header = galleryHeader( model, current, inDetail, appearance.value, { destination.value = GalleryDestination.Catalog }, { appearance.value = !appearance.value } ) val catalog = galleryNavigation(model, entries, current) { entry -> selectEntry(entry) } val content = when (current) { case Some(val entry) -> if (route == GalleryDestination.Catalog) galleryText(model, "Select an example", 24.0, 600) else galleryDemo(model, entry) case None -> galleryText(model, "No matching examples", 24.0, 600) } val detail = makeScrollArea( makeFrame(content) { maxWidth = Some(960.0) padding = Some(Insets.all(6.0)) alignment = Alignment(AxisAlignment.Center, AxisAlignment.Start) } ) val panes = galleryPanes(catalog, detail, inDetail, keyboardInset.value) return cachedScene( direction( makeFrame( makeColumn { alignment = CrossAlignment.Stretch add(header) item(panes) { basis = FlexBasis.Fixed(0.0) grow = 1.0 } } ) { style { background( Some( Paint( model.source.current.value.tokens.palette.background.background ) ) ) } }, model.direction.value ) ) } } private fun compactGallery(constraints: Constraints): Boolean = when (constraints.maxWidth) { case Some(val width) -> width < 720.0 case None -> false } private fun galleryPanes(catalog: View, detail: View, inDetail: Boolean, keyboard: Double): View = makeFlex { val narrow = FlexLayout( FlexAxis.Vertical, 8.0, CrossAlignment.Stretch, padding = Insets(12.0, 0.0, 12.0, 12.0 + keyboard) ) val wide = FlexLayout( FlexAxis.Horizontal, 32.0, CrossAlignment.Stretch, padding = Insets(28.0, 0.0, 28.0, 20.0 + keyboard) ) val fill = FlexItemOptions(basis = FlexBasis.Fixed(0.0), grow = 1.0) val rail = FlexItemOptions(basis = FlexBasis.Fixed(206.0), shrink = 0.0) layoutFor { limits -> if (compactGallery(limits)) narrow else wide } itemFor(catalog) { limits -> if (!compactGallery(limits)) Some(rail) else if (inDetail) None else Some(fill) } itemFor(detail) { limits -> if (compactGallery(limits) && !inDetail) None else Some(fill) } } private fun dockedInset(metrics: WindowMetrics?, keyboard: SoftwareKeyboardState): Double { val window = metrics ?: return 0.0 val overlap = when (keyboard) { case Shown(Some(val rect)) -> rect else -> return 0.0 } val size = window.logicalSize if (overlap.origin.x > 0.0 || overlap.size.width < size.width || overlap.origin.y + overlap.size.height < size.height ) return 0.0 val covered = size.height - window.safeAreaInsets.bottom - overlap.origin.y return if (covered > 0.0) covered else 0.0 } private fun galleryHeader( model: GalleryModel, current: Demo?, inDetail: Boolean, expanded: Boolean, back: () -> Unit, toggle: () -> Unit ): View = makeFlex { val narrow = FlexLayout( FlexAxis.Vertical, 8.0, CrossAlignment.Stretch, padding = Insets(12.0, 12.0, 12.0, 8.0) ) val wide = FlexLayout( FlexAxis.Horizontal, 12.0, CrossAlignment.Center, padding = Insets(28.0, 20.0, 28.0, 20.0) ) val natural = FlexItemOptions() val stretch = FlexItemOptions(grow = 1.0) layoutFor { limits -> if (compactGallery(limits)) narrow else wide } itemFor( makeRow { spacing = 8.0 alignment = CrossAlignment.Center if (inDetail) button("Examples", back) { variant = ButtonVariant.Outline semantics = ComponentSemantics(automationId = Some("gallery-back")) } item( galleryText( model, if (inDetail && current != None) demoTitle(current!!) else "UI Gallery", 18.0, 600 ) ) { grow = 1.0 } button("Appearance", toggle) { variant = ButtonVariant.Outline } } ) { limits -> if (compactGallery(limits)) Some(natural) else None } itemFor(galleryText(model, "Scoop / UI Gallery", 22.0, 600)) { limits -> if (compactGallery(limits)) None else Some(stretch) } itemFor(galleryAppearance(model)) { limits -> if (!compactGallery(limits) || expanded) Some(natural) else None } } private fun galleryAppearance(model: GalleryModel): View { val theme = if (model.mode.value == ThemeMode.Light) "Dark theme" else "Light theme" val direction = if (model.direction.value == LayoutDirection.Ltr) "Switch to RTL" else "Switch to LTR" return makeWrap { spacing = 6.0 lineSpacing = 6.0 alignment = CrossAlignment.Center button(theme, { model.toggleTheme() }) { variant = ButtonVariant.Outline } button(direction, { model.toggleDirection() }) { variant = ButtonVariant.Outline } button( if (model.reduced.value) "Full motion" else "Reduce motion", { model.toggleMotion() } ) { variant = ButtonVariant.Outline } if (model.hasSystemColors) button( if (model.usesSystemColors) "Colors: System" else "Colors: Preset", { model.toggleColors() } ) { variant = ButtonVariant.Outline } select( binding( { Some(model.style.value) }, { next -> model.selectStyle(next ?: model.style.value) } ) ) { key = ViewKey.Text("gallery-style") width = Some(190.0) position = SelectPosition.Popup focus { handle = Some(model.styleFocus) } semantics = ComponentSemantics( name = Some("Style"), automationId = Some("gallery-style") ) for (value in model.styles()) item( Choice( ViewKey.Text(galleryStyleTitle(value)), value, galleryStyleTitle(value) ) ) } } } private fun galleryNavigation( model: GalleryModel, entries: Array, current: Demo?, select: (Demo) -> Unit ): View = makeFrame( makeScrollArea( makeColumn { spacing = 12.0 alignment = CrossAlignment.Stretch padding = Insets(8.0, 4.0, 8.0, 4.0) input(model.query) { placeholder = "Search examples..." semantics = ComponentSemantics( name = Some("Search examples"), automationId = Some("gallery-search") ) } if (model.query.text.byteLength > 0L) button("Clear search", { model.query.replaceAll("") }) { variant = ButtonVariant.Ghost semantics = ComponentSemantics(automationId = Some("gallery-clear-search")) } for (applications in [true, false]) { var present = false for (entry in entries) if (applicationDemo(entry) == applications) present = true if (!present) continue add( galleryText( model, if (applications) "APPLICATIONS" else "COMPONENTS", 11.0, 600, true ) ) flexGrid(152.0) { key = ViewKey.Text(if (applications) "applications" else "components") columnGap = 8.0 rowGap = 8.0 for (entry in entries) if (applicationDemo(entry) == applications) button(demoTitle(entry), { select(entry) }) { key = ViewKey.Text(demoTitle(entry)) semantics = ComponentSemantics( automationId = Some("gallery-entry-" + demoTitle(entry)) ) variant = if (current == Some(entry)) ButtonVariant.Default else ButtonVariant.Outline style { surface { suggestedSize = Some(Size(0.0, 44.0)) } } } } } } ) { state = Some(model.catalogScroll) scrollbarBehavior { placement = ScrollbarPlacement.Gutter } } ) { alignment = Alignment(AxisAlignment.Start, AxisAlignment.Start) } internal fun galleryText( model: GalleryModel, value: String, size: Double = 14.0, weight: Int = 400, muted: Boolean = false ): View { val palette = model.source.current.value.tokens.palette return label(value) { style { fontSize = Some(size) this.weight = Some(weight) color = Some( if (muted) palette.muted.foreground else palette.background.foreground ) } } }