|
| 1 | +import { Button, Grid, IconButton, SvgIcon } from "@mui/material"; |
| 2 | +import { useContext } from "react"; |
| 3 | +import { ViewerContext } from "."; |
| 4 | +import { Message } from "./WebsocketMessages"; |
| 5 | + |
| 6 | +import * as Icons from "@mui/icons-material"; |
| 7 | + |
| 8 | +// Type helpers. |
| 9 | +type IconName = keyof typeof Icons; |
| 10 | +type ArrayElement<ArrayType extends readonly unknown[]> = |
| 11 | + ArrayType extends readonly (infer ElementType)[] ? ElementType : never; |
| 12 | +type NoNull<T> = Exclude<T, null>; |
| 13 | +type TitlebarContent = NoNull< |
| 14 | + (Message & { type: "ThemeConfigurationMessage" })["titlebar_content"] |
| 15 | +>; |
| 16 | + |
| 17 | +// We inherit props directly from message contents. |
| 18 | +export function TitlebarButton( |
| 19 | + props: ArrayElement<NoNull<TitlebarContent["buttons"]>> |
| 20 | +) { |
| 21 | + if (props.icon !== null && props.text === null) { |
| 22 | + return ( |
| 23 | + <IconButton href={props.href ?? ""}> |
| 24 | + <SvgIcon component={Icons[props.icon as IconName] ?? null} /> |
| 25 | + </IconButton> |
| 26 | + ); |
| 27 | + } |
| 28 | + return ( |
| 29 | + <Button |
| 30 | + variant={props.variant ?? "contained"} |
| 31 | + href={props.href ?? ""} |
| 32 | + sx={{ |
| 33 | + marginY: "0.4em", |
| 34 | + marginX: "0.125em", |
| 35 | + alignItems: "center", |
| 36 | + }} |
| 37 | + size="small" |
| 38 | + target="_blank" |
| 39 | + startIcon={ |
| 40 | + props.icon ? ( |
| 41 | + <SvgIcon component={Icons[props.icon as IconName] ?? null} /> |
| 42 | + ) : null |
| 43 | + } |
| 44 | + > |
| 45 | + {props.text ?? ""} |
| 46 | + </Button> |
| 47 | + ); |
| 48 | +} |
| 49 | + |
| 50 | +export function TitlebarImage(props: NoNull<TitlebarContent["image"]>) { |
| 51 | + const image = ( |
| 52 | + <img |
| 53 | + src={props.image_url} |
| 54 | + alt={props.image_alt} |
| 55 | + style={{ |
| 56 | + height: "2em", |
| 57 | + marginLeft: "0.125em", |
| 58 | + marginRight: "0.125em", |
| 59 | + }} |
| 60 | + /> |
| 61 | + ); |
| 62 | + if (props.href == null) { |
| 63 | + return image; |
| 64 | + } |
| 65 | + return <a href={props.href}>{image}</a>; |
| 66 | +} |
| 67 | + |
| 68 | +export function Titlebar() { |
| 69 | + const viewer = useContext(ViewerContext)!; |
| 70 | + const content = viewer.useGui((state) => state.theme.titlebar_content); |
| 71 | + |
| 72 | + if (content == null) { |
| 73 | + return null; |
| 74 | + } |
| 75 | + |
| 76 | + const buttons = content.buttons; |
| 77 | + const imageData = content.image; |
| 78 | + |
| 79 | + return ( |
| 80 | + <Grid |
| 81 | + container |
| 82 | + sx={{ |
| 83 | + width: "100%", |
| 84 | + zIndex: "1000", |
| 85 | + backgroundColor: "rgba(255, 255, 255, 0.85)", |
| 86 | + borderBottom: "1px solid", |
| 87 | + borderBottomColor: "divider", |
| 88 | + direction: "row", |
| 89 | + justifyContent: "space-between", |
| 90 | + alignItems: "center", |
| 91 | + paddingX: "0.875em", |
| 92 | + height: "2.5em", |
| 93 | + }} |
| 94 | + > |
| 95 | + <Grid |
| 96 | + item |
| 97 | + xs="auto" |
| 98 | + component="div" |
| 99 | + sx={{ |
| 100 | + display: "flex", |
| 101 | + direction: "row", |
| 102 | + alignItems: "center", |
| 103 | + justifyContent: "left", |
| 104 | + overflow: "visible", |
| 105 | + }} |
| 106 | + > |
| 107 | + {buttons?.map((btn) => TitlebarButton(btn))} |
| 108 | + </Grid> |
| 109 | + <Grid |
| 110 | + item |
| 111 | + xs={3} |
| 112 | + component="div" |
| 113 | + sx={{ |
| 114 | + display: "flex", |
| 115 | + direction: "row", |
| 116 | + alignItems: "center", |
| 117 | + justifyContent: "right", |
| 118 | + }} |
| 119 | + > |
| 120 | + {imageData !== null ? TitlebarImage(imageData) : null} |
| 121 | + </Grid> |
| 122 | + </Grid> |
| 123 | + ); |
| 124 | +} |
0 commit comments