/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later */

/** \file
 * \ingroup edinterface
 */

/* a full doc with API notes can be found in
 * bf-blender/trunk/blender/doc/guides/interface_API.txt */

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <cstring>

#include "MEM_guardedalloc.h"

#include "BLI_listbase.h"
#include "BLI_math_vector.h"
#include "BLI_string.h"
#include "BLI_string_utf8.h"
#include "BLI_time.h"
#include "BLI_utildefines.h"

#include "BLT_translation.hh"

#include "DNA_screen_types.h"
#include "DNA_userdef_types.h"

#include "BKE_context.hh"
#include "BKE_screen.hh"

#include "RNA_access.hh"

#include "BLF_api.hh"

#include "WM_api.hh"
#include "WM_types.hh"

#include "ED_screen.hh"

#include "UI_interface_c.hh"
#include "UI_interface_icons.hh"
#include "UI_resources.hh"
#include "UI_view2d.hh"

#include "GPU_batch_presets.hh"
#include "GPU_immediate.hh"
#include "GPU_matrix.hh"
#include "GPU_state.hh"

#include "RNA_prototypes.hh"

#include "interface_intern.hh"

namespace blender::ui {

/* -------------------------------------------------------------------- */
/** \name Defines & Structs
 * \{ */

#define ANIMATION_TIME 0.30
#define ANIMATION_INTERVAL 0.02

enum PanelRuntimeFlag {
  PANEL_LAST_ADDED = (1 << 0),
  PANEL_ACTIVE = (1 << 2),
  PANEL_WAS_ACTIVE = (1 << 3),
  PANEL_ANIM_ALIGN = (1 << 4),
  PANEL_NEW_ADDED = (1 << 5),
  PANEL_SEARCH_FILTER_MATCH = (1 << 7),
  /**
   * Use the status set by property search (#PANEL_SEARCH_FILTER_MATCH)
   * instead of #PNL_CLOSED. Set to true on every property search update.
   */
  PANEL_USE_CLOSED_FROM_SEARCH = (1 << 8),
  /** The Panel was before the start of the current / latest layout pass. */
  PANEL_WAS_CLOSED = (1 << 9),
  /**
   * Set when the panel is being dragged and while it animates back to its aligned
   * position. Unlike #PANEL_STATE_ANIMATION, this is applied to sub-panels as well.
   */
  PANEL_IS_DRAG_DROP = (1 << 10),
  /** Draw a border with the active color around the panel. */
  PANEL_ACTIVE_BORDER = (1 << 11),
};

/* The state of the mouse position relative to the panel. */
enum PanelMouseState {
  PANEL_MOUSE_OUTSIDE,        /** Mouse is not in the panel. */
  PANEL_MOUSE_INSIDE_CONTENT, /** Mouse is in the actual panel content. */
  PANEL_MOUSE_INSIDE_HEADER,  /** Mouse is in the panel header. */
  PANEL_MOUSE_INSIDE_LAYOUT_PANEL_HEADER /** Mouse is in the header of a layout panel. */,
};

enum HandlePanelState {
  PANEL_STATE_DRAG,
  PANEL_STATE_ANIMATION,
  PANEL_STATE_EXIT,
};

struct HandlePanelData {
  HandlePanelState state;

  /* Animation. */
  wmTimer *animtimer;
  double starttime;

