Skip to content

Commit

Permalink
Merge pull request #799 from rdbende/gtk4
Browse files Browse the repository at this point in the history
Port to GTK 4
  • Loading branch information
geigi authored Dec 3, 2023
2 parents 29570db + 1e6d0b6 commit 6c27470
Show file tree
Hide file tree
Showing 109 changed files with 2,242 additions and 6,145 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ venv/
*.log
*.prof
*.pyc

.DS_Store
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,8 @@ See [DEVELOPMENT.md](DEVELOPMENT.md) for detailed instructions and developing Co
## Requirements
- `python3`
- `meson >= 0.40.0` as build system
- `gtk3 >= 3.22`
- `libhandy >= 1.0.0`
- `libdazzle >= 3.34.0`
- `gtk4 >= 4.10`
- `libadwaita >= 1.4.0`
- `peewee >= 3.9.6` as object relation mapper
- `mutagen` for meta tag management
- `distro`
Expand Down Expand Up @@ -165,6 +164,7 @@ To the contributors on GitHub:
- paper42
- phpwutz
- rapenne-s
- rdbende
- thibaultamartin
- umeboshi2
- worldofpeace
Expand Down Expand Up @@ -277,9 +277,9 @@ The translators:
To nedrichards for the Flatpak.

## Help me translate cozy!
Cozy is on <a href="https://www.transifex.com/geigi/cozy/"> Transifex</a>, where anyone can contribute and translate. Can't find your language in the list? Let me know!
Cozy is on <a href="https://www.transifex.com/geigi/cozy/">Transifex</a>, where anyone can contribute and translate. Can't find your language in the list? Let me know!

If you like this project, consider supporting me on <a href="https://www.patreon.com/bePatron?u=8147127"> Patreon</a> :)
If you like this project, consider supporting me on <a href="https://www.patreon.com/geigi">Patreon</a> :)

----
[![Maintainability](https://api.codeclimate.com/v1/badges/fde8cbdff23033adaca2/maintainability)](https://codeclimate.com/github/geigi/cozy/maintainability)
35 changes: 2 additions & 33 deletions com.github.geigi.cozy.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"app-id": "com.github.geigi.cozy",
"runtime": "org.gnome.Platform",
"runtime-version": "42",
"runtime-version": "45",
"sdk": "org.gnome.Sdk",
"command": "com.github.geigi.cozy",
"finish-args": [
Expand Down Expand Up @@ -130,37 +130,6 @@
}
]
},
{
"name": "libhandy",
"buildsystem": "meson",
"config-opts": [
"-Dprofiling=false",
"-Dintrospection=enabled",
"-Dgtk_doc=false",
"-Dtests=false",
"-Dexamples=false",
"-Dvapi=false",
"-Dglade_catalog=disabled"
],
"sources": [
{
"type": "git",
"url": "https://gitlab.gnome.org/GNOME/libhandy",
"tag": "1.7.90"
}
]
},
{
"name": "libdazzle",
"buildsystem": "meson",
"sources": [
{
"type": "git",
"url": "https://gitlab.gnome.org/GNOME/libdazzle.git",
"tag": "3.44.0"
}
]
},
{
"name": "cozy",
"buildsystem": "meson",
Expand All @@ -172,4 +141,4 @@
]
}
]
}
}
4 changes: 2 additions & 2 deletions cozy.doap
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<shortdesc xml:lang="en">Listen to audio books</shortdesc>

<programming-language>Python</programming-language>
<platform>GTK 3</platform>
<platform>Libhandy</platform>
<platform>GTK 4</platform>
<platform>Libadwaita</platform>

<maintainer>
<foaf:Person>
Expand Down
9 changes: 2 additions & 7 deletions cozy/app_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from cozy.ui.app_view import AppView
from cozy.ui.book_detail_view import BookDetailView
from cozy.ui.headerbar import Headerbar
from cozy.ui.info_banner import InfoBanner
from cozy.ui.toaster import ToastNotifier
from cozy.ui.library_view import LibraryView
from cozy.ui.main_view import CozyUI
from cozy.ui.media_controller import MediaController
Expand Down Expand Up @@ -105,7 +105,7 @@ def configure_inject(self, binder):
binder.bind_to_constructor(SleepTimerViewModel, lambda: SleepTimerViewModel())
binder.bind_to_constructor(GstPlayer, lambda: GstPlayer())
binder.bind_to_constructor(PowerManager, lambda: PowerManager())
binder.bind_to_constructor(InfoBanner, lambda: InfoBanner())
binder.bind_to_constructor(ToastNotifier, lambda: ToastNotifier())
binder.bind_to_constructor(AppViewModel, lambda: AppViewModel())
binder.bind_to_constructor(SettingsViewModel, lambda: SettingsViewModel())

