Skip to content

Commit 57d8fb9

Browse files
committed
refactor view state
1 parent e131c4f commit 57d8fb9

File tree

6 files changed

+56
-45
lines changed

6 files changed

+56
-45
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ function SplitPane({messageRef, topElement, bottomElement}) {
175175
)}>
176176
<div
177177
style={{height: splitPos + "px"}}
178-
className="px-1 flex-none overflow-y-scroll">
178+
className="px-1 pt-1 flex-none overflow-y-scroll">
179179
{topElement}
180180
</div>
181181
<div

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,20 @@ import {
44
useState,
55
useCallback,
66
} from "react"
7+
import {
8+
vw,
9+
sanitizeSidebarWidth,
10+
} from "../util.js"
711
import {
812
useLayoutStore,
13+
useViewStateStore,
914
} from "../layout.js"
1015

1116
export const SideBar = ({page, children}) => {
12-
let vw = useLayoutStore(state => state.vw)
13-
let dragging = useLayoutStore(state => state.dragging)
14-
let setDragging = useLayoutStore(state => state.setDragging)
17+
let dragging = useViewStateStore(state => state.dragging)
18+
let setDragging = useViewStateStore(state => state.setDragging)
1519
let sidebarWidth = useLayoutStore(state => state.sidebarWidth[page])
1620
let setSidebarWidth = useLayoutStore(state => state.setSidebarWidth)
17-
let sanitizeSidebarWidth = useLayoutStore(state => state.sanitizeSidebarWidth)
1821
let [ghostWidth, setGhostWidth] = useState(sidebarWidth)
1922
let draggingRef = useRef()
2023
let ghostWidthRef = useRef()
@@ -25,7 +28,7 @@ export const SideBar = ({page, children}) => {
2528
if (!draggingRef.current) {
2629
return
2730
}
28-
let width = sanitizeSidebarWidth(vw - e.clientX)
31+
let width = sanitizeSidebarWidth(vw() - e.clientX)
2932
setGhostWidth(width)
3033
}
3134
let mouseup = () => {
@@ -43,7 +46,7 @@ export const SideBar = ({page, children}) => {
4346
window.document.removeEventListener("mousemove", mousemove)
4447
window.document.removeEventListener("mouseup", mouseup)
4548
}
46-
}, [vw, page, draggingRef, setDragging, setSidebarWidth, sanitizeSidebarWidth])
49+
}, [page, draggingRef, setDragging, setSidebarWidth])
4750
let onMouseDown = useCallback((e) => {
4851
e.preventDefault()
4952
setDragging(true)

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
useNavigate,
1212
} from "react-router-dom"
1313
import {
14+
vw,
1415
base,
1516
StompContext,
1617
BLACK,
@@ -27,6 +28,7 @@ import {
2728
} from "../../store.js"
2829
import {
2930
useLayoutStore,
31+
useViewStateStore,
3032
} from "../../layout.js"
3133
import {
3234
paintShadow,
@@ -52,7 +54,7 @@ import {
5254
export const Game = () => {
5355
let [cursor_x, setCursor_x] = useState(-1)
5456
let [cursor_y, setCursor_y] = useState(-1)
55-
let zoom = useLayoutStore(state => state.zoom)
57+
let zoom = useViewStateStore(state => state.zoom)
5658
let {gameId} = useParams()
5759
let navigate = useNavigate()
5860
let stompClient = useContext(StompContext)
@@ -74,7 +76,6 @@ export const Game = () => {
7476
let canvasRef = useRef()
7577
let countingGroup = !gameHasEnded() && counting ? getCountingGroup(board, cursor_x, cursor_y) : undefined
7678
let sidebarWidth = useLayoutStore(state => state.sidebarWidth.game)
77-
let vw = useLayoutStore(state => state.vw)
7879
let dragging = useLayoutStore(state => state.dragging)
7980
let muted = useMuteStore(state => state.muted)
8081
let setMuteState = useMuteStore((state) => state.setMuted)
@@ -287,7 +288,7 @@ export const Game = () => {
287288

288289
return (
289290
<div
290-
style={{ width: (vw - sidebarWidth) + "px" }}
291+
style={{ width: (vw() - sidebarWidth) + "px" }}
291292
className="h-full">
292293
<div className="grid h-full">
293294
<canvas className="place-self-center" ref={canvasRef}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import {
3232
useGameStore,
3333
} from "../../store.js"
3434
import {
35-
useLayoutStore,
35+
useViewStateStore,
3636
} from "../../layout.js"
3737
import {
3838
Chat,
@@ -53,8 +53,8 @@ export const GamePanel = () => {
5353

5454
function Panel() {
5555
let {gameId} = useParams()
56-
let zoom = useLayoutStore(state => state.zoom)
57-
let setZoom = useLayoutStore(state => state.setZoom)
56+
let zoom = useViewStateStore(state => state.zoom)
57+
let setZoom = useViewStateStore(state => state.setZoom)
5858
let stompClient = useContext(StompContext)
5959
let auth = useAuthStore(state => state.auth)
6060
let black = useGameStore(state => state.black)

src/main/client/src/layout.js

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,52 +8,47 @@ import {
88
persist,
99
} from "zustand/middleware"
1010

11+
const TINY = 0.0001220703125
12+
13+
export const useViewStateStore = create((set, get) => ({
14+
zoom: 0,
15+
dragging: false,
16+
setDragging: (dragging) => {
17+
set(produce(state => {
18+
state.dragging = dragging
19+
}))
20+
},
21+
setZoom: (zoom) => {
22+
set(produce(state => {
23+
if (Math.trunc(zoom)) {
24+
state.zoom = zoom
25+
} else {
26+
state.zoom = get().zoom ? 0 : TINY // force re-render
27+
}
28+
}))
29+
},
30+
}))
31+
1132
export const useLayoutStore = create(
1233
persist(
1334
(set, get) => ({
14-
zoom: 0,
15-
dragging: false,
16-
vw: Math.max(document.documentElement.clientWidth || 0, window.innerWidth || 0),
1735
sidebarWidth: {
18-
"game": 24 * getPixelRem(),
19-
"lobby": 24 * getPixelRem(),
20-
},
21-
setZoom: (zoom) => {
22-
set(produce(state => {
23-
if (!zoom) {
24-
state.zoom = 0.000244140625 // force re-render
25-
} else {
26-
state.zoom = zoom
27-
}
28-
}))
29-
},
30-
setDragging: (dragging) => {
31-
set(produce(state => {
32-
state.dragging = dragging
33-
}))
34-
},
35-
sanitizeSidebarWidth: (width) => {
36-
let vh = Math.max(document.documentElement.clientHeight || 0, window.innerHeight || 0)
37-
width = Math.max(200, width)
38-
return Math.min(Math.abs(get().vw - vh), width)
36+
"game": 24 * getRemInPixel(),
37+
"lobby": 24 * getRemInPixel(),
3938
},
4039
setSidebarWidth: (page, width) => {
4140
set(produce(state => {
4241
let newSidebarWidth = {...get().sidebarWidth}
43-
newSidebarWidth[page] = width
42+
newSidebarWidth[page] = Math.trunc(width)
4443
state.sidebarWidth = newSidebarWidth
45-
if (!get().zoom) {
46-
state.zoom = 0.000244140625 // force re-render
47-
} else {
48-
state.zoom = 0
49-
}
5044
}))
5145
},
5246
}),
5347
{name: "layout-storage"},
5448
),
5549
)
5650

57-
function getPixelRem() {
58-
return parseFloat(window.getComputedStyle(window.document.documentElement).fontSize)
51+
function getRemInPixel() {
52+
let fontSize = window.getComputedStyle(window.document.documentElement).fontSize
53+
return Math.trunc(parseFloat(fontSize))
5954
}

src/main/client/src/util.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,15 @@ export async function doTry(task, onError) {
9898
toast.error(e.message)
9999
}
100100
}
101+
102+
export function vw() {
103+
return Math.max(document.documentElement.clientWidth || 0, window.innerWidth || 0)
104+
}
105+
106+
export function vh() {
107+
return Math.max(document.documentElement.clientHeight || 0, window.innerHeight || 0)
108+
}
109+
110+
export function sanitizeSidebarWidth(width) {
111+
return Math.min(Math.abs(vw() - vh()), Math.max(200, width))
112+
}

0 commit comments

Comments
 (0)