diff --git a/bin/nwg-drawer b/bin/nwg-drawer index 1a403a3..645405e 100755 Binary files a/bin/nwg-drawer and b/bin/nwg-drawer differ diff --git a/go.mod b/go.mod index 33bca97..e6d0271 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,6 @@ go 1.16 require ( github.com/allan-simon/go-singleinstance v0.0.0-20210120080615-d0997106ab37 github.com/dlasky/gotk3-layershell v0.0.0-20210331230524-5cca0b819261 - github.com/gotk3/gotk3 v0.5.3-0.20210223154815-289cfb6dbf32 + github.com/gotk3/gotk3 v0.6.0 github.com/joshuarubin/go-sway v0.0.4 ) diff --git a/go.sum b/go.sum index 2ba66ba..13253ab 100644 --- a/go.sum +++ b/go.sum @@ -4,10 +4,9 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dlasky/gotk3-layershell v0.0.0-20210331230524-5cca0b819261 h1:eoXn91ckLWKMXmQKX34UHEF2XMyQpRnnP80fDiu+kys= github.com/dlasky/gotk3-layershell v0.0.0-20210331230524-5cca0b819261/go.mod h1:d56Gslp3IaiT8lqxD/lO1Msz1wYgD8D/HTKHgSdg9tU= -github.com/gotk3/gotk3 v0.5.3-0.20210223154815-289cfb6dbf32 h1:wE6C/HgLUBHi8YhHlCEulrmQMntVl4PFdh3kA0sWyAY= github.com/gotk3/gotk3 v0.5.3-0.20210223154815-289cfb6dbf32/go.mod h1:/hqFpkNa9T3JgNAE2fLvCdov7c5bw//FHNZrZ3Uv9/Q= -github.com/joshuarubin/go-sway v0.0.3 h1:uuY+dAMz+iAJvso+DP7TSRczDWhaV47nEPHJoRDOqjA= -github.com/joshuarubin/go-sway v0.0.3/go.mod h1:qcDd6f25vJ0++wICwA1BainIcRC67p2Mb4lsrZ0k3/k= +github.com/gotk3/gotk3 v0.6.0 h1:Aqlq4/6VabNwtCyA9M9zFNad5yHAqCi5heWnZ9y+3dA= +github.com/gotk3/gotk3 v0.6.0/go.mod h1:/hqFpkNa9T3JgNAE2fLvCdov7c5bw//FHNZrZ3Uv9/Q= github.com/joshuarubin/go-sway v0.0.4 h1:dpmIwQ/LytG+oMrjmaVKdk1aPdW2feXK/+wAcLKIx4A= github.com/joshuarubin/go-sway v0.0.4/go.mod h1:qcDd6f25vJ0++wICwA1BainIcRC67p2Mb4lsrZ0k3/k= github.com/joshuarubin/lifecycle v1.0.0 h1:N/lPEC8f+dBZ1Tn99vShqp36LwB+LI7XNAiNadZeLUQ= diff --git a/main.go b/main.go index eb9a65b..749d4b7 100644 --- a/main.go +++ b/main.go @@ -19,19 +19,15 @@ import ( "github.com/gotk3/gotk3/gtk" ) -const version = "0.0.2" +const version = "0.1.0" var ( - appDirs []string - configDirectory string - pinnedFile string - pinned []string - rightBox *gtk.Box - src glib.SourceHandle - imgSizeScaled int - currentWsNum, targetWsNum int64 - win *gtk.Window - id2entry map[string]desktopEntry + appDirs []string + configDirectory string + pinnedFile string + pinned []string + src glib.SourceHandle + id2entry map[string]desktopEntry ) var categoryNames = [...]string{ @@ -87,10 +83,7 @@ var ( fileSearchResults []string searchEntry *gtk.SearchEntry phrase string - fileSearchResultListBox *gtk.ListBox // tbd fileSearchResultFlowBox *gtk.FlowBox - buttonsWrapper *gtk.Box - buttonBox *gtk.EventBox userDirsMap map[string]string appFlowBox *gtk.FlowBox appSearchResultWrapper *gtk.Box @@ -100,7 +93,6 @@ var ( catButtons []*gtk.Button statusLabel *gtk.Label status string - mainColumnWidth int ignore string ) @@ -166,7 +158,7 @@ func main() { configDirectory = configDir() if !pathExists(filepath.Join(configDirectory, "drawer.css")) { - copyFile("/usr/share/nwg-drawer/drawer.css", filepath.Join(configDirectory, "drawer.css")) + copyFile(filepath.Join(getDataHome(), "nwg-drawer/drawer.css"), filepath.Join(configDirectory, "drawer.css")) } cacheDirectory := cacheDir() @@ -272,7 +264,7 @@ func main() { // Close the window on leave, but not immediately, to avoid accidental closes win.Connect("leave-notify-event", func() { - src, err = glib.TimeoutAdd(uint(500), func() bool { + src = glib.TimeoutAdd(uint(500), func() bool { gtk.MainQuit() return false }) @@ -319,6 +311,17 @@ func main() { resultsWrapper.PackStart(appSearchResultWrapper, false, false, 0) appFlowBox = setUpAppsFlowBox(nil, "") + // Focus 1st pinned item if any, otherwise focus 1st found app icon + var button gtk.IWidget + if pinnedFlowBox.GetChildren().Length() > 0 { + button, err = pinnedFlowBox.GetChildAtIndex(0).GetChild() + } else { + button, err = appFlowBox.GetChildAtIndex(0).GetChild() + } + if err == nil { + button.ToWidget().GrabFocus() + } + userDirsMap = mapXdgUserDirs() placeholder, _ := gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 0) diff --git a/tools.go b/tools.go index dd7d0b8..afb45a7 100644 --- a/tools.go +++ b/tools.go @@ -21,9 +21,9 @@ import ( ) /* -Window on-leave-notify event hides the window with glib Timeout 1000 ms. +Window leave-notify-event event quits the program with glib Timeout 500 ms. We might have left the window by accident, so let's clear the timeout if window re-entered. -Furthermore - hovering a widget triggers window on-leave-notify event, and the timeout +Furthermore - hovering a widget triggers window leave-notify-event event, and the timeout needs to be cleared as well. */ func cancelClose() { @@ -33,15 +33,6 @@ func cancelClose() { } } -func inPinned(taskID string) bool { - for _, id := range pinned { - if strings.TrimSpace(taskID) == strings.TrimSpace(id) { - return true - } - } - return false -} - func createPixbuf(icon string, size int) (*gdk.Pixbuf, error) { iconTheme, err := gtk.IconThemeGetDefault() if err != nil { @@ -57,7 +48,7 @@ func createPixbuf(icon string, size int) (*gdk.Pixbuf, error) { return pixbuf, nil } else if strings.HasSuffix(icon, ".svg") || strings.HasSuffix(icon, ".png") || strings.HasSuffix(icon, ".xpm") { - // for enties like "Icon=netflix-desktop.svg" + // for entries like "Icon=netflix-desktop.svg" icon = strings.Split(icon, ".")[0] } @@ -211,6 +202,13 @@ func copyFile(src, dst string) error { return os.Chmod(dst, srcinfo.Mode()) } +func getDataHome() string { + if os.Getenv("XDG_DATA_HOME") != "" { + return os.Getenv("XDG_DATA_HOME") + } + return "/usr/share/" +} + func getAppDirs() []string { var dirs []string xdgDataDirs := "" @@ -266,7 +264,7 @@ func listDesktopFiles() []string { } func setUpCategories() { - path := "/usr/share/nwg-menu/desktop-directories" + path := filepath.Join(getDataHome(), "nwg-menu/desktop-directories") var other category for _, cName := range categoryNames { @@ -373,9 +371,7 @@ func parseDesktopFiles(desktopFiles []string) string { exec = strings.Split(l, "Exec=")[1] disallowed := [2]string{"\"", "'"} for _, char := range disallowed { - if strings.Contains(exec, char) { - exec = strings.Replace(exec, char, "", -1) - } + exec = strings.Replace(exec, char, "", -1) } continue } @@ -606,9 +602,7 @@ func launch(command string, terminal bool) { // set env variables if len(envVars) > 0 { cmd.Env = os.Environ() - for _, envVar := range envVars { - cmd.Env = append(cmd.Env, envVar) - } + cmd.Env = append(cmd.Env, envVars...) } msg := fmt.Sprintf("env vars: %s; command: '%s'; args: %s\n", envVars, elements[cmdIdx], elements[1+cmdIdx:]) diff --git a/uicomponents.go b/uicomponents.go index 1f05f40..bdb6dc1 100644 --- a/uicomponents.go +++ b/uicomponents.go @@ -32,11 +32,15 @@ func setUpPinnedFlowBox() *gtk.FlowBox { entry := id2entry[desktopID] btn, _ := gtk.ButtonNew() - pixbuf, _ := createPixbuf(entry.Icon, *iconSize) - img, err := gtk.ImageNewFromPixbuf(pixbuf) - if err != nil { - println(err, entry.Icon) + + var img *gtk.Image + if entry.Icon != "" { + pixbuf, _ := createPixbuf(entry.Icon, *iconSize) + img, _ = gtk.ImageNewFromPixbuf(pixbuf) + } else { + img, _ = gtk.ImageNewFromIconName("image-missing", gtk.ICON_SIZE_INVALID) } + btn.SetImage(img) btn.SetAlwaysShowImage(true) btn.SetImagePosition(gtk.POS_TOP) @@ -164,7 +168,7 @@ func notEmpty(listCategory []string) bool { } for _, desktopID := range listCategory { entry := id2entry[desktopID] - if entry.NoDisplay == false { + if !entry.NoDisplay { return true } } @@ -222,8 +226,14 @@ func flowBoxButton(entry desktopEntry) *gtk.Button { button, _ := gtk.ButtonNew() button.SetAlwaysShowImage(true) - pixbuf, _ := createPixbuf(entry.Icon, *iconSize) - img, _ := gtk.ImageNewFromPixbuf(pixbuf) + var img *gtk.Image + if entry.Icon != "" { + pixbuf, _ := createPixbuf(entry.Icon, *iconSize) + img, _ = gtk.ImageNewFromPixbuf(pixbuf) + } else { + img, _ = gtk.ImageNewFromIconName("image-missing", gtk.ICON_SIZE_INVALID) + } + button.SetImage(img) button.SetImagePosition(gtk.POS_TOP) name := entry.NameLoc @@ -347,7 +357,7 @@ func searchUserDir(dir string) { ignore = userDirsMap[dir] filepath.WalkDir(userDirsMap[dir], walk) - if fileSearchResults != nil && len(fileSearchResults) > 0 { + if len(fileSearchResults) > 0 { btn := setUpUserDirButton(fmt.Sprintf("folder-%s", dir), "", dir, userDirsMap) fileSearchResultFlowBox.Add(btn)