105 lines
2.2 KiB
Go
105 lines
2.2 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fyne.io/fyne/v2"
|
|
"fyne.io/fyne/v2/app"
|
|
"fyne.io/fyne/v2/container"
|
|
"fyne.io/fyne/v2/dialog"
|
|
"fyne.io/fyne/v2/theme"
|
|
"fyne.io/fyne/v2/widget"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
var data = [][]string{[]string{"top left", "top right"},
|
|
[]string{"bottom left", "bottom right"}}
|
|
|
|
func updateTime(clock *widget.Label) {
|
|
formatted := time.Now().Format("Time: 03:04:05")
|
|
clock.SetText(formatted)
|
|
}
|
|
|
|
func GetJoke() (string, error) {
|
|
url := "https://icanhazdadjoke.com/"
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
req.Header.Set("Accept", "application/json")
|
|
|
|
client := &http.Client{}
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
// Assuming the response is in the expected format
|
|
var joke struct {
|
|
ID string `json:"id"`
|
|
Joke string `json:"joke"`
|
|
Status int `json:"status"`
|
|
}
|
|
|
|
err = json.Unmarshal(body, &joke)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return joke.Joke, nil
|
|
}
|
|
|
|
func main() {
|
|
myApp := app.New()
|
|
myWindow := myApp.NewWindow("TabContainer Widget")
|
|
clock := widget.NewLabel("")
|
|
updateTime(clock)
|
|
|
|
list := widget.NewTable(
|
|
func() (int, int) {
|
|
return len(data), len(data[0])
|
|
},
|
|
func() fyne.CanvasObject {
|
|
return widget.NewLabel("wide content")
|
|
},
|
|
func(i widget.TableCellID, o fyne.CanvasObject) {
|
|
o.(*widget.Label).SetText(data[i.Row][i.Col])
|
|
})
|
|
|
|
myWindow.SetContent(list)
|
|
tabs := container.NewAppTabs(
|
|
container.NewTabItem("Tab 1", widget.NewLabel("Hello")),
|
|
container.NewTabItem("Tab 2", list),
|
|
container.NewTabItem("Tab 3", clock),
|
|
container.NewTabItem("Tab 4", widget.NewButton("Get Joke", func() {
|
|
joke, err := GetJoke()
|
|
if err != nil {
|
|
dialog.ShowError(errors.New("Failed to get Joke"), myWindow)
|
|
return
|
|
}
|
|
dialog.ShowInformation("Joke", joke, myWindow)
|
|
})),
|
|
)
|
|
|
|
tabs.Append(container.NewTabItemWithIcon("Home", theme.HomeIcon(), widget.NewLabel("Home tab")))
|
|
|
|
tabs.SetTabLocation(container.TabLocationLeading)
|
|
myWindow.SetContent(tabs)
|
|
|
|
go func() {
|
|
for range time.Tick(time.Second) {
|
|
fyne.Do(func() { updateTime(clock) })
|
|
}
|
|
}()
|
|
myWindow.ShowAndRun()
|
|
}
|