# Forked from Alpine to apply Purism's patches for mobile usage
pkgname=gtk+3.0
pkgver=9999_git20231113
pkgrel=0
# pureos/latest branch
_commit="724e289ee0d8b9296722c256994825ae5f6235cc"
pkgdesc="The GTK+ Toolkit (v3)"
url="https://www.gtk.org/"
install="$pkgname.post-install $pkgname.post-upgrade $pkgname.post-deinstall"
arch="all"
license="LGPL-2.1-or-later"
options="!check" # Most glade tests fail :c
subpackages="$pkgname-demo $pkgname-dev $pkgname-lang $pkgname-dbg"
depends="shared-mime-info gtk-update-icon-cache"

replaces="gtk+"

depends_dev="
	at-spi2-core-dev
	gdk-pixbuf-dev
	glib-dev
	libepoxy-dev
	libxext-dev
	libxi-dev
	libxinerama-dev
	wayland-protocols
	wayland-libs-client
	wayland-libs-cursor
	libxkbcommon-dev
	"
makedepends="
	$depends_dev
	cups-dev
	expat-dev
	gettext-dev
	gnutls-dev
	gobject-introspection-dev
	libice-dev
	tiff-dev
	zlib-dev
	cairo-dev
	fontconfig-dev
	pango-dev
	wayland-dev
	libx11-dev
	libxcomposite-dev
	libxcursor-dev
	libxdamage-dev
	libxfixes-dev
	libxrandr-dev
	meson
	gtk-doc
	iso-codes-dev
	"
checkdepends="
	xvfb-run
	ibus
	librsvg
	gdk-pixbuf
	"
source="https://source.puri.sm/Librem5/gtk/-/archive/$_commit/gtk-$_commit.tar.gz
	0001-Port-file-chooser-to-phones.patch
	"

builddir="$srcdir/gtk-$_commit"

prepare() {
	default_prepare

	# Hand-picked Purism and Debian patches
	xargs -I {} -t -r -n1 patch -p1 -i debian/patches/{} << EOF
selection-Use-the-right-mime-type.patch
Update-Hebrew-translation.patch
Update-Galician-translation.patch
016_no_offscreen_widgets_grabbing.patch
017_no_offscreen_device_grabbing.patch
060_ignore-random-icons.patch
pureos/adaptive/libhandy/Add-GtkHdyViewSwitcherButton.patch
pureos/adaptive/libhandy/Add-GtkHdyViewSwitcher.patch
pureos/adaptive/libhandy/Add-GtkHdyViewSwitcherBar.patch
pureos/adaptive/libhandy/Add-GtkHdyAnimation.patch
pureos/adaptive/libhandy/Add-GtkHdySqueezer.patch
pureos/adaptive/libhandy/Add-GtkHdyViewSwitcherTitle.patch
pureos/adaptive/libhandy/Add-GtkHdyShadowHelper.patch
pureos/adaptive/libhandy/Add-GtkHdyNavigationDirection.patch
pureos/adaptive/libhandy/Add-GtkHdySwipeable-and-GtkHdySwipeTracker.patch
pureos/adaptive/libhandy/Add-GtkHdyClamp.patch
pureos/adaptive/libhandy/Add-GtkHdyFlap.patch
pureos/adaptive/libhandy/theme-Add-libhandy-styles.patch
pureos/adaptive/libhandy/Add-padding-for-HdyViewSwitcherTitle.patch
pureos/adaptive/libhandy/hdy-flap-Use-natural-size-for-folding-instead-of-minimum.patch
pureos/adaptive/Add-org.gtk.Settings.Purism.patch
pureos/adaptive/gtkprivate-Add-an-API-to-check-if-phone.patch
pureos/adaptive/aboutdialog-Port-to-phones.patch
pureos/adaptive/Add-the-view-sidebar-symbolic-icon.patch
pureos/adaptive/messagedialog-Set-orientation-based-on-device.patch
pureos/adaptive/dialog-Maximize-resizable-dialogs-on-phones.patch
pureos/adaptive/window-Maximize-resizable-pseudo-dialogs-on-mobile.patch
pureos/adaptive/headerbar-Use-a-back-button-in-dialogs-on-mobile.patch
pureos/adaptive/infobar-Move-the-action-area-below-on-the-phone.patch
pureos/adaptive/Reduce-the-font-chooser-minimum-size.patch
pureos/adaptive/printunixdialog-Adapt-for-phones.patch
pureos/adaptive/window-Disable-window-dragging-on-phones.patch
pureos/adaptive/librem5-Make-GtkShortcutsWindow-adaptive.patch
pureos/adaptive/scrolledwindow-Set-deceleration-value-based-on-the-device.patch
pureos/events-Compress-touch-update-events.patch
reftest_compare_surfaces-Report-how-much-the-images-diffe.patch
reftests-Allow-minor-differences-to-be-tolerated.patch
pureos/gdk-wayland-Track-last-touch-serial-on-seat.patch
EOF

	# Prevent unexpected downgrade: 3.90.99 is higher than Alpine's version
	# (pma#694), but still passes the libportal check (pma#841)
	sed -i "s/        version: '3.*',/        version: '3.90.99',/g" meson.build
	sed -i "s/^gtk_interface_age = .*/gtk_interface_age = 90/g" meson.build
}

