playground/ExampleApps/ExampleAppConstrainedResize.cpp
// Demonstrate creating a window with custom resize constraints.
Demonstrate creating a window with custom resize constraints.
Demonstrate creating a window with custom resize constraints .
static void ShowExampleAppConstrainedResize(bool* p_open)
{
    struct CustomConstraints
    {
        static void Square(ImGuiSizeCallbackData* data) { data->DesiredSize.x = data->DesiredSize.y = IM_MAX(data->DesiredSize.x, data->DesiredSize.y); }
        static void Step(ImGuiSizeCallbackData* data)   { float step = (float)(int)(intptr_t)data->UserData; data->DesiredSize = ImVec2((int)(data->DesiredSize.x / step + 0.5f) * step, (int)(data->DesiredSize.y / step + 0.5f) * step); }
    };
    const char* test_desc[] =
    {
        "Resize vertical only",
        "Resize horizontal only",
        "Width > 100, Height > 100",
        "Width 400-500",
        "Height 400-500",
        "Custom: Always Square",
        "Custom: Fixed Steps (100)",
    };
    static bool auto_resize = false;
    static int type = 0;
    static int display_lines = 10;
    if (type == 0) ImGui::SetNextWindowSizeConstraints(ImVec2(-1, 0),    ImVec2(-1, FLT_MAX));      
    if (type == 1) ImGui::SetNextWindowSizeConstraints(ImVec2(0, -1),    ImVec2(FLT_MAX, -1));      
    if (type == 2) ImGui::SetNextWindowSizeConstraints(ImVec2(100, 100), ImVec2(FLT_MAX, FLT_MAX)); 
    if (type == 3) ImGui::SetNextWindowSizeConstraints(ImVec2(400, -1),  ImVec2(500, -1));          
    if (type == 4) ImGui::SetNextWindowSizeConstraints(ImVec2(-1, 400),  ImVec2(-1, 500));          
    if (type == 5) ImGui::SetNextWindowSizeConstraints(ImVec2(0, 0),     ImVec2(FLT_MAX, FLT_MAX), CustomConstraints::Square);                     
    if (type == 6) ImGui::SetNextWindowSizeConstraints(ImVec2(0, 0),     ImVec2(FLT_MAX, FLT_MAX), CustomConstraints::Step, (void*)(intptr_t)100); 
    ImGuiWindowFlags flags = auto_resize ? ImGuiWindowFlags_AlwaysAutoResize : 0;
    if (ImGui::Begin("Example: Constrained Resize", p_open, flags))
    {
        DEMO_MARKER("Examples/Constrained Resizing window");
        if (ImGui::IsWindowDocked())
            ImGui::Text("Warning: Sizing Constraints won't work if the window is docked!");
        if (ImGui::Button("200x200")) { ImGui::SetWindowSize(ImVec2(200, 200)); } ImGui::SameLine();
        if (ImGui::Button("500x500")) { ImGui::SetWindowSize(ImVec2(500, 500)); } ImGui::SameLine();
        if (ImGui::Button("800x200")) { ImGui::SetWindowSize(ImVec2(800, 200)); }
        ImGui::SetNextItemWidth(200);
        ImGui::Combo("Constraint", &type, test_desc, IM_ARRAYSIZE(test_desc));
        ImGui::SetNextItemWidth(200);
        ImGui::DragInt("Lines", &display_lines, 0.2f, 1, 100);
        ImGui::Checkbox("Auto-resize", &auto_resize);
        for (int i = 0; i < display_lines; i++)
            ImGui::Text("%*sHello, sailor! Making this line long enough for the example.", i * 4, "");
    }
    ImGui::End();
}
static void ShowExampleAppConstrainedResize ( bool * p_open ) { struct CustomConstraints { static void Square ( ImGuiSizeCallbackData * data ) { data -> DesiredSize . x = data -> DesiredSize . y = IM_MAX ( data -> DesiredSize . x , data -> DesiredSize . y ) ; } static void Step ( ImGuiSizeCallbackData * data ) { float step = ( float ) ( int ) ( intptr_t ) data -> UserData ; data -> DesiredSize = ImVec2 ( ( int ) ( data -> DesiredSize . x / step + 0.5f ) * step , ( int ) ( data -> DesiredSize . y / step + 0.5f ) * step ) ; } } ; const char * test_desc [ ] = { " " , " " , " " , " " , " " , " " , " " , } ; static bool auto_resize = false ; static int type = 0 ; static int display_lines = 10 ; if ( type == 0 ) ImGui :: SetNextWindowSizeConstraints ( ImVec2 ( -1 , 0 ) , ImVec2 ( -1 , FLT_MAX ) ) ; if ( type == 1 ) ImGui :: SetNextWindowSizeConstraints ( ImVec2 ( 0 , -1 ) , ImVec2 ( FLT_MAX , -1 ) ) ; if ( type == 2 ) ImGui :: SetNextWindowSizeConstraints ( ImVec2 ( 100 , 100 ) , ImVec2 ( FLT_MAX , FLT_MAX ) ) ; if ( type == 3 ) ImGui :: SetNextWindowSizeConstraints ( ImVec2 ( 400 , -1 ) , ImVec2 ( 500 , -1 ) ) ; if ( type == 4 ) ImGui :: SetNextWindowSizeConstraints ( ImVec2 ( -1 , 400 ) , ImVec2 ( -1 , 500 ) ) ; if ( type == 5 ) ImGui :: SetNextWindowSizeConstraints ( ImVec2 ( 0 , 0 ) , ImVec2 ( FLT_MAX , FLT_MAX ) , CustomConstraints :: Square ) ; if ( type == 6 ) ImGui :: SetNextWindowSizeConstraints ( ImVec2 ( 0 , 0 ) , ImVec2 ( FLT_MAX , FLT_MAX ) , CustomConstraints :: Step , ( void * ) ( intptr_t ) 100 ) ; ImGuiWindowFlags flags = auto_resize ? ImGuiWindowFlags_AlwaysAutoResize : 0 ; if ( ImGui :: Begin ( " " , p_open , flags ) ) { DEMO_MARKER ( " " ) ; if ( ImGui :: IsWindowDocked ( ) ) ImGui :: Text ( " " ) ; if ( ImGui :: Button ( " " ) ) { ImGui :: SetWindowSize ( ImVec2 ( 200 , 200 ) ) ; } ImGui :: SameLine ( ) ; if ( ImGui :: Button ( " " ) ) { ImGui :: SetWindowSize ( ImVec2 ( 500 , 500 ) ) ; } ImGui :: SameLine ( ) ; if ( ImGui :: Button ( " " ) ) { ImGui :: SetWindowSize ( ImVec2 ( 800 , 200 ) ) ; } ImGui :: SetNextItemWidth ( 200 ) ; ImGui :: Combo ( " " , & type , test_desc , IM_ARRAYSIZE ( test_desc ) ) ; ImGui :: SetNextItemWidth ( 200 ) ; ImGui :: DragInt ( " " , & display_lines , 0.2f , 1 , 100 ) ; ImGui :: Checkbox ( " " , & auto_resize ) ; for ( int i = 0 ; i < display_lines ; i ++ ) ImGui :: Text ( " " , i * 4 , " " ) ; } ImGui :: End ( ) ; }
Demonstrate creating a window with custom resize constraints.
Demonstrate creating a window with custom resize constraints .
// Helper functions to demonstrate programmatic constraints // Vertical only // Horizontal only // Width > 100, Height > 100 // Width 400-500 // Height 400-500 // Always Square // Fixed Step