@@ -29,7 +29,9 @@ import com.lambda.gui.LambdaScreen
2929import com.lambda.gui.Layout
3030import com.lambda.gui.dsl.ImGuiBuilder
3131import com.lambda.imgui.ImGui
32+ import com.lambda.imgui.flag.ImGuiHoveredFlags
3233import com.lambda.imgui.flag.ImGuiInputTextFlags
34+ import com.lambda.imgui.flag.ImGuiMouseButton
3335import com.lambda.imgui.flag.ImGuiStyleVar
3436import com.lambda.imgui.flag.ImGuiWindowFlags
3537import com.lambda.imgui.type.ImString
@@ -50,12 +52,13 @@ object QuickSearch {
5052 var isOpen = false
5153 private set
5254 private var shouldFocus = false
53-
55+ private var pendingClose = false
5456 private var lastShiftPressTime = 0L
5557 private var lastShiftKeyCode = - 1
5658
5759 private const val DOUBLE_SHIFT_WINDOW_MS = 500L
5860 private const val MAX_RESULTS = 50
61+ private const val POPUP_ID = " QuickSearch"
5962 const val WINDOW_FLAGS =
6063 ImGuiWindowFlags .AlwaysAutoResize or
6164 ImGuiWindowFlags .NoTitleBar or
@@ -119,21 +122,23 @@ object QuickSearch {
119122 fun open () {
120123 isOpen = true
121124 shouldFocus = true
125+ pendingClose = false
122126 searchInput.clear()
123127 }
124128
125129 fun close () {
126130 isOpen = false
127131 shouldFocus = false
132+ pendingClose = true
128133 }
129134
130135 fun toggle () {
131136 if (isOpen) close() else open()
132137 }
133138
134139 fun ImGuiBuilder.renderQuickSearch () {
135- if (! isOpen) return
136- ImGui . openPopup(" QuickSearch " )
140+ if (! isOpen && ! pendingClose ) return
141+ if (isOpen) openPopup(POPUP_ID )
137142
138143 ImGui .setNextFrameWantCaptureKeyboard(true )
139144
@@ -146,7 +151,18 @@ object QuickSearch {
146151 ImGui .setNextWindowSize(maxW, 0f )
147152 ImGui .setNextWindowSizeConstraints(0f , 0f , maxW, maxH)
148153
149- popupModal(" QuickSearch" , WINDOW_FLAGS ) {
154+ popupModal(POPUP_ID , WINDOW_FLAGS ) {
155+ if (pendingClose) {
156+ pendingClose = false
157+ closeCurrentPopup()
158+ return @popupModal
159+ }
160+
161+ if (isMouseClicked(ImGuiMouseButton .Left ) && ! isWindowHovered(ImGuiHoveredFlags .RootAndChildWindows )) {
162+ close()
163+ return @popupModal
164+ }
165+
150166 if (shouldFocus) {
151167 ImGui .setKeyboardFocusHere()
152168 shouldFocus = false
@@ -315,7 +331,7 @@ object QuickSearch {
315331 if (lastShiftKeyCode == event.keyCode &&
316332 currentTime - lastShiftPressTime <= DOUBLE_SHIFT_WINDOW_MS
317333 ) {
318- open ()
334+ toggle ()
319335 lastShiftPressTime = 0L
320336 lastShiftKeyCode = - 1
321337 } else {
0 commit comments