the question is. i have NewVScroll in the NewVScroll i have NewVBoxLayout in NewVBoxLayout i have NewButton . the problem is when i press the NewButton it should loop over NewVScroll and then loop over NewVBoxLayout to find NewButton . but i don't know how to do it in fyne because i can't loop over var v1 fyne.CanvasObject
the error is : cannot range over v1 (variable of type fyne.CanvasObject)
this is the code
package main
import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/widget"
)
var cEntries *fyne.Container
func FindObject(object fyne.CanvasObject, objects []fyne.CanvasObject) (int, bool) {
for k1, v1 := range objects {
for _, v2 := range v1 {// the problem is here. i can't loop over fyne.CanvasObject
if v2 == object {
return k1, true
}
}
}
return 0, false
}
func w1() *fyne.Container {
wb := widget.NewButton("", nil)
wb.OnTapped = func() {
index, isin := FindObject(wb, cEntries.Objects)
fmt.Println(index, isin)
}
return container.New(layout.NewVBoxLayout(), wb)
}
func main() {
a := app.New()
w := a.NewWindow("")
wbAdd := widget.NewButton("+", nil)
cEntries = container.New(layout.NewVBoxLayout(), wbAdd, w1())
wbAdd.OnTapped = func() {
cEntries.Add(w1())
}
wsEntries := container.NewVScroll(cEntries)
w.SetContent(wsEntries)
w.ShowAndRun()
}
The object is a container, so in general you could iterate v1.(*fyne.Container).Objects.
However it looks like you could avoid looping in your code because the button always refers to an index that is set when the button is added - so you could pass the current index (length) to w1 and then increment it.
you don't need to loop over NewVBoxLayout because it is fyne.CanvasObject then if you want to know the index of the layout that contain this widget NewButton you can do it by just search for NewVBoxLayout in NewVScroll
this is the code
package main
import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/widget"
)
var cEntries *fyne.Container
func FindObject(object fyne.CanvasObject, objects []fyne.CanvasObject) (int, bool) {
for k1, v1 := range objects {
if v1 == object {
return k1, true
}
}
return 0, false
}
func w1() *fyne.Container {
wb := widget.NewButton("", nil)
c := container.New(layout.NewVBoxLayout(), wb)
wb.OnTapped = func() {
index, isin := FindObject(c, cEntries.Objects)
fmt.Println(index, isin)
}
return c
}
func main() {
a := app.New()
w := a.NewWindow("")
wbAdd := widget.NewButton("+", nil)
cEntries = container.New(layout.NewVBoxLayout(), wbAdd, w1())
wbAdd.OnTapped = func() {
cEntries.Add(w1())
}
wsEntries := container.NewVScroll(cEntries)
w.SetContent(wsEntries)
w.ShowAndRun()
}
see in this code you make the container that handle the button and then you search for the container that contain this button
Related
package main
import (
"fmt"
"reflect"
)
type Aservice struct {
}
type Adata struct {
msg string
}
type Bdata struct {
more string
}
var amap map[string]interface{} = make(map[string]interface{}, 1024)
func (aser *Aservice) Bar(data *Adata) error {
return nil
}
func (aser *Aservice) Foo(data *Bdata) error {
return nil
}
func main() {
var ser *Aservice
typeOfService := reflect.TypeOf(ser)
valueOfService := reflect.ValueOf(ser)
for i := 0; i < valueOfService.NumMethod(); i++ {
nref := valueOfService.Method(i).Type().In(0)
fmt.Println("++", nref.Elem().Name())
amap[typeOfService.Method(i).Name] = nref
}
}
Currently "Adata" and "Bdata" can be printed correctly
But I don’t know how to store the empty structure pointers of "Adata" and "Bdata" in amap
No idea for the next step
I want to use Method(i).Name() in amap to store the parameters that need to be passed in for the Method
Based on the suggestions in comments :
package main
import (
"fmt"
"reflect"
)
type Aservice struct {
}
type Adata struct {
msg string
}
type Bdata struct {
more string
}
var amap = map[string]interface{}{}
func (aser *Aservice) Bar(data *Adata) error {
return nil
}
func (aser *Aservice) Foo(data *Bdata) error {
return nil
}
func main() {
var ser *Aservice
typeOfService := reflect.TypeOf(ser)
valueOfService := reflect.ValueOf(ser)
for i := 0; i < valueOfService.NumMethod(); i++ {
nref := valueOfService.Method(i).Type().In(0)
amap[typeOfService.Method(i).Name] = reflect.New(nref.Elem()).Interface()
}
for k, v := range amap {
fmt.Printf("%s %#v\n", k, v)
}
}
Output:
Bar &main.Adata{msg:""}
Foo &main.Bdata{more:""}
Is there a way to list out all functions that uses/returns a specific type?
For example: I'm interested to use the following function.
func ListenAndServe(addr string, handler Handler) error
How can I find out all functions (across all Go packages) that can return a Handler?
I'd write an analysis tool using the x/tools/go/analysis framework. Here's a rough sketch that you can run on any module (it uses go/packages underneath so it fully supports modules):
import (
"bytes"
"fmt"
"go/ast"
"go/format"
"go/token"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/singlechecker"
)
var RtAnalysis = &analysis.Analyzer{
Name: "rtanalysis",
Doc: "finds functions by return type",
Run: run,
}
func main() {
singlechecker.Main(RtAnalysis)
}
func run(pass *analysis.Pass) (interface{}, error) {
for _, file := range pass.Files {
ast.Inspect(file, func(n ast.Node) bool {
if funcTy, ok := n.(*ast.FuncType); ok {
if funcTy.Results != nil {
for _, fl := range funcTy.Results.List {
if tv, ok := pass.TypesInfo.Types[fl.Type]; ok {
if tv.Type.String() == "net/http.Handler" {
ns := nodeString(funcTy, pass.Fset)
fmt.Printf("%s has return of type net/http.Handler\n", ns)
}
}
}
}
}
return true
})
}
return nil, nil
}
// nodeString formats a syntax tree in the style of gofmt.
func nodeString(n ast.Node, fset *token.FileSet) string {
var buf bytes.Buffer
format.Node(&buf, fset, n)
return buf.String()
}
Here is an example:
package main
type State int
const (
Created State = iota
Modified
Deleted
)
func main() {
// Some code here where I need the list
// of all available constants of this type.
}
The use case for this is to create a Finite State Machine (FSM). Being able to get all constants will help me in writing a test case that will ensure that every new value has a corresponding entry in the FSM map.
If your constants are all in an order, you can use this:
type T int
const (
TA T = iota
TB
TC
NumT
)
func AllTs() []T {
ts := make([]T, NumT)
for i := 0; i < int(NumT); i++ {
ts[i] = T(i)
}
return ts
}
You can also cache the output in e.g. init(). This will only work when all constants are initialised with iota in order. If you need something that works for all cases, use an explicit slice.
There is no way to do this at runtime, as the reflect package cannot be used for it. You could define a list:
const(
Created State = iota
Modified
Deleted
)
var allStates = []State{Created, Modified, Deleted}
You may go further and add in a string representation, or any number of other things.
You may be able to generate such a list from the source to make maintenance easier, but I generally don't think that saves enough time to be worth it. There are tools like stringer that can already do some of that.
Since you talking about a test-case I assume you have the type available as well as the file where the constants are defined in. I used for a similar problem the following approach (go playground):
package main
import (
"fmt"
"go/ast"
"go/importer"
"go/parser"
"go/token"
"go/types"
"log"
"strconv"
"strings"
)
type InterestingType uint64
const const_go = `
package p
type InterestingType uint64
const (
A InterestingType = iota << 1
B
C
)
type UninterestingType int
const (
D UninterestingType = iota
E
)
`
func main() {
constantValues := []InterestingType{}
ConstantsOf("InterestingType", const_go, func(v string) {
value, err := strconv.ParseUint(v, 0, 64)
if err != nil {
log.Fatal(err)
}
constantValues = append(
constantValues, InterestingType(value))
})
fmt.Printf("%#v\n", constantValues)
}
func ConstantsOf(ctype string, file string, value func(string)) {
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "const.go", file, 0)
if err != nil {
log.Fatal(err)
}
// Obtain type information.
conf := types.Config{Importer: importer.Default()}
info := &types.Info{
Defs: make(map[*ast.Ident]types.Object),
}
_, err = conf.Check("p", fset, []*ast.File{f}, info)
if err != nil {
log.Fatal(err)
}
for _, d := range f.Decls {
for _, s := range d.(*ast.GenDecl).Specs {
v, ok := s.(*ast.ValueSpec)
if !ok {
continue
}
for _, name := range v.Names {
c := info.ObjectOf(name).(*types.Const)
if strings.HasSuffix(c.Type().String(), ctype) {
value(c.Val().ExactString())
}
}
}
}
}
package main
import (
"fmt"
)
type State int
const (
Created State = iota
Modified
Deleted
)
func (s State) Name() (name string) {
switch s {
case Created:
name = "created"
case Modified:
name = "modified"
case Deleted:
name = "deleted"
}
return
}
func main() {
states := States()
fmt.Println(states)
}
func States() (states []State) {
state := State(0)
for {
name := state.Name()
if name == "" {
break
}
states = append(states, state)
state++
}
return
}
I am doing some testing and trying to test for equality of some function types. I have https://play.golang.org/p/GeE_YJF5lz :
package main
import (
"fmt"
"reflect"
)
type myStruct struct {
f []someFunc
}
type someFunc func(a string) bool
var sf1 someFunc = func(a string) bool {
return true
}
var sf2 someFunc = func(a string) bool {
return false
}
func main() {
a := []someFunc{sf1, sf2}
b := []someFunc{sf1, sf2}
fmt.Println(reflect.DeepEqual(a, b)) // false
m := &myStruct{
f: []someFunc{sf1, sf2},
}
n := &myStruct{
f: []someFunc{sf1, sf2},
}
fmt.Println(reflect.DeepEqual(m, n)) // false
}
I haven't been able to find anything in the docs about comparing functions and know I must be missing something important as to why reflect.DeepEqual doesn't work for them properly.
You can compare function like this, Read more about the representation of functions here: http://golang.org/s/go11func
func funcEqual(a, b interface{}) bool {
av := reflect.ValueOf(&a).Elem()
bv := reflect.ValueOf(&b).Elem()
return av.InterfaceData() == bv.InterfaceData()
}
For example: This is just an idea for your start point.
func main() {
a := []someFunc{sf1, sf2}
b := []someFunc{sf1, sf2}
for idx, f := range a {
fmt.Println("Index: ", idx, funcEqual(f, b[idx]))
}
}
Output:
Index: 0 true
Index: 1 true
Play link: https://play.golang.org/p/6cSVXSYfa5
I am trying to add scroll bars to my application windows in Go using GXUI.
Say I have this code:
package main
import (
"fmt"
"github.com/google/gxui"
"github.com/google/gxui/drivers/gl"
"github.com/google/gxui/samples/flags"
"github.com/google/gxui/themes/dark"
)
func appMain(driver gxui.Driver) {
theme := dark.CreateTheme(driver)
window := theme.CreateWindow(800, 600, "Grid")
window.SetScale(flags.DefaultScaleFactor)
window.OnClose(driver.Terminate)
row := theme.CreateLinearLayout()
row.SetDirection(gxui.LeftToRight)
for c := 0; c < 4; c++ {
col := theme.CreateLinearLayout()
col.SetDirection(gxui.TopToBottom)
for r := 0; r < 100; r++ {
cell := theme.CreateLabel()
cell.SetText(fmt.Sprintf("%d", r*4+c))
col.AddChild(cell)
}
row.AddChild(col)
}
window.AddChild(row)
}
func main() {
gl.StartDriver(appMain)
}
When I run it, I get this window:
How can I get the window to have a scroll bar so that I can view all of the lines?
I wasn't able to do with help ScrollLayout, but I can propose this variant on the basis of examples from github.
package main
import (
"fmt"
"github.com/google/gxui"
"github.com/google/gxui/drivers/gl"
"github.com/google/gxui/math"
"github.com/google/gxui/samples/flags"
"github.com/google/gxui/themes/dark"
)
type customAdapter struct {
gxui.AdapterBase
}
func (a *customAdapter) Count() int {
return 1000
}
func (a *customAdapter) ItemAt(index int) gxui.AdapterItem {
return index
}
func (a *customAdapter) ItemIndex(item gxui.AdapterItem) int {
return item.(int)
}
func (a *customAdapter) Size(theme gxui.Theme) math.Size {
return math.Size{W: 200, H: 25}
}
func (a *customAdapter) Create(theme gxui.Theme, index int) gxui.Control {
layout1 := theme.CreateLinearLayout()
layout1.SetDirection(gxui.LeftToRight)
for c := 0; c < 4; c++ {
col := theme.CreateLinearLayout()
col.SetDirection(gxui.TopToBottom)
cell := theme.CreateLabel()
cell.SetText(fmt.Sprintf("%d", index*4+c))
col.AddChild(cell)
layout1.AddChild(col)
}
return layout1
}
func appMain(driver gxui.Driver) {
theme := dark.CreateTheme(driver)
window := theme.CreateWindow(600, 400, "Grid")
window.BorderPen()
window.SetScale(flags.DefaultScaleFactor)
window.OnClose(driver.Terminate)
adapter := &customAdapter{}
list := theme.CreateList()
list.SetAdapter(adapter)
list.SetOrientation(gxui.Vertical)
window.AddChild(list)
}
func main() {
gl.StartDriver(appMain)
}
Each line is placed in the list,their number and size are specified in the overridden methods. The advantage is that in the list already have the scrollbar.
The following code uses ScrollLayout to add a scroll bar to the window. The trick is to make ScrollLayout the window's child and make the next widget (in this case a LinearLayout) a child of the ScrollLayout.
package main
import (
"fmt"
"github.com/google/gxui"
"github.com/google/gxui/drivers/gl"
"github.com/google/gxui/samples/flags"
"github.com/google/gxui/themes/dark"
)
func appMain(driver gxui.Driver) {
theme := dark.CreateTheme(driver)
window := theme.CreateWindow(800, 600, "Grid")
window.SetScale(flags.DefaultScaleFactor)
window.OnClose(driver.Terminate)
sl := theme.CreateScrollLayout()
row := theme.CreateLinearLayout()
row.SetDirection(gxui.LeftToRight)
for c := 0; c < 4; c++ {
col := theme.CreateLinearLayout()
col.SetDirection(gxui.TopToBottom)
for r := 0; r < 100; r++ {
cell := theme.CreateLabel()
cell.SetText(fmt.Sprintf("%d", r*4+c))
col.AddChild(cell)
}
row.AddChild(col)
}
sl.SetChild(row)
window.AddChild(sl)
}
func main() {
gl.StartDriver(appMain)
}
Note that my computer gave me display issues when I increased the number of rows (the rightmost columns began to get cut off), but other people did not encounter this issue, so it is likely due to a bad installation on my end.