From aba4d14f57fde16cefb024347dab94f298c7ce31 Mon Sep 17 00:00:00 2001 From: Stu Date: Sat, 13 Dec 2025 22:21:44 -0500 Subject: [PATCH] GTK/mpv embed: fix locale, add CSS theme, drag-and-drop support --- cmd/gtkplayer/main.go | 80 ++++++++++++++++++++++++++++++++++++++++++ player/mpvembed/mpv.go | 3 ++ 2 files changed, 83 insertions(+) diff --git a/cmd/gtkplayer/main.go b/cmd/gtkplayer/main.go index ae71e0f..fb22cdd 100644 --- a/cmd/gtkplayer/main.go +++ b/cmd/gtkplayer/main.go @@ -13,6 +13,28 @@ import ( "github.com/gotk3/gotk3/gtk" ) +const appCSS = ` +* { + font-family: "Noto Sans", "Cantarell", "Sans"; + color: #E1EEFF; +} +window, GtkDrawingArea, box { + background-color: #0B0F1A; +} +button { + background: #171C2A; + color: #E1EEFF; + border-radius: 6px; + padding: 4px 8px; +} +button:hover { + background: #24314A; +} +label { + color: #E1EEFF; +} +` + type pane struct { area *gtk.DrawingArea mpv *mpvembed.Client @@ -42,6 +64,12 @@ func main() { grid.Attach(left.area, 0, 1, 1, 1) grid.Attach(right.area, 1, 1, 1, 1) + applyCSS() + preferDark() + + setupDragDest(left, win) + setupDragDest(right, win) + win.Connect("destroy", func() { if left.mpv != nil { left.mpv.Destroy() @@ -215,3 +243,55 @@ func getWindowID(w *gdk.Window) uint64 { func gdkWindowGetXID(w *gdk.Window) uint { return uint(w.GetXID()) } + +func applyCSS() { + provider, err := gtk.CssProviderNew() + if err != nil { + return + } + if err := provider.LoadFromData(appCSS); err != nil { + return + } + screen, err := gdk.ScreenGetDefault() + if err != nil { + return + } + gtk.AddProviderForScreen(screen, provider, gtk.STYLE_PROVIDER_PRIORITY_APPLICATION) +} + +func preferDark() { + if settings, err := gtk.SettingsGetDefault(); err == nil && settings != nil { + _ = settings.SetProperty("gtk-application-prefer-dark-theme", true) + } +} + +func setupDragDest(p *pane, win *gtk.Window) { + // Accept URI drops + targets := []gtk.TargetEntry{ + {Target: "text/uri-list", Flags: uint(0), Info: uint(0)}, + } + p.area.DragDestSet(gtk.DEST_DEFAULT_ALL, targets, gdk.ACTION_COPY) + p.area.Connect("drag-data-received", func(_ *gtk.DrawingArea, ctx *gdk.DragContext, x, y int, data *gtk.SelectionData, info uint, t uint32) { + uris := data.GetURIs() + if len(uris) == 0 { + ctx.Finish(false, false, t) + return + } + // Take first URI + if path := uriToPath(uris[0]); path != "" { + loadIntoPane(p, path) + } + ctx.Finish(true, false, t) + }) +} + +func uriToPath(u string) string { + if u == "" { + return "" + } + // text/uri-list format: file:///path or path\n + if len(u) >= 7 && u[:7] == "file://" { + u = u[7:] + } + return u +} diff --git a/player/mpvembed/mpv.go b/player/mpvembed/mpv.go index 64b78c3..a957fe9 100644 --- a/player/mpvembed/mpv.go +++ b/player/mpvembed/mpv.go @@ -4,6 +4,7 @@ package mpvembed #cgo pkg-config: mpv #include #include +#include static inline const char* mpv_errstr(int err) { return mpv_error_string(err); } */ @@ -21,6 +22,8 @@ type Client struct { // New creates a new mpv client. func New() (*Client, error) { + // Ensure numeric locale is C to satisfy mpv. + C.setlocale(C.int(C.LC_NUMERIC), C.CString("C")) h := C.mpv_create() if h == nil { return nil, errors.New("mpv_create returned nil")