Store window height in cookies so initial html is the right size

This commit is contained in:
Frank A. Krueger 2018-02-01 21:02:59 -08:00
parent 4148ea17be
commit 28c8fac046
2 changed files with 40 additions and 2 deletions

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
namespace Ooui.AspNetCore namespace Ooui.AspNetCore
@ -21,9 +22,14 @@ namespace Ooui.AspNetCore
var response = context.HttpContext.Response; var response = context.HttpContext.Response;
response.StatusCode = 200; response.StatusCode = 200;
response.ContentType = "text/html; charset=utf-8"; response.ContentType = "text/html; charset=utf-8";
if (element.WantsFullScreen) {
element.Style.Width = GetCookieDouble (context.HttpContext.Request.Cookies, "oouiWindowWidth", 32, 640, 10000);
element.Style.Height = GetCookieDouble (context.HttpContext.Request.Cookies, "oouiWindowHeight", 24, 480, 10000);
}
var sessionId = WebSocketHandler.BeginSession (context.HttpContext, element); var sessionId = WebSocketHandler.BeginSession (context.HttpContext, element);
var initialHtml = element.OuterHtml; var initialHtml = element.OuterHtml;
Console.WriteLine(initialHtml);
var html = UI.RenderTemplate (WebSocketHandler.WebSocketPath + "?id=" + sessionId, title: title, initialHtml: initialHtml); var html = UI.RenderTemplate (WebSocketHandler.WebSocketPath + "?id=" + sessionId, title: title, initialHtml: initialHtml);
var htmlBytes = Encoding.UTF8.GetBytes (html); var htmlBytes = Encoding.UTF8.GetBytes (html);
response.ContentLength = htmlBytes.Length; response.ContentLength = htmlBytes.Length;
@ -31,5 +37,20 @@ namespace Ooui.AspNetCore
await s.WriteAsync (htmlBytes, 0, htmlBytes.Length).ConfigureAwait (false); await s.WriteAsync (htmlBytes, 0, htmlBytes.Length).ConfigureAwait (false);
} }
} }
static double GetCookieDouble (IRequestCookieCollection cookies, string key, double min, double def, double max)
{
if (cookies.TryGetValue (key, out var s)) {
if (double.TryParse (s, out var d)) {
if (d < min) return min;
if (d > max) return max;
return d;
}
return def;
}
else {
return def;
}
}
} }
} }

View File

@ -35,12 +35,29 @@ function getSize () {
}; };
} }
function setCookie (name, value, days) {
var expires = "";
if (days) {
var date = new Date ();
date.setTime(date.getTime () + (days*24*60*60*1000));
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + (value || "") + expires + "; path=/";
}
function saveSize (s) {
setCookie ("oouiWindowWidth", s.width, 7);
setCookie ("oouiWindowHeight", s.height, 7);
}
// Main entrypoint // Main entrypoint
function ooui (rootElementPath) { function ooui (rootElementPath) {
var initialSize = getSize ();
saveSize (initialSize);
return; return;
var initialSize = getSize ();
var wsArgs = (rootElementPath.indexOf("?") >= 0 ? "&" : "?") + var wsArgs = (rootElementPath.indexOf("?") >= 0 ? "&" : "?") +
"w=" + initialSize.width + "&h=" + initialSize.height; "w=" + initialSize.width + "&h=" + initialSize.height;