Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gap controls centered relatively to content #4334

Merged
merged 2 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ export const setFlexGapStrategy: CanvasStrategyFactory = (
resizeControl,
controlWithProps({
control: FloatingIndicator,
props: { ...props, color: colorTheme.gapControls.value },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean that gapControls should be updated?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mmmh it's still there for the striped background, maybe worth renaming it to gapControlsBg or something like that?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds pretty sensible to me at least.

props: {
...props,
color: colorTheme.brandNeonPink.value,
},
key: 'padding-value-indicator-control',
show: 'visible-except-when-other-strategy-is-active',
}),
Expand Down
122 changes: 86 additions & 36 deletions editor/src/components/canvas/controls/select-mode/flex-gap-control.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import React, { useState } from 'react'
import * as EP from '../../../../core/shared/element-path'
import type { CanvasRectangle, CanvasVector, Size } from '../../../../core/shared/math-utils'
import { size, windowPoint } from '../../../../core/shared/math-utils'
import {
boundingRectangleArray,
isFiniteRectangle,
size,
windowPoint,
} from '../../../../core/shared/math-utils'
import type { ElementPath } from '../../../../core/shared/project-file-types'
import { assertNever } from '../../../../core/shared/utils'
import { isFeatureEnabled } from '../../../../utils/feature-switches'
import { Modifier } from '../../../../utils/modifiers'
import { when } from '../../../../utils/react-conditionals'
import { useColorTheme, UtopiaStyles } from '../../../../uuiui'
Expand All @@ -25,6 +29,8 @@ import {
import { CanvasOffsetWrapper } from '../canvas-offset-wrapper'
import type { CSSNumberWithRenderedValue } from './controls-common'
import { CanvasLabel, PillHandle, useHoverWithDelay } from './controls-common'
import { MetadataUtils } from '../../../../core/model/element-metadata-utils'
import { mapDropNulls } from '../../../../core/shared/array-utils'

interface FlexGapControlProps {
selectedElement: ElementPath
Expand All @@ -37,7 +43,7 @@ export const FlexGapControlHandleTestId = 'FlexGapControlHandleTestId'
export const FlexGapControl = controlForStrategyMemoized<FlexGapControlProps>((props) => {
const { selectedElement, updatedGapValue } = props
const colorTheme = useColorTheme()
const indicatorColor = colorTheme.gapControls.value
const accentColor = colorTheme.gapControlsBg.value

const hoveredViews = useEditorState(
Substores.highlightedHoveredViews,
Expand Down Expand Up @@ -110,6 +116,40 @@ export const FlexGapControl = controlForStrategyMemoized<FlexGapControlProps>((p
flexGap.direction,
)

const contentArea = React.useMemo((): Size => {
function valueForDimension(
directions: FlexDirection[],
direction: FlexDirection,
directionSize: number,
gapSize: number,
) {
return directions.includes(direction) ? directionSize : gapSize
}

const children = MetadataUtils.getChildrenUnordered(metadata, selectedElement)
const bounds = boundingRectangleArray(
mapDropNulls(
(c) => (c.localFrame != null && isFiniteRectangle(c.localFrame) ? c.localFrame : null),
children,
),
) ?? { width: 0, height: 0 }

return {
width: valueForDimension(
['column', 'column-reverse'],
flexGap.direction,
bounds.width,
flexGapValue.renderedValuePx,
),
height: valueForDimension(
['row', 'row-reverse'],
flexGap.direction,
bounds.height,
flexGapValue.renderedValuePx,
),
}
}, [selectedElement, metadata, flexGap, flexGapValue])

return (
<CanvasOffsetWrapper>
<div data-testid={FlexGapControlTestId} style={{ pointerEvents: 'none' }}>
Expand All @@ -124,8 +164,9 @@ export const FlexGapControl = controlForStrategyMemoized<FlexGapControlProps>((p
elementHovered={elementHovered}
path={path}
bounds={bounds}
contentArea={contentArea}
flexDirection={flexGap.direction}
indicatorColor={indicatorColor}
accentColor={accentColor}
scale={scale}
backgroundShown={backgroundShown}
isDragging={isDragging}
Expand Down Expand Up @@ -167,11 +208,12 @@ interface GapControlSegmentProps {
hoverEnd: React.MouseEventHandler
onMouseDown: React.MouseEventHandler
bounds: CanvasRectangle
contentArea: Size
flexDirection: FlexDirection
gapValue: CSSNumber
elementHovered: boolean
path: string
indicatorColor: string
accentColor: string
scale: number
isDragging: boolean
backgroundShown: boolean
Expand All @@ -183,10 +225,11 @@ const GapControlSegment = React.memo<GapControlSegmentProps>((props) => {
hoverEnd,
onMouseDown,
bounds,
contentArea,
isDragging,
gapValue,
flexDirection,
indicatorColor,
accentColor: accentColor,
elementHovered,
scale,
path,
Expand Down Expand Up @@ -228,49 +271,56 @@ const GapControlSegment = React.memo<GapControlSegmentProps>((props) => {
top: bounds.y,
width: bounds.width,
height: bounds.height,
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
border: isDragging ? `${dragBorderWidth}px solid ${indicatorColor}` : undefined,
border: isDragging ? `${dragBorderWidth}px solid ${accentColor}` : undefined,
...(shouldShowBackground
? UtopiaStyles.backgrounds.stripedBackground(indicatorColor, scale)
? UtopiaStyles.backgrounds.stripedBackground(accentColor, scale)
: {}),
}}
>
<div
data-testid={FlexGapControlHandleTestId}
style={{
visibility: shouldShowHandle ? 'visible' : 'hidden',
padding: hitAreaPadding,
cursor: cursorFromFlexDirection(flexDirection),
width: contentArea.width,
height: contentArea.height,
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
}}
onMouseDown={onMouseDown}
onMouseEnter={handleHoverStartInner}
>
<div
data-testid={FlexGapControlHandleTestId}
style={{
position: 'absolute',
paddingTop: paddingIndicatorOffset,
paddingLeft: paddingIndicatorOffset,
pointerEvents: 'none',
visibility: shouldShowHandle ? 'visible' : 'hidden',
padding: hitAreaPadding,
cursor: cursorFromFlexDirection(flexDirection),
}}
onMouseDown={onMouseDown}
onMouseEnter={handleHoverStartInner}
>
{when(
shouldShowIndicator,
<CanvasLabel
value={printCSSNumber(gapValue, null)}
scale={scale}
color={colorTheme.gapControls.value}
textColor={colorTheme.white.value}
/>,
)}
<div
style={{
position: 'absolute',
paddingTop: paddingIndicatorOffset,
paddingLeft: paddingIndicatorOffset,
pointerEvents: 'none',
}}
>
{when(
shouldShowIndicator,
<CanvasLabel
value={printCSSNumber(gapValue, null)}
scale={scale}
color={colorTheme.brandNeonPink.value}
textColor={colorTheme.white.value}
/>,
)}
</div>
<PillHandle
width={width}
height={height}
pillColor={colorTheme.brandNeonPink.value}
borderWidth={borderWidth}
/>
</div>
<PillHandle
width={width}
height={height}
pillColor={colorTheme.gapControls.value}
borderWidth={borderWidth}
/>
</div>
</div>
)
Expand Down
2 changes: 1 addition & 1 deletion editor/src/uuiui/styles/theme/dark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ const darkTheme: typeof light = {
codeEditorGrid: createUtopiColor('#6d705b'),

// Gap controls
gapControls: darkBase.brandNeonGreen,
gapControlsBg: darkBase.brandNeonGreen,
}

export const dark = enforceUtopiColorTheme(darkTheme)
2 changes: 1 addition & 1 deletion editor/src/uuiui/styles/theme/light.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ const lightTheme = {
codeEditorGrid: createUtopiColor('#6d705b'),

// Gap controls
gapControls: createUtopiColor('#FFA500'),
gapControlsBg: createUtopiColor('#FFA500'),
}

// all values in light must be of the type UtopiColor! This will break if you made a mistake.
Expand Down