urwidtrees icon indicating copy to clipboard operation
urwidtrees copied to clipboard

Should toggle collapse via enter/space

Open wernight opened this issue 10 years ago • 3 comments

Currently + and - are used to expand/collapse. Those aren't the intuitive keys. I'd suggest to support space and/or enter if tree element is focused.

wernight avatar Jan 29 '16 12:01 wernight

i think the easiest would be to use constants treewidge.KEYS_EXPAND etc., so people could overide them.

pazz avatar Jan 29 '16 14:01 pazz

Yes that'd totally do.

On Fri, 29 Jan 2016 at 15:53 Patrick Totzke [email protected] wrote:

i think the easiest would be to use constants treewidge.KEYS_EXPAND etc., so people could overide them.

— Reply to this email directly or view it on GitHub https://github.com/pazz/urwidtrees/issues/26#issuecomment-176794076.

wernight avatar Feb 01 '16 11:02 wernight

To be even more usefull, IMHO, it would be better to have a unique key to expand/collapse some tree (space bar is the most logical for me), and furthermore you could and a second event with a double click mouse, so you can use the urwid app in a smartphone. Yes, I do that succesfully. What I have done is derive from the class TreeBox another class to catch events:


class IBInfoTreeBox(TreeBox):

def __init__(self, tree, focus = None):
    self.lastClickTime = self.currentTime() 
    self.__super.__init__(tree, focus)

def keypress(self, size, key):
    """allow subclasses to intercept keystrokes"""
    key = self.__super.keypress(size, key)
    if key:
        key = self.unhandledKeys(size, key)
    return key

def unhandledKeys(self, size, key):
    """
    Override this method to intercept keystrokes in subclasses.
    Default behavior: Toggle flagged on space, ignore other keys.
    """
    if key == " ":
        self.toggleCollapseExpand()
    else:
        return key

def currentTime(self):
    return int(round(time.time() * 1000))

def mouse_event(self, size, event, button, col, row, focus):
    self.__super.mouse_event(size, event, button, col, row, focus)
    if event == "mouse press":
        if button == 1: # left button click
            if (self.currentTime() - self.lastClickTime) < 500: #Double click 
                self.toggleCollapseExpand()
            self.lastClickTime = self.currentTime() 
    return False

def toggleCollapseExpand(self):
    w, focuspos = self.get_focus()
    self._tree.toggle_collapsed(focuspos)
    self._walker.clear_cache()
    self.refresh()

ramiro-alba avatar Apr 21 '16 16:04 ramiro-alba