Move pixbuff rounding to Utils

Signed-off-by: Vasiliy Doylov <nekocwd@mainlining.org>
This commit is contained in:
Vasiliy Doylov 2025-07-11 00:16:14 +03:00
parent c81720a377
commit 25473dc635
2 changed files with 39 additions and 39 deletions

View file

@ -37,15 +37,12 @@ public class MediaPlayer.Player : Gtk.Box {
Gtk.StyleContext.remove_provider_for_screen (Gdk.Screen.get_default (), css_provider);
}
private double deg (double deg) {
return Math.PI / 180.0 * deg;
}
void set_colors (Gdk.RGBA start, Gdk.RGBA end, Gdk.RGBA fg) {
var data = ".%s {
color: %s;
background: radial-gradient(circle at left, %s 0%%, %s 100%%);
}".printf (id, fg.to_string (), start.to_string (), end.to_string ());
var data =
".%s {
color: %s;
background: radial-gradient(circle at left, %s 0%%, %s 100%%);
}".printf (id, fg.to_string (), start.to_string (), end.to_string ());
try {
css_provider.load_from_data (data);
} catch (Error err) {
@ -54,10 +51,7 @@ public class MediaPlayer.Player : Gtk.Box {
}
private void generate_style (Gdk.Pixbuf pixbuff) {
message ("Start");
var histogram = Color.quanitize_pixbuff (pixbuff);
message ("Histogram OK");
message ("Sort OK");
Gdk.RGBA? fg_color = null, bg_start_color = null, bg_end_color = null;
double bg_start_lum = 0;
@ -103,7 +97,7 @@ public class MediaPlayer.Player : Gtk.Box {
private async Icon ? process_icon (Icon? icon) {
Gdk.Pixbuf pixbuff = null;
if (icon == null)
return null;
return icon;
if (icon is Gdk.Pixbuf)
pixbuff = (Gdk.Pixbuf) icon;
@ -113,39 +107,16 @@ public class MediaPlayer.Player : Gtk.Box {
pixbuff = yield new Gdk.Pixbuf.from_stream_async (yield fi.load_async (1024, null, null));
} catch (Error err) {
warning ("Icon load failed %s", err.message);
return null;
return icon;
}
} else {
return icon;
}
// Downscale to 110x110
// pixbuff = pixbuff.scale_simple (110, 110, Gdk.InterpType.BILINEAR);
// Start background generator
run_color_thred (pixbuff.scale_simple (64, 64, Gdk.InterpType.BILINEAR));
var width = pixbuff.width;
var height = pixbuff.height;
message ("%dx%d", width, height);
var surface = new Cairo.ImageSurface (Cairo.Format.ARGB32, width, height);
var cr = new Cairo.Context (surface);
{ // That's a hack for GTK3. TODO: Remove when phosh will be moved to gtk4
var radius = int.max (width, height) / 110 * 10;
cr.new_path ();
cr.arc (width - radius, radius, radius, deg (-90), deg (0));
cr.arc (width - radius, height - radius, radius, deg (0), deg (90));
cr.arc (radius, height - radius, radius, deg (90), deg (180));
cr.arc (radius, radius, radius, deg (180), deg (270));
cr.close_path ();
cr.clip ();
}
Gdk.cairo_set_source_pixbuf (cr, pixbuff, 0, 0);
cr.paint ();
pixbuff = Gdk.pixbuf_get_from_surface (surface, 0, 0, width, height);
image.gicon = pixbuff;
return pixbuff;
return Utils.round_pixbuff (pixbuff, (110.0 - 10.0) /* pixel-size - padding :TODO: REMOVE WHEN PHOSH MOVES TO GTK4 */ / 10 / 100);
}
public void bind_to_player (Gtk.Box player) {
@ -166,7 +137,10 @@ public class MediaPlayer.Player : Gtk.Box {
_artist.bind_property ("label", artist, "label", BindingFlags.SYNC_CREATE);
// Image
var _image = (Gtk.Image) Utils.find_widget_by_css_name (player, "img_art");
_image.notify["gicon"].connect (() => process_icon.begin (_image.gicon));
_image.notify["gicon"].connect (() => process_icon.begin (_image.gicon, (obj, res) => {
var cover = process_icon.end (res);
image.gicon = cover;
}));
_image.notify_property ("gicon");
}
}

View file

@ -42,4 +42,30 @@ namespace Utils {
}
container.remove (old);
}
public Gdk.Pixbuf round_pixbuff (Gdk.Pixbuf source, double roundness) {
// That's a hack for GTK3. TODO: Remove when phosh will be moved to gtk4
var width = source.width;
var height = source.height;
var surface = new Cairo.ImageSurface (Cairo.Format.ARGB32, width, height);
var cr = new Cairo.Context (surface);
var radius = int.max (width, height) * roundness;
cr.new_path ();
cr.arc (width - radius, radius, radius, deg (-90), deg (0));
cr.arc (width - radius, height - radius, radius, deg (0), deg (90));
cr.arc (radius, height - radius, radius, deg (90), deg (180));
cr.arc (radius, radius, radius, deg (180), deg (270));
cr.close_path ();
cr.clip ();
Gdk.cairo_set_source_pixbuf (cr, source, 0, 0);
cr.paint ();
return Gdk.pixbuf_get_from_surface (surface, 0, 0, width, height);
}
public double deg (double deg) {
return Math.PI / 180.0 * deg;
}
}