Installer/main.go

234 lines
6.9 KiB
Go
Raw Normal View History

2024-07-07 10:07:11 +00:00
package main
import (
"fmt"
"image"
_ "image/jpeg"
"os"
"path/filepath"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)
func main() {
a := app.New()
ApplyTheme(a) // Apply the theme here
w := a.NewWindow("Fyne Installer")
// Load Image
imagePath := "test.jpg"
file, err := os.Open(imagePath)
if err != nil {
dialog.ShowError(fmt.Errorf("failed to load image: %w", err), w)
return
}
defer file.Close()
img, _, err := image.Decode(file)
if err != nil {
dialog.ShowError(fmt.Errorf("failed to decode image: %w", err), w)
return
}
logo := canvas.NewImageFromImage(img)
logo.FillMode = canvas.ImageFillContain
logo.SetMinSize(fyne.NewSize(300, 300)) // Set a minimum size for the image
// Step 1: Language Selection
languageLabel := widget.NewLabelWithStyle("Select the setup language:", fyne.TextAlignLeading, fyne.TextStyle{})
languageSelect := widget.NewSelect([]string{
"English (United States)",
"Deutsch (Deutschland)",
"Italiano (Italia)",
"日本語 (日本)",
"Español (España)",
"Français (France)",
"Nederlands (Nederland)",
"中文(简体) (中国)",
}, func(value string) {
fmt.Println("Selected language:", value)
})
languageSelect.SetSelected("English (United States)")
// Navigation Buttons
nextButton := widget.NewButtonWithIcon("Next", theme.NavigateNextIcon(), func() {
w.SetContent(step2(a, w))
})
cancelButton := widget.NewButtonWithIcon("Cancel", theme.CancelIcon(), func() {
a.Quit()
})
// Step 1 Layout
step1Content := container.NewVBox(
widget.NewLabelWithStyle("Welcome to the Fyne Installer", fyne.TextAlignLeading, fyne.TextStyle{Bold: true}),
widget.NewLabelWithStyle("Please select the setup language and click 'Next'", fyne.TextAlignLeading, fyne.TextStyle{}),
languageLabel,
languageSelect,
layout.NewSpacer(),
container.NewHBox(layout.NewSpacer(), nextButton, cancelButton),
)
// Main Layout
mainLayout := container.NewBorder(nil, nil, logo, nil, step1Content)
w.SetContent(mainLayout)
w.Resize(fyne.NewSize(800, 400))
w.CenterOnScreen()
w.ShowAndRun()
}
func step2(a fyne.App, w fyne.Window) fyne.CanvasObject {
// Load Image
imagePath := "test2.jpg"
file, err := os.Open(imagePath)
if err != nil {
dialog.ShowError(fmt.Errorf("failed to load image: %w", err), w)
return nil
}
defer file.Close()
img, _, err := image.Decode(file)
if err != nil {
dialog.ShowError(fmt.Errorf("failed to decode image: %w", err), w)
return nil
}
logo := canvas.NewImageFromImage(img)
logo.FillMode = canvas.ImageFillContain
logo.SetMinSize(fyne.NewSize(300, 300)) // Set a minimum size for the image
// Step 2: Installation Path Selection
info := widget.NewLabelWithStyle("Please select the installation path and click 'Install'", fyne.TextAlignLeading, fyne.TextStyle{})
pathEntry := widget.NewEntry()
pathEntry.SetPlaceHolder("Select installation path...")
selectPath := widget.NewButtonWithIcon("Browse", theme.FolderOpenIcon(), func() {
dialog.ShowFolderOpen(func(uri fyne.ListableURI, err error) {
if err != nil {
dialog.ShowError(err, w)
return
}
if uri != nil {
pathEntry.SetText(uri.Path())
}
}, w)
})
progressBar := widget.NewProgressBar()
progressBar.Hide()
installButton := widget.NewButtonWithIcon("Install", theme.ConfirmIcon(), func() {
installPath := pathEntry.Text
if installPath == "" {
dialog.ShowError(fmt.Errorf("please select an installation path"), w)
return
}
progressBar.Show()
progressBar.SetValue(0)
go installApplication(installPath, progressBar)
})
backButton := widget.NewButtonWithIcon("Back", theme.NavigateBackIcon(), func() {
w.SetContent(step1(a, w))
})
cancelButton := widget.NewButtonWithIcon("Cancel", theme.CancelIcon(), func() {
a.Quit()
})
// Step 2 Layout
step2Content := container.NewVBox(
widget.NewLabelWithStyle("Installation Setup", fyne.TextAlignLeading, fyne.TextStyle{Bold: true}),
info,
container.NewHBox(pathEntry, selectPath),
progressBar,
layout.NewSpacer(),
container.NewHBox(layout.NewSpacer(), backButton, installButton, cancelButton),
)
return container.NewBorder(nil, nil, logo, nil, step2Content)
}
func step1(a fyne.App, w fyne.Window) fyne.CanvasObject {
// Load Image
imagePath := "test2.jpg"
file, err := os.Open(imagePath)
if err != nil {
dialog.ShowError(fmt.Errorf("failed to load image: %w", err), w)
return nil
}
defer file.Close()
img, _, err := image.Decode(file)
if err != nil {
dialog.ShowError(fmt.Errorf("failed to decode image: %w", err), w)
return nil
}
logo := canvas.NewImageFromImage(img)
logo.FillMode = canvas.ImageFillContain
logo.SetMinSize(fyne.NewSize(300, 300)) // Set a minimum size for the image
// Step 1: Language Selection
languageLabel := widget.NewLabelWithStyle("Select the setup language:", fyne.TextAlignLeading, fyne.TextStyle{})
languageSelect := widget.NewSelect([]string{
"English (United States)",
"Deutsch (Deutschland)",
"Italiano (Italia)",
"日本語 (日本)",
"Español (España)",
"Français (France)",
"Nederlands (Nederland)",
"中文(简体) (中国)",
}, func(value string) {
fmt.Println("Selected language:", value)
})
languageSelect.SetSelected("English (United States)")
// Navigation Buttons
nextButton := widget.NewButtonWithIcon("Next", theme.NavigateNextIcon(), func() {
w.SetContent(step2(a, w))
})
cancelButton := widget.NewButtonWithIcon("Cancel", theme.CancelIcon(), func() {
a.Quit()
})
// Step 1 Layout
step1Content := container.NewVBox(
widget.NewLabelWithStyle("Welcome to the Fyne Installer", fyne.TextAlignLeading, fyne.TextStyle{Bold: true}),
widget.NewLabelWithStyle("Please select the setup language and click 'Next'", fyne.TextAlignLeading, fyne.TextStyle{}),
languageLabel,
languageSelect,
layout.NewSpacer(),
container.NewHBox(layout.NewSpacer(), nextButton, cancelButton),
)
return container.NewBorder(nil, nil, logo, nil, step1Content)
}
func installApplication(path string, progressBar *widget.ProgressBar) {
// Simulate installation process
for i := 0; i <= 10; i++ {
progressBar.SetValue(float64(i) / 10)
// Simulate time-consuming installation step
time.Sleep(500 * time.Millisecond)
}
// Create a file to simulate an installation
file, err := os.Create(filepath.Join(path, "installed.txt"))
if err != nil {
dialog.ShowError(err, fyne.CurrentApp().Driver().AllWindows()[0])
return
}
defer file.Close()
file.WriteString("Installation complete!")
progressBar.SetValue(1)
dialog.ShowInformation("Installation Complete", "The application was successfully installed.", fyne.CurrentApp().Driver().AllWindows()[0])
}