Skip to content

Commit

Permalink
It is now possible to set width and height of Window widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
andreamancuso committed May 31, 2024
1 parent 58b0042 commit 1fad11a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 6 deletions.
7 changes: 6 additions & 1 deletion packages/dear-imgui/cpp/widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ void Fragment::Render(ReactImgui* view) {

void Window::Render(ReactImgui* view) {
ImGui::PushID(m_id);
ImGui::Begin(m_title.c_str(), &m_open, m_flags);
ImGui::SetNextWindowSize(ImVec2(m_width, m_height), ImGuiCond_FirstUseEver);

if (!ImGui::Begin(m_title.c_str(), &m_open, m_flags)) {
ImGui::End();
return;
}
Widget::HandleChildren(view);
ImGui::End();
ImGui::PopID();
Expand Down
10 changes: 8 additions & 2 deletions packages/dear-imgui/cpp/widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,23 +98,29 @@ class Window final : public Widget {
ImGuiWindowFlags m_flags = ImGuiWindowFlags_None;
bool m_open;
std::string m_title;
float m_width;
float m_height;

static std::unique_ptr<Window> makeWidget(const json& val) {
if (val.is_object()) {
auto id = val["id"].template get<int>();
auto title = val["title"].template get<std::string>();
auto width = val["width"].template get<float>();
auto height = val["height"].template get<float>();

return std::make_unique<Window>(id, title);
return std::make_unique<Window>(id, title, width, height);
}

throw std::invalid_argument("Invalid JSON data");
}

Window(int id, std::string title) : Widget(id) {
Window(int id, std::string title, float width, float height) : Widget(id) {
m_type = "Window";
m_handlesChildrenWithinRenderMethod = true;

m_title = title;
m_width = width;
m_height = height;
m_open = true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export const ImGuiDemo = () => {
</ReactImgui.SameLine>
</ReactImgui.CollapsingHeader>

<ReactImgui.DIWindow title="another window">
<ReactImgui.DIWindow title="another window" width={820} height={600}>
<ReactImgui.SameLine>
<ReactImgui.Child width={400} height={0}>
<ReactImgui.InputText
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { PropsWithChildren, WidgetFunctionComponent, WidgetPropsMap } from "./ty
// todo: perhaps we can come up with a way to deal with 'real' fragments
export const DIWindow: WidgetFunctionComponent<PropsWithChildren & WidgetPropsMap["DIWindow"]> = ({
title,
width,
height,
children,
}) => {
return (
<widget type="DIWindow" title={title}>
<widget type="DIWindow" width={width} height={height} title={title}>
{children}
</widget>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export type WidgetPropsMap = {
Unknown: {};
Fragment: {};
Child: { width: number; height: number };
DIWindow: { title: string };
DIWindow: { title: string; width: number; height: number };
Group: {};
TabBar: {};
TabItem: { label: string; onOpenChange?: (value: boolean) => void };
Expand Down

0 comments on commit 1fad11a

Please sign in to comment.