sys: Cache provider validity when drawing toolbar items

Possibly addresses issue mentioned in #465
This commit is contained in:
WerWolv 2022-03-13 17:46:59 +01:00
parent caad8c25ad
commit af882b172e
1 changed files with 8 additions and 7 deletions

View File

@ -64,21 +64,22 @@ namespace hex::plugin::builtin {
void addToolbarItems() { void addToolbarItems() {
ContentRegistry::Interface::addToolbarItem([] { ContentRegistry::Interface::addToolbarItem([] {
auto provider = ImHexApi::Provider::get(); auto provider = ImHexApi::Provider::get();
bool providerValid = provider != nullptr;
// Undo // Undo
ImGui::Disabled([&provider] { ImGui::Disabled([&provider] {
if (ImGui::ToolBarButton(ICON_VS_DISCARD, ImGui::GetCustomColorVec4(ImGuiCustomCol_ToolbarBlue))) if (ImGui::ToolBarButton(ICON_VS_DISCARD, ImGui::GetCustomColorVec4(ImGuiCustomCol_ToolbarBlue)))
provider->undo(); provider->undo();
}, },
!ImHexApi::Provider::isValid() || !provider->canUndo()); !providerValid || !provider->canUndo());
// Redo // Redo
ImGui::Disabled([&provider] { ImGui::Disabled([&provider] {
if (ImGui::ToolBarButton(ICON_VS_REDO, ImGui::GetCustomColorVec4(ImGuiCustomCol_ToolbarBlue))) if (ImGui::ToolBarButton(ICON_VS_REDO, ImGui::GetCustomColorVec4(ImGuiCustomCol_ToolbarBlue)))
provider->redo(); provider->redo();
}, },
!ImHexApi::Provider::isValid() || !provider->canRedo()); !providerValid || !provider->canRedo());
ImGui::SeparatorEx(ImGuiSeparatorFlags_Vertical); ImGui::SeparatorEx(ImGuiSeparatorFlags_Vertical);
@ -99,7 +100,7 @@ namespace hex::plugin::builtin {
if (ImGui::ToolBarButton(ICON_VS_SAVE, ImGui::GetCustomColorVec4(ImGuiCustomCol_ToolbarBlue))) if (ImGui::ToolBarButton(ICON_VS_SAVE, ImGui::GetCustomColorVec4(ImGuiCustomCol_ToolbarBlue)))
provider->save(); provider->save();
}, },
!ImHexApi::Provider::isValid() || !provider->isWritable() || !provider->isSavable()); !providerValid || !provider->isWritable() || !provider->isSavable());
// Save file as // Save file as
ImGui::Disabled([&provider] { ImGui::Disabled([&provider] {
@ -108,7 +109,7 @@ namespace hex::plugin::builtin {
provider->saveAs(path); provider->saveAs(path);
}); });
}, },
!ImHexApi::Provider::isValid() || !provider->isSavable()); !providerValid || !provider->isSavable());
ImGui::SeparatorEx(ImGuiSeparatorFlags_Vertical); ImGui::SeparatorEx(ImGuiSeparatorFlags_Vertical);
@ -123,7 +124,7 @@ namespace hex::plugin::builtin {
ImHexApi::Bookmarks::add(region.address, region.size, {}, {}); ImHexApi::Bookmarks::add(region.address, region.size, {}, {});
} }
}, },
!ImHexApi::Provider::isValid() || !provider->isReadable() || ImHexApi::HexEditor::getSelection().size == 0); !providerValid || !provider->isReadable() || ImHexApi::HexEditor::getSelection().size == 0);
ImGui::SeparatorEx(ImGuiSeparatorFlags_Vertical); ImGui::SeparatorEx(ImGuiSeparatorFlags_Vertical);
@ -149,7 +150,7 @@ namespace hex::plugin::builtin {
ImGui::EndCombo(); ImGui::EndCombo();
} }
}, },
!ImHexApi::Provider::isValid()); !providerValid);
}); });
} }