Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chart onChange and rendering connection moved to TS API #509

Merged
merged 1 commit into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions src/apps/weblib/cinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,10 @@ void vizzu_update(APIHandles::Chart chart,
double width,
double height,
double timeInMSecs,
int renderControl)
bool render)
{
return Interface::getInstance().update(chart,
canvas,
width,
height,
timeInMSecs,
static_cast<Interface::RenderControl>(renderControl));
return Interface::getInstance()
.update(chart, canvas, width, height, timeInMSecs, render);
}

const char *style_getList() { return Interface::getStyleList(); }
Expand Down
2 changes: 1 addition & 1 deletion src/apps/weblib/cinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ extern void vizzu_update(APIHandles::Chart chart,
double width,
double height,
double timeInMSecs,
int renderControl);
bool render);
extern const char *vizzu_errorMessage(
APIHandles::Exception exceptionPtr,
const std::type_info *typeinfo);
Expand Down
20 changes: 13 additions & 7 deletions src/apps/weblib/interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,12 +329,19 @@ ObjectRegistry::Handle Interface::createChart()
{
auto &&widget = std::make_shared<UI::ChartWidget>();

widget->openUrl = [&](const std::string &url)
auto handle = objects.reg(std::move(widget));

widget->openUrl = [handle](const std::string &url)
{
::chart_openUrl(handle, url.c_str());
};

widget->doChange = [handle]()
{
::openUrl(url.c_str());
::chart_doChange(handle);
};

return objects.reg(std::move(widget));
return handle;
}

