refresh optimization by limiting the .refresh() calls at the STRICT minimum

parent eabf05c8
...@@ -69,9 +69,6 @@ SHOW_NAME = { ...@@ -69,9 +69,6 @@ SHOW_NAME = {
resize_lock = threading.Lock() resize_lock = threading.Lock()
def doupdate():
curses.doupdate()
class Gui(object): class Gui(object):
""" """
User interface using ncurses User interface using ncurses
...@@ -82,6 +79,7 @@ class Gui(object): ...@@ -82,6 +79,7 @@ class Gui(object):
self.xmpp = xmpp self.xmpp = xmpp
default_tab = InfoTab(self.stdscr, "Info") if self.xmpp.anon\ default_tab = InfoTab(self.stdscr, "Info") if self.xmpp.anon\
else RosterInfoTab(self.stdscr) else RosterInfoTab(self.stdscr)
default_tab.on_gain_focus()
self.tabs = [default_tab] self.tabs = [default_tab]
self.roster = Roster() self.roster = Roster()
# a unique buffer used to store global informations # a unique buffer used to store global informations
...@@ -281,7 +279,7 @@ class Gui(object): ...@@ -281,7 +279,7 @@ class Gui(object):
else: else:
self.on_user_change_status(room, user, from_nick, from_room, affiliation, role, show, status) self.on_user_change_status(room, user, from_nick, from_room, affiliation, role, show, status)
self.refresh_window() self.refresh_window()
doupdate() self.doupdate()
def on_user_join(self, room, from_nick, affiliation, show, status, role, jid): def on_user_join(self, room, from_nick, affiliation, show, status, role, jid):
""" """
...@@ -426,7 +424,7 @@ class Gui(object): ...@@ -426,7 +424,7 @@ class Gui(object):
We received a Private Message (from someone in a Muc) We received a Private Message (from someone in a Muc)
""" """
jid = message['from'] jid = message['from']
nick_from = jid.boundjid.resource nick_from = jid.resource
room_from = jid.bare room_from = jid.bare
room = self.get_room_by_name(jid.full) # get the tab with the private conversation room = self.get_room_by_name(jid.full) # get the tab with the private conversation
if not room: # It's the first message we receive: create the tab if not room: # It's the first message we receive: create the tab
...@@ -436,7 +434,7 @@ class Gui(object): ...@@ -436,7 +434,7 @@ class Gui(object):
body = message['body'] body = message['body']
self.add_message_to_text_buffer(room, body, None, nick_from) self.add_message_to_text_buffer(room, body, None, nick_from)
self.refresh_window() self.refresh_window()
doupdate() self.doupdate()
def focus_tab_named(self, tab_name): def focus_tab_named(self, tab_name):
for tab in self.tabs: for tab in self.tabs:
...@@ -504,14 +502,15 @@ class Gui(object): ...@@ -504,14 +502,15 @@ class Gui(object):
This is to avoid multiple unnecessary software resizes (this This is to avoid multiple unnecessary software resizes (this
can be heavy on resource on slow computers or networks) can be heavy on resource on slow computers or networks)
""" """
with resize_lock: # with resize_lock:
if self.resize_timer: # if self.resize_timer:
# a recent terminal resize occured. # # a recent terminal resize occured.
# Cancel the programmed software resize # # Cancel the programmed software resize
self.resize_timer.cancel() # self.resize_timer.cancel()
# add the new timer # # add the new timer
self.resize_timer = threading.Timer(0.15, self.resize_window) # self.resize_timer = threading.Timer(0.15, self.resize_window)
self.resize_timer.start() # self.resize_timer.start()
self.resize_window()
def resize_window(self): def resize_window(self):
""" """
...@@ -529,7 +528,7 @@ class Gui(object): ...@@ -529,7 +528,7 @@ class Gui(object):
""" """
self.refresh_window() self.refresh_window()
while True: while True:
doupdate() self.doupdate()
char=read_char(self.stdscr) char=read_char(self.stdscr)
# search for keyboard shortcut # search for keyboard shortcut
if char in list(self.key_func.keys()): if char in list(self.key_func.keys()):
...@@ -567,8 +566,10 @@ class Gui(object): ...@@ -567,8 +566,10 @@ class Gui(object):
""" """
ncurses initialization ncurses initialization
""" """
theme.init_colors() curses.curs_set(1)
curses.noecho() curses.noecho()
# curses.raw()
theme.init_colors()
stdscr.keypad(True) stdscr.keypad(True)
def reset_curses(self): def reset_curses(self):
...@@ -586,7 +587,6 @@ class Gui(object): ...@@ -586,7 +587,6 @@ class Gui(object):
""" """
self.current_tab().set_color_state(theme.COLOR_TAB_CURRENT) self.current_tab().set_color_state(theme.COLOR_TAB_CURRENT)
self.current_tab().refresh(self.tabs, self.information_buffer, self.roster) self.current_tab().refresh(self.tabs, self.information_buffer, self.roster)
doupdate()
def open_new_room(self, room, nick, focus=True): def open_new_room(self, room, nick, focus=True):
""" """
...@@ -778,7 +778,7 @@ class Gui(object): ...@@ -778,7 +778,7 @@ class Gui(object):
date = date if delayed == True else None date = date if delayed == True else None
self.add_message_to_text_buffer(room, body, date, nick_from) self.add_message_to_text_buffer(room, body, date, nick_from)
self.refresh_window() self.refresh_window()
doupdate() self.doupdate()
def add_message_to_text_buffer(self, room, txt, time=None, nickname=None, colorized=False): def add_message_to_text_buffer(self, room, txt, time=None, nickname=None, colorized=False):
""" """
...@@ -1334,4 +1334,7 @@ class Gui(object): ...@@ -1334,4 +1334,7 @@ class Gui(object):
self.add_message_to_text_buffer(self.current_tab().get_room(), line, None, self.current_tab().get_room().own_nick) self.add_message_to_text_buffer(self.current_tab().get_room(), line, None, self.current_tab().get_room().own_nick)
elif isinstance(self.current_tab(), MucTab): elif isinstance(self.current_tab(), MucTab):
muc.send_groupchat_message(self.xmpp, self.current_tab().get_name(), line) muc.send_groupchat_message(self.xmpp, self.current_tab().get_name(), line)
doupdate() self.doupdate()
def doupdate(self):
curses.doupdate()
...@@ -27,6 +27,7 @@ MIN_HEIGHT = 16 ...@@ -27,6 +27,7 @@ MIN_HEIGHT = 16
import window import window
import theme import theme
import curses
from roster import RosterGroup from roster import RosterGroup
from common import debug from common import debug
...@@ -118,6 +119,14 @@ class Tab(object): ...@@ -118,6 +119,14 @@ class Tab(object):
""" """
raise NotImplementedError raise NotImplementedError
def just_before_refresh(self):
"""
Method called just before the screen refresh.
Particularly useful to move the cursor at the
correct position.
"""
raise NotImplementedError
class InfoTab(Tab): class InfoTab(Tab):
""" """
The information tab, used to display global informations The information tab, used to display global informations
...@@ -159,6 +168,7 @@ class InfoTab(Tab): ...@@ -159,6 +168,7 @@ class InfoTab(Tab):
def on_gain_focus(self): def on_gain_focus(self):
self.color_state = theme.COLOR_TAB_CURRENT self.color_state = theme.COLOR_TAB_CURRENT
curses.curs_set(0)
def on_scroll_up(self): def on_scroll_up(self):
pass pass
...@@ -169,6 +179,9 @@ class InfoTab(Tab): ...@@ -169,6 +179,9 @@ class InfoTab(Tab):
def on_info_win_size_changed(self, size, stdscr): def on_info_win_size_changed(self, size, stdscr):
return return
def just_before_refresh(self):
return
class MucTab(Tab): class MucTab(Tab):
""" """
The tab containing a multi-user-chat room. The tab containing a multi-user-chat room.
...@@ -276,6 +289,7 @@ class MucTab(Tab): ...@@ -276,6 +289,7 @@ class MucTab(Tab):
def on_gain_focus(self): def on_gain_focus(self):
self._room.set_color_state(theme.COLOR_TAB_CURRENT) self._room.set_color_state(theme.COLOR_TAB_CURRENT)
curses.curs_set(1)
def on_scroll_up(self): def on_scroll_up(self):
self._room.scroll_up(self.text_win.height-1) self._room.scroll_up(self.text_win.height-1)
...@@ -290,6 +304,9 @@ class MucTab(Tab): ...@@ -290,6 +304,9 @@ class MucTab(Tab):
self.info_header.resize(1, (self.width//10)*9, self.height-3-self.info_win_size, 0, stdscr, self.visible) self.info_header.resize(1, (self.width//10)*9, self.height-3-self.info_win_size, 0, stdscr, self.visible)
self.info_win.resize(self.info_win_size, (self.width//10)*9, self.height-2-self.info_win_size, 0, stdscr, self.visible) self.info_win.resize(self.info_win_size, (self.width//10)*9, self.height-2-self.info_win_size, 0, stdscr, self.visible)
def just_before_refresh(self):
self.input.move_cursor_to_pos()
class PrivateTab(Tab): class PrivateTab(Tab):
""" """
The tab containg a private conversation (someone from a MUC) The tab containg a private conversation (someone from a MUC)
...@@ -341,6 +358,7 @@ class PrivateTab(Tab): ...@@ -341,6 +358,7 @@ class PrivateTab(Tab):
def on_gain_focus(self): def on_gain_focus(self):
self._room.set_color_state(theme.COLOR_TAB_CURRENT) self._room.set_color_state(theme.COLOR_TAB_CURRENT)
curses.curs_set(1)
def on_scroll_up(self): def on_scroll_up(self):
self._room.scroll_up(self.text_win.height-1) self._room.scroll_up(self.text_win.height-1)
...@@ -357,6 +375,9 @@ class PrivateTab(Tab): ...@@ -357,6 +375,9 @@ class PrivateTab(Tab):
def get_room(self): def get_room(self):
return self._room return self._room
def just_before_refresh(self):
return
class RosterInfoTab(Tab): class RosterInfoTab(Tab):
""" """
A tab, splitted in two, containing the roster and infos A tab, splitted in two, containing the roster and infos
...@@ -386,9 +407,9 @@ class RosterInfoTab(Tab): ...@@ -386,9 +407,9 @@ class RosterInfoTab(Tab):
self.input.resize(1, self.width, self.height-1, 0, stdscr, self.visible) self.input.resize(1, self.width, self.height-1, 0, stdscr, self.visible)
def refresh(self, tabs, informations, roster): def refresh(self, tabs, informations, roster):
self.v_separator.refresh()
self.roster_win.refresh(roster) self.roster_win.refresh(roster)
self.contact_info_win.refresh(self.roster_win.get_selected_row()) self.contact_info_win.refresh(self.roster_win.get_selected_row())
self.v_separator.refresh()
self.info_win.refresh(informations) self.info_win.refresh(informations)
self.tab_win.refresh(tabs, tabs[0]) self.tab_win.refresh(tabs, tabs[0])
self.input.refresh() self.input.refresh()
...@@ -412,6 +433,7 @@ class RosterInfoTab(Tab): ...@@ -412,6 +433,7 @@ class RosterInfoTab(Tab):
def on_gain_focus(self): def on_gain_focus(self):
self._color_state = theme.COLOR_TAB_CURRENT self._color_state = theme.COLOR_TAB_CURRENT
curses.curs_set(0)
def add_message(self): def add_message(self):
return False return False
...@@ -429,6 +451,9 @@ class RosterInfoTab(Tab): ...@@ -429,6 +451,9 @@ class RosterInfoTab(Tab):
selected_row = self.roster_win.get_selected_row() selected_row = self.roster_win.get_selected_row()
return selected_row return selected_row
def just_before_refresh(self):
return
class ConversationTab(Tab): class ConversationTab(Tab):
""" """
The tab containg a normal conversation (someone from our roster) The tab containg a normal conversation (someone from our roster)
...@@ -457,6 +482,7 @@ class ConversationTab(Tab): ...@@ -457,6 +482,7 @@ class ConversationTab(Tab):
self.info_win.refresh(informations) self.info_win.refresh(informations)
self.tab_win.refresh(tabs, tabs[0]) self.tab_win.refresh(tabs, tabs[0])
self.input.refresh() self.input.refresh()
curses.curs_set(1)
def get_color_state(self): def get_color_state(self):
if self._room.color_state == theme.COLOR_TAB_NORMAL or\ if self._room.color_state == theme.COLOR_TAB_NORMAL or\
...@@ -495,3 +521,6 @@ class ConversationTab(Tab): ...@@ -495,3 +521,6 @@ class ConversationTab(Tab):
def get_room(self): def get_room(self):
return self._room return self._room
def just_before_refresh(self):
return
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment