Skip to content

Commit ae33121

Browse files
committed
fix some layout problems
1 parent 68b835c commit ae33121

File tree

4 files changed

+73
-56
lines changed

4 files changed

+73
-56
lines changed

src/main/client/src/component/Chat.jsx

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,20 +136,24 @@ function isLastChildVisible(container) {
136136
}
137137

138138
function SplitPane({className, messageRef, topElement, bottomElement}) {
139+
let [initialized, setInitialized] = useState(false)
139140
let [dragOffset, setDragOffset] = useState(Number.NaN)
140-
let [splitPos, setSplitPos] = useState(200)
141+
let [splitPos, setSplitPos] = useState(60)
141142
let splitPosRef = useRef()
142143
splitPosRef.current = splitPos
143144
let containerRef = useRef()
144145
let chatState = useChatStore(state => state.chatState)
145146

146147
// force re-render: sidebar layout may have changed
147148
useEffect(() => {
148-
let splitPos = splitPosRef.current
149-
if (containerRef.current) {
150-
setTimeout(() => setSplitPos(getSplitPos(chatState ? splitPos + DELTA : splitPos - DELTA), containerRef.current), 0)
149+
let container = containerRef.current
150+
if (initialized && container) {
151+
setTimeout(() => {
152+
let pos = getSplitPos(splitPosRef.current, container)
153+
setSplitPos(pos)
154+
}, 0)
151155
}
152-
}, [chatState])
156+
}, [chatState, initialized])
153157

154158
useEffect(() => {
155159
let onMouseMove = (e) => {
@@ -166,15 +170,18 @@ function SplitPane({className, messageRef, topElement, bottomElement}) {
166170
window.document.removeEventListener("mouseup", onMouseUp)
167171
}
168172
}, [dragOffset, setDragOffset])
173+
169174
let onMouseDown = useCallback((e) => {
170175
e.preventDefault()
171176
setDragOffset(splitPosRef.current - e.clientY)
172177
}, [setDragOffset])
178+
173179
let topElementHeight = Math.trunc(splitPos)
174180
if (containerRef.current) {
175181
let rect = containerRef.current.getBoundingClientRect()
176182
topElementHeight = Math.trunc(getSplitPos(splitPos, containerRef.current) - rect.top)
177183
}
184+
178185
return (
179186
<div
180187
ref={(ref) => {
@@ -183,7 +190,10 @@ function SplitPane({className, messageRef, topElement, bottomElement}) {
183190
}
184191
if (!containerRef.current) {
185192
let rect = ref.getBoundingClientRect()
186-
setSplitPos(getSplitPos(rect.top + 4.5 * getRemInPixel(), ref))
193+
let pos = getSplitPos(rect.top + 60, ref)
194+
setSplitPos(pos)
195+
splitPosRef.current = pos
196+
setInitialized(true)
187197
}
188198
containerRef.current = ref
189199
}}

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

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,23 @@ import {
6060
createGameState,
6161
} from "./state.js"
6262

63-
export const Game = () => {
63+
export function Game() {
64+
let [gameState, setGameState] = useState(initialState())
65+
let sidebarWidth = useLayoutStore(state => state.sidebarWidth.game)
66+
return (
67+
<div
68+
style={{ width: vw() - sidebarWidth }}
69+
className="h-full">
70+
<MuteIcon />
71+
<Board gameState={gameState} setGameState={setGameState} />
72+
<GamePanel gameState={gameState} />
73+
</div>
74+
)
75+
}
76+
77+
function Board({gameState, setGameState}) {
6478
let [cursor_x, setCursor_x] = useState(-1)
6579
let [cursor_y, setCursor_y] = useState(-1)
66-
let [gameState, setGameState] = useState(initialState())
6780
let zoom = useViewStateStore(state => state.zoom)
6881
let {gameId} = useParams()
6982
let toggleChatState = useChatStore(state => state.toggleChatState)
@@ -80,10 +93,8 @@ export const Game = () => {
8093
let [forbidden_x, forbidden_y] = gameState.forbidden
8194
let canvasRef = useRef()
8295
let countingGroup = !gameHasEnded(gameState) && counting ? getCountingGroup(board, cursor_x, cursor_y) : undefined
83-
let sidebarWidth = useLayoutStore(state => state.sidebarWidth.game)
8496
let dragging = useLayoutStore(state => state.dragging)
8597
let muted = useMuteStore(state => state.muted)
86-
let setMuteState = useMuteStore((state) => state.setMuted)
8798
let howler = useRef()
8899
let playClickSound = useCallback(() => {
89100
if (muted) {
@@ -224,15 +235,7 @@ export const Game = () => {
224235
destination: "/app/game/move",
225236
body: JSON.stringify(move),
226237
})
227-
}, [gameState, context, auth, board, stompClient, counting, forbidden_x, forbidden_y, movesLength, myColor, playClickSound])
228-
229-
let onMuteClick = useCallback(() => {
230-
if (muted) {
231-
setMuteState(false)
232-
} else {
233-
setMuteState(true)
234-
}
235-
}, [setMuteState, muted])
238+
}, [gameState, setGameState, toggleChatState, context, auth, board, stompClient, counting, forbidden_x, forbidden_y, movesLength, myColor, playClickSound])
236239

237240
useEffect(() => {
238241
if (!board.length) {
@@ -286,7 +289,7 @@ export const Game = () => {
286289
setGameState(createGameState(game, auth))
287290
toggleChatState()
288291
}, () => navigate(base + "/lobby"))
289-
}, [queueStatus, auth, id, gameId, navigate])
292+
}, [setGameState, toggleChatState, queueStatus, auth, id, gameId, navigate])
290293

291294
useEffect(() => {
292295
let sub = stompClient.subscribe("/topic/move/" + gameId, (message) => {
@@ -295,43 +298,23 @@ export const Game = () => {
295298
toggleChatState()
296299
})
297300
return sub.unsubscribe
298-
}, [gameState, stompClient, gameId])
301+
}, [gameState, setGameState, toggleChatState, stompClient, gameId])
299302

