Skip to content

Commit

Permalink
Fixed list layouts locking up when dragging widget out
Browse files Browse the repository at this point in the history
  • Loading branch information
Gold872 committed Nov 6, 2023
1 parent 8249245 commit cb1e09a
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 28 deletions.
52 changes: 42 additions & 10 deletions lib/widgets/dashboard_grid.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class DashboardGrid extends StatelessWidget {
onUpdate: _nt4ContainerOnUpdate,
onDragBegin: _nt4ContainerOnDragBegin,
onDragEnd: _nt4ContainerOnDragEnd,
onDragCancel: _nt4ContainerOnDragCancel,
onResizeBegin: _nt4ContainerOnResizeBegin,
onResizeEnd: _nt4ContainerOnResizeEnd,
),
Expand Down Expand Up @@ -84,13 +85,15 @@ class DashboardGrid extends StatelessWidget {
onUpdate: _nt4ContainerOnUpdate,
onDragBegin: _nt4ContainerOnDragBegin,
onDragEnd: _nt4ContainerOnDragEnd,
onDragCancel: _nt4ContainerOnDragCancel,
onResizeBegin: _nt4ContainerOnResizeBegin,
onResizeEnd: _nt4ContainerOnResizeEnd,
);
},
onUpdate: _layoutContainerOnUpdate,
onDragBegin: _layoutContainerOnDragBegin,
onDragEnd: _layoutContainerOnDragEnd,
onDragCancel: _layoutContainerOnDragCancel,
onResizeBegin: _layoutContainerOnResizeBegin,
onResizeEnd: _layoutContainerOnResizeEnd,
);
Expand Down Expand Up @@ -219,6 +222,25 @@ class DashboardGrid extends StatelessWidget {
widget.dispose();
}

void onWidgetDragCancel(DraggableWidgetContainer widget) {
if (!widget.dragging && !widget.resizing) {
return;
}

widget.draggablePositionRect = widget.dragStartLocation;
widget.displayRect = widget.draggablePositionRect;
widget.previewRect = widget.draggablePositionRect;

widget.previewVisible = false;
widget.validLocation = true;

widget.dragging = false;
widget.resizing = false;
widget.draggingIntoLayout = false;

widget.dispose();
}

