Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple colors for different buttons. #5274

Closed
2 tasks done
lebaoworks opened this issue Nov 18, 2024 · 4 comments
Closed
2 tasks done

Support multiple colors for different buttons. #5274

lebaoworks opened this issue Nov 18, 2024 · 4 comments
Labels
wontfix This will not be worked on

Comments

@lebaoworks
Copy link

lebaoworks commented Nov 18, 2024

Checklist

  • I have searched the issue tracker for open issues that relate to the same feature, before opening a new one.
  • This issue only relates to a single feature. I will open new issues for any other features.

Is your feature request related to a problem?

I want to create a table with multiple buttons, each button having a different color.

Is it possible to construct a solution with the existing API?

As far as I know, the button color can currently only be assigned through the theme.

I've tried https://blogvali.com/button-color-background-image-fyne-gui-golang-tutorial-44/, the color only show at the four small corners of each button.
image

Describe the solution you'd like to see.

Add single field like color to Button struct

@andydotxyz
Copy link
Member

The widget APIs are based on semantics - I.e. what does a coloured button /convey/. For this you can use the Importance field to set the meaning and colours will adjust accordingly.

The blog you posted is out of date - from when buttons were transparent.

You can of course create a simple custom widget that includes a coloured rectangle that is tappable.
Lastly there is a ThemeOverride container that would allow every button to have a different theme.

@lebaoworks
Copy link
Author

lebaoworks commented Nov 25, 2024

@andydotxyz
I have finally set the color of the button using NewThemeOverride.

package main

import (
	"image/color"
	"log"

	"fyne.io/fyne/v2"
)

type CustomTheme struct {
	org   fyne.Theme
	color func(fyne.ThemeColorName, fyne.ThemeVariant) color.Color
	font  func(fyne.TextStyle) fyne.Resource
	icon  func(fyne.ThemeIconName) fyne.Resource
	size  func(fyne.ThemeSizeName) float32
}

func NewCustomTheme(org fyne.Theme) *CustomTheme {
	return &CustomTheme{
		org:   org,
		color: org.Color,
		font:  org.Font,
		icon:  org.Icon,
		size:  org.Size,
	}
}

func (b *CustomTheme) ColorCb(new func(fyne.ThemeColorName, fyne.ThemeVariant) color.Color) *CustomTheme {
	b.color = new
	return b
}

func (b *CustomTheme) FontCb(new func(fyne.TextStyle) fyne.Resource) *CustomTheme {
	b.font = new
	return b
}

func (b *CustomTheme) IconCb(new func(fyne.ThemeIconName) fyne.Resource) *CustomTheme {
	b.icon = new
	return b
}

func (b *CustomTheme) SizeCb(new func(fyne.ThemeSizeName) float32) *CustomTheme {
	b.size = new
	return b
}

func (b *CustomTheme) Color(name fyne.ThemeColorName, variant fyne.ThemeVariant) color.Color {
	log.Printf("name: %v, variant: %v", name, variant)
	return b.color(name, variant)
}

func (b *CustomTheme) Font(style fyne.TextStyle) fyne.Resource {
	log.Printf("style: %v", style)
	return b.font(style)
}

func (b *CustomTheme) Icon(name fyne.ThemeIconName) fyne.Resource {
	log.Printf("name: %v", name)
	return b.icon(name)
}

func (b *CustomTheme) Size(name fyne.ThemeSizeName) float32 {
	log.Printf("name: %v", name)
	return b.size(name)
}
importButtonContainer := container.NewThemeOverride(
		importButton,
		NewCustomTheme(Application.Settings().Theme()).
			ColorCb(func(name fyne.ThemeColorName, variant fyne.ThemeVariant) color.Color {
				switch name {
				case theme.ColorNameButton:
					return color.RGBA{R: 0x2C, G: 0x2C, B: 0x2C, A: 255}
				case "text":
					return color.Black
				default:
					return Application.Settings().Theme().Color(name, variant)
				}
			}),
	)

However, I want to set the color of the text as well, but there is no fyne.ThemeSizeName for text. I tried matching 'text', but it didn't work.

@andydotxyz
Copy link
Member

Maybe you are interested in #4959 then?

@lebaoworks
Copy link
Author

@andydotxyz

Currently, I can work around it by overriding the theme theme.ColorNameForeground to change the button text color.

Thank you for your support, I will close this as completed.

@Jacalz Jacalz added the wontfix This will not be worked on label Nov 28, 2024
@Jacalz Jacalz closed this as not planned Won't fix, can't repro, duplicate, stale Nov 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

3 participants