Mosaic: motore di rendering proprio, gusci desktop e Sailfish
Primo commit del progetto: motore Rust con parser HTML, cascata CSS, layout, raster CPU, formulari, bidi, flex, tabelle, JavaScript (Boa) con eventi e costruzione di nodi, rete HTTP/HTTPS con TLS, sotto-risorse, invio dei formulari; guscio grafico per Sailfish (QML/Silica) e per desktop (minifb, distribuito come AppImage), con 11 documenti di specifica e 91 casi di reftest.
This commit is contained in:
commit
577682ae22
173 files changed
+23685
No files matched your search
@@ -0,0 +1,150 @@
|
||||
#ifndef ENGINEVIEW_H
|
||||
#define ENGINEVIEW_H
|
||||
|
||||
#include <QHash>
|
||||
#include <QImage>
|
||||
#include <QQuickPaintedItem>
|
||||
#include <QString>
|
||||
#include <QVariantList>
|
||||
|
||||
#include <atomic>
|
||||
#include <memory>
|
||||
|
||||
/* Elemento QML che presenta il raster prodotto dal motore.
|
||||
*
|
||||
* `zoom` è il fattore CSS -> dispositivo: il motore impagina con un viewport CSS
|
||||
* largo `width() / zoom` e rasterizza a `zoom`, quindi il testo resta nitido
|
||||
* (si scala la geometria, non l'immagine). Con l'altezza automatica l'immagine
|
||||
* copre tutto il documento e il guscio la scorre.
|
||||
*
|
||||
* Il motore è sincrono e veloce su documenti piccoli: M0 lo chiama sul thread
|
||||
* grafico. Con la rete (M1) la chiamata passerà a un thread dedicato con
|
||||
* consegna via coda: la superficie QML non cambia.
|
||||
*/
|
||||
class EngineView : public QQuickPaintedItem
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(QString html READ html WRITE setHtml NOTIFY htmlChanged)
|
||||
Q_PROPERTY(QString extraCss READ extraCss WRITE setExtraCss NOTIFY extraCssChanged)
|
||||
Q_PROPERTY(QString lastError READ lastError NOTIFY lastErrorChanged)
|
||||
Q_PROPERTY(int renderMs READ renderMs NOTIFY renderMsChanged)
|
||||
Q_PROPERTY(qreal zoom READ zoom WRITE setZoom NOTIFY zoomChanged)
|
||||
Q_PROPERTY(int contentHeight READ contentHeight NOTIFY contentChanged)
|
||||
Q_PROPERTY(int contentWidth READ contentWidth NOTIFY contentChanged)
|
||||
/// chiave del controllo che ha il fuoco ("" se nessuno)
|
||||
Q_PROPERTY(QString focusedKey READ focusedKey NOTIFY focusedKeyChanged)
|
||||
/// controlli del documento: [{key, kind, x, y, w, h}] in pixel CSS
|
||||
Q_PROPERTY(QVariantList controls READ controls NOTIFY controlsChanged)
|
||||
/// indirizzo dell'ultima richiesta ("" se il contenuto è locale)
|
||||
Q_PROPERTY(QString url READ url NOTIFY urlChanged)
|
||||
/// 0 fermo · 1 in caricamento · 2 riuscito · 3 errore
|
||||
Q_PROPERTY(int loadState READ loadState NOTIFY loadStateChanged)
|
||||
/// messaggio dell'ultimo errore di rete
|
||||
Q_PROPERTY(QString networkError READ networkError NOTIFY loadStateChanged)
|
||||
|
||||
public:
|
||||
explicit EngineView(QQuickItem *parent = nullptr);
|
||||
~EngineView() override;
|
||||
|
||||
void paint(QPainter *painter) override;
|
||||
|
||||
QString html() const { return m_html; }
|
||||
void setHtml(const QString &html);
|
||||
|
||||
QString extraCss() const { return m_css; }
|
||||
void setExtraCss(const QString &css);
|
||||
|
||||
QString lastError() const { return m_error; }
|
||||
int renderMs() const { return m_renderMs; }
|
||||
|
||||
qreal zoom() const { return m_zoom; }
|
||||
void setZoom(qreal zoom);
|
||||
|
||||
int contentHeight() const { return m_image.isNull() ? 0 : m_image.height(); }
|
||||
int contentWidth() const { return m_image.isNull() ? 0 : m_image.width(); }
|
||||
|
||||
/// Scarica l'indirizzo e lo impagina. La richiesta gira su un thread a parte:
|
||||
/// il thread grafico non si blocca e la finestra resta viva durante l'attesa.
|
||||
Q_INVOKABLE void loadUrl(const QString &url);
|
||||
/// come `loadUrl`, ma solo se l'indirizzo è http/https
|
||||
QString url() const { return m_url; }
|
||||
int loadState() const { return m_loadState; }
|
||||
QString networkError() const { return m_networkError; }
|
||||
|
||||
/// Chiave del controllo sotto il punto indicato (pixel CSS), "" se nessuno.
|
||||
Q_INVOKABLE QString controlAt(qreal cssX, qreal cssY) const;
|
||||
Q_INVOKABLE QString kindOf(const QString &key) const;
|
||||
Q_INVOKABLE QString value(const QString &key) const;
|
||||
/// vero se il controllo ha un valore già impostato dal guscio (anche vuoto)
|
||||
Q_INVOKABLE bool hasValue(const QString &key) const { return m_values.contains(key); }
|
||||
/// testo mostrato dal controllo (valore dell'HTML se non è stato modificato)
|
||||
Q_INVOKABLE QString textOf(const QString &key) const;
|
||||
Q_INVOKABLE void setValue(const QString &key, const QString &value);
|
||||
Q_INVOKABLE void setFocus(const QString &key);
|
||||
Q_INVOKABLE void toggle(const QString &key);
|
||||
/// posizione del cursore (caratteri, -1 per nasconderlo) e intervallo selezionato
|
||||
Q_INVOKABLE void setCaret(int pos);
|
||||
Q_INVOKABLE void setSelection(int inizio, int fine);
|
||||
Q_INVOKABLE int caret() const { return m_caret; }
|
||||
/// apre/chiude l'elenco di un `select` ("" per chiudere)
|
||||
Q_INVOKABLE void setOpenSelect(const QString &key);
|
||||
/// sceglie l'opzione `index` del `select` indicato e chiude l'elenco
|
||||
Q_INVOKABLE void chooseOption(const QString &key, int index);
|
||||
Q_INVOKABLE bool isSelectOpen(const QString &key) const { return m_openSelect == key; }
|
||||
/// inverte l'apertura dell'elenco: un solo posto tiene lo stato (qui, non in QML)
|
||||
Q_INVOKABLE void toggleSelect(const QString &key);
|
||||
|
||||
public slots:
|
||||
/// consegna del caricamento di rete, sempre sul thread grafico
|
||||
void fetchDone(const QString &html, const QString &errore, int stato);
|
||||
|
||||
signals:
|
||||
void focusedKeyChanged();
|
||||
void controlsChanged();
|
||||
void htmlChanged();
|
||||
void extraCssChanged();
|
||||
void lastErrorChanged();
|
||||
void renderMsChanged();
|
||||
void zoomChanged();
|
||||
void contentChanged();
|
||||
|
||||
protected:
|
||||
void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) override;
|
||||
|
||||
signals:
|
||||
void urlChanged();
|
||||
void loadStateChanged();
|
||||
|
||||
private:
|
||||
QString m_url;
|
||||
int m_loadState = 0;
|
||||
QString m_networkError;
|
||||
/// vivo finché l'oggetto esiste: un thread in corso lo controlla prima di consegnare
|
||||
std::shared_ptr<std::atomic<bool>> m_vivo;
|
||||
void rerender();
|
||||
void setError(const QString &error);
|
||||
QString resolveFont() const;
|
||||
|
||||
QImage m_image;
|
||||
QString m_html;
|
||||
QString m_css;
|
||||
QString m_error;
|
||||
QString m_fontPath;
|
||||
/// cartella per risolvere i `src` relativi delle immagini
|
||||
QString m_baseDir;
|
||||
qreal m_zoom = 2.0;
|
||||
int m_renderMs = 0;
|
||||
/// valori digitati nei moduli e chiave con il fuoco
|
||||
QHash<QString, QString> m_values;
|
||||
QString m_focus;
|
||||
int m_caret = -1;
|
||||
int m_selectionStart = -1;
|
||||
int m_selectionEnd = -1;
|
||||
QString m_openSelect;
|
||||
QVariantList m_controls;
|
||||
QString stateJson() const;
|
||||
QString focusedKey() const { return m_focus; }
|
||||
QVariantList controls() const { return m_controls; }
|
||||
};
|
||||
|
||||
#endif // ENGINEVIEW_H
|
||||
Reference in new issue
Block a user