Skip to content
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
27 changes: 17 additions & 10 deletions gui-js/apps/minsky-web/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ export class AppComponent implements OnInit, DoCheck {

loading = true;
publicationTabs = [];
private windowUtilityService: WindowUtilityService;
private resizeTimeout;
private tabSwitchGeneration=0; // identifies the most recent main tab switch
constructor(
private electronService: ElectronService,
private cmService: CommunicationService,
private translate: TranslateService,
private cdRef: ChangeDetectorRef,
public router: Router,
private windowUtilityService: WindowUtilityService,
) {
this.windowUtilityService=new WindowUtilityService(electronService);
this.translate.setDefaultLang('en');
}

Expand Down Expand Up @@ -174,17 +174,24 @@ export class AppComponent implements OnInit, DoCheck {
this.router.navigate(['wiring']);

if (this.electronService.isElectron) {
const generation=++this.tabSwitchGeneration;
this.cmService.tabSwitching = true;
this.cmService.currentTab = tab;

setTimeout(async ()=> {
await this.windowUtilityService.reInitialize();
const payload = { newTab: tab };
await this.electronService.invoke(events.CHANGE_MAIN_TAB, payload);
var container=this.windowUtilityService.getMinskyContainerElement();
const scrollableArea=this.windowUtilityService.getScrollableArea();
container.scrollTop=scrollableArea.height / 2;
container.scrollLeft=scrollableArea.width / 2;
this.cmService.resetScroll();
try {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
await this.windowUtilityService.reInitialize();
const payload = { newTab: tab };
await this.electronService.invoke(events.CHANGE_MAIN_TAB, payload);
// restore the new tab's own pan position (without moving it), and
// size the scrollbars now that the tab has rendered
await this.cmService.resetScroll();
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
} finally {
// re-enable scrolling, even if a step above failed, but only if no
// later switch has started and is still in progress
if (generation===this.tabSwitchGeneration)
this.cmService.tabSwitching = false;
}
}, 1);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ export class WindowUtilityService {
private scrollableAreaWidth = null;
private scrollableAreaHeight = null;
private mainWindowId = 1;
SCROLLABLE_AREA_FACTOR = 100;
// scrollable area is this multiple of the extent of the canvas contents
SCROLLABLE_AREA_FACTOR = 1.5;
// scrollable area as a multiple of the drawable area, for tabs that cannot report their content extents
UNKNOWN_EXTENT_FACTOR = 100;

constructor(private electronService: ElectronService) { }

Expand All @@ -35,12 +38,9 @@ export class WindowUtilityService {
bodyElement.clientHeight - this.minskyCanvasContainer.offsetTop + 'px';

if (!this.scrollableAreaWidth || !this.scrollableAreaHeight) {
// Scrollable area size should not change

this.scrollableAreaWidth =
bodyElement.clientWidth * this.SCROLLABLE_AREA_FACTOR;
this.scrollableAreaHeight =
bodyElement.clientHeight * this.SCROLLABLE_AREA_FACTOR;
// initial size, until updateScrollableArea is called with the contents' bounds
this.scrollableAreaWidth = bodyElement.clientWidth * 3;
this.scrollableAreaHeight = bodyElement.clientHeight * 3;
}
this.minskyCanvasElement.style.width = this.scrollableAreaWidth + 'px';
this.minskyCanvasElement.style.height = this.scrollableAreaHeight + 'px';
Expand All @@ -57,6 +57,37 @@ export class WindowUtilityService {
}
}

/**
* Resize the scrollable area to SCROLLABLE_AREA_FACTOR times the extent of
* the canvas contents. The pan position (scrollable area centre minus
* scroll offset) lies within +/- half the excess over the drawable area.
* @param bounds [x0,y0,x1,y1] of canvas contents in window coordinates
* @param pos current pan position [x,y]
* @returns true if the scrollable area changed
*/
public updateScrollableArea(bounds: number[], pos: number[]): boolean {
if (!this.minskyCanvasElement) return false;
if (!bounds || bounds.length < 4)
// content extent unknown for this tab: use a generously sized area
bounds = [0, 0, this.drawableWidth * this.UNKNOWN_EXTENT_FACTOR / 2, this.drawableHeight * this.UNKNOWN_EXTENT_FACTOR / 2];
const size = (b0: number, b1: number, p: number, drawable: number) => {
// contents' extent relative to the unpanned origin
const extent = Math.max(Math.abs(b0 - p), Math.abs(b1 - p), 0);
const half = this.SCROLLABLE_AREA_FACTOR * extent;
// also keep the current pan within the scroll range:
// 0 <= W/2 - p <= W - drawable
return Math.ceil(Math.max(drawable + 2 * half, 2 * drawable, 2 * p, 2 * (drawable - p)));
};
const w = size(bounds[0], bounds[2], pos[0], this.drawableWidth);
const h = size(bounds[1], bounds[3], pos[1], this.drawableHeight);
if (w === this.scrollableAreaWidth && h === this.scrollableAreaHeight) return false;
this.scrollableAreaWidth = w;
this.scrollableAreaHeight = h;
this.minskyCanvasElement.style.width = w + 'px';
this.minskyCanvasElement.style.height = h + 'px';
return true;
}

public scrollToCenter() {
this.minskyCanvasElement.scrollTop = this.scrollableAreaHeight / 2;
this.minskyCanvasElement.scrollLeft = this.scrollableAreaWidth / 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ export class CommunicationService {
private dialogRef: MatDialogRef<DialogComponent, any> = null;
availableOperations = null;

resetScroll = () => { }; // callback to reset canvas scrollbars
tabSwitching = false; // true while the main tab is being changed: scroll events must not pan the tab
resetScroll: () => Promise<void> | void = () => { }; // callback to reset canvas scrollbars

constructor(
// private socket: Socket,
Expand Down
8 changes: 8 additions & 0 deletions gui-js/libs/shared/src/lib/backend/minsky.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ export class RenderNativeWindow extends EventInterface {
super(prefix.$prefix())
this.backgroundColour=new ecolab__cairo__Colour(this.$prefix()+'.backgroundColour');
}
async contentBounds(): Promise<number[]> {return this.$callMethod('contentBounds');}
async destroyFrame(): Promise<void> {return this.$callMethod('destroyFrame');}
async draw(): Promise<void> {return this.$callMethod('draw');}
async frameArgs(): Promise<minsky__RenderNativeWindow__RenderFrameArgs> {return this.$callMethod('frameArgs');}
Expand Down Expand Up @@ -436,6 +437,7 @@ export class Canvas extends RenderNativeWindow {
async applyDefaultPlotOptions(): Promise<void> {return this.$callMethod('applyDefaultPlotOptions');}
async clickType(...args: string[]): Promise<string> {return this.$callMethod('clickType',...args);}
async closestInPort(a1: number,a2: number): Promise<object> {return this.$callMethod('closestInPort',a1,a2);}
async contentBounds(): Promise<number[]> {return this.$callMethod('contentBounds');}
async controlMouseDown(a1: number,a2: number): Promise<void> {return this.$callMethod('controlMouseDown',a1,a2);}
async copyAllFlowVars(): Promise<void> {return this.$callMethod('copyAllFlowVars');}
async copyAllStockVars(): Promise<void> {return this.$callMethod('copyAllStockVars');}
Expand Down Expand Up @@ -640,6 +642,7 @@ export class EquationDisplay extends RenderNativeWindow {
this.item=new Item(this.$prefix()+'.item');
this.wire=new Wire(this.$prefix()+'.wire');
}
async contentBounds(): Promise<number[]> {return this.$callMethod('contentBounds');}
async controlMouseDown(a1: number,a2: number): Promise<void> {return this.$callMethod('controlMouseDown',a1,a2);}
async destroyFrame(): Promise<void> {return this.$callMethod('destroyFrame');}
async draw(): Promise<void> {return this.$callMethod('draw');}
Expand Down Expand Up @@ -692,6 +695,7 @@ export class FontDisplay extends CppClass {
this.item=new Item(this.$prefix()+'.item');
this.wire=new Wire(this.$prefix()+'.wire');
}
async contentBounds(): Promise<number[]> {return this.$callMethod('contentBounds');}
async controlMouseDown(a1: number,a2: number): Promise<void> {return this.$callMethod('controlMouseDown',a1,a2);}
async destroyFrame(): Promise<void> {return this.$callMethod('destroyFrame');}
async draw(): Promise<void> {return this.$callMethod('draw');}
Expand Down Expand Up @@ -926,6 +930,7 @@ export class GodleyTableWindow extends CppClass {
async colX(a1: number): Promise<number> {return this.$callMethod('colX',a1);}
async colXZoomed(a1: number): Promise<number> {return this.$callMethod('colXZoomed',a1);}
async columnButtonsOffset(...args: number[]): Promise<number> {return this.$callMethod('columnButtonsOffset',...args);}
async contentBounds(): Promise<number[]> {return this.$callMethod('contentBounds');}
async controlMouseDown(a1: number,a2: number): Promise<void> {return this.$callMethod('controlMouseDown',a1,a2);}
async copy(): Promise<void> {return this.$callMethod('copy');}
async cut(): Promise<void> {return this.$callMethod('cut');}
Expand Down Expand Up @@ -1491,6 +1496,7 @@ export class PhillipsDiagram extends RenderNativeWindow {
this.wire=new Wire(this.$prefix()+'.wire');
}
async clear(): Promise<void> {return this.$callMethod('clear');}
async contentBounds(): Promise<number[]> {return this.$callMethod('contentBounds');}
async controlMouseDown(a1: number,a2: number): Promise<void> {return this.$callMethod('controlMouseDown',a1,a2);}
async destroyFrame(): Promise<void> {return this.$callMethod('destroyFrame');}
async draw(): Promise<void> {return this.$callMethod('draw');}
Expand Down Expand Up @@ -1605,6 +1611,7 @@ export class PlotWidget extends Item {
async clickType(a1: number,a2: number): Promise<string> {return this.$callMethod('clickType',a1,a2);}
async connectVar(a1: VariableValue,a2: number): Promise<void> {return this.$callMethod('connectVar',a1,a2);}
async contains(a1: number,a2: number): Promise<boolean> {return this.$callMethod('contains',a1,a2);}
async contentBounds(): Promise<number[]> {return this.$callMethod('contentBounds');}
async controlMouseDown(a1: number,a2: number): Promise<void> {return this.$callMethod('controlMouseDown',a1,a2);}
async destroyFrame(): Promise<void> {return this.$callMethod('destroyFrame');}
async disconnectAllVars(): Promise<void> {return this.$callMethod('disconnectAllVars');}
Expand Down Expand Up @@ -1870,6 +1877,7 @@ export class RavelPopup extends CppClass {
this.item=new Item(this.$prefix()+'.item');
this.wire=new Wire(this.$prefix()+'.wire');
}
async contentBounds(): Promise<number[]> {return this.$callMethod('contentBounds');}
async controlMouseDown(a1: number,a2: number): Promise<void> {return this.$callMethod('controlMouseDown',a1,a2);}
async destroyFrame(): Promise<void> {return this.$callMethod('destroyFrame');}
async draw(): Promise<void> {return this.$callMethod('draw');}
Expand Down
62 changes: 49 additions & 13 deletions gui-js/libs/ui-components/src/lib/wiring/wiring.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
ElectronService,
WindowUtilityService,
} from '@minsky/core';
import { events, MainRenderingTabs, minsky } from '@minsky/shared';
import { events, EventInterface, MainRenderingTabs, minsky, RenderNativeWindow } from '@minsky/shared';
import { fromEvent, Observable, Subject, takeUntil } from 'rxjs';
import { sampleTime } from 'rxjs/operators';
import { AvailableOperationsComponent } from './available-operations/available-operations.component';
Expand All @@ -35,6 +35,7 @@ export class WiringComponent implements OnInit, OnDestroy {
wiringTab = MainRenderingTabs.canvas;

destroy$ = new Subject<{}>();
private rescaleTimer: ReturnType<typeof setTimeout>; // pending scrollable area resize

constructor(
public cmService: CommunicationService,
Expand Down Expand Up @@ -62,8 +63,8 @@ export class WiringComponent implements OnInit, OnDestroy {

private setupEventListenersForCanvas(minskyCanvasContainer: HTMLElement) {
const minskyCanvasElement = document.getElementById('main-minsky-canvas') as HTMLElement;
const scrollableArea = this.windowUtilityService.getScrollableArea();
var moveOnScroll=0; // avoid moving when programmatically setting scroll bars
var suppressScroll=false; // ignore scroll events caused by programmatically setting scroll bars or resizing
var scrollGeneration=0; // incremented on each user scroll, to detect stale resets

const tabElements = document.getElementsByClassName('tab');
for(let i = 0; i < tabElements.length; i++) {
Expand All @@ -76,33 +77,67 @@ export class WiringComponent implements OnInit, OnDestroy {
}

this.cmService.resetScroll=async ()=>{
++moveOnScroll;
var pos= await this.electronService.currentTabPosition();
minskyCanvasContainer.scrollLeft=scrollableArea.width/2 - pos[0];
minskyCanvasContainer.scrollTop=scrollableArea.height/2 - pos[1];
const generation=scrollGeneration;
const tab=this.cmService.currentTab;
var pos= await new EventInterface(tab).position();
let bounds: number[];
try {
// bounds of the currently displayed tab (empty if the tab cannot report them)
bounds=await new RenderNativeWindow(tab).contentBounds();
} catch (e) {console.error(e);}
// user scrolled while awaiting: discard this stale reset (including the
// resize), the debounce from the latest scroll will perform a fresh one
if (generation!==scrollGeneration || tab!==this.cmService.currentTab) return;
// resizing or setting the scroll position generates scroll events,
// which must not be interpreted as the user panning
suppressScroll=true;
this.windowUtilityService.updateScrollableArea(bounds,pos);
const scrollableArea = this.windowUtilityService.getScrollableArea();
minskyCanvasContainer.scrollLeft=Math.round(scrollableArea.width/2 - pos[0]);
minskyCanvasContainer.scrollTop=Math.round(scrollableArea.height/2 - pos[1]);
// scroll events are delivered before the next animation frame callbacks
await new Promise<void>(resolve=>requestAnimationFrame(()=>requestAnimationFrame(()=>resolve())));
suppressScroll=false;
};
this.cmService.resetScroll();

this.zone.runOutsideAngular(() => {
if (this.electronService.isElectron) {
const handleScroll = async (scrollTop: number, scrollLeft: number) => {
// address the tab explicitly, so a move still in flight when the tab
// is switched cannot be applied to the newly selected tab
const tab=this.cmService.currentTab;
const scrollableArea = this.windowUtilityService.getScrollableArea();
const posX = scrollableArea.width / 2 - scrollLeft;
const posY = scrollableArea.height / 2 - scrollTop;
if (!moveOnScroll)
await this.electronService.currentTabMoveTo(posX,posY);
else
--moveOnScroll;
await new EventInterface(tab).moveTo(posX,posY);
};

minskyCanvasContainer.addEventListener('scroll', async () => {
if (suppressScroll || this.cmService.tabSwitching) return;
// once scrolling stops, resize the scrollable area to fit the contents
{
++scrollGeneration;
clearTimeout(this.rescaleTimer);
const rescale=()=>{
// resizing mid-drag would shift the scroll baseline the drag is relative to,
// so wait until the drag ends
if (this.cmService.drag)
this.rescaleTimer=setTimeout(rescale,500);
else
this.cmService.resetScroll();
};
this.rescaleTimer=setTimeout(rescale,500);
}
await handleScroll(
minskyCanvasContainer.scrollTop,
minskyCanvasContainer.scrollLeft
);
});
minskyCanvasContainer.addEventListener('RESET_ZOOM', async () => {
minskyCanvasContainer.scrollTop=scrollableArea.width / 2;
minskyCanvasContainer.scrollLeft=scrollableArea.height / 2;
const scrollableArea = this.windowUtilityService.getScrollableArea();
minskyCanvasContainer.scrollTop=scrollableArea.height / 2;
minskyCanvasContainer.scrollLeft=scrollableArea.width / 2;
});
minskyCanvasContainer.addEventListener('RESET_SCROLL', async () => {
this.cmService.resetScroll();
Expand Down Expand Up @@ -198,6 +233,7 @@ export class WiringComponent implements OnInit, OnDestroy {
}

ngOnDestroy() {
clearTimeout(this.rescaleTimer);
this.destroy$.next(undefined);
this.destroy$.complete();
}
Expand Down
8 changes: 8 additions & 0 deletions model/canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ namespace minsky
requestRedraw();
}

/// bounds {x0,y0,x1,y1} of the items on the canvas, in window
/// coordinates (ie including the current pan position)
std::vector<double> contentBounds() const override {
double x0,y0,x1,y1;
model->contentBounds(x0,y0,x1,y1);
return {x0,y0,x1,y1};
}

std::vector<float> position() const override {
return {model->x(), model->y()};
}
Expand Down
4 changes: 4 additions & 0 deletions model/equationDisplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ namespace minsky
EquationDisplay(const EquationDisplay&)=default;
void requestRedraw() {if (surface.get()) surface->requestRedraw();}
bool hasScrollBars() const override {return true;}
std::vector<double> contentBounds() const override {
// equations are rendered at the pan offset, scaled by the zoom factor
return {offsx, offsy, offsx+m_width*m_zoomFactor, offsy+m_height*m_zoomFactor};
}
};

}
Expand Down
28 changes: 28 additions & 0 deletions model/phillipsDiagram.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,34 @@ namespace minsky
return true;
}

std::vector<double> PhillipsDiagram::contentBounds() const
{
// stocks and flow handles are drawn at their own positions, offset by the pan position (x,y)
double x0=std::numeric_limits<double>::max(), y0=x0, x1=-x0, y1=-x0;
for (auto& i: stocks)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
{
x0=std::min<double>(x0,i.second.left());
x1=std::max<double>(x1,i.second.right());
y0=std::min<double>(y0,i.second.top());
y1=std::max<double>(y1,i.second.bottom());
}
for (auto& i: flows)
{
if (!i.second.from() || !i.second.to()) continue;
// end points and intermediate control points
auto c=i.second.coords();
for (size_t j=0; j+1<c.size(); j+=2)
{
x0=std::min<double>(x0,c[j]);
x1=std::max<double>(x1,c[j]);
y0=std::min<double>(y0,c[j+1]);
y1=std::max<double>(y1,c[j+1]);
}
}
if (x0>x1) return {x,y,x,y}; // nothing to display
return {x0+x, y0+y, x1+x, y1+y};
}

void PhillipsDiagram::init()
{
decltype(stocks) newStocks;
Expand Down
1 change: 1 addition & 0 deletions model/phillipsDiagram.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ namespace minsky
std::vector<float> position() const override {return {x,y};}
void startRotatingItem(float x, float y);
bool hasScrollBars() const override {return true;}
std::vector<double> contentBounds() const override;
};
}

Expand Down
Loading
Loading