EditorWrapper.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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)");
  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_change = [this] {
  37. if (this->on_change)
  38. this->on_change();
  39. bool was_dirty = m_document_dirty;
  40. m_document_dirty = true;
  41. if (!was_dirty)
  42. update_title();
  43. };
  44. }
  45. EditorWrapper::~EditorWrapper()
  46. {
  47. }
  48. void EditorWrapper::set_editor_has_focus(Badge<Editor>, bool focus)
  49. {
  50. auto& font = Gfx::FontDatabase::default_font();
  51. m_filename_label->set_font(focus ? font.bold_variant() : font);
  52. }
  53. LanguageClient& EditorWrapper::language_client() { return m_editor->language_client(); }
  54. void EditorWrapper::set_mode_displayable()
  55. {
  56. editor().set_mode(GUI::TextEditor::Editable);
  57. editor().set_background_role(Gfx::ColorRole::Base);
  58. editor().set_palette(GUI::Application::the()->palette());
  59. }
  60. void EditorWrapper::set_mode_non_displayable()
  61. {
  62. editor().set_mode(GUI::TextEditor::ReadOnly);
  63. editor().set_background_role(Gfx::ColorRole::InactiveSelection);
  64. auto palette = editor().palette();
  65. palette.set_color(Gfx::ColorRole::BaseText, Color::from_rgb(0xffffff));
  66. editor().set_palette(palette);
  67. editor().document().set_text("The contents of this file could not be displayed. Is it a binary file?");
  68. }
  69. void EditorWrapper::set_filename(const String& filename)
  70. {
  71. m_filename = filename;
  72. update_title();
  73. update_diff();
  74. }
  75. void EditorWrapper::save()
  76. {
  77. editor().write_to_file(filename());
  78. m_document_dirty = false;
  79. update_title();
  80. update_diff();
  81. editor().update();
  82. }
  83. void EditorWrapper::update_diff()
  84. {
  85. if (m_git_repo)
  86. m_hunks = Diff::parse_hunks(m_git_repo->unstaged_diff(LexicalPath(filename())).value());
  87. }
  88. void EditorWrapper::set_project_root(LexicalPath const& project_root)
  89. {
  90. m_project_root = project_root;
  91. auto result = GitRepo::try_to_create(*m_project_root);
  92. switch (result.type) {
  93. case GitRepo::CreateResult::Type::Success:
  94. m_git_repo = result.repo;
  95. break;
  96. case GitRepo::CreateResult::Type::GitProgramNotFound:
  97. break;
  98. case GitRepo::CreateResult::Type::NoGitRepo:
  99. break;
  100. default:
  101. VERIFY_NOT_REACHED();
  102. }
  103. }
  104. void EditorWrapper::update_title()
  105. {
  106. StringBuilder title;
  107. title.append(m_filename);
  108. if (m_document_dirty)
  109. title.append(" (*)");
  110. m_filename_label->set_text(title.to_string());
  111. }
  112. void EditorWrapper::set_debug_mode(bool enabled)
  113. {
  114. m_editor->set_debug_mode(enabled);
  115. }
  116. }