diff --git a/app.go b/app.go index 22bd5ee..0d130a4 100644 --- a/app.go +++ b/app.go @@ -20,8 +20,8 @@ type app struct { ini *os.File } -func (a *app) getTreeIter(store *gtk.TreeStore) *gtk.TreeIter { - it := store.Append(nil) +func (a *app) getTreeIter(store *gtk.TreeStore, parent *gtk.TreeIter) *gtk.TreeIter { + it := store.Append(parent) store.SetValue(it, 0, a.icon) store.SetValue(it, 1, a.name) if len(a.ex) > 1 { @@ -131,178 +131,3 @@ func (a *app) launchSub(sub int) { cmd.Stderr = os.Stderr cmd.Start() } - -// type appExNode struct { -// ap app -// exInd int -// } -// -// func (a *appExNode) launch() { -// if wine { -// var cmd *exec.Cmd -// if !contains(a.ap.lin, a.ap.ex[a.exInd]) { -// cmd = exec.Command("/bin/sh", "-c", "cd \""+a.ap.dir+"\"; wine \""+a.ap.ex[a.exInd]+"\"") -// } else { -// if comEnbld { -// cmd = exec.Command("/bin/sh", "-c", ". PortableApps/LinuxPACom/common.sh || exit 1;cd \""+a.ap.dir+"\"; \"./"+a.ap.ex[a.exInd]+"\"") -// } else { -// cmd = exec.Command("/bin/sh", "-c", "cd \""+a.ap.dir+"\"; \"./"+a.ap.ex[a.exInd]+"\"") -// } -// } -// cmd.Stdout = os.Stdout -// cmd.Stderr = os.Stderr -// cmd.Start() -// } -// var cmd *exec.Cmd -// if comEnbld { -// cmd = exec.Command("/bin/sh", "-c", ". PortableApps/LinuxPACom/common.sh || exit 1;cd \""+a.ap.dir+"\"; \"./"+a.ap.ex[a.exInd]+"\"") -// } else { -// cmd = exec.Command("/bin/sh", "-c", "cd \""+a.ap.dir+"\"; \"./"+a.ap.ex[a.exInd]+"\"") -// } -// cmd.Stdout = os.Stdout -// cmd.Stderr = os.Stderr -// cmd.Start() -// } - -// func (a *appExNode) Count() int { -// return 0 -// } -// -// func (a *appExNode) NodeAt(int) gxui.TreeNode { -// return nil -// } -// -// func (a *appExNode) ItemIndex(gxui.AdapterItem) int { -// return -1 -// } -// -// func (a *appExNode) Item() gxui.AdapterItem { -// if wine { -// return a.ap.ex[a.exInd] -// } -// return a.ap.lin[a.exInd] -// } -// -// func (a *appExNode) Create(the gxui.Theme) gxui.Control { -// box := the.CreateLinearLayout() -// box.SetDirection(gxui.LeftToRight) -// box.SetVerticalAlignment(gxui.AlignMiddle) -// img := the.CreateImage() -// img.SetTexture(a.ap.icon) -// img.SetExplicitSize(math.Size{H: 32, W: 32}) -// lbl := the.CreateLabel() -// lbl.SetText(a.ap.ex[a.exInd]) -// box.AddChild(img) -// box.AddChild(lbl) -// box.OnDoubleClick(func(gxui.MouseEvent) { -// a.launch() -// }) -// return box -// } - -// func (a *appNode) Count() int { -// if wine { -// if len(a.ex) > 1 { -// return len(a.ap.ex) -// } -// return 0 -// } -// if len(a.ap.lin) > 1 { -// return len(a.ap.lin) -// } -// return 0 -// } -// -// func (a *appNode) NodeAt(i int) gxui.TreeNode { -// return &appExNode{ap: a.ap, exInd: i} -// } -// -// func (a *appNode) ItemIndex(item gxui.AdapterItem) int { -// if wine { -// for i, v := range a.ap.ex { -// if v == item { -// return i -// } -// } -// } else { -// for i, v := range a.ap.lin { -// if v == item { -// return i -// } -// } -// } -// return -1 -// } -// -// func (a *appNode) Item() gxui.AdapterItem { -// return a.ap.name -// } -// -// func (a *appNode) Create(the gxui.Theme) gxui.Control { -// box := the.CreateLinearLayout() -// box.SetDirection(gxui.LeftToRight) -// box.SetPadding(math.CreateSpacing(2)) -// box.SetVerticalAlignment(gxui.AlignMiddle) -// img := the.CreateImage() -// if a.ap.icon != nil { -// img.SetTexture(a.ap.icon) -// } -// img.SetExplicitSize(math.Size{H: 32, W: 32}) -// lbl := the.CreateLabel() -// lbl.SetText(a.ap.name) -// box.AddChild(img) -// box.AddChild(lbl) -// box.OnDoubleClick(func(gxui.MouseEvent) { -// a.launch() -// }) -// return box -// } - -// type catAdap struct { -// gxui.AdapterBase -// cat string -// } -// -// func (a *catAdap) setCat(cat string) { -// a.cat = cat -// a.DataChanged(false) -// } -// -// func (a *catAdap) refresh() { -// a.DataChanged(false) -// } -// -// func (a *catAdap) Count() int { -// if wine { -// return len(master[a.cat]) -// } -// return len(linmaster[a.cat]) -// } -// -// func (a *catAdap) NodeAt(i int) gxui.TreeNode { -// if wine { -// return &appNode{ap: master[a.cat][i]} -// } -// return &appNode{ap: linmaster[a.cat][i]} -// } -// -// func (a *catAdap) Size(gxui.Theme) math.Size { -// return math.Size{H: 34, W: math.MaxSize.W} -// } -// -// func (a *catAdap) ItemIndex(item gxui.AdapterItem) int { -// if wine { -// for i, v := range master[a.cat] { -// if v.name == item { -// return i -// } -// } -// } else { -// for i, v := range linmaster[a.cat] { -// if v.name == item { -// return i -// } -// } -// } -// return -1 -// } diff --git a/main.go b/main.go index ffc74f0..ff44155 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "encoding/gob" "fmt" "os" @@ -20,7 +21,6 @@ var ( wine bool comEnbld bool wineAvail bool - showMsg = true ) func main() { @@ -39,6 +39,7 @@ func uiStart() { } win.SetTitle("LinuxPA") win.Connect("destroy", func() { + savePrefs() gtk.MainQuit() }) win.SetDefaultSize(500, 500) @@ -50,6 +51,27 @@ func uiStart() { gtk.Main() } +func savePrefs() { + fil, err := os.Open("PortableApps/LinuxPACom/Prefs.gob") + if os.IsNotExist(err) { + fil, err = os.Create("PortableApps/LinuxPACom/Prefs.gob") + } + if err != nil { + return + } + enc := gob.NewEncoder(fil) + enc.Encode(wine) +} + +func loadPrefs() { + fil, err := os.Open("PortableApps/LinuxPACom/Prefs.gob") + if err != nil { + return + } + dec := gob.NewDecoder(fil) + dec.Decode(&wine) +} + func contains(arr []string, str string) bool { for _, v := range arr { if v == str { diff --git a/setup.go b/setup.go index e4b9289..a3d6a1e 100644 --- a/setup.go +++ b/setup.go @@ -2,7 +2,6 @@ package main import ( "bufio" - "fmt" _ "image/png" "os" "os/exec" @@ -15,6 +14,7 @@ import ( ) func setup() { + loadPrefs() if _, err := os.Open("PortableApps/LinuxPACom/Wine"); os.IsNotExist(err) { if _, errd := exec.LookPath("wine"); errd == nil { wineAvail = true @@ -33,7 +33,6 @@ func setup() { if _, err = os.Open("PortableApps/LinuxPACom"); err != nil { os.Mkdir("PortableApps/LinuxPACom", 0777) } - fmt.Println(err) _, err = os.Open("PortableApps/LinuxPACom/common.sh") if err == nil { comEnbld = true diff --git a/ui.go b/ui.go index dd73a2f..06e729b 100644 --- a/ui.go +++ b/ui.go @@ -2,14 +2,16 @@ package main import ( "fmt" + "strings" + "github.com/gotk3/gotk3/gdk" "github.com/gotk3/gotk3/glib" "github.com/gotk3/gotk3/gtk" ) +var catIcons = []string{"accessories", "development", "engineering", "games", "graphics", "internet", "multimedia", "office", "other", "science", "system", "utilities"} + func ui(win *gtk.Window) { - ls := getCatRows() - var treeApps []*gtk.TreeIter header, _ := gtk.HeaderBarNew() header.SetShowCloseButton(true) header.SetTitle("LinuxPA") @@ -23,8 +25,8 @@ func ui(win *gtk.Window) { win.SetTitlebar(header) topLvl, _ := gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 0) lrBox, _ := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 5) - catList, _ := gtk.ListBoxNew() - catList.SetActivateOnSingleClick(true) + // catList, _ := gtk.ListBoxNew() + // catList.SetActivateOnSingleClick(true) store, _ := gtk.TreeStoreNew(glib.TYPE_OBJECT, glib.TYPE_STRING) appsList, _ := gtk.TreeViewNewWithModel(store) render, _ := gtk.CellRendererPixbufNew() @@ -34,21 +36,21 @@ func ui(win *gtk.Window) { appsList.AppendColumn(pixColumn) appsList.AppendColumn(txtColumn) appsList.SetHeadersVisible(false) - catList.SetHExpand(true) - catList.SetVExpand(true) + // catList.SetHExpand(true) + // catList.SetVExpand(true) appsList.SetHExpand(true) appsList.SetVExpand(true) - vScrollCat, _ := gtk.AdjustmentNew(0, 0, 0, 0, 0, 0) - hScrollCat, _ := gtk.AdjustmentNew(0, 0, 0, 0, 0, 0) + // vScrollCat, _ := gtk.AdjustmentNew(0, 0, 0, 0, 0, 0) + // hScrollCat, _ := gtk.AdjustmentNew(0, 0, 0, 0, 0, 0) vScrollApp, _ := gtk.AdjustmentNew(0, 0, 0, 0, 0, 0) hScrollApp, _ := gtk.AdjustmentNew(0, 0, 0, 0, 0, 0) - catScrl, _ := gtk.ScrolledWindowNew(hScrollCat, vScrollCat) - catScrl.Add(catList) - catScrl.SetSizeRequest(170, 500) + // catScrl, _ := gtk.ScrolledWindowNew(hScrollCat, vScrollCat) + // catScrl.Add(catList) + // catScrl.SetSizeRequest(170, 500) appScrl, _ := gtk.ScrolledWindowNew(hScrollApp, vScrollApp) appScrl.Add(appsList) appScrl.SetSizeRequest(300, 500) - lrBox.Add(catScrl) + // lrBox.Add(catScrl) lrBox.Add(appScrl) botBox, _ := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 2) wineCheck, _ := gtk.CheckButtonNewWithLabel("Show Windows apps (Wine)") @@ -56,80 +58,80 @@ func ui(win *gtk.Window) { wineCheck.SetSensitive(false) wineCheck.SetTooltipText("Download wine to run windows apps") } + wineCheck.SetActive(wine) wineCheck.Connect("toggled", func() { wine = wineCheck.GetActive() - for i := range ls { - catList.Remove(catList.GetRowAtIndex(i)) - } - ls = getCatRows() - for _, v := range ls { - catList.Add(v) - } - catList.ShowAll() + store.Clear() + getTreeIters(store) }) botBox.Add(wineCheck) topLvl.Add(lrBox) topLvl.PackEnd(botBox, false, true, 0) win.Add(topLvl) - for _, v := range ls { - catList.Prepend(v) - } - catList.Connect("row-selected", func() { - store.Clear() - if catList.GetSelectedRow().GetIndex() >= 0 { - treeApps = make([]*gtk.TreeIter, 0) - if wine { - apps := master[cats[catList.GetSelectedRow().GetIndex()]] - for _, v := range apps { - treeApps = append(treeApps, v.getTreeIter(store)) - } - } else { - apps := linmaster[lin[catList.GetSelectedRow().GetIndex()]] - for _, v := range apps { - treeApps = append(treeApps, v.getTreeIter(store)) - } - } - } - }) appsList.Connect("row-activated", func() { selec, _ := appsList.GetSelection() _, it, ok := selec.GetSelected() if ok { pth, _ := store.GetPath(it) ind := pth.GetIndices() - if len(ind) == 1 { + if len(ind) == 2 { if wine { - app := master[cats[catList.GetSelectedRow().GetIndex()]][ind[0]] + app := master[cats[ind[0]]][ind[1]] app.launch() } else { - app := linmaster[lin[catList.GetSelectedRow().GetIndex()]][ind[0]] + app := linmaster[lin[ind[0]]][ind[1]] app.launch() } - } else if len(ind) == 2 { + } else if len(ind) == 3 { if wine { - app := master[cats[catList.GetSelectedRow().GetIndex()]][ind[0]] - app.launchSub(ind[1]) + app := master[cats[ind[0]]][ind[1]] + app.launchSub(ind[2]) } else { - app := linmaster[lin[catList.GetSelectedRow().GetIndex()]][ind[0]] - app.launchSub(ind[1]) + app := linmaster[lin[ind[0]]][ind[1]] + app.launchSub(ind[2]) } } } }) } -func getCatRows() (out []*gtk.Label) { +func getTreeIters(store *gtk.TreeStore) (out []*gtk.TreeIter) { if wine { for _, v := range cats { - txt, _ := gtk.LabelNew(v) - out = append(out, txt) - fmt.Println(v) + it := store.Append(nil) + if contains(catIcons, strings.ToLower(v)) { + img, _ := gtk.ImageNewFromIconName("applications-"+strings.ToLower(v), gtk.ICON_SIZE_BUTTON) + buf, _ := img.GetPixbuf().ScaleSimple(32, 32, gdk.INTERP_BILINEAR) + store.SetValue(it, 0, buf) + } else { + img, _ := gtk.ImageNewFromIconName("applications-other", gtk.ICON_SIZE_BUTTON) + buf, _ := img.GetPixbuf().ScaleSimple(32, 32, gdk.INTERP_BILINEAR) + store.SetValue(it, 0, buf) + } + store.SetValue(it, 1, v) + for _, v := range master[v] { + v.getTreeIter(store, it) + } + out = append(out, it) } } else { for _, v := range lin { - txt, _ := gtk.LabelNew(v) - out = append(out, txt) fmt.Println(v) + it := store.Append(nil) + // if contains(catIcons, strings.ToLower(v)) { + // img, _ := gtk.ImageNewFromIconName("applications-"+strings.ToLower(v), gtk.ICON_SIZE_BUTTON) + // buf := img.GetPixbuf() + // store.SetValue(it, 0, buf) + // } else { + // img, _ := gtk.ImageNewFromIconName("applications-other", gtk.ICON_SIZE_BUTTON) + // buf, _ := img.GetPixbuf().ScaleSimple(32, 32, gdk.INTERP_BILINEAR) + // store.SetValue(it, 0, buf) + // } + store.SetValue(it, 1, v) + for _, v := range linmaster[v] { + v.getTreeIter(store, it) + } + out = append(out, it) } } return