Simplify native child OpenGL window creation.

This commit is contained in:
John Preston 2021-06-21 10:27:11 +04:00
parent bd989cb67f
commit 685db2ab5e
3 changed files with 147 additions and 0 deletions

View file

@ -69,6 +69,8 @@ PRIVATE
ui/gl/gl_shader.h
ui/gl/gl_surface.cpp
ui/gl/gl_surface.h
ui/gl/gl_window.cpp
ui/gl/gl_window.h
ui/image/image_prepare.cpp
ui/image/image_prepare.h
ui/layers/box_content.cpp

106
ui/gl/gl_window.cpp Normal file
View file

@ -0,0 +1,106 @@
// This file is part of Desktop App Toolkit,
// a set of libraries for developing nice desktop applications.
//
// For license and copyright information please follow this link:
// https://github.com/desktop-app/legal/blob/master/LEGAL
//
#include "ui/gl/gl_window.h"
#include "ui/gl/gl_detection.h"
#include "ui/widgets/window.h"
#include "base/platform/base_platform_info.h"
#include "base/debug_log.h"
namespace Ui::GL {
namespace {
constexpr auto kUseNativeChild = ::Platform::IsWindows();
} // namespace
Window::Window()
: _window(std::make_unique<Ui::Window>())
, _bodyNativeWrap(createNativeBodyWrap())
, _body(_bodyNativeWrap ? _bodyNativeWrap.get() : _window->body().get()) {
}
Window::~Window() = default;
Backend Window::backend() const {
return _backend;
}
not_null<Ui::Window*> Window::window() const {
return _window.get();
}
not_null<Ui::RpWidget*> Window::widget() const {
return _body.get();
}
std::unique_ptr<Ui::Window> Window::createWindow() {
auto result = std::make_unique<Ui::Window>();
if constexpr (!kUseNativeChild) {
const auto capabilities = Ui::GL::CheckCapabilities(result.get());
const auto use = ::Platform::IsMac()
? true
: ::Platform::IsWindows()
? capabilities.supported
: capabilities.transparency;
LOG(("OpenGL: %1 (Ui::GL::Window)").arg(use ? "[TRUE]" : "[FALSE]"));
_backend = use ? Ui::GL::Backend::OpenGL : Ui::GL::Backend::Raster;
if (!use) {
// We have to create a new window, if OpenGL initialization failed.
result = std::make_unique<Ui::Window>();
}
}
return result;
}
std::unique_ptr<Ui::RpWidget> Window::createNativeBodyWrap() {
if constexpr (!kUseNativeChild) {
return nullptr;
}
const auto create = [] {
auto result = std::make_unique<Ui::RpWidget>();
result->setWindowFlags(Qt::FramelessWindowHint | Qt::Window);
result->setAttribute(Qt::WA_NativeWindow);
result->setAttribute(Qt::WA_DontCreateNativeAncestors);
result->setAttribute(Qt::WA_OpaquePaintEvent);
result->setAttribute(Qt::WA_NoSystemBackground);
return result;
};
auto result = create();
const auto capabilities = Ui::GL::CheckCapabilities(result.get());
const auto use = ::Platform::IsMac()
? true
: ::Platform::IsWindows()
? capabilities.supported
: capabilities.transparency;
LOG(("OpenGL: %1 (Ui::GL::WindowBody)").arg(use ? "[TRUE]" : "[FALSE]"));
_backend = use ? Ui::GL::Backend::OpenGL : Ui::GL::Backend::Raster;
if (!use) {
// We have to create a new window, if OpenGL initialization failed.
result = create();
}
const auto nativeParent = _window->body();
nativeParent->setAttribute(Qt::WA_OpaquePaintEvent);
nativeParent->setAttribute(Qt::WA_NoSystemBackground);
const auto raw = result.get();
raw->setParent(nativeParent);
raw->show();
raw->update();
_window->sizeValue(
) | rpl::start_with_next([=](QSize size) {
raw->setGeometry(QRect(QPoint(), size));
}, raw->lifetime());
return result;
}
} // namespace Ui::GL

39
ui/gl/gl_window.h Normal file
View file

@ -0,0 +1,39 @@
// This file is part of Desktop App Toolkit,
// a set of libraries for developing nice desktop applications.
//
// For license and copyright information please follow this link:
// https://github.com/desktop-app/legal/blob/master/LEGAL
//
#pragma once
namespace Ui {
class Window;
class RpWidget;
} // namespace Ui
namespace Ui::GL {
enum class Backend;
class Window final {
public:
Window();
~Window();
[[nodiscard]] Backend backend() const;
[[nodiscard]] not_null<Ui::Window*> window() const;
[[nodiscard]] not_null<Ui::RpWidget*> widget() const;
private:
[[nodiscard]] std::unique_ptr<Ui::Window> createWindow();
[[nodiscard]] std::unique_ptr<Ui::RpWidget> createNativeBodyWrap();
Ui::GL::Backend _backend = Ui::GL::Backend();
const std::unique_ptr<Ui::Window> _window;
const std::unique_ptr<Ui::RpWidget> _bodyNativeWrap;
const not_null<Ui::RpWidget*> _body;
};
} // namespace Ui::GL