Skip to content

Commit

Permalink
skillview: add icon support
Browse files Browse the repository at this point in the history
Currently the icons are black, which doesn't mix well with dark themes.
This will be fixed in another commit.
  • Loading branch information
knuxify committed Jul 8, 2022
1 parent 602eed1 commit 26274a9
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Before you can use the `./run` script, you will need to install Meson.
- PyGObject
- GTK4
- libadwaita
- requests
- [mycroft-core](https://github.com/MycroftAI/mycroft-core)
- [mycroft_bus_client](https://github.com/MycroftAI/mycroft-messagebus-client)

Expand Down
30 changes: 24 additions & 6 deletions data/ui/skillview.ui
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,30 @@
</style>

<child>
<object class="GtkLabel" id="title_label">
<property name="halign">start</property>
<property name="wrap">true</property>
<style>
<class name="title-4"/>
</style>
<object class="GtkBox" id="title_box">
<property name="hexpand">true</property>
<property name="spacing">12</property>

<child>
<object class="GtkLabel" id="title_label">
<property name="halign">start</property>
<property name="hexpand">true</property>
<property name="wrap">true</property>
<style>
<class name="title-4"/>
</style>
</object>
</child>

<child>
<object class="GtkImage" id="icon_image">
<property name="icon-size">large</property>
<property name="halign">end</property>
<style>
<class name="skill-icon"/>
</style>
</object>
</child>
</object>
</child>

Expand Down
50 changes: 48 additions & 2 deletions src/types/skill.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,22 @@
Contains code for handling skills.
"""
from mycroft.configuration import Configuration
from gi.repository import Gtk, GObject
from gi.repository import GLib, Gtk, GObject
import pathlib
import os.path
import re
import requests
import threading
import time
import xdg

# Set up icon cache directory

ICON_CACHE = os.path.join(GLib.get_user_cache_dir(), 'lapel', 'icons')

if not os.path.exists(ICON_CACHE):
pathlib.Path(ICON_CACHE).mkdir(parents=True, exist_ok=True)

mycroft_config = None
mycroft_config_set = False

Expand Down Expand Up @@ -55,7 +65,7 @@ def skill_id_to_path(skill_id):
if os.path.isdir(filename):
return filename

return None # Skill cannot be resolved
return None # Skill cannot be resolved

class LapelSkill(GObject.Object):
"""
Expand All @@ -64,6 +74,9 @@ class LapelSkill(GObject.Object):
"""
__gtype_name__ = 'LapelSkill'

has_icon = False
_icon_path = None

def __init__(self, skill_id, data=None):
"""Initializes a LapelSkill object."""
super().__init__()
Expand Down Expand Up @@ -127,6 +140,21 @@ def __init__(self, skill_id, data=None):
self.data['category'] = None

# TODO: Get tags

icon_path = os.path.join(ICON_CACHE, self.skill_id + '.svg')
if 'icon' in self.data and self.data['icon']:
# Download the icon so that we can display it
if not os.path.exists(icon_path) or time.time() - os.path.getmtime(icon_path) > 2678400:
icon_data = requests.get(self.data['icon'], stream=True)
if icon_data.status_code == 200:
with open(icon_path, 'w+b') as icon_file:
for chunk in icon_data:
icon_file.write(chunk)
icon_file.flush()
self._icon_path = icon_path
elif os.path.exists(icon_path):
self._icon_path = icon_path
self.notify('icon-path')
else:
self.data = None

Expand All @@ -145,6 +173,11 @@ def is_active(self):
"""Whether the skill is active or not."""
return self.active

@GObject.Property(flags=GObject.ParamFlags.READABLE)
def icon_path(self):
"""The path to the skill's icon."""
return self._icon_path

@Gtk.Template(resource_path='/org/dithernet/lapel/ui/skillview.ui')
class SkillView(Gtk.Box):
"""
Expand All @@ -154,6 +187,7 @@ class SkillView(Gtk.Box):
__gtype_name__ = 'SkillView'

title_label = Gtk.Template.Child()
icon_image = Gtk.Template.Child()
description_label = Gtk.Template.Child()
examples_label = Gtk.Template.Child()

Expand Down Expand Up @@ -184,8 +218,20 @@ def bind_to_skill(self, skill):
self.examples_label.set_use_markup(True)
# TRANSLATORS: Shown in the skills menu when no a skill has no provided examples.
self.examples_label.set_label('<i>' + _('No examples found.') + '</i>') # noqa: F821

if skill.data['icon'] and skill._icon_path:
self.icon_image.set_from_file(skill._icon_path)
skill.connect('notify::icon-path', self.update_icon)
else:
self.icon_image.set_from_icon_name('dialog-question-symbolic')
else:
self.title_label.set_label(skill.id)
self.examples_label.set_use_markup(True)
# TRANSLATORS: Shown in the skills menu when a skill's information could not be found.
self.examples_label.set_label('<i>' + _("Skill data not found.") + '</i>') # noqa: F821

def update_icon(self, *args):
if self.skill.data['icon'] and skill._icon_path:
self.icon_image.set_from_path(self.skill._icon_path)
else:
self.icon_image.set_from_icon_name('dialog-question-symbolic')

0 comments on commit 26274a9

Please sign in to comment.