Window Framework v1.2.4
Window Framework for Unity UI Toolkit
Loading...
Searching...
No Matches
GWG.WindowFramework.Manipulators.WindowFrameResizer Class Reference

A mouse manipulator that provides resizing functionality for WindowFrame elements in Unity's UI Toolkit. More...

Inheritance diagram for GWG.WindowFramework.Manipulators.WindowFrameResizer:

Public Member Functions

 WindowFrameResizer ()
 Initializes a new instance of the WindowFrameResizer with left-click activation.

Protected Member Functions

void OnMouseMove (MouseMoveEvent e)
 Handles mouse move events to update window size during resize operations.
void OnMouseUp (MouseUpEvent e)
 Handles mouse up events to complete window resizing operations.
override void RegisterCallbacksOnTarget ()
 Registers mouse event callbacks on the target element.
override void UnregisterCallbacksFromTarget ()
 Unregisters mouse event callbacks from the target element.

Private Member Functions

Vector2 GetClampedWindowSize (float rootX, float rootY, VisualElement targetWindow)
 Calculates and enforces size constraints for window resizing operations.
void OnMouseDown (MouseDownEvent e)
 Handles mouse down events to initiate window resizing operations.

Private Attributes

bool _active
 Indicates whether the resize operation is currently active.
float _clickXOffset
 The horizontal offset between the mouse click position and the window's right edge.
float _clickYOffset
 The vertical offset between the mouse click position and the window's bottom edge.

Static Private Attributes

const float MinHeight = 15f
 The absolute minimum height that any window can be resized to, in pixels.
const float MinWidth = 15f
 The absolute minimum width that any window can be resized to, in pixels.

Detailed Description

This manipulator enables users to resize windows by dragging from a resize handle or corner. It enforces minimum and maximum size constraints while keeping windows within screen boundaries. The resizer integrates with the WindowFrame system to respect resize permissions and size limits.

Key Features:

  • Left-click drag resizing from any attached element
  • Enforces minimum and maximum size constraints
  • Keeps windows within screen boundaries
  • Respects WindowFrame.AllowResize property
  • Integrates with tooltip system (closes tooltips during resize)
  • Maintains proper event handling and mouse capture
// Add resizer to a window's corner handle
var resizeHandle = new VisualElement();
resizeHandle.AddToClassList("resize-handle");
resizeHandle.AddManipulator(new WindowFrameResizer());
windowFrame.Add(resizeHandle);
// Add resizer to window border for edge resizing
var rightBorder = windowFrame.Q("right-border");
rightBorder.AddManipulator(new WindowFrameResizer());
// CSS styling for resize handles
.resize-handle {
width: 16px;
height: 16px;
position: absolute;
bottom: 0;
right: 0;
cursor: nw-resize;
}
WindowFrameResizer()
Initializes a new instance of the WindowFrameResizer with left-click activation.
Definition WindowFrameResizer.cs:121

Constructor & Destructor Documentation

◆ WindowFrameResizer()

GWG.WindowFramework.Manipulators.WindowFrameResizer.WindowFrameResizer ( )

The constructor configures the manipulator to activate only on left mouse button clicks. This prevents conflicts with other mouse operations like context menus (right-click) or middle-click actions.

The manipulator will automatically register its event callbacks when added to a target element.

// Create and attach resizer to an element
var resizer = new WindowFrameResizer();
myResizeHandle.AddManipulator(resizer);
// The resizer is now ready to handle left-click drag operations

Member Function Documentation

◆ GetClampedWindowSize()

Vector2 GWG.WindowFramework.Manipulators.WindowFrameResizer.GetClampedWindowSize ( float rootX,
float rootY,
VisualElement targetWindow )
private
Parameters
rootXThe requested X position of the mouse cursor
rootYThe requested Y position of the mouse cursor
targetWindowThe VisualElement being resized (typically a WindowFrame)
Returns
A Vector2 containing the constrained width (x) and height (y) values

This method implements comprehensive size constraint logic:

Size Calculation:

  • Converts mouse position to window dimensions using click offsets
  • Accounts for window's current position in the calculation

Constraint Enforcement (in order of priority):

  1. Absolute minimums (MinWidth/MinHeight constants)
  2. Element style minimums (minWidth/minHeight styles)
  3. Element style maximums (maxWidth/maxHeight styles, defaulting to screen size)
  4. Screen boundary limits (prevents windows from exceeding screen area)

The method ensures windows remain usable and visible while respecting both framework constraints and user-defined size limits.

// GetClampedWindowSize is used internally during resize operations
// Example constraint scenarios:
// Window with custom constraints:
myWindow.style.minWidth = 200; // Minimum width: 200px (overrides 15px default)
myWindow.style.maxWidth = 800; // Maximum width: 800px
myWindow.style.minHeight = 150; // Minimum height: 150px (overrides 15px default)
// maxHeight defaults to screen height if not set
// Constraint priority example:
// If user drags to create 10x10 window:
// 1. Applied MinWidth (15) → width becomes 15
// 2. Applied MinHeight (15) → height becomes 15
// 3. Applied minWidth style (200) → width becomes 200
// 4. Applied minHeight style (150) → height becomes 150
// Final size: 200x150

◆ OnMouseDown()

void GWG.WindowFramework.Manipulators.WindowFrameResizer.OnMouseDown ( MouseDownEvent e)
private
Parameters
eThe mouse down event data containing click position and button information

