From c991127c7cefc42f34718f04891970c2fe54123a Mon Sep 17 00:00:00 2001 From: LarsBrubaker Date: Sat, 8 May 2021 10:38:59 -0700 Subject: [PATCH] Improving loading progress --- MatterControlLib/Library/HttpProgress.cs | 69 +++++++++++++++++++ .../Providers/GitHub/GitHubLibraryItem.cs | 11 +-- .../Library/Widgets/InsertionGroupObject3D.cs | 3 +- .../View3D/DragDropLoadProgress.cs | 20 +++++- Submodules/agg-sharp | 2 +- 5 files changed, 95 insertions(+), 10 deletions(-) create mode 100644 MatterControlLib/Library/HttpProgress.cs diff --git a/MatterControlLib/Library/HttpProgress.cs b/MatterControlLib/Library/HttpProgress.cs new file mode 100644 index 000000000..b1f7c255c --- /dev/null +++ b/MatterControlLib/Library/HttpProgress.cs @@ -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 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); + } + } +} \ No newline at end of file diff --git a/MatterControlLib/Library/Providers/GitHub/GitHubLibraryItem.cs b/MatterControlLib/Library/Providers/GitHub/GitHubLibraryItem.cs index 59eab8e87..fdfead93c 100644 --- a/MatterControlLib/Library/Providers/GitHub/GitHubLibraryItem.cs +++ b/MatterControlLib/Library/Providers/GitHub/GitHubLibraryItem.cs @@ -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); } } } diff --git a/MatterControlLib/Library/Widgets/InsertionGroupObject3D.cs b/MatterControlLib/Library/Widgets/InsertionGroupObject3D.cs index 063d155e9..184f8728f 100644 --- a/MatterControlLib/Library/Widgets/InsertionGroupObject3D.cs +++ b/MatterControlLib/Library/Widgets/InsertionGroupObject3D.cs @@ -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); diff --git a/MatterControlLib/PartPreviewWindow/View3D/DragDropLoadProgress.cs b/MatterControlLib/PartPreviewWindow/View3D/DragDropLoadProgress.cs index 3b2416fb3..f2016df60 100644 --- a/MatterControlLib/PartPreviewWindow/View3D/DragDropLoadProgress.cs +++ b/MatterControlLib/PartPreviewWindow/View3D/DragDropLoadProgress.cs @@ -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(); diff --git a/Submodules/agg-sharp b/Submodules/agg-sharp index 8ad40eee3..4c0f9c8ca 160000 --- a/Submodules/agg-sharp +++ b/Submodules/agg-sharp @@ -1 +1 @@ -Subproject commit 8ad40eee320c47e6cf459266346e81185c0f2999 +Subproject commit 4c0f9c8cabf149ce6b4d70515b2a38c04483a72d