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,8 @@ export function usePosition(panelEl: Ref<HTMLElement | undefined>) {
6459 }
6560
6661 onMounted ( ( ) => {
67- setTimeout ( ( ) => {
68- setWindowSize ( )
69- } , 200 )
70-
7162 bringUp ( )
7263
73- useEventListener ( window , 'resize' , ( ) => {
74- setWindowSize ( )
75- } )
76-
7764 useEventListener ( window , 'pointerup' , ( ) => {
7865 isDragging . value = false
7966 } )
@@ -84,8 +71,8 @@ export function usePosition(panelEl: Ref<HTMLElement | undefined>) {
8471 if ( ! isDragging . value )
8572 return
8673
87- const centerX = windowSize . width / 2
88- const centerY = windowSize . height / 2
74+ const centerX = windowWidth . value / 2
75+ const centerY = windowHeight . value / 2
8976
9077 const x = e . clientX - draggingOffset . x
9178 const y = e . clientY - draggingOffset . y
@@ -97,9 +84,9 @@ export function usePosition(panelEl: Ref<HTMLElement | undefined>) {
9784 const deg = Math . atan2 ( y - centerY , x - centerX )
9885 const HORIZONTAL_MARGIN = 70
9986 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 )
87+ const TR = Math . atan2 ( 0 - centerY + HORIZONTAL_MARGIN , windowWidth . value - centerX )
88+ const BL = Math . atan2 ( windowHeight . value - HORIZONTAL_MARGIN - centerY , 0 - centerX )
89+ const BR = Math . atan2 ( windowHeight . value - HORIZONTAL_MARGIN - centerY , windowWidth . value - centerX )
10390
10491 updateState ( {
10592 position : ( deg >= TL && deg <= TR )
@@ -109,8 +96,8 @@ export function usePosition(panelEl: Ref<HTMLElement | undefined>) {
10996 : ( deg >= BR && deg <= BL )
11097 ? 'bottom'
11198 : 'left' ,
112- left : snapToPoints ( x / windowSize . width * 100 ) ,
113- top : snapToPoints ( y / windowSize . height * 100 ) ,
99+ left : snapToPoints ( x / windowWidth . value * 100 ) ,
100+ top : snapToPoints ( y / windowHeight . value * 100 ) ,
114101 } )
115102 } )
116103 } )
@@ -134,30 +121,30 @@ export function usePosition(panelEl: Ref<HTMLElement | undefined>) {
134121 const halfWidth = ( panelEl . value ?. clientWidth || 0 ) / 2
135122 const halfHeight = ( panelEl . value ?. clientHeight || 0 ) / 2
136123
137- const left = state . value . left * windowSize . width / 100
138- const top = state . value . top * windowSize . height / 100
124+ const left = state . value . left * windowWidth . value / 100
125+ const top = state . value . top * windowHeight . value / 100
139126
140127 switch ( state . value . position ) {
141128 case 'top' :
142129 return {
143- left : clamp ( left , halfWidth + panelMargins . left , windowSize . width - halfWidth - panelMargins . right ) ,
130+ left : clamp ( left , halfWidth + panelMargins . left , windowWidth . value - halfWidth - panelMargins . right ) ,
144131 top : panelMargins . top + halfHeight ,
145132 }
146133 case 'right' :
147134 return {
148- left : windowSize . width - panelMargins . right - halfHeight ,
149- top : clamp ( top , halfWidth + panelMargins . top , windowSize . height - halfWidth - panelMargins . bottom ) ,
135+ left : windowWidth . value - panelMargins . right - halfHeight ,
136+ top : clamp ( top , halfWidth + panelMargins . top , windowHeight . value - halfWidth - panelMargins . bottom ) ,
150137 }
151138 case 'left' :
152139 return {
153140 left : panelMargins . left + halfHeight ,
154- top : clamp ( top , halfWidth + panelMargins . top , windowSize . height - halfWidth - panelMargins . bottom ) ,
141+ top : clamp ( top , halfWidth + panelMargins . top , windowHeight . value - halfWidth - panelMargins . bottom ) ,
155142 }
156143 case 'bottom' :
157144 default :
158145 return {
159- left : clamp ( left , halfWidth + panelMargins . left , windowSize . width - halfWidth - panelMargins . right ) ,
160- top : windowSize . height - panelMargins . bottom - halfHeight ,
146+ left : clamp ( left , halfWidth + panelMargins . left , windowWidth . value - halfWidth - panelMargins . right ) ,
147+ top : windowHeight . value - panelMargins . bottom - halfHeight ,
161148 }
162149 }
163150 } )
@@ -180,8 +167,8 @@ export function usePosition(panelEl: Ref<HTMLElement | undefined>) {
180167 const marginHorizontal = frameMargin . left + frameMargin . right
181168 const marginVertical = frameMargin . top + frameMargin . bottom
182169
183- const maxWidth = windowSize . width - marginHorizontal
184- const maxHeight = windowSize . height - marginVertical
170+ const maxWidth = windowWidth . value - marginHorizontal
171+ const maxHeight = windowHeight . value - marginVertical
185172
186173 const style : CSSProperties = {
187174 zIndex : - 1 ,
@@ -191,8 +178,8 @@ export function usePosition(panelEl: Ref<HTMLElement | undefined>) {
191178 }
192179
193180 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 )
181+ const width = Math . min ( maxWidth , state . value . width * windowWidth . value / 100 )
182+ const height = Math . min ( maxHeight , state . value . height * windowHeight . value / 100 )
196183
197184 const anchorX = anchor ?. left || 0
198185 const anchorY = anchor ?. top || 0
@@ -204,17 +191,17 @@ export function usePosition(panelEl: Ref<HTMLElement | undefined>) {
204191 style . transform = 'translate(-50%, 0)'
205192 if ( ( anchorX - frameMargin . left ) < width / 2 )
206193 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`
194+ else if ( ( windowWidth . value - anchorX - frameMargin . right ) < width / 2 )
195+ style . left = `${ windowWidth . value - anchorX - width / 2 - frameMargin . right } px`
209196 break
210197 case 'right' :
211198 case 'left' :
212199 style . top = 0
213200 style . transform = 'translate(0, -50%)'
214201 if ( ( anchorY - frameMargin . top ) < height / 2 )
215202 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`
203+ else if ( ( windowHeight . value - anchorY - frameMargin . bottom ) < height / 2 )
204+ style . top = `${ windowHeight . value - anchorY - height / 2 - frameMargin . bottom } px`
218205 break
219206 }
220207
0 commit comments