🏡 index : quine-global/bazaar.git

/* bge-carousel.c
 *
 * Copyright 2026 Eva M
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

/**
 * BgeCarousel:
 *
 * Arranges widgets into a horizontal carousel
 */

#define G_LOG_DOMAIN "BGE::CAROUSEL"

#include "bge.h"

#include "bge-marshalers.h"
#include "util.h"

#define RAISE_FACTOR 0.025

/* `ratio < 1.0` means it overshoots */
#define ANIMATION_DAMPING_RATIO 1.15

struct _BgeCarousel
{
  GtkWidget parent_instance;

  GtkEventController *motion;
  double              motion_x;
  double              motion_y;
  GtkEventController *scroll;
  gboolean            scrolling;
  int                 hscroll_start;
  int                 hscroll_current;
  GtkGesture         *drag;
  gboolean            dragging;
  double              drag_x;
  double              drag_y;
  BgeAnimation       *animation;

  gboolean            allow_mouse_drag;
  gboolean            allow_overshoot;
  gboolean            allow_scroll_wheel;
  gboolean            allow_raise;
  gboolean            pass_through_start;
  gboolean            align_start;
  int                 align_offset;
  gboolean            revealed;
  int                 max_page_width;
  gboolean            wrap;
  guint               last_stop;
  GtkSingleSelection *model;

  GPtrArray *mirror;
  GPtrArray *widgets;
};

G_DEFINE_FINAL_TYPE (BgeCarousel, bge_carousel, GTK_TYPE_WIDGET);

enum
{
  PROP_0,

  PROP_ALLOW_MOUSE_DRAG,
  PROP_ALLOW_OVERSHOOT,
  PROP_ALLOW_SCROLL_WHEEL,
  PROP_ALLOW_RAISE,
  PROP_PASS_THROUGH_START,
  PROP_ALIGN_START,
  PROP_MAX_PAGE_WIDTH,
  PROP_WRAP,
  PROP_LAST_STOP,
  PROP_MODEL,

  LAST_PROP
};
static GParamSpec *props[LAST_PROP] = { 0 };

enum
{
  SIGNAL_CREATE_WIDGET,
  SIGNAL_REMOVE_WIDGET,

  LAST_SIGNAL,
};
static guint signals[LAST_SIGNAL];

BGE_DEFINE_DATA (
    carousel_widget,
    CarouselWidget,
    {
      GtkWidget *widget;
      int        width;

      graphene_rect_t rect;
      graphene_rect_t target;

      gboolean raised;

      GCancellable *cancellable;
    },
    BGE_RELEASE_DATA (widget, gtk_widget_unparent);
    BGE_RELEASE_DATA (cancellable, g_object_unref))

static void
items_changed (BgeCarousel *self,
               guint        position,
               guint        removed,
               guint        added,
               GListModel  *model);

static void
model_selected_changed (BgeCarousel        *self,
                        GParamSpec         *pspec,
                        GtkSingleSelection *selection);

static void
move_to_idx (BgeCarousel *self,
             guint        idx,
             /* damping_ratio <= 0.0 means no animation */
             double damping_ratio);

static void
animate (BgeCarousel        *self,
         const char         *key,
         double              value,
         CarouselWidgetData *data);

static void
ensure_viewport (BgeCarousel        *self,
                 GtkSingleSelection *model,
                 gboolean            animate);

static void
motion_enter (BgeCarousel              *self,
              gdouble                   x,
              gdouble                   y,
              GtkEventControllerMotion *controller);

static void
motion_event (BgeCarousel              *self,
              gdouble                   x,
              gdouble                   y,
              GtkEventControllerMotion *controller);

static void
motion_leave (BgeCarousel              *self,
              GtkEventControllerMotion *controller);

static void
update_motion (BgeCarousel *self,
               gdouble      x,
               gdouble      y);

static void
scroll_begin (BgeCarousel              *self,
              GtkEventControllerScroll *controller);

static void
scroll_end (BgeCarousel              *self,
            GtkEventControllerScroll *controller);

static gboolean
scroll (BgeCarousel              *self,
        gdouble                   dx,
        gdouble                   dy,
        GtkEventControllerScroll *controller);

static void
drag_begin (BgeCarousel    *self,
            gdouble         start_x,
            gdouble         start_y,
            GtkGestureDrag *gesture);

static void
drag_end (BgeCarousel    *self,
          gdouble         offset_x,
          gdouble         offset_y,
          GtkGestureDrag *gesture);

static void
drag_update (BgeCarousel    *self,
             gdouble         offset_x,
             gdouble         offset_y,
             GtkGestureDrag *gesture);

static void
finish_horizontal_gesture (BgeCarousel *self,
                           int          offset_x,
                           int          offset_y);

static guint
get_last_index (BgeCarousel *self);

static gboolean
should_wrap (BgeCarousel *self);

static gboolean
is_rtl (BgeCarousel *self);

static void
bge_carousel_dispose (GObject *object)
{
  BgeCarousel *self = BGE_CAROUSEL (object);

  if (self->model != NULL)
    {
      g_signal_handlers_disconnect_by_func (self->model, items_changed, self);
      g_signal_handlers_disconnect_by_func (self->model, model_selected_changed, self);
    }

  g_clear_pointer (&self->animation, g_object_unref);
  g_clear_pointer (&self->model, g_object_unref);

  g_clear_pointer (&self->mirror, g_ptr_array_unref);
  g_clear_pointer (&self->widgets, g_ptr_array_unref);

  G_OBJECT_CLASS (bge_carousel_parent_class)->dispose (object);
}

static void
bge_carousel_get_property (GObject    *object,
                           guint       prop_id,
                           GValue     *value,
                           GParamSpec *pspec)
{
  BgeCarousel *self = BGE_CAROUSEL (object);

  switch (prop_id)
    {
    case PROP_ALLOW_MOUSE_DRAG:
      g_value_set_boolean (value, bge_carousel_get_allow_mouse_drag (self));
      break;
    case PROP_ALLOW_OVERSHOOT:
      g_value_set_boolean (value, bge_carousel_get_allow_overshoot (self));
      break;
    case PROP_ALLOW_SCROLL_WHEEL:
      g_value_set_boolean (value, bge_carousel_get_allow_scroll_wheel (self));
      break;
    case PROP_ALLOW_RAISE:
      g_value_set_boolean (value, bge_carousel_get_allow_raise (self));
      break;
    case PROP_PASS_THROUGH_START:
      g_value_set_boolean (value, bge_carousel_get_pass_through_start (self));
      break;
    case PROP_ALIGN_START:
      g_value_set_boolean (value, bge_carousel_get_align_start (self));
      break;
    case PROP_MAX_PAGE_WIDTH:
      g_value_set_int (value, bge_carousel_get_max_page_width (self));
      break;
    case PROP_WRAP:
      g_value_set_boolean (value, bge_carousel_get_wrap (self));
      break;
    case PROP_LAST_STOP:
      g_value_set_uint (value, bge_carousel_get_last_stop (self));
      break;
    case PROP_MODEL:
      g_value_set_object (value, bge_carousel_get_model (self));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
bge_carousel_set_property (GObject      *object,
                           guint         prop_id,
                           const GValue *value,
                           GParamSpec   *pspec)
{
  BgeCarousel *self = BGE_CAROUSEL (object);

  switch (prop_id)
    {
    case PROP_ALLOW_MOUSE_DRAG:
      bge_carousel_set_allow_mouse_drag (self, g_value_get_boolean (value));
      break;
    case PROP_ALLOW_OVERSHOOT:
      bge_carousel_set_allow_overshoot (self, g_value_get_boolean (value));
      break;
    case PROP_ALLOW_SCROLL_WHEEL:
      bge_carousel_set_allow_scroll_wheel (self, g_value_get_boolean (value));
      break;
    case PROP_ALLOW_RAISE:
      bge_carousel_set_allow_raise (self, g_value_get_boolean (value));
      break;
    case PROP_PASS_THROUGH_START:
      bge_carousel_set_pass_through_start (self, g_value_get_boolean (value));
      break;
    case PROP_ALIGN_START:
      bge_carousel_set_align_start (self, g_value_get_boolean (value));
      break;
    case PROP_MAX_PAGE_WIDTH:
      bge_carousel_set_max_page_width (self, g_value_get_int (value));
      break;
    case PROP_WRAP:
      bge_carousel_set_wrap (self, g_value_get_boolean (value));
      break;
    case PROP_MODEL:
      bge_carousel_set_model (self, g_value_get_object (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
bge_carousel_measure (GtkWidget     *widget,
                      GtkOrientation orientation,
                      int            for_size,
                      int           *minimum,
                      int           *natural,
                      int           *minimum_baseline,
                      int           *natural_baseline)
{
  BgeCarousel *self = BGE_CAROUSEL (widget);

  for (guint i = 0; i < self->widgets->len; i++)
    {
      CarouselWidgetData *child                = NULL;
      int                 tmp_minimum          = 0;
      int                 tmp_natural          = 0;
      int                 tmp_minimum_baseline = 0;
      int                 tmp_natural_baseline = 0;

      child = g_ptr_array_index (self->widgets, i);

      gtk_widget_measure (
          child->widget,
          orientation,
          for_size,
          &tmp_minimum,
          &tmp_natural,
          &tmp_minimum_baseline,
          &tmp_natural_baseline);

      if (tmp_minimum > 0 && tmp_minimum > *minimum)
        *minimum = tmp_minimum;
      if (tmp_natural > 0 && tmp_natural > *natural)
        *natural = tmp_natural;
      if (tmp_minimum_baseline > 0 && tmp_minimum_baseline > *minimum_baseline)
        *minimum_baseline = tmp_minimum_baseline;
      if (tmp_natural_baseline > 0 && tmp_natural_baseline > *natural_baseline)
        *natural_baseline = tmp_natural_baseline;
    }
}

static void
bge_carousel_size_allocate (GtkWidget *widget,
                            int        width,
                            int        height,
                            int        baseline)
{
  BgeCarousel *self = BGE_CAROUSEL (widget);

  ensure_viewport (self, self->model, FALSE);

  for (guint i = 0; i < self->widgets->len; i++)
    {
      CarouselWidgetData *child          = NULL;
      graphene_point_t    origin         = { 0 };
      g_autoptr (GskTransform) transform = NULL;

      child  = g_ptr_array_index (self->widgets, i);
      origin = child->rect.origin;
      if (is_rtl (self))
        origin.x = width - origin.x - child->rect.size.width;

      transform = gsk_transform_translate (
          gsk_transform_new (),
          &origin);

      gtk_widget_allocate (
          child->widget,
          child->rect.size.width,
          child->rect.size.height,
          baseline,
          g_steal_pointer (&transform));
    }
}

static void
bge_carousel_class_init (BgeCarouselClass *klass)
{
  GObjectClass   *object_class = G_OBJECT_CLASS (klass);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

  object_class->set_property = bge_carousel_set_property;
  object_class->get_property = bge_carousel_get_property;
  object_class->dispose      = bge_carousel_dispose;

  /**
   * BgeCarousel:allow-mouse-drag:
   *
   * Whether to allow dragging with the mouse.
   */
  props[PROP_ALLOW_MOUSE_DRAG] =
      g_param_spec_boolean (
          "allow-mouse-drag",
          NULL, NULL, FALSE,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

  /**
   * BgeCarousel:allow-overshoot:
   *
   * Whether to allow overshooting the ends of the carousel with drag/touchpad
   * input. Once the event completes, the carousel offset value will go back to
   * that of the start/end widget. Setting this value to FALSE will prevent this
   * widget from capturing input events which would result in an overshoot.
   */
  props[PROP_ALLOW_OVERSHOOT] =
      g_param_spec_boolean (
          "allow-overshoot",
          NULL, NULL, FALSE,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

  /**
   * BgeCarousel:allow-scroll-wheel:
   *
   * Whether to allow moving the carousel contents with the horizontal scroll
   * wheel.
   */
  props[PROP_ALLOW_SCROLL_WHEEL] =
      g_param_spec_boolean (
          "allow-scroll-wheel",
          NULL, NULL, FALSE,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

  /**
   * BgeCarousel:allow-raise:
   *
   * Whether to allow raise animations when motion input events hover over them
   * carousel widgets.
   */
  props[PROP_ALLOW_RAISE] =
      g_param_spec_boolean (
          "allow-raise",
          NULL, NULL, FALSE,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

  /**
   * BgeCarousel:pass-through-start:
   *
   * Whether drag/touchpad swipes that start by moving back from the first
   * widget are passed on to ancestors, such as the swipe back gesture of a
   * navigation view, instead of overshooting.
   */
  props[PROP_PASS_THROUGH_START] =
      g_param_spec_boolean (
          "pass-through-start",
          NULL, NULL, FALSE,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

  /**
   * BgeCarousel:align-start:
   *
   * Whether to align the selected widget to the start of the carousel instead
   * of centering it. If all widgets fit within the carousel, they are centered
   * as a group.
   */
  props[PROP_ALIGN_START] =
      g_param_spec_boolean (
          "align-start",
          NULL, NULL, FALSE,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

  /**
   * BgeCarousel:max-page-width:
   *
   * The maximum width of each carousel widget, or -1 for no limit.
   */
  props[PROP_MAX_PAGE_WIDTH] =
      g_param_spec_int (
          "max-page-width",
          NULL, NULL,
          -1, G_MAXINT, -1,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

  /**
   * BgeCarousel:wrap:
   *
   * Whether to wrap around from the last widget to the first and vice versa.
   * Only applies when [property@Bge.Carousel:align-start] is not set.
   */
  props[PROP_WRAP] =
      g_param_spec_boolean (
          "wrap",
          NULL, NULL, FALSE,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

  /**
   * BgeCarousel:last-stop:
   *
   * The index of the last widget the carousel can move to. When
   * [property@Bge.Carousel:align-start] is set, this is the first widget that
   * brings the end of the carousel into view.
   */
  props[PROP_LAST_STOP] =
      g_param_spec_uint (
          "last-stop",
          NULL, NULL,
          0, G_MAXUINT, 0,
          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

  /**
   * BgeCarousel:model:
   *
   * The selection model for the carousel contents.
   */
  props[PROP_MODEL] =
      g_param_spec_object (
          "model",
          NULL, NULL,
          GTK_TYPE_SINGLE_SELECTION,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

  g_object_class_install_properties (object_class, LAST_PROP, props);

  /**
   * BgeCarousel::create-widget:
   * @carousel: the object that received the signal
   * @object: a list item object from [property@Bge.Carousel:model]
   *
   * Emitted when an object is being bound to the carousel
   *
   * Return: a newly allocated widget to add to the carousel
   */
  signals[SIGNAL_CREATE_WIDGET] =
      g_signal_new (
          "create-widget",
          G_OBJECT_CLASS_TYPE (klass),
          G_SIGNAL_RUN_FIRST,
          0,
          NULL, NULL,
          bge_marshal_OBJECT__OBJECT,
          GTK_TYPE_WIDGET,
          1,
          G_TYPE_OBJECT);
  g_signal_set_va_marshaller (
      signals[SIGNAL_CREATE_WIDGET],
      G_TYPE_FROM_CLASS (klass),
      bge_marshal_OBJECT__OBJECTv);

  /**
   * BgeCarousel::remove-widget:
   * @carousel: the object that received the signal
   * @widget: the widget which was created by [signal@Bge.Carousel::create-widget]
   * @object: a list item object from [property@Bge.Carousel:model]
   *
   * Emitted when an object is being unbound from the carousel
   */
  signals[SIGNAL_REMOVE_WIDGET] =
      g_signal_new (
          "remove-widget",
          G_OBJECT_CLASS_TYPE (klass),
          G_SIGNAL_RUN_FIRST,
          0,
          NULL, NULL,
          bge_marshal_VOID__OBJECT_OBJECT,
          G_TYPE_NONE,
          2,
          GTK_TYPE_WIDGET, G_TYPE_OBJECT);
  g_signal_set_va_marshaller (
      signals[SIGNAL_REMOVE_WIDGET],
      G_TYPE_FROM_CLASS (klass),
      bge_marshal_VOID__OBJECT_OBJECTv);

  widget_class->measure       = bge_carousel_measure;
  widget_class->size_allocate = bge_carousel_size_allocate;
}

static void
bge_carousel_init (BgeCarousel *self)
{
  self->animation = bge_animation_new (GTK_WIDGET (self));

  self->max_page_width = -1;

  self->mirror = g_ptr_array_new_with_free_func (
      (GDestroyNotify) g_object_unref);
  self->widgets = g_ptr_array_new_with_free_func (
      carousel_widget_data_unref);

  gtk_widget_set_overflow (GTK_WIDGET (self), GTK_OVERFLOW_HIDDEN);

  self->motion = gtk_event_controller_motion_new ();
  g_signal_connect_swapped (self->motion, "enter", G_CALLBACK (motion_enter), self);
  g_signal_connect_swapped (self->motion, "motion", G_CALLBACK (motion_event), self);
  g_signal_connect_swapped (self->motion, "leave", G_CALLBACK (motion_leave), self);
  gtk_widget_add_controller (GTK_WIDGET (self), self->motion);

  self->scroll = gtk_event_controller_scroll_new (
      GTK_EVENT_CONTROLLER_SCROLL_HORIZONTAL |
      GTK_EVENT_CONTROLLER_SCROLL_KINETIC);
  g_signal_connect_swapped (self->scroll, "scroll-begin", G_CALLBACK (scroll_begin), self);
  g_signal_connect_swapped (self->scroll, "scroll-end", G_CALLBACK (scroll_end), self);
  g_signal_connect_swapped (self->scroll, "scroll", G_CALLBACK (scroll), self);
  gtk_widget_add_controller (GTK_WIDGET (self), self->scroll);

  self->drag = gtk_gesture_drag_new ();
  gtk_event_controller_set_propagation_phase (GTK_EVENT_CONTROLLER (self->drag), GTK_PHASE_CAPTURE);
  g_signal_connect_swapped (self->drag, "drag-begin", G_CALLBACK (drag_begin), self);
  g_signal_connect_swapped (self->drag, "drag-end", G_CALLBACK (drag_end), self);
  g_signal_connect_swapped (self->drag, "drag-update", G_CALLBACK (drag_update), self);
  gtk_widget_add_controller (GTK_WIDGET (self), GTK_EVENT_CONTROLLER (self->drag));
}

/**
 * bge_carousel_new:
 *
 * Creates a new `BgeCarousel` object.
 *
 * Returns: The newly created `BgeCarousel` object.
 */
GtkWidget *
bge_carousel_new (void)
{
  return g_object_new (BGE_TYPE_CAROUSEL, NULL);
}

/**
 * bge_carousel_get_allow_mouse_drag:
 * @self: a `BgeCarousel`
 *
 * Gets [property@Bge.Carousel:allow-mouse-drag].
 *
 * Returns: the value of the property
 */
gboolean
bge_carousel_get_allow_mouse_drag (BgeCarousel *self)
{
  g_return_val_if_fail (BGE_IS_CAROUSEL (self), FALSE);
  return self->allow_mouse_drag;
}

/**
 * bge_carousel_get_allow_overshoot:
 * @self: a `BgeCarousel`
 *
 * Gets [property@Bge.Carousel:allow-overshoot].
 *
 * Returns: the value of the property
 */
gboolean
bge_carousel_get_allow_overshoot (BgeCarousel *self)
{
  g_return_val_if_fail (BGE_IS_CAROUSEL (self), FALSE);
  return self->allow_overshoot;
}

/**
 * bge_carousel_get_allow_scroll_wheel:
 * @self: a `BgeCarousel`
 *
 * Gets [property@Bge.Carousel:allow-scroll-wheel].
 *
 * Returns: the value of the property
 */
gboolean
bge_carousel_get_allow_scroll_wheel (BgeCarousel *self)
{
  g_return_val_if_fail (BGE_IS_CAROUSEL (self), FALSE);
  return self->allow_scroll_wheel;
}

/**
 * bge_carousel_get_allow_raise:
 * @self: a `BgeCarousel`
 *
 * Gets [property@Bge.Carousel:allow-raise].
 *
 * Returns: the value of the property
 */
gboolean
bge_carousel_get_allow_raise (BgeCarousel *self)
{
  g_return_val_if_fail (BGE_IS_CAROUSEL (self), FALSE);
  return self->allow_raise;
}

/**
 * bge_carousel_get_pass_through_start:
 * @self: a `BgeCarousel`
 *
 * Gets [property@Bge.Carousel:pass-through-start].
 *
 * Returns: the value of the property
 */
gboolean
bge_carousel_get_pass_through_start (BgeCarousel *self)
{
  g_return_val_if_fail (BGE_IS_CAROUSEL (self), FALSE);
  return self->pass_through_start;
}

/**
 * bge_carousel_get_align_start:
 * @self: a `BgeCarousel`
 *
 * Gets [property@Bge.Carousel:align-start].
 *
 * Returns: the value of the property
 */
gboolean
bge_carousel_get_align_start (BgeCarousel *self)
{
  g_return_val_if_fail (BGE_IS_CAROUSEL (self), FALSE);
  return self->align_start;
}

/**
 * bge_carousel_get_max_page_width:
 * @self: a `BgeCarousel`
 *
 * Gets [property@Bge.Carousel:max-page-width].
 *
 * Returns: the value of the property
 */
int
bge_carousel_get_max_page_width (BgeCarousel *self)
{
  g_return_val_if_fail (BGE_IS_CAROUSEL (self), -1);
  return self->max_page_width;
}

/**
 * bge_carousel_get_wrap:
 * @self: a `BgeCarousel`
 *
 * Gets [property@Bge.Carousel:wrap].
 *
 * Returns: the value of the property
 */
gboolean
bge_carousel_get_wrap (BgeCarousel *self)
{
  g_return_val_if_fail (BGE_IS_CAROUSEL (self), FALSE);
  return self->wrap;
}

/**
 * bge_carousel_get_last_stop:
 * @self: a `BgeCarousel`
 *
 * Gets [property@Bge.Carousel:last-stop].
 *
 * Returns: the value of the property
 */
guint
bge_carousel_get_last_stop (BgeCarousel *self)
{
  g_return_val_if_fail (BGE_IS_CAROUSEL (self), 0);
  return self->last_stop;
}

/**
 * bge_carousel_get_model:
 * @self: a `BgeCarousel`
 *
 * Gets [property@Bge.Carousel:model].
 *
 * Returns: (nullable): the value of the property
 */
GtkSingleSelection *
bge_carousel_get_model (BgeCarousel *self)
{
  g_return_val_if_fail (BGE_IS_CAROUSEL (self), NULL);
  return self->model;
}

/**
 * bge_carousel_get_nth_page:
 * @self: a `BgeCarousel`
 * @index: Index of the page.
 *
 * Gets the carousel widget at @index.
 *
 * Returns: (nullable) (transfer none): the page at @index, or NULL if out of bounds
 */
GtkWidget *
bge_carousel_get_nth_page (BgeCarousel *self,
                           guint        index)
{
  CarouselWidgetData *data = NULL;

  g_return_val_if_fail (BGE_IS_CAROUSEL (self), NULL);

  if (index >= self->widgets->len)
    return NULL;

  data = g_ptr_array_index (self->widgets, index);
  return data->widget;
}

/**
 * bge_carousel_reveal:
 * @self: a `BgeCarousel`
 * @index: the index of the widget to reveal
 *
 * Scrolls just far enough for the widget at @index to be fully visible when
 * [property@Bge.Carousel:align-start] is set, otherwise selects @index.
 */
void
bge_carousel_reveal (BgeCarousel *self,
                     guint        index)
{
  int   width       = 0;
  int   total_width = 0;
  int   start       = 0;
  int   end         = 0;
  int   offset      = 0;
  guint first       = 0;

  g_return_if_fail (BGE_IS_CAROUSEL (self));

  if (index >= self->widgets->len)
    return;

  width = gtk_widget_get_width (GTK_WIDGET (self));
  for (guint i = 0; i < self->widgets->len; i++)
    {
      CarouselWidgetData *child = NULL;

      child = g_ptr_array_index (self->widgets, i);
      if (i < index)
        start += child->width;
      else if (i == index)
        end = start + child->width;
      total_width += child->width;
    }

  if (!self->align_start || total_width <= width)
    {
      gtk_single_selection_set_selected (self->model, index);
      return;
    }

  offset = CLAMP (self->align_offset, -start, width - end);
  offset = CLAMP (offset, width - total_width, 0);

  for (start = 0; first < self->widgets->len - 1 && start + offset < 0; first++)
    {
      CarouselWidgetData *child = NULL;

      child = g_ptr_array_index (self->widgets, first);
      start += child->width;
    }
  if (offset > width - total_width && start >= total_width - width)
    first--;

  gtk_single_selection_set_selected (self->model, first);
  self->align_offset = offset;
  self->revealed     = TRUE;
  ensure_viewport (self, self->model, TRUE);
}

/**
 * bge_carousel_set_allow_mouse_drag:
 * @self: a `BgeCarousel`
 * @allow_mouse_drag: a boolean
 *
 * Sets [property@Bge.Carousel:allow-mouse-drag].
 */
void
bge_carousel_set_allow_mouse_drag (BgeCarousel *self,
                                   gboolean     allow_mouse_drag)
{
  g_return_if_fail (BGE_IS_CAROUSEL (self));

  if (!!allow_mouse_drag == !!self->allow_mouse_drag)
    return;

  self->allow_mouse_drag = allow_mouse_drag;
  if (!allow_mouse_drag && self->dragging)
    {
      self->dragging = FALSE;
      finish_horizontal_gesture (self, self->drag_x, self->drag_y);
      self->drag_x = 0.0;
      self->drag_y = 0.0;
    }

  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_ALLOW_MOUSE_DRAG]);
}

/**
 * bge_carousel_set_allow_overshoot:
 * @self: a `BgeCarousel`
 * @allow_overshoot: a boolean
 *
 * Sets [property@Bge.Carousel:allow-overshoot].
 */
void
bge_carousel_set_allow_overshoot (BgeCarousel *self,
                                  gboolean     allow_overshoot)
{
  g_return_if_fail (BGE_IS_CAROUSEL (self));

  if (!!allow_overshoot == !!self->allow_overshoot)
    return;

  self->allow_overshoot = allow_overshoot;

  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_ALLOW_OVERSHOOT]);
}

/**
 * bge_carousel_set_allow_scroll_wheel:
 * @self: a `BgeCarousel`
 * @allow_scroll_wheel: a boolean
 *
 * Sets [property@Bge.Carousel:allow-scroll-wheel].
 */
void
bge_carousel_set_allow_scroll_wheel (BgeCarousel *self,
                                     gboolean     allow_scroll_wheel)
{
  g_return_if_fail (BGE_IS_CAROUSEL (self));

  if (!!allow_scroll_wheel == !!self->allow_scroll_wheel)
    return;

  self->allow_scroll_wheel = allow_scroll_wheel;

  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_ALLOW_SCROLL_WHEEL]);
}

/**
 * bge_carousel_set_allow_raise:
 * @self: a `BgeCarousel`
 * @allow_raise: a boolean
 *
 * Sets [property@Bge.Carousel:allow-raise].
 */
void
bge_carousel_set_allow_raise (BgeCarousel *self,
                              gboolean     allow_raise)
{
  g_return_if_fail (BGE_IS_CAROUSEL (self));

  if (!!allow_raise == !!self->allow_raise)
    return;

  self->allow_raise = allow_raise;
  if (self->model != NULL)
    ensure_viewport (self, self->model, TRUE);

  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_ALLOW_RAISE]);
}

/**
 * bge_carousel_set_pass_through_start:
 * @self: a `BgeCarousel`
 * @pass_through_start: a boolean
 *
 * Sets [property@Bge.Carousel:pass-through-start].
 */
void
bge_carousel_set_pass_through_start (BgeCarousel *self,
                                     gboolean     pass_through_start)
{
  g_return_if_fail (BGE_IS_CAROUSEL (self));

  if (!!pass_through_start == !!self->pass_through_start)
    return;

  self->pass_through_start = pass_through_start;
  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_PASS_THROUGH_START]);
}

/**
 * bge_carousel_set_align_start:
 * @self: a `BgeCarousel`
 * @align_start: a boolean
 *
 * Sets [property@Bge.Carousel:align-start].
 */
void
bge_carousel_set_align_start (BgeCarousel *self,
                              gboolean     align_start)
{
  g_return_if_fail (BGE_IS_CAROUSEL (self));

  if (!!align_start == !!self->align_start)
    return;

  self->align_start = align_start;
  if (self->model != NULL)
    ensure_viewport (self, self->model, TRUE);

  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_ALIGN_START]);
}

/**
 * bge_carousel_set_max_page_width:
 * @self: a `BgeCarousel`
 * @max_page_width: an integer
 *
 * Sets [property@Bge.Carousel:max-page-width].
 */
void
bge_carousel_set_max_page_width (BgeCarousel *self,
                                 int          max_page_width)
{
  g_return_if_fail (BGE_IS_CAROUSEL (self));

  if (max_page_width == self->max_page_width)
    return;

  self->max_page_width = max_page_width;
  if (self->model != NULL)
    ensure_viewport (self, self->model, TRUE);

  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_MAX_PAGE_WIDTH]);
}

/**
 * bge_carousel_set_wrap:
 * @self: a `BgeCarousel`
 * @wrap: a boolean
 *
 * Sets [property@Bge.Carousel:wrap].
 */
void
bge_carousel_set_wrap (BgeCarousel *self,
                       gboolean     wrap)
{
  g_return_if_fail (BGE_IS_CAROUSEL (self));

  if (!!wrap == !!self->wrap)
    return;

  self->wrap = wrap;
  if (self->model != NULL)
    ensure_viewport (self, self->model, TRUE);

  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_WRAP]);
}

