Verified Commit fa0d6201 authored by Zander Brown's avatar Zander Brown

Show app icon on headerbar, extract an accent colour

parent 813a7758
......@@ -11,4 +11,4 @@ Categories=System;TerminalEmulator;X-GNOME-Utilities;GTK;GNOME;
StartupNotify=true
DBusActivatable=true
X-GNOME-UsesNotifications=true
x-terminal-handler/standard;x-terminal-handler/enhanced;
MimeType=x-terminal-handler/standard;x-terminal-handler/enhanced;
......@@ -44,6 +44,7 @@
<part id="util-api">
<title>Utils</title>
<xi:include href="xml/kgx-utils.xml"/>
<xi:include href="xml/rgba.xml"/>
<xi:include href="xml/fp-vte-util.xml"/>
</part>
......
......@@ -39,6 +39,7 @@
#include "kgx-application.h"
#include "kgx-search-box.h"
#include "kgx-window.h"
#include "kgx-utils.h"
#define LOGO_COL_SIZE 28
#define LOGO_ROW_SIZE 14
......@@ -333,6 +334,7 @@ kgx_application_command_line (GApplication *app,
if (desktop) {
g_autoptr (GDesktopAppInfo) info = NULL;
GdkRGBA colour;
info = g_desktop_app_info_new (desktop);
......@@ -344,7 +346,36 @@ kgx_application_command_line (GApplication *app,
return 1;
}
command = g_strdup (g_app_info_get_commandline (G_APP_INFO (info)));
if (kgx_get_app_colour (G_APP_INFO (info), &colour)) {
GtkCssProvider *provider;
g_autofree char* css = NULL;
const char *icon_name = "application-x-executable";
GIcon *icon = NULL;
g_message ("App Colour -> %s", gdk_rgba_to_string (&colour));
icon = g_app_info_get_icon (G_APP_INFO (info));
if (icon && G_IS_THEMED_ICON (icon)) {
icon_name = g_themed_icon_get_names (G_THEMED_ICON (icon))[0];
}
gtk_window_set_default_icon_name (icon_name);
css = g_strdup_printf ("headerbar {"
" background: -gtk-icontheme(\"%s\") 50px 0/64px 64px no-repeat,"
" #241f31;"
" border-bottom-color: %s;"
"}",
icon_name,
gdk_rgba_to_string (&colour));
provider = gtk_css_provider_new ();
gtk_css_provider_load_from_data (provider, css, -1, NULL);
gtk_style_context_add_provider_for_screen (gdk_screen_get_default (),
GTK_STYLE_PROVIDER (provider),
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION + 1);
}
}
window = g_object_new (KGX_TYPE_WINDOW,
......
/* kgx-utils.c
*
* Copyright 2019 Zander Brown
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* SECTION:kgx-utils
* @title: Utilities
* @short_description: Various useful functions
*
* Oddities that don't belong anywhere else
*
* Since: 0.2.0
*/
#define G_LOG_DOMAIN "Kgx"
#include <glib/gi18n.h>
#include "kgx-config.h"
#include "kgx-utils.h"
/**
* kgx_get_app_colour:
* @info: the #GAppInfo to pick a colour for
* @colour: (out caller-allocates): the picked #GdkRGBA
*
* Looks at the icon for @info and picks a @colour to represent it
*
* Returns: %TRUE on success
*
* Stability: Private
*
* Since: 0.2.0
*/
gboolean
kgx_get_app_colour (GAppInfo *info, GdkRGBA *colour)
{
GIcon *icon;
g_autoptr (GtkIconInfo) icon_info = NULL;
g_autoptr (GdkPixbuf) pixbuf = NULL;
g_autoptr (GError) error = NULL;
int n_channels = 0;
int width = 32;
int height = 32;
int rowstride;
guchar *pixels;
guchar *p;
int red = 0;
int blue = 0;
int green = 0;
int count = 0;
g_return_val_if_fail (G_IS_APP_INFO (info), FALSE);
if (colour == NULL) {
return FALSE;
}
icon = g_app_info_get_icon (info);
if (info == NULL) {
return FALSE;
}
icon_info = gtk_icon_theme_lookup_by_gicon (gtk_icon_theme_get_default (),
icon,
32,
GTK_ICON_LOOKUP_FORCE_SIZE);
if (icon_info == NULL) {
return FALSE;
}
pixbuf = gtk_icon_info_load_icon (icon_info, &error);
if (error) {
g_warning ("Can't load icon: %s", error->message);
return FALSE;
}
g_return_val_if_fail (gdk_pixbuf_get_colorspace (pixbuf) == GDK_COLORSPACE_RGB, FALSE);
g_return_val_if_fail (gdk_pixbuf_get_bits_per_sample (pixbuf) == 8, FALSE);
n_channels = gdk_pixbuf_get_n_channels (pixbuf);
if (n_channels == 4) {
g_return_val_if_fail (gdk_pixbuf_get_has_alpha (pixbuf), FALSE);
}
height = gdk_pixbuf_get_height (pixbuf);
width = gdk_pixbuf_get_width (pixbuf);
rowstride = gdk_pixbuf_get_rowstride (pixbuf);
pixels = gdk_pixbuf_get_pixels (pixbuf);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
p = pixels + y * rowstride + x * n_channels;
if (G_LIKELY (n_channels == 3 || (n_channels == 4 && p[3] > 0))) {
red += p[0];
green += p[1];
blue += p[2];
count++;
}
}
}
red /= count;
green /= count;
blue /= count;
colour->alpha = 1.0;
colour->red = red / 255.0;
colour->green = green / 255.0;
colour->blue = blue / 255.0;
return TRUE;
}
\ No newline at end of file
/* kgx-utils.h
*
* Copyright 2019 Zander Brown
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <gtk/gtk.h>
G_BEGIN_DECLS
gboolean kgx_get_app_colour (GAppInfo *info, GdkRGBA *colour);
G_END_DECLS
......@@ -49,6 +49,7 @@ G_BEGIN_DECLS
* @exit_info: the #GtkRevealer hat wraps @exit_message
* @exit_message: the #GtkLabel for showing important messages
* @zoom_level: the #GtkLabel in the #GtkPopover showing the current zoom level
* @notification_id: the id used for #GNotification s sent by this window
*
* Since: 0.1.0
*/
......
......@@ -23,5 +23,6 @@
#include "kgx-search-box.h"
#include "kgx-enums.h"
#include "kgx-process.h"
#include "kgx-utils.h"
#include "fp-vte-util.h"
......@@ -13,6 +13,8 @@ kgx_sources = [
'kgx-close-dialog-row.h',
'kgx-window.c',
'kgx-window.h',
'kgx-utils.c',
'kgx-utils.h',
'kgx-process.h',
]
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment