Skip to content

Commit

Permalink
add: add theme and size customization in ui
Browse files Browse the repository at this point in the history
  • Loading branch information
jerome.lesaux committed Jul 29, 2024
1 parent a8a59cf commit 0bc355f
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 64 deletions.
30 changes: 0 additions & 30 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -224,34 +224,6 @@ github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANyt
github.com/jackmordaunt/icns/v2 v2.2.1/go.mod h1:6aYIB9eSzyfHHMKqDf17Xrs1zetQPReAkiUSHzdw4cI=
github.com/jeromelesaux/dsk v0.0.0-20230417191026-0e405580626b h1:KMI+CK5VfgVD/k5HmsAprsDOXCujV9tsICJd5l77LPs=
github.com/jeromelesaux/dsk v0.0.0-20230417191026-0e405580626b/go.mod h1:yF2+YgO/6yWlK932z+qKOU8wY5rrbtsYAPlSXHfxIS8=
github.com/jeromelesaux/fyne-io v0.0.0-20240423152922-6b0078cfa196 h1:UtPAfecWsVfoHk9gn/zWyho7w8UVwIrkgwkRaeJN7XM=
github.com/jeromelesaux/fyne-io v0.0.0-20240423152922-6b0078cfa196/go.mod h1:PHMS7sRywpN563NOk5OKuI4LS6dEo7dZFfvApM6z/AE=
github.com/jeromelesaux/fyne-io v0.0.0-20240424201921-5a02d23e5faf h1:C88j4zAYSBfBSO3wAhlx9xYoiRQRS1aLqGLyhQf8jAk=
github.com/jeromelesaux/fyne-io v0.0.0-20240424201921-5a02d23e5faf/go.mod h1:PHMS7sRywpN563NOk5OKuI4LS6dEo7dZFfvApM6z/AE=
github.com/jeromelesaux/fyne-io v0.0.0-20240425064507-610406c0b788 h1:DJghXRtc3RsXPkKAPL5kwCudNAodL/nKJsqva+20WDE=
github.com/jeromelesaux/fyne-io v0.0.0-20240425064507-610406c0b788/go.mod h1:PHMS7sRywpN563NOk5OKuI4LS6dEo7dZFfvApM6z/AE=
github.com/jeromelesaux/fyne-io v0.0.0-20240430213327-8496ab56671d h1:iXqm3/TgOFjRUeT1NkofgGqdMPIhDDSyNA1s/eqjtX0=
github.com/jeromelesaux/fyne-io v0.0.0-20240430213327-8496ab56671d/go.mod h1:PHMS7sRywpN563NOk5OKuI4LS6dEo7dZFfvApM6z/AE=
github.com/jeromelesaux/fyne-io v0.0.0-20240430215740-b0f64d1db0a4 h1:Tcvn54GRDvfJei22ObA1Sdrk21kYYv5pjRrrQ+hP0M4=
github.com/jeromelesaux/fyne-io v0.0.0-20240430215740-b0f64d1db0a4/go.mod h1:PHMS7sRywpN563NOk5OKuI4LS6dEo7dZFfvApM6z/AE=
github.com/jeromelesaux/fyne-io v0.0.0-20240501083012-272db0816d59 h1:6Nnrx2eu2EuFvM04DziVNJyu7QbZKkA9lgk3BGYjcg8=
github.com/jeromelesaux/fyne-io v0.0.0-20240501083012-272db0816d59/go.mod h1:PHMS7sRywpN563NOk5OKuI4LS6dEo7dZFfvApM6z/AE=
github.com/jeromelesaux/fyne-io v0.0.0-20240501091249-bc1d31dc25dd h1:a7donFr2bBtqNvmTyjt9ymCiIko/UV15emLFQklz2V4=
github.com/jeromelesaux/fyne-io v0.0.0-20240501091249-bc1d31dc25dd/go.mod h1:PHMS7sRywpN563NOk5OKuI4LS6dEo7dZFfvApM6z/AE=
github.com/jeromelesaux/fyne-io v0.0.0-20240501192640-7a74c8748a33 h1:vMCQF9AyK5mR6FtYoQ8Vfkt34g/VoB9cvDDhaTd4lYw=
github.com/jeromelesaux/fyne-io v0.0.0-20240501192640-7a74c8748a33/go.mod h1:dsEn41lwXG/HadYC+I6sk0wDVFWeiFKtp+ADjO856QU=
github.com/jeromelesaux/fyne-io v0.0.0-20240517120332-28d3e7af2cd3 h1:SeQ+CwdXUukEP5p8DGD8i84W3toplx3k4wTA1g7pH/0=
github.com/jeromelesaux/fyne-io v0.0.0-20240517120332-28d3e7af2cd3/go.mod h1:dsEn41lwXG/HadYC+I6sk0wDVFWeiFKtp+ADjO856QU=
github.com/jeromelesaux/fyne-io v0.0.0-20240520123534-ad1a2818a6cb h1:QIx9h+oEjSV+jucZOpNTplNw+lm6N7s/ihRlHHNdE8Q=
github.com/jeromelesaux/fyne-io v0.0.0-20240520123534-ad1a2818a6cb/go.mod h1:dsEn41lwXG/HadYC+I6sk0wDVFWeiFKtp+ADjO856QU=
github.com/jeromelesaux/fyne-io v0.0.0-20240521205349-26b19932414c h1:urbYUiEZgp1niP+9qn3asUTXcIaAYyklUvAEIjD56ws=
github.com/jeromelesaux/fyne-io v0.0.0-20240521205349-26b19932414c/go.mod h1:dsEn41lwXG/HadYC+I6sk0wDVFWeiFKtp+ADjO856QU=
github.com/jeromelesaux/fyne-io v0.0.0-20240523193232-976a6c954474 h1:nB/xf8+N6iL8ZjiCfLfsDGjC2o/eVN6xdh55oH4LBqk=
github.com/jeromelesaux/fyne-io v0.0.0-20240523193232-976a6c954474/go.mod h1:dsEn41lwXG/HadYC+I6sk0wDVFWeiFKtp+ADjO856QU=
github.com/jeromelesaux/fyne-io v0.0.0-20240527065929-195841a9a96b h1:efCim3lvP30xqHhc/Y8zYkxevvKbQIxXv/niCHaZpHA=
github.com/jeromelesaux/fyne-io v0.0.0-20240527065929-195841a9a96b/go.mod h1:dsEn41lwXG/HadYC+I6sk0wDVFWeiFKtp+ADjO856QU=
github.com/jeromelesaux/fyne-io v0.0.0-20240527102448-14b1bf5c125c h1:wH2X9jO9XsirsEcagV0gAHs5S34dJw+4BetwkoUpQ2k=
github.com/jeromelesaux/fyne-io v0.0.0-20240527102448-14b1bf5c125c/go.mod h1:dsEn41lwXG/HadYC+I6sk0wDVFWeiFKtp+ADjO856QU=
github.com/jeromelesaux/fyne-io v0.0.0-20240530064356-5128e23c0093 h1:lFxtKgjX+jPsu9xZ68UwuoOmZ0DZuDExe17Kk9pRcm0=
github.com/jeromelesaux/fyne-io v0.0.0-20240530064356-5128e23c0093/go.mod h1:dsEn41lwXG/HadYC+I6sk0wDVFWeiFKtp+ADjO856QU=
github.com/jeromelesaux/m4client v0.0.0-20200309212559-efb59c22369c/go.mod h1:JO0ijl8YXO6FxogJoC3yAFbBjSAnkwFquc8W6C7k94E=
Expand Down Expand Up @@ -364,8 +336,6 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
Expand Down
122 changes: 89 additions & 33 deletions ui/martine-ui/greetings.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package ui

