From c7d4d27327391d6a441fcd1dc7f9d8fb3f2fb15a Mon Sep 17 00:00:00 2001 From: LarsBrubaker Date: Sat, 16 May 2020 17:38:51 -0700 Subject: [PATCH 1/2] tree view keyboard navigation Making images work on printer add ons --- .../CustomWidgets/TreeView/TreeNode.cs | 43 +++++++++++- .../CustomWidgets/TreeView/TreeView.cs | 68 ++++++++++++++++++- .../Widgets/HardwarePage/PrinterDetails.cs | 31 ++++++--- .../Widgets/HardwarePage/ProductSidData.cs | 14 ++-- Submodules/agg-sharp | 2 +- 5 files changed, 138 insertions(+), 20 deletions(-) diff --git a/MatterControlLib/CustomWidgets/TreeView/TreeNode.cs b/MatterControlLib/CustomWidgets/TreeView/TreeNode.cs index dfc2f1339..3ee50a7ad 100644 --- a/MatterControlLib/CustomWidgets/TreeView/TreeNode.cs +++ b/MatterControlLib/CustomWidgets/TreeView/TreeNode.cs @@ -119,7 +119,7 @@ namespace MatterHackers.MatterControl.CustomWidgets Margin = new BorderDouble(right: 4), Selectable = false }); - }; + } this.HighlightRegion.AddChild(textWidget = new TextWidget(this.Text, pointSize: theme.DefaultFontSize, textColor: theme.TextColor) { @@ -141,6 +141,47 @@ namespace MatterHackers.MatterControl.CustomWidgets this.Nodes.CollectionChanged += this.Nodes_CollectionChanged; } + public override void OnKeyDown(KeyEventArgs keyEvent) + { + base.OnKeyDown(keyEvent); + + var restoreFocus = Focused; + + if (!keyEvent.Handled) + { + switch (keyEvent.KeyCode) + { + case Keys.Right: + this.Expanded = true; + keyEvent.Handled = true; + break; + + case Keys.Left: + if (!this.Expanded) + { + if (this.NodeParent != null) + { + // navigate back up to the parent of this node + TreeView.SelectedNode = this.NodeParent; + } + restoreFocus = false; + } + else + { + this.Expanded = false; + } + + keyEvent.Handled = true; + break; + } + } + + if (restoreFocus && !Focused) + { + Focus(); + } + } + private void Nodes_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add) diff --git a/MatterControlLib/CustomWidgets/TreeView/TreeView.cs b/MatterControlLib/CustomWidgets/TreeView/TreeView.cs index faf984626..50e9e9cca 100644 --- a/MatterControlLib/CustomWidgets/TreeView/TreeView.cs +++ b/MatterControlLib/CustomWidgets/TreeView/TreeView.cs @@ -202,6 +202,65 @@ namespace MatterHackers.MatterControl.CustomWidgets _selectedNode = null; } + public override void OnKeyDown(KeyEventArgs keyEvent) + { + if (!keyEvent.Handled) + { + switch (keyEvent.KeyCode) + { + case Keys.Up: + var prev = PreviousVisibleTreeNode(SelectedNode); + if (prev != null) + { + SelectedNode = prev; + keyEvent.Handled = true; + } + + break; + + case Keys.Down: + var next = NextVisibleTreeNode(SelectedNode); + if (next != null) + { + SelectedNode = next; + keyEvent.Handled = true; + } + + break; + } + } + + base.OnKeyDown(keyEvent); + } + + private TreeNode NextVisibleTreeNode(TreeNode treeNode) + { + var nodes = this.Descendants((child) => child.Visible).ToList(); + + var selectedIndex = nodes.IndexOf(SelectedNode); + + if (selectedIndex < nodes.Count - 1) + { + return nodes[selectedIndex + 1]; + } + + return null; + } + + private TreeNode PreviousVisibleTreeNode(TreeNode treeNode) + { + var nodes = this.Descendants((child) => child.Visible).ToList(); + + var selectedIndex = nodes.IndexOf(SelectedNode); + + if (selectedIndex > 0) + { + return nodes[selectedIndex - 1]; + } + + return null; + } + public TreeNode SelectedNode { get => _selectedNode; @@ -211,9 +270,12 @@ namespace MatterHackers.MatterControl.CustomWidgets { OnBeforeSelect(null); + var hadFocus = false; + // if the current selection (before change) is !null than clear its background color if (_selectedNode != null) { + hadFocus = _selectedNode.ContainsFocus; _selectedNode.HighlightRegion.BackgroundColor = Color.Transparent; } @@ -234,7 +296,11 @@ namespace MatterHackers.MatterControl.CustomWidgets _selectedNode.HighlightRegion.BackgroundColor = theme.AccentMimimalOverlay; } - this.ScrollIntoView(_selectedNode); + this.ScrollIntoView(_selectedNode);//, ScrollAmount.Minimum); + if (hadFocus) + { + _selectedNode?.Focus(); + } OnAfterSelect(null); } diff --git a/MatterControlLib/Library/Widgets/HardwarePage/PrinterDetails.cs b/MatterControlLib/Library/Widgets/HardwarePage/PrinterDetails.cs index b63ae7c3a..398d4bf00 100644 --- a/MatterControlLib/Library/Widgets/HardwarePage/PrinterDetails.cs +++ b/MatterControlLib/Library/Widgets/HardwarePage/PrinterDetails.cs @@ -27,7 +27,6 @@ of the authors and should not be interpreted as representing official policies, either expressed or implied, of the FreeBSD Project. */ - using System; using System.Diagnostics; using System.IO; @@ -114,6 +113,25 @@ namespace MatterHackers.MatterControl.Library.Widgets.HardwarePage { var result = JsonConvert.DeserializeObject(json); productDataContainer.RemoveAllChildren(); + + foreach (var addOn in result.ProductSku.ProductListing.AddOns) + { + WebCache.RetrieveText($"https://mh-pls-prod.appspot.com/p/1/product-sid/{addOn.AddOnSkuReference}?IncludeListingData=True", + (addOnJson) => + { + var addOnResult = JsonConvert.DeserializeObject(addOnJson); + + var icon = new ImageBuffer(80, 0); + + if (addOnResult?.ProductSku?.FeaturedImage?.ImageUrl != null) + { + WebCache.RetrieveImageAsync(icon, addOnResult.ProductSku.FeaturedImage.ImageUrl, scaleToImageX: true); + } + + addOn.Icon = icon; + }); + } + CreateProductDataWidgets(result.ProductSku); }); }); @@ -193,18 +211,9 @@ namespace MatterHackers.MatterControl.Library.Widgets.HardwarePage theme.ApplyBoxStyle(addonsSection); addonsSection.Margin = addonsSection.Margin.Clone(left: 0); - - foreach (var item in product.ProductListing.AddOns) { - var icon = new ImageBuffer(80, 0); - - if (item.FeaturedImage != null) - { - WebCache.RetrieveImageAsync(icon, item.FeaturedImage.ImageUrl, scaleToImageX: true); - } - - var addOnRow = new AddOnRow(item.AddOnTitle, theme, null, icon) + var addOnRow = new AddOnRow(item.AddOnTitle, theme, null, item.Icon) { HAnchor = HAnchor.Stretch, Cursor = Cursors.Hand diff --git a/MatterControlLib/Library/Widgets/HardwarePage/ProductSidData.cs b/MatterControlLib/Library/Widgets/HardwarePage/ProductSidData.cs index 90c76c997..a17af6ab2 100644 --- a/MatterControlLib/Library/Widgets/HardwarePage/ProductSidData.cs +++ b/MatterControlLib/Library/Widgets/HardwarePage/ProductSidData.cs @@ -27,6 +27,7 @@ of the authors and should not be interpreted as representing official policies, either expressed or implied, of the FreeBSD Project. */ +using MatterHackers.Agg.Image; using System.Collections.Generic; namespace MatterHackers.MatterControl.Library.Widgets.HardwarePage @@ -54,19 +55,20 @@ namespace MatterHackers.MatterControl.Library.Widgets.HardwarePage public class ProductListing { public string ActiveSkuCount { get; set; } + public List ActiveSkus { get; set; } + public List AddOns { get; set; } } public class AddOns { - public string AddOnListingReference { get; set; } public string AddOnSkuReference { get; set; } + public string AddOnTitle { get; set; } - public FeaturedImage FeaturedImage { get; set; } - public bool ListingIsActive { get; set; } - public int QuantityAvailable { get; set; } - public bool SingleSku { get; set; } - public bool SkuIsActive { get; set; } + + public object AddOnListingReference { get; set; } + + public ImageBuffer Icon { get; set; } = new ImageBuffer(80, 0); } } diff --git a/Submodules/agg-sharp b/Submodules/agg-sharp index 8a78a0dbe..3b210ed22 160000 --- a/Submodules/agg-sharp +++ b/Submodules/agg-sharp @@ -1 +1 @@ -Subproject commit 8a78a0dbe0bcd7940a601bb56cbd94eaa8918843 +Subproject commit 3b210ed2206930cb7d7d6cac176ee3f1eef65a15 From 0e3a40959520b2689a6480aeb1a9be9af9bb1647 Mon Sep 17 00:00:00 2001 From: LarsBrubaker Date: Sat, 16 May 2020 22:35:25 -0700 Subject: [PATCH 2/2] latest ms and agg --- MatterControlLib/CustomWidgets/TreeView/TreeNode.cs | 12 +----------- Submodules/MatterSlice | 2 +- Submodules/agg-sharp | 2 +- 3 files changed, 3 insertions(+), 13 deletions(-) diff --git a/MatterControlLib/CustomWidgets/TreeView/TreeNode.cs b/MatterControlLib/CustomWidgets/TreeView/TreeNode.cs index 3ee50a7ad..f312f70e6 100644 --- a/MatterControlLib/CustomWidgets/TreeView/TreeNode.cs +++ b/MatterControlLib/CustomWidgets/TreeView/TreeNode.cs @@ -164,6 +164,7 @@ namespace MatterHackers.MatterControl.CustomWidgets // navigate back up to the parent of this node TreeView.SelectedNode = this.NodeParent; } + restoreFocus = false; } else @@ -369,7 +370,6 @@ namespace MatterHackers.MatterControl.CustomWidgets /// public int Level { get; } - // // Summary: // Gets the next sibling tree node. // @@ -377,7 +377,6 @@ namespace MatterHackers.MatterControl.CustomWidgets // A TreeNode that represents the next sibling tree node. public TreeNode NextNode { get; } - // // Summary: // Gets the next visible tree node. // @@ -385,7 +384,6 @@ namespace MatterHackers.MatterControl.CustomWidgets // A TreeNode that represents the next visible tree node. public TreeNode NextVisibleNode { get; } - // // Summary: // Gets or sets the font that is used to display the text on the tree node label. // @@ -393,7 +391,6 @@ namespace MatterHackers.MatterControl.CustomWidgets // The StyledTypeFace that is used to display the text on the tree node label. public StyledTypeFace NodeFont { get; set; } - // // Summary: // Gets the parent tree node of the current tree node. // @@ -406,7 +403,6 @@ namespace MatterHackers.MatterControl.CustomWidgets public int PointSize { get; set; } - // // Summary: // Gets the previous sibling tree node. // @@ -414,7 +410,6 @@ namespace MatterHackers.MatterControl.CustomWidgets // A TreeNode that represents the previous sibling tree node. public TreeNode PrevNode { get; } - // // Summary: // Gets the previous visible tree node. // @@ -422,7 +417,6 @@ namespace MatterHackers.MatterControl.CustomWidgets // A TreeNode that represents the previous visible tree node. public TreeNode PrevVisibleNode { get; } - // // Summary: // Gets a value indicating whether the tree node is in the selected state. // @@ -441,7 +435,6 @@ namespace MatterHackers.MatterControl.CustomWidgets } } - // // Summary: // Gets or sets the image list index value of the image that is displayed when the // tree node is in the selected state. @@ -450,7 +443,6 @@ namespace MatterHackers.MatterControl.CustomWidgets // A zero-based index value that represents the image position in an ImageList. public ImageBuffer SelectedImage { get; set; } - // // Summary: // Gets or sets the index of the image that is used to indicate the state of the // TreeNode when the parent TreeView has @@ -464,7 +456,6 @@ namespace MatterHackers.MatterControl.CustomWidgets // The specified index is less than -1 or greater than 14. public ImageBuffer StateImage { get; set; } - // // Summary: // Gets or sets the object that contains data about the tree node. // @@ -474,7 +465,6 @@ namespace MatterHackers.MatterControl.CustomWidgets public Color TextColor { get; set; } - // // Summary: // Gets the parent tree view that the tree node is assigned to. // diff --git a/Submodules/MatterSlice b/Submodules/MatterSlice index 17cac6f9e..d58cefe19 160000 --- a/Submodules/MatterSlice +++ b/Submodules/MatterSlice @@ -1 +1 @@ -Subproject commit 17cac6f9ede0580fc446f7c4b0cc48e4ec7349f3 +Subproject commit d58cefe19c7d52038b7173c551555e22586480f0 diff --git a/Submodules/agg-sharp b/Submodules/agg-sharp index 3b210ed22..607eb087a 160000 --- a/Submodules/agg-sharp +++ b/Submodules/agg-sharp @@ -1 +1 @@ -Subproject commit 3b210ed2206930cb7d7d6cac176ee3f1eef65a15 +Subproject commit 607eb087a5170d299b8af1ef4161b9cf9843154c