Browse Source

getActiveDocumentComponent(), multiwindow support, share first context, use doc's navTo, inherit enable_shared_from_this, new window keyboard shortcut

master
Odilitime 5 years ago
parent
commit
1e034db408
  1. 133
      src/graphics/opengl/Window.cpp
  2. 11
      src/graphics/opengl/Window.h

133
src/graphics/opengl/Window.cpp

@ -16,11 +16,14 @@ @@ -16,11 +16,14 @@
#include "../components/TabbedComponent.h"
#include "../components/InputComponent.h"
#include "../opengl/Shader.h"
#include "../../browser.h"
#include <cmath>
#include <ctime>
#include <iostream>
extern std::unique_ptr<Browser> browser;
Window::~Window() {
glfwTerminate();
}
@ -82,7 +85,7 @@ bool Window::init() { @@ -82,7 +85,7 @@ bool Window::init() {
tabbedComponent->uiControl.w = { 100, 0 }; // 100%
tabbedComponent->uiControl.h = { 100, -64 }; // 100% - 64px
tabbedComponent->boundToPage = false;
tabbedComponent->win = this;
tabbedComponent->win = shared_from_this();
// add it to our components
tabbedComponent->setParent(rootComponent);
rootComponent->children.push_back(tabbedComponent);
@ -171,8 +174,12 @@ bool Window::init() { @@ -171,8 +174,12 @@ bool Window::init() {
navRefreshButton->name = "navRefreshButton";
navRefreshButton->boundToPage = false;
navRefreshButton->onClick=[this]() {
std::cout << "Refreshing " << this->currentURL << std::endl;
this->navTo(this->currentURL.toString());
std::shared_ptr<DocumentComponent> docComponent = this->getActiveDocumentComponent();
if (docComponent) {
std::cout << "Refreshing " << docComponent->currentURL << std::endl;
// now tell it to navigate somewhere
docComponent->navTo(docComponent->currentURL.toString());
}
};
navRefreshButton->onMouseover = [navRefreshButton, this]() {
//std::cout << "navRefreshButton->onMouseover" << std::endl;
@ -199,13 +206,32 @@ bool Window::init() { @@ -199,13 +206,32 @@ bool Window::init() {
navAddressBar->uiControl.h = { 0, 24 }; // 24px
navAddressBar->name = "navAddressBar";
navAddressBar->boundToPage = false;
navAddressBar->win = this;
navAddressBar->win = shared_from_this();
//navAddressBar->y = -48; // this works but breaks picking
navAddressBar->value = currentURL.toString();
navAddressBar->updateText();
//navAddressBar->value = currentURL.toString();
//navAddressBar->updateText();
navAddressBar->onEnter=[this](std::string value) {
std::cout << "navAddressBar::onEnter got " << value << std::endl;
this->navTo(value);
TabbedComponent *tabComponent = dynamic_cast<TabbedComponent*>(this->tabComponent.get());
if (tabComponent) {
if (!tabComponent->tabs.size()) {
tabComponent->addTab("Loading...");
tabComponent->selectTab(tabComponent->tabs.back());
this->renderDirty = true;
this->render(); // display loading tab before IO
}
}
std::shared_ptr<DocumentComponent> docComponent = this->getActiveDocumentComponent();
if (docComponent) {
std::cout << "Found an active document component" << std::endl;
// now tell it to navigate somewhere
docComponent->navTo(value);
} else {
std::cout << "No active document component" << std::endl;
}
};
this->addressComponent = navAddressBar;
@ -254,8 +280,13 @@ bool Window::initGLFW() { @@ -254,8 +280,13 @@ bool Window::initGLFW() {
glfwSetErrorCallback([](int error, const char* description) {
std::cout << "glfw error [" << error << "]:" << description << std::endl;
});
window = glfwCreateWindow(windowWidth, windowHeight, "NetRunner", nullptr, nullptr);
GLFWwindow *context = nullptr;
if (browser->windows.size()) {
context = browser->windows.front().get()->window;
}
// after title, it's monitor
window = glfwCreateWindow(windowWidth, windowHeight, "NetRunner", nullptr, context);
if (!window) {
glfwTerminate();
std::cout << "Could not create window" << std::endl;
@ -422,6 +453,11 @@ bool Window::initGLFW() { @@ -422,6 +453,11 @@ bool Window::initGLFW() {
glfwSetKeyCallback(window, [](GLFWwindow *win, int key, int scancode, int action, int mods) {
Window *thiz = reinterpret_cast<Window*>(glfwGetWindowUserPointer(win));
//std::cout << "Key " << key << " action: " << action << "mods: " << mods << std::endl;
if (key == GLFW_KEY_N && action == GLFW_RELEASE && mods == GLFW_MOD_CONTROL) {
browser->addWindow();
return;
}
// we're focused on something
//std::cout << "glfwSetKeyCallback" << std::endl;
if (thiz->focusedComponent) {
@ -645,16 +681,12 @@ void Window::onResize(int passedWidth, int passedHeight) { @@ -645,16 +681,12 @@ void Window::onResize(int passedWidth, int passedHeight) {
void Window::resize() {
//std::cout << "Window::resize" << std::endl;
const std::clock_t begin = clock();
//resizeComponentTree(rootComponent, windowWidth, windowHeight);
//this->printComponentTree(rootComponent, 0);
//this->buildUI();
const std::clock_t begin = clock();
rootComponent->windowWidth = windowWidth;
rootComponent->windowHeight = windowHeight;
rootComponent->layout();
const std::clock_t end = clock();
std::cout << "Window::resize - resized components in: " << std::fixed << ((static_cast<double>(end - begin)) / CLOCKS_PER_SEC) << std::scientific << " seconds" << std::endl;
//this->printComponentTree(rootComponent, 0);
@ -665,8 +697,6 @@ void Window::resize() { @@ -665,8 +697,6 @@ void Window::resize() {
transformMatrixDirty = true;
}
//thiz->printComponentTree(thiz->rootComponent, 0);
//printComponentTree(rootComponent, 0);
renderDirty = true;
}
@ -775,6 +805,7 @@ void Window::render() { @@ -775,6 +805,7 @@ void Window::render() {
glfwPollEvents();
}
/*
void Window::setDOM(const std::shared_ptr<Node> rootNode) {
// reset scroll position
@ -790,15 +821,16 @@ void Window::setDOM(const std::shared_ptr<Node> rootNode) { @@ -790,15 +821,16 @@ void Window::setDOM(const std::shared_ptr<Node> rootNode) {
TabbedComponent *tabbedComponent = dynamic_cast<TabbedComponent*>(tabComponent.get());
if (!tabbedComponent->tabs.size()) {
// if no tabs, make one
tabbedComponent->addTab(currentURL.toString());
tabbedComponent->addTab("New Tab");
//tabbedComponent->selectTab(tabbedComponent->tabs.back()->id);
tabbedComponent->selectTab(tabbedComponent->tabs.back());
}
tabbedComponent->loadDomIntoTab(rootNode, currentURL.toString());
}
*/
void Window::createComponentTree(const std::shared_ptr<Node> node, const std::shared_ptr<Component> &parentComponent) {
std::shared_ptr<Component> component = componentBuilder.build(node, parentComponent, windowWidth, windowHeight);
std::shared_ptr<Component> component = componentBuilder.build(node, parentComponent, shared_from_this());
// ComponentBuilder now calls setParent and setParents adds children
//component->setParent(parentComponent);
@ -902,6 +934,22 @@ std::shared_ptr<Component> Window::searchComponentTree(const std::shared_ptr<Com @@ -902,6 +934,22 @@ std::shared_ptr<Component> Window::searchComponentTree(const std::shared_ptr<Com
return nullptr;
}
std::shared_ptr<DocumentComponent> Window::getActiveDocumentComponent() {
// need to cast it first
TabbedComponent *p_tabComponent = dynamic_cast<TabbedComponent*>(this->tabComponent.get());
if (!p_tabComponent) {
std::cout << "Window::getActiveDocumentComponent - No active tab" << std::endl;
return nullptr;
}
std::shared_ptr<DocumentComponent> docComponent = std::static_pointer_cast<DocumentComponent>(p_tabComponent->documentComponent);
if (!docComponent) {
std::cout << "Window::getActiveDocumentComponent - No active document" << std::endl;
return nullptr;
}
return docComponent;
}
/*
// moving naviagtion closer to window, as window is now the owner of currentURL
// preparation for multiple HTML documents
void Window::navTo(std::string url) {
@ -915,46 +963,8 @@ void Window::navTo(std::string url) { @@ -915,46 +963,8 @@ void Window::navTo(std::string url) {
setWindowContent(currentURL);
}
/*
void handleRequest(const HTTPResponse &response) {
std::cout << "main:::handleRequest - statusCode: " << response.statusCode << std::endl;
if (response.statusCode == 200) {
const std::unique_ptr<HTMLParser> parser = std::make_unique<HTMLParser>();
const std::clock_t begin = clock();
std::shared_ptr<Node> rootNode = parser->parse(response.body);
const std::clock_t end = clock();
std::cout << "Parsed document in: " << std::fixed << ((static_cast<double>(end - begin)) / CLOCKS_PER_SEC) << std::scientific << " seconds" << std::endl;
window->setDOM(rootNode);
}
else if (response.statusCode == 301) {
std::string location;
if (response.properties.find("Location")==response.properties.end()) {
if (response.properties.find("location")==response.properties.end()) {
std::cout << "::handleRequest - got 301 without a location" << std::endl;
for(auto const &row : response.properties) {
std::cout << "::handleRequest - " << row.first << "=" << response.properties.at(row.first) << std::endl;
}
return;
} else {
location = response.properties.at("location");
}
} else {
location = response.properties.at("Location");
}
std::cout << "Redirect To: " << location << std::endl;
std::shared_ptr<URI> uri = parseUri(location);
const std::unique_ptr<HTTPRequest> request = std::make_unique<HTTPRequest>(uri);
request->sendRequest(handleRequest);
return;
}
else {
std::cout << "Unknown Status Code: " << response.statusCode << std::endl;
}
}
*/
// tried to make a window method
void handleRequest(const HTTPResponse &response) {
void handleRequest(std::shared_ptr<Window> tWin, const HTTPResponse &response) {
std::cout << "Window:::handleRequest - statusCode: " << response.statusCode << std::endl;
if (response.statusCode == 200) {
const std::unique_ptr<HTMLParser> parser = std::make_unique<HTMLParser>();
@ -962,7 +972,7 @@ void handleRequest(const HTTPResponse &response) { @@ -962,7 +972,7 @@ void handleRequest(const HTTPResponse &response) {
std::shared_ptr<Node> rootNode = parser->parse(response.body);
const std::clock_t end = clock();
std::cout << "Parsed document in: " << std::fixed << ((static_cast<double>(end - begin)) / CLOCKS_PER_SEC) << std::scientific << " seconds" << std::endl;
window->setDOM(rootNode);
tWin->setDOM(rootNode);
}
else if (response.statusCode == 301) {
std::string location;
@ -988,10 +998,13 @@ void handleRequest(const HTTPResponse &response) { @@ -988,10 +998,13 @@ void handleRequest(const HTTPResponse &response) {
}
std::unique_ptr<URL> uri = std::move(std::get<0>(result));
const std::unique_ptr<HTTPRequest> request = std::make_unique<HTTPRequest>(std::move(uri));
request->sendRequest(handleRequest);
request->sendRequest([tWin](const HTTPResponse &response2) {
handleRequest(tWin, response2);
});
return;
}
else {
std::cout << "Unknown Status Code: " << response.statusCode << std::endl;
}
}
*/

11
src/graphics/opengl/Window.h

@ -13,7 +13,9 @@ @@ -13,7 +13,9 @@
#include "../../networking/HTTPResponse.h"
#include "../../URL.h"
class Window {
class DocumentComponent;
class Window : public std::enable_shared_from_this<Window> {
private:
public:
~Window();
@ -33,7 +35,8 @@ public: @@ -33,7 +35,8 @@ public:
void resizeComponentTree(const std::shared_ptr<Component> &component, const int windowWidth, const int windowHeight);
std::shared_ptr<Component> searchComponentTree(const std::shared_ptr<Component> &component, const int x, const int y);
void navTo(std::string url);
std::shared_ptr<DocumentComponent> getActiveDocumentComponent();
//void navTo(std::string url);
// properties
int maxTextureSize = 0;
@ -68,12 +71,14 @@ public: @@ -68,12 +71,14 @@ public:
GLFWcursor* cursorHand;
GLFWcursor* cursorArrow;
GLFWcursor* cursorIbeam;
URL currentURL;
//URL currentURL;
ShaderLoader shaderLoader;
// is there only one selection per window? (probably not)
int highlightStartX = 0;
int highlightStartY = 0;
std::vector<std::shared_ptr<Component>> selectionList;
// multiwindow support
size_t id; // key for shader caches
};
bool setWindowContent(URL const& url);

Loading…
Cancel
Save