build() {
	# postmarketOS specific: disable stuff that takes forever to (cross)compile:
	# man=false, gtk_doc=false, tests=false
	meson \
		--prefix=/usr \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--localstatedir=/var \
		--buildtype=plain \
		-Dman=false \
		-Dgtk_doc=false \
		-Dtests=false \
		-Dbroadway_backend=true \
		output
	ninja -C output
}

check() {
	xvfb-run ninja -C output test
}

package() {
	DESTDIR="$pkgdir" ninja -C output install

	# use gtk-update-icon-cache from gtk+2.0 for now
	rm -f "$pkgdir"/usr/bin/gtk-update-icon-cache
	rm -f "$pkgdir"/usr/share/man/man1/gtk-update-icon-cache.1
}

demo() {
	pkgdesc="$pkgdesc (demonstration application)"
	install -Dm755 "$pkgdir"/usr/bin/gtk3-demo \
				   "$pkgdir"/usr/bin/gtk3-widget-factory \
				   "$pkgdir"/usr/bin/gtk3-demo-application \
				   -t "$subpkgdir"/usr/bin
	install -Dm644 "$pkgdir"/usr/share/gtk-3.0/gtkbuilder.rng \
				   -t "$subpkgdir"/usr/share/gtk-3.0
	install -Dm644 "$pkgdir"/usr/share/glib-2.0/schemas/org.gtk.Demo.gschema.xml \
				   -t "$subpkgdir"/usr/share/glib-2.0/schemas
	install -Dm644 "$pkgdir"/usr/share/applications/gtk3-widget-factory.desktop \
				   "$pkgdir"/usr/share/applications/gtk3-demo.desktop \
				   -t "$subpkgdir"/usr/share/applications
	mkdir -p "$subpkgdir"/usr/share
	mv "$pkgdir"/usr/share/icons "$subpkgdir"/usr/share
}

dev() {
	replaces="gtk+-dev"
	default_dev
}

doc() {
	replaces="gtk+-doc"
	default_doc
}

sha512sums="
d62c9133e27d02c4252e1e175a316849417c4726ba03f80dac9686dcf4dd984823dd45b45bafd2049f4a1e3fd74a61982ac8733bfe9456eb7c0a9f5c7beef1bf  gtk-724e289ee0d8b9296722c256994825ae5f6235cc.tar.gz
6160b4feb366714d609b8ceae62ed8065b000a89a62563180d54c47f22a506d7f5dd85a1bb3e00625ff9f2d5091093adf7b5325b0e5b205cb59aa247f2e17902  0001-Port-file-chooser-to-phones.patch
"
