A lightweight Clip Studio Paint alternative with real-time multiplayer collaborative drawing. Built with Tauri and React.
TypeScript animationcartoonclip-studio-paintcollaborativedesktop-appdigital-paintingdrawing-appillustrationmangamultiplayeropen-sourcerusttaurithreejswebglwebtoon
import { useCallback } from 'react';
import {
CanvasSelection,
ExpressionColor,
LayerItem,
LayerTreeDelta,
TileDelta,
isDedicatedToneLayer,
isFolderLikeLayer,
} from '../../types/editor';
import { HalftoneDotShape } from '../../utils/screentoneEngine';
import {
nativeImageOpApply,
nativeSelectionFillColor,
syncNativeLayers,
updateNativeLayer,
} from '../../utils/nativeEngineBridge';
import { createLayerTreeSnapshotDelta } from './layerTreeOps';
import { LayerCanvasRegistryApi, LayerManagerContext } from './types';
export function useLayerEffects(
ctx: LayerManagerContext,
canvasRegistry: Pick<LayerCanvasRegistryApi, 'updateLayerThumbnail'>
) {
const {
activeSessionIdRef,
defaultToneFrequencyRef,
layersRef,
activeLayerIdRef,
setLayers,
} = ctx;
const { updateLayerThumbnail } = canvasRegistry;
const toggleTone = useCallback(
(id: string, activeSelection?: CanvasSelection | null) => {
const prev = layersRef.current;
const target = prev.find((l) => l.id === id);
if (!target || isFolderLikeLayer(target)) return;
const docId = activeSessionIdRef.current;
const newEnabled = !target.toneEnabled;
const shouldFillSelection =
newEnabled && Boolean(activeSelection) && target.type === 'raster' && !target.locked;
const isToneLayer = shouldFillSelection ? true : isDedicatedToneLayer(target);
const updatedLayer: LayerItem = {
...target,
isToneLayer: shouldFillSelection ? true : target.isToneLayer,
toneEnabled: newEnabled,
toneFrequency: target.toneFrequency ?? defaultToneFrequencyRef.current ?? 60,
toneAngle: target.toneAngle ?? 45,
toneDotShape: target.toneDotShape ?? 'circle',
toneDensity: target.toneDensity ?? 40,
toneDensityType: isToneLayer ? 'specified' : 'image',
};
const nextLayers = prev.map((l) => (l.id === id ? updatedLayer : l));
layersRef.current = nextLayers;
setLayers(nextLayers);
if (docId) {
void (async () => {
if (shouldFillSelection) {
await nativeSelectionFillColor({ color: '#000000', layerId: id, docId });
}
await syncNativeLayers(docId, nextLayers, activeLayerIdRef.current);
updateLayerThumbnail(id, true);
})();
}
},
[
activeLayerIdRef,
activeSessionIdRef,
defaultToneFrequencyRef,
layersRef,
setLayers,
updateLayerThumbnail,
]
);
const setToneFrequency = useCallback(
(id: string, frequency: number) => {
const clampedFreq = Math.max(20, Math.min(85, Math.round(frequency)));
const prev = layersRef.current;
const target = prev.find((l) => l.id === id);
if (!target || isFolderLikeLayer(target) || target.toneFrequency === clampedFreq) return;
const next = prev.map((l) => (l.id === id ? { ...l, toneFrequency: clampedFreq } : l));
layersRef.current = next;
setLayers(next);
if (activeSessionIdRef.current) {
void syncNativeLayers(activeSessionIdRef.current, next, activeLayerIdRef.current);
}
},
[activeLayerIdRef, activeSessionIdRef, layersRef, setLayers]
);
const setToneAngle = useCallback(
(id: string, angle: number) => {
const clampedAngle = Math.max(0, Math.min(90, Math.round(angle)));
const prev = layersRef.current;
const target = prev.find((l) => l.id === id);
if (!target || isFolderLikeLayer(target) || target.toneAngle === clampedAngle) return;
const next = prev.map((l) => (l.id === id ? { ...l, toneAngle: clampedAngle } : l));
layersRef.current = next;
setLayers(next);
if (activeSessionIdRef.current) {
void syncNativeLayers(activeSessionIdRef.current, next, activeLayerIdRef.current);
}
},
[activeLayerIdRef, activeSessionIdRef, layersRef, setLayers]
);
const setToneDotShape = useCallback(
(id: string, dotShape: HalftoneDotShape) => {
const prev = layersRef.current;
const target = prev.find((l) => l.id === id);
if (!target || isFolderLikeLayer(target) || target.toneDotShape === dotShape) return;
const next = prev.map((l) => (l.id === id ? { ...l, toneDotShape: dotShape } : l));
layersRef.current = next;
setLayers(next);
if (activeSessionIdRef.current) {
void syncNativeLayers(activeSessionIdRef.current, next, activeLayerIdRef.current);
}
},
[activeLayerIdRef, activeSessionIdRef, layersRef, setLayers]
);
const setToneDensity = useCallback(
(id: string, density: number) => {
const clampedDensity = Math.max(1, Math.min(100, Math.round(density)));
const prev = layersRef.current;
const target = prev.find((l) => l.id === id);
if (!target || isFolderLikeLayer(target) || target.toneDensity === clampedDensity) return;
const next = prev.map((l) => (l.id === id ? { ...l, toneDensity: clampedDensity } : l));
layersRef.current = next;
setLayers(next);
if (activeSessionIdRef.current) {
void syncNativeLayers(activeSessionIdRef.current, next, activeLayerIdRef.current);
}
},
[activeLayerIdRef, activeSessionIdRef, layersRef, setLayers]
);
const setToneDensityType = useCallback(
(id: string, densityType: 'specified' | 'image') => {
const prev = layersRef.current;
const target = prev.find((l) => l.id === id);
if (!target || isFolderLikeLayer(target) || target.toneDensityType === densityType) return;
const next = prev.map((l) =>
l.id === id
? { ...l, toneDensityType: densityType, isToneLayer: densityType === 'specified' }
: l
);
layersRef.current = next;
setLayers(next);
if (activeSessionIdRef.current) {
void syncNativeLayers(activeSessionIdRef.current, next, activeLayerIdRef.current);
}
},
[activeLayerIdRef, activeSessionIdRef, layersRef, setLayers]
);
const toggleLayerColor = useCallback(
(id: string) => {
const prev = layersRef.current;
const target = prev.find((l) => l.id === id);
if (!target || isFolderLikeLayer(target)) return;
const updatedLayer: LayerItem = {
...target,
layerColorEnabled: !target.layerColorEnabled,
layerColor: target.layerColor || '#1c73c2',
layerSubColor: target.layerSubColor || '#ffffff',
};
const next = prev.map((l) => (l.id === id ? updatedLayer : l));
layersRef.current = next;
setLayers(next);
if (activeSessionIdRef.current) {
void syncNativeLayers(activeSessionIdRef.current, next, activeLayerIdRef.current);
}
},
[activeLayerIdRef, activeSessionIdRef, layersRef, setLayers]
);
const setLayerColor = useCallback(
(id: string, color: string) => {
const prev = layersRef.current;
const target = prev.find((l) => l.id === id);
if (!target || isFolderLikeLayer(target) || target.layerColor === color) return;
const next = prev.map((l) => (l.id === id ? { ...l, layerColor: color } : l));
layersRef.current = next;
setLayers(next);
if (activeSessionIdRef.current) {
void syncNativeLayers(activeSessionIdRef.current, next, activeLayerIdRef.current);
}
},
[activeLayerIdRef, activeSessionIdRef, layersRef, setLayers]
);
const setLayerSubColor = useCallback(
(id: string, color: string) => {
const prev = layersRef.current;
const target = prev.find((l) => l.id === id);
if (!target || isFolderLikeLayer(target) || target.layerSubColor === color) return;
const next = prev.map((l) => (l.id === id ? { ...l, layerSubColor: color } : l));
layersRef.current = next;
setLayers(next);
if (activeSessionIdRef.current) {
void syncNativeLayers(activeSessionIdRef.current, next, activeLayerIdRef.current);
}
},
[activeLayerIdRef, activeSessionIdRef, layersRef, setLayers]
);
const setLayerExpressionColor = useCallback(
(id: string, mode: ExpressionColor) => {
const prev = layersRef.current;
const target = prev.find((l) => l.id === id);
if (!target || isFolderLikeLayer(target) || target.expressionColor === mode) return;
const updatedLayer: LayerItem = {
...target,
expressionColor: mode,
monoThreshold: target.monoThreshold ?? 128,
monoExpressBlack: target.monoExpressBlack ?? true,
monoExpressWhite: target.monoExpressWhite ?? true,
};
const next = prev.map((l) => (l.id === id ? updatedLayer : l));
layersRef.current = next;
setLayers(next);
if (activeSessionIdRef.current) {
void syncNativeLayers(activeSessionIdRef.current, next, activeLayerIdRef.current);
}
},
[activeLayerIdRef, activeSessionIdRef, layersRef, setLayers]
);
const setLayerMonoThreshold = useCallback(
(id: string, threshold: number) => {
const clamped = Math.max(1, Math.min(255, Math.round(threshold)));
const prev = layersRef.current;
const target = prev.find((l) => l.id === id);
if (!target || isFolderLikeLayer(target) || target.monoThreshold === clamped) return;
const next = prev.map((l) => (l.id === id ? { ...l, monoThreshold: clamped } : l));
layersRef.current = next;
setLayers(next);
if (activeSessionIdRef.current) {
void syncNativeLayers(activeSessionIdRef.current, next, activeLayerIdRef.current);
}
},
[activeLayerIdRef, activeSessionIdRef, layersRef, setLayers]
);
const setLayerMonoExpressMode = useCallback(
(id: string, expressBlack: boolean, expressWhite: boolean) => {
const prev = layersRef.current;
const target = prev.find((l) => l.id === id);
if (!target || isFolderLikeLayer(target)) return;
const next = prev.map((l) =>
l.id === id ? { ...l, monoExpressBlack: expressBlack, monoExpressWhite: expressWhite } : l
);
layersRef.current = next;
setLayers(next);
if (activeSessionIdRef.current) {
void syncNativeLayers(activeSessionIdRef.current, next, activeLayerIdRef.current);
}
},
[activeLayerIdRef, activeSessionIdRef, layersRef, setLayers]
);
const applyExpressionColorOfPreview = useCallback(
(id: string): { tileDeltas: TileDelta[] } | null => {
const target = layersRef.current.find((l) => l.id === id);
if (!target || isFolderLikeLayer(target)) return null;
const exprMode = target.expressionColor || 'color';
if (exprMode === 'color') return null;
const docId = activeSessionIdRef.current;
if (docId) {
const op =
exprMode === 'monochrome'
? ({
category: 'tonal' as const,
tonal: { kind: 'binarization' as const, threshold: target.monoThreshold ?? 128 },
})
: ({
category: 'tonal' as const,
tonal: { kind: 'hue-saturation' as const, hue: 0, saturation: -100, luminosity: 0 },
});
void nativeImageOpApply(op, id, docId).then(() => {
updateLayerThumbnail(id, true);
});
}
return { tileDeltas: [] };
},
[activeSessionIdRef, layersRef, updateLayerThumbnail]
);
const applyLayerColorPermanently = useCallback(
(id: string): { tileDeltas: TileDelta[]; layerTreeDelta: LayerTreeDelta } | null => {
const prev = layersRef.current;
const target = prev.find((l) => l.id === id);
if (!target || isFolderLikeLayer(target) || !target.layerColorEnabled) return null;
const docId = activeSessionIdRef.current;
const next = prev.map((l) => (l.id === id ? { ...l, layerColorEnabled: false } : l));
layersRef.current = next;
setLayers(next);
if (docId) {
const op = {
category: 'tonal' as const,
tonal: {
kind: 'gradient-map' as const,
stops: [
{ position: 0, color: target.layerColor || '#1c73c2' },
{ position: 1, color: target.layerSubColor || '#ffffff' },
],
},
};
void nativeImageOpApply(op, id, docId)
.then(() =>
updateNativeLayer(docId, id, {
layerColorEnabled: false,
recordHistory: false,
})
)
.then(() => syncNativeLayers(docId, next, activeLayerIdRef.current))
.then(() => {
updateLayerThumbnail(id, true);
})
.catch(() => {});
}
const layerTreeDelta = createLayerTreeSnapshotDelta(
prev,
next,
activeLayerIdRef.current,
activeLayerIdRef.current
);
return { tileDeltas: [], layerTreeDelta };
},
[activeLayerIdRef, activeSessionIdRef, layersRef, setLayers, updateLayerThumbnail]
);
const convertLayer = useCallback(
(
id: string,
targetMode: ExpressionColor,
options?: { threshold?: number; expressBlack?: boolean; expressWhite?: boolean }
): { tileDeltas: TileDelta[]; layerTreeDelta: LayerTreeDelta } | null => {
const prev = layersRef.current;
const target = prev.find((l) => l.id === id);
if (!target || isFolderLikeLayer(target)) return null;
const threshold = options?.threshold ?? target.monoThreshold ?? 128;
const expressBlack = options?.expressBlack ?? target.monoExpressBlack ?? true;
const expressWhite = options?.expressWhite ?? target.monoExpressWhite ?? true;
const updatedLayer: LayerItem = {
...target,
expressionColor: targetMode,
monoThreshold: threshold,
monoExpressBlack: expressBlack,
monoExpressWhite: expressWhite,
};
const next = prev.map((l) => (l.id === id ? updatedLayer : l));
layersRef.current = next;
setLayers(next);
const docId = activeSessionIdRef.current;
if (docId) {
void (async () => {
if (targetMode === 'monochrome') {
await nativeImageOpApply(
{ category: 'tonal', tonal: { kind: 'binarization', threshold } },
id,
docId
);
} else if (targetMode === 'gray') {
await nativeImageOpApply(
{
category: 'tonal',
tonal: { kind: 'hue-saturation', hue: 0, saturation: -100, luminosity: 0 },
},
id,
docId
);
}
await syncNativeLayers(docId, next, activeLayerIdRef.current);
updateLayerThumbnail(id, true);
})();
}
const layerTreeDelta = createLayerTreeSnapshotDelta(
prev,
next,
activeLayerIdRef.current,
activeLayerIdRef.current
);
return { tileDeltas: [], layerTreeDelta };
},
[activeLayerIdRef, activeSessionIdRef, layersRef, setLayers, updateLayerThumbnail]
);
return {
toggleTone,
setToneFrequency,
setToneAngle,
setToneDotShape,
setToneDensity,
setToneDensityType,
toggleLayerColor,
setLayerColor,
setLayerSubColor,
setLayerExpressionColor,
setLayerMonoThreshold,
setLayerMonoExpressMode,
applyExpressionColorOfPreview,
applyLayerColorPermanently,
convertLayer,
};
}