Skip to content

Facefull bridge interface

NickWare edited this page May 6, 2022 · 4 revisions

Facefull bridge is a communication system which connects application native backend (C++/Java/etc...) and HTML5 user interface (runs through webview component).

Event message structure

Event coming from a webview component is a string that contain eventname and data fields in format: eventname|data. Facefull uses document.title to send events to the application backend. So you need to capture the webview title change in your native code.

To send an event from native code to Facefull UI you need to run JS function facefull.doEventHandle(eventname, data); using webview's RunScript method.

Bridge interface sample

C++

facefullbridgeinterface.cpp:

#include "facefullbridgeinterface.h"

FacefullBridgeInterface::FacefullBridgeInterface() {
    WindowReady = false;
    doEventAttach("doWindowReady", [this](auto data) {
        WindowReady = true;
        onWindowReady();
    });
    doEventAttach("doWindowMove", [this](auto data) {
        onWindowMove();
    });
    doEventAttach("doWindowMaximize", [this](auto data) {
        onWindowMaximize();
    });
    doEventAttach("doWindowMinimize", [this](auto data) {
        onWindowMinimize();
    });
    doEventAttach("doWindowClose", [this](auto data) {
        onWindowClose();
    });

}

void FacefullBridgeInterface::doEventCatch(const std::string &event) {
    if (event.empty()) return;
    size_t p;
    std::string name, data;
    if ((p = event.find('|')) >= 0) {
        name = event.substr(0, p);
        data = event.substr(p+1);
    }
    if (name.empty() || name == "0") return;
    auto R = Events.equal_range(name);
    for (auto it = R.first; it != R.second; ++it) {
        EventHandler handler = it->second;
        handler(data);
    }
}

void FacefullBridgeInterface::doEventAttach(const std::string& name, EventHandler function) {
    Events.insert(std::pair<std::string, EventHandler>(name, function));
}

void FacefullBridgeInterface::doEventSend(const std::string &name, const std::string &data) {
    if (WindowReady) WebViewEventExecutor("facefull.doEventHandle('"+name+"', '"+data+"');");
}

bool FacefullBridgeInterface::isWindowReady() const {
    return WindowReady;
}

facefullbridgeinterface.h:

#ifndef FACEFULLBRIDGEINTERFACE_H
#define FACEFULLBRIDGEINTERFACE_H

#include <string>
#include <map>
#include <functional>

class FacefullBridgeInterface {
public:
    typedef std::function<void(const std::string&)> EventHandler;
    FacefullBridgeInterface();
    void doEventAttach(const std::string& eventname, EventHandler function);
    void doEventSend(const std::string &eventname, const std::string &data = "");
    bool isWindowReady() const;
    ~FacefullBridgeInterface() = default;
private:
    std::multimap<std::string, EventHandler> Events;
    bool WindowReady;
protected:
    virtual void onWindowReady() = 0;
    virtual void onWindowMove() = 0;
    virtual void onWindowMaximize() = 0;
    virtual void onWindowMinimize() = 0;
    virtual void onWindowClose() = 0;
    void doEventCatch(const std::string&);
    virtual void WebViewEventExecutor(const std::string&) = 0;
};


#endif //FACEFULLBRIDGEINTERFACE_H

Bridge implementation sample

C++

facefullbridge.cpp:

#include "facefullbridge.h"

FacefullBridge::FacefullBridge() {
    // bind FacefullBridge::onEventReceive method to webview title change event
}

void FacefullBridge::onEventReceive() {
    // get webview page title and catch Facefull event by doEventCatch(title);
}

void FacefullBridge::onWindowMaximize() {
    // maximize the window
}

void FacefullBridge::onWindowMinimize() {
    // minimize the window
}

void FacefullBridge::onWindowMove() {
    // move the window when not maximized
}

void FacefullBridge::WebViewEventExecutor(const std::string &str) {
    // something like webview->RunScript(str);
}

void FacefullBridge::onWindowReady() {
    // show the window
}

void FacefullBridge::onWindowClose() {
    // hide the window and exit app
}

facefullbridge.h:

#ifndef FACEFULLBRIDGE_H
#define FACEFULLBRIDGE_H

#include "facefullbridgeinterface.h"

class FacefullBridge : public FacefullBridgeInterface {
public:
    FacefullBridge();
    void onEventReceive();
    void onWindowMinimize() override;
    void onWindowMaximize() override;
    void onWindowMove() override;
    void WebViewEventExecutor(const std::string&) override;
    void onWindowReady() override;
    void onWindowClose() override;
};


#endif //FACEFULLBRIDGE_H

Clone this wiki locally