Skip to content

Commit 9d6dc9e

Browse files
authored
Store video widget size in redux (#828)
* add video scale to slice * use redux for scale * add useEffect * cleanup duplicate code * copilot changes
1 parent 6ba139a commit 9d6dc9e

2 files changed

Lines changed: 27 additions & 19 deletions

File tree

gcs/src/components/dashboard/videoWidget.jsx

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,27 @@ import Webcam from "react-webcam"
2525
import GetOutsideVisibilityColor from "../../helpers/outsideVisibility"
2626
import {
2727
selectVideoMaximized,
28+
selectVideoScale,
2829
selectVideoSource,
2930
setVideoMaximized,
31+
setVideoScale,
3032
setVideoSource,
3133
} from "../../redux/slices/droneConnectionSlice"
3234
import VideoWidgetSourceSelectModal from "./videoWidgetSourceSelectModal"
3335

3436
export default function VideoWidget({ telemetryPanelWidth }) {
3537
const videoSource = useSelector(selectVideoSource)
3638
const isMaximized = useSelector(selectVideoMaximized)
39+
const scale = useSelector(selectVideoScale) // Scale factor for resizing
3740
const dispatch = useDispatch()
3841

3942
const [error, setError] = useState(null)
4043
const [videoDimensions, setVideoDimensions] = useState({
41-
width: 350,
42-
height: 197,
44+
width: 350 * scale,
45+
height: 197 * scale,
4346
}) // Default 16:9 aspect ratio
47+
4448
const [baseAspectRatio, setBaseAspectRatio] = useState(16 / 9) // Track original aspect ratio
45-
const [scale, setScale] = useState(1) // Scale factor for resizing
4649
const [isPoppedOut, setIsPoppedOut] = useState(false) // Track if video is popped out
4750

4851
const [
@@ -61,21 +64,6 @@ export default function VideoWidget({ telemetryPanelWidth }) {
6164
dispatch(setVideoMaximized(true))
6265
}
6366

64-
function updateScale(newScale) {
65-
const clampedScale = Math.max(1, Math.min(3, newScale)) // Clamp between 1x and 3x
66-
setScale(clampedScale)
67-
68-
// Recalculate dimensions based on new scale
69-
const baseWidth = 350
70-
const newWidth = baseWidth * clampedScale
71-
const newHeight = Math.round(newWidth / baseAspectRatio)
72-
73-
setVideoDimensions({
74-
width: newWidth,
75-
height: newHeight,
76-
})
77-
}
78-
7967
function handleResizeStart(e) {
8068
const startX = e.clientX
8169
const startScale = scale
@@ -84,7 +72,8 @@ export default function VideoWidget({ telemetryPanelWidth }) {
8472
const deltaX = e.clientX - startX
8573
const scaleChange = deltaX / 200 // Adjust sensitivity
8674
const newScale = startScale + scaleChange
87-
updateScale(newScale)
75+
const clampedScale = Math.max(1, Math.min(3, newScale)) // Clamp between 1x and 3x
76+
dispatch(setVideoScale(clampedScale))
8877
}
8978

9079
const handleMouseUp = () => {
@@ -250,6 +239,18 @@ export default function VideoWidget({ telemetryPanelWidth }) {
250239
}
251240
}
252241

242+
useEffect(() => {
243+
// Update video dimensions when scale changes
244+
const baseWidth = 350
245+
const newWidth = baseWidth * scale
246+
const newHeight = Math.round(newWidth / baseAspectRatio)
247+
248+
setVideoDimensions({
249+
width: newWidth,
250+
height: newHeight,
251+
})
252+
}, [scale, baseAspectRatio])
253+
253254
useEffect(() => {
254255
// Listen for video window close events
255256
const handleVideoWindowClose = async () => {

gcs/src/redux/slices/droneConnectionSlice.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const initialState = {
4242

4343
videoSource: null,
4444
videoMaximized: false,
45+
videoScale: 1,
4546
}
4647

4748
const droneConnectionSlice = createSlice({
@@ -139,6 +140,9 @@ const droneConnectionSlice = createSlice({
139140
setVideoMaximized: (state, action) => {
140141
state.videoMaximized = action.payload
141142
},
143+
setVideoScale: (state, action) => {
144+
state.videoScale = action.payload
145+
},
142146

143147
// Emits
144148
emitIsConnectedToDrone: () => {},
@@ -182,6 +186,7 @@ const droneConnectionSlice = createSlice({
182186
selectOutsideVisibility: (state) => state.outsideVisibility,
183187
selectVideoSource: (state) => state.videoSource,
184188
selectVideoMaximized: (state) => state.videoMaximized,
189+
selectVideoScale: (state) => state.videoScale,
185190
},
186191
})
187192

@@ -207,6 +212,7 @@ export const {
207212
setOutsideVisibility,
208213
setVideoSource,
209214
setVideoMaximized,
215+
setVideoScale,
210216

211217
// Emitters
212218
emitIsConnectedToDrone,
@@ -247,6 +253,7 @@ export const {
247253
selectOutsideVisibility,
248254
selectVideoSource,
249255
selectVideoMaximized,
256+
selectVideoScale,
250257
} = droneConnectionSlice.selectors
251258

252259
export default droneConnectionSlice

0 commit comments

Comments
 (0)