fix: apply size constraints to NSWindow (#39975)

This commit is contained in:
Cheng Zhao 2023-09-27 18:11:24 +09:00 committed by GitHub
parent e613595982
commit ad57867594
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 0 deletions

View file

@ -354,10 +354,14 @@ void NativeWindow::SetContentSizeConstraints(
size_constraints_.reset(); size_constraints_.reset();
} }
// Windows/Linux:
// The return value of GetContentSizeConstraints will be passed to Chromium // The return value of GetContentSizeConstraints will be passed to Chromium
// to set min/max sizes of window. Note that we are returning content size // to set min/max sizes of window. Note that we are returning content size
// instead of window size because that is what Chromium expects, see the // instead of window size because that is what Chromium expects, see the
// comment of |WidgetSizeIsClientSize| in Chromium's codebase to learn more. // comment of |WidgetSizeIsClientSize| in Chromium's codebase to learn more.
//
// macOS:
// The min/max sizes are set directly by calling NSWindow's methods.
extensions::SizeConstraints NativeWindow::GetContentSizeConstraints() const { extensions::SizeConstraints NativeWindow::GetContentSizeConstraints() const {
if (content_size_constraints_) if (content_size_constraints_)
return *content_size_constraints_; return *content_size_constraints_;

View file

@ -58,6 +58,8 @@ class NativeWindowMac : public NativeWindow,
gfx::Rect GetBounds() override; gfx::Rect GetBounds() override;
bool IsNormal() override; bool IsNormal() override;
gfx::Rect GetNormalBounds() override; gfx::Rect GetNormalBounds() override;
void SetSizeConstraints(
const extensions::SizeConstraints& window_constraints) override;
void SetContentSizeConstraints( void SetContentSizeConstraints(
const extensions::SizeConstraints& size_constraints) override; const extensions::SizeConstraints& size_constraints) override;
void SetResizable(bool resizable) override; void SetResizable(bool resizable) override;

View file

@ -761,6 +761,16 @@ gfx::Rect NativeWindowMac::GetNormalBounds() {
// return widget()->GetRestoredBounds(); // return widget()->GetRestoredBounds();
} }
void NativeWindowMac::SetSizeConstraints(
const extensions::SizeConstraints& window_constraints) {
// Apply the size constraints to NSWindow.
if (window_constraints.HasMinimumSize())
[window_ setMinSize:window_constraints.GetMinimumSize().ToCGSize()];
if (window_constraints.HasMaximumSize())
[window_ setMaxSize:window_constraints.GetMaximumSize().ToCGSize()];
NativeWindow::SetSizeConstraints(window_constraints);
}
void NativeWindowMac::SetContentSizeConstraints( void NativeWindowMac::SetContentSizeConstraints(
const extensions::SizeConstraints& size_constraints) { const extensions::SizeConstraints& size_constraints) {
auto convertSize = [this](const gfx::Size& size) { auto convertSize = [this](const gfx::Size& size) {
@ -775,6 +785,7 @@ void NativeWindowMac::SetContentSizeConstraints(
} }
}; };
// Apply the size constraints to NSWindow.
NSView* content = [window_ contentView]; NSView* content = [window_ contentView];
if (size_constraints.HasMinimumSize()) { if (size_constraints.HasMinimumSize()) {
NSSize min_size = convertSize(size_constraints.GetMinimumSize()); NSSize min_size = convertSize(size_constraints.GetMinimumSize());