Skip to content

Commit 4a45155

Browse files
committed
Fix QuickSearch closing on double-shift and click outside
1 parent 8019a99 commit 4a45155

3 files changed

Lines changed: 45 additions & 157 deletions

File tree

src/main/kotlin/com/lambda/gui/components/ModuleEntry.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@ class ModuleEntry(val module: Module): Layout {
3333
lambdaTooltip(module.description)
3434

3535
val popupId = "##ctx-${module.name}"
36+
val entryId = getId(popupId)
3637

3738
onItemHover(ImGuiHoveredFlags.AllowWhenBlockedByPopup) {
38-
if (isMouseClicked() && isPopupOpen(popupId)) suppressedPopupId = popupId
39-
if (isMouseReleased() && suppressedPopupId != popupId) openPopup(popupId)
39+
if (isMouseClicked() && isPopupOpen(popupId)) suppressedEntryId = entryId
40+
if (isMouseReleased() && suppressedEntryId != entryId) openPopup(popupId)
4041
}
4142

42-
if (isMouseReleased() && suppressedPopupId == popupId) suppressedPopupId = null
43+
if (isMouseReleased() && suppressedEntryId == entryId) suppressedEntryId = null
4344

4445
ImGui.setNextWindowSizeConstraints(0f, 0f, Float.MAX_VALUE, io.displaySize.y * 0.5f)
4546
popupContextItem(popupId, ImGuiPopupFlags.None) {
@@ -48,7 +49,7 @@ class ModuleEntry(val module: Module): Layout {
4849
}
4950

5051
private companion object {
51-
/** Popup whose reopen is pending suppression; only one item can be pressed at a time. */
52-
var suppressedPopupId: String? = null
52+
/** Entry whose reopen is pending suppression; only one item can be pressed at a time. */
53+
var suppressedEntryId: Int? = null
5354
}
5455
}

src/main/kotlin/com/lambda/gui/components/QuickSearch.kt

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ import com.lambda.gui.LambdaScreen
2929
import com.lambda.gui.Layout
3030
import com.lambda.gui.dsl.ImGuiBuilder
3131
import com.lambda.imgui.ImGui
32+
import com.lambda.imgui.flag.ImGuiHoveredFlags
3233
import com.lambda.imgui.flag.ImGuiInputTextFlags
34+
import com.lambda.imgui.flag.ImGuiMouseButton
3335
import com.lambda.imgui.flag.ImGuiStyleVar
3436
import com.lambda.imgui.flag.ImGuiWindowFlags
3537
import 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

Comments
 (0)