ObjectRegistry::Handle Interface::createCanvas()
Expand All @@ -353,7 +360,7 @@ void Interface::update(ObjectRegistry::Handle chart,
double width,
double height,
double timeInMSecs,
RenderControl renderControl)
bool render)
{
auto &&widget = objects.get<UI::ChartWidget>(chart);

Expand All @@ -367,9 +374,8 @@ void Interface::update(ObjectRegistry::Handle chart,

widget->getChart().getAnimControl().update(time);

if (const Geom::Size size{width, height};
renderControl == force
|| (renderControl == allow && widget->needsUpdate(size))) {
if (render) {
const Geom::Size size{width, height};
auto ptr = objects.get<Vizzu::Main::JScriptCanvas>(canvas);
ptr->frameBegin();
widget->onUpdateSize(size);
Expand Down
4 changes: 1 addition & 3 deletions src/apps/weblib/interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ class Interface
public:
static Interface &getInstance();

enum RenderControl { allow = 0, force = 1, inhibit = 2 };

Interface();
static const char *version();
ObjectRegistry::Handle createChart();
Expand Down Expand Up @@ -48,7 +46,7 @@ class Interface
double width,
double height,
double timeInMSecs,
RenderControl renderControl);
bool render);

ObjectRegistry::Handle storeAnim(ObjectRegistry::Handle chart);
void restoreAnim(ObjectRegistry::Handle chart,
Expand Down
7 changes: 5 additions & 2 deletions src/apps/weblib/interface.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
mergeInto(LibraryManager.library, {
openUrl: function (url) {
window.open(UTF8ToString(url), '_blank')
chart_openUrl: function (chart, url) {
Module.charts[chart].openUrl(url)
},
chart_doChange: function (chart) {
Module.charts[chart].doChange()
},
textBoundary: function (font, text, sizeX, sizeY) {
const dc = Module.measureCanvas
Expand Down
3 changes: 2 additions & 1 deletion src/apps/weblib/interfacejs.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
#define INTERFACEJS_H

extern "C" {
extern void openUrl(const char *);
extern void chart_openUrl(const void *, const char *);
extern void chart_doChange(const void *);
extern void
textBoundary(const char *, const char *, double *, double *);
}
Expand Down
29 changes: 20 additions & 9 deletions src/apps/weblib/ts-api/chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as Config from './types/config.js'
import * as Styles from './types/styles.js'
import * as D from './types/data.js'
import { Module } from './module/module.js'
import { Chart as ChartInterface } from './module/chart.js'
import { CChart, Snapshot } from './module/cchart.js'
import { CAnimControl, CAnimation } from './module/canimctrl.js'
import { CData } from './module/cdata.js'
Expand All @@ -26,7 +27,7 @@ import { Scheduler } from './plugins/scheduler.js'
import { RenderControl } from './plugins/rendercontrol.js'
import { CCanvas } from './module/ccanvas.js'

export class Chart {
export class Chart implements ChartInterface {
private _options: VizzuOptions
private _cChart: CChart
private _cCanvas: CCanvas
Expand All @@ -35,13 +36,16 @@ export class Chart {
private _data: Data
private _events: Events
private _plugins: PluginRegistry
private _needsUpdate = true

constructor(module: Module, options: VizzuOptions, plugins: PluginRegistry) {
this._options = options
this._plugins = plugins
this._module = module

this._cChart = this._module.createChart()
this._module.registerChart(this._cChart, this)

this._cCanvas = this._module.createCanvas()
this._cData = this._module.getData(this._cChart)
this._data = new Data(this._cData)
Expand All @@ -65,6 +69,10 @@ export class Chart {
this._plugins.register(new Tooltip(), false)
}

detach(): void {
this._module.unregisterChart(this._cChart)
}

start(): void {
const ctx = {
update: (force: boolean): void => this.updateFrame(force)
Expand All @@ -84,21 +92,24 @@ export class Chart {
}
this._plugins.hook(Hooks.render, ctx).default((ctx) => {
if (ctx.size.x >= 1 && ctx.size.y >= 1 && ctx.timeInMSecs !== null && ctx.renderer) {
const renderControl = !ctx.enable ? 2 : ctx.force ? 1 : 0
const render = ctx.force || (ctx.enable && this._needsUpdate)
ctx.renderer.canvas = this._cCanvas
this._module.registerRenderer(this._cCanvas, ctx.renderer)
this._cChart.update(
this._cCanvas,
ctx.size.x,
ctx.size.y,
ctx.timeInMSecs,
renderControl
)
this._cChart.update(this._cCanvas, ctx.size.x, ctx.size.y, ctx.timeInMSecs, render)
this._module.unregisterRenderer(this._cCanvas)
this._needsUpdate = false
}
})
}

doChange(): void {
this._needsUpdate = true
}

openUrl(url: number): void {
window.open(this._cChart.getString(url), '_blank')
}

async prepareAnimation(
target: Anim.Keyframes | CAnimation,
options?: Anim.ControlOptions & Anim.Options
Expand Down
6 changes: 4 additions & 2 deletions src/apps/weblib/ts-api/cvizzu.types.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Canvas } from './module/canvas'
import { type Canvas } from './module/canvas'
import { type Chart } from './module/chart'

export type CPointer = number
export type CString = CPointer
Expand Down Expand Up @@ -46,6 +47,7 @@ export interface ModuleOptions {
export interface CVizzu {
// decorations
canvases: { [key: CPointer]: Canvas }
charts: { [key: CPointer]: Chart }
measureCanvas: CanvasRenderingContext2D

// members
Expand Down Expand Up @@ -95,7 +97,7 @@ export interface CVizzu {
width: number,
height: number,
time: number,
renderControl: number
render: boolean
): void
_vizzu_errorMessage(exceptionPtr: CException, typeinfo: CTypeInfo): CString
_vizzu_version(): CString
Expand Down
14 changes: 6 additions & 8 deletions src/apps/weblib/ts-api/module/cchart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,9 @@ export class CChart extends CObject {
this.animOptions = this._makeAnimOptions()
}

update(
cCanvas: CCanvas,
width: number,
height: number,
time: number,
renderControl: number
): void {
update(cCanvas: CCanvas, width: number, height: number, time: number, render: boolean): void {
this._cCanvas = cCanvas
this._call(this._wasm._vizzu_update)(cCanvas.getId(), width, height, time, renderControl)
this._call(this._wasm._vizzu_update)(cCanvas.getId(), width, height, time, render)
}

animate(callback: (ok: boolean) => void): void {
Expand Down Expand Up @@ -127,6 +121,10 @@ export class CChart extends CObject {
this._call(this._wasm._vizzu_wheel)(this._cCanvas.getId(), delta)
}

getString(text: CString): string {
return this._wasm.UTF8ToString(text)
}

private _makeConfig(): CConfig {
return new CConfig(
this.getId,
Expand Down
6 changes: 6 additions & 0 deletions src/apps/weblib/ts-api/module/chart.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as C from '../cvizzu.types.js'

export interface Chart {
doChange(): void
openUrl(url: C.CString): void
}
10 changes: 10 additions & 0 deletions src/apps/weblib/ts-api/module/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { CCanvas } from './ccanvas.js'
import { CAnimControl } from './canimctrl.js'
import { CCoordSystem } from './ccoordsys.js'
import { Canvas } from './canvas.js'
import { Chart } from './chart.js'

export class Module extends CEnv {
constructor(wasm: CVizzu) {
Expand All @@ -16,9 +17,18 @@ export class Module extends CEnv {
if (!context2D) throw new Error('Failed to get 2D context')
this._wasm.measureCanvas = context2D
this._wasm.canvases = {}
this._wasm.charts = {}
this.setLogging(false)
}

registerChart(cChart: CChart, chart: Chart): void {
this._wasm.charts[cChart.getId()] = chart
}

unregisterChart(cChart: CChart): void {
delete this._wasm.charts[cChart.getId()]
}

registerRenderer(cCanvas: CCanvas, canvas: Canvas): void {
this._wasm.canvases[cCanvas.getId()] = canvas
}
Expand Down
1 change: 1 addition & 0 deletions src/apps/weblib/ts-api/vizzu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ export default class Vizzu {
detach(): void {
try {
this._plugins.destruct()
this._chart?.detach()
} catch (e) {
console.error(`Error during plugin destruct: ${e}`)
}
Expand Down
1 change: 0 additions & 1 deletion src/base/gui/widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class Widget
virtual void onChanged() = 0;
virtual void onDraw(const std::shared_ptr<Gfx::ICanvas> &) = 0;
virtual void onUpdateSize(Geom::Size) = 0;
[[nodiscard]] virtual bool needsUpdate(Geom::Size) const = 0;
};

}
Expand Down
2 changes: 0 additions & 2 deletions src/chart/ui/chart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ ChartWidget::~ChartWidget()
void ChartWidget::onChanged()
{
if (doChange) doChange();
needUpdate = true;
}

void ChartWidget::onPointerDown(const GUI::PointerEvent &event)
Expand Down Expand Up @@ -81,7 +80,6 @@ void ChartWidget::onWheel(double delta)
void ChartWidget::onDraw(const std::shared_ptr<Gfx::ICanvas> &canvas)
{
chart.draw(*canvas);
needUpdate = false;
}

void ChartWidget::onUpdateSize(Geom::Size size)
Expand Down
6 changes: 0 additions & 6 deletions src/chart/ui/chart.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ class ChartWidget : public GUI::Widget

[[nodiscard]] Chart &getChart() { return chart; }

[[nodiscard]] bool needsUpdate(Geom::Size size) const final
{
return needUpdate || chart.getLayout().boundary.size != size;
}

private:
Chart chart;
Util::EventDispatcher::event_ptr onClick;
Expand All @@ -44,7 +39,6 @@ class ChartWidget : public GUI::Widget
Util::EventDispatcher::event_ptr onPointerDownEvent;
Util::EventDispatcher::event_ptr onPointerUpEvent;
Util::EventDispatcher::event_ptr onPointerLeaveEvent;
bool needUpdate{true};
};

}
Expand Down