github.com/Seikaijyu/gio@v0.0.1/widget/example_test.go (about)

     1  // SPDX-License-Identifier: Unlicense OR MIT
     2  
     3  package widget_test
     4  
     5  import (
     6  	"fmt"
     7  	"image"
     8  
     9  	"github.com/Seikaijyu/gio/f32"
    10  	"github.com/Seikaijyu/gio/io/pointer"
    11  	"github.com/Seikaijyu/gio/io/router"
    12  	"github.com/Seikaijyu/gio/io/transfer"
    13  	"github.com/Seikaijyu/gio/layout"
    14  	"github.com/Seikaijyu/gio/op"
    15  	"github.com/Seikaijyu/gio/op/clip"
    16  	"github.com/Seikaijyu/gio/widget"
    17  )
    18  
    19  func ExampleClickable_passthrough() {
    20  	// When laying out clickable widgets on top of each other,
    21  	// pointer events can be passed down for the underlying
    22  	// widgets to pick them up.
    23  	var button1, button2 widget.Clickable
    24  	var r router.Router
    25  	gtx := layout.Context{
    26  		Ops:         new(op.Ops),
    27  		Constraints: layout.Exact(image.Pt(100, 100)),
    28  		Queue:       &r,
    29  	}
    30  
    31  	// widget lays out two buttons on top of each other.
    32  	widget := func() {
    33  		content := func(gtx layout.Context) layout.Dimensions { return layout.Dimensions{Size: gtx.Constraints.Min} }
    34  		button1.Layout(gtx, content)
    35  		// button2 completely covers button1, but pass-through allows pointer
    36  		// events to pass through to button1.
    37  		defer pointer.PassOp{}.Push(gtx.Ops).Pop()
    38  		button2.Layout(gtx, content)
    39  	}
    40  
    41  	// The first layout and call to Frame declare the Clickable handlers
    42  	// to the input router, so the following pointer events are propagated.
    43  	widget()
    44  	r.Frame(gtx.Ops)
    45  	// Simulate one click on the buttons by sending a Press and Release event.
    46  	r.Queue(
    47  		pointer.Event{
    48  			Source:   pointer.Mouse,
    49  			Buttons:  pointer.ButtonPrimary,
    50  			Kind:     pointer.Press,
    51  			Position: f32.Pt(50, 50),
    52  		},
    53  		pointer.Event{
    54  			Source:   pointer.Mouse,
    55  			Buttons:  pointer.ButtonPrimary,
    56  			Kind:     pointer.Release,
    57  			Position: f32.Pt(50, 50),
    58  		},
    59  	)
    60  
    61  	if button1.Clicked(gtx) {
    62  		fmt.Println("button1 clicked!")
    63  	}
    64  	if button2.Clicked(gtx) {
    65  		fmt.Println("button2 clicked!")
    66  	}
    67  
    68  	// Output:
    69  	// button1 clicked!
    70  	// button2 clicked!
    71  }
    72  
    73  func ExampleDraggable_Layout() {
    74  	var r router.Router
    75  	gtx := layout.Context{
    76  		Ops:         new(op.Ops),
    77  		Constraints: layout.Exact(image.Pt(100, 100)),
    78  		Queue:       &r,
    79  	}
    80  	// mime is the type used to match drag and drop operations.
    81  	// It could be left empty in this example.
    82  	mime := "MyMime"
    83  	drag := &widget.Draggable{Type: mime}
    84  	var drop int
    85  	// widget lays out the drag and drop handlers and processes
    86  	// the transfer events.
    87  	widget := func() {
    88  		// Setup the draggable widget.
    89  		w := func(gtx layout.Context) layout.Dimensions {
    90  			sz := image.Pt(10, 10) // drag area
    91  			return layout.Dimensions{Size: sz}
    92  		}
    93  		drag.Layout(gtx, w, w)
    94  		// drag must respond with an Offer event when requested.
    95  		// Use the drag method for this.
    96  		if m, ok := drag.Update(gtx); ok {
    97  			drag.Offer(gtx.Ops, m, offer{Data: "hello world"})
    98  		}
    99  
   100  		// Setup the area for drops.
   101  		ds := clip.Rect{
   102  			Min: image.Pt(20, 20),
   103  			Max: image.Pt(40, 40),
   104  		}.Push(gtx.Ops)
   105  		transfer.TargetOp{
   106  			Tag:  &drop,
   107  			Type: mime, // this must match the drag Type for the drop to succeed
   108  		}.Add(gtx.Ops)
   109  		ds.Pop()
   110  		// Check for the received data.
   111  		for _, ev := range gtx.Events(&drop) {
   112  			switch e := ev.(type) {
   113  			case transfer.DataEvent:
   114  				data := e.Open()
   115  				fmt.Println(data.(offer).Data)
   116  			}
   117  		}
   118  	}
   119  	// Register and lay out the widget.
   120  	widget()
   121  	r.Frame(gtx.Ops)
   122  
   123  	// Send drag and drop gesture events.
   124  	r.Queue(
   125  		pointer.Event{
   126  			Kind:     pointer.Press,
   127  			Position: f32.Pt(5, 5), // in the drag area
   128  		},
   129  		pointer.Event{
   130  			Kind:     pointer.Move,
   131  			Position: f32.Pt(5, 5), // in the drop area
   132  		},
   133  		pointer.Event{
   134  			Kind:     pointer.Release,
   135  			Position: f32.Pt(30, 30), // in the drop area
   136  		},
   137  	)
   138  	// Let the widget process the events.
   139  	widget()
   140  	r.Frame(gtx.Ops)
   141  
   142  	// Process the transfer.DataEvent.
   143  	widget()
   144  
   145  	// Output:
   146  	// hello world
   147  }
   148  
   149  type offer struct {
   150  	Data string
   151  }
   152  
   153  func (offer) Read([]byte) (int, error) { return 0, nil }
   154  func (offer) Close() error             { return nil }