-
I ported the application management gadget program that used the Unison UI library before, and found that there are several places where I don't know how to adjust the style to the previous appearance, the first is the background color of each widget, if you can change the background color of the button by styling, then each widget has to write a code to set the style, including label, text editor, chooser These widgets need to change the background color, It would be convenient if the appbody had a global setting API that could change the background color of all widgets to light gray with one click, and the other is that the style of grid display does not seem to be able to specify how much each line is displayed, which leads to a long screen width to browse all the widgets, although you can drag the horizontal scrollbar, but the scene of the widget is that it only needs to scroll vertically, which will be more convenient to use. The last puzzle is to extract ico from the Windows executable file and then convert it to png, and finally convert it to svg and set it as the button icon, and then I found that the icon is too small, if the size can be specified, then the parameters of the conversion api should be adjusted during the conversion process instead of manually changing the width and height parameters of each svg file after the conversion is completed, I'm not sure. All styling is still new to me. package main
import (
"cogentcore.org/core/events"
"cogentcore.org/core/gi"
"cogentcore.org/core/grr"
"cogentcore.org/core/icons"
"cogentcore.org/core/styles"
"embed"
"fmt"
"github.com/ddkwork/golibrary/mylog"
"github.com/ddkwork/golibrary/stream"
"github.com/ddkwork/golibrary/stream/cmd"
"github.com/ddkwork/golibrary/widget"
"image/png"
"io/fs"
"os"
"path/filepath"
)
//go:embed svg/*.svg
var myIcons embed.FS
func main() {
icons.AddFS(grr.Log1(fs.Sub(myIcons, "svg")))
paths := getPaths()
go saveIco2Png(paths)
b := gi.NewAppBody("vstart")
b.Style(func(s *styles.Style) {
s.Direction = styles.Row
s.Display = styles.Grid
//s.Overflow.Set(styles.OverflowAuto)
})
for _, path := range paths {
button := gi.NewButton(b).SetText(stream.BaseName(path))
button.SetIcon(icons.Icon(stream.BaseName(path)))
//todo bug 下面的三个的地方放path没有正确的传递进去
button.OnClick(func(e events.Event) {
cmd.Run(button.Text)
})
widget.ButtonStyle(button)
button.AddContextMenu(func(m *gi.Scene) {
o := gi.NewButton(m).SetText("delete").SetIcon(icons.DeleteForever)
widget.ButtonStyle(o)
o.OnClick(func(e events.Event) {
//todo warning
os.Remove(path)
})
gi.NewButton(m).SetText("open dir").SetIcon(icons.FolderOpen).OnClick(func(e events.Event) {
//todo support linux
cmd.Run("explorer " + path) //todo bug
//println("clicked open dir")
})
})
}
widget.NewWindowRunAndWait(b, func(names []string) {
for _, name := range names {
println(name)
}
})
}
func getPaths() []string {
paths := make([]string, 0)
filepath.Walk("d:/app", func(path string, info fs.FileInfo, err error) error {
if filepath.Ext(path) == ".exe" {
path = stream.FixUncPath(path)
paths = append(paths, path)
}
return err
})
return paths
}
func saveIco2Png(paths []string) {
for i, path := range paths {
ExtractPrivateExtractIcons(path)
//这里打印有延时居然把全部图标提取了,之前取了几个就停止了,神奇的bug,看来还是保存到本地靠谱
//可能有重名覆盖了
//需要延时1-2秒?
mylog.Success(fmt.Sprint(i), path)
}
stream.Png2SvgAll("svg")
}
func ExtractPrivateExtractIcons(path string) {
image, ok := stream.ExtractIcon2Image(path)
if !ok {
return
}
p := stream.TrimExtension(path) + ".png"
os.Remove(p)
p = stream.TrimExtension(filepath.Base(path)) + ".png"
p = filepath.Join("svg", p)
fp, err := os.Create(p)
if !mylog.Error(err) {
return
}
if !mylog.Error(png.Encode(fp, image)) {
return
}
mylog.Error(fp.Close())
}
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
In addition, need to add this button animation #739 |
Beta Was this translation helpful? Give feedback.
-
We will support global styling of all widgets soon with #543. To specify how many columns there are in the grid layout, you can specify |
Beta Was this translation helpful? Give feedback.
-
In fact, there is no problem with SVG loading, and the reason for the large display may be the reason why the DPI is set image, err := unison.NewImageFromBytes(file, 0.3)
https://github.com/richardwilkes/unison/blob/main/image.go#L55-L73 // NewImageFromBytes creates a new image from raw bytes.
func NewImageFromBytes(buffer []byte, scale float32) (*Image, error) {
if scale <= 0 {
return nil, errs.New("invalid scale")
}
if len(buffer) < 1 {
return nil, errs.New("no data in input buffer")
}
data := skia.DataNewWithCopy(buffer)
defer skia.DataUnref(data)
img := skia.ImageNewFromEncoded(data)
if img == nil {
return nil, errs.New("unable to decode image data")
}
hash, err := hashImageData(skia.ImageGetWidth(img), skia.ImageGetHeight(img), scale, buffer)
if err != nil {
return nil, errs.Wrap(err)
}
return newImage(img, scale, hash)
} Leave this problem alone for now, because all the functions can be ported to the old project normally, but the style problem will be studied slowly in the future. |
Beta Was this translation helpful? Give feedback.
The icons are smaller because the size of the icons is set in the button styles. You can change that with the following code: