Files
openchamber-android-native/third_party/mygo/internal/linux/icon.go
T
OpenChamber 6c0ee61750 feat: port the client to MyGo native UI (0.3.0)
Switch from Asuka to MyGo, keeping the API client and the feature set:

- servers: saved servers, passwordless and password login, proxy,
  self-signed certificates, per-server device tokens (Keystore), connect
  form, instance list sheet with switch/disconnect
- sessions: virtualized list, search, new session, rename/archive/delete,
  running indicator, event stream keeps it live
- chat: pushed window with system back bar, streaming Markdown via
  ui/markdown, reasoning and tool rows with image previews, footer menu
  (read aloud via a Kotlin TTS plugin, copy, fork), user message menu
  (copy, revert, fork), context usage ring and detail sheet, attachments
  (photos/files via the document picker), permissions
- release builds allow plain http:// LAN servers (Go's net/http dials
  directly, no cleartext manifest needed)
- a local fork of MyGo (third_party/mygo) lowers the Android host's
  minSdk to 24 (Android 7.0); Go 1.27.4 via mise

Version 0.3.0.
2026-11-18 13:22:59 +08:00

110 lines
3.6 KiB
Go

//go:build linux && !android && (amd64 || arm64)
package linux
import (
"fmt"
"os"
"strings"
"sync"
)
var (
iconOnce sync.Once
gFileQueryInfo func(file ptr, attributes *byte, flags uint32, cancellable ptr, gerr *ptr) ptr
gFileInfoGetIcon func(info ptr) ptr
gDesktopAppInfoNewFromFile func(path *byte) ptr
gAppInfoGetIcon func(info ptr) ptr
gtkIconThemeGetDefault func() ptr
gtkIconThemeLookupByGicon func(theme, icon ptr, size int32, flags uint32) ptr
gtkIconInfoLoadIcon func(info ptr, gerr *ptr) ptr
gContentTypeGuess func(filename *byte, data ptr, size uintptr, uncertain *int32) ptr
gContentTypeGetIcon func(contentType ptr) ptr
)
func loadIcons() {
iconOnce.Do(func() {
mustBind(libGIO, &gFileQueryInfo, "g_file_query_info")
mustBind(libGIO, &gFileInfoGetIcon, "g_file_info_get_icon")
mustBind(libGIO, &gDesktopAppInfoNewFromFile, "g_desktop_app_info_new_from_filename")
mustBind(libGIO, &gAppInfoGetIcon, "g_app_info_get_icon")
mustBind(libGTK, &gtkIconThemeGetDefault, "gtk_icon_theme_get_default")
mustBind(libGTK, &gtkIconThemeLookupByGicon, "gtk_icon_theme_lookup_by_gicon")
mustBind(libGTK, &gtkIconInfoLoadIcon, "gtk_icon_info_load_icon")
mustBind(libGIO, &gContentTypeGuess, "g_content_type_guess")
mustBind(libGIO, &gContentTypeGetIcon, "g_content_type_get_icon")
})
}
// FileIcon returns the icon the file manager shows for a file or a folder,
// that of its content type (GIO's standard::icon), or the icon of the
// application of a desktop entry, from the icon theme at size by size
// pixels. Where the theme has no icon of the application, a desktop entry
// gets that of its content type, as other files do.
func (shell) FileIcon(path string, size int) ([]byte, error) {
if _, err := os.Stat(path); err != nil {
return nil, err
}
loadIcons()
if strings.HasSuffix(path, ".desktop") {
if app := gDesktopAppInfoNewFromFile(cs(path)); app != 0 {
png := themeIcon(gAppInfoGetIcon(app), size)
gObjectUnref(app)
if png != nil {
return png, nil
}
}
}
f := gFileNewForPath(cs(path))
var gerr ptr
info := gFileQueryInfo(f, cs("standard::icon"), 0, 0, &gerr)
gObjectUnref(f)
if info == 0 {
return nil, gErr(gerr)
}
defer gObjectUnref(info)
if png := themeIcon(gFileInfoGetIcon(info), size); png != nil {
return png, nil
}
return nil, fmt.Errorf("mygo: the icon theme has no icon for %q", path)
}
// TypeIcon returns the icon the file manager shows for files with the
// extension ext: that of the content type GIO guesses from such a name,
// from the icon theme at size by size pixels.
func (shell) TypeIcon(ext string, size int) ([]byte, error) {
loadIcons()
ct := gContentTypeGuess(cs("file."+ext), 0, 0, nil)
icon := gContentTypeGetIcon(ct)
gFree(ct)
png := themeIcon(icon, size)
gObjectUnref(icon)
if png == nil {
return nil, fmt.Errorf("mygo: the icon theme has no icon for files of type %q", ext)
}
return png, nil
}
// themeIcon draws a GIcon from the icon theme at size by size pixels, or
// returns nil where the theme has none of its names.
func themeIcon(icon ptr, size int) []byte {
if icon == 0 {
return nil
}
// GIO's themed icons list their generic names themselves, and GTK takes
// GTK_ICON_LOOKUP_GENERIC_FALLBACK only with names of its own.
const flags = 1 << 4 // GTK_ICON_LOOKUP_FORCE_SIZE
info := gtkIconThemeLookupByGicon(gtkIconThemeGetDefault(), icon, int32(size), flags)
if info == 0 {
return nil
}
defer gObjectUnref(info)
pix := gtkIconInfoLoadIcon(info, nil)
if pix == 0 {
return nil
}
defer gObjectUnref(pix)
return pngFromPixbuf(pix)
}