|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "sync" |
| 6 | + "sync/atomic" |
| 7 | + |
| 8 | + fyne "fyne.io/fyne/v2" |
| 9 | + "fyne.io/fyne/v2/app" |
| 10 | + "fyne.io/fyne/v2/container" |
| 11 | + "fyne.io/fyne/v2/data/binding" |
| 12 | + "fyne.io/fyne/v2/widget" |
| 13 | + |
| 14 | + "fyne.io/fyne/v2/dialog" |
| 15 | + |
| 16 | + data "github.com/sarumaj/water-maker/pkg/data" |
| 17 | + handler "github.com/sarumaj/water-maker/pkg/handler" |
| 18 | +) |
| 19 | + |
| 20 | +func main() { |
| 21 | + h := handler.NewHandler() |
| 22 | + |
| 23 | + myApp := app.NewWithID("com.github.sarumaj.domestic.water-maker") |
| 24 | + icon, err := data.Fs.ReadFile("images/icon.png") |
| 25 | + if err != nil { |
| 26 | + panic(err) |
| 27 | + } |
| 28 | + app.SetMetadata( |
| 29 | + fyne.AppMetadata{ |
| 30 | + Name: "Watermaker", |
| 31 | + Build: 1, |
| 32 | + Version: "1.0.0", |
| 33 | + ID: myApp.Metadata().ID, |
| 34 | + Icon: fyne.NewStaticResource("icon", icon), |
| 35 | + }, |
| 36 | + ) |
| 37 | + |
| 38 | + myWindow := myApp.NewWindow(myApp.Metadata().Name) |
| 39 | + myWindow.Resize(fyne.Size{Width: 800, Height: 600}) |
| 40 | + myWindow.SetFixedSize(true) |
| 41 | + |
| 42 | + resolver := func() { |
| 43 | + if err := recover(); err != nil { |
| 44 | + if val, ok := err.(error); ok { |
| 45 | + dialog.ShowError(val, myWindow) |
| 46 | + } else { |
| 47 | + dialog.ShowError(fmt.Errorf("%v", err), myWindow) |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + defer resolver() |
| 52 | + |
| 53 | + progress := widget.NewProgressBar() |
| 54 | + progress.Resize(fyne.Size{Width: 390, Height: 60}) |
| 55 | + progress.Move(fyne.Position{X: 390, Y: 10}) |
| 56 | + |
| 57 | + list := widget.NewListWithData( |
| 58 | + h.Messages, |
| 59 | + func() fyne.CanvasObject { |
| 60 | + return widget.NewLabel("template") |
| 61 | + }, |
| 62 | + func(i binding.DataItem, o fyne.CanvasObject) { |
| 63 | + o.(*widget.Label).Bind(i.(binding.String)) |
| 64 | + }, |
| 65 | + ) |
| 66 | + list.Resize(fyne.Size{Width: 780, Height: 510}) |
| 67 | + list.Move(fyne.Position{X: 10, Y: 80}) |
| 68 | + |
| 69 | + SendMessage := func(msg string) { |
| 70 | + h.Log(msg) |
| 71 | + list.ScrollToBottom() |
| 72 | + } |
| 73 | + |
| 74 | + browseBtn := widget.NewButton("browse", func() { |
| 75 | + defer resolver() |
| 76 | + dialog.ShowFolderOpen(h.Browse, myWindow) |
| 77 | + list.ScrollToBottom() |
| 78 | + }) |
| 79 | + browseBtn.Resize(fyne.Size{Width: 180, Height: 60}) |
| 80 | + browseBtn.Move(fyne.Position{X: 10, Y: 10}) |
| 81 | + |
| 82 | + startBtn := widget.NewButton("start", func() { |
| 83 | + defer resolver() |
| 84 | + if length := len(h.Files); length > 0 { |
| 85 | + wg := &sync.WaitGroup{} |
| 86 | + var cnt uint32 |
| 87 | + for i, length := 0, len(h.Files); i < length; i++ { |
| 88 | + wg.Add(1) |
| 89 | + go func(i int, wg *sync.WaitGroup) { |
| 90 | + file := h.Files[i] |
| 91 | + SendMessage(fmt.Sprintf("Setting watermark on %s\n", file.Base)) |
| 92 | + file.SetWatermark() |
| 93 | + SendMessage(fmt.Sprintf("Set watermark on %s\n", file.Base)) |
| 94 | + progress.SetValue(float64(atomic.LoadUint32(&cnt)+1) / float64(length)) |
| 95 | + atomic.AddUint32(&cnt, 1) |
| 96 | + wg.Done() |
| 97 | + }(i, wg) |
| 98 | + } |
| 99 | + wg.Wait() |
| 100 | + h.Clear() |
| 101 | + dialog.ShowInformation("Completed", "Done", myWindow) |
| 102 | + } else { |
| 103 | + dialog.ShowInformation("Nothing to do", "Select files first", myWindow) |
| 104 | + } |
| 105 | + }) |
| 106 | + startBtn.Resize(fyne.Size{Width: 180, Height: 60}) |
| 107 | + startBtn.Move(fyne.Position{X: 200, Y: 10}) |
| 108 | + |
| 109 | + myWindow.SetContent( |
| 110 | + container.NewWithoutLayout( |
| 111 | + browseBtn, |
| 112 | + startBtn, |
| 113 | + progress, |
| 114 | + list, |
| 115 | + ), |
| 116 | + ) |
| 117 | + |
| 118 | + myWindow.ShowAndRun() |
| 119 | +} |
0 commit comments