Skip to content

Commit

Permalink
Stretch type tags to fill available space (#2635)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment authored Feb 29, 2024
1 parent 2f0da37 commit b721c94
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 29 deletions.
61 changes: 44 additions & 17 deletions src/renderer/components/TypeTag.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ export const TypeTag = memo(
<Tag
bgColor="var(--tag-bg)"
color="var(--tag-fg)"
display="inline-block"
fontSize="x-small"
fontStyle={isOptional ? 'italic' : undefined}
height="15px"
Expand All @@ -287,39 +288,54 @@ export const TypeTag = memo(
export interface TypeTagsProps {
type: Type;
isOptional: boolean;
longText?: boolean;
}

const Punctuation = memo(({ children }: React.PropsWithChildren<unknown>) => {
return <span style={{ opacity: '50%' }}>{children}</span>;
});

const TagRenderer = memo(({ tag }: { tag: TagValue }) => {
interface TagRendererProps {
tag: TagValue;
longText: boolean;
}
const TagRenderer = memo(({ tag, longText }: TagRendererProps) => {
const { kind, value } = tag;

let tt: string | undefined;
let text: NonNullable<ReactNode>;
let direction: 'ltr' | 'rtl' = 'ltr';

switch (kind) {
case 'path': {
tt = value;
const maxLength = 14;
text = (
<>
{value.length > maxLength && <Punctuation></Punctuation>}
{value.slice(Math.max(0, value.length - maxLength))}
</>
);
if (longText) {
text = value;
direction = 'rtl';
} else {
const maxLength = 14;
text = (
<>
{value.length > maxLength && <Punctuation></Punctuation>}
{value.slice(Math.max(0, value.length - maxLength))}
</>
);
}
break;
}
case 'string': {
tt = value;
const maxLength = 16;
text = (
<>
{value.slice(0, maxLength)}
{value.length > maxLength && <Punctuation></Punctuation>}
</>
);
if (longText) {
text = value;
} else {
const maxLength = 16;
text = (
<>
{value.slice(0, maxLength)}
{value.length > maxLength && <Punctuation></Punctuation>}
</>
);
}
break;
}
case 'literal': {
Expand All @@ -344,12 +360,22 @@ const TagRenderer = memo(({ tag }: { tag: TagValue }) => {
px={2}
textAlign="center"
>
<TypeTag>{text}</TypeTag>
{longText ? (
<TypeTag
overflow="hidden"
style={{ direction }}
textOverflow="ellipsis"
>
{text}
</TypeTag>
) : (
<TypeTag>{text}</TypeTag>
)}
</Tooltip>
);
});

export const TypeTags = memo(({ type, isOptional }: TypeTagsProps) => {
export const TypeTags = memo(({ type, isOptional, longText = false }: TypeTagsProps) => {
const { t } = useTranslation();
const tags = getTypeText(withoutNull(type));

Expand All @@ -358,6 +384,7 @@ export const TypeTags = memo(({ type, isOptional }: TypeTagsProps) => {
{tags.map((tag) => (
<TagRenderer
key={`${tag.kind};${tag.value}`}
longText={longText}
tag={tag}
/>
))}
Expand Down
21 changes: 9 additions & 12 deletions src/renderer/components/outputs/GenericOutput.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
import { Center, Flex, Spacer, Text } from '@chakra-ui/react';
import { Flex, Spacer, Text } from '@chakra-ui/react';
import { memo } from 'react';
import { TypeTags } from '../TypeTag';
import { OutputProps } from './props';

export const GenericOutput = memo(({ output, type }: OutputProps) => {
return (
<Flex
h="full"
minH="2rem"
alignItems="center"
h="2rem"
style={{ contain: 'layout size' }}
verticalAlign="middle"
w="full"
>
<Spacer />
<Center
h="2rem"
verticalAlign="middle"
>
<TypeTags
isOptional={false}
type={type}
/>
</Center>
<TypeTags
longText
isOptional={false}
type={type}
/>
<Text
h="full"
lineHeight="2rem"
Expand Down

0 comments on commit b721c94

Please sign in to comment.