Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BeginMainMenuBar() and BeginViewportSideBar() disappears #7697

Closed
thewoz opened this issue Jun 16, 2024 · 5 comments
Closed

BeginMainMenuBar() and BeginViewportSideBar() disappears #7697

thewoz opened this issue Jun 16, 2024 · 5 comments
Labels
layout menus menu bars, menu items

Comments

@thewoz
Copy link

thewoz commented Jun 16, 2024

Version/Branch of Dear ImGui:

Version 1.90.5, Branch: docking

Back-ends:

ImGui_ImplGlfw ImGui_ImplOpenGL3

Compiler, OS:

macOS

Full config/build information:

No response

Details:

I can't figure out what I'm doing wrong.
As soon as I click on the window the menu and the status bar disappears

This issue is part of issue #7631
And I don't know if it is related to issue #6283

Screenshots/Video:

screen.mov

Minimal, Complete and Verifiable Example code:

#include <cstdio>
#include <cstdlib>

#include <iostream>

#include <glad/glad.h>

#include <GLFW/glfw3.h>

#define IMGUI_DEFINE_MATH_OPERATORS
#include <imgui/imgui.hpp>

static void glfwErrorCallback(int error, const char * description) {
    fprintf(stderr, "GLFW error (%d): %s\n", error, description);
}

int main(int argc, const char * argv[]) {

  if(!glfwInit()) {
    fprintf(stderr, "GLFW init error\n");
    exit(EXIT_FAILURE);
  }

#if __APPLE__
  const char* glsl_version = "#version 100";
  glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#else
  const char* glsl_version = "#version 130";
  glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
#endif

  glfwSetErrorCallback(glfwErrorCallback);

  GLFWwindow * window = glfwCreateWindow(640, 480, "ImGui", NULL, NULL);

  if(!window) {
    glfwTerminate();
    abort();
  }

  glfwMakeContextCurrent(window);
  
  glfwSwapInterval(1);

  if(!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) abort();

  IMGUI_CHECKVERSION();
  ImGui::CreateContext();

  ImGui_ImplGlfw_InitForOpenGL(window, true);
  ImGui_ImplOpenGL3_Init(glsl_version);
  
  ImGui::StyleColorsDark();

  while(!glfwWindowShouldClose(window)) {
    
    glfwPollEvents();
    
    ImGui_ImplOpenGL3_NewFrame();
    ImGui_ImplGlfw_NewFrame();
    ImGui::NewFrame();

    ImGuiViewport * viewport = ImGui::GetMainViewport();
    ImGui::SetNextWindowPos(viewport->Pos);
    ImGui::SetNextWindowSize(viewport->Size);
    ImGui::SetNextWindowViewport(viewport->ID);
        
     ImGuiWindowFlags windowFlags = ImGuiWindowFlags_MenuBar | ImGuiWindowFlags_NoDocking | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoNavFocus | ImGuiWindowFlags_NoMove;
    
    ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));
    ImGui::Begin("MainWindow", NULL, windowFlags);
    ImGui::PopStyleVar();
    
    if(ImGui::BeginMainMenuBar()) {
      if(ImGui::BeginMenu("File")) {
        ImGui::MenuItem("About", NULL, false, true);
        ImGui::EndMenu();
      }
      ImGui::EndMainMenuBar();
    }

    float heightSideBar = ImGui::GetFrameHeight();

    if(ImGui::BeginViewportSideBar("StatusBar", viewport, ImGuiDir_Down, heightSideBar, ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_MenuBar)) {
      if(ImGui::BeginMenuBar()) {
        ImGui::Text("Status Bar:");
        ImGui::EndMenuBar();
      }
      ImGui::End();
    }

    // right part red
    ImGui::PushStyleColor(ImGuiCol_ChildBg, ImColor(255,0,0).Value);
    ImGui::BeginChild("3", ImVec2(0, 0), true);
    ImGui::PopStyleColor();
    ImGui::EndChild();
    
    ImGui::End();
    
    ImGui::Render();
    int displayW, displayH;
    glfwGetFramebufferSize(window, &displayW, &displayH);
    glViewport(0, 0, displayW, displayH);
    static ImVec4 clearColor = ImColor(60, 55, 15);
    glClearColor(clearColor.x, clearColor.y, clearColor.z, clearColor.w);
    glClear(GL_COLOR_BUFFER_BIT);
    ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());

    glfwSwapBuffers(window);
    
  }

  ImGui_ImplOpenGL3_Shutdown();
  ImGui_ImplGlfw_Shutdown();
  ImGui::DestroyContext();
  
  glfwDestroyWindow(window);
  glfwTerminate();
  
  return 0;
  
}
@ocornut
Copy link
Owner

ocornut commented Jun 18, 2024

ImGui::SetNextWindowPos(viewport->Pos);
ImGui::SetNextWindowSize(viewport->Size);
ImGui::SetNextWindowViewport(viewport->ID);

Your window is programmed to fill the entire viewport so that’s what it does. Bringing to front makes it appear over those bars.

You should use viewport->WorkPos and viewport->WorkSize to avoid overlapping menu bars and side bars.

@thewoz
Copy link
Author

thewoz commented Jun 18, 2024

Hi @ocornut ,
thank you very much for the explanation.
I absolutely did not understand that was the problem.
I implemented what you suggested and indeed nothing disappears anymore.
What I don't understand is why the red part doesn't fill all the available space.

