Player WIP

Signed-off-by: Vasiliy Doylov <nekocwd@mainlining.org>
This commit is contained in:
Vasiliy Doylov 2025-07-10 05:27:57 +03:00
parent 550161590a
commit 9db49ee448
4 changed files with 145 additions and 2 deletions

View file

@ -23,6 +23,7 @@ lockscreen_plugins_dir = phosh_dep.get_variable('lockscreen_plugins_dir')
quick_setting_plugins_dir = phosh_dep.get_variable('quick_setting_plugins_dir')
common_deps = [
dependency('gee-0.8'),
dependency('gio-2.0'),
dependency('gtk+-3.0'),
dependency('libphosh-0.45'),

View file

@ -7,10 +7,9 @@ template $ExtensionPlayer: Box {
"nekoplayer",
]
Image {
Image image {
visible: true;
icon-name: "org.gnome.Totem-symbolic";
pixel-size: 120;
}
Box {

View file

@ -10,9 +10,151 @@ using Phosh;
using GLib;
[GtkTemplate (ui = "/mobi/phosh/plugins/vala-quick-setting/player.ui")]
public class Extension.Player : Gtk.Box {
[GtkChild]
private unowned Gtk.Image image;
construct {
}
private double deg (double deg) {
return Math.PI / 180.0 * deg;
}
private Gdk.RGBA max_color (ref Gee.HashMap<Gdk.RGBA?, int> histogram) {
int max = 0;
Gdk.RGBA bebra = {};
foreach (var kv in histogram) {
if (kv.value > max) {
max = kv.value;
bebra = kv.key;
}
}
return bebra;
}
private void quanitize (Gdk.Pixbuf pixbuf) {
message ("Meow");
var histogram = new Gee.HashMap<Gdk.RGBA?, int>
(
(a) => ((uint8) a.red * 255 + ((uint8) a.green * 255 << 8) + ((uint8) a.blue * 255 << 16)),
(a, b) => (a.red == b.red && a.blue == b.blue && a.green == b.green)
);
unowned uint8[] pixels = pixbuf.get_pixels ();
var width = pixbuf.get_width ();
var height = pixbuf.get_height ();
var rowstride = pixbuf.get_rowstride ();
var n_channels = pixbuf.get_n_channels ();
for (var y = 0; y < height; y++) {
for (var x = 0; x < width; x++) {
var offset = y * rowstride + x * n_channels;
var r = pixels[offset];
var g = pixels[offset + 1];
var b = pixels[offset + 2];
var a = (n_channels == 4) ? pixels[offset + 3] : 255;
var rgba = Gdk.RGBA () {
red = r / 255.0,
green = g / 255.0,
blue = b / 255.0,
alpha = a / 255.0
};
if (histogram.has_key (rgba)) {
histogram[rgba] += 1;
} else
histogram[rgba] = 1;
}
}
for (var i = 0; i < 10; i++) {
var c = max_color (ref histogram);
message ("%s %d %lf %lf %lf %lf", c.to_string (), histogram[c], c.alpha, c.red, c.green, c.blue);
histogram[c] = -1;
}
}
private Icon ? process_pixbuff (Icon? icon) {
if (icon == null)
return null;
Gdk.Pixbuf pixbuff = null;
if (icon is Gdk.Pixbuf)
pixbuff = (Gdk.Pixbuf) icon;
else if (icon is FileIcon) {
var fi = (LoadableIcon) icon;
pixbuff = new Gdk.Pixbuf.from_stream (fi.load (1024, null, null));
} else {
return icon;
}
pixbuff = pixbuff.scale_simple (120, 120, Gdk.InterpType.BILINEAR);
message ("Processing pixbuff");
var radius = 10;
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
cr.new_path ();
radius = 0; // Don't cut that edges;
cr.arc (width - radius, radius, radius, deg (-90), deg (0));
cr.arc (width - radius, height - radius, radius, deg (0), deg (90));
radius = 10;
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);
quanitize (pixbuff);
return pixbuff;
}
private void bind_to_info_btn (Gtk.Box box) {
foreach (var child in box.get_children ()) {
switch (child.get_name ()) {
case "img_art" :
child.bind_property
("gicon", image, "gicon", BindingFlags.SYNC_CREATE,
(_, src, ref dst) => {
dst = process_pixbuff ((Gdk.Pixbuf) src.get_object ());
return true;
});
message ("Found art");
break;
default :
break;
}
}
}
private void bind_to_control_box (Gtk.Box box) {
}
private void bind_to_duration_box (Gtk.Box box) {
}
public void bind_to_player (Gtk.Box player) {
foreach (var child in player.get_children ()) {
switch (child.get_name ()) {
case "btn_details":
bind_to_info_btn (((Gtk.Button) child).get_child () as Gtk.Box);
break;
case "box_pos_len":
bind_to_duration_box (child as Gtk.Box);
break;
default:
bind_to_control_box (child as Gtk.Box);
break;
}
}
}
}

View file

@ -83,6 +83,7 @@ public class Extension.QuickSetting : Phosh.QuickSetting {
if (child.name == "PhoshDefaultMediaPlayer") {
message ("Player found");
var player = new Player ();
player.bind_to_player ((Gtk.Box) child);
container.add (player);
var b = (Gtk.Box) container;
b.reorder_child (player, i);