/**
 * bge_carousel_set_model:
 * @self: a `BgeCarousel`
 * @model: a `GtkSingleSelection` object
 *
 * Sets [property@Bge.Carousel:model].
 */
void
bge_carousel_set_model (BgeCarousel        *self,
                        GtkSingleSelection *model)
{
  g_return_if_fail (BGE_IS_CAROUSEL (self));
  g_return_if_fail (model == NULL || GTK_IS_SINGLE_SELECTION (model));

  if (model == self->model)
    return;

  if (self->model != NULL)
    {
      g_signal_handlers_disconnect_by_func (self->model, items_changed, self);
      g_signal_handlers_disconnect_by_func (self->model, model_selected_changed, self);
      items_changed (
          self,
          0,
          g_list_model_get_n_items (G_LIST_MODEL (self->model)),
          0,
          G_LIST_MODEL (self->model));
    }
  g_clear_pointer (&self->model, g_object_unref);

  if (model != NULL)
    {
      self->model = g_object_ref (model);
      items_changed (
          self,
          0,
          0,
          g_list_model_get_n_items (G_LIST_MODEL (model)),
          G_LIST_MODEL (model));

      g_signal_connect_swapped (
          model,
          "items-changed",
          G_CALLBACK (items_changed),
          self);
      g_signal_connect_swapped (
          model,
          "notify::selected",
          G_CALLBACK (model_selected_changed),
          self);
    }

  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_MODEL]);
}

