1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 00:08:11 +00:00

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.
This commit is contained in:
Itamar 2021-03-19 13:06:32 +02:00 committed by Andreas Kling
parent 3cc7b00e24
commit dee0c46c9b
5 changed files with 35 additions and 4 deletions

View file

@ -28,11 +28,13 @@
#include "Editor.h" #include "Editor.h"
#include "HackStudio.h" #include "HackStudio.h"
#include <LibGUI/Action.h> #include <LibGUI/Action.h>
#include <LibGUI/Application.h>
#include <LibGUI/BoxLayout.h> #include <LibGUI/BoxLayout.h>
#include <LibGUI/InputBox.h> #include <LibGUI/InputBox.h>
#include <LibGUI/Label.h> #include <LibGUI/Label.h>
#include <LibGfx/Font.h> #include <LibGfx/Font.h>
#include <LibGfx/FontDatabase.h> #include <LibGfx/FontDatabase.h>
#include <LibGfx/Palette.h>
namespace HackStudio { 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(); } 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?");
}
} }

View file

@ -39,6 +39,7 @@ class Editor;
class EditorWrapper : public GUI::Widget { class EditorWrapper : public GUI::Widget {
C_OBJECT(EditorWrapper) C_OBJECT(EditorWrapper)
public: public:
virtual ~EditorWrapper() override; virtual ~EditorWrapper() override;
@ -51,6 +52,9 @@ public:
void set_editor_has_focus(Badge<Editor>, bool); void set_editor_has_focus(Badge<Editor>, bool);
LanguageClient& language_client(); LanguageClient& language_client();
void set_mode_displayable();
void set_mode_non_displayable();
private: private:
EditorWrapper(); EditorWrapper();

View file

@ -81,6 +81,7 @@
#include <LibGUI/Widget.h> #include <LibGUI/Widget.h>
#include <LibGUI/Window.h> #include <LibGUI/Window.h>
#include <LibGfx/FontDatabase.h> #include <LibGfx/FontDatabase.h>
#include <LibGfx/Palette.h>
#include <LibThread/Lock.h> #include <LibThread/Lock.h>
#include <LibThread/Thread.h> #include <LibThread/Thread.h>
#include <LibVT/TerminalWidget.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_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().horizontal_scrollbar().set_value(new_project_file->horizontal_scroll_value());
current_editor().vertical_scrollbar().set_value(new_project_file->vertical_scroll_value()); current_editor().vertical_scrollbar().set_value(new_project_file->vertical_scroll_value());
current_editor().set_editing_engine(make<GUI::RegularEditingEngine>()); current_editor().set_editing_engine(make<GUI::RegularEditingEngine>());

View file

@ -79,10 +79,11 @@ void ProjectFile::create_document_if_needed() const
if (file_or_error.is_error()) { if (file_or_error.is_error()) {
warnln("Couldn't open '{}': {}", m_name, file_or_error.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. // This is okay though, we'll just go with an empty document and create the file when saving.
} else { return;
auto& file = *file_or_error.value();
m_document->set_text(file.read_all());
} }
auto& file = *file_or_error.value();
m_could_render_text = m_document->set_text(file.read_all());
} }
} }

View file

@ -42,6 +42,7 @@ public:
} }
const String& name() const { return m_name; } const String& name() const { return m_name; }
bool could_render_text() const { return m_could_render_text; }
GUI::TextDocument& document() const; GUI::TextDocument& document() const;
CodeDocument& code_document() const; CodeDocument& code_document() const;
@ -57,6 +58,7 @@ private:
String m_name; String m_name;
mutable RefPtr<CodeDocument> m_document; mutable RefPtr<CodeDocument> m_document;
mutable bool m_could_render_text { false };
int m_vertical_scroll_value { 0 }; int m_vertical_scroll_value { 0 };
int m_horizontal_scroll_value { 0 }; int m_horizontal_scroll_value { 0 };
}; };