summaryrefslogtreecommitdiff
path: root/Userland/DevTools
diff options
context:
space:
mode:
authorItamar <itamar8910@gmail.com>2021-03-19 13:06:32 +0200
committerAndreas Kling <kling@serenityos.org>2021-03-21 09:46:21 +0100
commitdee0c46c9b9bb14f7da1c52534f3b8e821efcf66 (patch)
treef84f74de28106cdcdd2ae24536cc06d67e96dac6 /Userland/DevTools
parent3cc7b00e2400d851fa38ca3d107a796ef97d52f8 (diff)
downloadserenity-dee0c46c9b9bb14f7da1c52534f3b8e821efcf66.zip
HackStudio: Display warning when opening binary files
We now detect and display a warning when we can't render the text of an opened file. Closes #5062.
Diffstat (limited to 'Userland/DevTools')
-rw-r--r--Userland/DevTools/HackStudio/EditorWrapper.cpp19
-rw-r--r--Userland/DevTools/HackStudio/EditorWrapper.h4
-rw-r--r--Userland/DevTools/HackStudio/HackStudioWidget.cpp7
-rw-r--r--Userland/DevTools/HackStudio/ProjectFile.cpp7
-rw-r--r--Userland/DevTools/HackStudio/ProjectFile.h2
5 files changed, 35 insertions, 4 deletions
diff --git a/Userland/DevTools/HackStudio/EditorWrapper.cpp b/Userland/DevTools/HackStudio/EditorWrapper.cpp
index b96c4d645f..5d0a1f1fca 100644
--- a/Userland/DevTools/HackStudio/EditorWrapper.cpp
+++ b/Userland/DevTools/HackStudio/EditorWrapper.cpp
@@ -28,11 +28,13 @@
#include "Editor.h"
#include "HackStudio.h"
#include <LibGUI/Action.h>
+#include <LibGUI/Application.h>
#include <LibGUI/BoxLayout.h>
#include <LibGUI/InputBox.h>
#include <LibGUI/Label.h>
#include <LibGfx/Font.h>
#include <LibGfx/FontDatabase.h>
+#include <LibGfx/Palette.h>
namespace HackStudio {
@@ -82,4 +84,21 @@ void EditorWrapper::set_editor_has_focus(Badge<Editor>, bool focus)
LanguageClient& EditorWrapper::language_client() { return m_editor->language_client(); }
+void EditorWrapper::set_mode_displayable()
+{
+ editor().set_mode(GUI::TextEditor::Editable);
+ editor().set_background_role(Gfx::ColorRole::Base);
+ editor().set_palette(GUI::Application::the()->palette());
+}
+
+void EditorWrapper::set_mode_non_displayable()
+{
+ editor().set_mode(GUI::TextEditor::ReadOnly);
+ editor().set_background_role(Gfx::ColorRole::InactiveSelection);
+ auto palette = editor().palette();
+ palette.set_color(Gfx::ColorRole::BaseText, Color::from_rgb(0xffffff));
+ editor().set_palette(palette);
+ editor().document().set_text("The contents of this file could not be displayed. Is it a binary file?");
+}
+
}
diff --git a/Userland/DevTools/HackStudio/EditorWrapper.h b/Userland/DevTools/HackStudio/EditorWrapper.h
index 6dd19012c0..2341b70dc3 100644
--- a/Userland/DevTools/HackStudio/EditorWrapper.h
+++ b/Userland/DevTools/HackStudio/EditorWrapper.h
@@ -39,6 +39,7 @@ class Editor;
class EditorWrapper : public GUI::Widget {
C_OBJECT(EditorWrapper)
+
public:
virtual ~EditorWrapper() override;
@@ -51,6 +52,9 @@ public:
void set_editor_has_focus(Badge<Editor>, bool);
LanguageClient& language_client();
+ void set_mode_displayable();
+ void set_mode_non_displayable();
+
private:
EditorWrapper();
diff --git a/Userland/DevTools/HackStudio/HackStudioWidget.cpp b/Userland/DevTools/HackStudio/HackStudioWidget.cpp
index 2bd45a0638..dfb8611eb7 100644
--- a/Userland/DevTools/HackStudio/HackStudioWidget.cpp
+++ b/Userland/DevTools/HackStudio/HackStudioWidget.cpp
@@ -81,6 +81,7 @@
#include <LibGUI/Widget.h>
#include <LibGUI/Window.h>
#include <LibGfx/FontDatabase.h>
+#include <LibGfx/Palette.h>
#include <LibThread/Lock.h>
#include <LibThread/Thread.h>
#include <LibVT/TerminalWidget.h>
@@ -242,7 +243,11 @@ void HackStudioWidget::open_file(const String& full_filename)
}
current_editor().set_document(const_cast<GUI::TextDocument&>(new_project_file->document()));
- current_editor().set_mode(GUI::TextEditor::Editable);
+ if (new_project_file->could_render_text()) {
+ current_editor_wrapper().set_mode_displayable();
+ } else {
+ current_editor_wrapper().set_mode_non_displayable();
+ }
current_editor().horizontal_scrollbar().set_value(new_project_file->horizontal_scroll_value());
current_editor().vertical_scrollbar().set_value(new_project_file->vertical_scroll_value());
current_editor().set_editing_engine(make<GUI::RegularEditingEngine>());
diff --git a/Userland/DevTools/HackStudio/ProjectFile.cpp b/Userland/DevTools/HackStudio/ProjectFile.cpp
index 07f6ed8d34..7ff63c6633 100644
--- a/Userland/DevTools/HackStudio/ProjectFile.cpp
+++ b/Userland/DevTools/HackStudio/ProjectFile.cpp
@@ -79,10 +79,11 @@ void ProjectFile::create_document_if_needed() const
if (file_or_error.is_error()) {
warnln("Couldn't open '{}': {}", m_name, file_or_error.error());
// This is okay though, we'll just go with an empty document and create the file when saving.
- } else {
- auto& file = *file_or_error.value();
- m_document->set_text(file.read_all());
+ return;
}
+
+ auto& file = *file_or_error.value();
+ m_could_render_text = m_document->set_text(file.read_all());
}
}
diff --git a/Userland/DevTools/HackStudio/ProjectFile.h b/Userland/DevTools/HackStudio/ProjectFile.h
index 29c7ea8777..ebabb94563 100644
--- a/Userland/DevTools/HackStudio/ProjectFile.h
+++ b/Userland/DevTools/HackStudio/ProjectFile.h
@@ -42,6 +42,7 @@ public:
}
const String& name() const { return m_name; }
+ bool could_render_text() const { return m_could_render_text; }
GUI::TextDocument& document() const;
CodeDocument& code_document() const;
@@ -57,6 +58,7 @@ private:
String m_name;
mutable RefPtr<CodeDocument> m_document;
+ mutable bool m_could_render_text { false };
int m_vertical_scroll_value { 0 };
int m_horizontal_scroll_value { 0 };
};