Skip to content

bytemystery-com/picbutton

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

Getting PicButton

go get github.com/bytemystery-com/picbutton

Import PicButton

import github.com/bytemystery-com/picbutton`

Usage of 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()
}

Screenshots from the example

alt text alt text alt text alt text

Docu

picbutton

import "github.com/bytemystery-com/picbutton"

Index

type PicButton

type PicButton struct {
    widget.BaseWidget

    OnTapped          func()
    OnTappedSecondary func()
    // contains filtered or unexported fields
}

func NewPicButton

func NewPicButton(uImg []byte, dImg []byte, uxImg []byte, dxImg []byte, isToggle bool, tapped func(), tappedSecondary func()) *PicButton

creates a new picture button widget. At least uImg and dImg must be given

func NewPicButtonEx

func NewPicButtonEx(uImg []byte, dImg []byte, uxImg []byte, dxImg []byte, isToggle, hasPadding bool, buttonMask desktop.MouseButton, tapped func(), tappedSecondary func()) *PicButton

creates 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 (*PicButton) CreateRenderer

func (p *PicButton) CreateRenderer() fyne.WidgetRenderer

Widget interface

func (*PicButton) Cursor

func (p *PicButton) Cursor() desktop.Cursor

Cursorable interface

func (*PicButton) FocusGained

func (p *PicButton) FocusGained()

Focusable interface

func (*PicButton) FocusLost

func (p *PicButton) FocusLost()

Focusable interface

func (*PicButton) GetLastKeyModifier

func (p *PicButton) GetLastKeyModifier() fyne.KeyModifier

Get the last keyboard modifier

func (*PicButton) GetLastMouseButton

func (p *PicButton) GetLastMouseButton() desktop.MouseButton

Get the last mouse button

func (*PicButton) IsDown

func (b *PicButton) IsDown() bool

Checks if the button (used in toggle mode) is down

func (*PicButton) IsEnabled

func (b *PicButton) IsEnabled() bool

Checks if the button is enabled

func (*PicButton) MouseDown

func (p *PicButton) MouseDown(ev *desktop.MouseEvent)

Mouseable interface

func (*PicButton) MouseIn

func (p *PicButton) MouseIn(ev *desktop.MouseEvent)

Hoverable interface

func (*PicButton) MouseMoved

func (p *PicButton) MouseMoved(ev *desktop.MouseEvent)

Hoverable interface

func (*PicButton) MouseOut

func (p *PicButton) MouseOut()

Hoverable interface

func (*PicButton) MouseUp

func (p *PicButton) MouseUp(ev *desktop.MouseEvent)

Mouseable interface

func (*PicButton) SetDImg

func (p *PicButton) SetDImg(dImg []byte) error

Sets a new dImg

func (*PicButton) SetDown

func (b *PicButton) SetDown(bDown bool)

Sets the button in down state - no Tapped event is triggered

func (*PicButton) SetDxImg

func (p *PicButton) SetDxImg(dxImg []byte) error

Sets a new dxImg

func (*PicButton) SetEnabled

func (b *PicButton) SetEnabled(bEnabled bool)

Sets the button enabled / disabled

func (*PicButton) SetHooverImg

func (p *PicButton) SetHooverImg(hImgBottom []byte, hImgTop []byte) error

Sets a new hooverImg

func (*PicButton) SetMinSize

func (p *PicButton) SetMinSize(minSize fyne.Size)

Override the automatic from uImg derived minSize

func (*PicButton) SetUImg

func (p *PicButton) SetUImg(uImg []byte) error

Sets a new uImg

func (*PicButton) SetUxImg

func (p *PicButton) SetUxImg(uxImg []byte) error

Sets a new uxImg

func (*PicButton) Tapped

func (p *PicButton) Tapped(ev *fyne.PointEvent)

Tappable interface

func (*PicButton) TappedSecondary

func (p *PicButton) TappedSecondary(ev *fyne.PointEvent)

SecondaryTappable interface

func (*PicButton) TypedKey

func (p *PicButton) TypedKey(ev *fyne.KeyEvent)

Focusable interface

func (*PicButton) TypedRune

func (p *PicButton) TypedRune(r rune)

Focusable interface

type PicButtonRenderer

PicButtonRenderer implements:

  • fyne.WidgetRenderer
type PicButtonRenderer struct {
    // contains filtered or unexported fields
}

func (*PicButtonRenderer) Destroy

func (r *PicButtonRenderer) Destroy()

WidgetRenderer interface

func (*PicButtonRenderer) Layout

func (r *PicButtonRenderer) Layout(size fyne.Size)

WidgetRenderer interface

func (*PicButtonRenderer) MinSize

func (r *PicButtonRenderer) MinSize() fyne.Size

WidgetRenderer interface

func (*PicButtonRenderer) Objects

func (r *PicButtonRenderer) Objects() []fyne.CanvasObject

WidgetRenderer interface

func (*PicButtonRenderer) Refresh

func (r *PicButtonRenderer) Refresh()

WidgetRenderer interface

Generated by gomarkdoc

About

Go PicButton for Fyne - Button widget with images for fyne framework

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors