> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/ocornut/imgui/llms.txt
> Use this file to discover all available pages before exploring further.

# Window Utilities

> Query and manipulate window state, position, size, and scrolling

Window utility functions allow you to query window properties, check focus and hover states, and manipulate window positioning, sizing, and scrolling.

## Window State Queries

### IsWindowFocused

Checks if the current window is focused.

```cpp theme={null}
bool IsWindowFocused(ImGuiFocusedFlags flags = 0);
```

#### Parameters

<ParamField path="flags" type="ImGuiFocusedFlags" default="0">
  Flags to modify the focus check behavior. Common flags:

  * `ImGuiFocusedFlags_ChildWindows` - Check child windows too
  * `ImGuiFocusedFlags_RootWindow` - Check root window instead
  * `ImGuiFocusedFlags_AnyWindow` - Return true if any window is focused
</ParamField>

#### Returns

<ResponseField name="return" type="bool">
  True if the current window (or specified scope) is focused.
</ResponseField>

#### Example

```cpp theme={null}
ImGui::Begin("My Window");

if (ImGui::IsWindowFocused()) {
    ImGui::Text("This window has focus");
    // Handle keyboard shortcuts specific to this window
}

// Check if this window OR any of its children are focused
if (ImGui::IsWindowFocused(ImGuiFocusedFlags_ChildWindows)) {
    ImGui::Text("This window or a child has focus");
}

ImGui::End();
```

### IsWindowHovered

Checks if the current window is hovered and hoverable.

```cpp theme={null}
bool IsWindowHovered(ImGuiHoveredFlags flags = 0);
```

#### Parameters

<ParamField path="flags" type="ImGuiHoveredFlags" default="0">
  Flags to modify hover check behavior. Common flags:

  * `ImGuiHoveredFlags_ChildWindows` - Check child windows too
  * `ImGuiHoveredFlags_RootWindow` - Check root window instead
  * `ImGuiHoveredFlags_AllowWhenBlockedByPopup` - Return true even if blocked by popup
  * `ImGuiHoveredFlags_AllowWhenBlockedByActiveItem` - Return true even if blocked by active item
</ParamField>

#### Returns

<ResponseField name="return" type="bool">
  True if the current window is hovered and hoverable (not blocked by popup/modal).
</ResponseField>

#### Example

```cpp theme={null}
ImGui::Begin("My Window");

if (ImGui::IsWindowHovered()) {
    ImGui::Text("Mouse is over this window");
    
    // Handle mouse wheel for custom scrolling
    float wheel = ImGui::GetIO().MouseWheel;
    if (wheel != 0.0f) {
        // Custom scroll handling
    }
}

ImGui::End();
```

<Warning>
  If you want to check whether your mouse should be dispatched to ImGui or your application, **do not use this function**. Use `io.WantCaptureMouse` instead. See FAQ: "How can I tell whether to dispatch mouse/keyboard to Dear ImGui or my application?"
</Warning>

## Window Position & Size Queries

### GetWindowPos

Gets the current window position in screen space.

```cpp theme={null}
ImVec2 GetWindowPos();
```

#### Returns

<ResponseField name="return" type="ImVec2">
  Current window position in screen coordinates (pixels).
</ResponseField>

#### Example

```cpp theme={null}
ImGui::Begin("Window");

ImVec2 pos = ImGui::GetWindowPos();
ImGui::Text("Window at (%.0f, %.0f)", pos.x, pos.y);

ImGui::End();
```

<Note>
  It is unlikely you ever need to use this. Consider using `GetCursorScreenPos()` and `GetContentRegionAvail()` instead for layout purposes.
</Note>

### GetWindowSize

Gets the current window size.

```cpp theme={null}
ImVec2 GetWindowSize();
```

#### Returns

<ResponseField name="return" type="ImVec2">
  Current window size including decorations (title bar, borders, etc.).
</ResponseField>

#### Example

```cpp theme={null}
ImGui::Begin("Window");

ImVec2 size = ImGui::GetWindowSize();
ImGui::Text("Window size: %.0fx%.0f", size.x, size.y);

ImGui::End();
```

### GetWindowWidth / GetWindowHeight

Gets the current window width or height.

```cpp theme={null}
float GetWindowWidth();
float GetWindowHeight();
```

#### Returns

<ResponseField name="return" type="float">
  Current window width or height in pixels.
</ResponseField>

#### Example

```cpp theme={null}
ImGui::Begin("Window");

float width = ImGui::GetWindowWidth();
float height = ImGui::GetWindowHeight();

ImGui::Text("Width: %.0f, Height: %.0f", width, height);

ImGui::End();
```

## Window Manipulation

<Note>
  Prefer using `SetNextXXX` functions (before `Begin()`) rather than `SetXXX` functions (after `Begin()`). The `SetNext` functions are cleaner and avoid potential side effects.
