forked from sugarlabs/sugar-toolkit-gtk3
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Detect scrolling in treviews and optimize drawing
Detecting if the treeview is scrolling we can improve the performance of the CellRendererIcon by avoiding unneeded calculations. The example scrollingdetector.py shows how this can be used. The changes are backward compatible. Signed-off-by: Manuel Quiñones <manuel.por.aca@gmail.com> Signed-off-by: Gonzalo Odiard <godiard@sugarlabs.org>
- Loading branch information
Showing
4 changed files
with
201 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import os | ||
import time | ||
|
||
from gi.repository import Gtk | ||
|
||
from sugar3.graphics import style | ||
from sugar3.graphics.icon import CellRendererIcon | ||
from sugar3.graphics.xocolor import XoColor | ||
from sugar3.graphics.scrollingdetector import ScrollingDetector | ||
import common | ||
|
||
|
||
test = common.Test() | ||
test.show() | ||
|
||
model = Gtk.ListStore(str) | ||
|
||
data_dir = os.getenv('GTK_DATA_PREFIX', '/usr/') | ||
|
||
iconlist = os.listdir(os.path.join(data_dir, | ||
'share/icons/sugar/scalable/actions/')) | ||
print "Displaying %s icons" % len(iconlist) | ||
for icon in iconlist: | ||
icon = os.path.basename(icon) | ||
icon = icon[:icon.find('.')] | ||
model.append([icon]) | ||
|
||
scrolled = Gtk.ScrolledWindow() | ||
scrolled.set_size_request(800, 800) | ||
treeview = Gtk.TreeView() | ||
|
||
treeview.set_model(model) | ||
scrolled.add(treeview) | ||
test.pack_start(scrolled, True, True, 0) | ||
test.show_all() | ||
|
||
col = Gtk.TreeViewColumn() | ||
treeview.append_column(col) | ||
|
||
xo_color = XoColor('#FF0000,#00FF00') | ||
cell_icon = CellRendererIcon(treeview) | ||
cell_icon.props.width = style.GRID_CELL_SIZE | ||
cell_icon.props.height = style.GRID_CELL_SIZE | ||
cell_icon.props.size = style.STANDARD_ICON_SIZE | ||
cell_icon.props.xo_color = xo_color | ||
|
||
col.pack_start(cell_icon, expand=False) | ||
col.add_attribute(cell_icon, 'icon-name', 0) | ||
cell_text = Gtk.CellRendererText() | ||
col.pack_start(cell_text, expand=True) | ||
col.add_attribute(cell_text, 'text', 0) | ||
|
||
detector = ScrollingDetector(scrolled) | ||
detector.connect_treeview(treeview) | ||
|
||
if __name__ == '__main__': | ||
time_ini = time.time() | ||
common.main(test) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# Copyright (C) 2014, Sugarlabs | ||
# | ||
# This library is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU Lesser General Public | ||
# License as published by the Free Software Foundation; either | ||
# version 2 of the License, or (at your option) any later version. | ||
# | ||
# This library 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 | ||
# Lesser General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Lesser General Public | ||
# License along with this library; if not, write to the | ||
# Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
# Boston, MA 02111-1307, USA. | ||
|
||
from gi.repository import GObject | ||
from gi.repository import GLib | ||
|
||
from sugar3.graphics.icon import CellRendererIcon | ||
|
||
|
||
class ScrollingDetector(GObject.GObject): | ||
""" | ||
""" | ||
|
||
scroll_start_signal = GObject.Signal('scroll-start') | ||
scroll_end_signal = GObject.Signal('scroll-end') | ||
|
||
def __init__(self, scrolled_window, timeout=100): | ||
self._scrolled_window = scrolled_window | ||
self._timeout = timeout | ||
self.is_scrolling = False | ||
self._prev_value = 0 | ||
|
||
self.connect_scrolled_window() | ||
GObject.GObject.__init__(self) | ||
|
||
def connect_scrolled_window(self): | ||
adj = self._scrolled_window.get_vadjustment() | ||
adj.connect('value-changed', self._value_changed_cb) | ||
|
||
def _check_scroll_cb(self, adj): | ||
if (adj.props.value == self._prev_value): | ||
self.is_scrolling = False | ||
self.scroll_end_signal.emit() | ||
return False | ||
|
||
self._prev_value = adj.props.value | ||
return True | ||
|
||
def _value_changed_cb(self, adj): | ||
if (self.is_scrolling): | ||
return | ||
|
||
self.is_scrolling = True | ||
self.scroll_start_signal.emit() | ||
self._prev_value = adj.props.value | ||
GLib.timeout_add(self._timeout, self._check_scroll_cb, adj) | ||
|
||
|
||
def connect_treeview(self, treeview): | ||
""" | ||
treeview -- Gtk.TreeView: a TreeView with CellRendererIcons already | ||
added. | ||
""" | ||
for column in treeview.get_columns(): | ||
for cell_renderer in column.get_cells(): | ||
if isinstance(cell_renderer, CellRendererIcon): | ||
cell_renderer.connect_to_scroller(self) |