# Copyright (C) 2009, 2010 Roman Zimbelmann # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . """The BrowserView manages a set of BrowserColumns.""" import curses from ranger.ext.signal_dispatcher import Signal from . import Widget from .browsercolumn import BrowserColumn from .pager import Pager from ..displayable import DisplayableContainer class BrowserView(Widget, DisplayableContainer): ratios = None preview = True is_collapsed = False draw_bookmarks = False stretch_ratios = None need_clear = False def __init__(self, win, ratios, preview = True): DisplayableContainer.__init__(self, win) self.preview = preview self.columns = [] self.pager = Pager(self.win, embedded=True) self.pager.visible = False self.add_child(self.pager) self.change_ratios(ratios, resize=False) for option in ('preview_directories', 'preview_files'): self.settings.signal_bind('setopt.' + option, self._request_clear_if_has_borders, weak=True) self.fm.env.signal_bind('move', self.request_clear) self.settings.signal_bind('setopt.column_ratios', self.request_clear) def change_ratios(self, ratios, resize=True): if isinstance(ratios, Signal): ratios = ratios.value for column in self.columns: column.destroy() self.remove_child(column) self.columns = [] ratio_sum = float(sum(ratios)) self.ratios = tuple(x / ratio_sum for x in ratios) if len(self.ratios) >= 2: self.stretch_ratios = self.ratios[:-2] + \ ((self.ratios[-2] + self.ratios[-1] * 0.9), (self.ratios[-1] * 0.1)) offset = 1 - len(ratios) if self.preview: offset += 1 for level in range(len(ratios)): fl = BrowserColumn(self.win, level + offset) self.add_child(fl) self.columns.append(fl) try: self.main_column = self.columns[self.preview and -2 or -1] except IndexError: self.main_column = None else: self.main_column.display_infostring = True self.main_column.main_column = True self.resize(self.y, self.x, self.hei, self.wid) def _request_clear_if_has_borders(self): if self.settings.draw_borders: self.request_clear() def request_clear(self): self.need_clear = True def draw(self): if self.draw_bookmarks: self._draw_bookmarks() else: if self.need_clear: self.win.erase() self.need_redraw = True self.need_clear = False DisplayableContainer.draw(self) if self.settings.draw_borders: self._draw_borders() def finalize(self): if self.pager.visible: try: self.fm.ui.win.move(self.main_column.y, self.main_column.x) except: pass else: try: x = self.main_column.x y = self.main_column.y + self.main_column.target.pointer\ - self.main_column.scroll_begin self.fm.ui.win.move(y, x) except: pass def _draw_bookmarks(self): self.color_reset() self.need_clear = True sorted_bookmarks = sorted(item for item in self.fm.bookmarks \ if self.settings.show_hidden_bookmarks or