From 97e32ac14069e52d439af226e7db5b0d5ec84660 Mon Sep 17 00:00:00 2001 From: stuartmorgan Date: Thu, 22 Aug 2019 16:48:22 -0700 Subject: [PATCH] Allow non-resizable windows in GLFW embedding (flutter/engine#11386) Adds a flag to create non-resizeable windows. Since the number of parameters is getting awkward, extracts window-related parameters and engine-related parameters into structs for clarity. This also removes some duplication in method signatures. The window parameters struct change is also made to the C++ wrapper, making this a breaking change for the runners. Fixes https://github.com/flutter/flutter/issues/37623 --- .../flutter_window_controller.cc | 31 ++++++---- .../flutter/flutter_window_controller.h | 17 +++++- .../testing/stub_flutter_glfw_api.cc | 23 +++---- .../testing/stub_flutter_glfw_api.h | 15 ++--- .../shell/platform/glfw/flutter_glfw.cc | 42 ++++++------- .../shell/platform/glfw/public/flutter_glfw.h | 61 +++++++++++-------- 6 files changed, 97 insertions(+), 92 deletions(-) diff --git a/engine/src/flutter/shell/platform/glfw/client_wrapper/flutter_window_controller.cc b/engine/src/flutter/shell/platform/glfw/client_wrapper/flutter_window_controller.cc index 0d3fc3c4c3..09df37c355 100644 --- a/engine/src/flutter/shell/platform/glfw/client_wrapper/flutter_window_controller.cc +++ b/engine/src/flutter/shell/platform/glfw/client_wrapper/flutter_window_controller.cc @@ -25,9 +25,7 @@ FlutterWindowController::~FlutterWindowController() { } bool FlutterWindowController::CreateWindow( - int width, - int height, - const std::string& title, + const WindowProperties& window_properties, const std::string& assets_path, const std::vector& arguments) { if (!init_succeeded_) { @@ -41,15 +39,26 @@ bool FlutterWindowController::CreateWindow( return false; } - std::vector engine_arguments; - std::transform( - arguments.begin(), arguments.end(), std::back_inserter(engine_arguments), - [](const std::string& arg) -> const char* { return arg.c_str(); }); - size_t arg_count = engine_arguments.size(); + FlutterDesktopWindowProperties c_window_properties = {}; + c_window_properties.title = window_properties.title.c_str(); + c_window_properties.width = window_properties.width; + c_window_properties.height = window_properties.height; + c_window_properties.prevent_resize = window_properties.prevent_resize; - controller_ = FlutterDesktopCreateWindow( - width, height, title.c_str(), assets_path.c_str(), icu_data_path_.c_str(), - arg_count > 0 ? &engine_arguments[0] : nullptr, arg_count); + FlutterDesktopEngineProperties c_engine_properties = {}; + c_engine_properties.assets_path = assets_path.c_str(); + c_engine_properties.icu_data_path = icu_data_path_.c_str(); + std::vector engine_switches; + std::transform( + arguments.begin(), arguments.end(), std::back_inserter(engine_switches), + [](const std::string& arg) -> const char* { return arg.c_str(); }); + if (engine_switches.size() > 0) { + c_engine_properties.switches = &engine_switches[0]; + c_engine_properties.switches_count = engine_switches.size(); + } + + controller_ = + FlutterDesktopCreateWindow(c_window_properties, c_engine_properties); if (!controller_) { std::cerr << "Failed to create window." << std::endl; return false; diff --git a/engine/src/flutter/shell/platform/glfw/client_wrapper/include/flutter/flutter_window_controller.h b/engine/src/flutter/shell/platform/glfw/client_wrapper/include/flutter/flutter_window_controller.h index a49fd0ccca..95da9cb657 100644 --- a/engine/src/flutter/shell/platform/glfw/client_wrapper/include/flutter/flutter_window_controller.h +++ b/engine/src/flutter/shell/platform/glfw/client_wrapper/include/flutter/flutter_window_controller.h @@ -17,6 +17,19 @@ namespace flutter { +// Properties for Flutter window creation. +struct WindowProperties { + // The display title. + std::string title; + // Width in screen coordinates. + int32_t width; + // Height in screen coordinates. + int32_t height; + // Whether or not the user is prevented from resizing the window. + // Reversed so that the default for a cleared struct is to allow resizing. + bool prevent_resize; +}; + // A controller for a window displaying Flutter content. // // This is the primary wrapper class for the desktop C API. @@ -51,9 +64,7 @@ class FlutterWindowController { // for details. Not all arguments will apply to desktop. // // Only one Flutter window can exist at a time; see constructor comment. - bool CreateWindow(int width, - int height, - const std::string& title, + bool CreateWindow(const WindowProperties& window_properties, const std::string& assets_path, const std::vector& arguments); diff --git a/engine/src/flutter/shell/platform/glfw/client_wrapper/testing/stub_flutter_glfw_api.cc b/engine/src/flutter/shell/platform/glfw/client_wrapper/testing/stub_flutter_glfw_api.cc index 46c691bac7..a5e1be7c73 100644 --- a/engine/src/flutter/shell/platform/glfw/client_wrapper/testing/stub_flutter_glfw_api.cc +++ b/engine/src/flutter/shell/platform/glfw/client_wrapper/testing/stub_flutter_glfw_api.cc @@ -49,17 +49,11 @@ void FlutterDesktopTerminate() { } FlutterDesktopWindowControllerRef FlutterDesktopCreateWindow( - int initial_width, - int initial_height, - const char* title, - const char* assets_path, - const char* icu_data_path, - const char** arguments, - size_t argument_count) { + const FlutterDesktopWindowProperties& window_properties, + const FlutterDesktopEngineProperties& engine_properties) { if (s_stub_implementation) { - return s_stub_implementation->CreateWindow( - initial_width, initial_height, title, assets_path, icu_data_path, - arguments, argument_count); + return s_stub_implementation->CreateWindow(window_properties, + engine_properties); } return nullptr; } @@ -131,13 +125,10 @@ bool FlutterDesktopRunWindowEventLoopWithTimeout( return true; } -FlutterDesktopEngineRef FlutterDesktopRunEngine(const char* assets_path, - const char* icu_data_path, - const char** arguments, - size_t argument_count) { +FlutterDesktopEngineRef FlutterDesktopRunEngine( + const FlutterDesktopEngineProperties& properties) { if (s_stub_implementation) { - return s_stub_implementation->RunEngine(assets_path, icu_data_path, - arguments, argument_count); + return s_stub_implementation->RunEngine(properties); } return nullptr; } diff --git a/engine/src/flutter/shell/platform/glfw/client_wrapper/testing/stub_flutter_glfw_api.h b/engine/src/flutter/shell/platform/glfw/client_wrapper/testing/stub_flutter_glfw_api.h index 52e0682da6..eaa5409d21 100644 --- a/engine/src/flutter/shell/platform/glfw/client_wrapper/testing/stub_flutter_glfw_api.h +++ b/engine/src/flutter/shell/platform/glfw/client_wrapper/testing/stub_flutter_glfw_api.h @@ -37,13 +37,8 @@ class StubFlutterGlfwApi { // Called for FlutterDesktopCreateWindow. virtual FlutterDesktopWindowControllerRef CreateWindow( - int initial_width, - int initial_height, - const char* title, - const char* assets_path, - const char* icu_data_path, - const char** arguments, - size_t argument_count) { + const FlutterDesktopWindowProperties& window_properties, + const FlutterDesktopEngineProperties& engine_properties) { return nullptr; } @@ -76,10 +71,8 @@ class StubFlutterGlfwApi { } // Called for FlutterDesktopRunEngine. - virtual FlutterDesktopEngineRef RunEngine(const char* assets_path, - const char* icu_data_path, - const char** arguments, - size_t argument_count) { + virtual FlutterDesktopEngineRef RunEngine( + const FlutterDesktopEngineProperties& properties) { return nullptr; } diff --git a/engine/src/flutter/shell/platform/glfw/flutter_glfw.cc b/engine/src/flutter/shell/platform/glfw/flutter_glfw.cc index ba51eecbfe..b5f9fe666b 100644 --- a/engine/src/flutter/shell/platform/glfw/flutter_glfw.cc +++ b/engine/src/flutter/shell/platform/glfw/flutter_glfw.cc @@ -482,17 +482,15 @@ static void GLFWErrorCallback(int error_code, const char* description) { // Returns a caller-owned pointer to the engine. static FLUTTER_API_SYMBOL(FlutterEngine) RunFlutterEngine(GLFWwindow* window, - const char* assets_path, - const char* icu_data_path, - const char** arguments, - size_t arguments_count, + const FlutterDesktopEngineProperties& engine_properties, const FlutterCustomTaskRunners* custom_task_runners) { // FlutterProjectArgs is expecting a full argv, so when processing it for // flags the first item is treated as the executable and ignored. Add a dummy // value so that all provided arguments are used. std::vector argv = {"placeholder"}; - if (arguments_count > 0) { - argv.insert(argv.end(), &arguments[0], &arguments[arguments_count]); + if (engine_properties.switches_count > 0) { + argv.insert(argv.end(), &engine_properties.switches[0], + &engine_properties.switches[engine_properties.switches_count]); } FlutterRendererConfig config = {}; @@ -516,8 +514,8 @@ static FLUTTER_API_SYMBOL(FlutterEngine) } FlutterProjectArgs args = {}; args.struct_size = sizeof(FlutterProjectArgs); - args.assets_path = assets_path; - args.icu_data_path = icu_data_path; + args.assets_path = engine_properties.assets_path; + args.icu_data_path = engine_properties.icu_data_path; args.command_line_argc = static_cast(argv.size()); args.command_line_argv = &argv[0]; args.platform_message_callback = GLFWOnFlutterPlatformMessage; @@ -544,19 +542,19 @@ void FlutterDesktopTerminate() { } FlutterDesktopWindowControllerRef FlutterDesktopCreateWindow( - int initial_width, - int initial_height, - const char* title, - const char* assets_path, - const char* icu_data_path, - const char** arguments, - size_t argument_count) { + const FlutterDesktopWindowProperties& window_properties, + const FlutterDesktopEngineProperties& engine_properties) { auto state = std::make_unique(); // Create the window, and set the state as its user data. + if (window_properties.prevent_resize) { + glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); + } state->window = UniqueGLFWwindowPtr( - glfwCreateWindow(initial_width, initial_height, title, NULL, NULL), + glfwCreateWindow(window_properties.width, window_properties.height, + window_properties.title, NULL, NULL), glfwDestroyWindow); + glfwDefaultWindowHints(); GLFWwindow* window = state->window.get(); if (window == nullptr) { return nullptr; @@ -598,8 +596,7 @@ FlutterDesktopWindowControllerRef FlutterDesktopCreateWindow( // Start the engine. state->engine = - RunFlutterEngine(window, assets_path, icu_data_path, arguments, - argument_count, &custom_task_runners); + RunFlutterEngine(window, engine_properties, &custom_task_runners); if (state->engine == nullptr) { return nullptr; } @@ -745,13 +742,10 @@ FlutterDesktopPluginRegistrarRef FlutterDesktopGetPluginRegistrar( return controller->plugin_registrar.get(); } -FlutterDesktopEngineRef FlutterDesktopRunEngine(const char* assets_path, - const char* icu_data_path, - const char** arguments, - size_t argument_count) { +FlutterDesktopEngineRef FlutterDesktopRunEngine( + const FlutterDesktopEngineProperties& properties) { auto engine = - RunFlutterEngine(nullptr, assets_path, icu_data_path, arguments, - argument_count, nullptr /* custom task runners */); + RunFlutterEngine(nullptr, properties, nullptr /* custom task runners */); if (engine == nullptr) { return nullptr; } diff --git a/engine/src/flutter/shell/platform/glfw/public/flutter_glfw.h b/engine/src/flutter/shell/platform/glfw/public/flutter_glfw.h index 502722cda6..ef6268376f 100644 --- a/engine/src/flutter/shell/platform/glfw/public/flutter_glfw.h +++ b/engine/src/flutter/shell/platform/glfw/public/flutter_glfw.h @@ -26,6 +26,34 @@ typedef struct FlutterDesktopWindow* FlutterDesktopWindowRef; // Opaque reference to a Flutter engine instance. typedef struct FlutterDesktopEngineState* FlutterDesktopEngineRef; +// Properties for configuring a Flutter engine instance. +typedef struct { + // The path to the flutter_assets folder for the application to be run. + const char* assets_path; + // The path to the icudtl.dat file for the version of Flutter you are using. + const char* icu_data_path; + // The switches to pass to the Flutter engine. + // + // See: https://github.com/flutter/engine/blob/master/shell/common/switches.h + // for details. Not all arguments will apply to desktop. + const char** switches; + // The number of elements in |switches|. + size_t switches_count; +} FlutterDesktopEngineProperties; + +// Properties for configuring the initial settings of a Flutter window. +typedef struct { + // The display title. + const char* title; + // Width in screen coordinates. + int32_t width; + // Height in screen coordinates. + int32_t height; + // Whether or not the user is prevented from resizing the window. + // Reversed so that the default for a cleared struct is to allow resizing. + bool prevent_resize; +} FlutterDesktopWindowProperties; + // Sets up the library's graphic context. Must be called before any other // methods. // @@ -41,26 +69,16 @@ FLUTTER_EXPORT void FlutterDesktopTerminate(); // // FlutterDesktopInit() must be called prior to this function. // -// The |assets_path| is the path to the flutter_assets folder for the Flutter -// application to be run. |icu_data_path| is the path to the icudtl.dat file -// for the version of Flutter you are using. -// -// The |arguments| are passed to the Flutter engine. See: -// https://github.com/flutter/engine/blob/master/shell/common/switches.h for -// for details. Not all arguments will apply to desktop. +// This will set up and run an associated Flutter engine using the settings in +// |engine_properties|. // // Returns a null pointer in the event of an error. Otherwise, the pointer is // valid until FlutterDesktopDestroyWindow is called. // Note that calling FlutterDesktopCreateWindow without later calling // FlutterDesktopDestroyWindow on the returned reference is a memory leak. -FLUTTER_EXPORT FlutterDesktopWindowControllerRef -FlutterDesktopCreateWindow(int initial_width, - int initial_height, - const char* title, - const char* assets_path, - const char* icu_data_path, - const char** arguments, - size_t argument_count); +FLUTTER_EXPORT FlutterDesktopWindowControllerRef FlutterDesktopCreateWindow( + const FlutterDesktopWindowProperties& window_properties, + const FlutterDesktopEngineProperties& engine_properties); // Shuts down the engine instance associated with |controller|, and cleans up // associated state. @@ -143,20 +161,9 @@ FLUTTER_EXPORT double FlutterDesktopWindowGetScaleFactor( // Runs an instance of a headless Flutter engine. // -// The |assets_path| is the path to the flutter_assets folder for the Flutter -// application to be run. |icu_data_path| is the path to the icudtl.dat file -// for the version of Flutter you are using. -// -// The |arguments| are passed to the Flutter engine. See: -// https://github.com/flutter/engine/blob/master/shell/common/switches.h for -// for details. Not all arguments will apply to desktop. -// // Returns a null pointer in the event of an error. FLUTTER_EXPORT FlutterDesktopEngineRef -FlutterDesktopRunEngine(const char* assets_path, - const char* icu_data_path, - const char** arguments, - size_t argument_count); +FlutterDesktopRunEngine(const FlutterDesktopEngineProperties& properties); // Shuts down the given engine instance. Returns true if the shutdown was // successful. |engine_ref| is no longer valid after this call.