GTK/mpv embed: fix locale, add CSS theme, drag-and-drop support

This commit is contained in:
Stu 2025-12-13 22:21:44 -05:00
parent 03b6804a9e
commit aba4d14f57
2 changed files with 83 additions and 0 deletions

View File

@ -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
}

View File

@ -4,6 +4,7 @@ package mpvembed
#cgo pkg-config: mpv
#include <mpv/client.h>
#include <stdlib.h>
#include <locale.h>
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")