Skip to content

Commit

Permalink
Remove some hacks
Browse files Browse the repository at this point in the history
  • Loading branch information
kra-mo committed Dec 25, 2023
1 parent 62ec4c2 commit 23fe1e9
Show file tree
Hide file tree
Showing 16 changed files with 193 additions and 275 deletions.
2 changes: 1 addition & 1 deletion hyperplane/file_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#
# SPDX-License-Identifier: GPL-3.0-or-later

"""Miscallenious variables for determining file properties."""
"""Miscellaneous variables for determining file properties."""

DOT_IS_NOT_EXTENSION = {
"application/x-sharedlib",
Expand Down
30 changes: 6 additions & 24 deletions hyperplane/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

from hyperplane import shared
from hyperplane.file_properties import DOT_IS_NOT_EXTENSION
from hyperplane.utils.get_color_for_content_type import get_color_for_content_type
from hyperplane.utils.symbolics import get_color_for_symbolic, get_symbolic
from hyperplane.utils.thumbnail import generate_thumbnail


Expand Down Expand Up @@ -98,17 +98,10 @@ def bind(self) -> None:
self.file_info = self.item.get_item()

self.gfile = self.file_info.get_attribute_object("standard::file")
gicon = self.file_info.get_symbolic_icon()

for icon_name in gicon.get_names():
if icon_name.endswith("-symbolic") and icon_name in shared.icon_names:
break
else:
gicon = Gio.Icon.new_for_string("text-x-generic-symbolic")

self.gicon = gicon
self.gicon = get_symbolic(self.file_info.get_symbolic_icon())
self.content_type = self.file_info.get_content_type()
self.color = get_color_for_content_type(self.content_type, self.gicon)
self.color = get_color_for_symbolic(self.content_type, self.gicon)
self.edit_name = self.file_info.get_edit_name()

self.is_dir = self.content_type == "inode/directory"
Expand Down Expand Up @@ -222,17 +215,9 @@ def next_files_cb(
index += 1
files.next_files_async(1, GLib.PRIORITY_DEFAULT, None, next_files_cb, index)

if gicon := file_info.get_symbolic_icon():
for icon_name in gicon.get_names():
if (
icon_name.endswith("-symbolic")
and icon_name in shared.icon_names
):
break
else:
gicon = Gio.Icon.new_for_string("text-x-generic-symbolic")
gicon = get_symbolic(file_info.get_symbolic_icon())

idle_add(thumbnail.get_child().set_from_gicon, gicon)
idle_add(thumbnail.get_child().set_from_gicon, gicon)

if content_type == "inode/directory":
idle_add(
Expand All @@ -252,7 +237,7 @@ def next_files_cb(
self.dir_thumb_init_classes + ["white-background"],
)

color = get_color_for_content_type(content_type, gicon)
color = get_color_for_symbolic(content_type, gicon)

idle_add(
thumbnail.get_child().set_css_classes,
Expand All @@ -267,8 +252,6 @@ def next_files_cb(
)
return

# HACK: I don't know how else to get a GFile for file_info.
# Maybe this is the proper way?
child_gfile = gfile.get_child(file_info.get_name())

GLib.Thread.new(
Expand Down Expand Up @@ -449,7 +432,6 @@ def __right_click(self, *_args: Any) -> None:
menu_items.add("trash-restore")
menu_items.add("trash-delete")

# TODO: Ugly
self.page.menu_items = menu_items

def __middle_click(self, *_args: Any) -> None:
Expand Down
7 changes: 1 addition & 6 deletions hyperplane/item_sorter.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,7 @@ def do_compare(

def __ordering_from_cmpfunc(self, cmpfunc_result: int) -> Gtk.Ordering:
"""Converts the result of a `GCompareFunc` like `strcmp()` to a `GtkOrdering` value."""
# HACK: Gtk.Ordering.from_cmpfunc seems to not work
# gi.repository.GLib.GError: g-invoke-error-quark:
# Could not locate gtk_ordering_from_cmpfunc:
# 'gtk_ordering_from_cmpfunc': /usr/lib/x86_64-linux-gnu/libgtk-4.so.1:
# undefined symbol: gtk_ordering_from_cmpfunc (1)

# HACK: https://gitlab.gnome.org/GNOME/gtk/-/issues/6298
if cmpfunc_result > 0:
return Gtk.Ordering.LARGER
if cmpfunc_result < 0:
Expand Down
21 changes: 8 additions & 13 deletions hyperplane/items_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
move,
restore,
rm,
trash_rm,
validate_name,
)
from hyperplane.utils.iterplane import iterplane
Expand Down Expand Up @@ -80,7 +79,7 @@ def __init__(
self.tags = tags

if self.gfile:
if self.gfile.get_path() == str(shared.home):
if self.gfile.get_path() == str(shared.home_path):
self.set_title(_("Home"))
else:
self.set_title(get_gfile_display_name(self.gfile))
Expand Down Expand Up @@ -190,8 +189,6 @@ def reload(self) -> None:
self.dir_list.set_monitored(True)
return

# TODO: This works, but it would be best if instead of manually refreshing,
# tags would be monitored for changes too. I don't know how I would do that though.
if isinstance(self.dir_list, Gtk.FlattenListModel):
self.dir_list = self.__get_list(tags=self.tags)
self.filter_list.set_model(self.dir_list)
Expand Down Expand Up @@ -360,7 +357,6 @@ def __unbind(self, _factory: Gtk.SignalListItemFactory, item: Gtk.ListItem) -> N
item.get_child().unbind()

def __tags_changed(self, _obj: GObject.Object, change: Gtk.FilterChange) -> None:
# TODO: I could do less/more strict with adding/removing tags separately
self.item_filter.changed(change)

def __toggle_hidden(self, *_args: Any) -> None:
Expand All @@ -378,8 +374,7 @@ def activate(self, _grid_view: Gtk.GridView, pos: int) -> None:
gfile = file_info.get_attribute_object("standard::file")

if file_info.get_content_type() == "inode/directory":
# TODO: get_parent is ugly, but probably less ugly than the alternative
self.get_parent().get_parent().new_page(gfile)
self.get_root().new_page(gfile)
return

if not (
Expand Down Expand Up @@ -460,7 +455,7 @@ def __drop_file(
dst = Gio.File.new_for_path(
str(
Path(
shared.home,
shared.home_path,
*tags,
)
)
Expand Down Expand Up @@ -493,7 +488,7 @@ def __drop_texture(
dst = Gio.File.new_for_path(
str(
Path(
shared.home,
shared.home_path,
*tags,
)
)
Expand Down Expand Up @@ -532,7 +527,7 @@ def __drop_text(
dst = Gio.File.new_for_path(
str(
Path(
shared.home,
shared.home_path,
*tags,
)
)
Expand Down Expand Up @@ -633,7 +628,7 @@ def __open_with(self, *_args: Any) -> None:
def __new_folder(self, *_args: Any) -> None:
if self.tags:
tags = tuple(tag for tag in shared.tags if tag in self.tags)
gfile = Gio.File.new_for_path(str(Path(shared.home, *tags)))
gfile = Gio.File.new_for_path(str(Path(shared.home_path, *tags)))
else:
gfile = self.gfile

Expand Down Expand Up @@ -744,7 +739,7 @@ def __paste(self, *_args: Any) -> None:
dst = Gio.File.new_for_path(
str(
Path(
shared.home,
shared.home_path,
*tags,
)
)
Expand Down Expand Up @@ -894,7 +889,7 @@ def __trash_delete(self, *args: Any) -> None:

def delete():
for gfile in gfiles:
trash_rm(gfile)
rm(gfile)

match len(gfiles):
case 0:
Expand Down
4 changes: 2 additions & 2 deletions hyperplane/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ def __init__(self) -> None:
logging_config()

# Create home
shared.home.mkdir(parents=True, exist_ok=True)
(shared.home / ".hyperplane").touch(exist_ok=True)
shared.home_path.mkdir(parents=True, exist_ok=True)
(shared.home_path / ".hyperplane").touch(exist_ok=True)

shared.app = self

Expand Down
29 changes: 1 addition & 28 deletions hyperplane/path_bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"""The path bar in a HypWindow."""
from typing import Optional

from gi.repository import Gio, GLib, GObject, Gtk
from gi.repository import GLib, Gtk

from hyperplane import shared
from hyperplane.path_segment import HypPathSegment
Expand All @@ -42,7 +42,6 @@ class HypPathBar(Gtk.ScrolledWindow):
def __init__(self, **kwargs) -> None:
super().__init__(**kwargs)
self.segments = []
self.connections = {}
self.separators = {}
self.tags = False

Expand Down Expand Up @@ -70,9 +69,6 @@ def remove(self, n: int) -> None:
)
self.separators.pop(child)

for connection in self.connections.pop(child):
child.disconnect(connection)

if self.tags:
return

Expand Down Expand Up @@ -117,16 +113,6 @@ def append(

self.separators[segment] = sep
self.segments.append(segment)
self.connections[segment] = {
segment.connect(
"open-gfile",
lambda *args: self.emit("open-gfile", *args[1:]),
),
segment.connect(
"open-tag",
lambda *args: self.emit("open-tag", *args[1:]),
),
}

last_segment = self.segments[-1]

Expand All @@ -151,23 +137,10 @@ def purge(self) -> None:
while child := self.segments_box.get_first_child():
self.segments_box.remove(child)

for segment, connections in self.connections.items():
for connection in connections:
segment.disconnect(connection)

self.segments = []
self.separators = {}
self.connections = {}

def __remove_child(self, parent: Gtk.Box, child: Gtk.Widget) -> None:
# This is so GTK doesn't freak out when the child isn't in the parent anymore
if child.get_parent == parent:
parent.remove(child)

@GObject.Signal(name="open-tag")
def open_tag(self, _tag: str) -> None:
"""Signals to the main window that it should open `tag` as the only tag."""

@GObject.Signal(name="open-gfile")
def open_gfile(self, _gfile: Gio.File) -> None:
"""Signals to the main window that it should open `gfile`."""
24 changes: 13 additions & 11 deletions hyperplane/path_segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"""A segment in a HypPathBar."""
from typing import Any, Optional

from gi.repository import Adw, Gio, GObject, Gtk
from gi.repository import Adw, Gdk, Gio, GObject, Gtk

from hyperplane import shared

Expand Down Expand Up @@ -50,26 +50,28 @@ def __init__(
self.uri = uri
self.tag = tag

middle_click = Gtk.GestureClick(button=Gdk.BUTTON_MIDDLE)
middle_click.connect(
"pressed",
lambda *_: self.get_root().new_tab(
Gio.File.new_for_uri(self.uri) if self.uri else None,
tags=[self.tag] if self.tag else None,
),
)
self.add_controller(middle_click)

self.button.connect("clicked", self.__navigate)

def __navigate(self, *_args: Any) -> None:
if self.tag:
self.emit("open-tag", self.tag)
self.get_root().new_page(tags=[self.tag])
return

if self.uri:
if self.active: # pylint: disable=using-constant-test
return

self.emit("open-gfile", Gio.File.new_for_uri(self.uri))

@GObject.Signal(name="open-tag")
def open_tag(self, _tag: str) -> None:
"""Signals to the path bar that `tag` should be opened as the only tag."""

@GObject.Signal(name="open-gfile")
def open_gfile(self, _gfile: Gio.File) -> None:
"""Signals to the path bar that `gfile` should be opened."""
self.get_root().new_page(Gio.File.new_for_uri(self.uri))

@GObject.Property(type=bool, default=True)
def active(self) -> bool:
Expand Down
20 changes: 5 additions & 15 deletions hyperplane/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

from hyperplane import shared
from hyperplane.utils.files import clear_recent_files, empty_trash, get_gfile_path
from hyperplane.utils.get_color_for_content_type import get_color_for_content_type
from hyperplane.utils.symbolics import get_color_for_symbolic, get_symbolic
from hyperplane.utils.tags import path_represents_tags


Expand Down Expand Up @@ -117,20 +117,13 @@ def __init__(self, gfile: Gio.File, **kwargs) -> None:
)
)

elif gicon:
for icon_name in gicon.get_names():
if (
icon_name.endswith("-symbolic")
and icon_name in shared.icon_names
):
break
else:
gicon = Gio.Icon.new_for_string("text-x-generic-symbolic")
else:
gicon = get_symbolic(gicon)

icon_group.add(image := Gtk.Image(gicon=gicon, halign=Gtk.Align.CENTER))
image.set_icon_size(Gtk.IconSize.LARGE)

color = get_color_for_content_type(content_type, gicon)
color = get_color_for_symbolic(content_type, gicon)

image.add_css_class(color + "-icon")
image.add_css_class(color + "-background")
Expand Down Expand Up @@ -329,10 +322,7 @@ def clear(*_args: Any) -> None:
tags_row := Adw.ActionRow(
title=_("Categories"),
subtitle=", ".join(
Gio.File.new_for_path(str(shared.home))
.get_relative_path(parent)
.strip("/")
.split("/")
shared.home.get_relative_path(parent).strip("/").split("/")
),
subtitle_selectable=True,
)
Expand Down
10 changes: 3 additions & 7 deletions hyperplane/shared.py.in
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,14 @@ PROFILE = "@PROFILE@"
schema = Gio.Settings.new(APP_ID)
state_schema = Gio.Settings.new(APP_ID + ".State")

data_dir = Path(GLib.get_user_data_dir())
config_dir = Path(GLib.get_user_config_dir())
cache_dir = Path(GLib.get_user_cache_dir())
home_path = Path(GLib.get_user_data_dir(), "hyperplane", "home")
home = Gio.File.new_for_path(str(home_path))

home = data_dir / "hyperplane" / "home"
show_hidden = state_schema.get_boolean("show-hidden")

tags = []

if (path := home / ".hyperplane").is_file():
if (path := home_path / ".hyperplane").is_file():
tags = list(
tag for tag in path.read_text(encoding="utf-8").strip().split("\n") if tag
)
Expand Down Expand Up @@ -80,8 +78,6 @@ if (
else:
recent_manager = Gtk.RecentManager.get_default()

icon_names = Gtk.IconTheme.get_for_display(Gdk.Display.get_default()).get_icon_names()

cut_widgets = set()


Expand Down
Loading

0 comments on commit 23fe1e9

Please sign in to comment.