Skip to content

Commit

Permalink
Refactor to use CSS for slider height
Browse files Browse the repository at this point in the history
  • Loading branch information
selinali2010 committed Feb 15, 2023
1 parent 0bdeacd commit f2c2909
Show file tree
Hide file tree
Showing 13 changed files with 228 additions and 237 deletions.
16 changes: 8 additions & 8 deletions src/__tests__/unit_tests/RangeInput.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('RangeInput', () => {
max={5}
handleChange={handleChange}
handleMouseUp={handleMouseUp}
width={150}
height="150px"
value={testValue}
isDisabled={false}
/>
Expand All @@ -39,7 +39,7 @@ describe('RangeInput', () => {
max={5}
handleChange={handleChange}
handleMouseUp={handleMouseUp}
width={150}
height="150px"
value={testValue}
isDisabled={false}
/>
Expand All @@ -63,7 +63,7 @@ describe('RangeInput', () => {
max={5}
handleChange={handleChange}
handleMouseUp={handleMouseUp}
width={150}
height="150px"
value={testValue}
isDisabled={false}
/>
Expand All @@ -87,7 +87,7 @@ describe('RangeInput', () => {
max={5}
handleChange={handleChange}
handleMouseUp={handleMouseUp}
width={150}
height="150px"
value={testValue}
isDisabled={false}
/>
Expand All @@ -106,7 +106,7 @@ describe('RangeInput', () => {
max={5}
handleChange={handleChange}
handleMouseUp={handleMouseUp}
width={150}
height="150px"
value={testValue}
isDisabled={false}
/>
Expand Down Expand Up @@ -163,7 +163,7 @@ describe('RangeInput', () => {
max={5}
handleChange={handleChange}
handleMouseUp={handleMouseUp}
width={150}
height="150px"
value={testValue}
isDisabled={false}
incrementPrecision={1}
Expand All @@ -184,7 +184,7 @@ describe('RangeInput', () => {
max={5}
handleChange={handleChange}
handleMouseUp={handleMouseUp}
width={150}
height="150px"
value={testValue}
isDisabled={false}
incrementPrecision={0}
Expand All @@ -205,7 +205,7 @@ describe('RangeInput', () => {
max={5}
handleChange={handleChange}
handleMouseUp={handleMouseUp}
width={150}
height="150px"
value={testValue}
isDisabled
/>
Expand Down
2 changes: 1 addition & 1 deletion src/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export const getDefaultState = (): IState => {
return {
isEnabled: true,
isAutoPreAmpOn: true,
isGraphViewOn: true,
isGraphViewOn: true, // true as default so that spinner can be seen on initial load
preAmp: 0,
filters: getDefaultFilters(),
};
Expand Down
7 changes: 2 additions & 5 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,11 @@ const setWindowDimension = (isExpanded: boolean) => {
// TODO: decide if this change is worth it or not
mainWindow.setSize(
currWidth,
Math.min(currHeight, WINDOW_HEIGHT_EXPANDED)
Math.max(currHeight, WINDOW_HEIGHT_EXPANDED)
);
} else {
mainWindow.setMinimumSize(WINDOW_WIDTH, WINDOW_HEIGHT);
mainWindow.setSize(
currWidth,
Math.min(currHeight, WINDOW_HEIGHT_EXPANDED)
);
mainWindow.setSize(currWidth, WINDOW_HEIGHT);
}
}
};
Expand Down
37 changes: 3 additions & 34 deletions src/renderer/App.tsx
Original file line number Diff line number Diff line change
@@ -1,53 +1,22 @@
import { MemoryRouter as Router, Routes, Route } from 'react-router-dom';
import './styles/App.scss';
import { useLayoutEffect, useRef, useState } from 'react';
import MainContent from './MainContent';
import { AquaProvider, useAquaContext } from './utils/AquaContext';
import PrereqMissingModal from './PrereqMissingModal';
import SideBar from './SideBar';
import FrequencyResponseChart from './graph/FrequencyResponseChart';
import PresetsBar from './PresetsBar';
import { useThrottle } from './utils/utils';

const AppContent = () => {
const { isGraphViewOn, isLoading, globalError, performHealthCheck } =
useAquaContext();
const wrapperRef = useRef<HTMLDivElement>(null);

const [height, setHeight] = useState<number>(566);

const updateDimensions = () => {
const newHeight = wrapperRef.current?.clientHeight;
if (newHeight && newHeight > 0) {
setHeight(newHeight);
}
};

const throttledDimensions = useThrottle(() => {
const newHeight = wrapperRef.current?.clientHeight;
if (newHeight && newHeight > 0) {
setHeight(newHeight);
}
}, 100);

useLayoutEffect(() => {
// Compute dimensions on initial render
updateDimensions();
window.addEventListener('resize', throttledDimensions);
return () => window.removeEventListener('resize', throttledDimensions);
}, [throttledDimensions]);

useLayoutEffect(() => {
// Update layout measurements when graph view is toggled
updateDimensions();
}, [isGraphViewOn]);

return (
<>
<SideBar height={height} />
<MainContent ref={wrapperRef} height={height} />
<SideBar />
<MainContent />
<PresetsBar />
<FrequencyResponseChart />
{isGraphViewOn && <FrequencyResponseChart />}
{globalError && (
<PrereqMissingModal
isLoading={isLoading}
Expand Down
174 changes: 74 additions & 100 deletions src/renderer/MainContent.tsx
Original file line number Diff line number Diff line change
@@ -1,116 +1,90 @@
import {
CSSProperties,
ForwardedRef,
forwardRef,
Fragment,
useMemo,
useRef,
} from 'react';
import { Fragment, useMemo } from 'react';
import { MAX_NUM_FILTERS, MIN_NUM_FILTERS } from 'common/constants';
import FrequencyBand from './components/FrequencyBand';
import { useAquaContext } from './utils/AquaContext';
import './styles/MainContent.scss';
import AddSliderDivider from './components/AddSliderDivider';
import Spinner from './icons/Spinner';

const MainContent = forwardRef(
({ height }: { height: number }, ref: ForwardedRef<HTMLDivElement>) => {
const bandsWrapperRef = useRef<HTMLDivElement>(null);
const { filters: freqSortedFilters, isLoading } = useAquaContext();
const MainContent = () => {
const { filters: freqSortedFilters, isLoading } = useAquaContext();

// Store widths for AddSliderDividers and FrequencyBands so we can manually position them
const DIVIDER_WIDTH = 28;
const BAND_WIDTH = 72.94;
// Store widths for AddSliderDividers and FrequencyBands so we can manually position them
const DIVIDER_WIDTH = 28;
const BAND_WIDTH = 72.94;

const [idSortedFilters, sortIndexMap] = useMemo(() => {
// Obtain a fixed order list of the filters
const fixedSort = freqSortedFilters
.slice()
.sort((a, b) => a.id.localeCompare(b.id));
const [idSortedFilters, sortIndexMap] = useMemo(() => {
// Obtain a fixed order list of the filters
const fixedSort = freqSortedFilters
.slice()
.sort((a, b) => a.id.localeCompare(b.id));

// Compute a mapping from a filter id to its sorted index
const map: { [key: string]: number } = {};
freqSortedFilters.forEach((f, index) => {
map[f.id] = index;
});
// Compute a mapping from a filter id to its sorted index
const map: { [key: string]: number } = {};
freqSortedFilters.forEach((f, index) => {
map[f.id] = index;
});

return [fixedSort, map];
}, [freqSortedFilters]);
return [fixedSort, map];
}, [freqSortedFilters]);

const sliderHeight = useMemo(
// TODO: improve comments here
() => height - 5 * 8 - 30 - 36 * 3 - 60 - 2 * 4 - 2 * 23,
[height]
);

return isLoading ? (
<div className="center full row">
<Spinner />
return isLoading ? (
<div className="center full row">
<Spinner />
</div>
) : (
<div className="center main-content">
<div className="col center band-label">
<span />
<span className="row-label">Filter Type</span>
<span className="row-label">Frequency (Hz)</span>
<span />
<span>+30dB</span>
<span />
<span>0dB</span>
<span />
<span>-30dB</span>
<span />
<span className="row-label">Gain (dB)</span>
<span className="row-label">Quality</span>
<span />
</div>
) : (
<div
ref={ref}
className="center main-content"
style={
// Set css variables for determining upper/lower track
{
'--slider-height': sliderHeight,
} as CSSProperties
}
>
<div className="col center band-label">
<span />
<span className="row-label">Filter Type</span>
<span className="row-label">Frequency (Hz)</span>
<span />
<span>+30dB</span>
<span />
<span>0dB</span>
<span />
<span>-30dB</span>
<span />
<span className="row-label">Gain (dB)</span>
<span className="row-label">Quality</span>
<span />
</div>
<div ref={bandsWrapperRef} className="bands row center">
<AddSliderDivider
sliderIndex={-1}
isMaxSliderCount={idSortedFilters.length >= MAX_NUM_FILTERS}
/>
{idSortedFilters.map((filter) => {
const sliderIndex = sortIndexMap[filter.id];
return (
<Fragment key={`slider-${filter.id}`}>
<FrequencyBand
sliderIndex={sliderIndex}
filter={filter}
isMinSliderCount={idSortedFilters.length <= MIN_NUM_FILTERS}
sliderHeight={sliderHeight}
// Manually position the frequency band
style={{
transform: `translateX(${
DIVIDER_WIDTH + sliderIndex * (DIVIDER_WIDTH + BAND_WIDTH)
}px)`,
}}
/>
<AddSliderDivider
sliderIndex={sliderIndex}
isMaxSliderCount={idSortedFilters.length >= MAX_NUM_FILTERS}
// Manually position the divider
style={{
transform: `translateX(${
(sliderIndex + 1) * (DIVIDER_WIDTH + BAND_WIDTH)
}px)`,
}}
/>
</Fragment>
);
})}
</div>
<div className="bands row center">
<AddSliderDivider
sliderIndex={-1}
isMaxSliderCount={idSortedFilters.length >= MAX_NUM_FILTERS}
/>
{idSortedFilters.map((filter) => {
const sliderIndex = sortIndexMap[filter.id];
return (
<Fragment key={`slider-${filter.id}`}>
<FrequencyBand
sliderIndex={sliderIndex}
filter={filter}
isMinSliderCount={idSortedFilters.length <= MIN_NUM_FILTERS}
// Manually position the frequency band
style={{
transform: `translateX(${
DIVIDER_WIDTH + sliderIndex * (DIVIDER_WIDTH + BAND_WIDTH)
}px)`,
}}
/>
<AddSliderDivider
sliderIndex={sliderIndex}
isMaxSliderCount={idSortedFilters.length >= MAX_NUM_FILTERS}
// Manually position the divider
style={{
transform: `translateX(${
(sliderIndex + 1) * (DIVIDER_WIDTH + BAND_WIDTH)
}px)`,
}}
/>
</Fragment>
);
})}
</div>
);
}
);
</div>
);
};

export default MainContent;
Loading

0 comments on commit f2c2909

Please sign in to comment.