dustinkredmond / FXTrayIcon

Tray Icon implementation for JavaFX applications. Say goodbye to using AWT's SystemTray icon, instead use a JavaFX Tray Icon.
MIT License
324 stars 25 forks source link

So that we can guarantee that each new FX menuItem is unique... #4

Closed EasyG0ing1 closed 3 years ago

EasyG0ing1 commented 3 years ago

added method addMenuItems() which spins a thread that monitors the LinkedList newMenuItems, checking it for new FX menuItems that get deposited by method addMenuItem. When it finds a new menuItem to be added, it verifies that it is unique, then it calls invokeLater to convert FX menuItem over to AWT and waits for that thread to finish before adding the next menuItem in the list.

This guarantees that there are no duplicate menuItems added to popupMenu which was previously possible if a new menuItem was added before invokeLater could finish adding it to popupMenu.

The Thread starts at class instantiation and executes once every 500 milliseconds which makes it virtually unnoticeable in terms of CPU time.

dustinkredmond commented 3 years ago

Thanks, I will review this and get back to you.

EasyG0ing1 commented 3 years ago

Here's the test code that I used to make sure everything was working, if you run it as is on the current version, you will see menuOne in the icon twice, but with the pull version, it throws the error after checking isUnique(). If you comment out the redundant menuOne, then run the code and as soon as you see the icon show up, click on it once, and you can watch it populate the menu in real time, showing you about how long it takes for that thread to add the menu into icon.

FXTrayIcon icon      = new FXTrayIcon(primaryStage, url);
MenuItem   menuOne   = new MenuItem("Menu One");
MenuItem   menuTwo   = new MenuItem("Menu Two");
MenuItem   menuThree = new MenuItem("Menu Three");
MenuItem   menuFour  = new MenuItem("Menu Four");
MenuItem   menuFive  = new MenuItem("Menu Five");
MenuItem   menuSix   = new MenuItem("Menu Six");
MenuItem   menuSeven = new MenuItem("Menu Seven");
MenuItem   menuEight = new MenuItem("Menu Eight");
icon.addMenuItem(menuOne);
icon.addMenuItem(menuOne);
icon.addMenuItem(menuTwo);
icon.addMenuItem(menuThree);
icon.addMenuItem(menuFour);
icon.addMenuItem(menuFive);
icon.addMenuItem(menuSix);
icon.addMenuItem(menuSeven);
icon.addMenuItem(menuEight);
icon.showMinimal();