300303
if (!board.length) {
301304
return <div>Loading...</div>
302305
}
303306

304307
return (
305-
<div
306-
style={{ width: (vw() - sidebarWidth) + "px" }}
307-
className="h-full">
308-
<div className="grid h-full">
309-
<canvas className="place-self-center" ref={canvasRef}
310-
onMouseLeave={() => {
311-
setCursor_x(-1)
312-
setCursor_y(-1)
313-
}}
314-
onMouseMove={onMouseMove}
315-
onClick={onClick}
316-
width={context.width} height={context.width}>
317-
</canvas>
318-
</div>
319-
<div className="absolute left-2 top-2">
320-
<button onClick={onMuteClick}>
321-
<IconContext.Provider value={{
322-
size: "1.5em",
323-
className: "pl-[4px]",
324-
}}>
325-
{muted && (
326-
<FaVolumeMute />
327-
)}
328-
{!muted && (
329-
<FaVolumeUp />
330-
)}
331-
</IconContext.Provider>
332-
</button>
333-
</div>
334-
<GamePanel gameState={gameState} />
308+
<div className="grid h-full">
309+
<canvas className="place-self-center" ref={canvasRef}
310+
onMouseLeave={() => {
311+
setCursor_x(-1)
312+
setCursor_y(-1)
313+
}}
314+
onMouseMove={onMouseMove}
315+
onClick={onClick}
316+
width={context.width} height={context.width}>
317+
</canvas>
335318
</div>
336319
)
337320
}
@@ -357,3 +340,20 @@ function getCountingGroup(board, cursor_x, cursor_y) {
357340
}
358341
return has
359342
}
343+
344+
function MuteIcon() {
345+
let toggleMuted = useMuteStore((state) => state.toggleMuted)
346+
let muted = useMuteStore(state => state.muted)
347+
return (
348+
<div className="absolute left-2 top-2">
349+
<button onClick={toggleMuted}>
350+
<IconContext.Provider value={{
351+
size: "1.5em",
352+
className: "pl-[4px]",
353+
}}>
354+
{muted ? <FaVolumeMute /> : <FaVolumeUp /> }
355+
</IconContext.Provider>
356+
</button>
357+
</div>
358+
)
359+
}

src/main/client/src/feature/lobby/Lobby.jsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
StompContext,
1919
tfetch,
2020
doTry,
21+
vw,
2122
} from "src/util.js"
2223
import {
2324
LobbyPanel,
@@ -31,6 +32,9 @@ import {
3132
import {
3233
useAuthStore,
3334
} from "src/store.js"
35+
import {
36+
useLayoutStore,
37+
} from "src/layout.js"
3438
import {
3539
CgClose,
3640
} from "react-icons/cg"
@@ -76,8 +80,11 @@ export function Lobby() {
7680
})
7781
navigate(base + "/game/" + response.id)
7882
}), [auth, navigate])
83+
let sidebarWidth = useLayoutStore(state => state.sidebarWidth.lobby)
7984
return (
80-
<>
85+
<div
86+
style={{ width: vw() - sidebarWidth }}
87+
className="h-full">
8188
<div className={twJoin(
8289
"mt-2 inline-flex py-2 pr-4 gap-x-1 border-r-2 border-y-2",
8390
isNewGameOpen && "rounded-r-full border-slate-600",
@@ -110,7 +117,7 @@ export function Lobby() {
110117
)}
111118
</div>
112119
<LobbyPanel />
113-
</>
120+
</div>
114121
)
115122
}
116123

src/main/client/src/store.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ export const useAuthStore = create(
3535

3636
export const useMuteStore = create(
3737
persist(
38-
(set) => ({
38+
(set, get) => ({
3939
muted: false,
40-
setMuted: (mute) =>
40+
toggleMuted: () =>
4141
set(() => ({
42-
muted: mute
42+
muted: !get().muted
4343
}))
4444
}),
4545
{ name: "mute-storage" },

0 commit comments

Comments
 (0)