fix(preferences): retain MIDI device labels across dropdown refreshes

This commit is contained in:
Johannes Findeisen 2026-11-06 02:43:56 +01:00
commit 8795516124
4 changed files with 57 additions and 1 deletions

View file

@ -8,6 +8,8 @@
#include <FL/Fl_Box.H>
#include <FL/Fl_Button.H>
#include <functional>
#include <cstring>
#include <utility>
#include <vector>
#include <string>
@ -17,11 +19,17 @@ class PreferencesDialog : public Fl_Double_Window {
std::vector<Fl_Input *> inputs_;
Fl_Box *message_;
std::function<std::vector<std::string>(bool)> devices_;
std::vector<std::string> midi_names_[2];
Fl_Input_Choice *midi_input_=nullptr,*midi_output_=nullptr;
void refresh_devices() {
for(auto pair:{std::pair<Fl_Input_Choice *,bool>{midi_input_,true},{midi_output_,false}}){
if(!pair.first)continue;
std::string value=pair.first->value();std::vector<std::string> names=devices_ ? devices_(pair.second) : std::vector<std::string>{};
std::string value=pair.first->value();
// copy() owns the menu array but borrows its label strings.
// Release old items before replacing their backing storage.
pair.first->menubutton()->clear();
auto &names=midi_names_[pair.second ? 0 : 1];
names=devices_ ? devices_(pair.second) : std::vector<std::string>{};
names.insert(names.begin(),"");std::vector<Fl_Menu_Item> items(names.size()+1);
for(size_t i=0;i<names.size();++i)items[i].text=i ? names[i].c_str() : "(None)";
pair.first->menubutton()->copy(items.data());pair.first->value(value.c_str());
@ -89,6 +97,11 @@ class PreferencesDialog : public Fl_Double_Window {
callback([](Fl_Widget *,void *c){static_cast<PreferencesDialog *>(c)->hide();},this);
end(); xtc_ui::style(this);
}
~PreferencesDialog() override {
// Destroy borrowed-label menus before the member strings are released.
if(midi_input_)midi_input_->menubutton()->clear();
if(midi_output_)midi_output_->menubutton()->clear();
}
void run() { show(); while(shown()) Fl::wait(); }
};
#endif