Skip to content

Commit

Permalink
feat: added get_focused_item() to request the current nav focus
Browse files Browse the repository at this point in the history
  • Loading branch information
v-ein committed Jan 19, 2024
1 parent c9d6a91 commit f99ca4c
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 0 deletions.
10 changes: 10 additions & 0 deletions dearpygui/dearpygui.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/dearpygui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,7 @@ PyInit__dearpygui(void)
MV_ADD_COMMAND(get_windows);
MV_ADD_COMMAND(get_all_items);
MV_ADD_COMMAND(get_active_window);
MV_ADD_COMMAND(get_focused_item);
MV_ADD_COMMAND(set_primary_window);
MV_ADD_COMMAND(push_container_stack);
MV_ADD_COMMAND(pop_container_stack);
Expand Down
8 changes: 8 additions & 0 deletions src/dearpygui_commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -2991,6 +2991,14 @@ get_active_window(PyObject* self, PyObject* args, PyObject* kwargs)
return ToPyUUID(GContext->itemRegistry->activeWindow);
}

static PyObject*
get_focused_item(PyObject* self, PyObject* args, PyObject* kwargs)
{
std::lock_guard<std::recursive_mutex> lk(GContext->mutex);

return ToPyUUID(GContext->itemRegistry->focusedItem);
}

static PyObject*
move_item(PyObject* self, PyObject* args, PyObject* kwargs)
{
Expand Down
12 changes: 12 additions & 0 deletions src/dearpygui_parsers.h
Original file line number Diff line number Diff line change
Expand Up @@ -1215,6 +1215,18 @@ InsertParser_Block2(std::map<std::string, mvPythonParser>& parsers)
parsers.insert({ "get_active_window", parser });
}

{
std::vector<mvPythonDataElement> args;

mvPythonParserSetup setup;
setup.about = "Returns the item currently having focus.";
setup.category = { "Item Registry" };
setup.returnType = mvPyDataType::UUID;

mvPythonParser parser = FinalizeParser(setup, args);
parsers.insert({ "get_focused_item", parser });
}

{
std::vector<mvPythonDataElement> args;
args.push_back({ mvPyDataType::UUID, "window" });
Expand Down
5 changes: 5 additions & 0 deletions src/mvAppItemState.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "mvAppItemState.h"
#include <imgui.h>
#include "mvAppItem.h"
#include "mvItemRegistry.h"
#include "mvContext.h"
#include "mvPyUtils.h"

Expand Down Expand Up @@ -30,6 +31,10 @@ UpdateAppItemState(mvAppItemState& state)
state.hovered = ImGui::IsItemHovered();
state.active = ImGui::IsItemActive();
state.focused = ImGui::IsItemFocused();
if (state.focused)
{
GContext->itemRegistry->focusedItem = state.parent->uuid;
}
state.leftclicked = ImGui::IsItemClicked();
state.rightclicked = ImGui::IsItemClicked(1);
state.middleclicked = ImGui::IsItemClicked(2);
Expand Down
1 change: 1 addition & 0 deletions src/mvItemRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ struct mvItemRegistry
std::stack<mvAppItem*> containers; // parent stack, top of stack becomes widget's parent
std::unordered_map<std::string, mvUUID> aliases;
mvUUID activeWindow = 0;
mvUUID focusedItem = 0;
std::vector<mvAppItem*> delayedSearch;
b8 showImGuiDebug = false;
b8 showImPlotDebug = false;
Expand Down

0 comments on commit f99ca4c

Please sign in to comment.