Files
Syndie_GUI/gui.go

101 lines
2.4 KiB
Go
Raw Normal View History

2020-07-19 22:25:17 -04:00
package main
import (
"io/ioutil"
"log"
2020-07-23 14:12:45 -04:00
"os"
"fyne.io/fyne"
2020-07-19 22:25:17 -04:00
"fyne.io/fyne/app"
"fyne.io/fyne/dialog"
"fyne.io/fyne/layout"
2020-07-19 22:25:17 -04:00
"fyne.io/fyne/widget"
"github.com/kpetku/syndie-core/fetcher"
2020-07-19 22:25:17 -04:00
)
2020-07-25 19:55:40 -04:00
// GUI contains various GUI configuration options
2020-07-19 22:25:17 -04:00
type GUI struct {
db *database
window fyne.Window
2020-07-25 15:59:57 -04:00
channelPane *widget.Box
threadPane *widget.Box
contentPane *widget.Box
channelList *widget.ScrollContainer
messageList *widget.ScrollContainer
contentArea *widget.ScrollContainer
pagination int
selectedChannel string
2020-07-25 15:59:57 -04:00
channelNeedle int
2020-07-21 03:26:52 -04:00
selectedMessage int
2020-07-19 22:25:17 -04:00
}
2020-07-25 19:55:40 -04:00
// NewGUI creates a new GUI
2020-07-19 22:25:17 -04:00
func NewGUI() *GUI {
return new(GUI)
}
2020-07-25 19:55:40 -04:00
// Start launches a new syndie-gui application
2020-07-19 22:25:17 -04:00
func (client *GUI) Start(path string) {
2020-07-25 19:55:40 -04:00
client.db = newDatabase()
2020-07-19 22:25:17 -04:00
client.db.openDB(path)
2020-07-23 14:16:14 -04:00
client.db.reload()
2020-07-19 22:25:17 -04:00
a := app.New()
client.window = a.NewWindow("Syndie GUI")
client.loadMainMenu()
2020-07-25 15:59:57 -04:00
client.applyOptions()
client.repaint()
2020-07-21 22:13:04 -04:00
client.window.Resize(fyne.NewSize(800, 600))
client.window.ShowAndRun()
}
func (client *GUI) repaint() {
client.channelList = widget.NewVScrollContainer(client.renderChannelList())
2020-07-25 15:59:57 -04:00
client.messageList = widget.NewVScrollContainer(client.renderThreadList(client.channelNeedle))
2020-07-21 03:26:52 -04:00
client.contentArea = widget.NewVScrollContainer(client.renderContentArea())
client.window.SetContent(fyne.NewContainerWithLayout(layout.NewGridLayout(3), client.channelList, client.messageList, client.contentArea))
2020-07-19 22:25:17 -04:00
}
func (client *GUI) loadMainMenu() {
main := fyne.NewMenu("File",
2020-07-23 15:22:00 -04:00
fyne.NewMenuItem("Fetch", func() {
client.fetchFromArchiveServer()
}),
)
client.window.SetMainMenu(fyne.NewMainMenu(main))
}
func (client *GUI) fetchFromArchiveServer() {
content := widget.NewLabel("Press Fetch to sync from http://localhost:8080/")
dialog.NewCustomConfirm("Fetch", "Fetch", "Cancel", content, client.fetch, client.window)
}
func (client *GUI) fetch(fetch bool) {
dir, err := ioutil.TempDir("", "syndie")
if err != nil {
log.Fatalf("Unable to create a temporary directory: %s", err)
}
2020-07-23 14:12:45 -04:00
defer os.RemoveAll(dir)
if fetch {
2020-07-25 21:21:15 -04:00
progress := dialog.NewProgressInfinite("Fetching", "Fetching from http://localhost:8080/", client.window)
f := fetcher.New("http://localhost:8080/", dir, 60, 50)
2020-07-25 21:21:15 -04:00
progress.Show()
err := f.RemoteFetch()
if err != nil {
progress.Hide()
dialog.NewError(err, client.window)
}
2020-07-23 14:16:14 -04:00
client.db.reload()
2020-07-25 21:21:15 -04:00
progress.Hide()
client.repaint()
}
}
2020-07-25 15:59:57 -04:00
func (client *GUI) applyOptions() {
client.pagination = 25
}