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
2 changes: 2 additions & 0 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,9 @@ FILE: ../../../flutter/shell/platform/embedder/vsync_waiter_embedder.cc
FILE: ../../../flutter/shell/platform/embedder/vsync_waiter_embedder.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/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
FILE: ../../../flutter/shell/platform/glfw/flutter_glfw.cc
FILE: ../../../flutter/shell/platform/glfw/key_event_handler.cc
FILE: ../../../flutter/shell/platform/glfw/key_event_handler.h
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class PluginRegistrar {
// provides must remain valid as long as this object exists.
explicit PluginRegistrar(FlutterDesktopPluginRegistrarRef core_registrar);

~PluginRegistrar();
virtual ~PluginRegistrar();

// Prevent copying.
PluginRegistrar(PluginRegistrar const&) = delete;
Expand Down
6 changes: 5 additions & 1 deletion shell/platform/glfw/client_wrapper/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

import("$flutter_root/shell/platform/common/cpp/client_wrapper/publish.gni")

_wrapper_includes = [ "include/flutter/flutter_window_controller.h" ]
_wrapper_includes = [
"include/flutter/flutter_window.h",
"include/flutter/flutter_window_controller.h",
"include/flutter/plugin_registrar_glfw.h",
]

_wrapper_sources = [ "flutter_window_controller.cc" ]

Expand Down
35 changes: 14 additions & 21 deletions shell/platform/glfw/client_wrapper/flutter_window_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ FlutterWindowController::FlutterWindowController(
}

FlutterWindowController::~FlutterWindowController() {
if (controller_) {
FlutterDesktopDestroyWindow(controller_);
}
if (init_succeeded_) {
FlutterDesktopTerminate();
}
Expand All @@ -33,7 +36,7 @@ bool FlutterWindowController::CreateWindow(
return false;
}

if (window_) {
if (controller_) {
std::cerr << "Only one Flutter window can exist at a time." << std::endl;
return false;
}
Expand All @@ -44,45 +47,35 @@ bool FlutterWindowController::CreateWindow(
[](const std::string& arg) -> const char* { return arg.c_str(); });
size_t arg_count = engine_arguments.size();

window_ = FlutterDesktopCreateWindow(
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);
if (!window_) {
if (!controller_) {
std::cerr << "Failed to create window." << std::endl;
return false;
}
window_ =
std::make_unique<FlutterWindow>(FlutterDesktopGetWindow(controller_));
return true;
}

FlutterDesktopPluginRegistrarRef FlutterWindowController::GetRegistrarForPlugin(
const std::string& plugin_name) {
if (!window_) {
if (!controller_) {
std::cerr << "Cannot get plugin registrar without a window; call "
"CreateWindow first."
<< std::endl;
return nullptr;
}
return FlutterDesktopGetPluginRegistrar(window_, plugin_name.c_str());
}

void FlutterWindowController::SetHoverEnabled(bool enabled) {
FlutterDesktopSetHoverEnabled(window_, enabled);
}

void FlutterWindowController::SetTitle(const std::string& title) {
FlutterDesktopSetWindowTitle(window_, title.c_str());
}

void FlutterWindowController::SetIcon(uint8_t* pixel_data,
int width,
int height) {
FlutterDesktopSetWindowIcon(window_, pixel_data, width, height);
return FlutterDesktopGetPluginRegistrar(controller_, plugin_name.c_str());
}

void FlutterWindowController::RunEventLoop() {
if (window_) {
FlutterDesktopRunWindowLoop(window_);
if (controller_) {
FlutterDesktopRunWindowLoop(controller_);
}
window_ = nullptr;
controller_ = nullptr;
}

} // namespace flutter
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef FLUTTER_SHELL_PLATFORM_GLFW_CLIENT_WRAPPER_INCLUDE_FLUTTER_FLUTTER_WINDOW_H_
#define FLUTTER_SHELL_PLATFORM_GLFW_CLIENT_WRAPPER_INCLUDE_FLUTTER_FLUTTER_WINDOW_H_

#include <string>
#include <vector>

#include <flutter_glfw.h>

#include "plugin_registrar.h"

namespace flutter {

// A window displaying Flutter content.
class FlutterWindow {
public:
explicit FlutterWindow(FlutterDesktopWindowRef window) : window_(window) {}

~FlutterWindow() = default;

// Prevent copying.
FlutterWindow(FlutterWindow const&) = delete;
FlutterWindow& operator=(FlutterWindow const&) = delete;

// Enables or disables hover tracking.
//
// If hover is enabled, mouse movement will send hover events to the Flutter
// engine, rather than only tracking the mouse while the button is pressed.
// Defaults to off.
void SetHoverEnabled(bool enabled) {
FlutterDesktopWindowSetHoverEnabled(window_, enabled);
}

// Sets the displayed title of the window.
void SetTitle(const std::string& title) {
FlutterDesktopWindowSetTitle(window_, title.c_str());
}

// Sets the displayed icon for the window.
//
// The pixel format is 32-bit RGBA. The provided image data only needs to be
// valid for the duration of the call to this method. Pass a nullptr to revert
// to the default icon.
void SetIcon(uint8_t* pixel_data, int width, int height) {
FlutterDesktopWindowSetIcon(window_, pixel_data, width, height);
}

private:
// Handle for interacting with the C API's window.
//
// Note: window_ is conceptually owned by the controller, not this object.
FlutterDesktopWindowRef window_;
};

} // namespace flutter

#endif // FLUTTER_SHELL_PLATFORM_GLFW_CLIENT_WRAPPER_INCLUDE_FLUTTER_FLUTTER_WINDOW_H_
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
#ifndef FLUTTER_SHELL_PLATFORM_GLFW_CLIENT_WRAPPER_INCLUDE_FLUTTER_FLUTTER_WINDOW_CONTROLLER_H_
#define FLUTTER_SHELL_PLATFORM_GLFW_CLIENT_WRAPPER_INCLUDE_FLUTTER_FLUTTER_WINDOW_CONTROLLER_H_

#include <memory>
#include <string>
#include <vector>

#include <flutter_glfw.h>

#include "flutter_window.h"
#include "plugin_registrar.h"

namespace flutter {
Expand All @@ -33,6 +35,10 @@ class FlutterWindowController {

~FlutterWindowController();

// Prevent copying.
FlutterWindowController(FlutterWindowController const&) = delete;
FlutterWindowController& operator=(FlutterWindowController const&) = delete;

// Creates and displays a window for displaying Flutter content.
//
// The |assets_path| is the path to the flutter_assets folder for the Flutter
Expand All @@ -57,22 +63,9 @@ class FlutterWindowController {
FlutterDesktopPluginRegistrarRef GetRegistrarForPlugin(
const std::string& plugin_name);

// Enables or disables hover tracking.
//
// If hover is enabled, mouse movement will send hover events to the Flutter
// engine, rather than only tracking the mouse while the button is pressed.
// Defaults to off.
void SetHoverEnabled(bool enabled);

// Sets the displayed title of the window.
void SetTitle(const std::string& title);

// Sets the displayed icon for the window.
//
// The pixel format is 32-bit RGBA. The provided image data only needs to be
// valid for the duration of the call to this method. Pass a nullptr to revert
// to the default icon.
void SetIcon(uint8_t* pixel_data, int width, int height);
// The FlutterWindow managed by this controller, if any. Returns nullptr
// before CreateWindow is called, and after RunEventLoop returns;
FlutterWindow* window() { return window_.get(); }

// Loops on Flutter window events until the window closes.
void RunEventLoop();
Expand All @@ -85,8 +78,11 @@ class FlutterWindowController {
// Whether or not FlutterDesktopInit succeeded at creation time.
bool init_succeeded_ = false;

// The curent Flutter window, if any.
FlutterDesktopWindowRef window_ = nullptr;
// The owned FlutterWindow, if any.
std::unique_ptr<FlutterWindow> window_;

// Handle for interacting with the C API's window controller, if any.
FlutterDesktopWindowControllerRef controller_ = nullptr;
};

} // namespace flutter
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef FLUTTER_SHELL_PLATFORM_GLFW_CLIENT_WRAPPER_INCLUDE_FLUTTER_PLUGIN_REGISTRAR_GLFW_H_
#define FLUTTER_SHELL_PLATFORM_GLFW_CLIENT_WRAPPER_INCLUDE_FLUTTER_PLUGIN_REGISTRAR_GLFW_H_

#include <memory>

#include <flutter_glfw.h>

#include "flutter_window.h"
#include "plugin_registrar.h"

namespace flutter {

// An extension to PluginRegistrar providing access to GLFW-shell-specific
// functionality.
class PluginRegistrarGlfw : public PluginRegistrar {
public:
// Creates a new PluginRegistrar. |core_registrar| and the messenger it
// provides must remain valid as long as this object exists.
explicit PluginRegistrarGlfw(FlutterDesktopPluginRegistrarRef core_registrar)
: PluginRegistrar(core_registrar) {
window_ = std::make_unique<FlutterWindow>(
FlutterDesktopRegistrarGetWindow(core_registrar));
}

virtual ~PluginRegistrarGlfw() = default;

// Prevent copying.
PluginRegistrarGlfw(PluginRegistrarGlfw const&) = delete;
PluginRegistrarGlfw& operator=(PluginRegistrarGlfw const&) = delete;

FlutterWindow* window() { return window_.get(); }

private:
// The owned FlutterWindow, if any.
std::unique_ptr<FlutterWindow> window_;
};

} // namespace flutter

#endif // FLUTTER_SHELL_PLATFORM_GLFW_CLIENT_WRAPPER_INCLUDE_FLUTTER_PLUGIN_REGISTRAR_GLFW_H_
41 changes: 27 additions & 14 deletions shell/platform/glfw/client_wrapper/testing/stub_flutter_glfw_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,14 @@ void FlutterDesktopTerminate() {
}
}

FlutterDesktopWindowRef 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) {
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) {
if (s_stub_implementation) {
return s_stub_implementation->CreateWindow(
initial_width, initial_height, title, assets_path, icu_data_path,
Expand All @@ -63,21 +64,27 @@ FlutterDesktopWindowRef FlutterDesktopCreateWindow(int initial_width,
return nullptr;
}

void FlutterDesktopSetHoverEnabled(FlutterDesktopWindowRef flutter_window,
bool enabled) {
void FlutterDesktopDestroyWindow(FlutterDesktopWindowControllerRef controller) {
if (s_stub_implementation) {
s_stub_implementation->DestroyWindow();
}
}

void FlutterDesktopWindowSetHoverEnabled(FlutterDesktopWindowRef flutter_window,
bool enabled) {
if (s_stub_implementation) {
s_stub_implementation->SetHoverEnabled(enabled);
}
}

void FlutterDesktopSetWindowTitle(FlutterDesktopWindowRef flutter_window,
void FlutterDesktopWindowSetTitle(FlutterDesktopWindowRef flutter_window,
const char* title) {
if (s_stub_implementation) {
s_stub_implementation->SetWindowTitle(title);
}
}

void FlutterDesktopSetWindowIcon(FlutterDesktopWindowRef flutter_window,
void FlutterDesktopWindowSetIcon(FlutterDesktopWindowRef flutter_window,
uint8_t* pixel_data,
int width,
int height) {
Expand All @@ -86,7 +93,7 @@ void FlutterDesktopSetWindowIcon(FlutterDesktopWindowRef flutter_window,
}
}

void FlutterDesktopRunWindowLoop(FlutterDesktopWindowRef flutter_window) {
void FlutterDesktopRunWindowLoop(FlutterDesktopWindowControllerRef controller) {
if (s_stub_implementation) {
s_stub_implementation->RunWindowLoop();
}
Expand All @@ -110,9 +117,15 @@ bool FlutterDesktopShutDownEngine(FlutterDesktopEngineRef engine_ref) {
return true;
}

FlutterDesktopWindowRef FlutterDesktopGetWindow(
FlutterDesktopWindowControllerRef controller) {
// The stub ignores this, so just return an arbitrary non-zero value.
return reinterpret_cast<FlutterDesktopWindowRef>(1);
}

FlutterDesktopPluginRegistrarRef FlutterDesktopGetPluginRegistrar(
FlutterDesktopWindowRef flutter_window,
FlutterDesktopWindowControllerRef controller,
const char* plugin_name) {
// The stub ignores this, so just return an arbitrary non-zero value.
return reinterpret_cast<FlutterDesktopPluginRegistrarRef>(1);
return reinterpret_cast<FlutterDesktopPluginRegistrarRef>(2);
}
24 changes: 14 additions & 10 deletions shell/platform/glfw/client_wrapper/testing/stub_flutter_glfw_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,27 @@ class StubFlutterGlfwApi {
virtual void Terminate() {}

// Called for FlutterDesktopCreateWindow.
virtual FlutterDesktopWindowRef 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) {
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) {
return nullptr;
}

// Called for FlutterDesktopSetHoverEnabled.
// Called for FlutterDesktopDestroyWindow
virtual void DestroyWindow() {}

// Called for FlutterDesktopWindowSetHoverEnabled.
virtual void SetHoverEnabled(bool enabled) {}

// Called for FlutterDesktopSetWindowTitle.
// Called for FlutterDesktopWindowSetTitle.
virtual void SetWindowTitle(const char* title) {}

// Called for FlutterDesktopSetWindowIcon.
// Called for FlutterDesktopWindowSetIcon.
virtual void SetWindowIcon(uint8_t* pixel_data, int width, int height) {}

// Called for FlutterDesktopRunWindowLoop.
Expand Down
Loading