@@ -15,6 +15,11 @@ import * as React from "react";
1515
1616
1717function Desktop ( ) {
18+ const GRID_WIDTH = 5.35 * 16 ; // in rem cuz yes sybau
19+ const GRID_HEIGHT = 6 * 16 ;
20+ const GRID_GAP = 0.5 * 16 ;
21+
22+
1823 const [ isOpenProgressPanel , setIsOpenProgressPanel ] = useState ( false ) ;
1924 const [ isStartMenuActive , setIsStartMenuActive ] = useState ( false ) ;
2025
@@ -76,7 +81,6 @@ function Desktop() {
7681 } ) ;
7782
7883 const lastPos = useRef ( { x : 100 , y : 100 } ) ;
79- const OFFSET = 20 ;
8084
8185 const nodeRefs = useMemo ( ( ) => {
8286 return Object . keys ( APP_REGISTRY ) . reduce ( ( acc , key ) => {
@@ -94,23 +98,33 @@ function Desktop() {
9498 const [ draggingIcon , setDraggingIcon ] = useState ( null ) ;
9599
96100 const handleIconStop = ( e , data , id ) => {
97- const gridX = 5.35 * 16 ;
98- const gridY = 6 * 16 ;
101+ setDraggingIcon ( null ) ;
102+
103+ const maxCols = Math . floor ( window . innerWidth / ( GRID_WIDTH + GRID_GAP ) ) ;
104+ const maxRows = Math . floor ( ( window . innerHeight - 48 ) / ( GRID_HEIGHT + GRID_GAP ) ) ;
105+
106+ const movedCols = Math . round ( data . x / ( GRID_WIDTH + GRID_GAP ) ) ;
107+ const movedRows = Math . round ( data . y / ( GRID_HEIGHT + GRID_GAP ) ) ;
99108
100- const newCol = Math . max ( 1 , Math . round ( data . x / gridX ) + iconPositions [ id ] . col ) ;
101- const newRow = Math . max ( 1 , Math . round ( data . y / gridY ) + iconPositions [ id ] . row ) ;
109+ const newCol = Math . min (
110+ Math . max ( 1 , iconPositions [ id ] . col + movedCols ) ,
111+ maxCols
112+ ) ;
113+ const newRow = Math . min (
114+ Math . max ( 1 , iconPositions [ id ] . row + movedRows ) ,
115+ maxRows
116+ ) ;
102117
103118 const isOccupied = Object . entries ( iconPositions ) . some ( ( [ appId , pos ] ) => {
104119 return appId !== id && pos . col === newCol && pos . row === newRow ;
105120 } ) ;
106121
107- if ( isOccupied ) {
108- return ;
122+ if ( ! isOccupied ) {
123+ setIconPositions ( prev => ( {
124+ ...prev ,
125+ [ id ] : { col : newCol , row : newRow }
126+ } ) ) ;
109127 }
110- setIconPositions ( prev => ( {
111- ...prev ,
112- [ id ] : { col : newCol , row : newRow }
113- } ) ) ;
114128 } ;
115129
116130 function openApp ( name ) {
@@ -211,7 +225,6 @@ function Desktop() {
211225 key = { id }
212226 nodeRef = { nodeRefs [ id ] }
213227 position = { { x : 0 , y : 0 } }
214- grid = { [ window . innerWidth / 14 , ( window . innerHeight - 48 ) / 6 ] }
215228 onStart = { ( e ) => {
216229 e . stopPropagation ( ) ;
217230 setSelectedIcon ( id ) ;
@@ -236,8 +249,8 @@ function Desktop() {
236249 gridRow : iconPositions [ id ] . row ,
237250
238251 zIndex : draggingIcon === id ? 99999 : 1 ,
239- opacity : draggingIcon === id ? 1 : undefined ,
240- position : 'relative'
252+ opacity : draggingIcon === id ? 0.6 : 1 ,
253+ position : 'relative' ,
241254 } }
242255 >
243256 < img className = "app-icon" src = { APP_REGISTRY [ id ] . imgSrc } draggable = "false" alt = "icon" />
0 commit comments