Screenshot 2024-06-18 at 12 29 54

@ocornut
Copy link
Owner

ocornut commented Jun 18, 2024

Because your red window also has a Menubar as you passed ImGuiWindowFlags_MenuBar to it.

@ocornut ocornut added layout menus menu bars, menu items labels Jun 18, 2024
@thewoz
Copy link
Author

thewoz commented Jun 18, 2024

I'm so sorry.
I understand what you mean.
Every time there is some thing I miss.
It's just that I still don't fully understand what the internal architecture of ImGui is.

So I by doing this:

    if(ImGui::BeginMainMenuBar()) {
      if(ImGui::BeginMenu("File")) {
        ImGui::MenuItem("About", NULL, false, true);
        ImGui::EndMenu();
      }
      ImGui::EndMainMenuBar();
    }

I was already defining a menu in the "main" window.

So actually by putting this flag ImGuiWindowFlags_MenuBar I was adding another menu in the window that I was creating below

I was sure that putting ImGuiWindowFlags_MenuBar was to put the menu in the window I was creating here:

ImGui::Begin("MainWindow", NULL, windowFlags);

And that this was the code that was needed to make it work.

    if(ImGui::BeginMainMenuBar()) {
      if(ImGui::BeginMenu("File")) {
        ImGui::MenuItem("About", NULL, false, true);
        ImGui::EndMenu();
      }
      ImGui::EndMainMenuBar();
    }

Anyway here is the complete corrected code.
thank you very much again.

#include <cstdio>
#include <cstdlib>

#include <iostream>

#include <glad/glad.h>

#include <GLFW/glfw3.h>

#define IMGUI_DEFINE_MATH_OPERATORS
#include <imgui/imgui.hpp>

static void glfwErrorCallback(int error, const char * description) {
    fprintf(stderr, "GLFW error (%d): %s\n", error, description);
}

int main(int argc, const char * argv[]) {

  if(!glfwInit()) {
    fprintf(stderr, "GLFW init error\n");
    exit(EXIT_FAILURE);
  }

#if __APPLE__
  const char* glsl_version = "#version 100";
  glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#else
  const char* glsl_version = "#version 130";
  glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
#endif

  glfwSetErrorCallback(glfwErrorCallback);

  GLFWwindow * window = glfwCreateWindow(640, 480, "ImGui", NULL, NULL);

  if(!window) {
    glfwTerminate();
    abort();
  }

  glfwMakeContextCurrent(window);
  
  glfwSwapInterval(1);

  if(!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) abort();

  IMGUI_CHECKVERSION();
  ImGui::CreateContext();

  ImGui_ImplGlfw_InitForOpenGL(window, true);
  ImGui_ImplOpenGL3_Init(glsl_version);
  
  ImGui::StyleColorsDark();

  while(!glfwWindowShouldClose(window)) {
    
    glfwPollEvents();
    
    ImGui_ImplOpenGL3_NewFrame();
    ImGui_ImplGlfw_NewFrame();
    ImGui::NewFrame();

    ImGuiViewport * viewport = ImGui::GetMainViewport();
    ImGui::SetNextWindowPos(viewport->WorkPos);
    ImGui::SetNextWindowSize(viewport->WorkSize);
    ImGui::SetNextWindowViewport(viewport->ID);
        
    ImGuiWindowFlags windowFlags = ImGuiWindowFlags_NoDocking | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoNavFocus | ImGuiWindowFlags_NoMove;
    
    ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));
    ImGui::Begin("MainWindow", NULL, windowFlags);
    ImGui::PopStyleVar();
    
    if(ImGui::BeginMainMenuBar()) {
      if(ImGui::BeginMenu("File")) {
        ImGui::MenuItem("About", NULL, false, true);
        ImGui::EndMenu();
      }
      ImGui::EndMainMenuBar();
    }

    float heightSideBar = ImGui::GetFrameHeight();

    if(ImGui::BeginViewportSideBar("StatusBar", viewport, ImGuiDir_Down, heightSideBar, ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_MenuBar)) {
      if(ImGui::BeginMenuBar()) {
        ImGui::Text("Status Bar:");
        ImGui::EndMenuBar();
      }
      ImGui::End();
    }

    // right part red
    ImGui::PushStyleColor(ImGuiCol_ChildBg, ImColor(255,0,0).Value);
    ImGui::BeginChild("3", ImVec2(0, 0), true);
    ImGui::PopStyleColor();
    ImGui::EndChild();
    
    ImGui::End();
    
    ImGui::Render();
    int displayW, displayH;
    glfwGetFramebufferSize(window, &displayW, &displayH);
    glViewport(0, 0, displayW, displayH);
    static ImVec4 clearColor = ImColor(60, 55, 15);
    glClearColor(clearColor.x, clearColor.y, clearColor.z, clearColor.w);
    glClear(GL_COLOR_BUFFER_BIT);
    ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());

    glfwSwapBuffers(window);
    
  }

  ImGui_ImplOpenGL3_Shutdown();
  ImGui_ImplGlfw_Shutdown();
  ImGui::DestroyContext();
  
  glfwDestroyWindow(window);
  glfwTerminate();
  
  return 0;
  
}

@ocornut
Copy link
Owner

ocornut commented Jun 18, 2024

Happy to help!

@ocornut ocornut closed this as completed Jun 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
layout menus menu bars, menu items
Projects
None yet
Development

No branches or pull requests

2 participants