static void
items_changed (BgeCarousel *self,
               guint        position,
               guint        removed,
               guint        added,
               GListModel  *model)
{
  self->revealed = FALSE;
  for (guint i = 0; i < removed; i++)
    {
      GObject            *object = NULL;
      CarouselWidgetData *child  = NULL;

      object = g_ptr_array_index (self->mirror, position + i);
      child  = g_ptr_array_index (self->widgets, position + i);

      if (child->cancellable != NULL)
        g_cancellable_cancel (child->cancellable);
      g_signal_emit (self, signals[SIGNAL_REMOVE_WIDGET], 0, child->widget, object);
    }
  if (removed > 0)
    {
      g_ptr_array_remove_range (self->mirror, position, removed);
      g_ptr_array_remove_range (self->widgets, position, removed);
    }

  for (guint i = 0; i < added; i++)
    {
      g_autoptr (GObject) object = NULL;
      GtkWidget          *child  = NULL;
      CarouselWidgetData *data   = NULL;

      object = g_list_model_get_item (model, position + i);

      g_signal_emit (self, signals[SIGNAL_CREATE_WIDGET], 0, object, &child);
      if (child == NULL)
        {
          g_critical ("Failed to populate child for carousel widget");
          child = gtk_fixed_new ();
        }

      if (position + i == 0)
        gtk_widget_set_parent (child, GTK_WIDGET (self));
      else
        {
          CarouselWidgetData *prev = NULL;

          prev = g_ptr_array_index (self->widgets, position + i - 1);
          gtk_widget_insert_after (child, GTK_WIDGET (self), prev->widget);
        }

      data         = carousel_widget_data_new ();
      data->widget = child;

      g_ptr_array_insert (self->mirror, position + i, g_object_ref (object));
      g_ptr_array_insert (self->widgets, position + i, data);
    }

  ensure_viewport (self, GTK_SINGLE_SELECTION (model), FALSE);
}

