AllenDang / giu

Cross platform rapid GUI framework for golang based on Dear ImGui.
MIT License
2.35k stars 135 forks source link

[bug] Image widget OnClick behavior #893

Open SleepyPrince opened 1 day ago

SleepyPrince commented 1 day ago

What happend?

For the onClick function to trigger, the current check requires the window to be in focus: https://github.com/AllenDang/giu/blob/cdbcd33dbed76caf1b0d105804cdde4ccd4c5fee/ImageWidgets.go#L88-L129

However, this causes requiring an extra click to trigger if the image is in another window (including across 2 sides of split layout). Other clickable widgets do not behave this way. Replacing IsWindowFocused by IsWindowHovered should fix this issue.

Code example

main.go ```golang package main import ( "runtime" "github.com/AllenDang/giu" ) var split1 float32 = 200 var split2 float32 = 200 var texture = giu.ReflectiveBoundTexture{} func loop() { giu.Window("A").Size(500, 500).Layout(giu.SplitLayout(giu.DirectionHorizontal, &split1, giu.Layout{ texture.ToImageWidget().OnClick(func() { fmt.Println("clicked A1 Image") }), }, giu.Layout{ texture.ToImageWidget().OnClick(func() { fmt.Println("clicked A2 Image") }), })) giu.Window("B").Size(500, 500).Layout(giu.SplitLayout(giu.DirectionHorizontal, &split1, giu.Layout{ texture.ToImageWidget().OnClick(func() { fmt.Println("clicked B1 Image") }), }, giu.Layout{ texture.ToImageWidget().OnClick(func() { fmt.Println("clicked B2 Image") }), })) } func main() { runtime.LockOSThread() wnd := giu.NewMasterWindow("test", 800, 800, 0) wnd.Run(loop) } ```

To Reproduce

  1. Run my demo
  2. Buttons in non-focus "window" require 2 clicks to trigger

Version

master

OS

Windows 10

gucio321 commented 1 day ago

@SleepyPrince makes sense. Could you open a PR or you want me to do so?