1
1
import { computed , onMounted , reactive , ref , watchEffect } from 'vue'
2
2
import type { CSSProperties , Ref } from 'vue'
3
- import { useEventListener , useScreenSafeArea } from '@vueuse/core'
3
+ import { useEventListener , useScreenSafeArea , useWindowSize } from '@vueuse/core'
4
4
import { clamp , pixelToNumber } from '../utils'
5
5
import { useFrameState } from './state'
6
6
@@ -17,11 +17,11 @@ function snapToPoints(value: number) {
17
17
}
18
18
19
19
export function usePosition ( panelEl : Ref < HTMLElement | undefined > ) {
20
+ const { width : windowWidth , height : windowHeight } = useWindowSize ( )
20
21
const { state, updateState } = useFrameState ( )
21
22
const isHovering = ref ( false )
22
23
const isDragging = ref ( false )
23
24
const draggingOffset = reactive ( { x : 0 , y : 0 } )
24
- const windowSize = reactive ( { width : 0 , height : 0 } )
25
25
const mousePosition = reactive ( { x : 0 , y : 0 } )
26
26
const panelMargins = reactive ( {
27
27
left : 10 ,
@@ -47,11 +47,6 @@ export function usePosition(panelEl: Ref<HTMLElement | undefined>) {
47
47
draggingOffset . y = e . clientY - top - height / 2
48
48
}
49
49
50
- const setWindowSize = ( ) => {
51
- windowSize . width = window . innerWidth
52
- windowSize . height = window . innerHeight
53
- }
54
-
55
50
const bringUp = ( ) => {
56
51
isHovering . value = true
57
52
if ( state . value . minimizePanelInactive < 0 )
@@ -64,16 +59,8 @@ export function usePosition(panelEl: Ref<HTMLElement | undefined>) {
64
59
}
65
60
66
61
onMounted ( ( ) => {
67
- setTimeout ( ( ) => {
68
- setWindowSize ( )
69
- } , 200 )
70
-
71
62
bringUp ( )
72
63
73
- useEventListener ( window , 'resize' , ( ) => {
74
- setWindowSize ( )
75
- } )
76
-
77
64
useEventListener ( window , 'pointerup' , ( ) => {
78
65
isDragging . value = false
79
66
} )
@@ -84,8 +71,8 @@ export function usePosition(panelEl: Ref<HTMLElement | undefined>) {
84
71
if ( ! isDragging . value )
85
72
return
86
73
87
- const centerX = windowSize . width / 2
88
- const centerY = windowSize . height / 2
74
+ const centerX = windowWidth . value / 2
75
+ const centerY = windowHeight . value / 2
89
76
90
77
const x = e . clientX - draggingOffset . x
91
78
const y = e . clientY - draggingOffset . y
@@ -97,9 +84,9 @@ export function usePosition(panelEl: Ref<HTMLElement | undefined>) {
97
84
const deg = Math . atan2 ( y - centerY , x - centerX )
98
85
const HORIZONTAL_MARGIN = 70
99
86
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 )
103
90
104
91
updateState ( {
105
92
position : ( deg >= TL && deg <= TR )
@@ -109,8 +96,8 @@ export function usePosition(panelEl: Ref<HTMLElement | undefined>) {
109
96
: ( deg >= BR && deg <= BL )
110
97
? 'bottom'
111
98
: '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 ) ,
114
101
} )
115
102
} )
116
103
} )
@@ -134,30 +121,30 @@ export function usePosition(panelEl: Ref<HTMLElement | undefined>) {
134
121
const halfWidth = ( panelEl . value ?. clientWidth || 0 ) / 2
135
122
const halfHeight = ( panelEl . value ?. clientHeight || 0 ) / 2
136
123
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
139
126
140
127
switch ( state . value . position ) {
141
128
case 'top' :
142
129
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 ) ,
144
131
top : panelMargins . top + halfHeight ,
145
132
}
146
133
case 'right' :
147
134
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 ) ,
150
137
}
151
138
case 'left' :
152
139
return {
153
140
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 ) ,
155
142
}
156
143
case 'bottom' :
157
144
default :
158
145
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 ,
161
148
}
162
149
}
163
150
} )
@@ -180,8 +167,8 @@ export function usePosition(panelEl: Ref<HTMLElement | undefined>) {
180
167
const marginHorizontal = frameMargin . left + frameMargin . right
181
168
const marginVertical = frameMargin . top + frameMargin . bottom
182
169
183
- const maxWidth = windowSize . width - marginHorizontal
184
- const maxHeight = windowSize . height - marginVertical
170
+ const maxWidth = windowWidth . value - marginHorizontal
171
+ const maxHeight = windowHeight . value - marginVertical
185
172
186
173
const style : CSSProperties = {
187
174
zIndex : - 1 ,
@@ -191,8 +178,8 @@ export function usePosition(panelEl: Ref<HTMLElement | undefined>) {
191
178
}
192
179
193
180
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 )
196
183
197
184
const anchorX = anchor ?. left || 0
198
185
const anchorY = anchor ?. top || 0
@@ -204,17 +191,17 @@ export function usePosition(panelEl: Ref<HTMLElement | undefined>) {
204
191
style . transform = 'translate(-50%, 0)'
205
192
if ( ( anchorX - frameMargin . left ) < width / 2 )
206
193
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`
209
196
break
210
197
case 'right' :
211
198
case 'left' :
212
199
style . top = 0
213
200
style . transform = 'translate(0, -50%)'
214
201
if ( ( anchorY - frameMargin . top ) < height / 2 )
215
202
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`
218
205
break
219
206
}
220
207
0 commit comments