func createIconButton(driver gxui.Driver, theme gxui.Theme, iconPath string) gxui.Button { button := theme.CreateButton() button.SetType(gxui.PushButton) fileBytes, err := assets.Asset(iconPath) if err != nil { log.Printf("Error: Failed to read asset %s: %s", iconPath, err) return button } f := bytes.NewBuffer(fileBytes) src, _, err := image.Decode(f) if err != nil { log.Printf("Error: Failed to decode image %s: %s", iconPath, err) return button } src = resize.Resize(24, 24, src, resize.Bilinear) rgba := image.NewRGBA(src.Bounds()) draw.Draw(rgba, src.Bounds(), src, image.ZP, draw.Src) texture := driver.CreateTexture(rgba, 1) icon := theme.CreateImage() icon.SetTexture(texture) button.AddChild(icon) return button }
func appMain(driver gxui.Driver) { args := flag.Args() if len(args) != 1 { fmt.Print("usage: image_viewer image-path\n") os.Exit(1) } file := args[0] f, err := os.Open(file) if err != nil { fmt.Printf("Failed to open image '%s': %v\n", file, err) os.Exit(1) } source, _, err := image.Decode(f) if err != nil { fmt.Printf("Failed to read image '%s': %v\n", file, err) os.Exit(1) } theme := flags.CreateTheme(driver) img := theme.CreateImage() mx := source.Bounds().Max window := theme.CreateWindow(mx.X, mx.Y, "Image viewer") window.SetScale(flags.DefaultScaleFactor) window.AddChild(img) // Copy the image to a RGBA format before handing to a gxui.Texture rgba := image.NewRGBA(source.Bounds()) draw.Draw(rgba, source.Bounds(), source, image.ZP, draw.Src) texture := driver.CreateTexture(rgba, 1) img.SetTexture(texture) window.OnClose(driver.Terminate) }