Skip to content

Commit

Permalink
runs in vscode;
Browse files Browse the repository at this point in the history
fix typescript and compile errors;
adjust border
  • Loading branch information
korbinian90 committed Dec 9, 2023
1 parent a94590f commit 0a832a2
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 85 deletions.
2 changes: 1 addition & 1 deletion niivue/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<link rel="stylesheet" type="text/css" href="/src/index.css" />
</head>

<body class="bg-gray-900 text-white">
<body class="text-white p-0">
<div id="app" class="w-screen h-screen"></div>
<script>
if (typeof acquireVsCodeApi === 'function') {
Expand Down
3 changes: 2 additions & 1 deletion niivue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"build": "tsc && vite build",
"preview": "vite preview",
"test": "vitest",
"test:ui": "vitest --ui"
"test:ui": "vitest --ui",
"watch": "vite build --watch"
},
"dependencies": {
"@niivue/niivue": "0.38.7",
Expand Down
1 change: 0 additions & 1 deletion niivue/src/components/HomeScreen.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { html } from 'htm/preact'
import { OpenFromWeb } from './OpenFromWeb'

export const HomeScreen = () => html`
<h2 class="text-3xl font-bold text-gray-200 p-2">Bookmarklet</h2>
Expand Down
79 changes: 46 additions & 33 deletions niivue/src/components/Menu.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import { html } from 'htm/preact'
import { AppProps } from './App'
import { computed, effect, useSignal } from '@preact/signals'
import { Signal, computed, effect, useSignal } from '@preact/signals'
import { useRef } from 'preact/hooks'
import { addImagesEvent, addOverlayEvent } from '../events'
import { SLICE_TYPE } from '@niivue/niivue'
import { Scaling } from './Scaling'
import { handleOpacity, handleOverlayColormap } from './OverlayOptions'

export const Menu = (props: AppProps) => {
const { selection, selectionMode, nvArray, nv0, sliceType, crosshair, hideUI, interpolation } =
props
const { selection, selectionMode, nvArray, sliceType, crosshair, hideUI, interpolation } = props
const nvArraySelected = computed(() =>
selectionMode.value > 0 && selection.value.length > 0
? nvArray.value.filter((_, i) => selection.value.includes(i))
: nvArray.value,
)
const multiImage = computed(() => nvArray.value.length > 1)
const nvFirstSelected = computed(() => nvArraySelected.value[0])

effect(() => {
if (selection.value.length == 0 && nvArray.value.length > 0) {
Expand Down Expand Up @@ -61,15 +59,15 @@ export const Menu = (props: AppProps) => {

const isOverlay = computed(() => nvArraySelected.value[0]?.volumes?.length > 1)

const overlayButtonOnClick = () => {
const nv = nvArraySelected.value[0]
if (!nv || nv.volumes.length == 0) {
return
}
// if no overlay, add one
// if overlay, show overlay menu
// overlay menu includes color, min, max, opacity, hide
}
// const overlayButtonOnClick = () => {
// const nv = nvArraySelected.value[0]
// if (!nv || nv.volumes.length == 0) {
// return
// }
// // if no overlay, add one
// // if overlay, show overlay menu
// // overlay menu includes color, min, max, opacity, hide
// }

const addOverlay = () => {
// if image
Expand Down Expand Up @@ -238,7 +236,9 @@ export const Menu = (props: AppProps) => {
<${MenuEntry} label="Set Header" onClick=${() => console.log('Not implemented yet')} />
<${MenuEntry} label="Set Headers to 1" onClick=${setVoxelSize1AndOrigin0} />
</${MenuItem}>
<${ImageSelect} label="Select" state=${selectionMode} visible=${multiImage} />
<${ImageSelect} label="Select" state=${selectionMode} visible=${multiImage}>
<${MenuEntry} label="Select All" onClick=${selectAll} />
</${ImageSelect}>
</div>
${isVolume.value && html`<p class="pl-2">${getMetadataString(nvArraySelected.value[0])}</p>`}
<${ScalingBox}
Expand All @@ -256,12 +256,12 @@ export const Menu = (props: AppProps) => {
`
}

