Enable/disable "start sketch", "edit sketch" and "extrude" appropriately (#1449)

* test that fails for when to enable extrude and sketch features

* add fix to make test pass

* A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu)

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
This commit is contained in:
Kurt Hutten
2024-02-19 17:23:03 +11:00
committed by GitHub
parent ad7c544754
commit de5885ce0b
7 changed files with 155 additions and 15 deletions

View File

@ -5,6 +5,8 @@ import { useModelingContext } from 'hooks/useModelingContext'
import { useCommandsContext } from 'hooks/useCommandsContext'
import { ActionButton } from 'components/ActionButton'
import usePlatform from 'hooks/usePlatform'
import { isSingleCursorInPipe } from 'lang/queryAst'
import { kclManager } from 'lang/KclSingleton'
export const Toolbar = () => {
const platform = usePlatform()
@ -13,14 +15,15 @@ export const Toolbar = () => {
const toolbarButtonsRef = useRef<HTMLUListElement>(null)
const bgClassName =
'group-enabled:group-hover:bg-energy-10 group-pressed:bg-energy-10 dark:group-enabled:group-hover:bg-chalkboard-80 dark:group-pressed:bg-chalkboard-80'
const pathId = useMemo(
() =>
isCursorInSketchCommandRange(
engineCommandManager.artifactMap,
context.selectionRanges
),
[engineCommandManager.artifactMap, context.selectionRanges]
)
const pathId = useMemo(() => {
if (!isSingleCursorInPipe(context.selectionRanges, kclManager.ast)) {
return false
}
return isCursorInSketchCommandRange(
engineCommandManager.artifactMap,
context.selectionRanges
)
}, [engineCommandManager.artifactMap, context.selectionRanges])
function handleToolbarButtonsWheelEvent(ev: WheelEvent<HTMLSpanElement>) {
const span = toolbarButtonsRef.current
@ -50,7 +53,9 @@ export const Toolbar = () => {
<li className="contents">
<ActionButton
Element="button"
onClick={() => send({ type: 'Enter sketch' })}
onClick={() =>
send({ type: 'Enter sketch', data: { forceNewSketch: true } })
}
icon={{
icon: 'sketch',
bgClassName,

View File

@ -37,6 +37,7 @@ import { sceneInfra } from 'clientSideScene/sceneInfra'
import { getSketchQuaternion } from 'clientSideScene/sceneEntities'
import { startSketchOnDefault } from 'lang/modifyAst'
import { Program } from 'lang/wasm'
import { isSingleCursorInPipe } from 'lang/queryAst'
type MachineContext<T extends AnyStateMachine> = {
state: StateFrom<T>
@ -182,7 +183,10 @@ export const ModelingMachineProvider = ({
return canExtrudeSelection(selectionRanges)
},
'Selection is one face': ({ selectionRanges }) => {
'Selection is on face': ({ selectionRanges }, { data }) => {
if (data?.forceNewSketch) return false
if (!isSingleCursorInPipe(selectionRanges, kclManager.ast))
return false
return !!isCursorInSketchCommandRange(
engineCommandManager.artifactMap,
selectionRanges

View File

@ -1,5 +1,5 @@
import { ToolTip } from '../useStore'
import { Selection } from 'lib/selections'
import { Selection, Selections } from 'lib/selections'
import {
BinaryExpression,
Program,
@ -558,3 +558,24 @@ export function hasExtrudeSketchGroup({
const varValue = programMemory?.root[varName]
return varValue?.type === 'ExtrudeGroup' || varValue?.type === 'SketchGroup'
}
export function isSingleCursorInPipe(
selectionRanges: Selections,
ast: Program
) {
if (selectionRanges.codeBasedSelections.length !== 1) return false
if (
doesPipeHaveCallExp({
ast,
selection: selectionRanges.codeBasedSelections[0],
calleeName: 'extrude',
})
)
return false
const selection = selectionRanges.codeBasedSelections[0]
const pathToNode = getNodePathFromSourceRange(ast, selection.range)
const nodeTypes = pathToNode.map(([, type]) => type)
if (nodeTypes.includes('FunctionExpression')) return false
if (nodeTypes.includes('PipeExpression')) return true
return false
}

View File

@ -9,7 +9,11 @@ import { SelectionRange } from '@uiw/react-codemirror'
import { isOverlap } from 'lib/utils'
import { isCursorInSketchCommandRange } from 'lang/util'
import { Program } from 'lang/wasm'
import { doesPipeHaveCallExp, getNodeFromPath } from 'lang/queryAst'
import {
doesPipeHaveCallExp,
getNodeFromPath,
isSingleCursorInPipe,
} from 'lang/queryAst'
import { CommandArgument } from './commandTypes'
import {
STRAIGHT_SEGMENT,
@ -455,6 +459,7 @@ function resetAndSetEngineEntitySelectionCmds(
}
export function isSketchPipe(selectionRanges: Selections) {
if (!isSingleCursorInPipe(selectionRanges, kclManager.ast)) return false
return isCursorInSketchCommandRange(
engineCommandManager.artifactMap,
selectionRanges

View File

@ -71,7 +71,12 @@ export type SetSelections =
}
export type ModelingMachineEvent =
| { type: 'Enter sketch' }
| {
type: 'Enter sketch'
data?: {
forceNewSketch?: boolean
}
}
| {
type: 'Select default plane'
data: { plane: DefaultPlaneStr; normal: [number, number, number] }
@ -153,7 +158,7 @@ export const modelingMachine = createMachine(
'Enter sketch': [
{
target: 'animating to existing sketch',
cond: 'Selection is one face',
cond: 'Selection is on face',
actions: ['set sketch metadata'],
},
'Sketch no face',