Skip to content

Commit 8019a99

Browse files
committed
Fix right click closing of module settings
1 parent 67887c0 commit 8019a99

2 files changed

Lines changed: 43 additions & 1 deletion

File tree

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import com.lambda.gui.Layout
2121
import com.lambda.gui.components.SettingsWidget.buildConfigSettingsContext
2222
import com.lambda.gui.dsl.ImGuiBuilder
2323
import com.lambda.imgui.ImGui
24+
import com.lambda.imgui.flag.ImGuiHoveredFlags
25+
import com.lambda.imgui.flag.ImGuiPopupFlags
2426
import com.lambda.module.Module
2527

2628
class ModuleEntry(val module: Module): Layout {
@@ -30,9 +32,23 @@ class ModuleEntry(val module: Module): Layout {
3032
}
3133
lambdaTooltip(module.description)
3234

35+
val popupId = "##ctx-${module.name}"
36+
37+
onItemHover(ImGuiHoveredFlags.AllowWhenBlockedByPopup) {
38+
if (isMouseClicked() && isPopupOpen(popupId)) suppressedPopupId = popupId
39+
if (isMouseReleased() && suppressedPopupId != popupId) openPopup(popupId)
40+
}
41+
42+
if (isMouseReleased() && suppressedPopupId == popupId) suppressedPopupId = null
43+
3344
ImGui.setNextWindowSizeConstraints(0f, 0f, Float.MAX_VALUE, io.displaySize.y * 0.5f)
34-
popupContextItem("##ctx-${module.name}") {
45+
popupContextItem(popupId, ImGuiPopupFlags.None) {
3546
buildConfigSettingsContext(module)
3647
}
3748
}
49+
50+
private companion object {
51+
/** Popup whose reopen is pending suppression; only one item can be pressed at a time. */
52+
var suppressedPopupId: String? = null
53+
}
3854
}

src/main/kotlin/com/lambda/gui/dsl/ImGuiBuilder.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,23 @@ object ImGuiBuilder {
295295
fun onItemClick(button: Int = ImGuiMouseButton.Right, block: ProcedureBlock) =
296296
if (isItemClicked(button)) block() else Unit
297297

298+
/**
299+
* Returns whether [button] went down this frame, regardless of what is hovered.
300+
*
301+
* Unlike [onItemClick] this does not imply the last item is hovered, so it can be combined with
302+
* [onItemHover] to observe clicks on items that are blocked by an open popup.
303+
*/
304+
@ImGuiDsl
305+
fun isMouseClicked(button: Int = ImGuiMouseButton.Right, repeat: Boolean = false): Boolean =
306+
ImGui.isMouseClicked(button, repeat)
307+
308+
/**
309+
* Returns whether [button] was released this frame, regardless of what is hovered.
310+
*/
311+
@ImGuiDsl
312+
fun isMouseReleased(button: Int = ImGuiMouseButton.Right): Boolean =
313+
ImGui.isMouseReleased(button)
314+
298315
/**
299316
* Returns whether:
300317
* - The last item modified its value in this frame
@@ -1586,6 +1603,15 @@ object ImGuiBuilder {
15861603
fun openPopup(strId: String, flags: Int = ImGuiPopupFlags.None) =
15871604
ImGui.openPopup(strId, flags)
15881605

1606+
/**
1607+
* Returns whether the popup identified by [strId] is currently open.
1608+
*
1609+
* The id is resolved against the current window, matching [openPopup] and the `popupContext*` helpers.
1610+
*/
1611+
@ImGuiDsl
1612+
fun isPopupOpen(strId: String, flags: Int = ImGuiPopupFlags.None): Boolean =
1613+
ImGui.isPopupOpen(strId, flags)
1614+
15891615
/**
15901616
* Creates a popup. You must first call [openPopup] with the same [strId]
15911617
*

0 commit comments

Comments
 (0)