More events, remove most of the statics, better shutdown

This commit is contained in:
Paul Rouget 2019-07-24 15:14:24 +02:00
parent 7adf022dfa
commit d0436f16b6
8 changed files with 342 additions and 217 deletions

View file

@ -5,37 +5,73 @@
#pragma once
#include "pch.h"
#include "logs.h"
namespace servo {
namespace capi {
extern "C" {
#include <simpleservo.h>
}
} // namespace capi
class ServoDelegate {
public:
// Called from any thread
virtual void WakeUp() = 0;
// Called from GL thread
virtual void OnLoadStarted() = 0;
virtual void OnLoadEnded() = 0;
virtual void OnHistoryChanged(bool, bool) = 0;
virtual void OnShutdownComplete() = 0;
virtual void OnTitleChanged(std::wstring) = 0;
virtual void OnAlert(std::wstring) = 0;
virtual void OnURLChanged(std::wstring) = 0;
virtual void Flush() = 0;
virtual void MakeCurrent() = 0;
virtual bool OnAllowNavigation(std::wstring) = 0;
virtual void OnAnimatingChanged(bool) = 0;
protected:
virtual ~ServoDelegate(){log("A1");};
};
class Servo {
public:
Servo(GLsizei width, GLsizei height);
Servo(GLsizei, GLsizei, ServoDelegate &);
~Servo();
void PerformUpdates();
void Click(float, float);
void Scroll(float dx, float dy, float x, float y);
void SetSize(GLsizei width, GLsizei height);
void SetBatchMode(bool);
void GoBack();
void GoForward();
ServoDelegate &Delegate() { return mDelegate; }
// Static lambas called by Servo callbacks.
// Will be called from any thead
static std::function<void()> sWakeUp;
// Will be called from GL thread
static std::function<void()> sFlush;
static std::function<void()> sMakeCurrent;
static std::function<void(std::wstring const &)> sOnAlert;
static std::function<void(std::wstring const &)> sOnTitleChanged;
static std::function<void(std::wstring const &)> sOnURLChanged;
static bool sAnimating;
void PerformUpdates() { capi::perform_updates(); }
void RequestShutdown() { capi::request_shutdown(); }
void SetBatchMode(bool mode) { capi::set_batch_mode(mode); }
void GoForward() { capi::go_forward(); }
void GoBack() { capi::go_back(); }
void Click(float x, float y) { capi::click(x, y); }
void Reload() { capi::reload(); }
void Stop() { capi::stop(); }
void Scroll(float dx, float dy, float x, float y) {
capi::scroll(dx, dy, x, y);
}
void SetSize(GLsizei width, GLsizei height) {
if (width != mWindowWidth || height != mWindowHeight) {
mWindowWidth = width;
mWindowHeight = height;
capi::resize(mWindowWidth, mWindowHeight);
}
}
private:
ServoDelegate &mDelegate;
GLsizei mWindowWidth;
GLsizei mWindowHeight;
bool mAnimating;
};
// This is sad. We need a static pointer to Servo because we use function
// pointer as callback in Servo, and these functions need a way to get
// the Servo instance. See https://github.com/servo/servo/issues/22967
static Servo *sServo = nullptr;
std::wstring char2w(const char *c_str);
} // namespace servo