55import com .bitwig .extension .controller .ControllerExtension ;
66
77import java .util .Random ;
8- import java .util .concurrent .atomic .AtomicReference ;
9- import java .util .stream .IntStream ;
108
119public class browserRandomizerExtension extends ControllerExtension {
1210 protected browserRandomizerExtension (
@@ -22,73 +20,42 @@ public void init() {
2220 final PopupBrowser popupBrowser = host .createPopupBrowser ();
2321 popupBrowser .exists ().markInterested ();
2422 final CursorTrack cursorTrack = host .createCursorTrack (0 , 0 );
25-
26- AtomicReference <Integer > numberOfChoices = new AtomicReference <>(0 );
27- AtomicReference <Integer > nextChoice = new AtomicReference <>(0 );
2823 Random rand = new Random ();
29- popupBrowser
30- .resultsColumn ()
31- .entryCount ()
32- .addValueObserver (
33- newValue -> {
34- if (newValue > 0 ) {
35- numberOfChoices .set (newValue );
36- }
37- });
24+
25+ popupBrowser .resultsColumn ().entryCount ().markInterested ();
26+ BrowserResultsItemBank resultsItemBank = popupBrowser .resultsColumn ().createItemBank (100000 );
3827
3928 documentState
4029 .getSignalSetting ("Select" , "browser" , "Select random item" )
4130 .addSignalObserver (
42- selectRandomItem (
43- host , popupBrowser , cursorTrack , numberOfChoices , nextChoice , rand , false ));
31+ selectRandomItem (host , popupBrowser , cursorTrack , resultsItemBank , rand , false ));
4432 documentState
4533 .getSignalSetting ("Add" , "browser" , "Add current item" )
4634 .addSignalObserver (popupBrowser ::commit );
4735 documentState
4836 .getSignalSetting ("Random" , "browser" , "Surprise me!" )
4937 .addSignalObserver (
50- selectRandomItem (
51- host , popupBrowser , cursorTrack , numberOfChoices , nextChoice , rand , true ));
38+ selectRandomItem (host , popupBrowser , cursorTrack , resultsItemBank , rand , true ));
5239 }
5340
5441 private NoArgsCallback selectRandomItem (
5542 ControllerHost host ,
5643 PopupBrowser popupBrowser ,
5744 CursorTrack cursorTrack ,
58- AtomicReference <Integer > numberOfChoices ,
59- AtomicReference <Integer > nextChoice ,
45+ BrowserResultsItemBank resultsItemBank ,
6046 Random rand ,
6147 Boolean commit ) {
6248 return () -> {
6349 if (!popupBrowser .exists ().getAsBoolean ()) {
6450 cursorTrack .endOfDeviceChainInsertionPoint ().browse ();
6551 }
66- // go back to first item
67- // unfortunately, selecting the first item does not work
68- // popupBrowser.selectFirstFile();
69- // TODO: replace this ugly trick with a proper way to select the first item
70- IntStream .range (0 , numberOfChoices .get ())
71- .forEach (
72- i -> {
73- host .println ("up " + i );
74- popupBrowser .selectPreviousFile ();
75- });
76-
77- // go to next random choice
78- // once again, I did not find a proper way to select a specific item
79- if (numberOfChoices .get () > 0 ) {
80- nextChoice .set (rand .nextInt (numberOfChoices .get ()));
81- host .println ("nextChoice " + nextChoice .get ());
82- IntStream .range (0 , nextChoice .get ())
83- .forEach (
84- i -> {
85- host .println ("down " + i );
86- popupBrowser .selectNextFile ();
87- });
52+ resultsItemBank
53+ .getItemAt (rand .nextInt (popupBrowser .resultsColumn ().entryCount ().get ()))
54+ .isSelected ()
55+ .set (true );
8856
89- if (commit ) {
90- host .scheduleTask (popupBrowser ::commit , 300 );
91- }
57+ if (commit ) {
58+ host .scheduleTask (popupBrowser ::commit , 300 );
9259 }
9360 };
9461 }
0 commit comments