Skip to content

Commit

Permalink
files: Fix scrolling on creating new folder
Browse files Browse the repository at this point in the history
  • Loading branch information
tchx84 committed Aug 8, 2023
1 parent a9f4430 commit a3138e9
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 75 deletions.
46 changes: 34 additions & 12 deletions src/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@


@Gtk.Template(resource_path="/dev/tchx84/Portfolio/files.ui")
class PortfolioFiles(Gtk.TreeView):
class PortfolioFiles(Gtk.ScrolledWindow):
__gtype_name__ = "PortfolioFiles"

__gsignals__ = {
Expand All @@ -38,8 +38,10 @@ class PortfolioFiles(Gtk.TreeView):
"path-rename-finished": (GObject.SignalFlags.RUN_LAST, None, ()),
"path-rename-failed": (GObject.SignalFlags.RUN_LAST, None, (str,)),
"path-added-failed": (GObject.SignalFlags.RUN_LAST, None, ()),
"path-adjustment-changed": (GObject.SignalFlags.RUN_LAST, None, (bool,)),
}

treeview = Gtk.Template.Child()
name_column = Gtk.Template.Child()
name_cell = Gtk.Template.Child()
sorted = Gtk.Template.Child()
Expand All @@ -64,21 +66,25 @@ def _setup(self):
self._last_clicked = None
self._dont_activate = False
self._force_select = False
self._last_vscroll_value = None
self._filter = ""
self._sort_order = PortfolioSettings.ALPHABETICAL_ORDER

self.filtered.set_visible_func(self._filter_func, data=None)
self.sorted.set_default_sort_func(self._sort, None)
self.selection.connect("changed", self._on_selection_changed)
self.selection.set_select_function(self._on_select)
self.connect("row-activated", self._on_row_activated)
self.connect("button-press-event", self._on_clicked)
self.treeview.connect("row-activated", self._on_row_activated)
self.treeview.connect("button-press-event", self._on_clicked)

self.name_cell.connect("editing-started", self._on_rename_started)
self.name_cell.connect("edited", self._on_rename_updated)
self.name_cell.connect("editing-canceled", self._on_rename_finished)

self.gesture = Gtk.GestureLongPress.new(self)
self._adjustment = self.get_vadjustment()
self._adjustment.connect("value-changed", self._on_adjustment_changed)

self.gesture = Gtk.GestureLongPress.new(self.treeview)
self.gesture.connect("pressed", self._on_long_pressed)

@property
Expand Down Expand Up @@ -163,6 +169,22 @@ def _sort(self, model, row1, row2, data=None):
else:
return self._sort_by_last_modified(path1, path2)

def _on_adjustment_changed(self, adjustment):
alloc = self.get_allocation()
reveal = self._adjustment.get_value() > (alloc.height / 2) and not self._editing
self.emit("path-adjustment-changed", reveal)

def _wait_and_edit(self):
value = self._adjustment.get_value()

if value == self._last_vscroll_value:
self.rename_selected_path()
self._last_vscroll_value = None
return False

self._last_vscroll_value = value
return True

def _get_path(self, model, treepath):
return model[model.get_iter(treepath)][self.PATH_COLUMN]

