Handle cached icon loading errors

Resolves #1
This commit is contained in:
Trevor Slocum 2020-01-27 08:25:38 -08:00
parent 23f1cfa9ca
commit 92317a4ce3
9 changed files with 31 additions and 36 deletions

View File

@ -28,13 +28,13 @@ test:
- apt-get update && apt-get install -y libgtk-3-dev
- go test -race -v ./...
build-tui:
build-gmenu:
stage: build
script:
- cd cmd/gmenu
- go build
build-gui:
build-gtkmenu:
stage: build
script:
- apt-get update && apt-get install -y libgtk-3-dev

View File

@ -1,3 +1,6 @@
0.2.6:
- Handle cached icon loading errors
0.2.5:
- Reduce startup time

View File

@ -49,7 +49,7 @@ The input buffer may be executed as a shell command by selecting the last entry
## Support
Please share issues/suggestions [here](https://gitlab.com/tslocum/gmenu/issues).
Please share issues and suggestions [here](https://gitlab.com/tslocum/gmenu/issues).
## Integration Example - [sway](https://swaywm.org)/[i3](https://i3wm.org) + [alacritty](https://github.com/jwilm/alacritty)

View File

@ -14,7 +14,7 @@ var (
entryList *optionsList
appDetailsView *cview.TextView
closedGUI bool
closedTUI bool
)
type optionsList struct {
@ -64,7 +64,7 @@ func (r *optionsList) Draw(screen tcell.Screen) {
r.TextView.SetText(b.String()).Highlight("gmenu").ScrollToBeginning().Draw(screen)
}
func initGUI() (*cview.Application, error) {
func initTUI() (*cview.Application, error) {
app = cview.NewApplication()
inputView = cview.NewInputField().
SetLabel("").
@ -166,11 +166,11 @@ func initGUI() (*cview.Application, error) {
return app, nil
}
func closeGUI() {
if closedGUI {
func closeTUI() {
if closedTUI {
return
}
closedGUI = true
closedTUI = true
app.Stop()
}

View File

@ -68,7 +68,7 @@ func updateEntryInfo() {
}
func listSelect(runInTerminal bool) error {
defer closeGUI()
defer closeTUI()
var (
execute string
@ -88,7 +88,7 @@ func listSelect(runInTerminal bool) error {
execute = shellquote.Join(config.BrowserCommand(), entry.URL)
}
closeGUI()
closeTUI()
path := ""
if entry != nil {

View File

@ -44,9 +44,9 @@ func main() {
gmenu.LoadEntries(&config.Config)
app, err := initGUI()
app, err := initTUI()
if err != nil {
panic(err)
log.Fatalf("failed to initialize terminal user interface: %s", err)
}
go func() {
@ -59,5 +59,5 @@ func main() {
<-done
closeGUI()
closeTUI()
}

View File

@ -23,18 +23,19 @@ var (
func loadIcon(icon string) (*gdk.Pixbuf, error) {
cachedIcon := path.Join(iconCacheDir, fmt.Sprintf("%x.png", md5.Sum([]byte(icon))))
if _, err := os.Stat(cachedIcon); !os.IsNotExist(err) {
return gdk.PixbufNewFromFileAtSize(cachedIcon, iconSize, iconSize)
}
var (
pbuf *gdk.Pixbuf
err error
)
if path.IsAbs(icon) {
pbuf, err = gdk.PixbufNewFromFileAtSize(icon, iconSize, iconSize)
} else {
pbuf, err = iconTheme.LoadIcon(icon, iconSize, gtk.ICON_LOOKUP_USE_BUILTIN)
if _, statErr := os.Stat(cachedIcon); !os.IsNotExist(statErr) {
pbuf, err = gdk.PixbufNewFromFileAtSize(cachedIcon, iconSize, iconSize)
}
if pbuf == nil || err != nil {
if path.IsAbs(icon) {
pbuf, err = gdk.PixbufNewFromFileAtSize(icon, iconSize, iconSize)
} else {
pbuf, err = iconTheme.LoadIcon(icon, iconSize, gtk.ICON_LOOKUP_USE_BUILTIN)
}
}
if err != nil {
return nil, err
@ -81,7 +82,7 @@ func loadIconImage(img *gtk.Image, entry *gmenu.ListEntry) {
pbuf, err = loadIcon(fallbackIcon(entry))
}
if err != nil {
log.Fatal("failed to create Icon:", err)
log.Printf("failed to load entry icon %s: %s", entry.Icon, err)
}
img.SetFromPixbuf(pbuf)

View File

@ -7,7 +7,6 @@ import (
"os"
"path"
"runtime/pprof"
"time"
"github.com/gotk3/gotk3/glib"
"github.com/gotk3/gotk3/gtk"
@ -32,9 +31,7 @@ var (
listBox *gtk.ListBox
inputView *gtk.TextView
t = time.Now()
loaded = make(chan bool)
loadedGTK = make(chan bool)
profileCPU *os.File
container *gtk.Box
@ -59,7 +56,7 @@ func load() {
homeDir, err := os.UserHomeDir()
if err != nil {
log.Fatal(err)
log.Fatalf("failed to determine user home dir: %s", err)
}
iconCacheDir = path.Join(homeDir, ".cache", "gmenu", "icons")
@ -78,10 +75,7 @@ func load() {
gmenu.FilteredEntries = append(gmenu.FilteredEntries, &gmenu.ListEntry{Label: "", Entry: nil})
go gmenu.HandleInput(func(input string) {
_, err := glib.IdleAdd(updateList, input)
if err != nil {
log.Fatal(err)
}
glib.IdleAdd(updateList, input)
})
loaded <- true
@ -94,26 +88,23 @@ func main() {
var err error
profileCPU, err = os.Create(config.CPUProfile)
if err != nil {
log.Fatal(err)
log.Fatalf("failed to create cpu profile %s: %s", config.CPUProfile, err)
}
err = pprof.StartCPUProfile(profileCPU)
if err != nil {
log.Fatal(err)
log.Fatalf("failed to start profiling cpu: %s", err)
}
}
go load()
gtk.Init(nil)
w := initWindow()
container = newBox(gtk.ORIENTATION_VERTICAL)
w.Add(container)
<-loaded
initList(container)
w.ShowAll()

View File

@ -66,7 +66,7 @@ func LoadEntries(c *Config) {
var err error
Entries, Names, err = DesktopEntries(c)
if err != nil {
log.Fatal(err)
log.Fatalf("failed to load desktop entries: %s", err)
}
}