Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
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
1 change: 1 addition & 0 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,7 @@ FILE: ../../../flutter/shell/platform/fuchsia/runtime/dart/utils/vmservice_objec
FILE: ../../../flutter/shell/platform/fuchsia/runtime/dart/utils/vmservice_object.h
FILE: ../../../flutter/shell/platform/glfw/client_wrapper/flutter_window_controller.cc
FILE: ../../../flutter/shell/platform/glfw/client_wrapper/flutter_window_controller_unittests.cc
FILE: ../../../flutter/shell/platform/glfw/client_wrapper/flutter_window_unittests.cc
FILE: ../../../flutter/shell/platform/glfw/client_wrapper/include/flutter/flutter_window.h
FILE: ../../../flutter/shell/platform/glfw/client_wrapper/include/flutter/flutter_window_controller.h
FILE: ../../../flutter/shell/platform/glfw/client_wrapper/include/flutter/plugin_registrar_glfw.h
Expand Down
1 change: 1 addition & 0 deletions shell/platform/glfw/client_wrapper/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ executable("client_wrapper_glfw_unittests") {
# TODO: Add more unit tests.
sources = [
"flutter_window_controller_unittests.cc",
"flutter_window_unittests.cc",
]

deps = [
Expand Down
57 changes: 57 additions & 0 deletions shell/platform/glfw/client_wrapper/flutter_window_unittests.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this file! I forgot this hadn't been done for the window class yet; desktop embedding testing is not in a good state yet obviously, so getting more of the initial test files set up is great.

// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

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

#include <memory>
#include <string>

#include "flutter/shell/platform/glfw/client_wrapper/testing/stub_flutter_glfw_api.h"
#include "gtest/gtest.h"

namespace flutter {

namespace {

// Stub implementation to validate calls to the API.
class TestGlfwApi : public testing::StubFlutterGlfwApi {
public:
// |flutter::testing::StubFlutterGlfwApi|
void SetSizeLimits(FlutterDesktopSize minimum_size,
FlutterDesktopSize maximum_size) override {
set_size_limits_called_ = true;
}

bool set_size_limits_called() { return set_size_limits_called_; }

private:
bool set_size_limits_called_ = false;
};

} // namespace

TEST(FlutterWindowTest, SetSizeLimits) {
const std::string icu_data_path = "fake/path/to/icu";
testing::ScopedStubFlutterGlfwApi scoped_api_stub(
std::make_unique<TestGlfwApi>());
auto test_api = static_cast<TestGlfwApi*>(scoped_api_stub.stub());
// This is not actually used so any non-zero value works.
auto raw_window = reinterpret_cast<FlutterDesktopWindowRef>(1);

auto window = std::make_unique<FlutterWindow>(raw_window);

FlutterDesktopSize minimum_size = {};
minimum_size.width = 100;
minimum_size.height = 100;

FlutterDesktopSize maximum_size = {};
maximum_size.width = -1;
maximum_size.height = -1;

window->SetSizeLimits(minimum_size, maximum_size);

EXPECT_EQ(test_api->set_size_limits_called(), true);
}

} // namespace flutter
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ class FlutterWindow {
FlutterDesktopWindowSetPixelRatioOverride(window_, pixel_ratio);
}

// Sets the min/max size of |flutter_window| in screen coordinates. Use
// kFlutterDesktopDontCare for any dimension you wish to leave unconstrained.
void SetSizeLimits(FlutterDesktopSize minimum_size,
FlutterDesktopSize maximum_size) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may want to use different structs/constants rather than relying on the wrapped API's types, but that's fine to revisit later.

FlutterDesktopWindowSetSizeLimits(window_, minimum_size, maximum_size);
}

private:
// Handle for interacting with the C API's window.
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ void FlutterDesktopWindowSetFrame(FlutterDesktopWindowRef flutter_window,
}
}

void FlutterDesktopWindowSetSizeLimits(FlutterDesktopWindowRef flutter_window,
FlutterDesktopSize minimum_size,
FlutterDesktopSize maximum_size) {
if (s_stub_implementation) {
s_stub_implementation->SetSizeLimits(minimum_size, maximum_size);
}
}

double FlutterDesktopWindowGetScaleFactor(
FlutterDesktopWindowRef flutter_window) {
if (s_stub_implementation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ class StubFlutterGlfwApi {
// Called for FlutterDesktopWindowSetPixelRatioOverride.
virtual void SetPixelRatioOverride(double pixel_ratio) {}

// Called for FlutterDesktopWindowSetSizeLimits.
virtual void SetSizeLimits(FlutterDesktopSize minimum_size,
FlutterDesktopSize maximum_size) {}

// Called for FlutterDesktopRunWindowEventLoopWithTimeout.
virtual bool RunWindowEventLoopWithTimeout(uint32_t millisecond_timeout) {
return true;
Expand Down
10 changes: 10 additions & 0 deletions shell/platform/glfw/flutter_glfw.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ using UniqueGLFWwindowPtr = std::unique_ptr<GLFWwindow, void (*)(GLFWwindow*)>;

static_assert(FLUTTER_ENGINE_VERSION == 1, "");

const int kFlutterDesktopDontCare = GLFW_DONT_CARE;

static constexpr double kDpPerInch = 160.0;

// Struct for storing state within an instance of the GLFW Window.
Expand Down Expand Up @@ -736,6 +738,14 @@ void FlutterDesktopWindowSetPixelRatioOverride(
}
}

void FlutterDesktopWindowSetSizeLimits(FlutterDesktopWindowRef flutter_window,
FlutterDesktopSize minimum_size,
FlutterDesktopSize maximum_size) {
glfwSetWindowSizeLimits(flutter_window->window, minimum_size.width,
minimum_size.height, maximum_size.width,
maximum_size.height);
}

bool FlutterDesktopRunWindowEventLoopWithTimeout(
FlutterDesktopWindowControllerRef controller,
uint32_t timeout_milliseconds) {
Expand Down
16 changes: 16 additions & 0 deletions shell/platform/glfw/public/flutter_glfw.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
extern "C" {
#endif

// Indicates that any value is acceptable for an otherwise required property.
extern const int32_t kFlutterDesktopDontCare;

// Opaque reference to a Flutter window controller.
typedef struct FlutterDesktopWindowControllerState*
FlutterDesktopWindowControllerRef;
Expand All @@ -26,6 +29,12 @@ typedef struct FlutterDesktopWindow* FlutterDesktopWindowRef;
// Opaque reference to a Flutter engine instance.
typedef struct FlutterDesktopEngineState* FlutterDesktopEngineRef;

// Properties representing a generic rectangular size.
typedef struct {
int32_t width;
int32_t height;
} FlutterDesktopSize;

// Properties for configuring a Flutter engine instance.
typedef struct {
// The path to the flutter_assets folder for the application to be run.
Expand Down Expand Up @@ -167,6 +176,13 @@ FLUTTER_EXPORT void FlutterDesktopWindowSetPixelRatioOverride(
FlutterDesktopWindowRef flutter_window,
double pixel_ratio);

// Sets the min/max size of |flutter_window| in screen coordinates. Use
// kFlutterDesktopDontCare for any dimension you wish to leave unconstrained.
FLUTTER_EXPORT void FlutterDesktopWindowSetSizeLimits(
FlutterDesktopWindowRef flutter_window,
FlutterDesktopSize minimum_size,
FlutterDesktopSize maximum_size);

// Runs an instance of a headless Flutter engine.
//
// Returns a null pointer in the event of an error.
Expand Down