This method performs the following operations when a valid resize operation begins:

  1. Validates that the manipulation can start (correct mouse button, etc.)
  2. Closes any active tooltips to prevent UI interference
  3. Locates the parent WindowFrame and checks if resizing is allowed
  4. Brings the target window to the front for better user experience
  5. Calculates click offsets for smooth resize behavior
  6. Activates the resize state and captures mouse input

The method will exit early if the target WindowFrame has AllowResize set to false. Click offsets are calculated to maintain consistent resize behavior regardless of exactly where on the resize handle the user clicks.

// OnMouseDown is triggered automatically when user clicks
// the element that has this manipulator attached
// Example of what happens internally:
// 1. User clicks resize handle
// 2. OnMouseDown validates the operation
// 3. Window comes to front
// 4. Offsets calculated:
// _clickXOffset = mouseX - (window.left + window.width)
// _clickYOffset = mouseY - (window.top + window.height)
// 5. Mouse capture begins

◆ OnMouseMove()

void GWG.WindowFramework.Manipulators.WindowFrameResizer.OnMouseMove ( MouseMoveEvent e)
protected
Parameters
eThe mouse move event data containing current mouse position

This method continuously updates the window size while a resize operation is active:

  1. Validates that the resize operation is active and mouse is captured
  2. Locates the target WindowFrame element
  3. Calculates the new window size based on mouse position and click offsets
  4. Applies size constraints through GetClampedWindowSize()
  5. Updates the window's width and height styles
  6. Stops event propagation to prevent interference with other elements

The size calculation uses the stored click offsets to provide smooth, predictable resizing behavior. All size constraints and screen boundaries are enforced through the clamping function.

// OnMouseMove is called continuously while dragging
// Example of the resize calculation:
// If user drags to position (500, 400):
// newWidth = 500 - window.left - _clickXOffset
// newHeight = 400 - window.top - _clickYOffset
// Then constraints are applied:
// - Minimum sizes (15px + element minWidth/minHeight)
// - Maximum sizes (element maxWidth/maxHeight or screen size)
// - Screen boundary limits

◆ OnMouseUp()

void GWG.WindowFramework.Manipulators.WindowFrameResizer.OnMouseUp ( MouseUpEvent e)
protected
Parameters
eThe mouse up event data

This method finalizes the resize operation when the user releases the mouse button:

  1. Validates that a resize operation is active and can be stopped
  2. Deactivates the resize state
  3. Releases mouse capture to restore normal input handling
  4. Stops event propagation to maintain clean event flow

After this method completes, the window retains its new size and normal mouse interaction is restored. The resize operation is fully complete.

// OnMouseUp is triggered when user releases mouse button
// Example of what happens:
// 1. User releases left mouse button
// 2. OnMouseUp validates the operation can be stopped
// 3. _active = false (resize operation ends)
// 4. Mouse capture released
// 5. Window keeps its new size
// 6. Normal mouse interaction restored

◆ RegisterCallbacksOnTarget()

override void GWG.WindowFramework.Manipulators.WindowFrameResizer.RegisterCallbacksOnTarget ( )
protected

This method is called automatically by the UI Toolkit when the manipulator is added to a target element. It registers callbacks for:

  • MouseDownEvent: Initiates resize operation
  • MouseMoveEvent: Updates window size during drag
  • MouseUpEvent: Completes resize operation

The callbacks are registered in the capture phase to ensure proper event handling even if other elements might intercept the events.

// RegisterCallbacksOnTarget is called automatically
// when you do:
myElement.AddManipulator(new WindowFrameResizer());
// Manual registration (not typically needed):
// manipulator.RegisterCallbacksOnTarget() is called internally

◆ UnregisterCallbacksFromTarget()

override void GWG.WindowFramework.Manipulators.WindowFrameResizer.UnregisterCallbacksFromTarget ( )
protected

This method is called automatically by the UI Toolkit when the manipulator is removed from a target element or when the element is destroyed.

Proper unregistration prevents memory leaks and ensures clean disposal of event handlers when the manipulator is no longer needed.

// UnregisterCallbacksFromTarget is called automatically
// when you do:
myElement.RemoveManipulator(existingResizer);
// Or when the element is removed from the hierarchy
myElement.RemoveFromHierarchy(); // Triggers cleanup

Member Data Documentation

◆ _active

bool GWG.WindowFramework.Manipulators.WindowFrameResizer._active
private

Set to true when a resize drag begins and false when it ends. Used to track the state of the resize operation and control event handling.

◆ _clickXOffset

float GWG.WindowFramework.Manipulators.WindowFrameResizer._clickXOffset
private

Calculated during OnMouseDown to maintain consistent resize behavior. This offset ensures that the resize operation feels natural and responsive regardless of where exactly on the resize handle the user clicks.

◆ _clickYOffset

float GWG.WindowFramework.Manipulators.WindowFrameResizer._clickYOffset
private

Calculated during OnMouseDown to maintain consistent resize behavior. This offset ensures that the resize operation feels natural and responsive regardless of where exactly on the resize handle the user clicks.

◆ MinHeight

const float GWG.WindowFramework.Manipulators.WindowFrameResizer.MinHeight = 15f
staticprivate

This constant ensures windows remain usable and visible even at minimum size. The value is enforced in addition to any minHeight style property set on the window. A 15-pixel minimum ensures basic window chrome remains visible.

◆ MinWidth

const float GWG.WindowFramework.Manipulators.WindowFrameResizer.MinWidth = 15f
staticprivate

This constant ensures windows remain usable and visible even at minimum size. The value is enforced in addition to any minWidth style property set on the window. A 15-pixel minimum ensures basic window chrome remains visible.