1
0
mirror of synced 2024-11-12 02:00:52 +01:00

impr: Handle read-only files more gracefully

This commit is contained in:
WerWolv 2024-02-23 17:59:37 +01:00
parent 9bfdfa149e
commit 4d91e7f347
6 changed files with 18 additions and 6 deletions

View File

@ -43,11 +43,17 @@ namespace hex::prv {
} }
void Provider::write(u64 offset, const void *buffer, size_t size) { void Provider::write(u64 offset, const void *buffer, size_t size) {
if (!this->isWritable())
return;
EventProviderDataModified::post(this, offset, size, static_cast<const u8*>(buffer)); EventProviderDataModified::post(this, offset, size, static_cast<const u8*>(buffer));
this->markDirty(); this->markDirty();
} }
void Provider::save() { void Provider::save() {
if (!this->isWritable())
return;
EventProviderSaved::post(this); EventProviderSaved::post(this);
} }
void Provider::saveAs(const std::fs::path &path) { void Provider::saveAs(const std::fs::path &path) {

View File

@ -89,8 +89,12 @@ namespace hex::plugin::builtin {
}); });
EventProviderOpened::subscribe([](hex::prv::Provider *provider) { EventProviderOpened::subscribe([](hex::prv::Provider *provider) {
if (provider != nullptr && ImHexApi::Provider::get() == provider) if (provider != nullptr && ImHexApi::Provider::get() == provider) {
RequestUpdateWindowTitle::post(); RequestUpdateWindowTitle::post();
if (!provider->isWritable())
ui::ToastInfo::open("hex.builtin.popup.error.read_only"_lang);
}
}); });
RequestOpenFile::subscribe(openFile); RequestOpenFile::subscribe(openFile);

View File

@ -264,11 +264,10 @@ namespace hex::plugin::builtin {
} }
// Enter editing mode when double-clicking the row // Enter editing mode when double-clicking the row
if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left) && editingFunction.has_value()) { if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left) && editingFunction.has_value() && m_selectedProvider->isWritable()) {
editing = true; editing = true;
m_editingValue = copyValue; m_editingValue = copyValue;
} }
} else { } else {
// Handle editing mode // Handle editing mode

View File

@ -37,7 +37,7 @@ namespace hex::plugin::builtin {
(*m_patternDrawer)->jumpToPattern(pattern); (*m_patternDrawer)->jumpToPattern(pattern);
}); });
m_patternDrawer.setOnCreateCallback([this](prv::Provider *, auto &drawer) { m_patternDrawer.setOnCreateCallback([this](prv::Provider *provider, auto &drawer) {
drawer = std::make_unique<ui::PatternDrawer>(); drawer = std::make_unique<ui::PatternDrawer>();
drawer->setSelectionCallback([](const pl::ptrn::Pattern *pattern) { drawer->setSelectionCallback([](const pl::ptrn::Pattern *pattern) {
@ -47,6 +47,7 @@ namespace hex::plugin::builtin {
drawer->setTreeStyle(m_treeStyle); drawer->setTreeStyle(m_treeStyle);
drawer->enableRowColoring(m_rowColoring); drawer->enableRowColoring(m_rowColoring);
drawer->enablePatternEditing(provider->isWritable());
}); });
} }

View File

@ -33,6 +33,7 @@ namespace hex::ui {
void setTreeStyle(TreeStyle style) { m_treeStyle = style; } void setTreeStyle(TreeStyle style) { m_treeStyle = style; }
void setSelectionCallback(std::function<void(const pl::ptrn::Pattern *)> callback) { m_selectionCallback = std::move(callback); } void setSelectionCallback(std::function<void(const pl::ptrn::Pattern *)> callback) { m_selectionCallback = std::move(callback); }
void enableRowColoring(bool enabled) { m_rowColoring = enabled; } void enableRowColoring(bool enabled) { m_rowColoring = enabled; }
void enablePatternEditing(bool enabled) { m_editingEnabled = enabled; }
void reset(); void reset();
void jumpToPattern(const pl::ptrn::Pattern *pattern) { m_jumpToPattern = pattern; } void jumpToPattern(const pl::ptrn::Pattern *pattern) { m_jumpToPattern = pattern; }
@ -103,6 +104,7 @@ namespace hex::ui {
TreeStyle m_treeStyle = TreeStyle::Default; TreeStyle m_treeStyle = TreeStyle::Default;
bool m_rowColoring = false; bool m_rowColoring = false;
bool m_editingEnabled = false;
pl::ptrn::Pattern *m_currVisualizedPattern = nullptr; pl::ptrn::Pattern *m_currVisualizedPattern = nullptr;
const pl::ptrn::Pattern *m_jumpToPattern = nullptr; const pl::ptrn::Pattern *m_jumpToPattern = nullptr;

View File

@ -411,7 +411,7 @@ namespace hex::ui {
} }
} }
if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left)) { if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left) && m_editingEnabled) {
m_editingPattern = &pattern; m_editingPattern = &pattern;
m_editingPatternOffset = pattern.getOffset(); m_editingPatternOffset = pattern.getOffset();
AchievementManager::unlockAchievement("hex.builtin.achievement.patterns", "hex.builtin.achievement.patterns.modify_data.name"); AchievementManager::unlockAchievement("hex.builtin.achievement.patterns", "hex.builtin.achievement.patterns.modify_data.name");