Add window title/icon support to GLFW shell (flutter/engine#8435)

The GLFW shell used a hard-coded title and default icon for the window,
but these are things that a client should be able to control.
This commit is contained in:
stuartmorgan
2019-04-04 13:54:07 -04:00
committed by GitHub
parent ab3af9a682
commit 783b5003c0
6 changed files with 86 additions and 9 deletions

View File

@@ -24,6 +24,7 @@ FlutterWindowController::~FlutterWindowController() {
bool FlutterWindowController::CreateWindow(
int width,
int height,
const std::string& title,
const std::string& assets_path,
const std::vector<std::string>& arguments) {
if (!init_succeeded_) {
@@ -44,7 +45,7 @@ bool FlutterWindowController::CreateWindow(
size_t arg_count = engine_arguments.size();
window_ = FlutterDesktopCreateWindow(
width, height, assets_path.c_str(), icu_data_path_.c_str(),
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_) {
std::cerr << "Failed to create window." << std::endl;
@@ -68,6 +69,16 @@ 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);
}
void FlutterWindowController::RunEventLoop() {
if (window_) {
FlutterDesktopRunWindowLoop(window_);

View File

@@ -46,6 +46,7 @@ class FlutterWindowController {
// Only one Flutter window can exist at a time; see constructor comment.
bool CreateWindow(int width,
int height,
const std::string& title,
const std::string& assets_path,
const std::vector<std::string>& arguments);
@@ -63,6 +64,16 @@ class FlutterWindowController {
// 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);
// Loops on Flutter window events until the window closes.
void RunEventLoop();

View File

@@ -50,14 +50,15 @@ 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) {
if (s_stub_implementation) {
return s_stub_implementation->CreateWindow(initial_width, initial_height,
assets_path, icu_data_path,
arguments, argument_count);
return s_stub_implementation->CreateWindow(
initial_width, initial_height, title, assets_path, icu_data_path,
arguments, argument_count);
}
return nullptr;
}
@@ -69,6 +70,22 @@ void FlutterDesktopSetHoverEnabled(FlutterDesktopWindowRef flutter_window,
}
}
void FlutterDesktopSetWindowTitle(FlutterDesktopWindowRef flutter_window,
const char* title) {
if (s_stub_implementation) {
s_stub_implementation->SetWindowTitle(title);
}
}
void FlutterDesktopSetWindowIcon(FlutterDesktopWindowRef flutter_window,
uint8_t* pixel_data,
int width,
int height) {
if (s_stub_implementation) {
s_stub_implementation->SetWindowIcon(pixel_data, width, height);
}
}
void FlutterDesktopRunWindowLoop(FlutterDesktopWindowRef flutter_window) {
if (s_stub_implementation) {
s_stub_implementation->RunWindowLoop();

View File

@@ -38,6 +38,7 @@ class StubFlutterGlfwApi {
// 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,
@@ -45,9 +46,15 @@ class StubFlutterGlfwApi {
return nullptr;
}
// Called for FlutterDesktopSetHoverEnabled
// Called for FlutterDesktopSetHoverEnabled.
virtual void SetHoverEnabled(bool enabled) {}
// Called for FlutterDesktopSetWindowTitle.
virtual void SetWindowTitle(const char* title) {}
// Called for FlutterDesktopSetWindowIcon.
virtual void SetWindowIcon(uint8_t* pixel_data, int width, int height) {}
// Called for FlutterDesktopRunWindowLoop.
virtual void RunWindowLoop() {}

View File

@@ -94,8 +94,6 @@ struct FlutterDesktopMessenger {
shell::IncomingMessageDispatcher* dispatcher;
};
static constexpr char kDefaultWindowTitle[] = "Flutter";
// Retrieves state bag for the window in question from the GLFWWindow.
static FlutterDesktopWindowState* GetSavedWindowState(GLFWwindow* window) {
return reinterpret_cast<FlutterDesktopWindowState*>(
@@ -473,6 +471,7 @@ 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,
@@ -481,8 +480,8 @@ FlutterDesktopWindowRef FlutterDesktopCreateWindow(int initial_width,
gtk_init(0, nullptr);
#endif
// Create the window.
auto window = glfwCreateWindow(initial_width, initial_height,
kDefaultWindowTitle, NULL, NULL);
auto window =
glfwCreateWindow(initial_width, initial_height, title, NULL, NULL);
if (window == nullptr) {
return nullptr;
}
@@ -543,6 +542,21 @@ void FlutterDesktopSetHoverEnabled(FlutterDesktopWindowRef flutter_window,
SetHoverCallbacksEnabled(flutter_window->window, enabled);
}
void FlutterDesktopSetWindowTitle(FlutterDesktopWindowRef flutter_window,
const char* title) {
GLFWwindow* window = flutter_window->window;
glfwSetWindowTitle(window, title);
}
void FlutterDesktopSetWindowIcon(FlutterDesktopWindowRef flutter_window,
uint8_t* pixel_data,
int width,
int height) {
GLFWwindow* window = flutter_window->window;
GLFWimage image = {width, height, static_cast<unsigned char*>(pixel_data)};
glfwSetWindowIcon(window, pixel_data ? 1 : 0, &image);
}
void FlutterDesktopRunWindowLoop(FlutterDesktopWindowRef flutter_window) {
GLFWwindow* window = flutter_window->window;
#ifdef __linux__

View File

@@ -52,6 +52,7 @@ FLUTTER_EXPORT void FlutterDesktopTerminate();
FLUTTER_EXPORT FlutterDesktopWindowRef
FlutterDesktopCreateWindow(int initial_width,
int initial_height,
const char* title,
const char* assets_path,
const char* icu_data_path,
const char** arguments,
@@ -66,6 +67,22 @@ FLUTTER_EXPORT void FlutterDesktopSetHoverEnabled(
FlutterDesktopWindowRef flutter_window,
bool enabled);
// Sets the displayed title for |flutter_window|.
FLUTTER_EXPORT void FlutterDesktopSetWindowTitle(
FlutterDesktopWindowRef flutter_window,
const char* title);
// Sets the displayed icon for |flutter_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.
FLUTTER_EXPORT void FlutterDesktopSetWindowIcon(
FlutterDesktopWindowRef flutter_window,
uint8_t* pixel_data,
int width,
int height);
// Loops on Flutter window events until the window is closed.
//
// Once this function returns, FlutterDesktopWindowRef is no longer valid, and