Better icons: device/app icon chains, volume icons, app icon (#2)
This commit was merged in pull request #2.
This commit is contained in:
1 parent
f08f241edb
commit
02b08cb471
21 files changed
+658
-70
No files matched your search
+99
-6
@@ -10,6 +10,7 @@
|
||||
#include <stdio.h>
|
||||
#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);
|
||||
@@ -42,7 +43,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)
|
||||
@@ -102,7 +180,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;
|
||||
@@ -168,8 +246,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) {
|
||||
@@ -253,7 +339,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]) {
|
||||
@@ -275,7 +364,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) {
|
||||
|
||||
Reference in new issue
Block a user