Skip to content

Commit

Permalink
impr: Handle read-only files more gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
WerWolv committed Feb 23, 2024
1 parent 9bfdfa1 commit 4d91e7f
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 6 deletions.
6 changes: 6 additions & 0 deletions lib/libimhex/source/providers/provider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,17 @@ namespace hex::prv {
}

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));
this->markDirty();
}

void Provider::save() {
if (!this->isWritable())
return;

EventProviderSaved::post(this);
}
void Provider::saveAs(const std::fs::path &path) {
Expand Down
6 changes: 5 additions & 1 deletion plugins/builtin/source/content/events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,12 @@ namespace hex::plugin::builtin {
});

EventProviderOpened::subscribe([](hex::prv::Provider *provider) {
if (provider != nullptr && ImHexApi::Provider::get() == provider)
if (provider != nullptr && ImHexApi::Provider::get() == provider) {
RequestUpdateWindowTitle::post();

if (!provider->isWritable())
ui::ToastInfo::open("hex.builtin.popup.error.read_only"_lang);
}
});

RequestOpenFile::subscribe(openFile);
Expand Down
5 changes: 2 additions & 3 deletions plugins/builtin/source/content/views/view_data_inspector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,10 @@ namespace hex::plugin::builtin {
}

// Enter editing mode when double-clicking the row
if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left) && editingFunction.has_value()) {
editing = true;
if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left) && editingFunction.has_value() && m_selectedProvider->isWritable()) {
editing = true;
m_editingValue = copyValue;
}

} else {
// Handle editing mode

Expand Down
3 changes: 2 additions & 1 deletion plugins/builtin/source/content/views/view_pattern_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace hex::plugin::builtin {
(*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->setSelectionCallback([](const pl::ptrn::Pattern *pattern) {
Expand All @@ -47,6 +47,7 @@ namespace hex::plugin::builtin {

drawer->setTreeStyle(m_treeStyle);
drawer->enableRowColoring(m_rowColoring);
drawer->enablePatternEditing(provider->isWritable());
});
}

Expand Down
2 changes: 2 additions & 0 deletions plugins/ui/include/ui/pattern_drawer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ namespace hex::ui {
void setTreeStyle(TreeStyle style) { m_treeStyle = style; }
void setSelectionCallback(std::function<void(const pl::ptrn::Pattern *)> callback) { m_selectionCallback = std::move(callback); }
void enableRowColoring(bool enabled) { m_rowColoring = enabled; }
void enablePatternEditing(bool enabled) { m_editingEnabled = enabled; }
void reset();

void jumpToPattern(const pl::ptrn::Pattern *pattern) { m_jumpToPattern = pattern; }
Expand Down Expand Up @@ -103,6 +104,7 @@ namespace hex::ui {

TreeStyle m_treeStyle = TreeStyle::Default;
bool m_rowColoring = false;
bool m_editingEnabled = false;
pl::ptrn::Pattern *m_currVisualizedPattern = nullptr;
const pl::ptrn::Pattern *m_jumpToPattern = nullptr;

Expand Down
2 changes: 1 addition & 1 deletion plugins/ui/source/ui/pattern_drawer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_editingPatternOffset = pattern.getOffset();
AchievementManager::unlockAchievement("hex.builtin.achievement.patterns", "hex.builtin.achievement.patterns.modify_data.name");
Expand Down

0 comments on commit 4d91e7f

Please sign in to comment.