PicButton for fyne
Implements a button (normal push button or toggle button) with
customer provided pictures for up, down and inactive state
if inactive pictures are missing they will be generated from the provided up and down pictures.
Pictures can be changed on the fly.
You can also specify which mouse button can be used to press / toggle the button.
Also the keyboard keyState and used Mouse button can be retrieved for implementing click + Ctrl
or right click + Shift.
You can also specify if the padding from the theme is used or displaying without a padding.
Hoover function was added.
Author: Reiner Pröls
Licence: MIT
go get github.com/bytemystery-com/picbuttonimport github.com/bytemystery-com/picbutton`button := picbutton.NewPicButton(imgUp, imgDown, imgUpX, imgDownX, false,
func() {
// Do what has to be done by primary mouseclick
},
func() {
// Do what has to be done by secondary mouseclick
})Example:
// Copyright (c) 2025-2016 Reiner Pröls
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// SPDX-License-Identifier: MIT
package main
import (
"embed"
"fmt"
"image/color"
"os"
"github.com/bytemystery-com/picbutton"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/driver/desktop"
"fyne.io/fyne/v2/widget"
)
//go:embed assets/*
var content embed.FS
func main() {
a := app.New()
w := a.NewWindow("PicButton")
w.SetFixedSize(true)
w.SetPadded(false)
w.CenterOnScreen()
imgPlayUp, _ := content.ReadFile("assets/play_u.png")
imgPlayDown, _ := content.ReadFile("assets/play_d.png")
imgPlay2Down, _ := content.ReadFile("assets/play2_d.png")
imgPlayUpX, _ := content.ReadFile("assets/play_ux.png")
imgPlayDownX, _ := content.ReadFile("assets/play_dx.png")
imgStopUp, _ := content.ReadFile("assets/stop_u.png")
imgStopDown, _ := content.ReadFile("assets/stop_d.png")
imgExitUp, _ := content.ReadFile("assets/exit_u.png")
imgExitDown, _ := content.ReadFile("assets/exit_d.png")
imgHoover, _ := content.ReadFile("assets/hoover.png")
imgHooverBottom, _ := content.ReadFile("assets/hoover_b.png")
imgHooverTop, _ := content.ReadFile("assets/hoover_t.png")
var play *picbutton.PicButton
var stop *picbutton.PicButton
var exit1 *picbutton.PicButton
var exit2 *picbutton.PicButton
play = picbutton.NewPicButton(imgPlayUp, imgPlayDown, imgPlayUpX, imgPlayDownX, true,
func() {
fmt.Println("Play click primary", play.GetLastKeyModifier(), play.GetLastMouseButton())
stop.SetEnabled(play.IsDown())
},
func() {
fmt.Println("Play click secondary", play.GetLastKeyModifier(), play.GetLastMouseButton())
stop.SetEnabled(play.IsDown())
})
// desktop.MouseButtonTertiary
stop = picbutton.NewPicButtonEx(imgStopUp, imgStopDown, nil, nil, false, true,
desktop.MouseButtonPrimary|desktop.MouseButtonSecondary|desktop.MouseButtonTertiary,
func() {
str := "primary"
if stop.GetLastMouseButton() == desktop.MouseButtonTertiary {
str = "tertiary"
}
fmt.Println("Stop click", str, stop.GetLastKeyModifier())
if stop.GetLastKeyModifier() == fyne.KeyModifierControl {
play.SetDImg(imgPlay2Down)
} else if stop.GetLastKeyModifier() == fyne.KeyModifierShift {
play.SetDImg(imgPlayDown)
} else {
play.SetDown(false)
stop.SetEnabled(false)
}
},
func() {
fmt.Println("Stop click secondary", stop.GetLastKeyModifier(), stop.GetLastMouseButton())
if stop.GetLastKeyModifier() == fyne.KeyModifierControl {
play.SetDImg(imgPlay2Down)
} else if stop.GetLastKeyModifier() == fyne.KeyModifierShift {
play.SetDImg(imgPlayDown)
} else {
play.SetDown(false)
stop.SetEnabled(false)
}
})
stop.SetEnabled(false)
// without padding
exit1 = picbutton.NewPicButtonEx(imgExitUp, imgExitDown, nil, nil, false, false, 0,
func() {
exit2.SetEnabled(true)
exit1.SetEnabled(false)
}, nil)
// without padding
exit2 = picbutton.NewPicButtonEx(imgExitUp, imgExitDown, nil, nil, false, false, 0,
func() {
os.Exit(0)
}, nil)
exit2.SetEnabled(false)
hoover := picbutton.NewPicButton(imgHoover, imgHoover, nil, nil, false, func() {}, nil)
hoover.SetHooverImg(imgHooverBottom, imgHooverTop)
bg := canvas.NewRectangle(color.NRGBA{R: 192, G: 192, B: 192, A: 255})
sep := widget.NewSeparator()
hbox := container.NewHBox(sep, play, stop, exit1, exit2, hoover, sep)
vbox := container.NewVBox(sep, hbox, sep)
w.SetContent(container.NewStack(bg, vbox))
w.ShowAndRun()
}import "github.com/bytemystery-com/picbutton"- type PicButton
- func NewPicButton(uImg []byte, dImg []byte, uxImg []byte, dxImg []byte, isToggle bool, tapped func(), tappedSecondary func()) *PicButton
- func NewPicButtonEx(uImg []byte, dImg []byte, uxImg []byte, dxImg []byte, isToggle, hasPadding bool, buttonMask desktop.MouseButton, tapped func(), tappedSecondary func()) *PicButton
- func (p *PicButton) CreateRenderer() fyne.WidgetRenderer
- func (p *PicButton) Cursor() desktop.Cursor
- func (p *PicButton) FocusGained()
- func (p *PicButton) FocusLost()
- func (p *PicButton) GetLastKeyModifier() fyne.KeyModifier
- func (p *PicButton) GetLastMouseButton() desktop.MouseButton
- func (b *PicButton) IsDown() bool
- func (b *PicButton) IsEnabled() bool
- func (p *PicButton) MouseDown(ev *desktop.MouseEvent)
- func (p *PicButton) MouseIn(ev *desktop.MouseEvent)
- func (p *PicButton) MouseMoved(ev *desktop.MouseEvent)
- func (p *PicButton) MouseOut()
- func (p *PicButton) MouseUp(ev *desktop.MouseEvent)
- func (p *PicButton) SetDImg(dImg []byte) error
- func (b *PicButton) SetDown(bDown bool)
- func (p *PicButton) SetDxImg(dxImg []byte) error
- func (b *PicButton) SetEnabled(bEnabled bool)
- func (p *PicButton) SetHooverImg(hImgBottom []byte, hImgTop []byte) error
- func (p *PicButton) SetMinSize(minSize fyne.Size)
- func (p *PicButton) SetUImg(uImg []byte) error
- func (p *PicButton) SetUxImg(uxImg []byte) error
- func (p *PicButton) Tapped(ev *fyne.PointEvent)
- func (p *PicButton) TappedSecondary(ev *fyne.PointEvent)
- func (p *PicButton) TypedKey(ev *fyne.KeyEvent)
- func (p *PicButton) TypedRune(r rune)
- type PicButtonRenderer
type PicButton struct {
widget.BaseWidget
OnTapped func()
OnTappedSecondary func()
// contains filtered or unexported fields
}func NewPicButton(uImg []byte, dImg []byte, uxImg []byte, dxImg []byte, isToggle bool, tapped func(), tappedSecondary func()) *PicButtoncreates a new picture button widget. At least uImg and dImg must be given
func NewPicButtonEx(uImg []byte, dImg []byte, uxImg []byte, dxImg []byte, isToggle, hasPadding bool, buttonMask desktop.MouseButton, tapped func(), tappedSecondary func()) *PicButtoncreates a new picture button widget. At least uImg and dImg must be given this function has 2 more parameters than NewPicButton you can switch off the padding and you can define a cutom MouseButtonMask (if you want to use tertiray Mouse button e.g.) If buttonMask is 0 then MouseButtonPrimary / MouseButtonSecondary is used automatically based on tapped != nil and tappedSecondary != nil
func (p *PicButton) CreateRenderer() fyne.WidgetRendererWidget interface
func (p *PicButton) Cursor() desktop.CursorCursorable interface
func (p *PicButton) FocusGained()Focusable interface
func (p *PicButton) FocusLost()Focusable interface
func (p *PicButton) GetLastKeyModifier() fyne.KeyModifierGet the last keyboard modifier
func (p *PicButton) GetLastMouseButton() desktop.MouseButtonGet the last mouse button
func (b *PicButton) IsDown() boolChecks if the button (used in toggle mode) is down
func (b *PicButton) IsEnabled() boolChecks if the button is enabled
func (p *PicButton) MouseDown(ev *desktop.MouseEvent)Mouseable interface
func (p *PicButton) MouseIn(ev *desktop.MouseEvent)Hoverable interface
func (p *PicButton) MouseMoved(ev *desktop.MouseEvent)Hoverable interface
func (p *PicButton) MouseOut()Hoverable interface
func (p *PicButton) MouseUp(ev *desktop.MouseEvent)Mouseable interface
func (p *PicButton) SetDImg(dImg []byte) errorSets a new dImg
func (b *PicButton) SetDown(bDown bool)Sets the button in down state - no Tapped event is triggered
func (p *PicButton) SetDxImg(dxImg []byte) errorSets a new dxImg
func (b *PicButton) SetEnabled(bEnabled bool)Sets the button enabled / disabled
func (p *PicButton) SetHooverImg(hImgBottom []byte, hImgTop []byte) errorSets a new hooverImg
func (p *PicButton) SetMinSize(minSize fyne.Size)Override the automatic from uImg derived minSize
func (p *PicButton) SetUImg(uImg []byte) errorSets a new uImg
func (p *PicButton) SetUxImg(uxImg []byte) errorSets a new uxImg
func (p *PicButton) Tapped(ev *fyne.PointEvent)Tappable interface
func (p *PicButton) TappedSecondary(ev *fyne.PointEvent)SecondaryTappable interface
func (p *PicButton) TypedKey(ev *fyne.KeyEvent)Focusable interface
func (p *PicButton) TypedRune(r rune)Focusable interface
PicButtonRenderer implements:
- fyne.WidgetRenderer
type PicButtonRenderer struct {
// contains filtered or unexported fields
}func (r *PicButtonRenderer) Destroy()WidgetRenderer interface
func (r *PicButtonRenderer) Layout(size fyne.Size)WidgetRenderer interface
func (r *PicButtonRenderer) MinSize() fyne.SizeWidgetRenderer interface
func (r *PicButtonRenderer) Objects() []fyne.CanvasObjectWidgetRenderer interface
func (r *PicButtonRenderer) Refresh()WidgetRenderer interface
Generated by gomarkdoc



