Skip to content

Commit

Permalink
Fix grid size not working (#75)
Browse files Browse the repository at this point in the history
Resolves #73
  • Loading branch information
Gold872 authored Jul 20, 2024
1 parent 1f70675 commit 49a2da3
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class DraggableWidgetContainer extends StatelessWidget {
this.updateFunctions,
});

static double snapToGrid(double value, [double? gridSize]) {
gridSize ??= Defaults.gridSize.toDouble();
static double snapToGrid(double value, [int? gridSize]) {
gridSize ??= Defaults.gridSize;
return (value / gridSize).roundToDouble() * gridSize;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -499,9 +499,13 @@ class ListLayoutModel extends LayoutContainerModel {
}
Future(() => setDraggable(true));

int? gridSize = preferences.getInt(PrefKeys.gridSize);

Rect previewLocation = Rect.fromLTWH(
DraggableWidgetContainer.snapToGrid(widget.draggingRect.left),
DraggableWidgetContainer.snapToGrid(widget.draggingRect.top),
DraggableWidgetContainer.snapToGrid(
widget.draggingRect.left, gridSize),
DraggableWidgetContainer.snapToGrid(
widget.draggingRect.top, gridSize),
widget.displayRect.width,
widget.displayRect.height,
);
Expand Down
22 changes: 11 additions & 11 deletions lib/widgets/draggable_containers/models/widget_container_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,17 +141,17 @@ abstract class WidgetContainerModel extends ChangeNotifier {

@mustCallSuper
void updateGridSize(int oldGridSize, int newGridSize) {
double newX = DraggableWidgetContainer.snapToGrid(
displayRect.left, newGridSize.toDouble());
double newY = DraggableWidgetContainer.snapToGrid(
displayRect.top, newGridSize.toDouble());

double newWidth = DraggableWidgetContainer.snapToGrid(
displayRect.width, newGridSize.toDouble())
.clamp(minWidth, double.infinity);
double newHeight = DraggableWidgetContainer.snapToGrid(
displayRect.height, newGridSize.toDouble())
.clamp(minHeight, double.infinity);
double newX =
DraggableWidgetContainer.snapToGrid(displayRect.left, newGridSize);
double newY =
DraggableWidgetContainer.snapToGrid(displayRect.top, newGridSize);

double newWidth =
DraggableWidgetContainer.snapToGrid(displayRect.width, newGridSize)
.clamp(minWidth, double.infinity);
double newHeight =
DraggableWidgetContainer.snapToGrid(displayRect.height, newGridSize)
.clamp(minHeight, double.infinity);

displayRect = Rect.fromLTWH(newX, newY, newWidth, newHeight);
draggingRect = displayRect;
Expand Down
37 changes: 25 additions & 12 deletions lib/widgets/tab_grid.dart
Original file line number Diff line number Diff line change
Expand Up @@ -302,24 +302,31 @@ class TabGridModel extends ChangeNotifier {
);
}

double previewX = DraggableWidgetContainer.snapToGrid(constrainedRect.left);
double previewY = DraggableWidgetContainer.snapToGrid(constrainedRect.top);
int? gridSize = preferences.getInt(PrefKeys.gridSize);

double previewX =
DraggableWidgetContainer.snapToGrid(constrainedRect.left, gridSize);
double previewY =
DraggableWidgetContainer.snapToGrid(constrainedRect.top, gridSize);

double previewWidth = DraggableWidgetContainer.snapToGrid(
constrainedRect.width.clamp(model.minWidth, double.infinity));
constrainedRect.width.clamp(model.minWidth, double.infinity), gridSize);
double previewHeight = DraggableWidgetContainer.snapToGrid(
constrainedRect.height.clamp(model.minHeight, double.infinity));
constrainedRect.height.clamp(model.minHeight, double.infinity),
gridSize);

if (previewWidth < model.minWidth) {
previewWidth = DraggableWidgetContainer.snapToGrid(
constrainedRect.width.clamp(model.minWidth, double.infinity) +
(preferences.getInt(PrefKeys.gridSize) ?? Defaults.gridSize));
(gridSize ?? Defaults.gridSize),
gridSize);
}

if (previewHeight < model.minHeight) {
previewHeight = DraggableWidgetContainer.snapToGrid(
constrainedRect.height.clamp(model.minHeight, double.infinity) +
(preferences.getInt(PrefKeys.gridSize) ?? Defaults.gridSize));
(preferences.getInt(PrefKeys.gridSize) ?? Defaults.gridSize),
gridSize);
}

Rect preview =
Expand Down Expand Up @@ -486,8 +493,12 @@ class TabGridModel extends ChangeNotifier {

Offset localPosition = getLocalPosition(globalPosition);

double previewX = DraggableWidgetContainer.snapToGrid(localPosition.dx);
double previewY = DraggableWidgetContainer.snapToGrid(localPosition.dy);
int? gridSize = preferences.getInt(PrefKeys.gridSize);

double previewX =
DraggableWidgetContainer.snapToGrid(localPosition.dx, gridSize);
double previewY =
DraggableWidgetContainer.snapToGrid(localPosition.dy, gridSize);

double width = widget.displayRect.width;
double height = widget.displayRect.height;
Expand Down Expand Up @@ -895,10 +906,12 @@ class TabGrid extends StatelessWidget {
),
);

double previewX =
DraggableWidgetContainer.snapToGrid(container.draggingRect.left);
double previewY =
DraggableWidgetContainer.snapToGrid(container.draggingRect.top);
int? gridSize = model.preferences.getInt(PrefKeys.gridSize);

double previewX = DraggableWidgetContainer.snapToGrid(
container.draggingRect.left, gridSize);
double previewY = DraggableWidgetContainer.snapToGrid(
container.draggingRect.top, gridSize);

Rect previewLocation = Rect.fromLTWH(previewX, previewY,
container.displayRect.width, container.displayRect.height);
Expand Down

0 comments on commit 49a2da3

Please sign in to comment.