user-interface - 使用 go 的 exp/shiny 时如何绘制底层小部件?

标签 user-interface go

假设以下小部件布局:

type myLeaf struct {
    node.LeafEmbed
    // some other fields
}

func NewMyLeaf() *myLeaf {
    w := &myLeaf{}
    w.Wrapper = w
    return w
}

func (w *myLeaf) Paint(ctx *node.PaintContext, origin image.Point) error {
    w.Marks.UnmarkNeedsPaint()
    // draw to ctx ...
}

driver.Main() 内部:

leafA := NewMyLeaf()
leafB := NewMyLeaf()

w := widget.NewFlow(widget.AxisVertical,
    widget.NewSheet(leafA),
    widget.NewSheet(leafB),
)

if err := widget.RunWindow(s, w, &widget.RunWindowOptions{
    NewWindowOptions: screen.NewWindowOptions{},
}); err != nil {
    log.Fatal(err)
}

如何从 driver 外部为每个叶子 (myLeaf) 在 widget 树上执行 func (w *myLeaf) Paint(...) error .Main()?

也可以为 leafAleafBw 使用指针。

最佳答案

当我们查找here时我们看到 widget.RunWindow( ... ) 不处理来自调用者的此类外部请求。

解决方法可能是实现一个类似的函数 MyRunWindow( ... ) 并将 Window 分配给一个全局接口(interface)。像这样:

func MyRunWindow(s screen.Screen, root node.Node, opts *RunWindowOptions) (err error) {
    var nwo *screen.NewWindowOptions

    // ... assign window options ...

    if MyWindow, err = s.NewWindow(nwo); err != nil {
        return err
    }

    // event loop, processing 'lifecycle.Event', 'input.Event', etc.
    // and you can process own 'Events' in a type switch
    for {
        e := MyWindow.NextEvent()

        if e = gef.Filter(e); e == nil {
            continue
        }

        switch e := e.(type) {
        case MyEvent:
        // ... call on 'root' here
        }
    }

在调用方,您可以像这样使用您的全局接口(interface):

MyWindow.Send(paint.Event{}) // for a needed call to root.Paint()
MyWindow.Send(MyEvent{})     // for your own event to be processed

关于user-interface - 使用 go 的 exp/shiny 时如何绘制底层小部件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45567710/

相关文章:

Java FileDialog 浏览和读取文件

java - 使用空的gridLayout,但稍后会保存ImageIcon并存储信息。我可以修改尺寸吗?

go - 速率限制特定端点

go - 如何直接在空接口(interface)上调用String方法?

Python GUI 回调函数

JQuery UI 无法与母版页一起使用

java - 如何在android中使用接口(interface)作为响应监听器?

golang gofmt 包重写通配符

go - 在阅读器中重置光标

json - 编码为 json 时转到转换类型