mattercontrol/MatterControlLib/SetupWizard/HelpPage.cs

379 lines
11 KiB
C#
Raw Normal View History

2018-04-24 10:15:01 -07:00
/*
2018-04-24 13:32:48 -07:00
Copyright (c) 2018, Lars Brubaker, John Lewin
2018-04-24 10:15:01 -07:00
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.Collections.Generic;
2018-06-05 09:02:35 -07:00
using System.Linq;
using Markdig.Agg;
2018-04-24 10:15:01 -07:00
using MatterHackers.Agg;
2018-04-24 16:22:45 -07:00
using MatterHackers.Agg.Image;
2018-04-24 10:15:01 -07:00
using MatterHackers.Agg.UI;
using MatterHackers.Localizations;
using MatterHackers.MatterControl.CustomWidgets;
2018-06-05 09:02:35 -07:00
using MatterHackers.MatterControl.PartPreviewWindow;
2018-04-25 13:48:07 -07:00
using MatterHackers.VectorMath;
2018-04-24 10:15:01 -07:00
namespace MatterHackers.MatterControl
{
public class HelpArticle
{
public string Name;
public string Path;
2018-08-28 18:45:58 -07:00
public string ArticleKey { get; set; }
public List<HelpArticle> Children { get; set; } = new List<HelpArticle>();
2018-06-27 19:36:09 -07:00
}
public class HelpPage : DialogPage
2018-04-24 10:15:01 -07:00
{
private TreeView treeView;
2018-06-28 14:41:43 -07:00
private string guideKey = null;
public HelpPage(string guideKey = null)
: base("Close".Localize())
2018-04-24 10:15:01 -07:00
{
2018-06-28 14:41:43 -07:00
WindowSize = new Vector2(940, 700);
2018-06-28 14:41:43 -07:00
this.guideKey = guideKey;
this.WindowTitle = "MatterControl " + "Help".Localize();
2018-05-09 20:40:04 -07:00
this.HeaderText = "How to succeed with MatterControl".Localize();
2018-10-16 21:21:42 -07:00
this.ChildBorderColor = theme.BorderColor40;
2018-04-24 10:15:01 -07:00
2018-04-24 13:32:48 -07:00
var tabControl = new SimpleTabs(theme, new GuiWidget())
2018-04-24 10:15:01 -07:00
{
HAnchor = HAnchor.Stretch,
2018-04-24 13:32:48 -07:00
VAnchor = VAnchor.Stretch
2018-04-24 10:15:01 -07:00
};
2018-05-08 17:23:55 -07:00
tabControl.TabBar.BackgroundColor = theme.TabBarBackground;
2018-04-24 13:32:48 -07:00
contentRow.AddChild(tabControl);
contentRow.Padding = 0;
2018-04-24 10:15:01 -07:00
// add the mouse commands
2018-04-24 13:32:48 -07:00
var mouseControls = new FlowLayoutWidget()
2018-04-24 10:15:01 -07:00
{
2018-04-24 13:32:48 -07:00
HAnchor = HAnchor.Fit | HAnchor.Center,
Padding = theme.DefaultContainerPadding
2018-04-24 10:15:01 -07:00
};
var mouseTab = new ToolTab("Mouse", "Mouse".Localize(), tabControl, mouseControls, theme, hasClose: false)
2018-04-25 13:48:07 -07:00
{
// this can be used to navigate to this tab on construction
Name = "Mouse Tab"
};
2018-04-24 13:32:48 -07:00
tabControl.AddTab(mouseTab);
var mouseKeys = new FlowLayoutWidget(FlowDirection.TopToBottom);
2018-04-24 10:15:01 -07:00
mouseControls.AddChild(mouseKeys);
var mouseActions = new FlowLayoutWidget(FlowDirection.TopToBottom)
{
2018-04-24 13:32:48 -07:00
Border = new BorderDouble(1, 0, 0, 0),
BorderColor = this.ChildBorderColor
2018-04-24 10:15:01 -07:00
};
mouseControls.AddChild(mouseActions);
2018-04-24 13:32:48 -07:00
var mouseKeyActions = new List<(string key, string action)>(new(string, string)[]
2018-04-24 10:15:01 -07:00
{
("left click".Localize(), "Make Selection".Localize()),
("left click".Localize() + " + shift","Add to Selection".Localize()),
("left click".Localize() + " + ctrl","Toggle Selection".Localize()),
("left drag".Localize(), "Rubber Band Selection".Localize()),
("left drag".Localize(), "Move Part".Localize()),
("left drag".Localize() + " + shift", "Move Part Constrained".Localize()),
("left drag".Localize() + " + shift + ctrl", "Pan View".Localize()),
("left drag".Localize() + " + ctrl","Rotate View".Localize()),
("middle drag".Localize(), "Pan View".Localize()),
("right drag".Localize(), "Rotate View".Localize()),
("wheel".Localize(), "Zoom".Localize())
2018-04-24 10:15:01 -07:00
});
AddContent(mouseKeys, "Mouse".Localize(), true, true);
AddContent(mouseActions, "Action".Localize(), false, true);
foreach (var keyAction in mouseKeyActions)
{
AddContent(mouseKeys, keyAction.key, true, false);
AddContent(mouseActions, keyAction.action, false, false);
}
2018-04-24 13:32:48 -07:00
// center the vertical bar in the view by adding margin to the small side
var left = Math.Max(0, mouseActions.Width - mouseKeys.Width);
var right = Math.Max(0, mouseKeys.Width - mouseActions.Width);
mouseControls.Margin = new BorderDouble(left, 0, right, 0);
2018-04-24 10:15:01 -07:00
// now add the keyboard commands
2018-04-24 13:32:48 -07:00
var shortcutKeys = new FlowLayoutWidget()
2018-04-24 10:15:01 -07:00
{
2018-04-24 13:32:48 -07:00
HAnchor = HAnchor.Fit | HAnchor.Center,
Padding = theme.DefaultContainerPadding
2018-04-24 10:15:01 -07:00
};
var keyboardTab = new ToolTab("Keys", "Keys".Localize(), tabControl, shortcutKeys, theme, hasClose: false)
2018-04-25 13:48:07 -07:00
{
// this can be used to navigate to this tab on construction
Name = "Keys Tab"
};
2018-04-24 13:32:48 -07:00
tabControl.AddTab(keyboardTab);
2018-04-24 10:15:01 -07:00
2018-04-24 13:32:48 -07:00
var keys = new FlowLayoutWidget(FlowDirection.TopToBottom);
2018-04-24 10:15:01 -07:00
shortcutKeys.AddChild(keys);
2018-04-24 13:32:48 -07:00
var actions = new FlowLayoutWidget(FlowDirection.TopToBottom)
2018-04-24 10:15:01 -07:00
{
2018-04-24 13:32:48 -07:00
Border = new BorderDouble(1, 0, 0, 0),
BorderColor = this.ChildBorderColor
2018-04-24 10:15:01 -07:00
};
2018-04-24 13:32:48 -07:00
shortcutKeys.AddChild(actions);
2018-04-24 10:15:01 -07:00
2018-04-24 13:32:48 -07:00
tabControl.TabBar.Padding = theme.ToolbarPadding.Clone(left: 2, bottom: 0);
var keyActions = new List<(string key, string action)>(new(string, string)[]
2018-04-24 10:15:01 -07:00
{
("F1","Show Help".Localize()),
("ctrl + +","Zoom in".Localize()),
("ctrl + -","Zoom out".Localize()),
2018-04-24 10:15:01 -07:00
("← → ↑ ↓","Rotate".Localize()),
2018-04-25 13:48:07 -07:00
("shift + ← → ↑ ↓","Pan".Localize()),
2018-04-24 10:15:01 -07:00
//("f","Zoom to fit".Localize()),
("w","Zoom to window".Localize()),
("ctrl + s","Save".Localize()),
2018-04-25 13:48:07 -07:00
("ctrl + z","Undo".Localize()),
("ctrl + y","Redo".Localize()),
("ctrl + p","Print".Localize()),
2018-04-24 10:15:01 -07:00
("delete","Delete selection".Localize()),
("space bar","Clear selection".Localize()),
("esc","Cancel command".Localize()),
//("enter","Accept command".Localize())
});
AddContent(keys, "Keys".Localize(), true, true);
2018-04-24 13:32:48 -07:00
AddContent(actions, "Action".Localize(), false, true);
2018-04-24 10:15:01 -07:00
foreach (var keyAction in keyActions)
{
AddContent(keys, keyAction.key, true, false);
2018-04-24 13:32:48 -07:00
AddContent(actions, keyAction.action, false, false);
2018-04-24 10:15:01 -07:00
}
2018-04-24 13:32:48 -07:00
// center the vertical bar in the view by adding margin to the small side
left = Math.Max(0, actions.Width - keys.Width);
right = Math.Max(0, keys.Width - actions.Width);
shortcutKeys.Margin = new BorderDouble(left, 0, right, 0);
var guideSectionContainer = new FlowLayoutWidget(FlowDirection.TopToBottom)
2018-04-24 13:32:48 -07:00
{
HAnchor = HAnchor.Stretch,
2018-06-05 09:19:43 -07:00
VAnchor = VAnchor.Stretch,
2018-04-24 13:32:48 -07:00
};
var guideTab = new ToolTab("Guides", "Guides".Localize(), tabControl, guideSectionContainer, theme, hasClose: false)
2018-04-25 13:48:07 -07:00
{
// this can be used to navigate to this tab on construction
Name = "Guides Tab"
2018-04-25 13:48:07 -07:00
};
tabControl.AddTab(guideTab);
2018-04-24 13:32:48 -07:00
AddGuides(guideSectionContainer);
2018-04-24 13:32:48 -07:00
// If guideKey is empty, switch to first tab
if (string.IsNullOrEmpty(guideKey))
{
tabControl.SelectedTabIndex = 0;
}
else
2018-04-25 13:48:07 -07:00
{
// Otherwise switch to guides tab and select the target item
tabControl.SelectedTabIndex = tabControl.GetTabIndex(guideTab);
2018-04-25 13:48:07 -07:00
}
2018-04-24 10:15:01 -07:00
}
private void AddGuides(FlowLayoutWidget guideContainer)
2018-04-24 10:15:01 -07:00
{
2018-04-24 16:22:45 -07:00
var sequence = new ImageSequence()
{
FramesPerSecond = 3,
2018-04-24 16:22:45 -07:00
};
sequence.AddImage(new ImageBuffer(1, 1));
2018-04-24 16:08:04 -07:00
2018-06-25 18:10:57 -07:00
var description = new GuiWidget();
2018-07-12 09:22:28 -07:00
var markdownWidget = new MarkdownWidget(theme)
2018-04-24 16:08:04 -07:00
{
BackgroundColor = theme.ResolveColor(theme.BackgroundColor, new Color(Color.White, 20)),
Padding = new BorderDouble(left: theme.DefaultContainerPadding / 2)
2018-04-24 16:08:04 -07:00
};
treeView = new TreeView(theme)
2018-04-24 16:08:04 -07:00
{
HAnchor = HAnchor.Stretch,
VAnchor = VAnchor.Fit | VAnchor.Top,
2018-04-24 16:08:04 -07:00
};
treeView.AfterSelect += (s, e) =>
2018-04-24 16:08:04 -07:00
{
if (treeView.SelectedNode.Tag is HelpArticle article)
2018-04-24 16:08:04 -07:00
{
if (!string.IsNullOrWhiteSpace(article.Path))
{
markdownWidget.LoadUri(new Uri(ApplicationController.Instance.HelpArticleSource, article.Path), sourceArticle: article);
}
else
{
// Switch to empty content when path article lacks path
markdownWidget.Markdown = "";
}
2018-06-27 19:36:09 -07:00
}
};
2018-04-24 16:08:04 -07:00
2018-06-27 19:36:09 -07:00
TreeNode rootNode = null;
2018-04-25 13:48:07 -07:00
treeView.Load += (s, e) =>
{
rootNode.Expanded = true;
if (treeView.SelectedNode == null)
{
2018-06-28 14:41:43 -07:00
if (string.IsNullOrEmpty(guideKey))
{
treeView.SelectedNode = rootNode.Nodes.FirstOrDefault();
}
else
{
2018-08-28 18:55:54 -07:00
if (initialSelection != null)
2018-08-28 18:45:58 -07:00
{
2018-08-28 18:55:54 -07:00
treeView.SelectedNode = initialSelection;
2018-08-28 18:45:58 -07:00
}
2018-08-28 18:55:54 -07:00
// TODO: Implement or revise .Expanded
2018-08-28 18:45:58 -07:00
if (treeView.SelectedNode != null)
2018-06-28 14:41:43 -07:00
{
2018-08-28 18:45:58 -07:00
foreach (var ancestor in treeView.SelectedNode.Parents<TreeNode>())
2018-06-28 14:41:43 -07:00
{
2018-08-28 18:45:58 -07:00
ancestor.Expanded = true;
2018-06-28 14:41:43 -07:00
}
}
}
}
if (treeView.SelectedNode == null)
{
treeView.SelectedNode = rootNode;
}
};
double maxMenuItemWidth = 0;
rootNode = ProcessTree(ApplicationController.Instance.HelpArticles);
2018-06-27 19:36:09 -07:00
rootNode.Text = "Help";
rootNode.TreeView = treeView;
treeView.AddChild(rootNode);
2018-06-27 19:36:09 -07:00
maxMenuItemWidth = Math.Max(maxMenuItemWidth, rootNode.Width);
2018-04-24 16:08:04 -07:00
var splitter = new Splitter()
{
HAnchor = HAnchor.Stretch,
VAnchor = VAnchor.Stretch,
SplitterBackground = theme.SplitterBackground
};
2018-06-27 19:36:09 -07:00
splitter.SplitterDistance = maxMenuItemWidth + 130;
splitter.Panel1.Padding = new BorderDouble(theme.DefaultContainerPadding).Clone(right: 2);
splitter.Panel1.AddChild(treeView);
splitter.Panel2.AddChild(markdownWidget);
guideContainer.AddChild(splitter);
2018-04-24 10:15:01 -07:00
}
2018-08-28 18:55:54 -07:00
private TreeNode initialSelection = null;
2018-08-28 18:45:58 -07:00
private TreeNode ProcessTree(HelpArticle container)
2018-06-27 19:36:09 -07:00
{
2018-07-12 09:22:28 -07:00
var treeNode = new TreeNode(theme, false)
2018-06-27 19:36:09 -07:00
{
Text = container.Name,
Tag = container
2018-06-27 19:36:09 -07:00
};
2018-07-05 11:16:13 -07:00
foreach (var item in container.Children.OrderBy(i => i.Children.Count == 0).ThenBy(i => i.Name))
2018-06-27 19:36:09 -07:00
{
if (item.Children.Count > 0)
{
treeNode.Nodes.Add(ProcessTree(item));
}
else
2018-06-27 19:36:09 -07:00
{
2018-08-28 18:45:58 -07:00
var newNode = new TreeNode(theme, false)
{
Text = item.Name,
Tag = item
2018-08-28 18:45:58 -07:00
};
2018-08-28 18:55:54 -07:00
if (item.Name == guideKey
|| (guideKey != null
&& item.ArticleKey == guideKey
2018-08-28 18:55:54 -07:00
&& ApplicationController.Instance.HelpArticlesByID.ContainsKey(guideKey)))
2018-08-28 18:45:58 -07:00
{
2018-08-28 18:55:54 -07:00
initialSelection = newNode;
2018-08-28 18:45:58 -07:00
}
treeNode.Nodes.Add(newNode);
}
2018-06-27 19:36:09 -07:00
}
return treeNode;
}
2018-04-24 13:32:48 -07:00
public Color ChildBorderColor { get; private set; }
2018-04-24 10:15:01 -07:00
private void AddContent(GuiWidget column, string text, bool left, bool bold)
{
var container = new GuiWidget()
{
HAnchor = HAnchor.Fit | (left ? HAnchor.Right: HAnchor.Left),
VAnchor = VAnchor.Fit
};
var content = new TextWidget(text, bold: bold, textColor: theme.TextColor, pointSize: theme.DefaultFontSize)
2018-04-24 10:15:01 -07:00
{
Margin = (left ? new BorderDouble(5, 3, 10, 3) : new BorderDouble(10, 3, 5, 3))
};
container.AddChild(content);
column.AddChild(container);
column.AddChild(new GuiWidget()
{
HAnchor = HAnchor.Stretch,
Border = new BorderDouble(0, 1, 0, 0),
2018-04-24 13:32:48 -07:00
BorderColor = this.ChildBorderColor,
2018-04-24 10:15:01 -07:00
});
}
}
}