static void
model_selected_changed (BgeCarousel        *self,
                        GParamSpec         *pspec,
                        GtkSingleSelection *selection)
{
  guint idx = 0;

  self->revealed = FALSE;
  idx = gtk_single_selection_get_selected (selection);
  if (idx != G_MAXUINT)
    move_to_idx (self, idx, ANIMATION_DAMPING_RATIO);

  gtk_widget_queue_allocate (GTK_WIDGET (self));
}

static void
move_to_idx (BgeCarousel *self,
             guint        idx,
             /* damping_ratio <= 0.0 means no animation */
             double damping_ratio)
{
  int             width        = 0;
  int             height       = 0;
  int             offset       = 0;
  int             shift        = 0;
  g_autofree int *widths       = NULL;
  g_autofree int *xs           = NULL;
  int             total_width  = 0;
  int             before_width = 0;
  guint           last_stop    = 0;

  width  = gtk_widget_get_width (GTK_WIDGET (self));
  height = gtk_widget_get_height (GTK_WIDGET (self));
  if (width == 0 ||
      height == 0)
    {
      gtk_widget_queue_allocate (GTK_WIDGET (self));
      return;
    }

  widths = g_new0 (int, self->widgets->len);
  for (guint i = 0; i < self->widgets->len; i++)
    {
      CarouselWidgetData *child    = NULL;
      int                 hminimum = 0;
      int                 hnatural = 0;
      int                 unused   = 0;

      child = g_ptr_array_index (self->widgets, i);

      gtk_widget_measure (
          child->widget,
          GTK_ORIENTATION_HORIZONTAL,
          height,
          &hminimum,
          &hnatural,
          &unused,
          &unused);
      if (gtk_widget_get_hexpand (child->widget))
        widths[i] = MAX (hminimum, width);
      else
        widths[i] = CLAMP (hnatural, hminimum, width);
      if (self->max_page_width >= 0)
        widths[i] = MIN (widths[i], self->max_page_width);

      total_width += widths[i];
      if (i < idx)
        before_width += widths[i];
    }

  if (self->align_start)
    {
      if (total_width <= width)
        {
          offset    = (width - total_width) / 2;
          last_stop = 0;
        }
      else
        {
          int start = 0;

          if (self->revealed)
            offset = CLAMP (self->align_offset, width - total_width, 0);
          else
            offset = MAX (-before_width, width - total_width);

          while (last_stop < self->widgets->len - 1 &&
                 start < total_width - width)
            start += widths[last_stop++];
        }
    }
  else
    {
      offset    = width / 2 - before_width - widths[idx] / 2;
      last_stop = self->widgets->len - 1;
    }
  self->align_offset = offset;

  if (self->scrolling)
    shift += self->hscroll_start - self->hscroll_current;
  if (self->dragging)
    shift += self->drag_x;
  offset += shift;

  xs = g_new0 (int, self->widgets->len);
  if (should_wrap (self))
    {
      guint n     = self->widgets->len;
      int   x     = offset + before_width;
      guint left  = shift > 0 ? n / 2 : (n - 1) / 2;

      for (guint d = 0; d < n - left; d++)
        {
          xs[(idx + d) % n] = x;
          x += widths[(idx + d) % n];
        }
      x = offset + before_width;
      for (guint d = 1; d <= left; d++)
        {
          x -= widths[(idx + n - d) % n];
          xs[(idx + n - d) % n] = x;
        }
    }
  else
    for (guint i = 0; i < self->widgets->len; i++)
      {
        xs[i] = offset;
        offset += widths[i];
      }

  if (last_stop != self->last_stop)
    {
      self->last_stop = last_stop;
      g_object_notify_by_pspec (G_OBJECT (self), props[PROP_LAST_STOP]);
    }

  for (guint i = 0; i < self->widgets->len; i++)
    {
      CarouselWidgetData *child           = NULL;
      int                 hminimum        = 0;
      int                 hnatural        = 0;
      int                 unused          = 0;
      int                 rect_width      = 0;
      int                 child_width     = 0;
      int                 child_height    = 0;
      int                 child_x         = 0;
      int                 child_y         = 0;
      graphene_rect_t     target          = { 0 };
      gboolean            avoid_animation = FALSE;
      gboolean            offscreen       = FALSE;

      child      = g_ptr_array_index (self->widgets, i);
      rect_width = widths[i];

      if (child->raised || !self->allow_raise)
        {
          child_width  = rect_width;
          child_height = height;
          child_x      = xs[i];
          child_y      = 0;
        }
      else
        {
          child_height = round ((double) height * (1.0 - RAISE_FACTOR));

          gtk_widget_measure (
              child->widget,
              GTK_ORIENTATION_HORIZONTAL,
              child_height,
              &hminimum,
              &hnatural,
              &unused,
              &unused);
          child_width = CLAMP (hnatural, hminimum, width);
          if (child_width > rect_width)
            child_width = round ((double) rect_width * (1.0 - RAISE_FACTOR));

          child_x = xs[i] + round ((double) (rect_width - child_width) * 0.5);
          child_y = round ((double) height * (0.5 * RAISE_FACTOR));
        }

      target          = GRAPHENE_RECT_INIT (child_x, child_y, child_width, child_height);
      avoid_animation = graphene_rect_equal (&target, &child->target);
      offscreen = (child->rect.origin.x + child->rect.size.width <= 0 || child->rect.origin.x >= width) &&
                  (target.origin.x + target.size.width <= 0 || target.origin.x >= width);
      if (child->width != rect_width ||
          (damping_ratio < 0.0 && !avoid_animation) ||
          (should_wrap (self) && offscreen))
        {
          if (child->cancellable != NULL)
            g_cancellable_cancel (child->cancellable);

          child->width  = rect_width;
          child->rect   = target;
          child->target = target;
        }
      else if (avoid_animation)
        child->target = target;
      else
        {
          char buf[64] = { 0 };

          g_clear_object (&child->cancellable);
          child->cancellable = g_cancellable_new ();

#define MASS      1.0
#define STIFFNESS 800.0

          /* pointer is to ensure a unique identifier so as not to overwrite any
             other child's key */
          g_snprintf (buf, sizeof (buf), "x%p", child);
          bge_animation_add_spring (
              self->animation, buf,
              child->rect.origin.x, target.origin.x,
              damping_ratio, MASS, STIFFNESS,
              (BgeAnimationCallback) animate,
              carousel_widget_data_ref (child),
              carousel_widget_data_unref,
              child->cancellable);

          g_snprintf (buf, sizeof (buf), "y%p", child);
          bge_animation_add_spring (
              self->animation, buf,
              child->rect.origin.y, target.origin.y,
              damping_ratio, MASS, STIFFNESS,
              (BgeAnimationCallback) animate,
              carousel_widget_data_ref (child),
              carousel_widget_data_unref,
              child->cancellable);

          g_snprintf (buf, sizeof (buf), "w%p", child);
          bge_animation_add_spring (
              self->animation, buf,
              child->rect.size.width, target.size.width,
              damping_ratio, MASS, STIFFNESS,
              (BgeAnimationCallback) animate,
              carousel_widget_data_ref (child),
              carousel_widget_data_unref,
              child->cancellable);

          g_snprintf (buf, sizeof (buf), "h%p", child);
          bge_animation_add_spring (
              self->animation, buf,
              child->rect.size.height, target.size.height,
              damping_ratio, MASS, STIFFNESS,
              (BgeAnimationCallback) animate,
              carousel_widget_data_ref (child),
              carousel_widget_data_unref,
              child->cancellable);

#undef STIFFNESS
#undef MASS

          child->target = target;
        }
    }

  gtk_widget_queue_allocate (GTK_WIDGET (self));
}

static void
animate (BgeCarousel        *self,
         const char         *key,
         double              value,
         CarouselWidgetData *data)
{
  switch (*key)
    {
    case 'x':
      data->rect.origin.x = value;
      break;
    case 'y':
      data->rect.origin.y = value;
      break;
    case 'w':
      data->rect.size.width = value;
      break;
    case 'h':
      data->rect.size.height = value;
      break;
    default:
      g_assert_not_reached ();
    }

  gtk_widget_queue_allocate (GTK_WIDGET (self));
}

static void
ensure_viewport (BgeCarousel        *self,
                 GtkSingleSelection *model,
                 gboolean            animate)
{
  guint n_items = 0;

  n_items = g_list_model_get_n_items (G_LIST_MODEL (model));
  if (n_items > 0)
    {
      guint selected = 0;

      selected = gtk_single_selection_get_selected (model);
      if (selected == G_MAXUINT)
        {
          gtk_single_selection_set_selected (model, 0);
          move_to_idx (self, 0, animate ? ANIMATION_DAMPING_RATIO : -1.0);
        }
      else
        move_to_idx (self, selected, animate ? ANIMATION_DAMPING_RATIO : -1.0);
    }

  gtk_widget_queue_allocate (GTK_WIDGET (self));
}

static void
motion_enter (BgeCarousel              *self,
              gdouble                   x,
              gdouble                   y,
              GtkEventControllerMotion *controller)
{
  self->motion_x = x;
  self->motion_y = y;
  update_motion (self, x, y);
}

static void
motion_event (BgeCarousel              *self,
              gdouble                   x,
              gdouble                   y,
              GtkEventControllerMotion *controller)
{
  self->motion_x = x;
  self->motion_y = y;
  update_motion (self, x, y);
}

static void
motion_leave (BgeCarousel              *self,
              GtkEventControllerMotion *controller)
{
  if (self->dragging)
    return;
  self->motion_x = -1.0;
  self->motion_y = -1.0;
  update_motion (self, -1.0, -1.0);
}

static void
update_motion (BgeCarousel *self,
               gdouble      x,
               gdouble      y)
{
  graphene_point_t point  = { 0 };
  gboolean         ensure = FALSE;

  if (is_rtl (self) && x >= 0.0)
    x = gtk_widget_get_width (GTK_WIDGET (self)) - x;
  point = GRAPHENE_POINT_INIT (x, y);

  if (self->scrolling)
    return;

  for (guint i = 0; i < self->widgets->len; i++)
    {
      CarouselWidgetData *child     = NULL;
      gboolean            contained = FALSE;

      child     = g_ptr_array_index (self->widgets, i);
      contained = graphene_rect_contains_point (&child->target, &point);
      if (!!contained != !!child->raised)
        {
          child->raised = contained;
          ensure        = TRUE;
        }
    }

  if (self->dragging)
    return;
  if (ensure)
    ensure_viewport (self, self->model, TRUE);
}

static void
scroll_begin (BgeCarousel              *self,
              GtkEventControllerScroll *controller)
{
  self->scrolling       = TRUE;
  self->hscroll_start   = self->motion_x;
  self->hscroll_current = self->motion_x;
}

static void
scroll_end (BgeCarousel              *self,
            GtkEventControllerScroll *controller)
{
  self->scrolling = FALSE;
  finish_horizontal_gesture (
      self,
      self->hscroll_start - self->hscroll_current,
      0);
  self->hscroll_start   = -1;
  self->hscroll_current = -1;

  update_motion (self, self->motion_x, self->motion_y);
}

static gboolean
scroll (BgeCarousel              *self,
        gdouble                   dx,
        gdouble                   dy,
        GtkEventControllerScroll *controller)
{
  guint          n_items = 0;
  GdkDevice     *device  = NULL;
  GdkInputSource source  = 0;

  if (self->model == NULL)
    {
      self->scrolling = FALSE;
      return FALSE;
    }

  if (is_rtl (self))
    dx = -dx;

  n_items = g_list_model_get_n_items (G_LIST_MODEL (self->model));
  if (n_items == 0)
    {
      self->scrolling = FALSE;
      return FALSE;
    }

  device = gtk_event_controller_get_current_event_device (
      GTK_EVENT_CONTROLLER (controller));
  source = gdk_device_get_source (device);

  switch (source)
    {
    case GDK_SOURCE_TOUCHPAD:
    case GDK_SOURCE_TRACKPOINT:
      {
        if (self->pass_through_start &&
            self->hscroll_current == self->hscroll_start &&
            dx < 0 &&
            gtk_single_selection_get_selected (self->model) == 0 &&
            !should_wrap (self))
          {
            self->scrolling = FALSE;
            return FALSE;
          }

        if (self->widgets->len > 0 &&
            !self->allow_overshoot &&
            !should_wrap (self))
          {
            guint selection = 0;

            selection = gtk_single_selection_get_selected (self->model);
            if ((selection == 0 && self->hscroll_current + dx < 0) ||
                (selection >= get_last_index (self) && self->hscroll_current + dx > 0))
              {
                self->scrolling = FALSE;
                return FALSE;
              }
          }

        self->hscroll_current += dx;
        ensure_viewport (self, self->model, FALSE);
      }
      break;
    case GDK_SOURCE_MOUSE:
    case GDK_SOURCE_PEN:
    case GDK_SOURCE_KEYBOARD:
    case GDK_SOURCE_TOUCHSCREEN:
    case GDK_SOURCE_TABLET_PAD:
    default:
      {
        guint selected     = 0;
        guint new_selected = 0;

        if (!self->allow_scroll_wheel)
          break;

        selected = MIN (gtk_single_selection_get_selected (self->model),
                        get_last_index (self));
        if (dx > 0)
          {
            if (selected == get_last_index (self) && should_wrap (self))
              new_selected = 0;
            else
              new_selected = MIN (selected + 1, get_last_index (self));
          }
        else
          {
            if (selected == 0)
              new_selected = should_wrap (self) ? get_last_index (self) : 0;
            else
              new_selected = selected - 1;
          }
        gtk_single_selection_set_selected (self->model, new_selected);
      }
      break;
    }

  return TRUE;
}

static void
drag_begin (BgeCarousel    *self,
            gdouble         start_x,
            gdouble         start_y,
            GtkGestureDrag *gesture)
{
  if (!self->allow_mouse_drag)
    return;

  self->dragging = TRUE;
  if (self->model == NULL)
    return;

  ensure_viewport (self, self->model, TRUE);
}

static void
drag_end (BgeCarousel    *self,
          gdouble         offset_x,
          gdouble         offset_y,
          GtkGestureDrag *gesture)
{
  if (!self->dragging)
    /* This situation will happen if the `allow-mouse-drag` prop is set to FALSE
       while a drag is taking place */
    return;

  self->dragging = FALSE;
  finish_horizontal_gesture (self, self->drag_x, self->drag_y);
  self->drag_x = 0.0;
  self->drag_y = 0.0;

  if (offset_x < -3 ||
      offset_x > 3 ||
      offset_y < -3 ||
      offset_y > 3)
    gtk_gesture_set_state (GTK_GESTURE (gesture), GTK_EVENT_SEQUENCE_CLAIMED);
}

static void
drag_update (BgeCarousel    *self,
             gdouble         offset_x,
             gdouble         offset_y,
             GtkGestureDrag *gesture)
{
  if (!self->dragging)
    return;

  if (is_rtl (self))
    offset_x = -offset_x;

  if (self->pass_through_start &&
      gtk_gesture_get_sequence_state (GTK_GESTURE (gesture),
                                      gtk_gesture_single_get_current_sequence (GTK_GESTURE_SINGLE (gesture))) !=
          GTK_EVENT_SEQUENCE_CLAIMED)
    {
      if (!gtk_drag_check_threshold (GTK_WIDGET (self), 0, 0, offset_x, offset_y))
        return;

      if (offset_x > ABS (offset_y) &&
          self->model != NULL &&
          gtk_single_selection_get_selected (self->model) == 0 &&
          !should_wrap (self))
        {
          self->dragging = FALSE;
          gtk_gesture_set_state (GTK_GESTURE (gesture), GTK_EVENT_SEQUENCE_DENIED);
          return;
        }

      gtk_gesture_set_state (GTK_GESTURE (gesture), GTK_EVENT_SEQUENCE_CLAIMED);
    }

  self->drag_x = offset_x;
  self->drag_y = offset_y;

  if (self->model == NULL)
    return;

  if (self->widgets->len > 0 &&
      !self->allow_overshoot &&
      !should_wrap (self))
    {
      guint selected = 0;

      selected = gtk_single_selection_get_selected (self->model);
      if (selected == 0)
        self->drag_x = MIN (self->drag_x, 0.0);
      if (selected >= get_last_index (self))
        self->drag_x = MAX (self->drag_x, 0.0);
    }

  ensure_viewport (self, self->model, FALSE);
}

static void
finish_horizontal_gesture (BgeCarousel *self,
                           int          offset_x,
                           int          offset_y)
{
  guint  selected     = 0;
  double width        = 0.0;
  double anchor       = 0.0;
  guint  last         = 0;
  guint  new_selected = G_MAXUINT;
  int    min_distance = G_MAXINT;

  if (self->model == NULL ||
      self->widgets->len == 0)
    return;

  if (offset_x == 0)
    return;

  last     = get_last_index (self);
  selected = MIN (gtk_single_selection_get_selected (self->model), last);

  width  = gtk_widget_get_width (GTK_WIDGET (self));
  anchor = self->align_start ? 0.0 : width / 2.0;
  for (guint i = 0; i <= last; i++)
    {
      CarouselWidgetData *child                = NULL;
      int                 left_distance        = 0;
      int                 right_distance       = 0;
      int                 distance_from_center = 0;

      child = g_ptr_array_index (self->widgets, i);

      if (child->rect.origin.x > anchor)
        left_distance = child->rect.origin.x - anchor;
      else
        left_distance = anchor - child->rect.origin.x;

      if ((child->rect.origin.x + child->rect.size.width) > anchor)
        right_distance = (child->rect.origin.x + child->rect.size.width) - anchor;
      else
        right_distance = anchor - (child->rect.origin.x + child->rect.size.width);

      if (self->align_start)
        distance_from_center = left_distance;
      else
        distance_from_center = MIN (left_distance, right_distance);
      if (distance_from_center < min_distance)
        {
          new_selected = i;
          min_distance = distance_from_center;
        }
    }

  if (new_selected == selected)
    {
      /* Ensure dragging is not too stiff; meaning if we drag the content at
         least 15 pixels in either direction, it will automatically snap to the
         next widget */
      if (offset_x > 15 && selected > 0)
        new_selected--;
      else if (offset_x > 15 && should_wrap (self))
        new_selected = last;
      else if (offset_x < -15 && selected < last)
        new_selected++;
      else if (offset_x < -15 && should_wrap (self))
        new_selected = 0;
    }

  if (new_selected == G_MAXUINT ||
      new_selected == gtk_single_selection_get_selected (self->model))
    ensure_viewport (self, self->model, TRUE);
  else
    gtk_single_selection_set_selected (self->model, new_selected);
}

static guint
get_last_index (BgeCarousel *self)
{
  if (self->widgets->len == 0)
    return 0;
  if (self->align_start)
    return MIN (self->last_stop, self->widgets->len - 1);
  return self->widgets->len - 1;
}

static gboolean
should_wrap (BgeCarousel *self)
{
  return self->wrap &&
         !self->align_start &&
         self->widgets->len > 1;
}

static gboolean
is_rtl (BgeCarousel *self)
{
  return gtk_widget_get_direction (GTK_WIDGET (self)) == GTK_TEXT_DIR_RTL;
}

/* End of bge-carousel.c */