void onWidgetUpdate(DraggableWidgetContainer widget, Rect newRect) {
double newX = DraggableWidgetContainer.snapToGrid(newRect.left);
double newY = DraggableWidgetContainer.snapToGrid(newRect.top);
Expand Down Expand Up @@ -287,6 +309,12 @@ class DashboardGrid extends StatelessWidget {
refresh();
}

void _nt4ContainerOnDragCancel(dynamic widget) {
onWidgetDragCancel(widget);

refresh();
}

void _nt4ContainerOnResizeBegin(dynamic widget) {
refresh();
}
Expand Down Expand Up @@ -314,6 +342,12 @@ class DashboardGrid extends StatelessWidget {
refresh();
}

void _layoutContainerOnDragCancel(dynamic widget) {
onWidgetDragCancel(widget);

refresh();
}

void _layoutContainerOnResizeBegin(dynamic widget) {
refresh();
}
Expand Down Expand Up @@ -400,6 +434,7 @@ class DashboardGrid extends StatelessWidget {
widget.displayRect = Rect.fromLTWH(previewX, previewY, width, height);
widget.draggablePositionRect =
Rect.fromLTWH(previewX, previewY, width, height);
widget.dragStartLocation = Rect.fromLTWH(previewX, previewY, width, height);

addWidget(widget);
_containerDraggingIn = null;
Expand Down Expand Up @@ -437,6 +472,7 @@ class DashboardGrid extends StatelessWidget {
double height = widget.displayRect.height;

Rect previewLocation = Rect.fromLTWH(previewX, previewY, width, height);
widget.previewRect = previewLocation;

if (isValidLayoutLocation(widget.cursorGlobalLocation)) {
DraggableLayoutContainer layoutContainer =
Expand All @@ -462,11 +498,7 @@ class DashboardGrid extends StatelessWidget {
widget.draggablePositionRect =
Rect.fromLTWH(previewX, previewY, width, height);

addNT4Widget(
widget,
Rect.fromLTWH(previewLocation.left, previewLocation.top,
previewLocation.width, previewLocation.height),
enabled: nt4Connection.isNT4Connected);
addWidget(widget);
}

_containerDraggingIn = null;
Expand Down Expand Up @@ -500,6 +532,7 @@ class DashboardGrid extends StatelessWidget {
onUpdate: _nt4ContainerOnUpdate,
onDragBegin: _nt4ContainerOnDragBegin,
onDragEnd: _nt4ContainerOnDragEnd,
onDragCancel: _nt4ContainerOnDragCancel,
onResizeBegin: _nt4ContainerOnResizeBegin,
onResizeEnd: _nt4ContainerOnResizeEnd,
child: widget.child as NT4Widget,
Expand All @@ -521,6 +554,7 @@ class DashboardGrid extends StatelessWidget {
onUpdate: _layoutContainerOnUpdate,
onDragBegin: _layoutContainerOnDragBegin,
onDragEnd: _layoutContainerOnDragEnd,
onDragCancel: _layoutContainerOnDragCancel,
onResizeBegin: _layoutContainerOnResizeBegin,
onResizeEnd: _layoutContainerOnResizeEnd,
);
Expand All @@ -530,11 +564,6 @@ class DashboardGrid extends StatelessWidget {
_widgetContainers.add(widget);
}

void addNT4Widget(DraggableNT4WidgetContainer widget, Rect initialPosition,
{bool enabled = true}) {
_widgetContainers.add(widget);
}

void addWidgetFromTabJson(Map<String, dynamic> widgetData) {
// If the widget is already in the tab, don't add it
if (!widgetData['layout']) {
Expand Down Expand Up @@ -587,6 +616,7 @@ class DashboardGrid extends StatelessWidget {
onUpdate: _nt4ContainerOnUpdate,
onDragBegin: _nt4ContainerOnDragBegin,
onDragEnd: _nt4ContainerOnDragEnd,
onDragCancel: _nt4ContainerOnDragCancel,
onResizeBegin: _nt4ContainerOnResizeBegin,
onResizeEnd: _nt4ContainerOnResizeEnd,
);
Expand All @@ -595,6 +625,7 @@ class DashboardGrid extends StatelessWidget {
onUpdate: _layoutContainerOnUpdate,
onDragBegin: _layoutContainerOnDragBegin,
onDragEnd: _layoutContainerOnDragEnd,
onDragCancel: _layoutContainerOnDragCancel,
onResizeBegin: _layoutContainerOnResizeBegin,
onResizeEnd: _layoutContainerOnResizeEnd,
),
Expand All @@ -610,6 +641,7 @@ class DashboardGrid extends StatelessWidget {
onUpdate: _nt4ContainerOnUpdate,
onDragBegin: _nt4ContainerOnDragBegin,
onDragEnd: _nt4ContainerOnDragEnd,
onDragCancel: _nt4ContainerOnDragCancel,
onResizeBegin: _nt4ContainerOnResizeBegin,
onResizeEnd: _nt4ContainerOnResizeEnd,
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ abstract class DraggableLayoutContainer extends DraggableWidgetContainer {
super.onUpdate,
super.onDragBegin,
super.onDragEnd,
super.onDragCancel,
super.onResizeBegin,
super.onResizeEnd,
}) : super();
Expand All @@ -31,6 +32,7 @@ abstract class DraggableLayoutContainer extends DraggableWidgetContainer {
super.onUpdate,
super.onDragBegin,
super.onDragEnd,
super.onDragCancel,
super.onResizeBegin,
super.onResizeEnd,
}) : super.fromJson();
Expand Down
32 changes: 21 additions & 11 deletions lib/widgets/draggable_containers/draggable_list_layout.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:elastic_dashboard/widgets/dialog_widgets/dialog_text_input.dart'
import 'package:elastic_dashboard/widgets/draggable_containers/draggable_layout_container.dart';
import 'package:elastic_dashboard/widgets/draggable_containers/draggable_nt4_widget_container.dart';
import 'package:elastic_dashboard/widgets/draggable_containers/draggable_widget_container.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';

class DraggableListLayout extends DraggableLayoutContainer {
Expand All @@ -23,6 +24,7 @@ class DraggableListLayout extends DraggableLayoutContainer {
super.onUpdate,
super.onDragBegin,
super.onDragEnd,
super.onDragCancel,
super.onResizeBegin,
super.onResizeEnd,
}) : super();
Expand All @@ -36,6 +38,7 @@ class DraggableListLayout extends DraggableLayoutContainer {
super.onUpdate,
super.onDragBegin,
super.onDragEnd,
super.onDragCancel,
super.onResizeBegin,
super.onResizeEnd,
}) : super.fromJson();
Expand Down Expand Up @@ -259,15 +262,15 @@ class DraggableListLayout extends DraggableLayoutContainer {
.whereNot((element) => element == PointerDeviceKind.trackpad)
.toSet(),
onPanDown: (details) {
if (dragging || resizing) {
dragging = false;
resizing = false;
draggablePositionRect = dragStartLocation;
previewRect = draggablePositionRect;
displayRect = draggablePositionRect;
refresh();
}
Future.delayed(Duration.zero, () => model?.setDraggable(false));
Future(() {
onDragCancel?.call(this);
if (dragging || resizing) {
onDragCancel?.call(this);
controller?.setRect(draggablePositionRect);
}

model?.setDraggable(false);
});

widget.cursorGlobalLocation = details.globalPosition;
},
Expand All @@ -280,7 +283,7 @@ class DraggableListLayout extends DraggableLayoutContainer {
dashboardGrid.layoutDragOutUpdate(widget, location);
},
onPanEnd: (details) {
Future.delayed(Duration.zero, () => model?.setDraggable(true));
Future(() => model?.setDraggable(true));

Rect previewLocation = Rect.fromLTWH(
DraggableWidgetContainer.snapToGrid(
Expand All @@ -300,7 +303,14 @@ class DraggableListLayout extends DraggableLayoutContainer {
dashboardGrid.layoutDragOutEnd(widget);
},
onPanCancel: () {
Future.delayed(Duration.zero, () => model?.setDraggable(true));
Future(() {
if (dragging || resizing) {
onDragCancel?.call(this);
controller?.setRect(draggablePositionRect);
}

model?.setDraggable(true);
});
},
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 4.0, vertical: 5.5),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class DraggableNT4WidgetContainer extends DraggableWidgetContainer {
super.onUpdate,
super.onDragBegin,
super.onDragEnd,
super.onDragCancel,
super.onResizeBegin,
super.onResizeEnd,
}) : super();
Expand All @@ -58,6 +59,7 @@ class DraggableNT4WidgetContainer extends DraggableWidgetContainer {
super.onUpdate,
super.onDragBegin,
super.onDragEnd,
super.onDragCancel,
super.onResizeBegin,
super.onResizeEnd,
}) : super.fromJson();
Expand Down
Loading

0 comments on commit cb1e09a

Please sign in to comment.