EditorWrapper.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. bool 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. if (save_path.has_value())
  67. set_filename(save_path.value());
  68. });
  69. file_picker_action->activate();
  70. if (filename().is_empty())
  71. return false;
  72. }
  73. editor().write_to_file(filename()).release_value_but_fixme_should_propagate_errors();
  74. update_diff();
  75. editor().update();
  76. return true;
  77. }
  78. void EditorWrapper::update_diff()
  79. {
  80. if (m_git_repo) {
  81. m_hunks = Diff::parse_hunks(m_git_repo->unstaged_diff(filename()).value()).release_value_but_fixme_should_propagate_errors();
  82. editor().update_git_diff_indicators().release_value_but_fixme_should_propagate_errors();
  83. }
  84. }
  85. void EditorWrapper::set_project_root(DeprecatedString const& project_root)
  86. {
  87. m_project_root = project_root;
  88. auto result = GitRepo::try_to_create(*m_project_root);
  89. switch (result.type) {
  90. case GitRepo::CreateResult::Type::Success:
  91. m_git_repo = result.repo;
  92. break;
  93. case GitRepo::CreateResult::Type::GitProgramNotFound:
  94. break;
  95. case GitRepo::CreateResult::Type::NoGitRepo:
  96. break;
  97. default:
  98. VERIFY_NOT_REACHED();
  99. }
  100. }
  101. void EditorWrapper::update_title()
  102. {
  103. StringBuilder title;
  104. if (m_filename.is_empty())
  105. title.append(untitled_label);
  106. else
  107. title.append(m_filename);
  108. if (editor().document().is_modified())
  109. title.append(" (*)"sv);
  110. m_filename_title = title.to_deprecated_string();
  111. }
  112. void EditorWrapper::set_debug_mode(bool enabled)
  113. {
  114. m_editor->set_debug_mode(enabled);
  115. }
  116. }