const ScalingBox = (props) => {
const ScalingBox = (props: any) => {
const { nvArraySelected, selectedOverlayNumber, overlayMenu, visible } = props
if (visible && !visible.value) return html``

const setScaling = (val: ScalingOpts) => {
nvArraySelected.value.forEach((nv) => {
nvArraySelected.value.forEach((nv: Niivue) => {
nv.volumes[0].cal_min = val.min
nv.volumes[0].cal_max = val.max
nv.updateGLVolume()
Expand All @@ -273,9 +273,9 @@ const ScalingBox = (props) => {
? ['symmetric', ...nvArraySelected.value[0].colormaps()]
: ['ge_color', 'hsv', 'symmetric', 'warm'],
)
const handleColormap = (e) => {
const handleColormap = (e: any) => {
const colormap = e.target.value
nvArraySelected.value.forEach((nv) => {
nvArraySelected.value.forEach((nv: Niivue) => {
handleOverlayColormap(nv, selectedOverlayNumber.value, colormap)
})
}
Expand All @@ -284,9 +284,9 @@ const ScalingBox = (props) => {
() => nvArraySelected.value[0]?.volumes?.[selectedOverlayNumber.value],
)

const changeOpacity = (e) => {
const changeOpacity = (e: any) => {
const opacity = e.target.value
nvArraySelected.value.forEach((nv) => {
nvArraySelected.value.forEach((nv: Niivue) => {
handleOpacity(nv, selectedOverlayNumber.value, opacity)
})
}
Expand Down Expand Up @@ -333,7 +333,7 @@ export interface ScalingOpts {
max: number
}

export const MenuButton = ({ label, onClick }) => {
export const MenuButton = ({ label, onClick }: { label: string; onClick: Function }) => {
return html`
<div class="relative">
<button class="hover:bg-gray-700 px-2 rounded-md h-6 align-middle" onClick=${onClick}>
Expand All @@ -343,11 +343,30 @@ export const MenuButton = ({ label, onClick }) => {
`
}

// It seems that one child is given directly while multiple children are given as an array
function setChildren(children: any, isOpen: Signal<boolean>) {
if (children) {
if (Array.isArray(children)) {
children.forEach((child, _) => {
if (child?.props) {
child.props.isOpen = isOpen
}
})
} else {
if (children?.props) {
children.props.isOpen = isOpen
}
}
}
}

// maybe selection menu with arrow and option to keep selection?
// select single and multiple options
export const ImageSelect = ({ label, state, children, visible }) => {
export const ImageSelect = ({ label, state, children, visible }: any) => {
if (visible && !visible.value) return html``
const isOpen = useSignal(false)
setChildren(children, isOpen)

return html`
<div class="relative group">
<button
Expand All @@ -368,16 +387,10 @@ export const ImageSelect = ({ label, state, children, visible }) => {
`
}

export const MenuItem = ({ label, onClick, children, visible }) => {
export const MenuItem = ({ label, onClick, children, visible }: any) => {
if (visible && !visible.value) return html``
const isOpen = useSignal(false)
if (children) {
children.forEach((child, _) => {
if (child?.props) {
child.props.isOpen = isOpen
}
})
}
setChildren(children, isOpen)

return html`
<div class="relative group">
Expand All @@ -401,7 +414,7 @@ export const MenuItem = ({ label, onClick, children, visible }) => {
`
}

export const MenuEntry = ({ label, onClick, isOpen, visible }) => {
export const MenuEntry = ({ label, onClick, isOpen, visible }: any) => {
if (visible && !visible.value) return html``
return html`
<button
Expand All @@ -416,7 +429,7 @@ export const MenuEntry = ({ label, onClick, isOpen, visible }) => {
`
}

export const MenuItemSelect = ({ menuEntries }) => {
export const MenuItemSelect = ({ menuEntries }: any) => {
const isOpen = useSignal(false)
const selectedElement = useSignal(0)

Expand All @@ -438,7 +451,7 @@ export const MenuItemSelect = ({ menuEntries }) => {
${isOpen.value &&
html`
${menuEntries.map(
(item, index) => html`
(item: any, index: number) => html`
<button
class="w-full px-2 py-1 text-left bg-gray-900 hover:bg-gray-700"
onClick=${() => {
Expand Down
37 changes: 0 additions & 37 deletions niivue/src/components/OverlayOptions.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,13 @@
import { html } from 'htm/preact'
import { Scaling } from './Scaling'

export const OverlayOptions = ({ nv }: { nv: Niivue }) => {
if (!isVolumeOverlay(nv) && !isMeshOverlay(nv)) {
return html``
}

const overlay = getOverlay(nv)
const colormaps = getColormaps(nv)

return html`
<div class="relative group">
<${Scaling} setScaling=${handleOverlayScaling(nv)} init=${overlay} />
<select onchange=${handleOverlayColormap(nv)} value=${overlay.colormap}>
${colormaps.map((c) => html`<option value=${c}>${c}</option>`)}
</select>
<input
type="number"
value=${overlay.opacity}
onchange=${handleOpacity(nv)}
min="0"
max="1"
step="0.1"
/>
</div>
`
}

export function getColormaps(nv: Niivue) {
if (isVolumeOverlay(nv)) {
return ['symmetric', ...nv.colormaps()]
}
return ['ge_color', 'hsv', 'symmetric', 'warm']
}

function getOverlay(nv: Niivue) {
const layers = isVolumeOverlay(nv) ? nv.volumes : nv.meshes[0].layers
return layers[layers.length - 1]
}

function isVolumeOverlay(nv: Niivue) {
return nv.volumes.length > 0
}
function isMeshOverlay(nv: Niivue) {
return nv.meshes.length > 0 && nv.meshes[0].layers.length > 0
}

export function handleOpacity(nv: Niivue, layerNumber: number, opacity: number) {
if (isVolumeOverlay(nv)) {
Expand Down
4 changes: 2 additions & 2 deletions niivue/src/components/Scaling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const Scaling = ({ setScaling, init }: ScalingProps) => {
<label class="items-baseline h-6 px-2">
<span>Min </span>
<input
class="border-2 border-gray-600 rounded-md bg-gray-700 h-6"
class="border-2 border-gray-600 rounded-md bg-gray-700 h-6 w-20"
type="number"
ref=${minRef}
onchange=${update}
Expand All @@ -40,7 +40,7 @@ export const Scaling = ({ setScaling, init }: ScalingProps) => {
<label class="items-baseline h-6 px-2">
<span>Max </span>
<input
class="border-2 border-gray-600 rounded-md bg-gray-700 h-6"
class="border-2 border-gray-600 rounded-md bg-gray-700 h-6 w-20"
type="number"
ref=${maxRef}
onchange=${update}
Expand Down
10 changes: 0 additions & 10 deletions niivue/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,6 @@
1px 1px 0 #000;
}

.orange-yellow-gradient {
background: linear-gradient(to right,
rgb(255 0 0 / 60%),
rgb(255 166 0 / 60%));
}

input[type='number'] {
width: 5em;
}

a:link {
color: #5599dd;
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@
"start:webview": "cd niivue && yarn run start",
"build:webview": "cd niivue && yarn run build",
"test:webview": "cd niivue && yarn run test",
"watch:webview": "cd niivue && yarn run watch",
"vscode:prepublish": "yarn run compile",
"compile": "tsc -p ./",
"watch": "tsc -watch -p ./",
Expand Down

0 comments on commit 0a832a2

Please sign in to comment.