Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix grid size not working #75

Merged
merged 1 commit into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading