Serenity Operating System
at master 129 lines 3.6 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * Copyright (c) 2022, the SerenityOS developers. 4 * 5 * SPDX-License-Identifier: BSD-2-Clause 6 */ 7 8#include "EditorWrapper.h" 9#include "Editor.h" 10#include "HackStudio.h" 11#include <LibGUI/Application.h> 12#include <LibGUI/BoxLayout.h> 13#include <LibGUI/FilePicker.h> 14#include <LibGUI/Label.h> 15#include <LibGfx/Font/Font.h> 16#include <LibGfx/Font/FontDatabase.h> 17#include <LibGfx/Palette.h> 18 19namespace HackStudio { 20 21EditorWrapper::EditorWrapper() 22{ 23 set_layout<GUI::VerticalBoxLayout>(); 24 m_filename_title = untitled_label; 25 26 // FIXME: Propagate errors instead of giving up 27 m_editor = MUST(Editor::try_create()); 28 29 add_child(*m_editor); 30 m_editor->set_ruler_visible(true); 31 m_editor->set_automatic_indentation_enabled(true); 32 33 m_editor->on_focus = [this] { 34 set_current_editor_wrapper(this); 35 }; 36 37 m_editor->on_open = [](DeprecatedString const& path) { 38 open_file(path); 39 }; 40 41 m_editor->on_modified_change = [this](bool) { 42 update_title(); 43 update_editor_window_title(); 44 }; 45} 46 47LanguageClient& EditorWrapper::language_client() { return m_editor->language_client(); } 48 49void EditorWrapper::set_mode_displayable() 50{ 51 editor().set_mode(GUI::TextEditor::Editable); 52 editor().set_background_role(Gfx::ColorRole::Base); 53 auto palette = GUI::Application::the()->palette(); 54 editor().set_palette(palette); 55} 56 57void EditorWrapper::set_mode_non_displayable() 58{ 59 editor().set_mode(GUI::TextEditor::ReadOnly); 60 editor().set_background_role(Gfx::ColorRole::InactiveSelection); 61 auto palette = editor().palette(); 62 palette.set_color(Gfx::ColorRole::BaseText, Color::from_rgb(0xffffff)); 63 editor().set_palette(palette); 64 editor().document().set_text("The contents of this file could not be displayed. Is it a binary file?"sv); 65} 66 67void EditorWrapper::set_filename(DeprecatedString const& filename) 68{ 69 m_filename = filename; 70 update_title(); 71 update_diff(); 72} 73 74void EditorWrapper::save() 75{ 76 if (filename().is_empty()) { 77 auto file_picker_action = GUI::CommonActions::make_save_as_action([&](auto&) { 78 Optional<DeprecatedString> save_path = GUI::FilePicker::get_save_filepath(window(), "file"sv, "txt"sv, project_root().value()); 79 set_filename(save_path.value()); 80 }); 81 file_picker_action->activate(); 82 } 83 editor().write_to_file(filename()).release_value_but_fixme_should_propagate_errors(); 84 update_diff(); 85 editor().update(); 86} 87 88void EditorWrapper::update_diff() 89{ 90 if (m_git_repo) 91 m_hunks = Diff::parse_hunks(m_git_repo->unstaged_diff(filename()).value()); 92} 93 94void EditorWrapper::set_project_root(DeprecatedString const& project_root) 95{ 96 m_project_root = project_root; 97 auto result = GitRepo::try_to_create(*m_project_root); 98 switch (result.type) { 99 case GitRepo::CreateResult::Type::Success: 100 m_git_repo = result.repo; 101 break; 102 case GitRepo::CreateResult::Type::GitProgramNotFound: 103 break; 104 case GitRepo::CreateResult::Type::NoGitRepo: 105 break; 106 default: 107 VERIFY_NOT_REACHED(); 108 } 109} 110 111void EditorWrapper::update_title() 112{ 113 StringBuilder title; 114 if (m_filename.is_null()) 115 title.append(untitled_label); 116 else 117 title.append(m_filename); 118 119 if (editor().document().is_modified()) 120 title.append(" (*)"sv); 121 m_filename_title = title.to_deprecated_string(); 122} 123 124void EditorWrapper::set_debug_mode(bool enabled) 125{ 126 m_editor->set_debug_mode(enabled); 127} 128 129}