Refactor resize handling in NCursesUI

This commit is contained in:
Maxime Coste 2015-06-30 00:31:26 +01:00
parent 3fe8446065
commit 130b22ff74
2 changed files with 34 additions and 37 deletions

View File

@ -239,11 +239,6 @@ void on_term_resize(int)
EventManager::instance().force_signal(0); EventManager::instance().force_signal(0);
} }
void on_sigint(int)
{
// do nothing
}
NCursesUI::NCursesUI() NCursesUI::NCursesUI()
: m_stdin_watcher{0, [this](FDWatcher&, EventMode mode) { : m_stdin_watcher{0, [this](FDWatcher&, EventMode mode) {
if (m_input_callback) if (m_input_callback)
@ -262,16 +257,17 @@ NCursesUI::NCursesUI()
use_default_colors(); use_default_colors();
set_escdelay(25); set_escdelay(25);
signal(SIGWINCH, on_term_resize);
signal(SIGINT, on_sigint);
mousemask(ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, nullptr); mousemask(ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, nullptr);
mouseinterval(0); mouseinterval(0);
// force enable report mouse position // force enable report mouse position
puts("\033[?1002h"); puts("\033[?1002h");
update_dimensions();
wrefresh(stdscr); signal(SIGWINCH, on_term_resize);
signal(SIGINT, [](int){});
check_resize(true);
redraw();
} }
NCursesUI::~NCursesUI() NCursesUI::~NCursesUI()
@ -326,17 +322,6 @@ static CharCoord window_pos(WINDOW* win)
return pos; return pos;
} }
void NCursesUI::update_dimensions()
{
m_dimensions = window_size(stdscr);
if (m_window)
delwin(m_window);
m_window = (NCursesWin*)newwin((int)m_dimensions.line, (int)m_dimensions.column, 0, 0);
--m_dimensions.line;
}
void NCursesUI::draw_line(const DisplayLine& line, CharCount col_index, void NCursesUI::draw_line(const DisplayLine& line, CharCount col_index,
const Face& default_face) const const Face& default_face) const
{ {
@ -440,22 +425,32 @@ void NCursesUI::draw_status(const DisplayLine& status_line,
m_dirty = true; m_dirty = true;
} }
void NCursesUI::check_resize() void NCursesUI::check_resize(bool force)
{ {
if (resize_pending) if (not force and not resize_pending)
return;
resize_pending = 0;
const int fd = open("/dev/tty", O_RDWR);
auto close_fd = on_scope_end([fd]{ close(fd); });
winsize ws;
if (ioctl(fd, TIOCGWINSZ, (void*)&ws) == 0)
{ {
int fd = open("/dev/tty", O_RDWR); resize_term(ws.ws_row, ws.ws_col);
winsize ws;
if (ioctl(fd, TIOCGWINSZ, (void*)&ws) == 0) if (m_window)
{ delwin(m_window);
close(fd); m_window = (NCursesWin*)newwin(ws.ws_row, ws.ws_col, 0, 0);
resizeterm(ws.ws_row, ws.ws_col);
update_dimensions(); m_dimensions = CharCoord{ws.ws_row-1, ws.ws_col};
}
ungetch(KEY_RESIZE);
m_dirty = true;
resize_pending = false;
} }
else
kak_assert(false);
m_dirty = true;
ungetch(KEY_RESIZE);
wrefresh(stdscr);
} }
bool NCursesUI::is_key_available() bool NCursesUI::is_key_available()
@ -496,7 +491,10 @@ Key NCursesUI::get_key()
if (c > 0 and c < 27) if (c > 0 and c < 27)
{ {
if (c == CTRL('l')) if (c == CTRL('l'))
redrawwin(stdscr); {
redrawwin(m_window);
redraw();
}
if (c == CTRL('z')) if (c == CTRL('z'))
{ {
raise(SIGTSTP); raise(SIGTSTP);

View File

@ -52,7 +52,7 @@ public:
static void abort(); static void abort();
private: private:
void check_resize(); void check_resize(bool force = false);
void redraw(); void redraw();
void draw_line(const DisplayLine& line, CharCount col_index, void draw_line(const DisplayLine& line, CharCount col_index,
const Face& default_face) const; const Face& default_face) const;
@ -62,7 +62,6 @@ private:
NCursesWin* m_window = nullptr; NCursesWin* m_window = nullptr;
CharCoord m_dimensions; CharCoord m_dimensions;
void update_dimensions();
NCursesWin* m_menu_win = nullptr; NCursesWin* m_menu_win = nullptr;
Vector<String> m_items; Vector<String> m_items;