Expand All @@ -125,9 +125,6 @@ def open_library(self):
self.library_view_model.open_library()
self.app_view_model.view = View.LIBRARY_FILTER

def navigate_back(self):
self.app_view_model.navigate_back()

def _connect_popovers(self):
self.headerbar.search_button.set_popover(self.search_view.popover)

Expand All @@ -140,8 +137,6 @@ def _on_open_view(self, event, data):
self.open_book(data)
elif event == OpenView.LIBRARY:
self.open_library()
elif event == OpenView.BACK:
self.navigate_back()

def _on_library_view_event(self, event: str, _):
if event == "work-done":
Expand Down
33 changes: 14 additions & 19 deletions cozy/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import locale
import logging
import os
import platform
import sys
import threading
from pathlib import Path
Expand All @@ -15,9 +16,7 @@
from cozy.ui.widgets.filter_list_box import FilterListBox
from cozy.ui.widgets.seek_bar import SeekBar

gi.require_version('Handy', '1')

from gi.repository import Gtk, GLib, Handy
from gi.repository import Gtk, GLib, Adw

from cozy.app_controller import AppController
from cozy.control.db import init_db
Expand Down Expand Up @@ -57,15 +56,15 @@ def run_with_except_hook(*args2, **kwargs2):
threading.Thread.__init__ = init


class Application(Gtk.Application):
class Application(Adw.Application):
ui: CozyUI
app_controller: AppController

def __init__(self, localedir: str, pkgdatadir: str):
self.localedir = localedir
self.pkgdatadir = pkgdatadir

Gtk.Application.__init__(self, application_id='com.github.geigi.cozy')
super().__init__(application_id='com.github.geigi.cozy')
self.init_custom_widgets()

GLib.setenv("PULSE_PROP_media.role", "music", True)
Expand All @@ -87,17 +86,12 @@ def __init__(self, localedir: str, pkgdatadir: str):

def do_startup(self):
log.info(distro.linux_distribution(full_distribution_name=False))
log.info("Starting up cozy " + __version__)
log.info(f"Starting up cozy {__version__}")
log.info(f"libadwaita version: {Adw._version}")

self.ui = CozyUI(self.pkgdatadir, self, __version__)
Adw.Application.do_startup(self)
init_db()
Gtk.Application.do_startup(self)
Handy.init()
try:
manager = Handy.StyleManager.get_default()
manager.set_color_scheme(Handy.ColorScheme.PREFER_LIGHT)
except:
log.info("Not setting libhandy style manager, version is too old.")
log.info("libhandy version: {}".format(Handy._version))
self.ui.startup()

def do_activate(self):
Expand All @@ -115,23 +109,24 @@ def do_activate(self):
os.makedirs(path, exist_ok=True)

self.add_window(self.ui.window)
mpris = MPRIS(self)
mpris._on_current_changed()

if platform.system().lower() == "linux":
mpris = MPRIS(self)
mpris._on_current_changed()

def handle_exception(self, exc_type, exc_value, exc_traceback):
print("handle exception")
try:
reporter.exception("uncaught", exc_value, "\n".join(format_exception(exc_type, exc_value, exc_traceback)))
except:
pass
except Exception:
None

self.old_except_hook(exc_type, exc_value, exc_traceback)

def quit(self):
self.app_controller.quit()
super(Application, self).quit()


@staticmethod
def init_custom_widgets():
FilterListBox()
2 changes: 1 addition & 1 deletion cozy/application_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def dark_mode(self) -> bool:

@dark_mode.setter
def dark_mode(self, new_value: bool):
self._settings.set_boolean("dark_mode", new_value)
self._settings.set_boolean("dark-mode", new_value)

