client.hh (4864B)
1 #ifndef client_hh_INCLUDED 2 #define client_hh_INCLUDED 3 4 #include "array.hh" 5 #include "clock.hh" 6 #include "display_buffer.hh" 7 #include "env_vars.hh" 8 #include "event_manager.hh" 9 #include "input_handler.hh" 10 #include "safe_ptr.hh" 11 #include "unique_ptr.hh" 12 #include "utils.hh" 13 #include "option.hh" 14 #include "enum.hh" 15 16 namespace Kakoune 17 { 18 19 class Window; 20 class UserInterface; 21 class String; 22 struct Key; 23 24 enum class InfoStyle; 25 enum class MenuStyle; 26 enum class StatusStyle; 27 28 class Client final : public SafeCountable, public OptionWatcher 29 { 30 public: 31 using OnExitCallback = Function<void (int status)>; 32 33 Client(UniquePtr<UserInterface>&& ui, 34 UniquePtr<Window>&& window, 35 SelectionList selections, 36 int pid, EnvVarMap env_vars, 37 String name, 38 OnExitCallback on_exit); 39 ~Client(); 40 41 Client(Client&&) = delete; 42 43 bool is_ui_ok() const; 44 45 bool process_pending_inputs(); 46 bool has_pending_inputs() const { return not m_pending_keys.empty(); } 47 48 void menu_show(Vector<DisplayLine> choices, BufferCoord anchor, MenuStyle style); 49 void menu_select(int selected); 50 void menu_hide(); 51 52 void info_show(DisplayLine title, DisplayLineList content, BufferCoord anchor, InfoStyle style); 53 void info_show(StringView title, StringView content, BufferCoord anchor, InfoStyle style); 54 void info_hide(bool even_modal = false); 55 bool info_pending() const { return m_ui_pending & PendingUI::InfoShow; }; 56 bool status_line_pending() const { return m_ui_pending & PendingUI::StatusLine; }; 57 58 void print_status(DisplayLine prompt, DisplayLine content, ColumnCount cursor_pos, StatusStyle style); 59 60 DisplayCoord dimensions() const; 61 62 void schedule_clear(); 63 void clear_pending(); 64 65 void force_redraw(bool full = false); 66 void redraw_ifn(); 67 68 void check_if_buffer_needs_reloading(); 69 70 Context& context() { return m_input_handler.context(); } 71 const Context& context() const { return m_input_handler.context(); } 72 73 InputHandler& input_handler() { return m_input_handler; } 74 const InputHandler& input_handler() const { return m_input_handler; } 75 76 void change_buffer(Buffer& buffer, Optional<FunctionRef<void()>> set_selection); 77 78 StringView get_env_var(StringView name) const; 79 80 void exit(int status) { m_on_exit(status); } 81 82 int pid() const { return m_pid; } 83 84 private: 85 friend class BusyIndicator; 86 87 void on_option_changed(const Option& option) override; 88 89 void on_buffer_reload_key(Key key); 90 void close_buffer_reload_dialog(); 91 void reload_buffer(); 92 93 DisplayLine generate_mode_line() const; 94 95 UniquePtr<UserInterface> m_ui; 96 UniquePtr<Window> m_window; 97 98 const int m_pid; 99 100 OnExitCallback m_on_exit; 101 102 EnvVarMap m_env_vars; 103 104 InputHandler m_input_handler; 105 106 DisplayLine m_status_prompt; 107 DisplayLine m_status_content; 108 ColumnCount m_status_cursor_pos; 109 StatusStyle m_status_style{}; 110 DisplayLine m_mode_line; 111 112 enum PendingUI : int 113 { 114 MenuShow = 1 << 0, 115 MenuSelect = 1 << 1, 116 MenuHide = 1 << 2, 117 InfoShow = 1 << 3, 118 InfoHide = 1 << 4, 119 StatusLine = 1 << 5, 120 Draw = 1 << 6, 121 Refresh = 1 << 7, 122 }; 123 int m_ui_pending = 0; 124 125 enum class PendingClear 126 { 127 None = 0, 128 Info = 0b01, 129 StatusLine = 0b10 130 }; 131 friend constexpr bool with_bit_ops(Meta::Type<PendingClear>) { return true; } 132 PendingClear m_pending_clear = PendingClear::None; 133 134 135 struct Menu 136 { 137 Vector<DisplayLine> items; 138 BufferCoord anchor; 139 Optional<DisplayCoord> ui_anchor; 140 MenuStyle style; 141 int selected; 142 } m_menu{}; 143 144 struct Info 145 { 146 DisplayLine title; 147 DisplayLineList content; 148 BufferCoord anchor; 149 Optional<DisplayCoord> ui_anchor; 150 InfoStyle style; 151 } m_info{}; 152 153 Vector<Key, MemoryDomain::Client> m_pending_keys; 154 155 bool m_buffer_reload_dialog_opened = false; 156 }; 157 158 enum class Autoreload 159 { 160 Yes, 161 No, 162 Ask 163 }; 164 165 constexpr auto enum_desc(Meta::Type<Autoreload>) 166 { 167 return make_array<EnumDesc<Autoreload>>({ 168 { Autoreload::Yes, "yes" }, 169 { Autoreload::No, "no" }, 170 { Autoreload::Ask, "ask" }, 171 { Autoreload::Yes, "true" }, 172 { Autoreload::No, "false" } 173 }); 174 } 175 176 class BusyIndicator 177 { 178 public: 179 BusyIndicator(const Context& context, 180 Function<DisplayLine(std::chrono::seconds)> status_message, 181 TimePoint wait_time = Clock::now()); 182 ~BusyIndicator(); 183 private: 184 const Context& m_context; 185 Timer m_timer; 186 struct PreviousStatus 187 { 188 DisplayLine prompt; 189 DisplayLine content; 190 ColumnCount cursor_pos; 191 StatusStyle style; 192 }; 193 Optional<PreviousStatus> m_previous_status; 194 }; 195 196 } 197 198 #endif // client_hh_INCLUDED