TerminalWidget.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/DeprecatedString.h>
  9. #include <LibCore/ElapsedTimer.h>
  10. #include <LibCore/Notifier.h>
  11. #include <LibCore/Timer.h>
  12. #include <LibGUI/Clipboard.h>
  13. #include <LibGUI/Frame.h>
  14. #include <LibGfx/Bitmap.h>
  15. #include <LibGfx/Rect.h>
  16. #include <LibVT/Color.h>
  17. #include <LibVT/Range.h>
  18. #include <LibVT/Terminal.h>
  19. namespace VT {
  20. class TerminalWidget final
  21. : public GUI::Frame
  22. , public VT::TerminalClient
  23. , public GUI::Clipboard::ClipboardClient {
  24. C_OBJECT(TerminalWidget);
  25. public:
  26. virtual ~TerminalWidget() override = default;
  27. void set_pty_master_fd(int fd);
  28. void inject_string(StringView string)
  29. {
  30. m_terminal.inject_string(string);
  31. flush_dirty_lines();
  32. }
  33. void flush_dirty_lines();
  34. void apply_size_increments_to_window(GUI::Window&);
  35. void set_opacity(u8);
  36. float opacity() { return m_opacity; };
  37. void set_show_scrollbar(bool);
  38. enum class BellMode {
  39. Visible,
  40. AudibleBeep,
  41. Disabled
  42. };
  43. BellMode bell_mode() { return m_bell_mode; }
  44. void set_bell_mode(BellMode bm) { m_bell_mode = bm; };
  45. bool has_selection() const;
  46. bool selection_contains(const VT::Position&) const;
  47. DeprecatedString selected_text() const;
  48. VT::Range normalized_selection() const { return m_selection.normalized(); }
  49. void set_selection(const VT::Range& selection);
  50. VT::Position buffer_position_at(Gfx::IntPoint) const;
  51. VT::Range find_next(StringView, const VT::Position& start = {}, bool case_sensitivity = false, bool should_wrap = false);
  52. VT::Range find_previous(StringView, const VT::Position& start = {}, bool case_sensitivity = false, bool should_wrap = false);
  53. void scroll_to_bottom();
  54. void scroll_to_row(int);
  55. bool is_scrollable() const;
  56. int scroll_length() const;
  57. size_t max_history_size() const { return m_terminal.max_history_size(); }
  58. void set_max_history_size(size_t value) { m_terminal.set_max_history_size(value); }
  59. GUI::Action& copy_action() { return *m_copy_action; }
  60. GUI::Action& paste_action() { return *m_paste_action; }
  61. GUI::Action& clear_including_history_action() { return *m_clear_including_history_action; }
  62. void copy();
  63. void paste();
  64. void clear_including_history();
  65. const StringView color_scheme_name() const { return m_color_scheme_name; }
  66. Function<void(StringView)> on_title_change;
  67. Function<void(Gfx::IntSize)> on_terminal_size_change;
  68. Function<void()> on_command_exit;
  69. GUI::Menu& context_menu() { return *m_context_menu; }
  70. constexpr Gfx::Color terminal_color_to_rgb(VT::Color) const;
  71. void set_font_and_resize_to_fit(Gfx::Font const&);
  72. void update_color_scheme();
  73. void set_logical_focus(bool);
  74. VT::CursorShape cursor_shape() { return m_cursor_shape; }
  75. virtual void set_cursor_blinking(bool) override;
  76. virtual void set_cursor_shape(CursorShape) override;
  77. static Optional<VT::CursorShape> parse_cursor_shape(StringView);
  78. static DeprecatedString stringify_cursor_shape(VT::CursorShape);
  79. private:
  80. TerminalWidget(int ptm_fd, bool automatic_size_policy);
  81. // ^GUI::Widget
  82. virtual void event(Core::Event&) override;
  83. virtual void paint_event(GUI::PaintEvent&) override;
  84. virtual void resize_event(GUI::ResizeEvent&) override;
  85. virtual void keydown_event(GUI::KeyEvent&) override;
  86. virtual void keyup_event(GUI::KeyEvent&) override;
  87. virtual void mousedown_event(GUI::MouseEvent&) override;
  88. virtual void mouseup_event(GUI::MouseEvent&) override;
  89. virtual void mousemove_event(GUI::MouseEvent&) override;
  90. virtual void mousewheel_event(GUI::MouseEvent&) override;
  91. virtual void doubleclick_event(GUI::MouseEvent&) override;
  92. virtual void focusin_event(GUI::FocusEvent&) override;
  93. virtual void focusout_event(GUI::FocusEvent&) override;
  94. virtual void context_menu_event(GUI::ContextMenuEvent&) override;
  95. virtual void drag_enter_event(GUI::DragEvent&) override;
  96. virtual void drop_event(GUI::DropEvent&) override;
  97. virtual void leave_event(Core::Event&) override;
  98. virtual void did_change_font() override;
  99. // ^TerminalClient
  100. virtual void beep() override;
  101. virtual void set_window_title(StringView) override;
  102. virtual void set_window_progress(int value, int max) override;
  103. virtual void terminal_did_resize(u16 columns, u16 rows) override;
  104. virtual void terminal_history_changed(int delta) override;
  105. virtual void emit(u8 const*, size_t) override;
  106. // ^GUI::Clipboard::ClipboardClient
  107. virtual void clipboard_content_did_change(DeprecatedString const&) override { update_paste_action(); }
  108. void send_non_user_input(ReadonlyBytes);
  109. Gfx::IntRect glyph_rect(u16 row, u16 column);
  110. Gfx::IntRect row_rect(u16 row);
  111. Gfx::IntSize widget_size_for_font(Gfx::Font const&) const;
  112. void update_cursor();
  113. void invalidate_cursor();
  114. void relayout(Gfx::IntSize);
  115. void update_copy_action();
  116. void update_paste_action();
  117. Gfx::IntSize compute_base_size() const;
  118. int first_selection_column_on_row(int row) const;
  119. int last_selection_column_on_row(int row) const;
  120. u32 code_point_at(const VT::Position&) const;
  121. VT::Position next_position_after(const VT::Position&, bool should_wrap) const;
  122. VT::Position previous_position_before(const VT::Position&, bool should_wrap) const;
  123. void update_cached_font_metrics();
  124. VT::Terminal m_terminal;
  125. VT::Range m_selection;
  126. DeprecatedString m_hovered_href;
  127. DeprecatedString m_hovered_href_id;
  128. DeprecatedString m_active_href;
  129. DeprecatedString m_active_href_id;
  130. // Snapshot of m_hovered_href when opening a context menu for a hyperlink.
  131. DeprecatedString m_context_menu_href;
  132. Gfx::Color m_colors[256];
  133. Gfx::Color m_default_foreground_color;
  134. Gfx::Color m_default_background_color;
  135. bool m_show_bold_text_as_bright { true };
  136. DeprecatedString m_color_scheme_name;
  137. BellMode m_bell_mode { BellMode::Visible };
  138. bool m_alt_key_held { false };
  139. bool m_rectangle_selection { false };
  140. int m_pixel_width { 0 };
  141. int m_pixel_height { 0 };
  142. int m_inset { 2 };
  143. int m_line_spacing { 4 };
  144. int m_line_height { 0 };
  145. int m_cell_height { 0 };
  146. int m_column_width { 0 };
  147. int m_ptm_fd { -1 };
  148. bool m_has_logical_focus { false };
  149. bool m_in_relayout { false };
  150. RefPtr<Core::Notifier> m_notifier;
  151. u8 m_opacity { 255 };
  152. bool m_cursor_blink_state { true };
  153. bool m_automatic_size_policy { false };
  154. VT::CursorShape m_cursor_shape { VT::CursorShape::Block };
  155. bool m_cursor_is_blinking_set { true };
  156. enum class AutoScrollDirection {
  157. None,
  158. Up,
  159. Down
  160. };
  161. void set_auto_scroll_direction(AutoScrollDirection);
  162. AutoScrollDirection m_auto_scroll_direction { AutoScrollDirection::None };
  163. RefPtr<Core::Timer> m_cursor_blink_timer;
  164. RefPtr<Core::Timer> m_visual_beep_timer;
  165. RefPtr<Core::Timer> m_auto_scroll_timer;
  166. RefPtr<GUI::Scrollbar> m_scrollbar;
  167. RefPtr<GUI::Action> m_copy_action;
  168. RefPtr<GUI::Action> m_paste_action;
  169. RefPtr<GUI::Action> m_clear_including_history_action;
  170. RefPtr<GUI::Menu> m_context_menu;
  171. RefPtr<GUI::Menu> m_context_menu_for_hyperlink;
  172. Core::ElapsedTimer m_triple_click_timer;
  173. Gfx::IntPoint m_left_mousedown_position;
  174. VT::Position m_left_mousedown_position_buffer;
  175. };
  176. }