Fix #34 Implement resizers

This commit is contained in:
Grim Maple 2023-05-26 20:53:08 +03:00
parent 098a604856
commit 0fbb3795f0
1 changed files with 27 additions and 12 deletions

View File

@ -313,11 +313,18 @@ class ResizerWidget : Widget {
protected string _styleHorizontal; protected string _styleHorizontal;
Signal!ResizeHandler resizeEvent; Signal!ResizeHandler resizeEvent;
/// Controls the minimum width of the previous item
size_t minPreviousItemWidth = 100;
/// Controls the minimum width of the next item
size_t minNextItemWidth = 100;
/// Orientation: Vertical to resize vertically, Horizontal - to resize horizontally /// Orientation: Vertical to resize vertically, Horizontal - to resize horizontally
@property Orientation orientation() { return _orientation; } @property Orientation orientation() { return _orientation; }
/// empty parameter list constructor - for usage by factory /// empty parameter list constructor - for usage by factory
this() { this() {
this(null); this(null);
minWidth = 7;
} }
/// create with ID parameter /// create with ID parameter
this(string ID, Orientation orient = Orientation.Vertical) { this(string ID, Orientation orient = Orientation.Vertical) {
@ -399,6 +406,7 @@ class ResizerWidget : Widget {
/// process mouse event; return true if event is processed by widget. /// process mouse event; return true if event is processed by widget.
override bool onMouseEvent(MouseEvent event) { override bool onMouseEvent(MouseEvent event) {
// support onClick // support onClick
immutable newWidth = _orientation == Orientation.Vertical ? event.y : event.x;
if (event.action == MouseAction.ButtonDown && event.button == MouseButton.Left) { if (event.action == MouseAction.ButtonDown && event.button == MouseButton.Left) {
setState(State.Pressed); setState(State.Pressed);
_dragging = true; _dragging = true;
@ -424,8 +432,8 @@ class ResizerWidget : Widget {
_delta = _minDragDelta; _delta = _minDragDelta;
if (_delta > _maxDragDelta) if (_delta > _maxDragDelta)
_delta = _maxDragDelta; _delta = _maxDragDelta;
} else if (resizeEvent.assigned) { } else {
resizeEvent(this, ResizerEventType.StartDragging, _orientation == Orientation.Vertical ? event.y : event.x); resizeAndFireEvent(newWidth, ResizerEventType.StartDragging);
} }
return true; return true;
} }
@ -437,18 +445,13 @@ class ResizerWidget : Widget {
if (_dragging) { if (_dragging) {
//sendScrollEvent(ScrollAction.SliderReleased, _position); //sendScrollEvent(ScrollAction.SliderReleased, _position);
_dragging = false; _dragging = false;
if (resizeEvent.assigned) { resizeAndFireEvent(newWidth, ResizerEventType.EndDragging);
resizeEvent(this, ResizerEventType.EndDragging, _orientation == Orientation.Vertical ? event.y : event.x);
}
} }
return true; return true;
} }
if (event.action == MouseAction.Move && _dragging) { if (event.action == MouseAction.Move && _dragging) {
int delta = _orientation == Orientation.Vertical ? event.y - _dragStart.y : event.x - _dragStart.x; int delta = _orientation == Orientation.Vertical ? event.y - _dragStart.y : event.x - _dragStart.x;
if (resizeEvent.assigned) { resizeAndFireEvent(newWidth, ResizerEventType.Dragging);
resizeEvent(this, ResizerEventType.Dragging, _orientation == Orientation.Vertical ? event.y : event.x);
return true;
}
_delta = _dragStartPosition + delta; _delta = _dragStartPosition + delta;
if (_delta < _minDragDelta) if (_delta < _minDragDelta)
_delta = _minDragDelta; _delta = _minDragDelta;
@ -506,14 +509,26 @@ class ResizerWidget : Widget {
if (_dragging) { if (_dragging) {
resetState(State.Pressed); resetState(State.Pressed);
_dragging = false; _dragging = false;
if (resizeEvent.assigned) { resizeAndFireEvent(newWidth, ResizerEventType.EndDragging);
resizeEvent(this, ResizerEventType.EndDragging, _orientation == Orientation.Vertical ? event.y : event.x);
}
} }
return true; return true;
} }
return false; return false;
} }
private void resizeAndFireEvent(short newWidth, ResizerEventType type)
{
// Respect the dimensions
if(newWidth > minPreviousItemWidth && newWidth < (parent.width - minWidth - minNextItemWidth) &&
newWidth > _previousWidget.minWidth() && newWidth < (parent.width - minWidth - _nextWidget.minWidth()))
{
_previousWidget.layoutWidth = newWidth;
if (resizeEvent.assigned)
{
resizeEvent(this, type, newWidth);
}
}
}
} }