Browse Source

new browser object (holds a collection of windows)

master
Odilitime 5 years ago
parent
commit
81e0bc65c8
  1. 77
      src/browser.cpp
  2. 60
      src/browser.h

77
src/browser.cpp

@ -0,0 +1,77 @@ @@ -0,0 +1,77 @@
#include "browser.h"
#include "scheduler.h"
#include "graphics/components/TabbedComponent.h"
// why can't I const this?
std::unique_ptr<Scheduler> scheduler = std::make_unique<Scheduler>();
// Previously defined components with functionality
// 1+ image (no functionality)
// 1+ tabbed document
// 1+ back/fwd/reload/stop components
// 1+ address bar
Browser::Browser() {
}
void Browser::addWindow() {
windowCounter++;
std::shared_ptr<Window> newWindow = std::make_shared<Window>();
newWindow->id = windowCounter;
newWindow->windowWidth = 1024;
newWindow->windowHeight = 640;
newWindow->init(); // load our UI into it
this->windows.push_back(std::move(newWindow));
if (!activeWindow) {
std::cout << "Browser::addWindow - setting active window" << std::endl;
activeWindow = this->windows.back().get();
}
}
std::shared_ptr<DocumentComponent> Browser::getActiveDocumentComponent() {
if (!activeWindow) {
std::cout << "Browser::getActiveDocumentComponent - No active window" << std::endl;
return nullptr;
}
return this->activeWindow->getActiveDocumentComponent();
}
// FIXME: put quit check in here and clean up windows
void Browser::render() {
// guessing during render this iterator can be invalidated
size_t w = this->windows.size();
//for(std::vector<std::shared_ptr<Window>>::iterator it = this->windows.begin(); it != this->windows.end(); ++it) {
//it->get()->render();
// definitely safer
for(size_t i = 0; i < w; ++i) {
glfwMakeContextCurrent(this->windows[i]->window);
this->windows[i]->render();
}
}
void Browser::loop() {
bool shouldQuit = false;
while (!shouldQuit) {
//const std::clock_t begin = clock();
this->render();
scheduler->fireTimers(); // render may have taken some time
double next = scheduler->getNext();
//std::cout << "next timer at " << next << std::endl;
if (next == LONG_MAX) {
glfwWaitEvents(); // block until something changes
} else {
glfwWaitEventsTimeout(next / 1000);
}
scheduler->fireTimers(); // check before we go into render again
//glfwWaitEventsTimeout(1.0 / 60.0); // increase the cpu from 0% to 2% on idle
//const std::clock_t end = clock();
//std::cout << '\r' << std::fixed << (((static_cast<double>(end - begin)) / CLOCKS_PER_SEC) * 1000) << std::scientific << " ms/f " << std::flush;
// FIXME: one close shouldn't close the app
for(std::vector<std::shared_ptr<Window>>::iterator it = this->windows.begin(); it != this->windows.end(); ++it) {
if (glfwWindowShouldClose(it->get()->window)) {
shouldQuit = true;
break;
}
}
}
}

60
src/browser.h

@ -0,0 +1,60 @@ @@ -0,0 +1,60 @@
#ifndef BROWSER_H
#define BROWSER_H
#include "graphics/opengl/Window.h"
#include "graphics/components/Component.h"
#include "graphics/components/DocumentComponent.h"
#include "graphics/components/ComponentBuilder.h"
#include "html/Node.h"
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <memory>
#include <vector>
#include <algorithm>
#include "networking/HTTPResponse.h"
#include "URL.h"
#include "graphics/opengl/Window.h"
// this is the actually application that
// contains a list of windows
// separate window functions from browser functions
class Browser {
private:
public:
Browser();
void addWindow();
void render();
void loop();
std::shared_ptr<DocumentComponent> getActiveDocumentComponent();
void navTo(std::string url);
// properties
std::vector<std::shared_ptr<Window>> windows;
Window *activeWindow = nullptr;
size_t windowCounter = 0;
std::shared_ptr<Node> domRootNode = nullptr;
bool domDirty = false;
bool renderDirty = true;
//bool uiSetUp = false;
std::shared_ptr<Component> rootComponent = std::make_shared<Component>();
// I hate doing this but we currently require this
std::shared_ptr<Component> tabComponent = nullptr;
std::shared_ptr<Component> addressComponent = nullptr;
// could break these out in some sort of cursor class
// to minimize dependencies
std::shared_ptr<Component> focusedComponent = nullptr;
std::shared_ptr<Component> hoverComponent = nullptr;
URL currentURL;
};
bool setWindowContent(URL const& url);
void handleRequest(const HTTPResponse &response);
extern const std::unique_ptr<Window> window;
#endif
Loading…
Cancel
Save