<commit_before>#!/usr/bin/env python
# encoding: utf-8
"""Library part of self.vimiv."""

import os
from gi import require_version
require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk
from vimiv.fileactions import is_image, populate
from vimiv.helpers import listdir_wrapper, sizeof_fmt


class Library(object):
    """Library of vimiv.

    Includes the treeview with the library and all actions that apply to it

    Attributes:
        vimiv: The main vimiv class to interact with.
        dir_pos: Dictionary that stores position in directories.
        show_at_start: If True show library at startup.
        default_width: Setting for the default width of the library.
        expand: If True expand the library to window width if no images are
            shown.
        width: Width of the actual library without border width.
        markup: Markup string used to highlight search results.
        show_hidden: If True show hidden files.
        file_check_amount: Amount of files checked in a directory to display
            amount of images in it.
        desktop_start_dir: Directory to start in if launched from desktop.
        files: Files in the library.
        datalist: List containing information on files (formatted name, size,
            mark indicator).
        filesize: Dictionary storing the size of files.
        file_liststore: Gtk.ListStore for the file information to be displayed.
        grid: Gtk.Grid containing the TreeView and the border.
        scrollable_treeview: Gtk.ScrolledWindow in which the TreeView gets
            packed.
        treeview: Gtk.TreeView object with file_liststore as model.
    """

    def __init__(self, vimiv, settings):
        """Create the necessary objects and settings.

        Args:
            vimiv: The main vimiv class to interact with.
            settings: Settings from configfiles to use.
        """
        self.vimiv = vimiv
        library = settings["LIBRARY"]

        # Settings
        self.dir_pos = {}  # Remembers positions in the library browser
        self.show_at_start = library["show_library"]
        self.default_width = library["library_width"]
        self.expand = library["expand_lib"]
        border_width = library["border_width"]
        self.width = self.default_width - border_width
        self.markup = library["markup"]
        self.show_hidden = library["show_hidden"]
        self.file_check_amount = library["file_check_amount"]
        self.desktop_start_dir = library["desktop_start_dir"]

        # Defaults
        self.files = []
        self.datalist = []
        self.filesize = {}
        # Filelist in a liststore model
        self.file_liststore = Gtk.ListStore(int, str, str, str)

        # Grid with treeview and border
        self.grid = Gtk.Grid()
        # A simple border
        if border_width:
            border = Gtk.Separator()
            border.set_size_request(border_width, 1)
            self.grid.attach(border, 1, 0, 1, 1)
        # Pack everything
        self.scrollable_treeview = Gtk.ScrolledWindow()
        self.scrollable_treeview.set_vexpand(True)
        self.scrollable_treeview.set_size_request(self.width, 10)
        self.grid.attach(self.scrollable_treeview, 0, 0, 1, 1)
        # Treeview
        self.treeview = Gtk.TreeView()
        self.scrollable_treeview.add(self.treeview)
        self.treeview.set_enable_search(False)
        # Select file when row activated
        self.treeview.connect("row-activated", self.file_select, True)
        # Handle key events
        self.treeview.add_events(Gdk.EventMask.KEY_PRESS_MASK)
        self.treeview.connect("key_press_event",
                              self.vimiv.keyhandler.run, "LIBRARY")
        # Call the function to create the treeview
        self.update_treeview()
        # Set the hexpand property if requested in the configfile
        if (not self.vimiv.paths or isinstance(self.vimiv.paths, str)) \
                and self.expand:
            self.treeview.set_hexpand(True)

    def toggle(self, update_image=True):
        """Toggle the library.

        Args:
            update_image: If True update the image shown. Always the case except
            for when running toggle after file_select(), as file_select() does
            this by itself.
        """
        if self.grid.is_visible():
            self.remember_pos(os.getcwd(),
                              self.vimiv.get_pos(force_widget="lib"))
            self.grid.hide()
            self.vimiv.image.animation_toggled = False  # Now play Gifs
            self.focus(False)
        else:
            self.grid.show()
            if not self.vimiv.paths:
                # Hide the non existing image and expand if necessary
                self.vimiv.image.vimiv.image.scrolled_win.hide()
                if self.expand:
                    self.scrollable_treeview.set_hexpand(True)
            else:  # Try to focus the current image in the library
                image = self.vimiv.paths[self.vimiv.index]
                image_path = os.path.dirname(image)
                image_name = os.path.basename(image)
                if image_path == os.getcwd():
                    for i, fil in enumerate(self.files):
                        if fil == image_name:
                            self.remember_pos(os.getcwd(), i)
                            break
            # Do not play Gifs with the lib
            self.vimiv.image.animation_toggled = True
            # Stop the slideshow
            if self.vimiv.slideshow.running:
                self.vimiv.slideshow.toggle()
            self.focus(True)
            # Markings and other stuff might have changed
            self.reload(os.getcwd())
        # Resize image and grid if necessary
        if self.vimiv.paths and update_image:
            if self.vimiv.thumbnail.toggled:
                self.vimiv.thumbnail.calculate_columns()
            elif not self.vimiv.image.user_zoomed:
                self.vimiv.image.zoom_to(0)
            else:
                #  Change the toggle state of animation
                self.vimiv.image.update()
<commit_msg>Do not stop playing Gifs in the library<commit_after>#!/usr/bin/env python
# encoding: utf-8
"""Library part of self.vimiv."""

import os
from gi import require_version
require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk
from vimiv.fileactions import is_image, populate
from vimiv.helpers import listdir_wrapper, sizeof_fmt


class Library(object):
    """Library of vimiv.

    Includes the treeview with the library and all actions that apply to it

    Attributes:
        vimiv: The main vimiv class to interact with.
        dir_pos: Dictionary that stores position in directories.
        show_at_start: If True show library at startup.
        default_width: Setting for the default width of the library.
        expand: If True expand the library to window width if no images are
            shown.
        width: Width of the actual library without border width.
        markup: Markup string used to highlight search results.
        show_hidden: If True show hidden files.
        file_check_amount: Amount of files checked in a directory to display
            amount of images in it.
        desktop_start_dir: Directory to start in if launched from desktop.
        files: Files in the library.
        datalist: List containing information on files (formatted name, size,
            mark indicator).
        filesize: Dictionary storing the size of files.
        file_liststore: Gtk.ListStore for the file information to be displayed.
        grid: Gtk.Grid containing the TreeView and the border.
        scrollable_treeview: Gtk.ScrolledWindow in which the TreeView gets
            packed.
        treeview: Gtk.TreeView object with file_liststore as model.
    """

    def __init__(self, vimiv, settings):
        """Create the necessary objects and settings.

        Args:
            vimiv: The main vimiv class to interact with.
            settings: Settings from configfiles to use.
        """
        self.vimiv = vimiv
        library = settings["LIBRARY"]

        # Settings
        self.dir_pos = {}  # Remembers positions in the library browser
        self.show_at_start = library["show_library"]
        self.default_width = library["library_width"]
        self.expand = library["expand_lib"]
        border_width = library["border_width"]
        self.width = self.default_width - border_width
        self.markup = library["markup"]
        self.show_hidden = library["show_hidden"]
        self.file_check_amount = library["file_check_amount"]
        self.desktop_start_dir = library["desktop_start_dir"]

        # Defaults
        self.files = []
        self.datalist = []
        self.filesize = {}
        # Filelist in a liststore model
        self.file_liststore = Gtk.ListStore(int, str, str, str)

        # Grid with treeview and border
        self.grid = Gtk.Grid()
        # A simple border
        if border_width:
            border = Gtk.Separator()
            border.set_size_request(border_width, 1)
            self.grid.attach(border, 1, 0, 1, 1)
        # Pack everything
        self.scrollable_treeview = Gtk.ScrolledWindow()
        self.scrollable_treeview.set_vexpand(True)
        self.scrollable_treeview.set_size_request(self.width, 10)
        self.grid.attach(self.scrollable_treeview, 0, 0, 1, 1)
        # Treeview
        self.treeview = Gtk.TreeView()
        self.scrollable_treeview.add(self.treeview)
        self.treeview.set_enable_search(False)
        # Select file when row activated
        self.treeview.connect("row-activated", self.file_select, True)
        # Handle key events
        self.treeview.add_events(Gdk.EventMask.KEY_PRESS_MASK)
        self.treeview.connect("key_press_event",
                              self.vimiv.keyhandler.run, "LIBRARY")
        # Call the function to create the treeview
        self.update_treeview()
        # Set the hexpand property if requested in the configfile
        if (not self.vimiv.paths or isinstance(self.vimiv.paths, str)) \
                and self.expand:
            self.treeview.set_hexpand(True)

    def toggle(self, update_image=True):
        """Toggle the library.

        Args:
            update_image: If True update the image shown. Always the case except
            for when running toggle after file_select(), as file_select() does
            this by itself.
        """
        if self.grid.is_visible():
            self.remember_pos(os.getcwd(),
                              self.vimiv.get_pos(force_widget="lib"))
            self.grid.hide()
            self.focus(False)
        else:
            self.grid.show()
            if not self.vimiv.paths:
                # Hide the non existing image and expand if necessary
                self.vimiv.image.vimiv.image.scrolled_win.hide()
                if self.expand:
                    self.scrollable_treeview.set_hexpand(True)
            else:  # Try to focus the current image in the library
                image = self.vimiv.paths[self.vimiv.index]
                image_path = os.path.dirname(image)
                image_name = os.path.basename(image)
                if image_path == os.getcwd():
                    for i, fil in enumerate(self.files):
                        if fil == image_name:
                            self.remember_pos(os.getcwd(), i)
                            break
            # Stop the slideshow
            if self.vimiv.slideshow.running:
                self.vimiv.slideshow.toggle()
            self.focus(True)
            # Markings and other stuff might have changed
            self.reload(os.getcwd())
        # Resize image and grid if necessary
        if self.vimiv.paths and update_image:
            if self.vimiv.thumbnail.toggled:
                self.vimiv.thumbnail.calculate_columns()
            elif not self.vimiv.image.user_zoomed:
                self.vimiv.image.zoom_to(0)
            else:
                #  Change the toggle state of animation
                self.vimiv.image.update()

{'fraction_non_alphanumeric': 0.07731618859215299, 'fraction_numerical': 0.002472799208704253, 'mean_word_length': 2.0144099378881988, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7173764', 'n_tokens_mistral': 3402, 'n_tokens_neox': 3255, 'n_words': 961}