You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

181 lines
4.1 KiB

package main
import (
"github.com/gotk3/gotk3/gtk"
"github.com/henvic/browser"
"log"
"os"
"strconv"
"fmt"
"strings"
)
var (
mainBuilder *gtk.Builder
AccelGroup *gtk.AccelGroup
MainWindow *gtk.Window
SettingsPath string = GetSettingsDir()
firstRun bool = !SettingsFileExists()
)
func init() {
// init GTK
gtk.Init(&os.Args)
// set up our main GTKBuilder instance
mainBuilder, _ = gtk.BuilderNew()
if err := mainBuilder.AddFromFile("glade/main.glade"); err != nil {
log.Fatal("couldn't load glade file", err)
}
AccelGroup, _ = gtk.AccelGroupNew()
}
func main() {
// go ahead and set up our main window but don't show it
setupMainWindow()
// if this is our first run (a settings file wasn't found)
// then we display our first run dialog
if firstRun {
displayFirstRun()
} else {
MainWindow.ShowAll()
}
gtk.Main()
}
func displayFirstRun() {
loginAccel, _ := gtk.AccelGroupNew()
obj, _ := mainBuilder.GetObject("LoginPopup")
popup := obj.(*gtk.Dialog)
popup.AddAccelGroup(loginAccel)
popup.Connect("destroy", func() {
if firstRun {
gtk.MainQuit()
} else {
popup.Hide()
}
})
obj, _ = mainBuilder.GetObject("LoginCancelBtn")
cancelBtn := obj.(*gtk.Button)
cancelBtn.Connect("clicked", func() {
if firstRun {
gtk.MainQuit()
} else {
popup.Hide()
}
})
obj, _ = mainBuilder.GetObject("LoginAuthBtn")
authBtn := obj.(*gtk.Button)
addAccelerator(&authBtn.Bin.Container.Widget, loginAccel, "Return", "clicked")
// hook up our login button
authBtn.Connect("clicked", func() {
// go ahead and get a ref to our auth code entry
// because we need it either way
obj, _ := mainBuilder.GetObject("LoginAuthEntry")
authEntry := obj.(*gtk.Entry)
// if the auth entry isn't visible then we
// know that the user doesn't have their auth code
// so we make sure to unhide the auth stuff first
// before we try and get the code
if !authEntry.GetVisible() {
// get our entry and the text it contained
obj, _ = mainBuilder.GetObject("LoginInstanceEntry")
entry := obj.(*gtk.Entry)
text, _ := entry.GetText()
// show the auth code stuff
obj, _ := mainBuilder.GetObject("LoginAuthLbl")
lbl := obj.(*gtk.Label)
authEntry.Show()
lbl.Show()
// did the text start with https scheme?
// if not we fix it internally
if !strings.HasPrefix(text, "https://") {
last := strings.LastIndex(text, "/")
if last > -1 {
text = "https://" + text[last + 1:]
} else {
text = "https://" + text
}
}
// for debugging
//fmt.Println(text)
// this needs to be replaced with the
// generated auth code URL
browser.OpenURL(text)
} else {
// here we're assuming that the user has entered in the
// auth code so we grab it
text, _ := authEntry.GetText()
fmt.Println(text)
}
})
popup.ShowAll()
}
func setupMainWindow() {
obj, _ := mainBuilder.GetObject("MainWindow")
MainWindow = obj.(*gtk.Window)
MainWindow.AddAccelGroup(AccelGroup)
MainWindow.Connect("destroy", func() {
gtk.MainQuit()
})
obj, _ = mainBuilder.GetObject("NewTootBtn")
btn := obj.(*gtk.ToolButton)
btn.Connect("clicked", func() {
fmt.Println("new toot window!")
})
addAccelerator(&btn.Bin.Container.Widget, AccelGroup,
"<Ctrl>Return", "clicked")
MainWindow.Hide()
}
func addRow(bldr *gtk.Builder, i int) {
// make a new builder so we can pull a 'fresh' copy
// of our template
builder, _ := gtk.BuilderNewFromFile("glade/main.glade")
tmplObj, _ := builder.GetObject("TootTemplate")
listObj, _ := bldr.GetObject("TootList") // make sure we get the old
// listBox instead
contentObj, _ := builder.GetObject("TootContent")
tmpl := tmplObj.(*gtk.Box)
tootList := listObj.(*gtk.ListBox)
content := contentObj.(*gtk.Label)
content.SetLabel("This is a test toot number " + strconv.FormatInt(int64(i), 10))
row, _ := gtk.ListBoxRowNew()
row.Add(tmpl)
row.ShowAll()
tootList.Prepend(row)
}
func addAccelerator(w *gtk.Widget, accel *gtk.AccelGroup, controlString, signal string) {
key, mod := gtk.AcceleratorParse(controlString)
w.AddAccelerator(signal, accel, key, mod, gtk.ACCEL_VISIBLE)
}