Line data Source code
1 : import type { CSSProperties, Ref } from 'vue'
2 : import { ref, watch } from 'vue'
3 :
4 : import { useCanvasPositionConversion } from '@/composables/element/useCanvasPositionConversion'
5 : import { useSettingStore } from '@/platform/settings/settingStore'
6 : import { useCanvasStore } from '@/renderer/core/canvas/canvasStore'
7 : import type { PositionConfig } from '@comfyorg/shared-frontend-utils/geometry'
8 :
9 : interface UseAbsolutePositionReturn {
10 : style: Ref<CSSProperties>
11 : updatePosition: (config: PositionConfig) => void
12 : }
13 :
14 6913 : export function useAbsolutePosition(
15 : options: { useTransform?: boolean } = {}
16 : ): UseAbsolutePositionReturn {
17 6913 : const { useTransform = false } = options
18 :
19 : const canvasStore = useCanvasStore()
20 6913 : const lgCanvas = canvasStore.getCanvas()
21 6913 : const { canvasPosToClientPos, update: updateCanvasPosition } =
22 : useCanvasPositionConversion(lgCanvas.canvas, lgCanvas)
23 :
24 6913 : const settingStore = useSettingStore()
25 6913 : watch(
26 : [
27 : () => settingStore.get('Comfy.Sidebar.Location'),
28 : () => settingStore.get('Comfy.Sidebar.Size'),
29 : () => settingStore.get('Comfy.UseNewMenu')
30 : ],
31 : () => updateCanvasPosition(),
32 : { flush: 'post' }
33 : )
34 :
35 6913 : const style = ref<CSSProperties>({})
36 :
37 : /**
38 : * Compute the style of the element based on the position and size.
39 : *
40 : * @param position
41 : */
42 6913 : const computeStyle = (position: PositionConfig): CSSProperties => {
43 6387 : const { pos, size, scale = lgCanvas.ds.scale } = position
44 6387 : const [left, top] = canvasPosToClientPos(pos)
45 6387 : const [width, height] = size
46 :
47 6387 : return useTransform
48 : ? {
49 : position: 'fixed',
50 : transformOrigin: '0 0',
51 : transform: `scale(${scale})`,
52 : left: `${left}px`,
53 : top: `${top}px`,
54 : width: `${width}px`,
55 : height: `${height}px`
56 : }
57 : : {
58 : position: 'fixed',
59 : left: `${left}px`,
60 : top: `${top}px`,
61 : width: `${width * scale}px`,
62 : height: `${height * scale}px`
63 : }
64 : }
65 :
66 : /**
67 : * Update the position of the element on the litegraph canvas.
68 : *
69 : * @param config
70 : */
71 6913 : const updatePosition = (config: PositionConfig) => {
72 6387 : style.value = computeStyle(config)
73 : }
74 :
75 6913 : return {
76 : style,
77 : updatePosition
78 : }
79 : }
|