github.com/benoitkugler/goacve@v0.0.0-20201217100549-151ce6e55dc8/client/GUI2/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"fyne.io/fyne"
     7  	"fyne.io/fyne/app"
     8  	"fyne.io/fyne/container"
     9  	"fyne.io/fyne/layout"
    10  	"fyne.io/fyne/theme"
    11  	"fyne.io/fyne/widget"
    12  )
    13  
    14  func main() {
    15  	a := app.New()
    16  	w := a.NewWindow("ACVE-Gestion")
    17  
    18  	w.Resize(fyne.NewSize(600, 600))
    19  	// content := tree()
    20  	ta := table(w.Canvas())
    21  	header := headerTable()
    22  	// layout.NewVBoxLayout()
    23  	content := fyne.NewContainerWithLayout(layout.NewBorderLayout(header, nil, nil, nil),
    24  		header,
    25  		fyne.NewContainerWithLayout(layout.NewMaxLayout(), ta))
    26  	w.SetContent(content)
    27  	w.ShowAndRun()
    28  }
    29  
    30  func tree() fyne.CanvasObject {
    31  	var count int
    32  	childUIDs := func(uid widget.TreeNodeID) (c []widget.TreeNodeID) {
    33  		fmt.Println("isBranch called ", count, "times")
    34  		if uid == "" {
    35  			out := make([]string, 1000)
    36  			for i := range out {
    37  				out[i] = fmt.Sprintf("node_%d", i)
    38  			}
    39  			return out
    40  		} else if uid[0] == 'n' {
    41  			return []string{"kid" + uid}
    42  		} else {
    43  			return nil
    44  		}
    45  	}
    46  	// Return a CanvasObject that can represent a Branch (if branch is true), or a Leaf (if branch is false)
    47  	createNode := func(branch bool) (o fyne.CanvasObject) {
    48  		return widget.NewLabel("")
    49  	}
    50  	// Return true if the given widget.TreeNodeID represents a Branch
    51  	isBranch := func(uid widget.TreeNodeID) (ok bool) {
    52  		count++
    53  		return uid == "" || uid[0] != 'k'
    54  	}
    55  	// Called to update the given CanvasObject to represent the data at the given widget.TreeNodeID
    56  	updateNode := func(uid widget.TreeNodeID, branch bool, node fyne.CanvasObject) {
    57  		node.(*widget.Label).SetText(uid)
    58  	}
    59  
    60  	return widget.NewTree(childUIDs, isBranch, createNode, updateNode)
    61  }
    62  
    63  type headerCell struct {
    64  	*fyne.Container
    65  	label *widget.Label
    66  	icon  *widget.Icon
    67  }
    68  
    69  func newHeaderCell() headerCell {
    70  	out := headerCell{label: widget.NewLabel("000000000"), icon: widget.NewIcon(nil)}
    71  	out.Container = container.NewHBox(out.label, layout.NewSpacer(), out.icon)
    72  	return out
    73  }
    74  
    75  func (h headerCell) Tapped(*fyne.PointEvent) {
    76  
    77  }
    78  
    79  func (h *headerCell) setSorted(sorted bool, reverse bool) {
    80  	if !sorted {
    81  		h.icon.Hide()
    82  	} else {
    83  		if reverse {
    84  			h.icon.SetResource(theme.MoveDownIcon())
    85  		} else {
    86  			h.icon.SetResource(theme.MoveUpIcon())
    87  		}
    88  	}
    89  }
    90  
    91  type withRightClick struct {
    92  	*widget.Label
    93  	cv  fyne.Canvas
    94  	row int
    95  }
    96  
    97  func newL(cv fyne.Canvas) *withRightClick {
    98  	out := &withRightClick{Label: widget.NewLabel("0000000000"), cv: cv}
    99  	out.ExtendBaseWidget(out)
   100  	return out
   101  }
   102  
   103  func (w withRightClick) TappedSecondary(p *fyne.PointEvent) {
   104  	menu := fyne.NewMenu("",
   105  		fyne.NewMenuItem(fmt.Sprintf("test %d", w.row), nil),
   106  		fyne.NewMenuItem(fmt.Sprintf("test %d", w.row), nil),
   107  		fyne.NewMenuItem(fmt.Sprintf("test %d", w.row), nil),
   108  	)
   109  	widget.ShowPopUpMenuAtPosition(menu, w.cv, p.AbsolutePosition)
   110  }
   111  
   112  func table(cv fyne.Canvas) fyne.CanvasObject {
   113  	length := func() (int, int) { return 1000, 10 }
   114  	create := func() fyne.CanvasObject {
   115  		return newL(cv)
   116  	}
   117  	update := func(cellID widget.TableCellID, w fyne.CanvasObject) {
   118  		l := w.(*withRightClick)
   119  		l.SetText(fmt.Sprintf("%v", cellID))
   120  		l.TextStyle.Bold = cellID.Row == 0
   121  		l.row = cellID.Row
   122  	}
   123  	return widget.NewTable(length, create, update)
   124  }
   125  
   126  func headerTable() *widget.Table {
   127  	length := func() (int, int) { return 1, 10 }
   128  	create := func() fyne.CanvasObject {
   129  		return newHeaderCell()
   130  	}
   131  	update := func(cellID widget.TableCellID, w fyne.CanvasObject) {
   132  		l := w.(headerCell)
   133  		l.label.SetText(fmt.Sprintf("col %d", cellID.Col))
   134  	}
   135  	return widget.NewTable(length, create, update)
   136  }