@property
def window_width(self) -> int:
Expand Down
12 changes: 4 additions & 8 deletions cozy/architecture/event_sender.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
from typing import List, Callable

import gi

gi.require_version('Gdk', '3.0')

from gi.repository import Gdk, GLib
from gi.repository import GLib


class EventSender:
Expand All @@ -14,15 +11,14 @@ def __init__(self):
self._listeners = []

def emit_event(self, event, message=None):
if type(event) is tuple and not message:
message = event[1]
event = event[0]
if isinstance(event, tuple) and message is None:
event, message = event

for function in self._listeners:
function(event, message)

def emit_event_main_thread(self, event: str, message=None):
Gdk.threads_add_idle(GLib.PRIORITY_DEFAULT_IDLE, self.emit_event, (event, message))
GLib.MainContext.default().invoke_full(GLib.PRIORITY_DEFAULT_IDLE, self.emit_event, (event, message))

def add_listener(self, function: Callable[[str, object], None]):
self._listeners.append(function)
Expand Down
4 changes: 2 additions & 2 deletions cozy/architecture/observable.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Callable

from gi.repository import Gdk, GLib
from gi.repository import GLib

from cozy.report import reporter
import logging
Expand Down Expand Up @@ -47,7 +47,7 @@ def _notify(self, prop: str):
reporter.exception("observable", e)

def _notify_main_thread(self, prop: str):
Gdk.threads_add_idle(GLib.PRIORITY_DEFAULT_IDLE, self._notify, (prop))
GLib.MainContext.default().invoke_full(GLib.PRIORITY_DEFAULT_IDLE, self._notify, (prop))

def _destroy_observers(self):
self._observers = {}
29 changes: 12 additions & 17 deletions cozy/control/artwork_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import uuid
import logging

from gi.repository import GdkPixbuf
from gi.repository import Gdk, GdkPixbuf

from cozy.application_settings import ApplicationSettings
from cozy.control.application_directories import get_cache_dir
Expand All @@ -21,31 +21,25 @@ def __init__(self):
_app_settings = inject.instance(ApplicationSettings)
_app_settings.add_listener(self._on_app_setting_changed)

def get_cover_pixbuf(self, book, scale, size=0):
def get_cover_paintable(self, book, scale, size=0) -> Gdk.Texture | None:
pixbuf = None
size *= scale

if size > 0:
# first try the cache
# First try the cache
pixbuf = self._load_pixbuf_from_cache(book, size)

if pixbuf:
return pixbuf
else:
# then try the db or file
if not pixbuf:
# Then try the db or file
pixbuf = self._load_cover_pixbuf(book)

if pixbuf:
# return original size if it is not greater than 0
if not size > 0:
return pixbuf

# create cached version
pixbuf = self._create_artwork_cache(book, pixbuf, size)
else:
pixbuf = None
if not pixbuf:
return None
elif size > 0:
# Resize and cache artwork if size is greater than 0
pixbuf = self._create_artwork_cache(book, pixbuf, size)

return pixbuf
return Gdk.Texture.new_for_pixbuf(pixbuf)

def delete_artwork_cache(self):
"""
Expand Down Expand Up @@ -240,3 +234,4 @@ def _load_pixbuf_from_file(self, book):
def _on_app_setting_changed(self, event: str, data):
if event == "prefer-external-cover":
self.delete_artwork_cache()

7 changes: 3 additions & 4 deletions cozy/control/db_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,7 @@ def update_db():
_restore_db(backup_dir_name)

from cozy.ui.db_migration_failed_view import DBMigrationFailedView
dialog = DBMigrationFailedView()
dialog.show()
DBMigrationFailedView().present()
exit(1)

if version < 10:
Expand All @@ -298,8 +297,7 @@ def update_db():
_restore_db(backup_dir_name)

from cozy.ui.db_migration_failed_view import DBMigrationFailedView
dialog = DBMigrationFailedView()
dialog.show()
DBMigrationFailedView().present()
exit(1)


Expand Down Expand Up @@ -354,3 +352,4 @@ def _restore_db(backup_dir_name: str):
if os.path.exists(wal_path_backup):
log.info("Copying wal file")
shutil.copyfile(wal_path_backup, wal_path)

Loading

0 comments on commit 6c27470

Please sign in to comment.