|
| 1 | +// Program 25671 demonstrates a bug in shiny/windriver (#25671) |
| 2 | +// |
| 3 | +// The program creates a main window (blue). |
| 4 | +// For any click into the window and a green client window appears. |
| 5 | +// Pressing the ESC key asks shiny to close the current window |
| 6 | +// by sending a lifecycle event. |
| 7 | +// The event loop exits and calls the deferred Release method on the window. |
| 8 | +// If this is done on the main window, it works as expected. |
| 9 | +// However if it is done on a client window, it does not disappear. |
| 10 | +// It is left in an unusable state (try to make it bigger), as the event loop is |
| 11 | +// not running anymore. |
| 12 | +package main |
| 13 | + |
| 14 | +import ( |
| 15 | + "image" |
| 16 | + "image/color" |
| 17 | + "image/draw" |
| 18 | + "log" |
| 19 | + |
| 20 | + "golang.org/x/exp/shiny/driver" |
| 21 | + "golang.org/x/exp/shiny/screen" |
| 22 | + "golang.org/x/mobile/event/key" |
| 23 | + "golang.org/x/mobile/event/lifecycle" |
| 24 | + "golang.org/x/mobile/event/mouse" |
| 25 | + "golang.org/x/mobile/event/paint" |
| 26 | + "golang.org/x/mobile/event/size" |
| 27 | +) |
| 28 | + |
| 29 | +func main() { |
| 30 | + driver.Main(func(s screen.Screen) { |
| 31 | + createWindow(s, false) |
| 32 | + }) |
| 33 | +} |
| 34 | + |
| 35 | +func createWindow(s screen.Screen, isClient bool) { |
| 36 | + |
| 37 | + title := "Main Window" |
| 38 | + if isClient { |
| 39 | + title = "Client Window" |
| 40 | + } |
| 41 | + opt := screen.NewWindowOptions{Width: 300, Height: 300, Title: title} |
| 42 | + |
| 43 | + w, err := s.NewWindow(&opt) |
| 44 | + if err != nil { |
| 45 | + log.Print(err) |
| 46 | + return |
| 47 | + } |
| 48 | + defer w.Release() |
| 49 | + |
| 50 | + var b screen.Buffer |
| 51 | + defer func() { |
| 52 | + if b != nil { |
| 53 | + b.Release() |
| 54 | + } |
| 55 | + }() |
| 56 | + |
| 57 | + for { |
| 58 | + switch e := w.NextEvent().(type) { |
| 59 | + |
| 60 | + case lifecycle.Event: |
| 61 | + if e.To == lifecycle.StageDead { |
| 62 | + log.Printf("received %v: window %p should be closing now", e, w) |
| 63 | + return |
| 64 | + } |
| 65 | + |
| 66 | + case paint.Event: |
| 67 | + // lock() |
| 68 | + w.Upload(image.Point{}, b, b.Bounds()) |
| 69 | + w.Publish() |
| 70 | + // unlock() |
| 71 | + |
| 72 | + case size.Event: |
| 73 | + // lock() |
| 74 | + if b != nil { |
| 75 | + b.Release() |
| 76 | + } |
| 77 | + b, err = s.NewBuffer(e.Size()) |
| 78 | + if err != nil { |
| 79 | + log.Print(err) |
| 80 | + } |
| 81 | + |
| 82 | + // Paint the main window blue, and client windows green. |
| 83 | + c := color.RGBA{0, 0, 255, 0} |
| 84 | + if isClient { |
| 85 | + c = color.RGBA{0, 255, 0, 255} |
| 86 | + } |
| 87 | + m := b.RGBA() |
| 88 | + draw.Draw(m, m.Bounds(), &image.Uniform{c}, image.ZP, draw.Src) |
| 89 | + // unlock |
| 90 | + |
| 91 | + case key.Event: |
| 92 | + // We ask to close the window when the ESC key is pressed. |
| 93 | + if e.Code == key.CodeEscape && e.Direction == key.DirPress { |
| 94 | + log.Printf("ESC: send lifecycle.StageDead to window %p", w) |
| 95 | + e := lifecycle.Event{To: lifecycle.StageDead} |
| 96 | + w.Send(e) |
| 97 | + } |
| 98 | + |
| 99 | + case mouse.Event: |
| 100 | + if isClient == false { |
| 101 | + if e.Button > 0 { |
| 102 | + if e.Direction == mouse.DirPress { |
| 103 | + log.Print("Create a new window") |
| 104 | + go createWindow(s, true) |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments