Skip to content

Sizing Behaviors

morphui.uix.behaviors.sizing

Comprehensive sizing behaviors for Kivy widgets.

This module provides all size-related functionality through three main behaviors: - Size bounds/constraints (minimum and maximum size limits) - Interactive resizing (drag to resize with mouse) - Auto-sizing (content-driven automatic sizing)

The sizing behaviors support: - Lower and upper bound size constraints - Edge and corner resizing with visual feedback - Automatic sizing based on content and children - Cursor changes and hover effects for resize - Aspect ratio preservation - Event system for resize and auto-size operations

MorphSizeBoundsBehavior(**kwargs)

Bases: EventDispatcher

A behavior that provides size constraint functionality.

This behavior adds lower and upper bound properties for constraining widget dimensions. It automatically resolves these bounds considering the widget's inherent minimum and maximum dimensions, providing a clean interface for size constraint enforcement.

Features
  • Lower Bounds: Minimum size constraints with fallback to inherent minimums
  • Upper Bounds: Maximum size constraints with infinity fallback
  • Automatic Resolution: Computed properties handle constraint logic
  • Flexible Configuration: Disable constraints with negative values
Properties
  • :attr:size_lower_bound: Minimum width and height constraints
  • :attr:size_upper_bound: Maximum width and height constraints
  • :attr:_resolved_size_lower_bound: Computed lower bounds (read-only)
  • :attr:_resolved_size_upper_bound: Computed upper bounds (read-only)

Examples:

Basic size-constrained widget:

from kivy.uix.widget import Widget
from morphui.uix.behaviors.sizing import MorphSizeBoundsBehavior

class ConstrainedWidget(MorphSizeBoundsBehavior, Widget):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # Set minimum size to 100x50, maximum size to 500x300
        self.size_lower_bound = [100, 50]
        self.size_upper_bound = [500, 300]

    def constrain_size(self, new_size):
        from morphui.utils import clamp
        width = clamp(new_size[0], 
                     self._resolved_size_lower_bound[0],
                     self._resolved_size_upper_bound[0])
        height = clamp(new_size[1],
                      self._resolved_size_lower_bound[1], 
                      self._resolved_size_upper_bound[1])
        return (width, height)

Using with negative values to disable constraints:

# Only constrain width, not height
widget.size_lower_bound = [100, -1]  # Min width 100, no min height
widget.size_upper_bound = [500, -1]  # Max width 500, no max height

size_lower_bound = VariableListProperty([-1, -1], length=2) class-attribute instance-attribute

Lower bound size constraints [width, height].

Prevents the widget from being resized smaller than these dimensions. Useful for maintaining usability and preventing widgets from becoming too small to interact with. Use [0, 0] to disable minimum size constraints. If a negative value is specified for a dimension, the widget's inherent minimum size will be used.

:attr:size_lower_bound is a :class:~kivy.properties.VariableListProperty and defaults to [-1, -1].

size_upper_bound = VariableListProperty([-1, -1], length=2) class-attribute instance-attribute

Upper bound size constraints [width, height].

Prevents the widget from being resized larger than these dimensions. Use [-1, -1] to disable maximum size constraints.

:attr:size_upper_bound is a :class:~kivy.properties.VariableListProperty and defaults to [-1, -1].

constrain_size(size)

Apply size bounds constraints to a given size.

This convenience method applies both lower and upper bound constraints to a size tuple, returning the constrained dimensions.

PARAMETER DESCRIPTION
size

The size to constrain (width, height)

TYPE: Tuple[float, float]

RETURNS DESCRIPTION
Tuple[float, float]

The constrained size (width, height)

Examples:

# Constrain a proposed size
new_size = (80, 600)  # Too small width, too large height
constrained = self.constrain_size(new_size)
# Returns (100, 300) if bounds are [100, 50] to [500, 300]

MorphAutoSizingBehavior(**kwargs)

Bases: EventDispatcher

Behavior for automatic widget sizing based on content.

This behavior provides three boolean properties that enable automatic sizing of widgets based on their content and children. It can automatically adjust width, height, or both dimensions to fit the minimum required size.

Features
  • Auto Width: Automatically adjust width to fit content
  • Auto Height: Automatically adjust height to fit content
  • Combined Auto Size: Automatically adjust both dimensions
  • Text Integration: Special handling for text-based widgets
  • Content Adaptation: Adapts to minimum/maximum size constraints
Properties
  • :attr:auto_width: Enable automatic width adjustment
  • :attr:auto_height: Enable automatic height adjustment
  • :attr:auto_size: Enable both width and height adjustment

Examples:

Auto-sizing label:

from kivy.uix.label import Label
from morphui.uix.behaviors.sizing import MorphAutoSizingBehavior

class AutoLabel(MorphAutoSizingBehavior, Label):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.auto_size = True  # Adjust both width and height

Auto-width button:

from kivy.uix.button import Button
from morphui.uix.behaviors.sizing import MorphAutoSizingBehavior

class AutoButton(MorphAutoSizingBehavior, Button):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.auto_width = True  # Only adjust width

auto_width = BooleanProperty(False) class-attribute instance-attribute

Automatically adjust widget width to minimum required size.

When True, the widget's width will be automatically calculated and set to the minimum size required to accommodate all its content and children. This is useful for creating widgets that adapt their width based on their packed content, such as buttons that resize based on text length or containers that fit their child widgets.

:attr:auto_width is a :class:~kivy.properties.BooleanProperty and defaults to False.

auto_height = BooleanProperty(False) class-attribute instance-attribute

Automatically adjust widget height to minimum required size.

When True, the widget's height will be automatically calculated and set to the minimum size required to accommodate all its content and children. This is useful for creating widgets that adapt their height based on their packed content, such as labels that resize based on text height or containers that fit their child widgets vertically.

:attr:auto_height is a :class:~kivy.properties.BooleanProperty and defaults to False.

auto_size = AliasProperty(_get_auto_size, _set_auto_size, bind=('auto_width', 'auto_height')) class-attribute instance-attribute

Automatically adjust both width and height to minimum required size.

When True, both the widget's width and height will be automatically calculated and set to the minimum size required to accommodate all its content and children. This is useful for creating widgets that fully adapt their size based on their packed content.

Note that setting :attr:auto_size to True will set both :attr:auto_width and :attr:auto_height to True. Conversely, setting :attr:auto_size to False will set both :attr:auto_width and :attr:auto_height to False. You can also set :attr:auto_size to a tuple of two booleans to independently control width and height.

:attr:auto_size is a :class:~kivy.properties.AliasProperty and defaults to (False, False).

has_text_layout property

Check if the widget uses Kivy's text layout system.

Returns True if the widget has both texture_size and text_size attributes, which is the case for text-based widgets like Label. Used to decide whether to measure and constrain text dimensions during auto sizing.

apply_auto_sizing(auto_width, auto_height)

Enforce auto sizing based on provided flags. This will not change the property values, but will apply the sizing adjustments as if the properties were set.

This method is responsible for applying the appropriate sizing adjustments to the widget based on the provided flags. It stores the original size and size_hint before making any changes, allowing for restoration when auto sizing is disabled. It uses :attr:texture_size if available, otherwise falls back to :attr:minimum_width and :attr:minimum_height.

PARAMETER DESCRIPTION
auto_width

Whether to apply auto width sizing.

TYPE: bool

auto_height

Whether to apply auto height sizing.

TYPE: bool

Notes

For text-based widgets, :attr:text_size is temporarily set to (None, None) to allow the texture to update to its natural (unconstrained) size before applying the new dimensions. This prevents a previously set text_size from wrapping longer text before we have a chance to measure the natural width.

refresh_auto_sizing()

Re-apply the current auto sizing settings.

This method can be called to refresh the auto sizing behavior, for example after dynamic changes to the widget that may affect sizing. It re-applies the sizing adjustments based on the current values of :attr:auto_width and :attr:auto_height.

This method preserves the original size and size_hint before re-applying the sizing adjustments, ensuring that the widget can return to its original size if needed.

on_auto_size_updated(*args)

Event fired after auto sizing has been applied or refreshed.

This event can be used to perform additional actions after the widget's size has been adjusted based on its content. It is triggered at the end of the :meth:apply_auto_sizing method.

MorphResizeBehavior(**kwargs)

Bases: MorphSizeBoundsBehavior, MorphHoverEnhancedBehavior, MorphOverlayLayerBehavior

A behavior that enables widgets to be resized by dragging edges and corners.

This behavior combines size bounds, enhanced hover detection, and overlay layer functionality to provide interactive resizing capabilities. It automatically detects when the mouse is over resizable edges or corners and provides visual feedback through edge highlighting and cursor changes.

Features
  • Edge Resizing: Resize by dragging left, right, top, or bottom edges
  • Corner Resizing: Resize diagonally by dragging corners
  • Visual Feedback: Highlighted edges and appropriate cursor changes
  • Size Constraints: Minimum and maximum size limits via inheritance
  • Aspect Ratio: Optional aspect ratio preservation
  • Collapse/Expand: Optional snap-shut collapsing below a percentage of the lower bound, reopening past another threshold, animated and with fading content
  • Animation: Smooth transitions for visual feedback
  • Events: Comprehensive event system for resize operations
Events
  • :meth:on_resize_start: Fired when resize operation begins
  • :meth:on_resize_progress: Fired during resize (real-time updates)
  • :meth:on_resize_end: Fired when resize operation completes
  • :meth:on_collapse: Fired when a dimension snaps entirely shut
  • :meth:on_expand: Fired when a collapsed dimension pops back open
Properties
  • :attr:resize_enabled: Enable/disable resize functionality
  • :attr:resizable_edges: List of edges that can be resized
  • :attr:size_lower_bound: Minimum width and height constraints (inherited)
  • :attr:size_upper_bound: Maximum width and height constraints (inherited)
  • :attr:preserve_aspect_ratio: Whether to maintain aspect ratio
  • :attr:collapsible: Enable collapse-on-resize behavior
  • :attr:collapse_threshold: Fraction of lower bound that triggers collapse
  • :attr:expand_threshold: Fraction of lower bound that triggers re-expand
  • :attr:collapsed_size: Size to snap to while collapsed
  • :attr:collapsed: Whether the widget is currently collapsed
  • :attr:collapse_animation_duration: Duration of the collapse/expand transition
  • :attr:collapse_animation_transition: Easing used for the collapse/expand transition
  • :attr:content_collapse_opacity: Opacity direct children fade to while collapsed
METHOD DESCRIPTION
- :meth:`collapse`: Programmatically collapse an axis
- :meth:`expand`: Programmatically expand an axis back to its pre-collapse size
- :meth:`toggle_collapse`: Collapse or expand an axis depending on its current state

Examples:

Basic resizable widget:

from kivy.uix.widget import Widget
from morphui.uix.behaviors.sizing import MorphResizeBehavior

class ResizableWidget(MorphResizeBehavior, Widget):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.size = (200, 150)
        self.pos = (100, 100)
        # Set size constraints
        self.size_lower_bound = [50, 50]
        self.size_upper_bound = [400, 300]

    def on_resize_start(self, edge_or_corner):
        print(f"Starting resize from {edge_or_corner}")

    def on_resize_end(self, edge_or_corner):
        print(f"Finished resizing")

Resizable widget with aspect ratio preservation:

class AspectResizableWidget(MorphResizeBehavior, Widget):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.preserve_aspect_ratio = True
        self.size_lower_bound = [100, 75]  # 4:3 ratio minimum

Collapsible sidebar that snaps shut when dragged small and pops back open past a reveal threshold:

class CollapsibleSidebar(MorphResizeBehavior, Widget):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.resizable_edges = ['right']
        self.size_lower_bound = [200, -1]
        self.collapsible = True
        self.collapse_threshold = 0.4  # collapse below 40% of 200px
        self.expand_threshold = 0.6    # reopen past 60% of 200px

Toggling collapse/expand from a button instead of dragging, e.g. a hamburger icon that shows/hides the sidebar above:

toggle_button.bind(on_release=lambda *a: sidebar.toggle_collapse())

resize_enabled = BooleanProperty(True) class-attribute instance-attribute

Enable or disable resize functionality.

When set to False, the widget will not respond to resize operations, but hover detection and visual feedback will still work. This is useful for temporarily disabling resize while maintaining visual consistency.

:attr:resize_enabled is a :class:~kivy.properties.BooleanProperty and defaults to True.

resizable_edges = ListProperty(NAME.EDGES) class-attribute instance-attribute

List of edges that can be used for resizing.

Controls which edges of the widget can be dragged to resize. Can contain any combination of 'left', 'right', 'top', 'bottom'. Corner resizing is automatically enabled when two adjacent edges are both resizable.

:attr:resizable_edges is a :class:~kivy.properties.ListProperty and defaults to all edges (left, right, top, bottom).

preserve_aspect_ratio = BooleanProperty(False) class-attribute instance-attribute

Whether to preserve the widget's aspect ratio during resize.

When True, the widget will maintain its width-to-height ratio during resize operations. This is particularly useful for images, video players, or other content where aspect ratio is important.

:attr:preserve_aspect_ratio is a :class:~kivy.properties.BooleanProperty and defaults to False.

resizing = BooleanProperty(False) class-attribute instance-attribute

Indicates whether a resize operation is currently in progress.

This property is True while the user is actively dragging an edge or corner to resize the widget, and False otherwise. It can be used to conditionally change behavior or appearance during resize operations. For example, you might want to disable certain interactions while resizing is happening. The :class:morphui.uix.behaviors.states.MorphStateBehavior listens to this property and so does the :class:morphui.uix.behaviors.layer.MorphOverlayLayerBehavior.

:attr:resizing is a :class:~kivy.properties.BooleanProperty and defaults to False.

collapsible = BooleanProperty(False) class-attribute instance-attribute

Enable collapse-on-resize behavior.

When True, dragging a dimension below :attr:collapse_threshold (a fraction of its resolved lower bound) snaps that dimension entirely shut to :attr:collapsed_size instead of stopping at the lower bound. A collapsed dimension only reopens once dragged back past :attr:expand_threshold, at which point it pops back open to its lower bound size.

:attr:collapsible is a :class:~kivy.properties.BooleanProperty and defaults to False.

collapse_threshold = NumericProperty(0.5) class-attribute instance-attribute

Fraction of the resolved lower bound below which a dimension collapses entirely.

While dragging, once a dimension's size drops to or below _resolved_size_lower_bound * collapse_threshold, that dimension snaps to :attr:collapsed_size. Has no effect unless :attr:collapsible is True.

:attr:collapse_threshold is a :class:~kivy.properties.NumericProperty and defaults to 0.5.

expand_threshold = NumericProperty(0.5) class-attribute instance-attribute

Fraction of the resolved lower bound above which a collapsed dimension expands again.

Once a dimension is collapsed, it remains at :attr:collapsed_size until dragged back past _resolved_size_lower_bound * expand_threshold, at which point it pops back open to its lower bound size. Set this higher than :attr:collapse_threshold to create a dead zone between collapsing and expanding, preventing rapid flicker near the threshold. Has no effect unless :attr:collapsible is True.

:attr:expand_threshold is a :class:~kivy.properties.NumericPropertyand defaults to 0.5.

collapsed_size = VariableListProperty([0, 0], length=2) class-attribute instance-attribute

Size [width, height] a dimension snaps to once collapsed.

:attr:collapsed_size is a :class:~kivy.properties.VariableListProperty and defaults to [0, 0].

collapsed_width = BooleanProperty(False) class-attribute instance-attribute

Whether the width is currently collapsed (read-only).

:attr:collapsed_width is a :class:~kivy.properties.BooleanProperty and defaults to False.

collapsed_height = BooleanProperty(False) class-attribute instance-attribute

Whether the height is currently collapsed (read-only).

:attr:collapsed_height is a :class:~kivy.properties.BooleanProperty and defaults to False.

collapsed = AliasProperty(_get_collapsed, bind=('collapsed_width', 'collapsed_height'), cache=True) class-attribute instance-attribute

Whether the widget is currently collapsed along any dimension (read-only).

:attr:collapsed is a :class:~kivy.properties.AliasProperty and is bound to :attr:collapsed_width and :attr:collapsed_height.

collapse_animation_duration = NumericProperty(0.2) class-attribute instance-attribute

Duration in seconds of the collapse/expand transition.

Whenever a dimension snaps shut or pops back open (see :attr:collapsible), that dimension animates to its target size over this duration instead of jumping there instantly. The same duration is also used to fade the widget's direct children in and out alongside the size transition. Has no effect unless :attr:collapsible is True.

:attr:collapse_animation_duration is a :class:~kivy.properties.NumericProperty and defaults to 0.2.

collapse_animation_transition = StringProperty('out_quad') class-attribute instance-attribute

Transition type for the collapse/expand animation.

This should be a valid Kivy transition name. See :class:~kivy.animation.Animation for available transitions.

:attr:collapse_animation_transition is a :class:~kivy.properties.StringProperty and defaults to 'out_quad'.

content_collapse_opacity = NumericProperty(0.0) class-attribute instance-attribute

Opacity applied to the widget's direct children while collapsed.

While :attr:collapsed is True, each of this widget's direct children fades to this opacity (in sync with the size transition) so that content doesn't appear squeezed or clipped as the widget snaps shut. Children fade back to fully opaque when the widget expands again.

:attr:content_collapse_opacity is a :class:~kivy.properties.NumericProperty and defaults to 0.0.

resize_edge_or_corner property

The edge or corner currently being used for resize operation (read-only).

This property is set when a resize operation starts and cleared when it ends. It indicates which edge ('left', 'right', 'top', 'bottom') or corner ('top-left', 'top-right', 'bottom-left', 'bottom-right') is being dragged to resize the widget. If no resize operation is in progress, this will be None.

hovered_resizable_edges property

List of currently hovered edges that are resizable.

Only edges present in :attr:resizable_edges will be included. If resize is disabled, this will always be an empty list.

hovered_resizable_corner property

Currently hovered corner if it is resizable, else None.

on_touch_down(touch)

Handle touch down events for resize operations.

This method initiates a resize operation if the touch occurs over a resizable edge or corner. It sets up the necessary state for the resize operation to proceed.

RETURNS DESCRIPTION
bool

True if touch was handled for resize

on_touch_move(touch)

Handle touch move events during resize operations.

This method updates the size and position of the widget being resized based on the current mouse position. It applies size constraints and dispatches the appropriate resize event.

PARAMETER DESCRIPTION
touch

Touch event

TYPE: MotionEvent

RETURNS DESCRIPTION
bool

True if touch was handled for resize

collapse(axis='width')

Programmatically collapse axis to :attr:collapsed_size, e.g. from a toggle button instead of a drag gesture.

The axis' current size is recorded first, so a later call to :meth:expand can restore it. Has no effect if :attr:collapsible is False or axis is already collapsed.

PARAMETER DESCRIPTION
axis

Either 'width' or 'height'. Defaults to 'width'.

TYPE: str DEFAULT: 'width'

Examples:

collapse_button.bind(on_release=lambda *a: sidebar.collapse())

expand(axis='width')

Programmatically expand axis back to the size it had before it collapsed, e.g. from a toggle button instead of a drag gesture.

Restores the size recorded by the preceding :meth:collapse call (clamped to the current resolved bounds), or falls back to the resolved lower bound if no prior size was recorded. Has no effect if axis isn't currently collapsed.

PARAMETER DESCRIPTION
axis

Either 'width' or 'height'. Defaults to 'width'.

TYPE: str DEFAULT: 'width'

Examples:

expand_button.bind(on_release=lambda *a: sidebar.expand())

toggle_collapse(axis='width')

Collapse axis if currently expanded, or expand it if currently collapsed. See :meth:collapse and :meth:expand.

PARAMETER DESCRIPTION
axis

Either 'width' or 'height'. Defaults to 'width'.

TYPE: str DEFAULT: 'width'

Examples:

toggle_button.bind(on_release=lambda *a: sidebar.toggle_collapse())

constrain_size(size)

Apply size bounds constraints, skipping collapsed dimensions.

This overrides :meth:MorphSizeBoundsBehavior.constrain_size so that the automatic size clamp bound to the size property does not immediately re-expand a dimension that was just snapped shut by :meth:_collapse_dimension.

PARAMETER DESCRIPTION
size

The size to constrain (width, height)

TYPE: Tuple[float, float]

RETURNS DESCRIPTION
Tuple[float, float]

The constrained size (width, height)

on_touch_up(touch)

Handle touch up events to end resize operations.

This method finalizes the resize operation, resets state, and dispatches the resize end event.

PARAMETER DESCRIPTION
touch

Touch event

TYPE: MotionEvent

RETURNS DESCRIPTION
bool

True if touch was handled for resize

on_resize_start(edge_or_corner)

Event fired when a resize operation starts.

This event is dispatched when the user starts dragging an edge or corner to resize the widget. Override this method to add custom behavior at the start of resize operations.

PARAMETER DESCRIPTION
edge_or_corner

The edge or corner being used for resize ('left', 'right', 'top', 'bottom', or corner names like 'top-left')

TYPE: str

Examples:

def on_resize_start(self, edge_or_corner):
    print(f"Starting resize from {edge_or_corner}")
    self.opacity = 0.8  # Make widget semi-transparent during resize

on_resize_progress(edge_or_corner, new_size, new_pos)

Event fired during resize operations with new dimensions.

This event is dispatched continuously while the user drags to resize the widget. The default implementation applies the new size and position to the widget. Override this method to customize resize behavior or add validation.

PARAMETER DESCRIPTION
edge_or_corner

The edge or corner being used for resize

TYPE: str

new_size

New widget size (width, height)

TYPE: Tuple[float, float]

new_pos

New widget position (x, y)

TYPE: Tuple[float, float]

Examples:

def on_resize_progress(self, edge_or_corner, new_size, new_pos):
    # Custom validation
    if new_size[0] < 100:
        return  # Don't allow width less than 100

    # Apply resize
    super().on_resize_progress(edge_or_corner, new_size, new_pos)

    # Update content
    self.update_content_layout()

on_resize_end(edge_or_corner)

Event fired when a resize operation completes.

This event is dispatched when the user releases the mouse after resizing the widget. Override this method to add custom behavior at the end of resize operations, such as saving the new size or triggering layout updates.

PARAMETER DESCRIPTION
edge_or_corner

The edge or corner that was used for resize

TYPE: str

Examples:

def on_resize_end(self, edge_or_corner):
    print(f"Finished resizing from {edge_or_corner}")
    self.opacity = 1.0  # Restore full opacity
    self.save_size_to_config()  # Save new size

on_collapse(axis)

Event fired when a dimension snaps entirely shut.

This event is dispatched when :attr:collapsible is True and the widget is dragged below :attr:collapse_threshold along the given axis. Override this method to react to the widget becoming collapsed, e.g. hiding its contents.

PARAMETER DESCRIPTION
axis

Either 'width' or 'height'; the dimension that collapsed.

TYPE: str

on_expand(axis)

Event fired when a collapsed dimension pops back open.

This event is dispatched when :attr:collapsible is True and a previously collapsed dimension is dragged back past :attr:expand_threshold. Override this method to react to the widget expanding again, e.g. restoring its contents.

PARAMETER DESCRIPTION
axis

Either 'width' or 'height'; the dimension that expanded.

TYPE: str

on_leave()

Override parent on_leave to reset cursor.