Skip to content

Commit

Permalink
Merge pull request #2 from nwg-piotr/cleaning
Browse files Browse the repository at this point in the history
Code cleanup; support for the XDG_DATA_HOME value; and more
  • Loading branch information
nwg-piotr authored Jun 18, 2021
2 parents bd153b8 + bef9f5d commit be2433f
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 48 deletions.
Binary file modified bin/nwg-drawer
Binary file not shown.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
5 changes: 2 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
37 changes: 20 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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
Expand All @@ -100,7 +93,6 @@ var (
catButtons []*gtk.Button
statusLabel *gtk.Label
status string
mainColumnWidth int
ignore string
)

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
})
Expand Down Expand Up @@ -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)
Expand Down
32 changes: 13 additions & 19 deletions tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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 {
Expand All @@ -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]
}

Expand Down Expand Up @@ -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 := ""
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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:])
Expand Down
26 changes: 18 additions & 8 deletions uicomponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit be2433f

Please sign in to comment.