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

Set size while keeping window centered #142

Closed
chimon2000 opened this issue May 5, 2022 · 3 comments
Closed

Set size while keeping window centered #142

chimon2000 opened this issue May 5, 2022 · 3 comments

Comments

@chimon2000
Copy link

Currently, the window grows downward and to the right, causing it to lose its positioning.

It would be nice to be able to set the size of the application window while keeping the window centered in the same call.

@lijy91
Copy link
Member

lijy91 commented May 8, 2022

We have a setBounds method that can set both position and size, You can use the screen_retriever plugin to get the screen size and then calculate the correct new position

Rect bounds = Rect.fromLTWH(10, 10, 800, 600);
windowManager.setBounds(bounds);

You can refer to the setAlignment method.

/// Move the window to a position aligned with the screen.
Future<void> setAlignment(Alignment alignment) async {
Size windowSize = await getSize();
Map<String, dynamic> primaryDisplay = await _getPrimaryDisplay();
num visibleWidth = primaryDisplay['size']['width'];
num visibleHeight = primaryDisplay['size']['height'];
num visibleStartX = 0;
num visibleStartY = 0;
if (primaryDisplay['visibleSize'] != null) {
visibleWidth = primaryDisplay['visibleSize']['width'];
visibleHeight = primaryDisplay['visibleSize']['height'];
}
if (primaryDisplay['visiblePosition'] != null) {
visibleStartX = primaryDisplay['visiblePosition']['x'];
visibleStartY = primaryDisplay['visiblePosition']['y'];
}
Offset position = Offset(0, 0);
if (alignment == Alignment.topLeft) {
position = Offset(0, 0);
} else if (alignment == Alignment.topCenter) {
position = Offset(
visibleStartX + (visibleWidth / 2) - (windowSize.width / 2),
visibleStartY + 0,
);
} else if (alignment == Alignment.topRight) {
position = Offset(
visibleStartX + visibleWidth - windowSize.width,
visibleStartY + 0,
);
} else if (alignment == Alignment.centerLeft) {
position = Offset(
visibleStartX + 0,
visibleStartY + ((visibleHeight / 2) - (windowSize.height / 2)),
);
} else if (alignment == Alignment.center) {
position = Offset(
visibleStartX + (visibleWidth / 2) - (windowSize.width / 2),
visibleStartY + ((visibleHeight / 2) - (windowSize.height / 2)),
);
} else if (alignment == Alignment.centerRight) {
position = Offset(
visibleStartX + visibleWidth - windowSize.width,
visibleStartY + ((visibleHeight / 2) - (windowSize.height / 2)),
);
} else if (alignment == Alignment.bottomLeft) {
position = Offset(
visibleStartX + 0,
visibleStartY + (visibleHeight - windowSize.height),
);
} else if (alignment == Alignment.bottomCenter) {
position = Offset(
visibleStartX + (visibleWidth / 2) - (windowSize.width / 2),
visibleStartY + (visibleHeight - windowSize.height),
);
} else if (alignment == Alignment.bottomRight) {
position = Offset(
visibleStartX + visibleWidth - windowSize.width,
visibleStartY + (visibleHeight - windowSize.height),
);
}
await this.setPosition(position);
}

@lijy91 lijy91 closed this as completed May 8, 2022
@chimon2000
Copy link
Author

@lijy91 setBounds currently ignores the animate property.

/// Resizes and moves the window to the supplied bounds.
Future<void> setBounds(Rect bounds, {animate = false}) async {
await setPosition(bounds.topLeft);
await setSize(bounds.size);
}

@lijy91
Copy link
Member

lijy91 commented May 11, 2022

@lijy91 setBounds currently ignores the animate property.

i will fix it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants