Forráskód Böngészése

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.
Itamar 4 éve
szülő
commit
dee0c46c9b

+ 19 - 0
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?");
+}
+
 }

+ 4 - 0
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();
 

+ 6 - 1
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>());

+ 4 - 3
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());
 }
 
 }

+ 2 - 0
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 };
 };