get viewport dimensions from controller

This commit is contained in:
Andrew Meadows 2016-04-26 11:23:29 -07:00
parent 8a02a585f2
commit e436c355a1

View file

@ -355,36 +355,20 @@ ToolBar = function(x, y, direction, optionalPersistenceKey, optionalInitialPosit
}); });
} }
}; };
that.windowDimensions = Controller.getViewportDimensions();
function clamp(value, min, max) {
return Math.min(Math.max(value, min), max);
}
var recommendedRect = Controller.getRecommendedOverlayRect();
var recommendedDimmensions = { x: recommendedRect.width, y: recommendedRect.height };
that.windowDimensions = recommendedDimmensions; // Controller.getViewportDimensions();
that.origin = { x: recommendedRect.x, y: recommendedRect.y };
// Maybe fixme: Keeping the same percent of the window size isn't always the right thing. // Maybe fixme: Keeping the same percent of the window size isn't always the right thing.
// For example, maybe we want "keep the same percentage to whatever two edges are closest to the edge of screen". // For example, maybe we want "keep the same percentage to whatever two edges are closest to the edge of screen".
// If we change that, the places to do so are onResizeViewport, save (maybe), and the initial move based on Settings, below. // If we change that, the places to do so are onResizeViewport, save (maybe), and the initial move based on Settings, below.
that.onResizeViewport = function (newSize) { // Can be overridden or extended by clients. that.onResizeViewport = function (newSize) { // Can be overridden or extended by clients.
var recommendedRect = Controller.getRecommendedOverlayRect(); var fractionX = that.x / that.windowDimensions.x;
var recommendedDimmensions = { x: recommendedRect.width, y: recommendedRect.height }; var fractionY = that.y / that.windowDimensions.y;
var originRelativeX = (that.x - that.origin.x); that.windowDimensions = newSize || Controller.getViewportDimensions();
var originRelativeY = (that.y - that.origin.y); that.move(fractionX * that.windowDimensions.x, fractionY * that.windowDimensions.y);
var fractionX = clamp(originRelativeX / that.windowDimensions.x, 0, 1);
var fractionY = clamp(originRelativeY / that.windowDimensions.y, 0, 1);
that.windowDimensions = newSize || recommendedDimmensions;
that.origin = { x: recommendedRect.x, y: recommendedRect.y };
var newX = (fractionX * that.windowDimensions.x) + recommendedRect.x;
var newY = (fractionY * that.windowDimensions.y) + recommendedRect.y;
that.move(newX, newY);
}; };
if (optionalPersistenceKey) { if (optionalPersistenceKey) {
this.fractionKey = optionalPersistenceKey + '.fraction'; this.fractionKey = optionalPersistenceKey + '.fraction';
this.save = function () { this.save = function () {
var recommendedRect = Controller.getRecommendedOverlayRect(); var screenSize = Controller.getViewportDimensions();
var screenSize = { x: recommendedRect.width, y: recommendedRect.height };
if (screenSize.x > 0 && screenSize.y > 0) { if (screenSize.x > 0 && screenSize.y > 0) {
// Guard against invalid screen size that can occur at shut-down. // Guard against invalid screen size that can occur at shut-down.
var fraction = {x: that.x / screenSize.x, y: that.y / screenSize.y}; var fraction = {x: that.x / screenSize.x, y: that.y / screenSize.y};
@ -427,9 +411,7 @@ ToolBar = function(x, y, direction, optionalPersistenceKey, optionalInitialPosit
that.move(that.dragOffsetX + event.x, that.dragOffsetY + event.y); that.move(that.dragOffsetX + event.x, that.dragOffsetY + event.y);
}; };
that.checkResize = function () { // Can be overriden or extended, but usually not. See onResizeViewport. that.checkResize = function () { // Can be overriden or extended, but usually not. See onResizeViewport.
var recommendedRect = Controller.getRecommendedOverlayRect(); var currentWindowSize = Controller.getViewportDimensions();
var currentWindowSize = { x: recommendedRect.width, y: recommendedRect.height };
if ((currentWindowSize.x !== that.windowDimensions.x) || (currentWindowSize.y !== that.windowDimensions.y)) { if ((currentWindowSize.x !== that.windowDimensions.x) || (currentWindowSize.y !== that.windowDimensions.y)) {
that.onResizeViewport(currentWindowSize); that.onResizeViewport(currentWindowSize);
} }
@ -452,8 +434,7 @@ ToolBar = function(x, y, direction, optionalPersistenceKey, optionalInitialPosit
} }
if (this.fractionKey || optionalInitialPositionFunction) { if (this.fractionKey || optionalInitialPositionFunction) {
var savedFraction = JSON.parse(Settings.getValue(this.fractionKey) || '0'); // getValue can answer empty string var savedFraction = JSON.parse(Settings.getValue(this.fractionKey) || '0'); // getValue can answer empty string
var recommendedRect = Controller.getRecommendedOverlayRect(); var screenSize = Controller.getViewportDimensions();
var screenSize = { x: recommendedRect.width, y: recommendedRect.height };
if (savedFraction) { if (savedFraction) {
// If we have saved data, keep the toolbar at the same proportion of the screen width/height. // If we have saved data, keep the toolbar at the same proportion of the screen width/height.
that.move(savedFraction.x * screenSize.x, savedFraction.y * screenSize.y); that.move(savedFraction.x * screenSize.x, savedFraction.y * screenSize.y);