EditorWrapper.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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/Action.h>
  10. #include <LibGUI/Application.h>
  11. #include <LibGUI/BoxLayout.h>
  12. #include <LibGUI/Label.h>
  13. #include <LibGfx/Font.h>
  14. #include <LibGfx/FontDatabase.h>
  15. #include <LibGfx/Palette.h>
  16. namespace HackStudio {
  17. EditorWrapper::EditorWrapper()
  18. {
  19. set_layout<GUI::VerticalBoxLayout>();
  20. auto& label_wrapper = add<GUI::Widget>();
  21. label_wrapper.set_fixed_height(14);
  22. label_wrapper.set_fill_with_background_color(true);
  23. label_wrapper.set_layout<GUI::HorizontalBoxLayout>();
  24. label_wrapper.layout()->set_margins({ 2, 0, 2, 0 });
  25. m_filename_label = label_wrapper.add<GUI::Label>("(Untitled)");
  26. m_filename_label->set_text_alignment(Gfx::TextAlignment::CenterLeft);
  27. m_filename_label->set_fixed_height(14);
  28. m_cursor_label = label_wrapper.add<GUI::Label>("(Cursor)");
  29. m_cursor_label->set_text_alignment(Gfx::TextAlignment::CenterRight);
  30. m_cursor_label->set_fixed_height(14);
  31. m_editor = add<Editor>();
  32. m_editor->set_ruler_visible(true);
  33. m_editor->set_automatic_indentation_enabled(true);
  34. m_editor->on_cursor_change = [this] {
  35. m_cursor_label->set_text(String::formatted("Line: {}, Column: {}", m_editor->cursor().line() + 1, m_editor->cursor().column()));
  36. };
  37. m_editor->on_focus = [this] {
  38. set_current_editor_wrapper(this);
  39. };
  40. m_editor->on_open = [](String path) {
  41. open_file(path);
  42. };
  43. m_editor->on_change = [this] {
  44. bool was_dirty = m_document_dirty;
  45. m_document_dirty = true;
  46. if (!was_dirty)
  47. update_title();
  48. };
  49. }
  50. EditorWrapper::~EditorWrapper()
  51. {
  52. }
  53. void EditorWrapper::set_editor_has_focus(Badge<Editor>, bool focus)
  54. {
  55. auto& font = Gfx::FontDatabase::default_font();
  56. m_filename_label->set_font(focus ? font.bold_variant() : font);
  57. }
  58. LanguageClient& EditorWrapper::language_client() { return m_editor->language_client(); }
  59. void EditorWrapper::set_mode_displayable()
  60. {
  61. editor().set_mode(GUI::TextEditor::Editable);
  62. editor().set_background_role(Gfx::ColorRole::Base);
  63. editor().set_palette(GUI::Application::the()->palette());
  64. }
  65. void EditorWrapper::set_mode_non_displayable()
  66. {
  67. editor().set_mode(GUI::TextEditor::ReadOnly);
  68. editor().set_background_role(Gfx::ColorRole::InactiveSelection);
  69. auto palette = editor().palette();
  70. palette.set_color(Gfx::ColorRole::BaseText, Color::from_rgb(0xffffff));
  71. editor().set_palette(palette);
  72. editor().document().set_text("The contents of this file could not be displayed. Is it a binary file?");
  73. }
  74. void EditorWrapper::set_filename(const String& filename)
  75. {
  76. m_filename = filename;
  77. update_title();
  78. update_diff();
  79. }
  80. void EditorWrapper::save()
  81. {
  82. editor().write_to_file(filename());
  83. m_document_dirty = false;
  84. update_title();
  85. update_diff();
  86. editor().update();
  87. }
  88. void EditorWrapper::update_diff()
  89. {
  90. if (m_git_repo)
  91. m_hunks = Diff::parse_hunks(m_git_repo->unstaged_diff(LexicalPath(filename())).value());
  92. }
  93. void EditorWrapper::set_project_root(LexicalPath const& project_root)
  94. {
  95. m_project_root = project_root;
  96. auto result = GitRepo::try_to_create(*m_project_root);
  97. switch (result.type) {
  98. case GitRepo::CreateResult::Type::Success:
  99. m_git_repo = result.repo;
  100. break;
  101. case GitRepo::CreateResult::Type::GitProgramNotFound:
  102. break;
  103. case GitRepo::CreateResult::Type::NoGitRepo:
  104. break;
  105. default:
  106. VERIFY_NOT_REACHED();
  107. }
  108. }
  109. void EditorWrapper::update_title()
  110. {
  111. StringBuilder title;
  112. title.append(m_filename);
  113. if (m_document_dirty)
  114. title.append(" (*)");
  115. m_filename_label->set_text(title.to_string());
  116. }
  117. }