Custom App Icon – Fyne GUI Golang tutorial 25

Custom App Icon – Fyne GUI Golang tutorial 25

Hey, In this tutorial we are going to learn. How to change App ICON in Fyne.

Though it is very easy.

How to change fyne App icon in windows title bar?

You can use the code give below. Where “w” is variable for window.

w.SetIcon(theme.fyneLogo())

How Load custom assets/images in fyne?

You can use load resource function. For example

r, _ := fyne.LoadResourceFromPath(“c:/assets/cat.png”)
Where “r” is a variable short for resource. You can use any.
use this ‘r’ variable to change app icon. For example
w.SetIcon(r)
How to Load image from internet in Fyne & Golang?
You can use “Load Resource From URL STRING” function.
r,_:= LoadResourceFromURLString(urlStr)
Just provide the url and call the function. For Example
r,_:= LoadResourceFromURLString(“https://picsum.photos/200”)
Where “_” is the error. Underscore mean to ignore the value of error here.
so You can use it change icon like given below.
w.SetIcon(r)

Tricky part

The tricky part is use custom image in the icon place holder.

Both source code and resource file is uploaded. which is used for custom icon.

Download Code

Resource

package main
// import fyne
import (
    "io/ioutil"
    "net/http"
    "path/filepath"
    "fyne.io/fyne/v2"
    "fyne.io/fyne/v2/app"
)
func main() {
    //New App
    a := app.New()
    // New Window
    w := a.NewWindow("How to change App ICON")
    // New title bar
    // New App ICON
    // How to use custom ICON ?
    r, _ := LoadResourceFromPath("c:/assets/cat.png")
    w.SetIcon(r)
    // Resize
    w.Resize(fyne.NewSize(400, 400))
    // Show and run
    w.ShowAndRun()
}
// //////// Paste Code here
// Resource represents a single binary resource, such as an image or font.
// A resource has an identifying name and byte array content.
// The serialised path of a resource can be obtained which may result in a
// blocking filesystem write operation.
type Resource interface {
    Name() string
    Content() []byte
}
// StaticResource is a bundled resource compiled into the application.
// These resources are normally generated by the fyne_bundle command included in
// the Fyne toolkit.
type StaticResource struct {
    StaticName    string
    StaticContent []byte
}
// Name returns the unique name of this resource, usually matching the file it
// was generated from.
func (r *StaticResource) Name() string {
    return r.StaticName
}
// Content returns the bytes of the bundled resource, no compression is applied
// but any compression on the resource is retained.
func (r *StaticResource) Content() []byte {
    return r.StaticContent
}
// NewStaticResource returns a new static resource object with the specified
// name and content. Creating a new static resource in memory results in
// sharable binary data that may be serialised to the location returned by
// CachePath().
func NewStaticResource(name string, content []byte) *StaticResource {
    return &StaticResource{
        StaticName:    name,
        StaticContent: content,
    }
}
// LoadResourceFromPath creates a new StaticResource in memory using the contents of the specified file.
func LoadResourceFromPath(path string) (Resource, error) {
    bytes, err := ioutil.ReadFile(filepath.Clean(path))
    if err != nil {
        return nil, err
    }
    name := filepath.Base(path)
    return NewStaticResource(name, bytes), nil
}
// LoadResourceFromURLString creates a new StaticResource in memory using the body of the specified URL.
func LoadResourceFromURLString(urlStr string) (Resource, error) {
    res, err := http.Get(urlStr)
    if err != nil {
        return nil, err
    }
    defer res.Body.Close()
    bytes, err := ioutil.ReadAll(res.Body)
    if err != nil {
        return nil, err
    }
    name := filepath.Base(urlStr)
    return NewStaticResource(name, bytes), nil
}

Download Code
Resource

 

Fyne Golang GUI Course

Tony BB
 

TonyBB is a Coach , marketer, hypnotist and a founder of RSKVF Production who specializes in providing simple, affordable, and easy to use solutions for Life.

Click Here to Leave a Comment Below 0 comments

Leave a Reply: