Use real icons: per-device/app icon chains, volume icons, app icon
CI / check (pull_request) Failing after 9s
CI / native (pull_request) Successful in 1m5s

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
SpriteandClaude Sonnet 5.5 committed 2026-11-23 00:43:28 +00:00
1 parent 92ae5cdbfe
commit cf8bab0d36
21 files changed
+658 -70

No files matched your search

+51 -3
View File
@@ -77,8 +77,13 @@ extern "C" fn ffi_scrolled_new(child : Widget) -> Widget = "mm_gtk_scrolled_new"
extern "C" fn ffi_notebook_new(tag : Int32) -> Widget = "mm_gtk_notebook_new"
///|
#borrow(label)
extern "C" fn ffi_notebook_append(nb : Widget, child : Widget, label : Bytes) = "mm_gtk_notebook_append"
#borrow(label, icon)
extern "C" fn ffi_notebook_append(
nb : Widget,
child : Widget,
label : Bytes,
icon : Bytes,
) = "mm_gtk_notebook_append"
///|
extern "C" fn ffi_notebook_set_page(nb : Widget, i : Int32) = "mm_gtk_notebook_set_page"
@@ -105,6 +110,14 @@ extern "C" fn ffi_image_new(icon : Bytes, size : Int32) -> Widget = "mm_gtk_imag
#borrow(icon)
extern "C" fn ffi_image_set_icon(img : Widget, icon : Bytes) = "mm_gtk_image_set_icon"
///|
#borrow(name)
extern "C" fn ffi_has_icon(name : Bytes) -> Int32 = "mm_gtk_has_icon"
///|
#borrow(key)
extern "C" fn ffi_app_icon(key : Bytes) -> Bytes = "mm_gtk_app_icon"
///|
extern "C" fn ffi_scale_new(max : Double, norm : Double, tag : Int32) -> Widget = "mm_gtk_scale_new"
@@ -339,8 +352,9 @@ pub fn Widget::append_page(
self : Widget,
child : Widget,
label : String,
icon~ : String = "",
) -> Unit {
ffi_notebook_append(self, child, cstr(label))
ffi_notebook_append(self, child, cstr(label), cstr(icon))
}
///|
@@ -476,3 +490,37 @@ pub fn Widget::set_valign_center(self : Widget) -> Unit {
pub fn Widget::save_png(self : Widget, path : String) -> Bool {
ffi_save_png(self, cstr(path)) != 0
}
///|
/// Does the current icon theme have this icon?
pub fn has_icon(name : String) -> Bool {
ffi_has_icon(cstr(name)) != 0
}
///|
/// Icon of the installed application matching `key` (desktop id, binary or
/// name) per its .desktop file, "" if there is none or the theme lacks it.
pub fn app_icon(key : String) -> String {
@utf8.decode_lossy(ffi_app_icon(cstr(key)))
}
///|
/// First entry of an icon chain (see @model.device_icon_chain) that the theme
/// has. "app:<key>" entries are looked up in the .desktop files. The last
/// entry is returned if nothing matches, so the result is never empty.
pub fn resolve_icon(chain : Array[String]) -> String {
for name in chain {
if name.has_prefix("app:") {
let icon = app_icon(name[4:].to_string())
if icon != "" {
return icon
}
} else if has_icon(name) {
return name
}
}
match chain.last() {
Some(n) => n
None => "image-missing"
}
}
+99 -6
View File
@@ -9,6 +9,7 @@
*/
#include <string.h>
#include <gtk/gtk.h>
#include <gio/gdesktopappinfo.h>
#include "moonbit.h"
typedef void (*mm_gcall_t)(void *closure, int32_t tag, double value);
@@ -41,7 +42,84 @@ MOONBIT_FFI_EXPORT void mm_gtk_set_handler(mm_gcall_t call, void *closure) {
g_closure = closure;
}
MOONBIT_FFI_EXPORT int32_t mm_gtk_init(void) { return gtk_init_check(); }
MOONBIT_FFI_EXPORT int32_t mm_gtk_init(void) {
/* The Wayland app id comes from the program name; match the .desktop file
* so the compositor shows the app icon. */
g_set_prgname("org.latha.moonmix");
if (!gtk_init_check())
return 0;
/* Let an uninstalled checkout find data/icons/hicolor/... too:
* $MOONMIX_ICON_DIR, then ./data/icons. */
GtkIconTheme *theme = gtk_icon_theme_get_for_display(gdk_display_get_default());
const char *dir = g_getenv("MOONMIX_ICON_DIR");
if (dir && *dir)
gtk_icon_theme_add_search_path(theme, dir);
gtk_icon_theme_add_search_path(theme, "data/icons");
gtk_window_set_default_icon_name("org.latha.moonmix");
return 1;
}
/* 1 if the icon theme has `name`. */
MOONBIT_FFI_EXPORT int32_t mm_gtk_has_icon(moonbit_bytes_t name) {
GdkDisplay *d = gdk_display_get_default();
if (!d)
return 0;
return gtk_icon_theme_has_icon(gtk_icon_theme_get_for_display(d), s(name));
}
static int theme_icon_of(GAppInfo *info, GtkIconTheme *theme, char **out) {
GIcon *icon = g_app_info_get_icon(info);
if (icon && G_IS_THEMED_ICON(icon)) {
const char *const *names = g_themed_icon_get_names(G_THEMED_ICON(icon));
for (int i = 0; names && names[i]; i++)
if (gtk_icon_theme_has_icon(theme, names[i])) {
*out = g_strdup(names[i]);
return 1;
}
}
return 0;
}
/* Look an application up in the installed .desktop files by desktop id
* ("org.mozilla.firefox"), executable basename ("firefox") or display name
* ("Firefox"), and return its icon name if the theme has it. "" if none. */
MOONBIT_FFI_EXPORT moonbit_bytes_t mm_gtk_app_icon(moonbit_bytes_t key) {
const char *k = s(key);
char *found = NULL;
GdkDisplay *d = gdk_display_get_default();
if (d && *k) {
GtkIconTheme *theme = gtk_icon_theme_get_for_display(d);
char *id = g_strconcat(k, ".desktop", NULL);
GDesktopAppInfo *di = g_desktop_app_info_new(id);
g_free(id);
if (di) {
theme_icon_of(G_APP_INFO(di), theme, &found);
g_object_unref(di);
}
if (!found) {
GList *all = g_app_info_get_all();
for (GList *l = all; l && !found; l = l->next) {
GAppInfo *info = l->data;
if (!g_app_info_should_show(info))
continue;
const char *exe = g_app_info_get_executable(info);
char *base = exe ? g_path_get_basename(exe) : NULL;
const char *nm = g_app_info_get_name(info);
if ((base && g_ascii_strcasecmp(base, k) == 0) ||
(nm && g_ascii_strcasecmp(nm, k) == 0))
theme_icon_of(info, theme, &found);
g_free(base);
}
g_list_free_full(all, g_object_unref);
}
}
size_t n = found ? strlen(found) : 0;
moonbit_bytes_t out = moonbit_make_bytes(n + 1, 0);
if (found)
memcpy(out, found, n);
g_free(found);
return out;
}
MOONBIT_FFI_EXPORT void mm_gtk_run(void) {
if (!g_loop)
@@ -101,7 +179,7 @@ MOONBIT_FFI_EXPORT GtkWidget *mm_gtk_window_new(moonbit_bytes_t title,
GtkWidget *win = gtk_window_new();
gtk_window_set_title(GTK_WINDOW(win), s(title));
gtk_window_set_default_size(GTK_WINDOW(win), w, h);
gtk_window_set_icon_name(GTK_WINDOW(win), "multimedia-volume-control");
gtk_window_set_icon_name(GTK_WINDOW(win), "org.latha.moonmix");
set_tag(win, tag);
g_signal_connect(win, "close-request", G_CALLBACK(on_close), NULL);
return win;
@@ -167,8 +245,16 @@ MOONBIT_FFI_EXPORT GtkWidget *mm_gtk_notebook_new(int32_t tag) {
}
MOONBIT_FFI_EXPORT void mm_gtk_notebook_append(GtkWidget *nb, GtkWidget *child,
moonbit_bytes_t label) {
gtk_notebook_append_page(GTK_NOTEBOOK(nb), child, gtk_label_new(s(label)));
moonbit_bytes_t label,
moonbit_bytes_t icon) {
GtkWidget *tab = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 6);
if (s(icon)[0]) {
GtkWidget *img = gtk_image_new_from_icon_name(s(icon));
gtk_image_set_pixel_size(GTK_IMAGE(img), 16);
gtk_box_append(GTK_BOX(tab), img);
}
gtk_box_append(GTK_BOX(tab), gtk_label_new(s(label)));
gtk_notebook_append_page(GTK_NOTEBOOK(nb), child, tab);
}
MOONBIT_FFI_EXPORT void mm_gtk_notebook_set_page(GtkWidget *nb, int32_t i) {
@@ -252,7 +338,10 @@ MOONBIT_FFI_EXPORT GtkWidget *mm_gtk_toggle_new(moonbit_bytes_t icon,
GtkWidget *b = gtk_toggle_button_new();
if (s(icon)[0] && s(label)[0]) {
GtkWidget *box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 6);
gtk_box_append(GTK_BOX(box), gtk_image_new_from_icon_name(s(icon)));
GtkWidget *img = gtk_image_new_from_icon_name(s(icon));
gtk_image_set_pixel_size(GTK_IMAGE(img), 16);
g_object_set_data(G_OBJECT(b), "mm-img", img);
gtk_box_append(GTK_BOX(box), img);
gtk_box_append(GTK_BOX(box), gtk_label_new(s(label)));
gtk_button_set_child(GTK_BUTTON(b), box);
} else if (s(icon)[0]) {
@@ -274,7 +363,11 @@ MOONBIT_FFI_EXPORT void mm_gtk_toggle_set(GtkWidget *b, int32_t active) {
MOONBIT_FFI_EXPORT void mm_gtk_button_set_icon(GtkWidget *b,
moonbit_bytes_t icon) {
gtk_button_set_icon_name(GTK_BUTTON(b), s(icon));
GtkWidget *img = g_object_get_data(G_OBJECT(b), "mm-img");
if (img)
gtk_image_set_from_icon_name(GTK_IMAGE(img), s(icon));
else
gtk_button_set_icon_name(GTK_BUTTON(b), s(icon));
}
static void on_selected(GObject *dd, GParamSpec *p, gpointer d) {
+7 -1
View File
@@ -2,8 +2,12 @@
package "geordi/moonmix/gtk"
// Values
pub fn app_icon(String) -> String
pub fn dropdown((Int32) -> Unit) -> (Widget, Int32)
pub fn has_icon(String) -> Bool
pub fn hbox(Int32) -> Widget
pub fn image(String, Int32) -> Widget
@@ -20,6 +24,8 @@ pub fn now_us() -> Int64
pub fn quit() -> Unit
pub fn resolve_icon(Array[String]) -> String
pub fn run() -> Unit
pub fn scale(Double, Double, (Double) -> Unit) -> (Widget, Int32)
@@ -47,7 +53,7 @@ pub fn window(String, Int32, Int32, on_close? : () -> Unit) -> Widget
pub type Widget
pub fn Widget::add_css_class(Self, String) -> Unit
pub fn Widget::append(Self, Self) -> Unit
pub fn Widget::append_page(Self, Self, String) -> Unit
pub fn Widget::append_page(Self, Self, String, icon? : String) -> Unit
pub fn Widget::present(Self) -> Unit
pub fn Widget::remove(Self, Self) -> Unit
pub fn Widget::reorder_after(Self, Self, Self?) -> Unit