EditorWrapper.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "EditorWrapper.h"
  7. #include "Editor.h"
  8. #include "HackStudio.h"
  9. #include <LibGUI/Application.h>
  10. #include <LibGUI/BoxLayout.h>
  11. #include <LibGUI/Label.h>
  12. #include <LibGfx/Font.h>
  13. #include <LibGfx/FontDatabase.h>
  14. #include <LibGfx/Palette.h>
  15. namespace HackStudio {
  16. EditorWrapper::EditorWrapper()
  17. {
  18. set_layout<GUI::VerticalBoxLayout>();
  19. auto& label_wrapper = add<GUI::Widget>();
  20. label_wrapper.set_fixed_height(14);
  21. label_wrapper.set_fill_with_background_color(true);
  22. label_wrapper.set_layout<GUI::HorizontalBoxLayout>();
  23. label_wrapper.layout()->set_margins({ 0, 2 });
  24. m_filename_label = label_wrapper.add<GUI::Label>(untitled_label);
  25. m_filename_label->set_text_alignment(Gfx::TextAlignment::CenterLeft);
  26. m_filename_label->set_fixed_height(14);
  27. m_editor = add<Editor>();
  28. m_editor->set_ruler_visible(true);
  29. m_editor->set_automatic_indentation_enabled(true);
  30. m_editor->on_focus = [this] {
  31. set_current_editor_wrapper(this);
  32. };
  33. m_editor->on_open = [](String path) {
  34. open_file(path);
  35. };
  36. m_editor->on_modified_change = [this](bool) {
  37. update_title();
  38. };
  39. }
  40. EditorWrapper::~EditorWrapper()
  41. {
  42. }
  43. void EditorWrapper::set_editor_has_focus(Badge<Editor>, bool focus)
  44. {
  45. auto& font = Gfx::FontDatabase::default_font();
  46. m_filename_label->set_font(focus ? font.bold_variant() : font);
  47. }
  48. LanguageClient& EditorWrapper::language_client() { return m_editor->language_client(); }
  49. void EditorWrapper::set_mode_displayable()
  50. {
  51. editor().set_mode(GUI::TextEditor::Editable);
  52. editor().set_background_role(Gfx::ColorRole::Base);
  53. editor().set_palette(GUI::Application::the()->palette());
  54. }
  55. void EditorWrapper::set_mode_non_displayable()
  56. {
  57. editor().set_mode(GUI::TextEditor::ReadOnly);
  58. editor().set_background_role(Gfx::ColorRole::InactiveSelection);
  59. auto palette = editor().palette();
  60. palette.set_color(Gfx::ColorRole::BaseText, Color::from_rgb(0xffffff));
  61. editor().set_palette(palette);
  62. editor().document().set_text("The contents of this file could not be displayed. Is it a binary file?");
  63. }
  64. void EditorWrapper::set_filename(const String& filename)
  65. {
  66. m_filename = filename;
  67. update_title();
  68. update_diff();
  69. }
  70. void EditorWrapper::save()
  71. {
  72. editor().write_to_file(filename());
  73. update_diff();
  74. editor().update();
  75. }
  76. void EditorWrapper::update_diff()
  77. {
  78. if (m_git_repo)
  79. m_hunks = Diff::parse_hunks(m_git_repo->unstaged_diff(LexicalPath(filename())).value());
  80. }
  81. void EditorWrapper::set_project_root(LexicalPath 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_null())
  101. title.append(untitled_label);
  102. else
  103. title.append(m_filename);
  104. if (editor().document().is_modified())
  105. title.append(" (*)");
  106. m_filename_label->set_text(title.to_string());
  107. }
  108. void EditorWrapper::set_debug_mode(bool enabled)
  109. {
  110. m_editor->set_debug_mode(enabled);
  111. }
  112. }