Prettify code

Signed-off-by: Vasiliy Doylov <nekocwd@mainlining.org>
This commit is contained in:
Vasiliy Doylov 2025-07-10 23:40:22 +03:00
parent c37bd1caae
commit 562ec9e94d
6 changed files with 69 additions and 81 deletions

View file

@ -6,7 +6,7 @@
* Author: Vasiliy Doylov <nekocwd@mainlining.org> * Author: Vasiliy Doylov <nekocwd@mainlining.org>
*/ */
namespace MediaPlayer { namespace MediaPlayer {
Gtk.CssProvider css; Gtk.CssProvider css;
[ModuleInit] [ModuleInit]
[CCode (cname = "g_io_phosh_plugin_media_player_load")] [CCode (cname = "g_io_phosh_plugin_media_player_load")]

View file

@ -25,6 +25,7 @@ sources = files(
'extension.vala', 'extension.vala',
'player.vala', 'player.vala',
'qs.vala', 'qs.vala',
'utils.vala',
) )
shared_module( shared_module(

View file

@ -32,6 +32,7 @@ public class MediaPlayer.Player : Gtk.Box {
} }
~Player () { ~Player () {
warning ("Destructor");
Gtk.StyleContext.remove_provider_for_screen (Gdk.Screen.get_default (), css_provider); Gtk.StyleContext.remove_provider_for_screen (Gdk.Screen.get_default (), css_provider);
} }
@ -151,15 +152,16 @@ public class MediaPlayer.Player : Gtk.Box {
switch (child.get_name ()) { switch (child.get_name ()) {
case "img_art" : case "img_art" :
child.notify["gicon"].connect (() => process_icon.begin (((Gtk.Image) child).gicon)); child.notify["gicon"].connect (() => process_icon.begin (((Gtk.Image) child).gicon));
child.notify_property ("gicon");
break; break;
default : default :
foreach (var child2 in ((Gtk.Box) child).get_children ()) { foreach (var child2 in ((Gtk.Box) child).get_children ()) {
switch (child2.get_name ()) { switch (child2.get_name ()) {
case "lbl_artist" : case "lbl_artist" :
child2.bind_property ("label", artist, "label"); child2.bind_property ("label", artist, "label", BindingFlags.SYNC_CREATE);
break; break;
case "lbl_title": case "lbl_title":
child2.bind_property ("label", title, "label"); child2.bind_property ("label", title, "label", BindingFlags.SYNC_CREATE);
break; break;
default: default:
break; break;

View file

@ -14,93 +14,32 @@ using GLib;
public class MediaPlayer.QuickSetting : Phosh.QuickSetting { public class MediaPlayer.QuickSetting : Phosh.QuickSetting {
private Player player = new Player () { margin_top = 24 }; private Player player = new Player () { margin_top = 24 };
private Gtk.Widget? hiden_player = null; private Gtk.Widget? old_player = null;
construct { construct {
parent_set.connect (() => replace_player (Stage.QickSettings_Box));
visible = false; visible = false;
var ts = new TimeoutSource (500);
ts.set_callback (() => try_replace_player ());
ts.attach ();
} }
enum Stage { ~QuickSetting () {
QickSettings_Box = 0, if (old_player != null)
QickSettings, Utils.replace_widget (player, old_player);
BoxSettings;
public string to_string () {
return ((EnumClass) typeof (Stage).class_ref ()).get_value (this).value_nick.up ();
}
} }
void replace_player (Stage stage) { bool try_replace_player () {
if (parent == null) { var anc = get_ancestor (Type.from_name ("PhoshSettings"));
if (stage == Stage.QickSettings_Box) if (anc == null)
clean_up (); return true;
return;
}
if (hiden_player != null)
return;
message ("Replace player: Stage %d %s", stage, stage.to_string ());
if (stage != Stage.BoxSettings) {
var _p = parent;
for (uint i = 0; i < stage; i++)
_p = _p.parent;
if (_p.parent != null)
replace_player (stage + 1);
_p.parent_set.connect (() => replace_player (stage + 1));
return;
}
Gtk.Box box = (Gtk.Box) parent.parent.parent;
foreach (var child in box.get_children ())
on_something_added (child);
box.add.connect ((w) => on_something_added (w));
}
void on_something_added (Gtk.Widget widget) { old_player = Utils.find_widget_by_name (anc, "PhoshDefaultMediaPlayer");
if (hiden_player != null) if (old_player == null)
return; return true;
if (widget.get_type ().is_a (typeof (Gtk.Container))) { Utils.replace_widget (old_player, player);
((Gtk.Container) widget).add.connect ((w) => on_something_added (w)); player.bind_to_player ((Gtk.Box) old_player);
}
Gtk.Box box = (Gtk.Box) parent.parent.parent; return false;
find_player (box);
}
void find_player (Gtk.Container container) {
if (hiden_player != null)
return;
var i = 0;
foreach (var child in container.get_children ()) {
if (child.name == "PhoshDefaultMediaPlayer") {
message ("Player found");
player.bind_to_player ((Gtk.Box) child);
container.add (player);
var b = (Gtk.Box) container;
b.reorder_child (player, i);
hiden_player = child;
container.remove (hiden_player);
}
if (child.get_type ().is_a (typeof (Gtk.Container))) {
find_player ((Gtk.Container) child);
}
i++;
}
}
void clean_up () {
player.dispose ();
if (player.parent != null) {
var i = 0;
foreach (var child in player.parent.get_children ()) {
if (child == player) {
parent.add (hiden_player);
((Gtk.Box) parent).reorder_child (hiden_player, i);
player.parent.remove (player);
}
i++;
}
}
hiden_player = null;
} }
} }

View file

@ -21,3 +21,8 @@
.nekoplayer image { .nekoplayer image {
padding: 10px; padding: 10px;
} }
#top-bar-bin {
background-color: rgba(255, 0, 0, 0.5);
padding-top: 100px;
}

41
src/utils.vala Normal file
View file

@ -0,0 +1,41 @@
namespace Utils {
public delegate bool FindDelegate (Gtk.Widget widget);
public Gtk.Widget ? find_widget (Gtk.Widget widget, FindDelegate find_func) {
if (find_func (widget)) {
return widget;
}
if (widget is Gtk.Container) {
foreach (var child in ((Gtk.Container) widget).get_children ()) {
var found = find_widget (child, find_func);
if (found != null)
return found;
}
}
return null;
}
public Gtk.Widget ? find_widget_by_name (Gtk.Widget widget, string name) {
return find_widget (widget, (w) => w.name == name);
}
public int get_box_position (Gtk.Box box, Gtk.Widget child) {
var i = 0;
foreach (var _child in box.get_children ()) {
if (_child == child)
return i;
}
return -1;
}
public void replace_widget (Gtk.Widget old, Gtk.Widget replacement) {
var container = old.parent;
container.add (replacement);
if (container is Gtk.Box) {
var box = (Gtk.Box) container;
var position = get_box_position (box, old);
box.reorder_child (replacement, position);
}
container.remove (old);
}
}