11import React , { MouseEvent } from 'react' ;
22import './App.css' ;
3- import {
4- Checkbox ,
5- FormControlLabel ,
6- SelectChangeEvent ,
7- Stack ,
8- Tab ,
9- Tabs ,
10- Typography ,
11- } from '@mui/material' ;
3+ import { SelectChangeEvent , Tab , Tabs } from '@mui/material' ;
124import {
135 SortValue ,
146 SortName ,
@@ -19,25 +11,24 @@ import {
1911 ColorPreset ,
2012} from './types' ;
2113import { SortingAlgorithms } from './sorting-algorithms' ;
22- import { createArr , shuffleArray , sleep , timeScale , toHz } from './utils' ;
14+ import { createArr , shuffleArray , sleep , toHz } from './utils' ;
2315import { SideDrawer } from './SideDrawer' ;
2416import {
2517 RAINBOW_BACKGROUND_COLOR ,
2618 INIT_STATE ,
2719 INIT_SETTINGS ,
28- SOUND_TYPE_OPTIONS ,
2920} from './constants' ;
3021import { SortAppBar } from './AppBar' ;
3122import { CanvasController } from './canvas-controller' ;
32- import { Colors } from './Colors ' ;
23+ import { ColorTab } from './ColorTab ' ;
3324import { ColumnSlider } from './ColumnSlider' ;
3425import { Options } from './Options' ;
3526import { TimeSlider } from './TimeSlider' ;
3627import { Audiotrack , BarChart , Palette } from '@mui/icons-material' ;
3728import { TabPanel } from './TabPanel' ;
3829import { NonCustomOscillatorType } from 'tone/build/esm/source/oscillator/OscillatorInterface' ;
3930import { TitledSelect } from './TitledSelect' ;
40- import { TitledSlider } from './TitledSlider ' ;
31+ import { SoundTab } from './SoundTab ' ;
4132
4233type Props = {
4334 playSound : ( params : { frequency : number ; duration ?: string } ) => void ;
@@ -96,34 +87,33 @@ class App extends React.Component<Props> {
9687 } ,
9788 } ;
9889
99- this . sortingAlgorithms = new SortingAlgorithms (
100- this . state . settings . columnNbr ,
101- this . compare ,
102- this . valueCompare ,
103- this . drawAndSwap ,
104- this . registerAuxWrite ,
105- ) ;
90+ this . sortingAlgorithms = new SortingAlgorithms ( {
91+ columnNbr : this . state . settings . columnNbr ,
92+ compare : this . compare ,
93+ valueCompare : this . valueCompare ,
94+ drawAndSwap : this . drawAndSwap ,
95+ registerAuxWrite : this . registerAuxWrite ,
96+ } ) ;
10697
98+ this . canvasController = new CanvasController ( {
99+ canvasRef :
100+ React . createRef < HTMLCanvasElement > ( ) as React . RefObject < HTMLCanvasElement > ,
101+ columnNbr : this . state . settings . columnNbr ,
102+ colorPreset : this . state . settings . colorPreset ,
103+ columnColor1 : this . state . settings . columnColor1 ,
104+ columnColor2 : this . state . settings . columnColor2 ,
105+ highlightColor : this . state . settings . highlightColor ,
106+ } ) ;
107+
108+ this . arr = createArr ( this . state . settings . columnNbr ) ;
107109 this . resetPresets = {
108110 [ ResetPreset . Shuffle ] : ( ) => shuffleArray ( this . arr ) ,
109111 [ ResetPreset . Sorted ] : ( ) => this . arr . sort ( ( a , b ) => a . value - b . value ) ,
110112 [ ResetPreset . ReverseSorted ] : ( ) =>
111113 this . arr . sort ( ( a , b ) => b . value - a . value ) ,
112114 } ;
113-
114- this . arr = createArr ( this . state . settings . columnNbr ) ;
115115 this . resetPresets [ this . state . settings . resetPreset ] ( ) ;
116116
117- const ref = React . createRef < HTMLCanvasElement > ( ) ;
118- this . canvasController = new CanvasController (
119- ref as React . RefObject < HTMLCanvasElement > ,
120- this . state . settings . columnNbr ,
121- this . state . settings . colorPreset ,
122- this . state . settings . columnColor1 ,
123- this . state . settings . columnColor2 ,
124- this . state . settings . highlightColor ,
125- ) ;
126-
127117 this . props . setVolume ( this . state . settings . soundVolume ) ;
128118 this . props . setSoundType ( this . state . settings . soundType ) ;
129119 }
@@ -354,10 +344,6 @@ class App extends React.Component<Props> {
354344 this . setSettings ( { chosenSortAlg : event . target . value as SortName } ) ;
355345 } ;
356346
357- chooseResetPreset = ( event : SelectChangeEvent < string > ) => {
358- this . setSettings ( { resetPreset : event . target . value as ResetPreset } ) ;
359- } ;
360-
361347 changeColumnNbr = ( _ : unknown , value : number | number [ ] ) => {
362348 if ( this . state . settings . columnNbr === value ) return ;
363349
@@ -367,18 +353,6 @@ class App extends React.Component<Props> {
367353 this . setSettings ( { columnNbr } , ( ) => this . resetAndDraw ( ) ) ;
368354 } ;
369355
370- changeSwapTime = ( _ : unknown , value : number | number [ ] ) => {
371- this . setSettings ( {
372- swapTime : timeScale ( value instanceof Array ? value [ 0 ] : value ) ,
373- } ) ;
374- } ;
375-
376- changeCompareTime = ( _ : unknown , value : number | number [ ] ) => {
377- this . setSettings ( {
378- compareTime : timeScale ( value instanceof Array ? value [ 0 ] : value ) ,
379- } ) ;
380- } ;
381-
382356 resetAndDraw = ( ) => {
383357 this . stopSorting ( ) ;
384358 this . resetCounters ( ) ;
@@ -451,10 +425,6 @@ class App extends React.Component<Props> {
451425 this . canvasController . redraw ( this . arr ) ;
452426 } ;
453427
454- setBackgroundColor = ( backgroundColor : string ) => {
455- this . setSettings ( { backgroundColor } ) ;
456- } ;
457-
458428 setHighlightColor = ( highlightColor : string ) => {
459429 this . setSettings ( { highlightColor } ) ;
460430 this . canvasController . highlightColor = highlightColor ;
@@ -478,21 +448,18 @@ class App extends React.Component<Props> {
478448 } ) ;
479449 } ;
480450
481- setSoundType = ( event : SelectChangeEvent < string > ) => {
482- const soundType = event . target . value as NonCustomOscillatorType ;
451+ setSoundType = ( soundType : NonCustomOscillatorType ) => {
483452 this . props . setSoundType ( soundType ) ;
484453 this . setSettings ( { soundType } ) ;
485454 } ;
486455
487- setVolume = ( _ : unknown , value : number | number [ ] ) => {
488- const volume = value instanceof Array ? value [ 0 ] : value ;
456+ setVolume = ( volume : number ) => {
489457 this . props . setVolume ( volume ) ;
490458 this . setSettings ( { soundVolume : volume } ) ;
491459 } ;
492460
493- setFrequencyRange = ( _ : unknown , value : number | number [ ] ) => {
494- const frequencyRange = value instanceof Array ? value : [ 0 , value ] ;
495- this . setSettings ( { frequencyRange : frequencyRange as [ number , number ] } ) ;
461+ setFrequencyRange = ( frequencyRange : [ number , number ] ) => {
462+ this . setSettings ( { frequencyRange } ) ;
496463 } ;
497464
498465 render ( ) {
@@ -569,22 +536,26 @@ class App extends React.Component<Props> {
569536 < TimeSlider
570537 title = "Time per swap"
571538 time = { this . state . settings . swapTime }
572- changeTime = { this . changeSwapTime }
539+ changeTime = { ( swapTime ) => this . setSettings ( { swapTime } ) }
573540 />
574541 < TimeSlider
575542 title = "Time per comparison"
576543 time = { this . state . settings . compareTime }
577- changeTime = { this . changeCompareTime }
544+ changeTime = { ( compareTime ) => this . setSettings ( { compareTime } ) }
578545 />
579546 < TitledSelect
580547 title = "Reset Preset"
581548 value = { this . state . settings . resetPreset }
582- onChange = { this . chooseResetPreset }
549+ onChange = { ( event ) => {
550+ this . setSettings ( {
551+ resetPreset : event . target . value as ResetPreset ,
552+ } ) ;
553+ } }
583554 options = { Object . values ( ResetPreset ) }
584555 />
585556 </ TabPanel >
586557 < TabPanel value = { this . state . tabIndex } index = { 1 } >
587- < Colors
558+ < ColorTab
588559 colorPreset = { this . state . settings . colorPreset }
589560 columnColor1 = { this . state . settings . columnColor1 }
590561 columnColor2 = { this . state . settings . columnColor2 }
@@ -593,93 +564,35 @@ class App extends React.Component<Props> {
593564 setColorPreset = { this . setColorPreset }
594565 setColumnColor1 = { this . setColumnColor1 }
595566 setColumnColor2 = { this . setColumnColor2 }
596- setBackgroundColor = { this . setBackgroundColor }
567+ setBackgroundColor = { ( backgroundColor ) =>
568+ this . setSettings ( { backgroundColor } )
569+ }
597570 setHighlightColor = { this . setHighlightColor }
598571 />
599572 </ TabPanel >
600573 < TabPanel value = { this . state . tabIndex } index = { 2 } >
601- < TitledSelect
602- title = "Sound type"
603- value = { this . props . soundType }
604- onChange = { this . setSoundType }
605- options = { SOUND_TYPE_OPTIONS }
606- />
607- < TitledSlider
608- title = "Volume"
609- defaultValue = { this . state . settings . soundVolume }
610- aria-labelledby = "discrete-slider"
611- valueLabelDisplay = "auto"
612- min = { 0 }
613- step = { 1 }
614- max = { 100 }
615- onChangeCommitted = { this . setVolume }
616- valueLabelFormat = { ( value : number ) => `${ value } %` }
617- />
618- < TitledSlider
619- title = "Frequency"
620- defaultValue = { this . state . settings . frequencyRange }
621- aria-labelledby = "discrete-slider"
622- valueLabelDisplay = "auto"
623- min = { 40 }
624- step = { 10 }
625- max = { 2000 }
626- onChangeCommitted = { this . setFrequencyRange }
627- disableSwap
628- valueLabelFormat = { ( value : number ) => `${ value } Hz` }
574+ < SoundTab
575+ soundType = { this . state . settings . soundType }
576+ setSoundType = { this . setSoundType }
577+ soundVolume = { this . state . settings . soundVolume }
578+ setVolume = { this . setVolume }
579+ frequencyRange = { this . state . settings . frequencyRange }
580+ setFrequencyRange = { ( frequencyRange ) =>
581+ this . setSettings ( { frequencyRange } )
582+ }
583+ playSoundOnComparison = {
584+ this . state . settings . playSoundOnComparison
585+ }
586+ setPlaySoundOnComparison = { ( playSoundOnComparison ) =>
587+ this . setSettings ( {
588+ playSoundOnComparison,
589+ } )
590+ }
591+ playSoundOnSwap = { this . state . settings . playSoundOnSwap }
592+ setPlaySoundOnSwap = { ( playSoundOnSwap ) =>
593+ this . setSettings ( { playSoundOnSwap } )
594+ }
629595 />
630- < div >
631- < Typography
632- align = "left"
633- variant = "subtitle1"
634- color = "textSecondary"
635- >
636- Play sound on
637- </ Typography >
638- < Stack >
639- < FormControlLabel
640- control = {
641- < Checkbox
642- checked = { this . state . settings . playSoundOnComparison }
643- onChange = { ( e ) =>
644- this . setSettings ( {
645- playSoundOnComparison : ! ! e . target . checked ,
646- } )
647- }
648- />
649- }
650- label = {
651- < Typography
652- align = "left"
653- variant = "subtitle1"
654- color = "textSecondary"
655- >
656- Comparison
657- </ Typography >
658- }
659- />
660- < FormControlLabel
661- control = {
662- < Checkbox
663- checked = { this . state . settings . playSoundOnSwap }
664- onChange = { ( e ) =>
665- this . setSettings ( {
666- playSoundOnSwap : ! ! e . target . checked ,
667- } )
668- }
669- />
670- }
671- label = {
672- < Typography
673- align = "left"
674- variant = "subtitle1"
675- color = "textSecondary"
676- >
677- Swap
678- </ Typography >
679- }
680- />
681- </ Stack >
682- </ div >
683596 </ TabPanel >
684597 </ SideDrawer >
685598 </ div >
0 commit comments