EditorWrapper.cpp 3.6 KB

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