Message 1 of 6
Bug: input changed event triggers twice
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello,
After recent Fusion 360 update I noticed a new bug, didn't try all the inputs, but I can reproduce with button row.
When user clicks on a button row item and input changed events triggers, if I change the button row selected item in the event, the event then triggers again and it shows that the selected item did not change - remained the same as the user clicked. After the second event, the button row shows the selected item as the one set from the code. So the problem is that the event triggers twice.
Sample code showing the issue (this is edited command inputs sample code):
#include <Core/CoreAll.h>
#include <Fusion/FusionAll.h>
using namespace adsk::core;
using namespace adsk::fusion;
Ptr<Application> app;
Ptr<UserInterface> ui;
// InputChange event handler.
class OnInputChangedEventHander : public adsk::core::InputChangedEventHandler
{
public:
void notify(const Ptr<InputChangedEventArgs>& eventArgs) override
{
Ptr<CommandInputs> inputs = eventArgs->inputs();
if (!inputs)
return;
Ptr<CommandInput> cmdInput = eventArgs->input();
if (!cmdInput)
return;
if (Ptr<ButtonRowCommandInput> buttonRow = cmdInput)
{
const auto selectedItem = buttonRow->selectedItem();
const auto name = selectedItem->name();
if (name == "Item 2")
{
Application::get()->userInterface()->messageBox("Selected item: " + name);
//Set first item as selected
const auto firstItem = buttonRow->listItems()->item(0);
firstItem->isSelected(true);
}
}
}
};
// CommandCreated event handler.
class CommandCreatedEventHandler : public adsk::core::CommandCreatedEventHandler
{
public:
void notify(const Ptr<CommandCreatedEventArgs>& eventArgs) override
{
if (eventArgs)
{
// Get the command that was created.
Ptr<Command> command = eventArgs->command();
if (command)
{
// Connect to the input changed event.
Ptr<InputChangedEvent> onInputChanged = command->inputChanged();
if (!onInputChanged)
return;
bool isOk = onInputChanged->add(&onInputChangedHandler);
if (!isOk)
return;
// Get the CommandInputs collection associated with the command.
Ptr<CommandInputs> inputs = command->commandInputs();
if (!inputs)
return;
// Create single selectable button row input.
Ptr<ButtonRowCommandInput> buttonRowInput = command->commandInputs()->addButtonRowCommandInput("buttonRow", "Button Row 1", false);
if (!buttonRowInput)
return;
Ptr<ListItems> buttonRowItems = buttonRowInput->listItems();
if (!buttonRowItems)
return;
buttonRowItems->add("Item 1", true, "resources/One");
buttonRowItems->add("Item 2", false, "resources/Two");
}
}
}
private:
OnInputChangedEventHander onInputChangedHandler;
} _cmdCreatedHandler;
extern "C" XI_EXPORT bool run(const char* context)
{
app = Application::get();
if (!app)
return false;
ui = app->userInterface();
if (!ui)
return false;
// Create the command definition.
Ptr<CommandDefinitions> commandDefinitions = ui->commandDefinitions();
if (!commandDefinitions)
return nullptr;
// Get the existing command definition or create it if it doesn't already exist.
Ptr<CommandDefinition> cmdDef = commandDefinitions->itemById("cmdInputsSample");
if (!cmdDef)
{
cmdDef = commandDefinitions->addButtonDefinition("cmdInputsSample",
"Command Inputs Sample",
"Sample to demonstrate various command inputs.");
}
// Connect to the command created event.
Ptr<CommandCreatedEvent> commandCreatedEvent = cmdDef->commandCreated();
if (!commandCreatedEvent)
return false;
commandCreatedEvent->add(&_cmdCreatedHandler);
// Execute the command definition.
cmdDef->execute();
// Prevent this module from being terminated when the script returns, because we are waiting for event handlers to fire.
adsk::autoTerminate(false);
return true;
}