From 7af9863e5181d2b78d7051f92d62f85e27ce9a29 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 15 Apr 2014 19:19:44 +0100 Subject: [PATCH] Add an explicit refresh method to user interface With this refresh method user interface can defer updating the display until really needed. --- src/client.cc | 1 + src/ncurses.cc | 18 +++++++++++++----- src/ncurses.hh | 4 ++++ src/remote.cc | 14 +++++++++++++- src/user_interface.hh | 2 ++ 5 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/client.cc b/src/client.cc index cda0ef5c..8abef5ab 100644 --- a/src/client.cc +++ b/src/client.cc @@ -91,6 +91,7 @@ void Client::redraw_ifn() context().ui().draw(context().window().display_buffer(), m_status_line, generate_mode_line()); } + context().ui().refresh(); } static void reload_buffer(Context& context, const String& filename) diff --git a/src/ncurses.cc b/src/ncurses.cc index 4380c419..0553a7ff 100644 --- a/src/ncurses.cc +++ b/src/ncurses.cc @@ -195,6 +195,14 @@ void NCursesUI::redraw() } doupdate(); } + +void NCursesUI::refresh() +{ + if (m_dirty) + redraw(); + m_dirty = false; +} + using Utf8Policy = utf8::InvalidBytePolicy::Pass; using Utf8Iterator = utf8::utf8_iterator; void addutf8str(WINDOW* win, Utf8Iterator begin, Utf8Iterator end) @@ -301,7 +309,7 @@ void NCursesUI::draw(const DisplayBuffer& display_buffer, printf("%s%s%s", tsl, title.c_str(), fsl); } - redraw(); + m_dirty = true; } bool NCursesUI::is_key_available() @@ -438,7 +446,7 @@ void NCursesUI::draw_menu() wattron(m_menu_win, COLOR_PAIR(menu_bg)); waddstr(m_menu_win, is_mark ? "┃" : "│"); } - redraw(); + m_dirty = true; } void NCursesUI::menu_show(memoryview items, @@ -520,7 +528,7 @@ void NCursesUI::menu_hide() (int)window_size(m_menu_win).line); delwin(m_menu_win); m_menu_win = nullptr; - redraw(); + m_dirty = true; } static DisplayCoord compute_needed_size(const String& str) @@ -711,7 +719,7 @@ void NCursesUI::info_show(const String& title, const String& content, break; it = eol + 1; } - redraw(); + m_dirty = true; } void NCursesUI::info_hide() @@ -722,7 +730,7 @@ void NCursesUI::info_hide() (int)window_size(m_info_win).line); delwin(m_info_win); m_info_win = nullptr; - redraw(); + m_dirty = true; } DisplayCoord NCursesUI::dimensions() diff --git a/src/ncurses.hh b/src/ncurses.hh index 40661fe4..4edd7502 100644 --- a/src/ncurses.hh +++ b/src/ncurses.hh @@ -37,6 +37,8 @@ public: MenuStyle style) override; void info_hide() override; + void refresh() override; + void set_input_callback(InputCallback callback) override; DisplayCoord dimensions() override; @@ -63,6 +65,8 @@ private: FDWatcher m_stdin_watcher; InputCallback m_input_callback; + + bool m_dirty = false; }; } diff --git a/src/remote.cc b/src/remote.cc index a72437ad..0c4ed900 100644 --- a/src/remote.cc +++ b/src/remote.cc @@ -25,7 +25,8 @@ enum class RemoteUIMsg MenuHide, InfoShow, InfoHide, - Draw + Draw, + Refresh }; struct socket_error{}; @@ -256,6 +257,8 @@ public: const DisplayLine& status_line, const DisplayLine& mode_line) override; + void refresh() override; + bool is_key_available() override; Key get_key() override; DisplayCoord dimensions() override; @@ -342,6 +345,12 @@ void RemoteUI::draw(const DisplayBuffer& display_buffer, msg.write(mode_line); } +void RemoteUI::refresh() +{ + Message msg(m_socket_watcher.fd()); + msg.write(RemoteUIMsg::Refresh); +} + static const Key::Modifiers resize_modifier = (Key::Modifiers)0x80; bool RemoteUI::is_key_available() @@ -466,6 +475,9 @@ void RemoteClient::process_next_message() m_ui->draw(display_buffer, status_line, mode_line); break; } + case RemoteUIMsg::Refresh: + m_ui->refresh(); + break; } } diff --git a/src/user_interface.hh b/src/user_interface.hh index affe822b..00366cb2 100644 --- a/src/user_interface.hh +++ b/src/user_interface.hh @@ -45,6 +45,8 @@ public: virtual bool is_key_available() = 0; virtual Key get_key() = 0; + virtual void refresh() = 0; + virtual void set_input_callback(InputCallback callback) = 0; };