Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit e70872b

Browse files
committed
Add support for setting window size limits for glfw
Add a function to the window which calls the glfw function for fixing the size limits of the window. This can then be called after window creation.
1 parent ff448c3 commit e70872b

File tree

8 files changed

+104
-0
lines changed

8 files changed

+104
-0
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,7 @@ FILE: ../../../flutter/shell/platform/fuchsia/runtime/dart/utils/vmservice_objec
10301030
FILE: ../../../flutter/shell/platform/fuchsia/runtime/dart/utils/vmservice_object.h
10311031
FILE: ../../../flutter/shell/platform/glfw/client_wrapper/flutter_window_controller.cc
10321032
FILE: ../../../flutter/shell/platform/glfw/client_wrapper/flutter_window_controller_unittests.cc
1033+
FILE: ../../../flutter/shell/platform/glfw/client_wrapper/flutter_window_unittests.cc
10331034
FILE: ../../../flutter/shell/platform/glfw/client_wrapper/include/flutter/flutter_window.h
10341035
FILE: ../../../flutter/shell/platform/glfw/client_wrapper/include/flutter/flutter_window_controller.h
10351036
FILE: ../../../flutter/shell/platform/glfw/client_wrapper/include/flutter/plugin_registrar_glfw.h

shell/platform/glfw/client_wrapper/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ executable("client_wrapper_glfw_unittests") {
7575
# TODO: Add more unit tests.
7676
sources = [
7777
"flutter_window_controller_unittests.cc",
78+
"flutter_window_unittests.cc",
7879
]
7980

8081
deps = [
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "flutter/shell/platform/glfw/client_wrapper/include/flutter/flutter_window.h"
6+
7+
#include <memory>
8+
#include <string>
9+
10+
#include "flutter/shell/platform/glfw/client_wrapper/testing/stub_flutter_glfw_api.h"
11+
#include "gtest/gtest.h"
12+
13+
namespace flutter {
14+
15+
namespace {
16+
17+
// Stub implementation to validate calls to the API.
18+
class TestGlfwApi : public testing::StubFlutterGlfwApi {
19+
public:
20+
// |flutter::testing::StubFlutterGlfwApi|
21+
void SetSizeLimits(FlutterDesktopSize minimum_size,
22+
FlutterDesktopSize maximum_size) override {
23+
set_size_limits_called_ = true;
24+
}
25+
26+
bool set_size_limits_called() { return set_size_limits_called_; }
27+
28+
private:
29+
bool set_size_limits_called_ = false;
30+
};
31+
32+
} // namespace
33+
34+
TEST(FlutterWindowTest, SetSizeLimits) {
35+
const std::string icu_data_path = "fake/path/to/icu";
36+
testing::ScopedStubFlutterGlfwApi scoped_api_stub(
37+
std::make_unique<TestGlfwApi>());
38+
auto test_api = static_cast<TestGlfwApi*>(scoped_api_stub.stub());
39+
// This is not actually used so any non-zero value works.
40+
auto raw_window = reinterpret_cast<FlutterDesktopWindowRef>(1);
41+
42+
auto window = std::make_unique<FlutterWindow>(raw_window);
43+
44+
FlutterDesktopSize minimum_size = {};
45+
minimum_size.width = 100;
46+
minimum_size.height = 100;
47+
48+
FlutterDesktopSize maximum_size = {};
49+
maximum_size.width = -1;
50+
maximum_size.height = -1;
51+
52+
window->SetSizeLimits(minimum_size, maximum_size);
53+
54+
EXPECT_EQ(test_api->set_size_limits_called(), true);
55+
}
56+
57+
} // namespace flutter

shell/platform/glfw/client_wrapper/include/flutter/flutter_window.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ class FlutterWindow {
8888
FlutterDesktopWindowSetPixelRatioOverride(window_, pixel_ratio);
8989
}
9090

91+
// Sets the min/max size of |flutter_window| in screen coordinates. Use
92+
// kFlutterDesktopDontCare for any dimension you wish to leave unconstrained.
93+
void SetSizeLimits(FlutterDesktopSize minimum_size,
94+
FlutterDesktopSize maximum_size) {
95+
FlutterDesktopWindowSetSizeLimits(window_, minimum_size, maximum_size);
96+
}
97+
9198
private:
9299
// Handle for interacting with the C API's window.
93100
//

shell/platform/glfw/client_wrapper/testing/stub_flutter_glfw_api.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@ void FlutterDesktopWindowSetFrame(FlutterDesktopWindowRef flutter_window,
107107
}
108108
}
109109

110+
void FlutterDesktopWindowSetSizeLimits(FlutterDesktopWindowRef flutter_window,
111+
FlutterDesktopSize minimum_size,
112+
FlutterDesktopSize maximum_size) {
113+
if (s_stub_implementation) {
114+
s_stub_implementation->SetSizeLimits(minimum_size, maximum_size);
115+
}
116+
}
117+
110118
double FlutterDesktopWindowGetScaleFactor(
111119
FlutterDesktopWindowRef flutter_window) {
112120
if (s_stub_implementation) {

shell/platform/glfw/client_wrapper/testing/stub_flutter_glfw_api.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ class StubFlutterGlfwApi {
6868
// Called for FlutterDesktopWindowSetPixelRatioOverride.
6969
virtual void SetPixelRatioOverride(double pixel_ratio) {}
7070

71+
// Called for FlutterDesktopWindowSetSizeLimits.
72+
virtual void SetSizeLimits(FlutterDesktopSize minimum_size,
73+
FlutterDesktopSize maximum_size) {}
74+
7175
// Called for FlutterDesktopRunWindowEventLoopWithTimeout.
7276
virtual bool RunWindowEventLoopWithTimeout(uint32_t millisecond_timeout) {
7377
return true;

shell/platform/glfw/flutter_glfw.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ using UniqueGLFWwindowPtr = std::unique_ptr<GLFWwindow, void (*)(GLFWwindow*)>;
3434

3535
static_assert(FLUTTER_ENGINE_VERSION == 1, "");
3636

37+
const int kFlutterDesktopDontCare = GLFW_DONT_CARE;
38+
3739
static constexpr double kDpPerInch = 160.0;
3840

3941
// Struct for storing state within an instance of the GLFW Window.
@@ -736,6 +738,14 @@ void FlutterDesktopWindowSetPixelRatioOverride(
736738
}
737739
}
738740

741+
void FlutterDesktopWindowSetSizeLimits(FlutterDesktopWindowRef flutter_window,
742+
FlutterDesktopSize minimum_size,
743+
FlutterDesktopSize maximum_size) {
744+
glfwSetWindowSizeLimits(flutter_window->window, minimum_size.width,
745+
minimum_size.height, maximum_size.width,
746+
maximum_size.height);
747+
}
748+
739749
bool FlutterDesktopRunWindowEventLoopWithTimeout(
740750
FlutterDesktopWindowControllerRef controller,
741751
uint32_t timeout_milliseconds) {

shell/platform/glfw/public/flutter_glfw.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
extern "C" {
1717
#endif
1818

19+
// Indicates that any value is acceptable for an otherwise required property.
20+
extern const int32_t kFlutterDesktopDontCare;
21+
1922
// Opaque reference to a Flutter window controller.
2023
typedef struct FlutterDesktopWindowControllerState*
2124
FlutterDesktopWindowControllerRef;
@@ -26,6 +29,12 @@ typedef struct FlutterDesktopWindow* FlutterDesktopWindowRef;
2629
// Opaque reference to a Flutter engine instance.
2730
typedef struct FlutterDesktopEngineState* FlutterDesktopEngineRef;
2831

32+
// Properties representing a generic rectangular size.
33+
typedef struct {
34+
int32_t width;
35+
int32_t height;
36+
} FlutterDesktopSize;
37+
2938
// Properties for configuring a Flutter engine instance.
3039
typedef struct {
3140
// The path to the flutter_assets folder for the application to be run.
@@ -167,6 +176,13 @@ FLUTTER_EXPORT void FlutterDesktopWindowSetPixelRatioOverride(
167176
FlutterDesktopWindowRef flutter_window,
168177
double pixel_ratio);
169178

179+
// Sets the min/max size of |flutter_window| in screen coordinates. Use
180+
// kFlutterDesktopDontCare for any dimension you wish to leave unconstrained.
181+
FLUTTER_EXPORT void FlutterDesktopWindowSetSizeLimits(
182+
FlutterDesktopWindowRef flutter_window,
183+
FlutterDesktopSize minimum_size,
184+
FlutterDesktopSize maximum_size);
185+
170186
// Runs an instance of a headless Flutter engine.
171187
//
172188
// Returns a null pointer in the event of an error.

0 commit comments

Comments
 (0)