Merge pull request #5059 from larsbrubaker/main
Improving loading progress
This commit is contained in:
commit
540bbcbba6
5 changed files with 95 additions and 10 deletions
69
MatterControlLib/Library/HttpProgress.cs
Normal file
69
MatterControlLib/Library/HttpProgress.cs
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
/*
|
||||
Copyright (c) 2019, John Lewin
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
The views and conclusions contained in the software and documentation are those
|
||||
of the authors and should not be interpreted as representing official policies,
|
||||
either expressed or implied, of the FreeBSD Project.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using MatterHackers.Localizations;
|
||||
|
||||
namespace MatterHackers.MatterControl.Library
|
||||
{
|
||||
public static class HttpProgress
|
||||
{
|
||||
public static async Task ProcessContentStream(long? totalDownloadSize,
|
||||
Stream contentStream,
|
||||
FileStream fileStream,
|
||||
Action<double, string> reportProgress)
|
||||
{
|
||||
var totalBytesRead = 0L;
|
||||
var readCount = 0L;
|
||||
var buffer = new byte[8192];
|
||||
var isMoreToRead = true;
|
||||
|
||||
do
|
||||
{
|
||||
var bytesRead = await contentStream.ReadAsync(buffer, 0, buffer.Length);
|
||||
if (bytesRead == 0)
|
||||
{
|
||||
reportProgress?.Invoke(1, "Downloading".Localize() + "...");
|
||||
isMoreToRead = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
await fileStream.WriteAsync(buffer, 0, bytesRead);
|
||||
|
||||
totalBytesRead += bytesRead;
|
||||
readCount += 1;
|
||||
|
||||
reportProgress?.Invoke(totalBytesRead / (double)totalDownloadSize, "Downloading".Localize() + "...");
|
||||
}
|
||||
while (isMoreToRead);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -158,16 +158,17 @@ namespace MatterHackers.MatterControl.Library
|
|||
try
|
||||
{
|
||||
// get the file contents;
|
||||
var downLoadUrl = new HttpRequestMessage(HttpMethod.Get, Url);
|
||||
GitHubContainer.AddCromeHeaders(downLoadUrl);
|
||||
var requestMessage = new HttpRequestMessage(HttpMethod.Get, Url);
|
||||
GitHubContainer.AddCromeHeaders(requestMessage);
|
||||
|
||||
using (var client = new HttpClient())
|
||||
{
|
||||
using (HttpResponseMessage contentResponse = await client.SendAsync(downLoadUrl))
|
||||
using (HttpResponseMessage response = await client.SendAsync(requestMessage))
|
||||
{
|
||||
using (var readData = await contentResponse.Content.ReadAsStreamAsync())
|
||||
using (var readData = await response.Content.ReadAsStreamAsync())
|
||||
{
|
||||
await readData.CopyToAsync(fileStream);
|
||||
var totalBytes = response.Content.Headers.ContentLength;
|
||||
await HttpProgress.ProcessContentStream(totalBytes, readData, fileStream, reportProgress);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -91,8 +91,9 @@ namespace MatterHackers.MatterControl.Library
|
|||
|
||||
this.Children.Add(placeholderItem);
|
||||
|
||||
var itemsToLoad = items.Where(item => item.IsContentFileType()).ToList();
|
||||
// Filter to content file types only
|
||||
foreach (var item in items.Where(item => item.IsContentFileType()).ToList())
|
||||
foreach (var item in itemsToLoad)
|
||||
{
|
||||
// Acquire
|
||||
var progressControl = new DragDropLoadProgress(view3DWidget, null, ApplicationController.Instance.Theme);
|
||||
|
|
|
|||
|
|
@ -26,8 +26,10 @@ The views and conclusions contained in the software and documentation are those
|
|||
of the authors and should not be interpreted as representing official policies,
|
||||
either expressed or implied, of the FreeBSD Project.
|
||||
*/
|
||||
using MatterHackers.Agg.Font;
|
||||
using MatterHackers.Agg.Transform;
|
||||
using MatterHackers.Agg.UI;
|
||||
using MatterHackers.Agg.VertexSource;
|
||||
using MatterHackers.DataConverters3D;
|
||||
using MatterHackers.VectorMath;
|
||||
|
||||
|
|
@ -40,15 +42,23 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
|
|||
|
||||
public DragDropLoadProgress(View3DWidget view3DWidget, IObject3D trackingObject, ThemeConfig theme)
|
||||
{
|
||||
this.theme = theme;
|
||||
this.TrackingObject = trackingObject;
|
||||
this.view3DWidget = view3DWidget;
|
||||
view3DWidget.AfterDraw += View3DWidget_AfterDraw;
|
||||
progressBar = new ProgressBar(80 * GuiWidget.DeviceScale, 15 * GuiWidget.DeviceScale)
|
||||
var height = 12;
|
||||
progressBar = new ProgressBar(80 * GuiWidget.DeviceScale, height * GuiWidget.DeviceScale)
|
||||
{
|
||||
FillColor = theme.PrimaryAccentColor,
|
||||
BackgroundColor = theme.BackgroundColor,
|
||||
BackgroundRadius = height / 2 * GuiWidget.DeviceScale,
|
||||
BackgroundOutlineWidth = 1 * GuiWidget.DeviceScale,
|
||||
BorderColor = theme.TextColor,
|
||||
};
|
||||
}
|
||||
|
||||
private ThemeConfig theme;
|
||||
|
||||
public IObject3D TrackingObject { get; set; }
|
||||
|
||||
public string State { get; set; }
|
||||
|
|
@ -63,7 +73,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
|
|||
AxisAlignedBoundingBox bounds = TrackingObject.GetAxisAlignedBoundingBox(offset);
|
||||
|
||||
Vector3 renderPosition = bounds.GetBottomCorner(2);
|
||||
Vector2 cornerScreenSpace = view3DWidget.Object3DControlLayer.World.GetScreenPosition(renderPosition) - new Vector2(20, 0);
|
||||
Vector2 cornerScreenSpace = view3DWidget.Object3DControlLayer.World.GetScreenPosition(renderPosition) - new Vector2(20, 10) * GuiWidget.DeviceScale;
|
||||
|
||||
e.Graphics2D.PushTransform();
|
||||
Affine currentGraphics2DTransform = e.Graphics2D.GetTransform();
|
||||
|
|
@ -74,7 +84,11 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
|
|||
|
||||
if (!string.IsNullOrEmpty(this.State))
|
||||
{
|
||||
e.Graphics2D.DrawString(this.State, 0, -20, 11);
|
||||
var stringPrinter = new TypeFacePrinter(this.State, 9, new Vector2(0, -20));
|
||||
var textBounds = stringPrinter.LocalBounds;
|
||||
textBounds.Inflate(textBounds.Height / 4);
|
||||
e.Graphics2D.Render(new RoundedRect(textBounds, textBounds.Height / 4), theme.BackgroundColor);
|
||||
stringPrinter.Render(e.Graphics2D, theme.TextColor);
|
||||
}
|
||||
|
||||
e.Graphics2D.PopTransform();
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
Subproject commit 8ad40eee320c47e6cf459266346e81185c0f2999
|
||||
Subproject commit 4c0f9c8cabf149ce6b4d70515b2a38c04483a72d
|
||||
Loading…
Add table
Add a link
Reference in a new issue