</Note>

### SetNextWindowPos

Sets the next window position (call before `Begin()`).

```cpp theme={null}
void SetNextWindowPos(const ImVec2& pos, ImGuiCond cond = 0, const ImVec2& pivot = ImVec2(0, 0));
```

#### Parameters

<ParamField path="pos" type="const ImVec2&" required>
  Position in screen coordinates.
</ParamField>

<ParamField path="cond" type="ImGuiCond" default="0">
  Condition for applying position:

  * `0` or `ImGuiCond_Always` - Always apply
  * `ImGuiCond_Once` - Apply once per runtime session
  * `ImGuiCond_FirstUseEver` - Apply only on first use
  * `ImGuiCond_Appearing` - Apply when window appears
</ParamField>

<ParamField path="pivot" type="const ImVec2&" default="ImVec2(0, 0)">
  Pivot point: (0,0) = top-left, (0.5,0.5) = center, (1,1) = bottom-right.
</ParamField>

#### Example

```cpp theme={null}
// Always position at top-left
ImGui::SetNextWindowPos(ImVec2(0, 0), ImGuiCond_Always);
ImGui::Begin("Fixed Position");

// Center on screen
ImGuiIO& io = ImGui::GetIO();
ImVec2 center(io.DisplaySize.x * 0.5f, io.DisplaySize.y * 0.5f);
ImGui::SetNextWindowPos(center, ImGuiCond_Appearing, ImVec2(0.5f, 0.5f));
ImGui::Begin("Centered");

// Bottom-right corner
ImVec2 br(io.DisplaySize.x, io.DisplaySize.y);
ImGui::SetNextWindowPos(br, ImGuiCond_Always, ImVec2(1.0f, 1.0f));
ImGui::Begin("Bottom Right");
```

### SetNextWindowSize

Sets the next window size (call before `Begin()`).

```cpp theme={null}
void SetNextWindowSize(const ImVec2& size, ImGuiCond cond = 0);
```

#### Parameters

<ParamField path="size" type="const ImVec2&" required>
  Window size. Set axis to 0.0f to force auto-fit on that axis.
</ParamField>

<ParamField path="cond" type="ImGuiCond" default="0">
  Condition for applying size.
</ParamField>

#### Example

```cpp theme={null}
// Fixed size
ImGui::SetNextWindowSize(ImVec2(400, 300), ImGuiCond_Always);
ImGui::Begin("Fixed Size");

// Auto-fit width, fixed height
ImGui::SetNextWindowSize(ImVec2(0, 300));
ImGui::Begin("Auto Width");
```

### SetNextWindowSizeConstraints

Sets the next window size constraints.

```cpp theme={null}
void SetNextWindowSizeConstraints(const ImVec2& size_min, const ImVec2& size_max, 
                                  ImGuiSizeCallback custom_callback = NULL, 
                                  void* custom_callback_data = NULL);
```

#### Parameters

<ParamField path="size_min" type="const ImVec2&" required>
  Minimum size. Use 0.0f or `FLT_MAX` for no limit.
</ParamField>

<ParamField path="size_max" type="const ImVec2&" required>
  Maximum size. Use `FLT_MAX` for no limit. Use -1 for both min and max of same axis to preserve current size.
</ParamField>

<ParamField path="custom_callback" type="ImGuiSizeCallback" default="NULL">
  Optional callback for custom programmatic constraints.
</ParamField>

<ParamField path="custom_callback_data" type="void*" default="NULL">
  User data passed to the callback.
</ParamField>

#### Example

```cpp theme={null}
// Minimum size 200x200, no maximum
ImGui::SetNextWindowSizeConstraints(ImVec2(200, 200), ImVec2(FLT_MAX, FLT_MAX));
ImGui::Begin("Constrained");

// Fixed aspect ratio (custom callback)
static auto AspectRatioCallback = [](ImGuiSizeCallbackData* data) {
    float aspect_ratio = *(float*)data->UserData;
    data->DesiredSize.y = data->DesiredSize.x / aspect_ratio;
};

static float aspect = 16.0f / 9.0f;
ImGui::SetNextWindowSizeConstraints(
    ImVec2(0, 0), ImVec2(FLT_MAX, FLT_MAX),
    AspectRatioCallback, &aspect);
ImGui::Begin("16:9 Aspect");
```

## Window Scrolling

### GetScrollX / GetScrollY

Gets the current scroll position.

```cpp theme={null}
float GetScrollX();
float GetScrollY();
```

#### Returns

<ResponseField name="return" type="float">
  Current scroll amount from 0 to `GetScrollMaxX()` or `GetScrollMaxY()`.
</ResponseField>

#### Example

