Skip to content

Commit 2892b33

Browse files
committed
clear ghost bar position after drag
1 parent b5470ec commit 2892b33

File tree

4 files changed

+37
-18
lines changed

4 files changed

+37
-18
lines changed

src/main/client/src/component/SideBar.jsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export const SideBar = ({children}) => {
1313
let setDragging = useLayoutStore(state => state.setDragging)
1414
let sidebarWidth = useLayoutStore(state => state.sidebarWidth)
1515
let setSidebarWidth = useLayoutStore(state => state.setSidebarWidth)
16+
let sanitizeSidebarWidth = useLayoutStore(state => state.sanitizeSidebarWidth)
1617
let [ghostWidth, setGhostWidth] = useState(sidebarWidth)
1718
let draggingRef = useRef()
1819
let ghostWidthRef = useRef()
@@ -29,7 +30,9 @@ export const SideBar = ({children}) => {
2930
if (!draggingRef.current) {
3031
return
3132
}
32-
setSidebarWidth(ghostWidthRef.current)
33+
let width = sanitizeSidebarWidth(ghostWidthRef.current)
34+
setSidebarWidth(width)
35+
setGhostWidth(width)
3336
setDragging(false)
3437
}
3538
window.document.addEventListener("mousemove", mousemove)
@@ -38,7 +41,7 @@ export const SideBar = ({children}) => {
3841
window.document.removeEventListener("mousemove", mousemove)
3942
window.document.removeEventListener("mouseup", mouseup)
4043
}
41-
}, [vw, draggingRef, setDragging, setSidebarWidth])
44+
}, [vw, draggingRef, setDragging, setSidebarWidth, sanitizeSidebarWidth])
4245
return (
4346
<div
4447
style={{width: sidebarWidth + "px"}}

src/main/client/src/feature/game/Game.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import {
4141
export const Game = () => {
4242
let [cursor_x, setCursor_x] = useState(-1)
4343
let [cursor_y, setCursor_y] = useState(-1)
44-
let [zoom, setZoom] = useState(0)
44+
let zoom = useLayoutStore(state => state.zoom)
4545
let {gameId} = useParams()
4646
let navigate = useNavigate()
4747
let stompClient = useContext(StompContext)
@@ -270,7 +270,7 @@ export const Game = () => {
270270
width={context.width} height={context.width}>
271271
</canvas>
272272
</div>
273-
<GamePanel zoom={zoom} setZoom={setZoom} />
273+
<GamePanel />
274274
</div>
275275
)
276276
}

src/main/client/src/feature/game/GamePanel.jsx

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,29 @@ import {
3030
useAuthStore,
3131
useGameStore,
3232
} from "../../store.js"
33+
import {
34+
useLayoutStore,
35+
} from "../../layout.js"
3336
import {
3437
GameChat,
3538
} from "./GameChat.jsx"
3639
import {
3740
SideBar,
3841
} from "../../component/SideBar.jsx"
3942

40-
export const GamePanel = ({zoom, setZoom}) => {
43+
export const GamePanel = () => {
4144
return (
4245
<SideBar>
4346
<div className="pr-3 pt-4 pl-2 h-full flex flex-col gap-y-1">
44-
<Panel zoom={zoom} setZoom={setZoom} />
47+
<Panel />
4548
</div>
4649
</SideBar>
4750
)
4851
}
4952

50-
function Panel({zoom, setZoom}) {
53+
function Panel() {
54+
let zoom = useLayoutStore(state => state.zoom)
55+
let setZoom = useLayoutStore(state => state.setZoom)
5156
let stompClient = useContext(StompContext)
5257
let auth = useAuthStore(state => state.auth)
5358
let black = useGameStore(state => state.black)
@@ -98,7 +103,7 @@ function Panel({zoom, setZoom}) {
98103
let result = gameHasEnded() ? getScore(board) : undefined
99104
return (
100105
<>
101-
<div className="flex-none grid w-full grid-cols-[min-content_min-content_min-content_auto] gap-x-2">
106+
<div className="flex-none grid w-full grid-cols-[min-content_max-content_min-content_auto] gap-x-2">
102107
<button
103108
onClick={() => setZoom(zoom - 1)}>
104109
<IconContext.Provider value={{
@@ -108,14 +113,8 @@ function Panel({zoom, setZoom}) {
108113
<FaAngleLeft />
109114
</IconContext.Provider>
110115
</button>
111-
<button onClick={() => {
112-
if (!zoom) {
113-
setZoom(-zoom) // force re-render
114-
} else {
115-
setZoom(0)
116-
}
117-
}}>
118-
<span>Zoom:&nbsp;{zoom + 0}</span>
116+
<button onClick={() => setZoom(0)}>
117+
<span>Zoom: {Math.trunc(zoom)}</span>
119118
</button>
120119
<button
121120
onClick={() => setZoom(zoom + 1)}>

src/main/client/src/layout.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,37 @@ import {
1111
export const useLayoutStore = create(
1212
persist(
1313
(set, get) => ({
14+
zoom: 0,
1415
dragging: false,
1516
vw: Math.max(document.documentElement.clientWidth || 0, window.innerWidth || 0),
1617
sidebarWidth: 24 * getPixelRem(),
18+
setZoom: (zoom) => {
19+
set(produce(state => {
20+
if (!zoom) {
21+
state.zoom = 0.000244140625 // force re-render
22+
} else {
23+
state.zoom = zoom
24+
}
25+
}))
26+
},
1727
setDragging: (dragging) => {
1828
set(produce(state => {
1929
state.dragging = dragging
2030
}))
2131
},
22-
setSidebarWidth: (width) => {
32+
sanitizeSidebarWidth: (width) => {
2333
let vh = Math.max(document.documentElement.clientHeight || 0, window.innerHeight || 0)
2434
width = Math.max(200, width)
25-
width = Math.min(Math.abs(get().vw - vh), width)
35+
return Math.min(Math.abs(get().vw - vh), width)
36+
},
37+
setSidebarWidth: (width) => {
2638
set(produce(state => {
2739
state.sidebarWidth = width
40+
if (!get().zoom) {
41+
state.zoom = 0.000244140625 // force re-render
42+
} else {
43+
state.zoom = 0
44+
}
2845
}))
2946
},
3047
}),

0 commit comments

Comments
 (0)