11import { computed , onMounted , reactive , ref , watchEffect } from 'vue'
22import type { CSSProperties , Ref } from 'vue'
3- import { useEventListener , useScreenSafeArea } from '@vueuse/core'
3+ import { useEventListener , useScreenSafeArea , useWindowSize } from '@vueuse/core'
44import { clamp , pixelToNumber } from '../utils'
55import { useFrameState } from './state'
66
@@ -17,11 +17,11 @@ function snapToPoints(value: number) {
1717}
1818
1919export function usePosition ( panelEl : Ref < HTMLElement | undefined > ) {
20+ const { width : windowWidth , height : windowHeight } = useWindowSize ( )
2021 const { state, updateState } = useFrameState ( )
2122 const isHovering = ref ( false )
2223 const isDragging = ref ( false )
2324 const draggingOffset = reactive ( { x : 0 , y : 0 } )
24- const windowSize = reactive ( { width : 0 , height : 0 } )
2525 const mousePosition = reactive ( { x : 0 , y : 0 } )
2626 const panelMargins = reactive ( {
2727 left : 10 ,
@@ -47,11 +47,6 @@ export function usePosition(panelEl: Ref<HTMLElement | undefined>) {
4747 draggingOffset . y = e . clientY - top - height / 2
4848 }
4949
50- const setWindowSize = ( ) => {
51- windowSize . width = window . innerWidth
52- windowSize . height = window . innerHeight
53- }
54-
5550 const bringUp = ( ) => {
5651 isHovering . value = true
5752 if ( state . value . minimizePanelInactive < 0 )
@@ -64,16 +59,6 @@ export function usePosition(panelEl: Ref<HTMLElement | undefined>) {
6459 }
6560
6661 onMounted ( ( ) => {
67- setTimeout ( ( ) => {
68- setWindowSize ( )
69- } , 200 )
70-
71- bringUp ( )
72-
73- useEventListener ( window , 'resize' , ( ) => {
74- setWindowSize ( )
75- } )
76-
7762 useEventListener ( window , 'pointerup' , ( ) => {
7863 isDragging . value = false
7964 } )
@@ -84,8 +69,8 @@ export function usePosition(panelEl: Ref<HTMLElement | undefined>) {
8469 if ( ! isDragging . value )
8570 return
8671
87- const centerX = windowSize . width / 2
88- const centerY = windowSize . height / 2
72+ const centerX = windowWidth . value / 2
73+ const centerY = windowHeight . value / 2
8974
9075 const x = e . clientX - draggingOffset . x
9176 const y = e . clientY - draggingOffset . y
@@ -97,9 +82,9 @@ export function usePosition(panelEl: Ref<HTMLElement | undefined>) {
9782 const deg = Math . atan2 ( y - centerY , x - centerX )
9883 const HORIZONTAL_MARGIN = 70
9984 const TL = Math . atan2 ( 0 - centerY + HORIZONTAL_MARGIN , 0 - centerX )
100- const TR = Math . atan2 ( 0 - centerY + HORIZONTAL_MARGIN , windowSize . width - centerX )
101- const BL = Math . atan2 ( windowSize . height - HORIZONTAL_MARGIN - centerY , 0 - centerX )
102- const BR = Math . atan2 ( windowSize . height - HORIZONTAL_MARGIN - centerY , windowSize . width - centerX )
85+ const TR = Math . atan2 ( 0 - centerY + HORIZONTAL_MARGIN , windowWidth . value - centerX )
86+ const BL = Math . atan2 ( windowHeight . value - HORIZONTAL_MARGIN - centerY , 0 - centerX )
87+ const BR = Math . atan2 ( windowHeight . value - HORIZONTAL_MARGIN - centerY , windowWidth . value - centerX )
10388
10489 updateState ( {
10590 position : ( deg >= TL && deg <= TR )
@@ -109,8 +94,8 @@ export function usePosition(panelEl: Ref<HTMLElement | undefined>) {
10994 : ( deg >= BR && deg <= BL )
11095 ? 'bottom'
11196 : 'left' ,
112- left : snapToPoints ( x / windowSize . width * 100 ) ,
113- top : snapToPoints ( y / windowSize . height * 100 ) ,
97+ left : snapToPoints ( x / windowWidth . value * 100 ) ,
98+ top : snapToPoints ( y / windowHeight . value * 100 ) ,
11499 } )
115100 } )
116101 } )
@@ -134,30 +119,30 @@ export function usePosition(panelEl: Ref<HTMLElement | undefined>) {
134119 const halfWidth = ( panelEl . value ?. clientWidth || 0 ) / 2
135120 const halfHeight = ( panelEl . value ?. clientHeight || 0 ) / 2
136121
137- const left = state . value . left * windowSize . width / 100
138- const top = state . value . top * windowSize . height / 100
122+ const left = state . value . left * windowWidth . value / 100
123+ const top = state . value . top * windowHeight . value / 100
139124
140125 switch ( state . value . position ) {
141126 case 'top' :
142127 return {
143- left : clamp ( left , halfWidth + panelMargins . left , windowSize . width - halfWidth - panelMargins . right ) ,
128+ left : clamp ( left , halfWidth + panelMargins . left , windowWidth . value - halfWidth - panelMargins . right ) ,
144129 top : panelMargins . top + halfHeight ,
145130 }
146131 case 'right' :
147132 return {
148- left : windowSize . width - panelMargins . right - halfHeight ,
149- top : clamp ( top , halfWidth + panelMargins . top , windowSize . height - halfWidth - panelMargins . bottom ) ,
133+ left : windowWidth . value - panelMargins . right - halfHeight ,
134+ top : clamp ( top , halfWidth + panelMargins . top , windowHeight . value - halfWidth - panelMargins . bottom ) ,
150135 }
151136 case 'left' :
152137 return {
153138 left : panelMargins . left + halfHeight ,
154- top : clamp ( top , halfWidth + panelMargins . top , windowSize . height - halfWidth - panelMargins . bottom ) ,
139+ top : clamp ( top , halfWidth + panelMargins . top , windowHeight . value - halfWidth - panelMargins . bottom ) ,
155140 }
156141 case 'bottom' :
157142 default :
158143 return {
159- left : clamp ( left , halfWidth + panelMargins . left , windowSize . width - halfWidth - panelMargins . right ) ,
160- top : windowSize . height - panelMargins . bottom - halfHeight ,
144+ left : clamp ( left , halfWidth + panelMargins . left , windowWidth . value - halfWidth - panelMargins . right ) ,
145+ top : windowHeight . value - panelMargins . bottom - halfHeight ,
161146 }
162147 }
163148 } )
@@ -180,8 +165,8 @@ export function usePosition(panelEl: Ref<HTMLElement | undefined>) {
180165 const marginHorizontal = frameMargin . left + frameMargin . right
181166 const marginVertical = frameMargin . top + frameMargin . bottom
182167
183- const maxWidth = windowSize . width - marginHorizontal
184- const maxHeight = windowSize . height - marginVertical
168+ const maxWidth = windowWidth . value - marginHorizontal
169+ const maxHeight = windowHeight . value - marginVertical
185170
186171 const style : CSSProperties = {
187172 zIndex : - 1 ,
@@ -191,8 +176,8 @@ export function usePosition(panelEl: Ref<HTMLElement | undefined>) {
191176 }
192177
193178 const anchor = anchorPos . value
194- const width = Math . min ( maxWidth , state . value . width * windowSize . width / 100 )
195- const height = Math . min ( maxHeight , state . value . height * windowSize . height / 100 )
179+ const width = Math . min ( maxWidth , state . value . width * windowWidth . value / 100 )
180+ const height = Math . min ( maxHeight , state . value . height * windowHeight . value / 100 )
196181
197182 const anchorX = anchor ?. left || 0
198183 const anchorY = anchor ?. top || 0
@@ -204,17 +189,17 @@ export function usePosition(panelEl: Ref<HTMLElement | undefined>) {
204189 style . transform = 'translate(-50%, 0)'
205190 if ( ( anchorX - frameMargin . left ) < width / 2 )
206191 style . left = `${ width / 2 - anchorX + frameMargin . left } px`
207- else if ( ( windowSize . width - anchorX - frameMargin . right ) < width / 2 )
208- style . left = `${ windowSize . width - anchorX - width / 2 - frameMargin . right } px`
192+ else if ( ( windowWidth . value - anchorX - frameMargin . right ) < width / 2 )
193+ style . left = `${ windowWidth . value - anchorX - width / 2 - frameMargin . right } px`
209194 break
210195 case 'right' :
211196 case 'left' :
212197 style . top = 0
213198 style . transform = 'translate(0, -50%)'
214199 if ( ( anchorY - frameMargin . top ) < height / 2 )
215200 style . top = `${ height / 2 - anchorY + frameMargin . top } px`
216- else if ( ( windowSize . height - anchorY - frameMargin . bottom ) < height / 2 )
217- style . top = `${ windowSize . height - anchorY - height / 2 - frameMargin . bottom } px`
201+ else if ( ( windowHeight . value - anchorY - frameMargin . bottom ) < height / 2 )
202+ style . top = `${ windowHeight . value - anchorY - height / 2 - frameMargin . bottom } px`
218203 break
219204 }
220205
0 commit comments