Expand Down Expand Up @@ -224,18 +246,18 @@ def _select_row(self, row):
def _go_to_selection(self):
model, treepaths = self.selection.get_selected_rows()
treepath = treepaths[-1]
self.set_cursor_on_cell(
self.treeview.set_cursor_on_cell(
treepath, self.name_column, self.name_cell, False
)
self.scroll_to_cell(treepath, None, False, 0, 0)
self.treeview.scroll_to_cell(treepath, None, False, 0, 0)

def _go_to(self, row):
result, row = self.filtered.convert_child_iter_to_iter(row)
result, row = self.sorted.convert_child_iter_to_iter(row)

treepath = self.sorted.get_path(row)

self.scroll_to_cell(treepath, None, False, 0, 0)
self.treeview.scroll_to_cell(treepath, None, False, 0, 0)
self._clear_to_go_to()

def _select_and_go(self, row, edit=False):
Expand All @@ -254,7 +276,7 @@ def _select_and_go(self, row, edit=False):

def go_to_top(self):
if len(self.sorted) >= 1:
self.scroll_to_cell(0, None, True, 0, 0)
self.treeview.scroll_to_cell(0, None, True, 0, 0)

def _on_row_activated(self, treeview, treepath, treecolumn, data=None):
if self._dont_activate is True:
Expand All @@ -265,7 +287,7 @@ def _on_row_activated(self, treeview, treepath, treecolumn, data=None):
self.emit("path-activated", path)

def _on_clicked(self, treeview, event):
result = self.get_path_at_pos(event.x, event.y)
result = self.treeview.get_path_at_pos(event.x, event.y)
if result is None:
return
treepath, column, x, y = result
Expand Down Expand Up @@ -316,7 +338,7 @@ def rename_selected_path(self):
self.name_cell.props.editable = True
model, treepaths = self.selection.get_selected_rows()
treepath = treepaths[-1]
self.set_cursor_on_cell(
self.treeview.set_cursor_on_cell(
treepath, self.name_column, self.name_cell, True
)

Expand All @@ -336,7 +358,7 @@ def _on_long_pressed(self, gesture, x, y):
return

self.switch_to_selection_mode()
path = self.get_path_at_pos(x, y)
path = self.treeview.get_path_at_pos(x, y)

if path is None:
self.switch_to_navigation_mode()
Expand All @@ -351,7 +373,7 @@ def _on_long_pressed(self, gesture, x, y):

def _update_treeview(self):
sensitive = not self._busy
self.props.sensitive = sensitive
self.treeview.props.sensitive = sensitive

def update_scrolling(self):
if self._to_select_row is not None:
Expand Down
75 changes: 41 additions & 34 deletions src/files.ui
Original file line number Diff line number Diff line change
Expand Up @@ -18,48 +18,55 @@
<object class="GtkTreeModelSort" id="sorted">
<property name="model">filtered</property>
</object>
<template class="PortfolioFiles" parent="GtkTreeView">
<template class="PortfolioFiles" parent="GtkScrolledWindow">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="model">sorted</property>
<property name="headers-visible">False</property>
<property name="headers-clickable">False</property>
<property name="enable-search">False</property>
<property name="search-column">1</property>
<property name="fixed-height-mode">True</property>
<property name="show-expanders">False</property>
<property name="activate-on-single-click">True</property>
<child internal-child="selection">
<object class="GtkTreeSelection" id="selection"/>
</child>
<property name="shadow-type">in</property>
<child>
<object class="GtkTreeViewColumn">
<property name="sizing">fixed</property>
<property name="title">icon</property>
<object class="GtkTreeView" id="treeview">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="model">sorted</property>
<property name="headers-visible">False</property>
<property name="headers-clickable">False</property>
<property name="enable-search">False</property>
<property name="search-column">1</property>
<property name="fixed-height-mode">True</property>
<property name="show-expanders">False</property>
<property name="activate-on-single-click">True</property>
<child internal-child="selection">
<object class="GtkTreeSelection" id="selection"/>
</child>
<child>
<object class="GtkCellRendererPixbuf" id="icon">
<property name="width">50</property>
<property name="height">50</property>
<property name="stock_size">5</property>
<object class="GtkTreeViewColumn">
<property name="sizing">fixed</property>
<property name="title">icon</property>
<child>
<object class="GtkCellRendererPixbuf" id="icon">
<property name="width">50</property>
<property name="height">50</property>
<property name="stock_size">5</property>
</object>
<attributes>
<attribute name="gicon">0</attribute>
</attributes>
</child>
</object>
<attributes>
<attribute name="gicon">0</attribute>
</attributes>
</child>
</object>
</child>
<child>
<object class="GtkTreeViewColumn" id="name_column">
<property name="sizing">fixed</property>
<property name="title">name</property>
<property name="expand">True</property>
<child>
<object class="GtkCellRendererText" id="name_cell">
<property name="ellipsize">end</property>
<object class="GtkTreeViewColumn" id="name_column">
<property name="sizing">fixed</property>
<property name="title">name</property>
<property name="expand">True</property>
<child>
<object class="GtkCellRendererText" id="name_cell">
<property name="ellipsize">end</property>
</object>
<attributes>
<attribute name="text">1</attribute>
</attributes>
</child>
</object>
<attributes>
<attribute name="text">1</attribute>
</attributes>
</child>
</object>
</child>
Expand Down
33 changes: 6 additions & 27 deletions src/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

import os

from pathlib import Path

from .translation import gettext as _

from gi.repository import Gtk, GLib, Gio, Handy
Expand Down Expand Up @@ -110,7 +108,7 @@ class PortfolioWindow(Handy.ApplicationWindow):
menu_popover = Gtk.Template.Child()
menu_button = Gtk.Template.Child()
home_menu_button = Gtk.Template.Child()
content_scroll = Gtk.Template.Child()
content_inner_box = Gtk.Template.Child()
go_top_revealer = Gtk.Template.Child()
properties_box = Gtk.Template.Child()
properties_inner_box = Gtk.Template.Child()
Expand Down Expand Up @@ -139,7 +137,6 @@ def _setup(self):
self._busy = False
self._to_copy = []
self._to_cut = []
self._last_vscroll_value = None
self._force_go_home = False
self._history = []
self._index = -1
Expand Down Expand Up @@ -181,10 +178,8 @@ def _setup(self):
self._files.connect("path-rename-finished", self._on_path_rename_finished)
self._files.connect("path-rename-failed", self._on_path_rename_failed)
self._files.connect("path-added-failed", self._on_path_added_failed)

self.content_scroll.add(self._files)
self._adjustment = self.content_scroll.get_vadjustment()
self._adjustment.connect("value-changed", self._update_go_top_button)
self._files.connect("path-adjustment-changed", self._on_path_adjustment_changed)
self.content_inner_box.pack_start(self._files, True, True, 0)

self.search.connect("toggled", self._on_search_toggled)
self.search_entry.connect("search-changed", self._on_search_changed)
Expand Down Expand Up @@ -225,17 +220,6 @@ def _setup_settings(self):
else:
self.last_modified_button.props.active = True

def _wait_and_edit(self):
value = self._adjustment.get_value()

if value == self._last_vscroll_value:
self._on_rename_clicked(None)
self._last_vscroll_value = None
return False

self._last_vscroll_value = value
return True

def _populate(self, directory):
self._files.switch_to_navigation_mode()

Expand Down Expand Up @@ -381,7 +365,6 @@ def _update_all(self):
self._update_action_stack()
self._update_tools_stack()
self._update_menu()
self._update_go_top_button()

def _update_search(self):
# XXX PortfolioFiles
Expand Down Expand Up @@ -493,12 +476,6 @@ def _update_filter(self):
def _update_menu(self):
self.menu_box.props.sensitive = not self._busy

def _update_go_top_button(self, *args):
# XXX PortfolioFiles
alloc = self.get_allocation()
reveal = self._adjustment.get_value() > (alloc.height / 2) and not self._files.editing
self.go_top_revealer.props.reveal_child = reveal

def _reset_search(self):
self.search.set_active(False)
self.search_entry.set_text("")
Expand All @@ -517,7 +494,6 @@ def _on_path_rename_started(self, files):
self._update_search()
self._update_selection()
self._update_selection_tools()
self._update_go_top_button()

def _on_path_rename_finished(self, files):
self._update_all()
Expand All @@ -543,6 +519,9 @@ def _on_path_added_failed(self, files):
None,
)

def _on_path_adjustment_changed(self, files, reveal):
self.go_top_revealer.props.reveal_child = reveal

def _on_open_started(self, worker):
self._busy = True
self.loading.update(_("Opening"), 0.0, "", "")
Expand Down
4 changes: 2 additions & 2 deletions src/window.ui
Original file line number Diff line number Diff line change
Expand Up @@ -456,10 +456,10 @@
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkScrolledWindow" id="content_scroll">
<object class="GtkBox" id="content_inner_box">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="shadow-type">in</property>
<property name="orientation">vertical</property>
<child>
<placeholder/>
</child>
Expand Down

0 comments on commit a3138e9

Please sign in to comment.