import (
"fmt"
"log"
"os"
"strconv"
"image/color"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)

Expand Down Expand Up @@ -47,38 +46,95 @@ func (m *MartineUI) newGreedings() *fyne.Container {
container.New(
layout.NewHBoxLayout(),
widget.NewLabel("Change application size "),
widget.NewButton("+", func() {
current := os.Getenv("FYNE_SCALE")
c, err := strconv.ParseFloat(current, 32)
if err != nil {
log.Default().Printf("error while getting FYNE_SCALE error [%v]", err)
}
os.Setenv("FYNE_SCALE", fmt.Sprintf("%f", c+0.1))
widget.NewButton("Increase", func() {
m.scale += .03
theme := myTheme{foregroundColor: m.variant, scale: m.scale}
fyne.CurrentApp().Settings().SetTheme(theme)
canvas.Refresh(m.window.Canvas().Content())

}),
widget.NewButton("Decrease", func() {

m.scale -= .03
theme := myTheme{foregroundColor: m.variant, scale: m.scale}
fyne.CurrentApp().Settings().SetTheme(theme)
canvas.Refresh(m.window.Canvas().Content())

}),
widget.NewButton("-", func() {
current := os.Getenv("FYNE_SCALE")
c, err := strconv.ParseFloat(current, 32)
if err != nil {
log.Default().Printf("error while getting FYNE_SCALE error [%v]", err)
widget.NewButton("Change foreground color", func() {

if m.variant == theme.VariantDark {
m.variant = theme.VariantLight
} else {
m.variant = theme.VariantDark
}
os.Setenv("FYNE_SCALE", fmt.Sprintf("%f", c-0.1))
theme := myTheme{foregroundColor: m.variant, scale: fyne.CurrentApp().Settings().Scale()}
fyne.CurrentApp().Settings().SetTheme(theme)
canvas.Refresh(m.window.Canvas().Content())

}),
),
/* container.New(
layout.NewHBoxLayout(),
widget.NewLabel("Font"),
widget.NewButtonWithIcon("Increase", theme.ContentAddIcon(), func() {
a := fyne.CurrentApp()
v := a.Settings().Scale()
v += .2
os.Setenv("FYNE_SCALE", fmt.Sprintf("%.2f", v))
}),
widget.NewButtonWithIcon("Decrease", theme.ContentRemoveIcon(), func() {
a := fyne.CurrentApp()
v := a.Settings().Scale()
v -= .2
os.Setenv("FYNE_SCALE", fmt.Sprintf("%.2f", v))
}),
),*/
)
}

type myTheme struct {
foregroundColor fyne.ThemeVariant
scale float32
}

func (m myTheme) Color(name fyne.ThemeColorName, variant fyne.ThemeVariant) color.Color {
if name == theme.ColorNameBackground {
if m.foregroundColor == theme.VariantLight {
return color.White
}
if variant == theme.VariantLight {
return color.White
}
return color.Black
}

return theme.DefaultTheme().Color(name, m.foregroundColor)
}

func (myTheme) Icon(n fyne.ThemeIconName) fyne.Resource {
return theme.DefaultTheme().Icon(n)
}

func (m myTheme) Size(s fyne.ThemeSizeName) float32 {
switch s {
case theme.SizeNameCaptionText:
return 11 * m.scale
case theme.SizeNameInlineIcon:
return 20 * m.scale
case theme.SizeNamePadding:
return 4 * m.scale
case theme.SizeNameScrollBar:
return 16 * m.scale
case theme.SizeNameScrollBarSmall:
return 3 * m.scale
case theme.SizeNameSeparatorThickness:
return 1 * m.scale
case theme.SizeNameText:
return 14 * m.scale
case theme.SizeNameInputBorder:
return 2 * m.scale
default:
return theme.DefaultTheme().Size(s) * m.scale
}
}

func (myTheme) Font(s fyne.TextStyle) fyne.Resource {
if s.Monospace {
return theme.DefaultTheme().Font(s)
}
if s.Bold {
if s.Italic {
return theme.DefaultTheme().Font(s)
}
return theme.DefaultTheme().Font(s)
}
if s.Italic {
return theme.DefaultTheme().Font(s)
}
return theme.DefaultTheme().Font(s)
}
5 changes: 5 additions & 0 deletions ui/martine-ui/martine_ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ var (
)

type MartineUI struct {
scale float32
variant fyne.ThemeVariant
window fyne.Window
main *menu.ImageMenu
tilemap *menu.TilemapMenu
Expand All @@ -43,7 +45,10 @@ type MartineUI struct {
}

func NewMartineUI() *MartineUI {

m := &MartineUI{
scale: fyne.CurrentApp().Settings().Scale(),
variant: fyne.CurrentApp().Settings().ThemeVariant(),
main: menu.NewImageMenu(),
tilemap: menu.NewTilemapMenu(),
animate: menu.NewAnimateMenu(),
Expand Down
2 changes: 1 addition & 1 deletion ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func main() {
/* main application */
scale := os.Getenv("FYNE_SCALE")
if scale == "" {
os.Setenv("FYNE_SCALE", "0.75")
os.Setenv("FYNE_SCALE", "0.7")
}
mapp := app.NewWithID("Martine @IMPact")
mapp.Settings().SetTheme(theme.DarkTheme()) // nolint:
Expand Down

0 comments on commit 0bc355f

Please sign in to comment.