```cpp theme={null}
ImGui::Begin("Scrollable");

float scroll_y = ImGui::GetScrollY();
float scroll_max_y = ImGui::GetScrollMaxY();

ImGui::Text("Scroll: %.0f / %.0f", scroll_y, scroll_max_y);

// Show scroll percentage
if (scroll_max_y > 0) {
    float percent = (scroll_y / scroll_max_y) * 100.0f;
    ImGui::Text("%.0f%% scrolled", percent);
}

ImGui::End();
```

### SetScrollX / SetScrollY

Sets the scroll position.

```cpp theme={null}
void SetScrollX(float scroll_x);
void SetScrollY(float scroll_y);
```

#### Parameters

<ParamField path="scroll_x" type="float" required>
  Scroll amount from 0 to `GetScrollMaxX()`.
</ParamField>

<ParamField path="scroll_y" type="float" required>
  Scroll amount from 0 to `GetScrollMaxY()`.
</ParamField>

#### Example

```cpp theme={null}
ImGui::Begin("Scrollable");

// Scroll to top button
if (ImGui::Button("Scroll to Top")) {
    ImGui::SetScrollY(0);
}

// Scroll to bottom button
if (ImGui::Button("Scroll to Bottom")) {
    ImGui::SetScrollY(ImGui::GetScrollMaxY());
}

// Scroll to middle
if (ImGui::Button("Scroll to Middle")) {
    ImGui::SetScrollY(ImGui::GetScrollMaxY() * 0.5f);
}

ImGui::End();
```

<Note>
  Changes to scroll will be applied at the beginning of the next frame in the first call to `Begin()`. You can use `SetNextWindowScroll()` before `Begin()` to avoid this one-frame delay.
</Note>

### SetScrollHereY

Adjusts scrolling to make the current cursor position visible.

```cpp theme={null}
void SetScrollHereY(float center_y_ratio = 0.5f);
```

#### Parameters

<ParamField path="center_y_ratio" type="float" default="0.5f">
  Position ratio: 0.0 = top, 0.5 = center, 1.0 = bottom.
</ParamField>

#### Example

```cpp theme={null}
ImGui::Begin("List");

static int selected = 0;

for (int i = 0; i < 100; i++) {
    if (ImGui::Selectable(fmt::format("Item {}", i).c_str(), selected == i)) {
        selected = i;
        // Scroll to make selected item visible in center
        ImGui::SetScrollHereY(0.5f);
    }
}

ImGui::End();
```

### GetScrollMaxX / GetScrollMaxY

Gets the maximum scroll amount.

```cpp theme={null}
float GetScrollMaxX();
float GetScrollMaxY();
```

#### Returns

<ResponseField name="return" type="float">
  Maximum scroll amount ≈ ContentSize - WindowSize - DecorationsSize.
</ResponseField>

#### Example

```cpp theme={null}
ImGui::Begin("Window");

float max_scroll = ImGui::GetScrollMaxY();

if (max_scroll > 0) {
    ImGui::Text("Window is scrollable (max: %.0f)", max_scroll);
} else {
    ImGui::Text("Window content fits, no scrolling needed");
}

ImGui::End();
```

## Complete Example: Scrollable Log

```cpp theme={null}
struct LogWindow {
    std::vector<std::string> lines;
    bool auto_scroll = true;
    
    void AddLog(const char* fmt, ...) {
        va_list args;
        va_start(args, fmt);
        char buf[1024];
        vsnprintf(buf, sizeof(buf), fmt, args);
        va_end(args);
        lines.push_back(buf);
    }
    
    void Draw(const char* title, bool* p_open = NULL) {
        ImGui::SetNextWindowSize(ImVec2(500, 400), ImGuiCond_FirstUseEver);
        if (!ImGui::Begin(title, p_open)) {
            ImGui::End();
            return;
        }
        
        // Options
        ImGui::Checkbox("Auto-scroll", &auto_scroll);
        ImGui::SameLine();
        if (ImGui::Button("Clear")) {
            lines.clear();
        }
        ImGui::SameLine();
        if (ImGui::Button("Add Test Line")) {
            AddLog("Test line %d", lines.size());
        }
        
        ImGui::Separator();
        
        // Log content
        ImGui::BeginChild("ScrollingRegion", ImVec2(0, 0), 
            ImGuiChildFlags_Borders, ImGuiWindowFlags_HorizontalScrollbar);
        
        for (const auto& line : lines) {
            ImGui::TextUnformatted(line.c_str());
        }
        
        // Auto-scroll to bottom
        if (auto_scroll && ImGui::GetScrollY() >= ImGui::GetScrollMaxY()) {
            ImGui::SetScrollHereY(1.0f);
        }
        
        ImGui::EndChild();
        ImGui::End();
    }
};

static LogWindow log_window;

// Usage
log_window.AddLog("Application started");
log_window.Draw("Log");
```