  /* Dragging. */
  int startx, starty;
  int startofsx, startofsy;
  float start_cur_xmin, start_cur_ymin;
};

struct PanelSort {
  Panel *panel;
  int new_offset_x;
  int new_offset_y;
};

static void panel_set_expansion_from_list_data(const bContext *C, Panel *panel);
static int get_panel_real_size_y(const Panel *panel);
static void panel_activate_state(const bContext *C, Panel *panel, const HandlePanelState state);
static bool panel_type_context_poll(ARegion *region,
                                    const PanelType *panel_type,
                                    const char *context);

/** \} */

/* -------------------------------------------------------------------- */
/** \name Local Functions
 * \{ */

static bool panel_active_animation_changed(ListBaseT<Panel> *lb,
                                           Panel **r_panel_animation,
                                           bool *r_no_animation)
{
  for (Panel &panel : *lb) {
    /* Detect panel active flag changes. */
    if (!(panel.type && panel.type->parent)) {
      if ((panel.runtime_flag & PANEL_WAS_ACTIVE) && !(panel.runtime_flag & PANEL_ACTIVE)) {
        return true;
      }
      if (!(panel.runtime_flag & PANEL_WAS_ACTIVE) && (panel.runtime_flag & PANEL_ACTIVE)) {
        return true;
      }
    }

    /* Detect changes in panel expansions. */
    if (bool(panel.runtime_flag & PANEL_WAS_CLOSED) != panel_is_closed(&panel)) {
      *r_panel_animation = &panel;
      return false;
    }

    if ((panel.runtime_flag & PANEL_ACTIVE) && !panel_is_closed(&panel)) {
      if (panel_active_animation_changed(&panel.children, r_panel_animation, r_no_animation)) {
        return true;
      }
    }

    /* Detect animation. */
    if (panel.activedata) {
      HandlePanelData *data = static_cast<HandlePanelData *>(panel.activedata);
      if (data->state == PANEL_STATE_ANIMATION) {
        *r_panel_animation = &panel;
      }
      else {
        /* Don't animate while handling other interaction. */
        *r_no_animation = true;
      }
    }
    if ((panel.runtime_flag & PANEL_ANIM_ALIGN) && !(*r_panel_animation)) {
      *r_panel_animation = &panel;
    }
  }

  return false;
}

/**
 * \return True if the properties editor switch tabs since the last layout pass.
 */
static bool properties_space_needs_realign(const ScrArea *area, const ARegion *region)
{
  if (area->spacetype == SPACE_PROPERTIES && region->regiontype == RGN_TYPE_WINDOW) {
    const SpaceProperties *sbuts = static_cast<SpaceProperties *>(area->spacedata.first);

    if (sbuts->mainbo != sbuts->mainb) {
      return true;
    }
  }

  return false;
}

static bool panels_need_realign(const ScrArea *area, ARegion *region, Panel **r_panel_animation)
{
  *r_panel_animation = nullptr;

  if (properties_space_needs_realign(area, region)) {
    return true;
  }

  /* Detect if a panel was added or removed. */
  Panel *panel_animation = nullptr;
  bool no_animation = false;
  if (panel_active_animation_changed(&region->panels, &panel_animation, &no_animation)) {
    return true;
  }

  /* Detect panel marked for animation, if we're not already animating. */
  if (panel_animation) {
    if (!no_animation) {
      *r_panel_animation = panel_animation;
    }
    return true;
  }

  return false;
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name Functions for Instanced Panels
 * \{ */

static Panel *panel_add_instanced(ListBaseT<Panel> *panels,
                                  PanelType *panel_type,
                                  PointerRNA *custom_data)
{
  Panel *panel = BKE_panel_new(panel_type);

  panel->runtime->custom_data_ptr = custom_data;
  panel->runtime_flag |= PANEL_NEW_ADDED;

  /* Add the panel's children too. Although they aren't instanced panels, we can still use this
   * function to create them, as panel_begin does other things we don't need to do. */
  for (LinkData &child : panel_type->children) {
    PanelType *child_type = static_cast<PanelType *>(child.data);
    panel_add_instanced(&panel->children, child_type, custom_data);
  }

  /* Make sure the panel is added to the end of the display-order as well. This is needed for
   * loading existing files.
   *
   * NOTE: We could use special behavior to place it after the panel that starts the list of
   * instanced panels, but that would add complexity that isn't needed for now. */
  int max_sortorder = 0;
  for (Panel &existing_panel : *panels) {
    max_sortorder = std::max(existing_panel.sortorder, max_sortorder);
  }
  panel->sortorder = max_sortorder + 1;

  BLI_addtail(panels, panel);

  return panel;
}

Panel *panel_add_instanced(const bContext *C,
                           ARegion *region,
                           ListBaseT<Panel> *panels,
                           const char *panel_idname,
                           PointerRNA *custom_data)
{
  ARegionType *region_type = region->runtime->type;

  PanelType *panel_type = static_cast<PanelType *>(
      BLI_findstring(&region_type->paneltypes, panel_idname, offsetof(PanelType, idname)));

  if (panel_type == nullptr) {
    printf("Panel type '%s' not found.\n", panel_idname);
    return nullptr;
  }

  Panel *new_panel = panel_add_instanced(panels, panel_type, custom_data);

  /* Do this after #panel_add_instatnced so all sub-panels are added. */
  panel_set_expansion_from_list_data(C, new_panel);

  return new_panel;
}

void list_panel_unique_str(Panel *panel, char *r_name)
{
  /* The panel sort-order will be unique for a specific panel type because the instanced
   * panel list is regenerated for every change in the data order / length. */
  BLI_snprintf_utf8(r_name, INSTANCED_PANEL_UNIQUE_STR_SIZE, "%d", panel->sortorder);
}

/**
 * Free a panel and its children. Custom data is shared by the panel and its children
 * and is freed by #panels_free_instanced.
 *
 * \note The only panels that should need to be deleted at runtime are panels with the
 * #PANEL_TYPE_INSTANCED flag set.
 */
static void panel_delete(ARegion *region, ListBaseT<Panel> *panels, Panel *panel)
{
  /* Recursively delete children. */
  for (Panel &child : panel->children.items_mutable()) {
    panel_delete(region, &panel->children, &child);
  }
  panel->children.free_no_destruct();

  BLI_remlink(panels, panel);
  BKE_panel_free(panel);
}

static void panel_exit_state_recursive(const bContext *C, Panel &panel)
{
  if (panel.activedata != nullptr) {
    panel_activate_state(C, &panel, PANEL_STATE_EXIT);
  }
  for (Panel &child : panel.children) {
    panel_exit_state_recursive(C, child);
  }
}

void panels_free_instanced(const bContext *C, ARegion *region)
{
  /* Delete panels with the instanced flag. */
  for (Panel &panel : region->panels.items_mutable()) {
    if (!panel.type) {
      continue;
    }
    if ((panel.type->flag & PANEL_TYPE_INSTANCED) == 0) {
      continue;
    }
    /* Make sure any active handler is removed from this panel or its children before deleting
     * them. */
    if (C != nullptr) {
      panel_exit_state_recursive(C, panel);
    }

    /* Free panel's custom data. */
    if (panel.runtime->custom_data_ptr != nullptr) {
      MEM_delete(panel.runtime->custom_data_ptr);
    }

    /* Free the panel and its sub-panels. */
    panel_delete(region, &region->panels, &panel);
  }
}

bool panel_list_matches_data(ARegion *region,
                             ListBase *data,
                             ListPanelIDFromDataFunc panel_idname_func)
{
  /* Check for nullptr data. */
  int data_len = 0;
  Link *data_link = nullptr;
  if (data == nullptr) {
    data_len = 0;
    data_link = nullptr;
  }
  else {
    data_len = BLI_listbase_count(data);
    data_link = static_cast<Link *>(data->first);
  }

  int i = 0;
  for (Panel &panel : region->panels) {
    if (panel.type != nullptr && panel.type->flag & PANEL_TYPE_INSTANCED) {
      /* The panels were reordered by drag and drop. */
      if (panel.flag & PNL_INSTANCED_LIST_ORDER_CHANGED) {
        return false;
      }

      /* We reached the last data item before the last instanced panel. */
      if (data_link == nullptr) {
        return false;
      }

      /* Check if the panel type matches the panel type from the data item. */
      char panel_idname[MAX_NAME];
      panel_idname_func(data_link, panel_idname);
      if (!STREQ(panel_idname, panel.type->idname)) {
        return false;
      }

      data_link = data_link->next;
      i++;
    }
  }

  /* If we didn't make it to the last list item, the panel list isn't complete. */
  if (i != data_len) {
    return false;
  }

  return true;
}

static void reorder_instanced_panel_list(bContext *C, ARegion *region, Panel *drag_panel)
{
  /* Without a type we cannot access the reorder callback. */
  if (drag_panel->type == nullptr) {
    return;
  }
  /* Don't reorder if this instanced panel doesn't support drag and drop reordering. */
  if (drag_panel->type->reorder == nullptr) {
    return;
  }

  char *context = nullptr;
  if (!panel_category_is_visible(region)) {
    context = drag_panel->type->context;
  }

  /* Find how many instanced panels with this context string. */
  int start_index = -1;
  Vector<Panel *> panel_sort;
  for (Panel &panel : region->panels) {
    if (panel.type) {
      if (panel.type->flag & PANEL_TYPE_INSTANCED) {
        if (panel_type_context_poll(region, panel.type, context)) {
          if (&panel == drag_panel) {
            BLI_assert(start_index == -1); /* This panel should only appear once. */
            start_index = panel_sort.size();
          }
          panel_sort.append(&panel);
        }
      }
    }
  }
  BLI_assert(start_index != -1); /* The drag panel should definitely be in the list. */

  /* Sort the matching instanced panels by their display order. */
  std::stable_sort(panel_sort.begin(), panel_sort.end(), [](const Panel *a, const Panel *b) {
    return a->sortorder < b->sortorder;
  });

  /* Find how many of those panels are above this panel. */
  int move_to_index = 0;
  for (; move_to_index < panel_sort.size(); move_to_index++) {
    if (panel_sort[move_to_index] == drag_panel) {
      break;
    }
  }

  if (move_to_index == start_index) {
    /* In this case, the reorder was not changed, so don't do any updates or call the callback. */
    return;
  }

  /* Set the bit to tell the interface to instanced the list. */
  drag_panel->flag |= PNL_INSTANCED_LIST_ORDER_CHANGED;

  CTX_store_set(C, drag_panel->runtime->context);

  /* Finally, move this panel's list item to the new index in its list. */
  drag_panel->type->reorder(C, drag_panel, move_to_index);

  CTX_store_set(C, nullptr);
}

/**
 * Recursive implementation for #panel_set_expansion_from_list_data.
 *
 * \return Whether the closed flag for the panel or any sub-panels changed.
 */
static bool panel_set_expand_from_list_data_recursive(Panel *panel, short flag, short *flag_index)
{
  const bool open = (flag & (1 << *flag_index));
  bool changed = (open == panel_is_closed(panel));

  SET_FLAG_FROM_TEST(panel->flag, !open, PNL_CLOSED);

  for (Panel &child : panel->children) {
    *flag_index = *flag_index + 1;
    changed |= panel_set_expand_from_list_data_recursive(&child, flag, flag_index);
  }
  return changed;
}

/**
 * Set the expansion of the panel and its sub-panels from the flag stored in the
 * corresponding list data. The flag has expansion stored in each bit in depth first order.
 */
static void panel_set_expansion_from_list_data(const bContext *C, Panel *panel)
{
  BLI_assert(panel->type != nullptr);
  BLI_assert(panel->type->flag & PANEL_TYPE_INSTANCED);
  if (panel->type->get_list_data_expand_flag == nullptr) {
    /* Instanced panel doesn't support loading expansion. */
    return;
  }

  const short expand_flag = panel->type->get_list_data_expand_flag(C, panel);
  short flag_index = 0;

  /* Start panel animation if the open state was changed. */
  if (panel_set_expand_from_list_data_recursive(panel, expand_flag, &flag_index)) {
    panel_activate_state(C, panel, PANEL_STATE_ANIMATION);
  }
}

/**
 * Set expansion based on the data for instanced panels.
 */
static void region_panels_set_expansion_from_list_data(const bContext *C, ARegion *region)
{
  for (Panel &panel : region->panels) {
    if (panel.runtime_flag & PANEL_ACTIVE) {
      PanelType *panel_type = panel.type;
      if (panel_type != nullptr && panel.type->flag & PANEL_TYPE_INSTANCED) {
        panel_set_expansion_from_list_data(C, &panel);
      }
    }
  }
}

/**
 * Recursive implementation for #set_panels_list_data_expand_flag.
 */
static void get_panel_expand_flag(const Panel *panel, short *flag, short *flag_index)
{
  const bool open = !(panel->flag & PNL_CLOSED);
  SET_FLAG_FROM_TEST(*flag, open, (1 << *flag_index));

  for (const Panel &child : panel->children) {
    *flag_index = *flag_index + 1;
    get_panel_expand_flag(&child, flag, flag_index);
  }
}

/**
 * Call the callback to store the panel and sub-panel expansion settings in the list item that
 * corresponds to each instanced panel.
 *
 * \note This needs to iterate through all of the region's panels because the panel with changed
 * expansion might have been the sub-panel of an instanced panel, meaning it might not know
 * which list item it corresponds to.
 */
static void set_panels_list_data_expand_flag(const bContext *C, const ARegion *region)
{
  for (Panel &panel : region->panels) {
    PanelType *panel_type = panel.type;
    if (panel_type == nullptr) {
      continue;
    }

    /* Check for #PANEL_ACTIVE so we only set the expand flag for active panels. */
    if (panel_type->flag & PANEL_TYPE_INSTANCED && panel.runtime_flag & PANEL_ACTIVE) {
      short expand_flag;
      short flag_index = 0;
      get_panel_expand_flag(&panel, &expand_flag, &flag_index);
      if (panel.type->set_list_data_expand_flag) {
        panel.type->set_list_data_expand_flag(C, &panel, expand_flag);
      }
    }
  }
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name Panels
 * \{ */

static bool panel_custom_pin_to_last_get(const Panel *panel)
{
  if (panel->type->pin_to_last_property[0] != '\0') {
    PointerRNA *ptr = panel_custom_data_get(panel);
    if (ptr != nullptr && !RNA_pointer_is_null(ptr)) {
      return RNA_boolean_get(ptr, panel->type->pin_to_last_property);
    }
  }

  return false;
}

static void panel_custom_pin_to_last_set(const bContext *C, const Panel *panel, const bool value)
{
  if (panel->type->pin_to_last_property[0] != '\0') {
    PointerRNA *ptr = panel_custom_data_get(panel);
    if (ptr != nullptr && !RNA_pointer_is_null(ptr)) {
      PropertyRNA *prop = RNA_struct_find_property(ptr, panel->type->pin_to_last_property);
      RNA_boolean_set(ptr, panel->type->pin_to_last_property, value);
      RNA_property_update(const_cast<bContext *>(C), ptr, prop);
    }
  }
}

static bool panel_custom_data_active_get(const Panel *panel)
{
  /* The caller should make sure the panel is active and has a type. */
  BLI_assert(panel_is_active(panel));
  BLI_assert(panel->type != nullptr);

  if (panel->type->active_property[0] != '\0') {
    PointerRNA *ptr = panel_custom_data_get(panel);
    if (ptr != nullptr && !RNA_pointer_is_null(ptr)) {
      return RNA_boolean_get(ptr, panel->type->active_property);
    }
  }

  return false;
}

static void panel_custom_data_active_set(Panel *panel)
{
  /* Since the panel is interacted with, it should be active and have a type. */
  BLI_assert(panel_is_active(panel));
  BLI_assert(panel->type != nullptr);

  if (panel->type->active_property[0] != '\0') {
    PointerRNA *ptr = panel_custom_data_get(panel);
    BLI_assert(RNA_struct_find_property(ptr, panel->type->active_property) != nullptr);
    if (ptr != nullptr && !RNA_pointer_is_null(ptr)) {
      RNA_boolean_set(ptr, panel->type->active_property, true);
    }
  }
}

/**
 * Set flag state for a panel and its sub-panels.
 */
static void panel_set_flag_recursive(Panel *panel, ePanel_Flag flag, bool value)
{
  SET_FLAG_FROM_TEST(panel->flag, value, flag);

  for (Panel &child : panel->children) {
    panel_set_flag_recursive(&child, flag, value);
  }
}

/**
 * Set runtime flag state for a panel and its sub-panels.
 */
static void panel_set_runtime_flag_recursive(Panel *panel, short flag, bool value)
{
  SET_FLAG_FROM_TEST(panel->runtime_flag, value, flag);

  for (Panel &sub_panel : panel->children) {
    panel_set_runtime_flag_recursive(&sub_panel, flag, value);
  }
}

static void panels_collapse_all(ARegion *region, const Panel *from_panel)
{
  const bool has_category = panel_category_is_visible(region);
  const char *category = has_category ? panel_category_active_get(region, false) : nullptr;
  const PanelType *from_pt = from_panel->type;

  for (Panel &panel : region->panels) {
    PanelType *pt = panel.type;

    /* Close panels with headers in the same context. */
    if (pt && from_pt && !(pt->flag & PANEL_TYPE_NO_HEADER)) {
      if (!pt->context[0] || !from_pt->context[0] || STREQ(pt->context, from_pt->context)) {
        if ((panel.flag & PNL_PIN) || !category || !pt->category[0] ||
            STREQ(pt->category, category))
        {
          panel.flag |= PNL_CLOSED;
        }
      }
    }
  }
}

static bool panel_type_context_poll(ARegion *region,
                                    const PanelType *panel_type,
                                    const char *context)
{
  if (!region->runtime->panels_category.is_empty()) {
    return STREQ(panel_type->category, panel_category_active_get(region, false));
  }

  if (panel_type->context[0] && STREQ(panel_type->context, context)) {
    return true;
  }

  return false;
}

Panel *panel_find_by_type(ListBaseT<Panel> *lb, const PanelType *pt)
{
  const char *idname = pt->idname;

  for (Panel &panel : *lb) {
    if (STREQLEN(panel.panelname, idname, sizeof(panel.panelname))) {
      return &panel;
    }
  }
  return nullptr;
}

Panel *panel_begin(
    ARegion *region, ListBaseT<Panel> *lb, Block *block, PanelType *pt, Panel *panel, bool *r_open)
{
  Panel *panel_last;
  const char *drawname = CTX_IFACE_(pt->translation_context, pt->label);
  const bool newpanel = (panel == nullptr);

  if (newpanel) {
    panel = BKE_panel_new(pt);

    if (pt->flag & PANEL_TYPE_DEFAULT_CLOSED) {
      panel->flag |= PNL_CLOSED;
      panel->runtime_flag |= PANEL_WAS_CLOSED;
    }

    panel->ofsx = 0;
    panel->ofsy = 0;
    panel->sizex = 0;
    panel->sizey = 0;
    panel->blocksizex = 0;
    panel->blocksizey = 0;
    panel->runtime_flag |= PANEL_NEW_ADDED;

    BLI_addtail(lb, panel);
  }
  else {
    /* Panel already exists. */
    panel->type = pt;
  }

  panel->runtime->block = block;

  panel_drawname_set(panel, drawname);

  /* If a new panel is added, we insert it right after the panel that was last added.
   * This way new panels are inserted in the right place between versions. */
  for (panel_last = static_cast<Panel *>(lb->first); panel_last; panel_last = panel_last->next) {
    if (panel_last->runtime_flag & PANEL_LAST_ADDED) {
      BLI_remlink(lb, panel);
      BLI_insertlinkafter(lb, panel_last, panel);
      break;
    }
  }

  if (newpanel) {
    panel->sortorder = (panel_last) ? panel_last->sortorder + 1 : 0;

    for (Panel &panel_next : *lb) {
      if (&panel_next != panel && panel_next.sortorder >= panel->sortorder) {
        panel_next.sortorder++;
      }
    }
  }

  if (panel_last) {
    panel_last->runtime_flag &= ~PANEL_LAST_ADDED;
  }

  /* Assign the new panel to the block. */
  block->panel = panel;
  panel->runtime_flag |= PANEL_ACTIVE | PANEL_LAST_ADDED;
  if (region->alignment == RGN_ALIGN_FLOAT) {
    block_theme_style_set(block, BLOCK_THEME_STYLE_POPUP);
  }

  *r_open = !panel_is_closed(panel);

  return panel;
}

void panel_header_buttons_begin(Panel *panel)
{
  Block *block = panel->runtime->block;

  block_new_button_group(block, UI_BUTTON_GROUP_LOCK | UI_BUTTON_GROUP_PANEL_HEADER);
}

void panel_header_buttons_end(Panel *panel)
{
  Block *block = panel->runtime->block;

  /* A button group should always be created in #panel_header_buttons_begin. */
  BLI_assert(!block->button_groups.is_empty());

  ButtonGroup &button_group = block->button_groups.last();
  button_group.flag &= ~UI_BUTTON_GROUP_LOCK;

  /* Repurpose the first header button group if it is empty, in case the first button added to
   * the panel doesn't add a new group (if the button is created directly rather than through an
   * interface layout call). */
  if (block->button_groups.size() == 1 && button_group.buttons.is_empty()) {
    button_group.flag &= ~UI_BUTTON_GROUP_PANEL_HEADER;
  }
  else {
    /* Always add a new button group. Although this may result in many empty groups, without it,
     * new buttons in the panel body not protected with a #block_new_button_group call would
     * end up in the panel header group. */
    block_new_button_group(block, ButtonGroupFlag(0));
  }
}

static float panel_region_offset_x_get(const ARegion *region)
{
  if (panel_category_tabs_is_visible(region)) {
    if (RGN_ALIGN_ENUM_FROM_MASK(region->alignment) != RGN_ALIGN_RIGHT) {
      return UI_PANEL_CATEGORY_MARGIN_WIDTH;
    }
  }

  return 0.0f;
}

/**
 * Starting from the "block size" set in #panel_end, calculate the full size
 * of the panel including the sub-panel headers and buttons.
 */
static void panel_calculate_size_recursive(ARegion *region, Panel *panel)
{
  int width = panel->blocksizex;
  int height = panel->blocksizey;

  for (Panel &child_panel : panel->children) {
    if (child_panel.runtime_flag & PANEL_ACTIVE) {
      panel_calculate_size_recursive(region, &child_panel);
      width = max_ii(width, child_panel.sizex);
      height += get_panel_real_size_y(&child_panel);
    }
  }

  /* Update total panel size. */
  if (panel->runtime_flag & PANEL_NEW_ADDED) {
    panel->runtime_flag &= ~PANEL_NEW_ADDED;
    panel->sizex = width;
    panel->sizey = height;
  }
  else {
    const int old_sizex = panel->sizex, old_sizey = panel->sizey;
    const int old_region_ofsx = panel->runtime->region_ofsx;

    /* Update width/height if non-zero. */
    if (width != 0) {
      panel->sizex = width;
    }
    if (height != 0 || !panel_is_closed(panel)) {
      panel->sizey = height;
    }

    /* Check if we need to do an animation. */
    if (panel->sizex != old_sizex || panel->sizey != old_sizey) {
      panel->runtime_flag |= PANEL_ANIM_ALIGN;
      panel->ofsy += old_sizey - panel->sizey;
    }

    panel->runtime->region_ofsx = panel_region_offset_x_get(region);
    if (old_region_ofsx != panel->runtime->region_ofsx) {
      panel->runtime_flag |= PANEL_ANIM_ALIGN;
    }
  }
}

void panel_end(Panel *panel, int width, int height)
{
  /* Store the size of the buttons layout in the panel. The actual panel size
   * (including sub-panels) is calculated in #panels_end. */
  panel->blocksizex = width;
  panel->blocksizey = height;
}

void panel_drawname_set(Panel *panel, StringRef name)
{
  MEM_SAFE_DELETE(panel->drawname);
  panel->drawname = BLI_strdupn(name.data(), name.size());
}

static void offset_panel_block(Block *block)
{
  const uiStyle *style = style_get_dpi();

  /* Compute bounds and offset. */
  block_bounds_calc(block);

  const int panels_space = style->panelspace;
  const int ofsy = block->panel->sizey - panels_space;

  for (Button &but : block->buttons()) {
    but.rect.ymin += ofsy;
    but.rect.ymax += ofsy;
  }
  for (LayoutPanelBody &body : block->panel->runtime->layout_panels.bodies) {
    body.start_y -= panels_space;
    body.end_y -= panels_space;
  }
  for (LayoutPanelHeader &header : block->panel->runtime->layout_panels.headers) {
    header.start_y -= panels_space;
    header.end_y -= panels_space;
  }

  block->rect.xmax = block->panel->sizex;
  block->rect.ymax = block->panel->sizey;
  block->rect.xmin = block->rect.ymin = 0.0;
}

void panel_tag_search_filter_match(Panel *panel)
{
  panel->runtime_flag |= PANEL_SEARCH_FILTER_MATCH;
}

static void panel_matches_search_filter_recursive(const Panel *panel, bool *filter_matches)
{
  *filter_matches |= bool(panel->runtime_flag & PANEL_SEARCH_FILTER_MATCH);

  /* If the panel has no match we need to make sure that its children are too. */
  if (!*filter_matches) {
    for (const Panel &child_panel : panel->children) {
      panel_matches_search_filter_recursive(&child_panel, filter_matches);
    }
  }
}

bool panel_matches_search_filter(const Panel *panel)
{
  bool search_filter_matches = false;
  panel_matches_search_filter_recursive(panel, &search_filter_matches);
  return search_filter_matches;
}

/**
 * Set the flag telling the panel to use its search result status for its expansion.
 */
static void panel_set_expansion_from_search_filter_recursive(const bContext *C,
                                                             Panel *panel,
                                                             const bool use_search_closed)
{
  /* This has to run on inactive panels that may not have a type,
   * but we can prevent running on header-less panels in some cases. */
  if (panel->type == nullptr || !(panel->type->flag & PANEL_TYPE_NO_HEADER)) {
    SET_FLAG_FROM_TEST(panel->runtime_flag, use_search_closed, PANEL_USE_CLOSED_FROM_SEARCH);
  }

  for (Panel &child_panel : panel->children) {
    /* Don't check if the sub-panel is active, otherwise the
     * expansion won't be reset when the parent is closed. */
    panel_set_expansion_from_search_filter_recursive(C, &child_panel, use_search_closed);
  }
}

/**
 * Set the flag telling every panel to override its expansion with its search result status.
 */
static void region_panels_set_expansion_from_search_filter(const bContext *C,
                                                           ARegion *region,
                                                           const bool use_search_closed)
{
  for (Panel &panel : region->panels) {
    /* Don't check if the panel is active, otherwise the expansion won't
     * be correct when switching back to tab after exiting search. */
    panel_set_expansion_from_search_filter_recursive(C, &panel, use_search_closed);
  }
  set_panels_list_data_expand_flag(C, region);
}

/**
 * Hide buttons in invisible layouts, which are created because buttons must be
 * added for all panels in order to search, even panels that will end up closed.
 */
static void panel_remove_invisible_layouts_recursive(Panel *panel, const Panel *parent_panel)
{
  Block *block = panel->runtime->block;
  BLI_assert(block != nullptr);
  BLI_assert(block->active);
  if (parent_panel != nullptr && panel_is_closed(parent_panel)) {
    /* The parent panel is closed, so this panel can be completely removed. */
    block_set_search_only(block, true);
    for (Button &but : block->buttons()) {
      but.flag |= UI_HIDDEN;
    }
  }
  else if (panel_is_closed(panel)) {
    /* If sub-panels have no search results but the parent panel does, then the parent panel open
     * and the sub-panels will close. In that case there must be a way to hide the buttons in the
     * panel but keep the header buttons. */
    for (const ButtonGroup &button_group : block->button_groups) {
      if (button_group.flag & UI_BUTTON_GROUP_PANEL_HEADER) {
        continue;
      }
      for (Button *but : button_group.buttons) {
        but->flag |= UI_HIDDEN;
      }
    }
  }

  for (Panel &child_panel : panel->children) {
    if (child_panel.runtime_flag & PANEL_ACTIVE) {
      BLI_assert(child_panel.runtime->block != nullptr);
      panel_remove_invisible_layouts_recursive(&child_panel, panel);
    }
  }
}

static void region_panels_remove_invisible_layouts(ARegion *region)
{
  for (Panel &panel : region->panels) {
    if (panel.runtime_flag & PANEL_ACTIVE) {
      BLI_assert(panel.runtime->block != nullptr);
      panel_remove_invisible_layouts_recursive(&panel, nullptr);
    }
  }
}

bool panel_is_closed(const Panel *panel)
{
  /* Header-less panels can never be closed, otherwise they could disappear. */
  if (panel->type && panel->type->flag & PANEL_TYPE_NO_HEADER) {
    return false;
  }

  if (panel->runtime_flag & PANEL_USE_CLOSED_FROM_SEARCH) {
    return !panel_matches_search_filter(panel);
  }

  return panel->flag & PNL_CLOSED;
}

bool panel_is_active(const Panel *panel)
{
  return panel->runtime_flag & PANEL_ACTIVE;
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name Drawing
 * \{ */

void panels_draw(const bContext *C, ARegion *region)
{
  /* Draw in reverse order, because #Blocks are added in reverse order
   * and we need child panels to draw on top. */
  for (Block &block : region->runtime->uiblocks.items_reversed()) {
    if (block.active && block.panel && !panel_is_dragging(block.panel) &&
        !block_is_search_only(&block))
    {
      block_draw(C, &block);
    }
  }

  for (Block &block : region->runtime->uiblocks.items_reversed()) {
    if (block.active && block.panel && panel_is_dragging(block.panel) &&
        !block_is_search_only(&block))
    {
      block_draw(C, &block);
    }
  }
}

#define PNL_ICON UI_UNIT_X /* Could be UI_UNIT_Y too. */

void panel_label_offset(const Block *block, int *r_x, int *r_y)
{
  Panel *panel = block->panel;
  const bool is_subpanel = (panel->type && panel->type->parent);

  *r_x = UI_UNIT_X * 1.0f;
  *r_y = UI_UNIT_Y * 1.5f;

  if (is_subpanel) {
    *r_x += (0.7f * UI_UNIT_X);
  }
}

static void panel_title_color_get(const Panel *panel,
                                  const bool show_background,
                                  const bool region_search_filter_active,
                                  uchar r_color[4])
{
  if (!show_background) {
    /* Use menu colors for floating panels. */
    bTheme *btheme = theme::theme_get();
    const uiWidgetColors *wcol = &btheme->tui.wcol_menu_back;
    copy_v4_v4_uchar(r_color, static_cast<const uchar *>(wcol->text));
    return;
  }

  const bool search_match = panel_matches_search_filter(panel);

  theme::get_color_4ubv(TH_TITLE, r_color);
  if (region_search_filter_active && !search_match) {
    r_color[0] *= 0.5;
    r_color[1] *= 0.5;
    r_color[2] *= 0.5;
  }
}

static void panel_draw_border(const Panel *panel,
                              const rcti *rect,
                              const rcti *header_rect,
                              const bool is_active)
{
  const bool is_subpanel = panel->type->parent != nullptr;
  if (is_subpanel) {
    return;
  }

  float color[4];
  theme::get_color_4fv(is_active ? TH_PANEL_ACTIVE : TH_PANEL_OUTLINE, color);
  if (color[3] == 0.0f) {
    return; /* No border to draw. */
  }

  const bTheme *btheme = theme::theme_get();
  const float aspect = panel->runtime->block->aspect;
  const float radius = (btheme->tui.panel_roundness * U.widget_unit * 0.5f) / aspect;
  draw_roundbox_corner_set(CNR_ALL);

  rctf box_rect;
  box_rect.xmin = rect->xmin;
  box_rect.xmax = rect->xmax;
  box_rect.ymin = panel_is_closed(panel) ? header_rect->ymin : rect->ymin;
  box_rect.ymax = header_rect->ymax;
  draw_roundbox_4fv(&box_rect, false, radius, color);
}

static void panel_draw_aligned_widgets(const uiStyle *style,
                                       const Panel *panel,
                                       const rcti *header_rect,
                                       const float aspect,
                                       const bool show_pin,
                                       const bool show_background,
                                       const bool region_search_filter_active)
{
  const bool is_subpanel = panel->type->parent != nullptr;
  const uiFontStyle *fontstyle = (is_subpanel) ? &style->widget : &style->paneltitle;

  const int header_height = BLI_rcti_size_y(header_rect);
  const int header_width = BLI_rcti_size_x(header_rect);
  const int scaled_unit = round_fl_to_int(UI_UNIT_X / aspect);

  /* Offset triangle and text to the right for sub-panels. */
  rcti widget_rect;
  widget_rect.xmin = header_rect->xmin + (is_subpanel ? scaled_unit * 0.7f : 0);
  widget_rect.xmax = header_rect->xmax;
  widget_rect.ymin = header_rect->ymin;
  widget_rect.ymax = header_rect->ymax;

  uchar title_color[4];
  panel_title_color_get(panel, show_background, region_search_filter_active, title_color);
  title_color[3] = 255;

  /* Draw collapse icon. */
  {
    const float size_y = BLI_rcti_size_y(&widget_rect);
    GPU_blend(GPU_BLEND_ALPHA);
    float alpha = 0.8f;
    /* Dim as its space is reduced to zero. */
    if (header_width < (scaled_unit * 4)) {
      alpha *= std::max(float(header_width - scaled_unit) / float(scaled_unit * 3), 0.0f);
    }
    icon_draw_ex(widget_rect.xmin + size_y * 0.2f,
                 widget_rect.ymin + size_y * (panel_is_closed(panel) ? 0.17f : 0.14f),
                 panel_is_closed(panel) ? ICON_RIGHTARROW : ICON_DOWNARROW_HLT,
                 aspect * UI_INV_SCALE_FAC,
                 alpha,
                 0.0f,
                 title_color,
                 false,
                 UI_NO_ICON_OVERLAY_TEXT);
    GPU_blend(GPU_BLEND_NONE);
  }

  /* Draw text label. */
  if (panel->drawname && panel->drawname[0] != '\0') {
    rcti title_rect;
    title_rect.xmin = widget_rect.xmin + (panel->labelofs / aspect) + scaled_unit * 1.1f;
    title_rect.xmax = widget_rect.xmax;
    if (!is_subpanel && show_background) {
      /* Don't draw over the drag widget. */
      title_rect.xmax -= scaled_unit;
    }
    title_rect.ymin = widget_rect.ymin - 2.0f / aspect;
    title_rect.ymax = widget_rect.ymax;

    FontStyleDrawParams params{};
    params.align = UI_STYLE_TEXT_LEFT;
    fontstyle_draw(
        fontstyle, &title_rect, panel->drawname, strlen(panel->drawname), title_color, &params);
  }

  /* Draw the pin icon. */
  if (show_pin && (panel->flag & PNL_PIN)) {
    GPU_blend(GPU_BLEND_ALPHA);
    icon_draw_ex(widget_rect.xmax - scaled_unit * 2.2f,
                 widget_rect.ymin + 5.0f / aspect,
                 ICON_PINNED,
                 aspect * UI_INV_SCALE_FAC,
                 1.0f,
                 0.0f,
                 title_color,
                 false,
                 UI_NO_ICON_OVERLAY_TEXT);
    GPU_blend(GPU_BLEND_NONE);
  }

  /* Draw drag widget. */
  if (!is_subpanel && show_background) {
    const float x = widget_rect.xmax - scaled_unit * 1.15;
    const float y = widget_rect.ymin + (header_height - (header_height * 0.7f)) * 0.5f;
    const bool is_pin = panel_custom_pin_to_last_get(panel);
    const int icon = is_pin ? ICON_PINNED : ICON_GRIP;
    const float size = aspect * UI_INV_SCALE_FAC;
    float alpha = is_pin ? 1.0f : 0.5f;
    if (header_width < (scaled_unit * 5)) {
      alpha *= std::max((header_width - scaled_unit) / float(scaled_unit * 4), 0.0f);
    }
    icon_draw_ex(x, y, icon, size, alpha, 0.0f, title_color, false, UI_NO_ICON_OVERLAY_TEXT);
  }
}

void draw_layout_panels_backdrop(const ARegion *region,
                                 const Panel *panel,
                                 const float radius,
                                 float subpanel_backcolor[4])
{
  /* Draw backdrops for layout panels. */
  const float aspect = block_is_popup_any(panel->runtime->block) ? panel->runtime->block->aspect :
                                                                   1.0f;

  for (const LayoutPanelBody &body : panel->runtime->layout_panels.bodies) {

    rctf panel_blockspace = panel->runtime->block->rect;
    panel_blockspace.ymax = panel->runtime->block->rect.ymax + body.end_y;
    panel_blockspace.ymin = panel->runtime->block->rect.ymax + body.start_y;

    if (panel_blockspace.ymax <= panel->runtime->block->rect.ymin) {
      /* Layout panels no longer fits in block rectangle, stop drawing backdrops. */
      break;
    }
    if (panel_blockspace.ymin >= panel->runtime->block->rect.ymax) {
      /* Skip layout panels that scrolled to the top of the block rectangle. */
      continue;
    }
    /* If the layout panel is at the end of the root panel, it's bottom corners are rounded. */
    const bool is_main_panel_end = panel_blockspace.ymin - panel->runtime->block->rect.ymin <
                                   (10.0f * UI_SCALE_FAC / aspect);
    if (is_main_panel_end) {
      panel_blockspace.ymin = panel->runtime->block->rect.ymin;
      draw_roundbox_corner_set(CNR_BOTTOM_RIGHT | CNR_BOTTOM_LEFT);
    }
    else {
      draw_roundbox_corner_set(CNR_NONE);
    }
    panel_blockspace.ymax = std::min(panel_blockspace.ymax, panel->runtime->block->rect.ymax);

    rcti panel_pixelspace = rect_to_pixelrect(region, panel->runtime->block, &panel_blockspace);
    rctf panel_pixelspacef;
    BLI_rctf_rcti_copy(&panel_pixelspacef, &panel_pixelspace);
    draw_roundbox_4fv(&panel_pixelspacef, true, radius, subpanel_backcolor);
  }
}

static void panel_draw_softshadow(const rctf *box_rect,
                                  const int roundboxalign,
                                  const float radius,
                                  const float shadow_width)
{
  const float outline = U.pixelsize;

  rctf shadow_rect = *box_rect;
  BLI_rctf_pad(&shadow_rect, -outline, -outline);
  draw_roundbox_corner_set(roundboxalign);

  const float shadow_alpha = theme::theme_get()->tui.menu_shadow_fac;
  draw_dropshadow(&shadow_rect, radius, shadow_width, 1.0f, shadow_alpha);
}

static void panel_draw_aligned_backdrop(const ARegion *region,
                                        const Panel *panel,
                                        const rcti *rect,
                                        const rcti *header_rect)
{
  const bool is_open = !panel_is_closed(panel);
  const bool is_subpanel = panel->type->parent != nullptr;
  const bool has_header = (panel->type->flag & PANEL_TYPE_NO_HEADER) == 0;
  const bool is_dragging = panel_is_dragging(panel);

  if (is_subpanel && !is_open) {
    return;
  }

  const bTheme *btheme = theme::theme_get();
  const float aspect = panel->runtime->block->aspect;
  const float radius = btheme->tui.panel_roundness * U.widget_unit * 0.5f / aspect;

  immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
  GPU_blend(GPU_BLEND_ALPHA);

  /* Draw shadow on top-level panels with headers during drag or region overlap. */
  if (!is_subpanel && has_header && (region->overlap || is_dragging)) {
    /* Make shadow wider (at least 16px) while the panel is being dragged. */
    const float shadow_width = is_dragging ? max_ii(int(16.0f * UI_SCALE_FAC),
                                                    theme::get_menu_shadow_width()) :
                                             theme::get_menu_shadow_width();
    const int roundboxalign = is_open ? CNR_BOTTOM_RIGHT | CNR_BOTTOM_LEFT : CNR_ALL;

    rctf box_rect;
    box_rect.xmin = rect->xmin;
    box_rect.xmax = rect->xmax;
    box_rect.ymin = is_open ? rect->ymin : header_rect->ymin;
    box_rect.ymax = header_rect->ymax;
    panel_draw_softshadow(&box_rect, roundboxalign, radius, shadow_width);
  }

  /* Panel backdrop. */
  if (is_open || !has_header) {
    float panel_backcolor[4];
    draw_roundbox_corner_set(is_open ? CNR_BOTTOM_RIGHT | CNR_BOTTOM_LEFT : CNR_ALL);
    if (!has_header) {
      theme::get_color_4fv(TH_BACK, panel_backcolor);
    }
    else {
      theme::get_color_4fv((is_subpanel ? TH_PANEL_SUB_BACK : TH_PANEL_BACK), panel_backcolor);
    }

    rctf box_rect;
    const float padding = is_subpanel ? U.widget_unit * 0.1f / aspect : 0.0f;
    box_rect.xmin = rect->xmin + padding;
    box_rect.xmax = rect->xmax - padding;
    box_rect.ymin = rect->ymin + padding;
    box_rect.ymax = rect->ymax;
    draw_roundbox_4fv(&box_rect, true, radius, panel_backcolor);

    float subpanel_backcolor[4];
    theme::get_color_4fv(TH_PANEL_SUB_BACK, subpanel_backcolor);
    draw_layout_panels_backdrop(region, panel, radius, subpanel_backcolor);
  }

  /* Panel header backdrops for non sub-panels. */
  if (!is_subpanel && has_header) {
    float panel_headercolor[4];
    theme::get_color_4fv(panel_matches_search_filter(panel) ? TH_MATCH : TH_PANEL_HEADER,
                         panel_headercolor);
    draw_roundbox_corner_set(is_open ? CNR_TOP_RIGHT | CNR_TOP_LEFT : CNR_ALL);

    /* Change the width a little bit to line up with the sides. */
    rctf box_rect;
    box_rect.xmin = rect->xmin;
    box_rect.xmax = rect->xmax;
    box_rect.ymin = header_rect->ymin;
    box_rect.ymax = header_rect->ymax;
    draw_roundbox_4fv(&box_rect, true, radius, panel_headercolor);
  }

  GPU_blend(GPU_BLEND_NONE);
  immUnbindProgram();
}

void draw_aligned_panel(const ARegion *region,
                        const uiStyle *style,
                        const Block *block,
                        const rcti *rect,
                        const bool show_pin,
                        const bool show_background,
                        const bool region_search_filter_active)
{
  const Panel *panel = block->panel;

  if (panel->sizex < 0 || panel->sizey < 0) {
    /* Nothing to draw. */
    return;
  }

  /* Add 0.001f to prevent flicker from float inaccuracy. */
  const rcti header_rect = {
      rect->xmin,
      rect->xmax,
      rect->ymax,
      rect->ymax + int(floor(PNL_HEADER / block->aspect + 0.001f)),
  };

  if (show_background || (panel->type->flag & PANEL_TYPE_NO_HEADER)) {
    panel_draw_aligned_backdrop(region, panel, rect, &header_rect);
  }

  /* Draw the widgets and text in the panel header. */
  if (!(panel->type->flag & PANEL_TYPE_NO_HEADER)) {
    panel_draw_aligned_widgets(style,
                               panel,
                               &header_rect,
                               block->aspect,
                               show_pin,
                               show_background,
                               region_search_filter_active);
  }

  /* Draw the panel outline on non-transparent panels. */
  if (panel_should_show_background(region, panel->type)) {
    panel_draw_border(panel, rect, &header_rect, panel_custom_data_active_get(panel));
  }
}

bool panel_should_show_background(const ARegion *region, const PanelType *panel_type)
{
  if (region->alignment == RGN_ALIGN_FLOAT) {
    return false;
  }

  if (panel_type && panel_type->flag & PANEL_TYPE_NO_HEADER) {
    if (region->regiontype == RGN_TYPE_TOOLS) {
      /* We never want a background around active tools. */
      return false;
    }
    /* Without a header there is no background except for region overlap. */
    return region->overlap != 0;
  }

  return true;
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name Category Drawing (Tabs)
 * \{ */

#define TABS_PADDING_BETWEEN_FACTOR 4.0f
#define TABS_PADDING_TEXT_FACTOR 6.0f

static constexpr const char *panel_category_tabs_block_name = "panel_category_tabs";

static void panel_region_width_set(ARegion *region, const float aspect, int unscaled_size);

static void expand_panel_region(bContext &C, ARegion *region)
{
  const float aspect = BLI_rctf_size_y(&region->v2d.cur) /
                       (BLI_rcti_size_y(&region->v2d.mask) + 1);
  const bool too_narrow = BLI_rcti_size_x(&region->winrct) <=
                          int((UI_PANEL_CATEGORY_MIN_WIDTH + PANEL_MIN_DRAW_WIDTH) * UI_SCALE_FAC /
                              aspect);
  if (!too_narrow) {
    return;
  }
  /* Enlarge region. */
  int new_width = region->runtime->type->prefsizex ? region->runtime->type->prefsizex : 250;

  if (new_width < int(UI_PANEL_CATEGORY_MIN_WIDTH + PANEL_MIN_DRAW_WIDTH)) {
    region->runtime->type->prefsizex = UI_SIDEBAR_PANEL_WIDTH;
    new_width = UI_SIDEBAR_PANEL_WIDTH;
  }

  panel_region_width_set(region, aspect, new_width);
  WM_event_add_notifier(&C, NC_SCREEN | NA_EDITED, nullptr);
  ED_region_tag_redraw(region);
  /* Reset scroll to the top (#38348). */
  view2d_offset(&region->v2d, -1.0f, 1.0f);
}

static void expand_panel_region_on_category_change(bContext &C,
                                                   StringRef category,
                                                   StringRef active)
{
  ARegion *region = CTX_wm_region(&C);
  if (category == active) {
    return;
  }
  expand_panel_region(C, region);
}

void panel_category_tabs_draw_all(const bContext *C,
                                  ARegion *region,
                                  const char *category_id_active)
{
  const bool is_left = RGN_ALIGN_ENUM_FROM_MASK(region->alignment) != RGN_ALIGN_RIGHT;
  View2D *v2d = &region->v2d;
  const uiStyle *style = style_get();
  const uiFontStyle *fstyle = &style->widget;
  fontstyle_set(fstyle);
  const int fontid = fstyle->uifont_id;
  const float aspect = BLI_rctf_size_y(&region->v2d.cur) /
                       (BLI_rcti_size_y(&region->v2d.mask) + 1);
  const float zoom = 1.0f / aspect;
  const int px = U.pixelsize;
  const int category_tabs_width = std::round(UI_PANEL_CATEGORY_MARGIN_WIDTH * zoom);
  const float dpi_fac = UI_SCALE_FAC;
  /* Padding of tabs around text. */
  const int tab_v_pad_text = round_fl_to_int(TABS_PADDING_TEXT_FACTOR * dpi_fac * zoom) + 2 * px;
  /* Padding between tabs. */
  const int tab_v_pad = round_fl_to_int(TABS_PADDING_BETWEEN_FACTOR * dpi_fac * zoom);

  /* Primary theme colors. */
  uchar theme_col_back[4];
  /* Draw the background. */
  uchar theme_col_tab_bg[4];
  theme::get_color_4ubv(TH_TAB_BACK, theme_col_tab_bg);
  theme::get_color_4ubv(TH_BACK, theme_col_back);
  const bool is_alpha = (region->overlap && (theme_col_back[3] != 255));

  /* Draw background. */
  if (!is_alpha || theme_col_tab_bg[3] != 0) {
    uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", gpu::VertAttrType::SFLOAT_32_32);
    immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);

    if (is_alpha) {
      GPU_blend(GPU_BLEND_ALPHA);
      immUniformColor4ubv(theme_col_tab_bg);
    }
    else {
      immUniformColor3ubv(theme_col_tab_bg);
    }

    if (is_left) {
      immRectf(pos,
               v2d->mask.xmin,
               v2d->mask.ymin,
               v2d->mask.xmin + category_tabs_width,
               v2d->mask.ymax);
    }
    else {
      immRectf(pos,
               v2d->mask.xmax - category_tabs_width,
               v2d->mask.ymin,
               v2d->mask.xmax + 1,
               v2d->mask.ymax);
    }

    if (is_alpha) {
      GPU_blend(GPU_BLEND_NONE);
    }

    immUnbindProgram();
  }
  /* If the area is too small to show panels, then don't show any tabs as active. */
  const bool too_narrow = BLI_rcti_size_x(&region->winrct) <
                          int((UI_PANEL_CATEGORY_MIN_WIDTH + PANEL_MIN_DRAW_WIDTH) * UI_SCALE_FAC /
                              aspect);
  /* #widget_roundbox_set has this correction, keep in sync. */
  const int align_pad = (!region->overlap && !is_left) ? px : 0;
  /* Same for all tabs. */
  const int rct_xmin = is_left ? (v2d->mask.xmin + 3) :
                                 (v2d->mask.xmax - category_tabs_width + align_pad);
  const int rct_xmax = is_left ? (v2d->mask.xmin + category_tabs_width) : (v2d->mask.xmax - 3);
  /* NOTE: This block is created in window coordinates. */
  Block *block = block_begin(C, region, panel_category_tabs_block_name, EmbossType::Emboss);

  const int w = (rct_xmax - rct_xmin);

  Layout &layout = block_layout(block,
                                LayoutDirection::Vertical,
                                LayoutType::VerticalBar,
                                rct_xmin,
                                v2d->mask.ymax - 1,
                                w,
                                0,
                                0,
                                ui::style_get_dpi());
  Layout &col = layout.column(true);
  col.alignment_set(LayoutAlign::Center);

  uiDefBut(block, ButtonType::Sepr, "", 0, 0, w, tab_v_pad, nullptr, 0.0, 0.0, "");

  const bool compact = U.uiflag2 & USER_UIFLAG2_PANEL_TABS_COMPACT;

  /* Check the region type supports categories to avoid an assert
   * for showing 3D view panels in the properties space. */
  if (BKE_regiontype_uses_category_tabs(region->runtime->type)) {
    BLI_assert(panel_category_is_visible(region));
  }
  float fstyle_points = fstyle->points;
  fontscale(&fstyle_points, aspect);
  BLF_size(fontid, fstyle_points * UI_SCALE_FAC);

  PointerRNA ptr = RNA_pointer_create_discrete(
      reinterpret_cast<ID *>(CTX_wm_screen(C)), RNA_Region, region);
  PropertyRNA *prop = RNA_struct_find_property(&ptr, "active_panel_category");
  for (auto [i, pc_dyn] : region->runtime->panels_category.enumerate()) {
    Button *button = nullptr;
    const char *category_id = pc_dyn.idname;
    const char *category_id_draw = IFACE_(category_id);
    const int category_width = round_fl_to_int(
        compact ? 0 : BLF_width(fontid, category_id_draw, BLF_DRAW_STR_DUMMY_MAX));

    const int h = compact ? w + align_pad : category_width + tab_v_pad_text * 2;

    if (compact && pc_dyn.icon != ICON_NONE) {
      button = uiDefIconButR_prop(
          block, ButtonType::Tab, pc_dyn.icon, 0, 0, w, h, &ptr, prop, -1, 0, i, nullptr);
    }
    else {
      std::string title = category_id_draw;
      if (compact) {
        size_t category_draw_len = BLF_DRAW_STR_DUMMY_MAX;

        int char_offset1 = BLI_str_utf8_offset_from_index(category_id_draw, category_draw_len, 1);
        if (char_offset1 > 2) {
          /* Only a single complex character, symbol, or emoji. */
          title = std::string(category_id_draw, char_offset1);
        }
        else {
          int char_offset2 = BLI_str_utf8_offset_from_index(
              category_id_draw, category_draw_len, 2);
          char *space = BLI_strcasestr(category_id_draw, " ");
          if (char_offset2 == 2 && isupper(category_id_draw[1])) {
            /* First two characters are Latin with second uppercase. */
            title = std::string(category_id_draw, char_offset2);
          }
          else if (space && category_draw_len > (space - category_id_draw)) {
            /* First characters from each of the first two words. */
            title = std::string(category_id_draw, char_offset1) + std::string(space + 1, 1);
          }
          else {
            /* First two characters of a single word. */
            title = std::string(category_id_draw, char_offset2);
          }
        }
      }
      button = uiDefIconTextButR_prop(
          block, ButtonType::Tab, ICON_NONE, title, 0, 0, w, h, &ptr, prop, -1, 0, i, nullptr);
      button->text_direction = compact ? TextDirection::Default :
                                         (is_left ? TextDirection::Up : TextDirection::Down);
    }
    button->flag |= ui::BUT_DRAG_LOCK_X;
    if (compact) {
      button_func_quick_tooltip_set(
          button, [category = std::string(category_id)](const blender::ui::Button * /*but*/) {
            return TIP_(category);
          });
      /* Avoid default enum button tooltip for `RNA_Region::active_panel_category`. */
      button_func_tooltip_custom_set_cpp(
          *button, [category = std::string(category_id)](bContext & /*C*/, ui::TooltipData &data) {
            tooltip_text_field_add(
                data, TIP_(category), {}, TIP_STYLE_HEADER, TIP_LC_NORMAL, false);
          });
    }
    else {
      /* Avoid default enum button tooltip for `RNA_Region::active_panel_category`. */
      button_drawflag_enable(button, BUT_NO_TOOLTIP);
    }
    button_func_set(button,
                    [category = std::string(category_id),
                     active = std::string(category_id_active)](bContext &C) -> void {
                      expand_panel_region_on_category_change(C, category, active);
                    });
    button_func_pushed_state_set(
        button,
        [category = std::string(category_id),
         active = std::string(category_id_active),
         too_narrow](const Button &) -> bool { return category == active && !too_narrow; });
    if (pc_dyn.next) {
      uiDefBut(block, ButtonType::Sepr, "", 0, 0, w, tab_v_pad, nullptr, 0.0, 0.0, "");
    }
  }
  uiDefBut(block, ButtonType::Sepr, "", 0, 0, w, tab_v_pad, nullptr, 0.0, 0.0, "");

  const int2 co = block_layout_resolve(block);
  const int max_scroll = std::max(-co.y, 0);
  const int scroll = std::clamp(region->category_scroll, 0, max_scroll);
  region->category_scroll = scroll;
  block_end(C, block);
  block->aspect = aspect;

  block_translate(block, 0, region->category_scroll);

  /* Align buttons the panel content when region overlap is disabled. */
  if (!is_alpha) {
    for (Button &button : block->buttons()) {
      button.drawflag |= is_left ? BUT_ALIGN_RIGHT : BUT_ALIGN_LEFT;
    }
  }

  block_draw(C, block);

  /* Avoid buttons being aligned to the region on redraws. */
  for (Button &button : block->buttons()) {
    button.drawflag &= ~BUT_ALIGN_ALL;
  }
}

#undef TABS_PADDING_BETWEEN_FACTOR
#undef TABS_PADDING_TEXT_FACTOR

/** \} */

static int panel_category_show_active_tab(const bContext &C, ARegion *region, const int mval[2])
{
  if (!ED_region_panel_category_gutter_isect_xy(region, mval)) {
    return WM_UI_HANDLER_CONTINUE;
  }

  BLI_assert(BKE_regiontype_uses_category_tabs(region->runtime->type));

  const View2D *v2d = &region->v2d;
  const Block *block = region->runtime->block_name_map.lookup_as(panel_category_tabs_block_name);
  if (!block) {
    return WM_UI_HANDLER_BREAK;
  }
  PointerRNA ptr = RNA_pointer_create_discrete(
      reinterpret_cast<ID *>(CTX_wm_screen(&C)), RNA_Region, region);
  PropertyRNA *prop = RNA_struct_find_property(&ptr, "active_panel_category");
  for (auto [i, pc_dyn] : region->runtime->panels_category.enumerate()) {
    const bool is_active = STREQ(pc_dyn.idname, region->runtime->category);
    if (!is_active) {
      continue;
    }
    for (Button &button : block->buttons()) {
      if (button.rnapoin.data == region && button.rnaprop == prop && button.hardmax == i) {
        region->category_scroll = -(button.rect.ymax - region->category_scroll - v2d->mask.ymax);
        break;
      }
    }
    break;
  }
  ED_region_tag_redraw(region);
  return WM_UI_HANDLER_BREAK;
}
/* -------------------------------------------------------------------- */
/** \name Panel Alignment
 * \{ */

static int get_panel_size_y(const Panel *panel)
{
  if (panel->type && (panel->type->flag & PANEL_TYPE_NO_HEADER)) {
    return panel->sizey;
  }

  return PNL_HEADER + panel->sizey;
}

static int get_panel_real_size_y(const Panel *panel)
{
  const int sizey = panel_is_closed(panel) ? 0 : panel->sizey;

  if (panel->type && (panel->type->flag & PANEL_TYPE_NO_HEADER)) {
    return sizey;
  }

  return PNL_HEADER + sizey;
}

int panel_size_y(const Panel *panel)
{
  return get_panel_real_size_y(panel);
}

/**
 * This function is needed because #Block and Panel itself don't
 * change #Panel.sizey or location when closed.
 */
static int get_panel_real_ofsy(Panel *panel)
{
  if (panel_is_closed(panel)) {
    return panel->ofsy + panel->sizey;
  }
  return panel->ofsy;
}

bool panel_is_dragging(const Panel *panel)
{
  return panel->runtime_flag & PANEL_IS_DRAG_DROP;
}

/**
 * \note about sorting:
 * The #Panel.sortorder has a lower value for new panels being added.
 * however, that only works to insert a single panel, when more new panels get
 * added the coordinates of existing panels and the previously stored to-be-inserted
 * panels do not match for sorting.
 */

static bool find_highest_panel(const PanelSort &a, const PanelSort &b)
{
  /* Stick uppermost header-less panels to the top of the region -
   * prevent them from being sorted (multiple header-less panels have to be sorted though). */
  if (a.panel->type->flag & PANEL_TYPE_NO_HEADER && b.panel->type->flag & PANEL_TYPE_NO_HEADER) {
    /* Pass the no-header checks and check for `ofsy` and #Panel.sortorder below. */
  }
  else if (a.panel->type->flag & PANEL_TYPE_NO_HEADER) {
    return true;
  }
  else if (b.panel->type->flag & PANEL_TYPE_NO_HEADER) {
    return false;
  }

  const bool pin_last_a = panel_custom_pin_to_last_get(a.panel);
  const bool pin_last_b = panel_custom_pin_to_last_get(b.panel);
  if (pin_last_a && !pin_last_b) {
    return false;
  }
  if (!pin_last_a && pin_last_b) {
    return true;
  }

  if (a.panel->ofsy + a.panel->sizey < b.panel->ofsy + b.panel->sizey) {
    return false;
  }
  if (a.panel->ofsy + a.panel->sizey > b.panel->ofsy + b.panel->sizey) {
    return true;
  }
  return a.panel->sortorder < b.panel->sortorder;
}

static bool compare_panel(const PanelSort &a, const PanelSort &b)
{
  return a.panel->sortorder < b.panel->sortorder;
}

static void align_sub_panels(Panel *panel)
{
  /* Position sub panels. */
  int ofsy = panel->ofsy + panel->sizey - panel->blocksizey;

  for (Panel &pachild : panel->children) {
    if (pachild.runtime_flag & PANEL_ACTIVE) {
      pachild.ofsx = panel->ofsx;
      pachild.ofsy = ofsy - get_panel_size_y(&pachild);
      ofsy -= get_panel_real_size_y(&pachild);

      if (pachild.children.first) {
        align_sub_panels(&pachild);
      }
    }
  }
}

/**
 * Calculate the position and order of panels as they are opened, closed, and dragged.
 */
static bool uiAlignPanelStep(ARegion *region, const float factor, const bool drag)
{
  Vector<PanelSort> panel_sort;
  for (Panel &panel : region->panels) {
    if (panel.runtime_flag & PANEL_ACTIVE) {
      /* These panels should have types since they are currently displayed to the user. */
      BLI_assert(panel.type != nullptr);
      panel_sort.append({&panel, 0, 0});
    }
  }
  if (panel_sort.is_empty()) {
    return false;
  }

  if (drag) {
    /* While dragging, sort based on location and update #Panel.sortorder. */
    std::stable_sort(panel_sort.begin(), panel_sort.end(), find_highest_panel);
    for (int i : panel_sort.index_range()) {
      panel_sort[i].panel->sortorder = i;
    }
  }
  else {
    /* Otherwise use #Panel.sortorder. */
    std::stable_sort(panel_sort.begin(), panel_sort.end(), compare_panel);
  }
  /* X offset. */
  const int region_offset_x = panel_region_offset_x_get(region);
  for (PanelSort &ps : panel_sort) {
    const bool show_background = panel_should_show_background(region, ps.panel->type);
    ps.panel->runtime->region_ofsx = region_offset_x;
    ps.new_offset_x = region_offset_x + (show_background ? UI_PANEL_MARGIN_X : 0);
  }

  /* Y offset. */
  int y = 0;
  for (PanelSort &ps : panel_sort) {
    const bool show_background = panel_should_show_background(region, ps.panel->type);

    y -= get_panel_real_size_y(ps.panel);

    /* Separate panel boxes a bit further (if they are drawn). */
    if (show_background) {
      y -= UI_PANEL_MARGIN_Y;
    }
    ps.new_offset_y = y;
    /* The header still draws offset by the size of closed panels, so apply the offset here. */
    if (panel_is_closed(ps.panel)) {
      ps.new_offset_y -= ps.panel->sizey;
    }
  }

  /* Interpolate based on the input factor. */
  bool changed = false;
  for (PanelSort &ps : panel_sort) {
    if (ps.panel->flag & PNL_SELECT) {
      continue;
    }

    if (ps.new_offset_x != ps.panel->ofsx) {
      const float x = interpf(float(ps.new_offset_x), float(ps.panel->ofsx), factor);
      ps.panel->ofsx = round_fl_to_int(x);
      changed = true;
    }
    if (ps.new_offset_y != ps.panel->ofsy) {
      const float y = interpf(float(ps.new_offset_y), float(ps.panel->ofsy), factor);
      ps.panel->ofsy = round_fl_to_int(y);
      changed = true;
    }
  }

  /* Set locations for tabbed and sub panels. */
  for (Panel &panel : region->panels) {
    if (panel.runtime_flag & PANEL_ACTIVE) {
      if (panel.children.first) {
        align_sub_panels(&panel);
      }
    }
  }

  return changed;
}

static void panels_size(ARegion *region, int *r_x, int *r_y)
{
  int sizex = 0;
  int sizey = 0;
  bool has_panel_with_background = false;

  /* Compute size taken up by panels, for setting in view2d. */
  for (Panel &panel : region->panels) {
    if (panel.runtime_flag & PANEL_ACTIVE) {
      const int pa_sizex = panel.ofsx + panel.sizex;
      const int pa_sizey = get_panel_real_ofsy(&panel);

      sizex = max_ii(sizex, pa_sizex);
      sizey = min_ii(sizey, pa_sizey);
      if (panel_should_show_background(region, panel.type)) {
        has_panel_with_background = true;
      }
    }
  }

  if (sizex == 0) {
    sizex = UI_PANEL_WIDTH;
  }
  if (sizey == 0) {
    sizey = -UI_PANEL_WIDTH;
  }
  /* Extra margin after the list so the view scrolls a few pixels further than the panel border.
   * Also makes the bottom match the top margin. */
  if (has_panel_with_background) {
    sizey -= UI_PANEL_MARGIN_Y;
  }

  *r_x = sizex;
  *r_y = sizey;
}

static void do_animate(bContext *C, Panel *panel)
{
  HandlePanelData *data = static_cast<HandlePanelData *>(panel->activedata);
  ARegion *region = CTX_wm_region(C);

  float fac = 1.0f;
  if (!(U.uiflag & USER_REDUCE_MOTION)) {
    fac = (BLI_time_now_seconds() - data->starttime) / ANIMATION_TIME;
    fac = min_ff(sqrtf(fac), 1.0f);
  }

  if (uiAlignPanelStep(region, fac, false)) {
    ED_region_tag_redraw(region);
  }
  else {
    if (panel_is_dragging(panel)) {
      /* NOTE: doing this in #panel_activate_state would require
       * removing `const` for context in many other places. */
      reorder_instanced_panel_list(C, region, panel);
    }

    panel_activate_state(C, panel, PANEL_STATE_EXIT);
  }
}

static void panels_layout_begin_clear_flags(ListBaseT<Panel> *lb)
{
  for (Panel &panel : *lb) {
    /* Flags to copy over to the next layout pass. */
    const short flag_copy = PANEL_USE_CLOSED_FROM_SEARCH | PANEL_IS_DRAG_DROP;

    const bool was_active = panel.runtime_flag & PANEL_ACTIVE;
    const bool was_closed = panel_is_closed(&panel);
    panel.runtime_flag &= flag_copy;
    SET_FLAG_FROM_TEST(panel.runtime_flag, was_active, PANEL_WAS_ACTIVE);
    SET_FLAG_FROM_TEST(panel.runtime_flag, was_closed, PANEL_WAS_CLOSED);

    panels_layout_begin_clear_flags(&panel.children);
  }
}

void panels_begin(const bContext * /*C*/, ARegion *region)
{
  /* Set all panels as inactive, so that at the end we know which ones were used. Also
   * clear other flags so we know later that their values were set for the current redraw. */
  panels_layout_begin_clear_flags(&region->panels);
}

void panels_end(const bContext *C, ARegion *region, int *r_x, int *r_y)
{
  ScrArea *area = CTX_wm_area(C);

  region_panels_set_expansion_from_list_data(C, region);

  const bool region_search_filter_active = region->flag & RGN_FLAG_SEARCH_FILTER_ACTIVE;

  if (properties_space_needs_realign(area, region)) {
    region_panels_set_expansion_from_search_filter(C, region, region_search_filter_active);
  }
  else if (region->flag & RGN_FLAG_SEARCH_FILTER_UPDATE) {
    region_panels_set_expansion_from_search_filter(C, region, region_search_filter_active);
  }

  if (region->flag & RGN_FLAG_SEARCH_FILTER_ACTIVE) {
    /* Clean up the extra panels and buttons created for searching. */
    region_panels_remove_invisible_layouts(region);
  }

  for (Panel &panel : region->panels) {
    if (panel.runtime_flag & PANEL_ACTIVE) {
      BLI_assert(panel.runtime->block != nullptr);
      panel_calculate_size_recursive(region, &panel);
    }
  }

  /* Offset contents. */
  for (Block &block : region->runtime->uiblocks) {
    if (block.active && block.panel) {
      offset_panel_block(&block);

      /* Update bounds for all "views" in this block. Usually this is done in #block_end(), but
       * that wouldn't work because of the offset applied above. */
      block_views_end(region, &block);
    }
  }

  /* Re-align, possibly with animation. */
  Panel *panel;
  if (panels_need_realign(area, region, &panel)) {
    if (panel) {
      panel_activate_state(C, panel, PANEL_STATE_ANIMATION);
    }
    else {
      uiAlignPanelStep(region, 1.0, false);
    }
  }

  /* Compute size taken up by panels. */
  panels_size(region, r_x, r_y);
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name Panel Dragging
 * \{ */

#define DRAG_REGION_PAD (PNL_HEADER * 0.5)
static void do_drag(const bContext *C, const wmEvent *event, Panel *panel)
{
  HandlePanelData *data = static_cast<HandlePanelData *>(panel->activedata);
  ARegion *region = CTX_wm_region(C);

  /* Keep the drag position in the region with a small pad to keep the panel visible. */
  const int y = clamp_i(event->xy[1], region->winrct.ymin, region->winrct.ymax + DRAG_REGION_PAD);

  float dy = float(y - data->starty);

  /* Adjust for region zoom. */
  dy *= BLI_rctf_size_y(&region->v2d.cur) / float(BLI_rcti_size_y(&region->winrct));

  /* Add the movement of the view due to edge scrolling while dragging. */
  dy += (region->v2d.cur.ymin - data->start_cur_ymin);

  panel->ofsy = data->startofsy + round_fl_to_int(dy);

  uiAlignPanelStep(region, 0.2f, true);

  ED_region_tag_redraw(region);
}
#undef DRAG_REGION_PAD

/** \} */

/* -------------------------------------------------------------------- */
/** \name Region Level Panel Interaction
 * \{ */

LayoutPanelHeader *layout_panel_header_under_mouse(const Panel &panel, const int my)
{
  for (LayoutPanelHeader &header : panel.runtime->layout_panels.headers) {
    if (IN_RANGE(float(my - panel.runtime->block->rect.ymax), header.start_y, header.end_y)) {
      return &header;
    }
  }
  return nullptr;
}

static PanelMouseState panel_mouse_state_get(const Block *block,
                                             const Panel *panel,
                                             const int mx,
                                             const int my)
{
  if (!IN_RANGE(float(mx), block->rect.xmin, block->rect.xmax)) {
    return PANEL_MOUSE_OUTSIDE;
  }

  if (IN_RANGE(float(my), block->rect.ymax, block->rect.ymax + PNL_HEADER)) {
    return PANEL_MOUSE_INSIDE_HEADER;
  }
  if (layout_panel_header_under_mouse(*panel, my) != nullptr) {
    return PANEL_MOUSE_INSIDE_LAYOUT_PANEL_HEADER;
  }

  if (!panel_is_closed(panel)) {
    if (IN_RANGE(float(my), block->rect.ymin, block->rect.ymax + PNL_HEADER)) {
      return PANEL_MOUSE_INSIDE_CONTENT;
    }
  }

  return PANEL_MOUSE_OUTSIDE;
}

struct PanelDragCollapseHandle {
  bool was_first_open;
  int xy_init[2];
};

static void panel_drag_collapse_handler_remove(bContext * /*C*/, void *userdata)
{
  PanelDragCollapseHandle *dragcol_data = static_cast<PanelDragCollapseHandle *>(userdata);
  MEM_delete(dragcol_data);
}

static void panel_drag_collapse(const bContext *C,
                                const PanelDragCollapseHandle *dragcol_data,
                                const int xy_dst[2])
{
  ARegion *region = CTX_wm_region_popup(C);
  if (!region) {
    region = CTX_wm_region(C);
  }
  for (Block &block : region->runtime->uiblocks) {
    float xy_a_block[2] = {float(dragcol_data->xy_init[0]), float(dragcol_data->xy_init[1])};
    float xy_b_block[2] = {float(xy_dst[0]), float(xy_dst[1])};
    Panel *panel = block.panel;

    if (panel == nullptr) {
      continue;
    }

    /* Lock axis. */
    xy_b_block[0] = dragcol_data->xy_init[0];

    /* Use cursor coords in block space. */
    window_to_block_fl(region, &block, &xy_a_block[0], &xy_a_block[1]);
    window_to_block_fl(region, &block, &xy_b_block[0], &xy_b_block[1]);

    for (LayoutPanelHeader &header : panel->runtime->layout_panels.headers) {
      rctf rect = block.rect;
      rect.ymin = block.rect.ymax + header.start_y;
      rect.ymax = block.rect.ymax + header.end_y;

      if (BLI_rctf_isect_segment(&rect, xy_a_block, xy_b_block)) {
        RNA_boolean_set(
            &header.open_owner_ptr, header.open_prop_name.c_str(), !dragcol_data->was_first_open);
        RNA_property_update(
            const_cast<bContext *>(C),
            &header.open_owner_ptr,
            RNA_struct_find_property(&header.open_owner_ptr, header.open_prop_name.c_str()));
        ED_region_tag_redraw(region);
        ED_region_tag_refresh_ui(region);
      }
    }

    if (panel->type && (panel->type->flag & PANEL_TYPE_NO_HEADER)) {
      continue;
    }
    const int oldflag = panel->flag;

    /* Set up `rect` to match header size. */
    rctf rect = block.rect;
    rect.ymin = rect.ymax;
    rect.ymax = rect.ymin + PNL_HEADER;

    /* Touch all panels between last mouse coordinate and the current one. */
    if (BLI_rctf_isect_segment(&rect, xy_a_block, xy_b_block)) {
      /* Force panel to open or close. */
      panel->runtime_flag &= ~PANEL_USE_CLOSED_FROM_SEARCH;
      SET_FLAG_FROM_TEST(panel->flag, dragcol_data->was_first_open, PNL_CLOSED);

      /* If panel->flag has changed this means a panel was opened/closed here. */
      if (panel->flag != oldflag) {
        panel_activate_state(C, panel, PANEL_STATE_ANIMATION);
      }
    }
  }
  /* Update the instanced panel data expand flags with the changes made here. */
  set_panels_list_data_expand_flag(C, region);
}

/**
 * Panel drag-collapse (modal handler).
 * Clicking and dragging over panels toggles their collapse state based on the panel
 * that was first dragged over. If it was open all affected panels including the initial
 * one are closed and vice versa.
 */
static int panel_drag_collapse_handler(bContext *C, const wmEvent *event, void *userdata)
{
  wmWindow *win = CTX_wm_window(C);
  PanelDragCollapseHandle *dragcol_data = static_cast<PanelDragCollapseHandle *>(userdata);
  short retval = WM_UI_HANDLER_CONTINUE;

  switch (event->type) {
    case MOUSEMOVE:
      panel_drag_collapse(C, dragcol_data, event->xy);

      retval = WM_UI_HANDLER_BREAK;
      break;
    case LEFTMOUSE:
      if (event->val == KM_RELEASE) {
        /* Done! */
        WM_event_remove_ui_handler(&win->runtime->modalhandlers,
                                   panel_drag_collapse_handler,
                                   panel_drag_collapse_handler_remove,
                                   dragcol_data,
                                   true);
        panel_drag_collapse_handler_remove(C, dragcol_data);
      }
      /* Don't let any left-mouse event fall through! */
      retval = WM_UI_HANDLER_BREAK;
      break;
    default: {
      break;
    }
  }

  return retval;
}

void panel_drag_collapse_handler_add(const bContext *C, const bool was_open)
{
  wmWindow *win = CTX_wm_window(C);
  const wmEvent *event = win->runtime->eventstate;
  PanelDragCollapseHandle *dragcol_data = MEM_new_zeroed<PanelDragCollapseHandle>(__func__);

  dragcol_data->was_first_open = was_open;
  copy_v2_v2_int(dragcol_data->xy_init, event->xy);

  WM_event_add_ui_handler(C,
                          &win->runtime->modalhandlers,
                          panel_drag_collapse_handler,
                          panel_drag_collapse_handler_remove,
                          dragcol_data,
                          eWM_EventHandlerFlag(0));
}

bool layout_panel_toggle_open(const bContext *C, LayoutPanelHeader *header)
{
  const bool is_open = RNA_boolean_get(&header->open_owner_ptr, header->open_prop_name.c_str());
  RNA_boolean_set(&header->open_owner_ptr, header->open_prop_name.c_str(), !is_open);
  RNA_property_update(
      const_cast<bContext *>(C),
      &header->open_owner_ptr,
      RNA_struct_find_property(&header->open_owner_ptr, header->open_prop_name.c_str()));
  return !is_open;
}

static void handle_layout_panel_header(
    bContext *C, const Block *block, const int /*mx*/, const int my, const int event_type)
{
  Panel *panel = block->panel;
  BLI_assert(panel->type != nullptr);

  LayoutPanelHeader *header = layout_panel_header_under_mouse(*panel, my);
  if (header == nullptr) {
    return;
  }
  const bool new_state = layout_panel_toggle_open(C, header);
  ED_region_tag_redraw(CTX_wm_region(C));
  WM_tooltip_clear(C, CTX_wm_window(C));

  if (event_type == LEFTMOUSE) {
    panel_drag_collapse_handler_add(C, !new_state);
  }
}

/**
 * Supposing the block has a panel and isn't a menu, handle opening, closing, pinning, etc.
 * Code currently assumes layout style for location of widgets
 *
 * \param mx: The mouse x coordinate, in panel space.
 */
static void handle_panel_header(const bContext *C,
                                const Block *block,
                                const int mx,
                                const int event_type,
                                const bool ctrl,
                                const bool shift)
{
  Panel *panel = block->panel;
  ARegion *region = CTX_wm_region(C);

  BLI_assert(panel->type != nullptr);
  BLI_assert(!(panel->type->flag & PANEL_TYPE_NO_HEADER));

  const bool is_subpanel = (panel->type->parent != nullptr);
  const bool use_pin = panel_category_tabs_is_visible(region) && panel_can_be_pinned(panel);
  const bool show_pin = use_pin && (panel->flag & PNL_PIN);
  const bool show_drag = !is_subpanel;

  /* Handle panel pinning. */
  if (use_pin && ELEM(event_type, EVT_RETKEY, EVT_PADENTER, LEFTMOUSE) && shift) {
    panel->flag ^= PNL_PIN;
    ED_region_tag_redraw(region);
    return;
  }

  float expansion_area_xmax = block->rect.xmax;
  if (show_drag) {
    expansion_area_xmax -= (PNL_ICON * 1.5f);
  }
  if (show_pin) {
    expansion_area_xmax -= PNL_ICON;
  }

  /* Collapse and expand panels. */
  if (ELEM(event_type, EVT_RETKEY, EVT_PADENTER, EVT_AKEY) || mx < expansion_area_xmax) {
    if (ctrl && !is_subpanel) {
      /* For parent panels, collapse all other panels or toggle children. */
      if (panel_is_closed(panel) || panel->children.is_empty()) {
        panels_collapse_all(region, panel);

        /* Reset the view - we don't want to display a view without content. */
        view2d_offset(&region->v2d, 0.0f, 1.0f);
      }
      else {
        /* If a panel has sub-panels and it's open, toggle the expansion
         * of the sub-panels (based on the expansion of the first sub-panel). */
        Panel *first_child = static_cast<Panel *>(panel->children.first);
        BLI_assert(first_child != nullptr);
        panel_set_flag_recursive(panel, PNL_CLOSED, !panel_is_closed(first_child));
        panel->flag |= PNL_CLOSED;
      }
    }

    SET_FLAG_FROM_TEST(panel->flag, !panel_is_closed(panel), PNL_CLOSED);

    if (event_type == LEFTMOUSE) {
      panel_drag_collapse_handler_add(C, panel_is_closed(panel));
    }

    /* Set panel custom data (modifier) active when expanding sub-panels, but not top-level
     * panels to allow collapsing and expanding without setting the active element. */
    if (is_subpanel) {
      panel_custom_data_active_set(panel);
    }

    set_panels_list_data_expand_flag(C, region);
    panel_activate_state(C, panel, PANEL_STATE_ANIMATION);
    return;
  }

  /* Handle panel dragging. For now don't allow dragging in floating regions. */
  if (show_drag && !(region->alignment == RGN_ALIGN_FLOAT)) {
    const float drag_area_xmin = block->rect.xmax - (PNL_ICON * 1.5f);
    const float drag_area_xmax = block->rect.xmax;
    if (IN_RANGE(mx, drag_area_xmin, drag_area_xmax)) {
      if (panel_custom_pin_to_last_get(panel)) {
        panel_custom_pin_to_last_set(C, panel, false);
        return;
      }
      panel_activate_state(C, panel, PANEL_STATE_DRAG);
      return;
    }
  }

  /* Handle panel unpinning. */
  if (show_pin) {
    const float pin_area_xmin = expansion_area_xmax;
    const float pin_area_xmax = pin_area_xmin + PNL_ICON;
    if (IN_RANGE(mx, pin_area_xmin, pin_area_xmax)) {
      panel->flag ^= PNL_PIN;
      ED_region_tag_redraw(region);
      return;
    }
  }
}

bool panel_category_is_visible(const ARegion *region)
{
  /* Check for more than one category. */
  return region->runtime->panels_category.first &&
         (!bool(region->runtime->type->flag & ARegionTypeFlag::HideSinglePanelCategories) ||
          region->runtime->panels_category.first != region->runtime->panels_category.last);
}

bool panel_category_tabs_is_visible(const ARegion *region)
{
  return panel_category_is_visible(region) &&
         BKE_regiontype_uses_category_tabs(region->runtime->type);
}

PanelCategoryDyn *panel_category_find(const ARegion *region, const char *idname)
{
  return static_cast<PanelCategoryDyn *>(BLI_findstring(
      &region->runtime->panels_category, idname, offsetof(PanelCategoryDyn, idname)));
}

int panel_category_index_find(ARegion *region, const char *idname)
{
  return BLI_findstringindex(
      &region->runtime->panels_category, idname, offsetof(PanelCategoryDyn, idname));
}

PanelCategoryStack *panel_category_active_find(ARegion *region, const char *idname)
{
  return static_cast<PanelCategoryStack *>(BLI_findstring(
      &region->panels_category_active, idname, offsetof(PanelCategoryStack, idname)));
}

static void panel_category_active_set(ARegion *region, const char *idname, bool fallback)
{
  ListBaseT<PanelCategoryStack> *lb = &region->panels_category_active;
  PanelCategoryStack *pc_act = panel_category_active_find(region, idname);

  if (pc_act) {
    BLI_remlink(lb, pc_act);
  }
  else {
    pc_act = MEM_new<PanelCategoryStack>(__func__);
    STRNCPY_UTF8(pc_act->idname, idname);
  }

  if (fallback) {
    /* For fall-backs, add at the end so explicitly chosen categories have priority. */
    BLI_addtail(lb, pc_act);
  }
  else {
    BLI_addhead(lb, pc_act);
  }

  /* Validate all active panels. We could do this on load, they are harmless -
   * but we should remove them somewhere.
   * (Add-ons could define panels and gather cruft over time). */
  {
    PanelCategoryStack *pc_act_next;
    /* intentionally skip first */
    pc_act_next = pc_act->next;
    while ((pc_act = pc_act_next)) {
      pc_act_next = pc_act->next;
      if (!BLI_findstring(
              &region->runtime->type->paneltypes, pc_act->idname, offsetof(PanelType, category)))
      {
        BLI_remlink(lb, pc_act);
        MEM_delete(pc_act);
      }
    }
  }
  ED_region_tag_redraw(region);
}

void panel_category_active_set(ARegion *region, const char *idname)
{
  panel_category_active_set(region, idname, false);
}

void panel_category_index_active_set(ARegion *region, const int index)
{
  PanelCategoryDyn *pc_dyn = static_cast<PanelCategoryDyn *>(
      BLI_findlink(&region->runtime->panels_category, index));
  if (!pc_dyn) {
    return;
  }

  panel_category_active_set(region, pc_dyn->idname, false);
}

void panel_category_active_set_default(ARegion *region, const char *idname)
{
  if (!panel_category_active_find(region, idname)) {
    panel_category_active_set(region, idname, true);
  }
}

const char *panel_category_active_get(ARegion *region, bool set_fallback)
{
  for (PanelCategoryStack &pc_act : region->panels_category_active) {
    if (panel_category_find(region, pc_act.idname)) {
      return pc_act.idname;
    }
  }

  if (set_fallback) {
    PanelCategoryDyn *pc_dyn = static_cast<PanelCategoryDyn *>(
        region->runtime->panels_category.first);
    if (pc_dyn) {
      panel_category_active_set(region, pc_dyn->idname, true);
      return pc_dyn->idname;
    }
  }

  return nullptr;
}

void panel_category_add(ARegion *region, const char *name, int icon)
{
  PanelCategoryDyn *pc_dyn = MEM_new<PanelCategoryDyn>(__func__);
  BLI_addtail(&region->runtime->panels_category, pc_dyn);

  STRNCPY_UTF8(pc_dyn->idname, name);
  pc_dyn->icon = icon;

  /* 'pc_dyn->rect' must be set on draw. */
}

void panel_category_clear_all(ARegion *region)
{
  region->runtime->panels_category.free_no_destruct();
}

static bool panel_categories_tab_is_mouse_over(ARegion *region, const wmEvent *event)
{
  BLI_assert(BKE_regiontype_uses_category_tabs(region->runtime->type));

  const float aspect = BLI_rctf_size_y(&region->v2d.cur) /
                       (BLI_rcti_size_y(&region->v2d.mask) + 1);
  const float zoom = 1.0f / aspect;
  const int category_tabs_width = round_fl_to_int(UI_PANEL_CATEGORY_MARGIN_WIDTH * zoom);
  const bool is_left = RGN_ALIGN_ENUM_FROM_MASK(region->alignment) != RGN_ALIGN_RIGHT;

  const View2D *v2d = &region->v2d;
  int ymin = region->overlap ? region->v2d.mask.ymax : region->v2d.mask.ymin;
  if (region->overlap) {
    if (const Block *block = region->runtime->block_name_map.lookup_as(
            panel_category_tabs_block_name);
        block && !block->buttons_ptrs.is_empty())
    {
      ymin = std::max(region->v2d.mask.ymin, int(block->buttons_ptrs.last()->rect.ymin));
    }
  }
  const rcti rect = {
      .xmin = is_left ? v2d->mask.xmin + 3 : (v2d->mask.xmax - category_tabs_width),
      .xmax = is_left ? v2d->mask.xmin + category_tabs_width : (v2d->mask.xmax - 3),
      .ymin = ymin,
      .ymax = region->v2d.mask.ymax,
  };

  return BLI_rcti_isect_pt(&rect, event->mval[0], event->mval[1]);
}

static int handle_panel_category_cycling(const wmEvent *event,
                                         ARegion *region,
                                         const Button *active_but)
{
  BLI_assert(BKE_regiontype_uses_category_tabs(region->runtime->type));

  const bool is_mousewheel = ELEM(event->type, WHEELUPMOUSE, WHEELDOWNMOUSE);
  const bool inside_tabregion = panel_categories_tab_is_mouse_over(region, event);

  /* If mouse is inside non-tab region, ctrl key is required. */
  if (is_mousewheel && (event->modifier & KM_CTRL) == 0 && !inside_tabregion) {
    return WM_UI_HANDLER_CONTINUE;
  }

  if (active_but && button_supports_cycling(active_but)) {
    /* Skip - exception to make cycling buttons using ctrl+mousewheel work in tabbed regions. */
  }
  else {
    const char *category = panel_category_active_get(region, false);
    if (LIKELY(category)) {
      PanelCategoryDyn *pc_dyn = panel_category_find(region, category);
      /* Cyclic behavior between categories
       * using Ctrl+Tab (+Shift for backwards) or Ctrl+Wheel Up/Down. */
      if (LIKELY(pc_dyn) && (event->modifier & KM_CTRL)) {
        if (is_mousewheel) {
          /* We can probably get rid of this and only allow Ctrl-Tabbing. */
          pc_dyn = (event->type == WHEELDOWNMOUSE) ? pc_dyn->next : pc_dyn->prev;
        }
        else {
          const bool backwards = event->modifier & KM_SHIFT;
          pc_dyn = backwards ? pc_dyn->prev : pc_dyn->next;
          if (!pc_dyn) {
            /* Proper cyclic behavior, back to first/last category (only used for ctrl+tab). */
            pc_dyn = backwards ?
                         static_cast<PanelCategoryDyn *>(region->runtime->panels_category.last) :
                         static_cast<PanelCategoryDyn *>(region->runtime->panels_category.first);
          }
        }

        if (pc_dyn) {
          /* Intentionally don't reset scroll in this case,
           * allowing for quick browsing between tabs. */
          panel_category_active_set(region, pc_dyn->idname);
        }
        return WM_UI_HANDLER_BREAK;
      }
    }
  }

  return WM_UI_HANDLER_CONTINUE;
}

static void panel_region_width_set(ARegion *region, const float aspect, int unscaled_size)
{
  const float size_new = unscaled_size / aspect;
  if (region->alignment & RGN_ALIGN_RIGHT) {
    region->winrct.xmin = region->winrct.xmax - (size_new * UI_SCALE_FAC);
  }
  else {
    region->winrct.xmax = region->winrct.xmin + (size_new * UI_SCALE_FAC);
  }
  region->winx = size_new * UI_SCALE_FAC;
  region->sizex = size_new;
  region->v2d.winx = region->winx;
  region->v2d.cur.xmin = 0;
  region->v2d.cur.xmax = size_new * UI_SCALE_FAC;
  region->v2d.mask.xmin = 0;
  region->v2d.mask.xmax = size_new * UI_SCALE_FAC;
  view2d_curRect_validate(&region->v2d);
}

int handler_panel_region(bContext *C,
                         const wmEvent *event,
                         ARegion *region,
                         const Button *active_but)
{
  if (event->val == KM_RELEASE) {
    return WM_UI_HANDLER_CONTINUE;
  }

  /* Scroll-bars can overlap panels now, they have handling priority. */
  if (view2d_mouse_in_scrollers(region, &region->v2d, event->xy)) {
    return WM_UI_HANDLER_CONTINUE;
  }

  int retval = WM_UI_HANDLER_CONTINUE;

  /* Handle category tabs. */
  if (panel_category_tabs_is_visible(region)) {
    if (event->type == LEFTMOUSE && event->val == KM_PRESS &&
        panel_categories_tab_is_mouse_over(region, event))
    {
      const Button *active_button = region_find_active_but(region);
      /* Expand/collapse panels when clicking the active category button. */
      PointerRNA ptr = RNA_pointer_create_discrete(
          reinterpret_cast<ID *>(CTX_wm_screen(C)), RNA_Region, region);
      const int val = RNA_enum_get(&ptr, "active_panel_category");
      if (active_button && active_button->hardmax == val) {
        const float aspect = BLI_rctf_size_y(&region->v2d.cur) /
                             (BLI_rcti_size_y(&region->v2d.mask) + 1);
        const bool too_narrow = BLI_rcti_size_x(&region->winrct) <
                                int(std::floor(
                                    (UI_PANEL_CATEGORY_MIN_WIDTH + PANEL_MIN_DRAW_WIDTH) *
                                    UI_SCALE_FAC / aspect));
        if (too_narrow) {
          expand_panel_region(*C, region);
        }
        else {
          /* Minimize region. */
          region->runtime->type->prefsizex = int(float(BLI_rcti_size_x(&region->winrct) + 1) /
                                                 UI_SCALE_FAC * aspect);
          panel_region_width_set(region, aspect, UI_PANEL_CATEGORY_MIN_WIDTH);
          WM_event_add_notifier(C, NC_SCREEN | NA_EDITED, nullptr);
          ED_region_tag_redraw(region);
          /* Reset scroll to the top (#38348). */
          view2d_offset(&region->v2d, -1.0f, 1.0f);
        }
        /* Do not break event, let click drag activate panel categories. */
      }
    }
    else if (((event->type == EVT_TABKEY) && (event->modifier & KM_CTRL)) ||
             ELEM(event->type, WHEELUPMOUSE, WHEELDOWNMOUSE))
    {
      /* Cycle tabs. */
      WM_tooltip_clear(C, CTX_wm_window(C));
      retval = handle_panel_category_cycling(event, region, active_but);
    }
    else if (event->type == EVT_PADPERIOD) {
      WM_tooltip_clear(C, CTX_wm_window(C));
      retval = panel_category_show_active_tab(*C, region, event->xy);
    }
    else if ((event->type == RIGHTMOUSE) && panel_categories_tab_is_mouse_over(region, event)) {
      BLI_assert(retval == WM_UI_HANDLER_CONTINUE);
      WM_tooltip_clear(C, CTX_wm_window(C));
      retval = popup_context_menu_for_panel(C, region, nullptr);
    }
  }

  if (retval == WM_UI_HANDLER_BREAK) {
    return retval;
  }

  const Button *region_active_but = region_find_active_but(region);
  const bool region_has_active_button = region_active_but &&
                                        region_active_but->type != ButtonType::Label;

  for (Block &block : region->runtime->uiblocks) {
    Panel *panel = block.panel;
    if (panel == nullptr || panel->type == nullptr) {
      continue;
    }
    /* We can't expand or collapse panels without headers, they would disappear. Layout panels can
     * be expanded and collapsed though. */
    const bool has_panel_header = !(panel->type->flag & PANEL_TYPE_NO_HEADER);

    int mx = event->xy[0];
    int my = event->xy[1];
    window_to_block(region, &block, &mx, &my);

    const PanelMouseState mouse_state = panel_mouse_state_get(&block, panel, mx, my);

    if (has_panel_header && mouse_state != PANEL_MOUSE_OUTSIDE) {
      /* Mark panels that have been interacted with so their expansion
       * doesn't reset when property search finishes. */
      SET_FLAG_FROM_TEST(panel->flag, panel_is_closed(panel), PNL_CLOSED);
      panel->runtime_flag &= ~PANEL_USE_CLOSED_FROM_SEARCH;

      /* The panel collapse / expand key "A" is special as it takes priority over
       * active button handling. */
      if ((event->type == EVT_AKEY) && (event->modifier == 0)) {
        retval = WM_UI_HANDLER_BREAK;
        handle_panel_header(
            C, &block, mx, event->type, event->modifier & KM_CTRL, event->modifier & KM_SHIFT);
        break;
      }
    }

    /* Don't do any other panel handling with an active button. */
    if (region_has_active_button) {
      continue;
    }

    if ((has_panel_header && mouse_state == PANEL_MOUSE_INSIDE_HEADER)) {
      /* All mouse clicks inside panel headers should return in break. */
      if (ELEM(event->type, EVT_RETKEY, EVT_PADENTER, LEFTMOUSE)) {
        retval = WM_UI_HANDLER_BREAK;
        handle_panel_header(
            C, &block, mx, event->type, event->modifier & KM_CTRL, event->modifier & KM_SHIFT);
      }
      else if (event->type == RIGHTMOUSE) {
        retval = popup_context_menu_for_panel(C, region, block.panel);
      }
      break;
    }
    if (mouse_state == PANEL_MOUSE_INSIDE_LAYOUT_PANEL_HEADER) {
      if (ELEM(event->type, EVT_RETKEY, EVT_PADENTER, LEFTMOUSE)) {
        retval = WM_UI_HANDLER_BREAK;
        handle_layout_panel_header(C, &block, mx, my, event->type);
      }
    }
  }

  return retval;
}

static void panel_custom_data_set_recursive(Panel *panel, PointerRNA *custom_data)
{
  panel->runtime->custom_data_ptr = custom_data;

  for (Panel &child_panel : panel->children) {
    panel_custom_data_set_recursive(&child_panel, custom_data);
  }
}

void panel_context_pointer_set(Panel *panel, const char *name, PointerRNA *ptr)
{
  panel->layout->context_ptr_set(name, ptr);
  panel->runtime->context = panel->layout->context_store();
}

void panel_custom_data_set(Panel *panel, PointerRNA *custom_data)
{
  BLI_assert(panel->type != nullptr);

  /* Free the old custom data, which should be shared among all of the panel's sub-panels. */
  if (panel->runtime->custom_data_ptr != nullptr) {
    MEM_delete(panel->runtime->custom_data_ptr);
  }

  panel_custom_data_set_recursive(panel, custom_data);
}

PointerRNA *panel_custom_data_get(const Panel *panel)
{
  return panel->runtime->custom_data_ptr;
}

PointerRNA *region_panel_custom_data_under_cursor(const bContext *C, const wmEvent *event)
{
  ARegion *region = CTX_wm_region(C);
  if (region) {
    for (Block &block : region->runtime->uiblocks) {
      Panel *panel = block.panel;
      if (panel == nullptr) {
        continue;
      }

      int mx = event->xy[0];
      int my = event->xy[1];
      window_to_block(region, &block, &mx, &my);
      const int mouse_state = panel_mouse_state_get(&block, panel, mx, my);
      if (ELEM(mouse_state, PANEL_MOUSE_INSIDE_CONTENT, PANEL_MOUSE_INSIDE_HEADER)) {
        return panel_custom_data_get(panel);
      }
    }
  }

  return nullptr;
}

bool panel_can_be_pinned(const Panel *panel)
{
  return (panel->type->parent == nullptr) && !(panel->type->flag & PANEL_TYPE_INSTANCED);
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name Window Level Modal Panel Interaction
 * \{ */

/* NOTE: this is modal handler and should not swallow events for animation. */
static int handler_panel(bContext *C, const wmEvent *event, void *userdata)
{
  Panel *panel = static_cast<Panel *>(userdata);
  HandlePanelData *data = static_cast<HandlePanelData *>(panel->activedata);

  /* Verify if we can stop. */
  if (event->type == LEFTMOUSE && event->val == KM_RELEASE) {
    panel_activate_state(C, panel, PANEL_STATE_ANIMATION);
  }
  else if (event->type == MOUSEMOVE) {
    if (data->state == PANEL_STATE_DRAG) {
      do_drag(C, event, panel);
    }
  }
  else if (event->type == TIMER && event->customdata == data->animtimer) {
    if (data->state == PANEL_STATE_ANIMATION) {
      do_animate(C, panel);
    }
    else if (data->state == PANEL_STATE_DRAG) {
      do_drag(C, event, panel);
    }
  }

  data = static_cast<HandlePanelData *>(panel->activedata);

  if (data && data->state == PANEL_STATE_ANIMATION) {
    return WM_UI_HANDLER_CONTINUE;
  }
  return WM_UI_HANDLER_BREAK;
}

static void handler_remove_panel(bContext *C, void *userdata)
{
  Panel *panel = static_cast<Panel *>(userdata);

  panel_activate_state(C, panel, PANEL_STATE_EXIT);
}

static void panel_handle_data_ensure(const bContext *C,
                                     wmWindow *win,
                                     const ARegion *region,
                                     Panel *panel,
                                     const HandlePanelState state)
{
  BLI_assert(ELEM(state, PANEL_STATE_DRAG, PANEL_STATE_ANIMATION));

  if (panel->activedata == nullptr) {
    panel->activedata = MEM_new_zeroed<HandlePanelData>(__func__);
    WM_event_add_ui_handler(C,
                            &win->runtime->modalhandlers,
                            handler_panel,
                            handler_remove_panel,
                            panel,
                            eWM_EventHandlerFlag(0));
  }

  HandlePanelData *data = static_cast<HandlePanelData *>(panel->activedata);

  /* Only create a new timer if necessary. Reuse can occur when PANEL_STATE_ANIMATION follows
   * PANEL_STATE_DRAG for example (i.e. panel->activedata was present already). */
  if (!data->animtimer) {
    data->animtimer = WM_event_timer_add(CTX_wm_manager(C), win, TIMER, ANIMATION_INTERVAL);
  }

  data->state = state;
  data->startx = win->runtime->eventstate->xy[0];
  data->starty = win->runtime->eventstate->xy[1];
  data->startofsx = panel->ofsx;
  data->startofsy = panel->ofsy;
  data->start_cur_xmin = region->v2d.cur.xmin;
  data->start_cur_ymin = region->v2d.cur.ymin;
  data->starttime = BLI_time_now_seconds();
}

/**
 * \note "select" and "drag drop" flags: First, the panel is "picked up" and both flags are set.
 * Then when the mouse releases and the panel starts animating to its aligned position, PNL_SELECT
 * is unset. When the animation finishes, PANEL_IS_DRAG_DROP is cleared.
 */
static void panel_activate_state(const bContext *C, Panel *panel, const HandlePanelState state)
{
  HandlePanelData *data = static_cast<HandlePanelData *>(panel->activedata);
  wmWindow *win = CTX_wm_window(C);
  ARegion *region = CTX_wm_region(C);

  if (data != nullptr && data->state == state) {
    return;
  }

  if (state == PANEL_STATE_DRAG) {
    panel_custom_data_active_set(panel);

    panel_set_flag_recursive(panel, PNL_SELECT, true);
    panel_set_runtime_flag_recursive(panel, PANEL_IS_DRAG_DROP, true);

    panel_handle_data_ensure(C, win, region, panel, state);

    /* Initiate edge panning during drags for scrolling beyond the initial region view. */
    wmOperatorType *ot = WM_operatortype_find("VIEW2D_OT_edge_pan", true);
    handle_afterfunc_add_operator(ot, wm::OpCallContext::InvokeDefault);
  }
  else if (state == PANEL_STATE_ANIMATION) {
    panel_set_flag_recursive(panel, PNL_SELECT, false);

    panel_handle_data_ensure(C, win, region, panel, state);
  }
  else if (state == PANEL_STATE_EXIT) {
    panel_set_runtime_flag_recursive(panel, PANEL_IS_DRAG_DROP, false);

    BLI_assert(data != nullptr);

    if (data->animtimer) {
      WM_event_timer_remove(CTX_wm_manager(C), win, data->animtimer);
      data->animtimer = nullptr;
    }

    MEM_delete(data);
    panel->activedata = nullptr;

    WM_event_remove_ui_handler(
        &win->runtime->modalhandlers, handler_panel, handler_remove_panel, panel, false);
  }

  ED_region_tag_redraw(region);
}

void panel_stop_animation(const bContext *C, Panel *panel)
{
  if (panel->activedata) {
    panel_activate_state(C, panel, PANEL_STATE_EXIT);
  }
}

/** \} */

}  // namespace blender::ui
