Merge embedded types into Tabs.cs

This commit is contained in:
John Lewin 2018-01-22 08:42:18 -08:00
parent 9fc9337aa4
commit f0aa5db046
2 changed files with 206 additions and 209 deletions

View file

@ -28,15 +28,221 @@ either expressed or implied, of the FreeBSD Project.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using MatterHackers.Agg;
using MatterHackers.Agg.Image;
using MatterHackers.Agg.Platform;
using MatterHackers.Agg.UI;
using MatterHackers.Agg.VertexSource;
using MatterHackers.Localizations;
using MatterHackers.VectorMath;
namespace MatterHackers.MatterControl.PartPreviewWindow
{
public interface ITab
{
GuiWidget TabContent { get; }
}
/// <summary>
/// A toolbar and associated tab body
/// </summary>
public class SimpleTabs : FlowLayoutWidget
{
public SimpleTabs(ThemeConfig theme, GuiWidget rightAnchorItem = null)
: base(FlowDirection.TopToBottom)
{
this.TabContainer = this;
if (rightAnchorItem == null)
{
TabBar = new OverflowBar(theme)
{
HAnchor = HAnchor.Stretch,
VAnchor = VAnchor.Fit
};
}
else
{
TabBar = new Toolbar(rightAnchorItem)
{
HAnchor = HAnchor.Stretch,
VAnchor = VAnchor.Fit
};
}
this.AddChild(this.TabBar);
}
public Toolbar TabBar { get; }
public GuiWidget TabContainer { get; protected set; }
public event EventHandler ActiveTabChanged;
private List<ITab> _allTabs = new List<ITab>();
public IEnumerable<ITab> AllTabs => _allTabs;
public int TabCount => _allTabs.Count;
public void AddTab(GuiWidget tabWidget, int position = -1)
{
var iTab = tabWidget as ITab;
_allTabs.Add(tabWidget as ITab);
tabWidget.Click += TabWidget_Click;
this.TabBar.ActionArea.AddChild(tabWidget, position);
this.TabContainer.AddChild(iTab.TabContent);
}
private void TabWidget_Click(object sender, MouseEventArgs e)
{
this.ActiveTab = sender as ITab;
//this.SelectedTabIndex = this.Children.IndexOf(sender as GuiWidget);
}
public int SelectedTabIndex
{
get => _allTabs.IndexOf(this.ActiveTab);
set
{
this.ActiveTab = _allTabs[value];
}
}
internal void RemoveTab(ITab tab)
{
_allTabs.Remove(tab);
TabBar.ActionArea.RemoveChild(tab as GuiWidget);
this.TabContainer.RemoveChild(tab.TabContent);
ActiveTab = _allTabs.LastOrDefault();
}
private ITab _activeTab;
public ITab ActiveTab
{
get => _activeTab;
set
{
if (_activeTab != value)
{
_activeTab = value;
var clickedWidget = value as GuiWidget;
foreach (var tab in _allTabs)
{
tab.TabContent.Visible = (tab == clickedWidget);
}
this.OnActiveTabChanged();
}
}
}
public override void AddChild(GuiWidget childToAdd, int indexInChildrenList = -1)
{
if (this.TabContainer == this)
{
base.AddChild(childToAdd, indexInChildrenList);
}
else
{
this.TabContainer.AddChild(childToAdd, indexInChildrenList);
}
}
protected virtual void OnActiveTabChanged()
{
this.ActiveTabChanged?.Invoke(this, null);
}
}
public class ChromeTabs : SimpleTabs
{
private NewTabButton plusTabButton;
public ChromeTabs(GuiWidget rightAnchorItem, ThemeConfig theme)
: base(theme, rightAnchorItem)
{
// TODO: add in the printers and designs that are currently open (or were open last run).
var leadingTabAdornment = new GuiWidget()
{
MinimumSize = new Vector2(16, theme.shortButtonHeight),
VAnchor = VAnchor.Bottom
};
leadingTabAdornment.AfterDraw += (s, e) =>
{
var firstItem = this.AllTabs.OfType<ChromeTab>().FirstOrDefault();
ChromeTab.DrawTabLowerRight(e.graphics2D, leadingTabAdornment.LocalBounds, (firstItem == this.ActiveTab) ? theme.ActiveTabColor : theme.InactiveTabColor);
};
this.TabBar.ActionArea.AddChild(leadingTabAdornment);
// TODO: add in the printers and designs that are currently open (or were open last run).
plusTabButton = new NewTabButton(
AggContext.StaticData.LoadIcon("fa-plus_12.png", IconColor.Theme),
this,
theme)
{
VAnchor = VAnchor.Bottom,
MinimumSize = new Vector2(16, theme.shortButtonHeight),
ToolTipText = "Create New".Localize()
};
plusTabButton.IconButton.Click += (s, e) =>
{
this.AddTab(
new ChromeTab("New Tab".Localize(), this, this.NewTabPage(), theme)
{
MinimumSize = new Vector2(0, theme.shortButtonHeight)
});
};
this.TabBar.ActionArea.AddChild(plusTabButton);
}
public void AddTab(GuiWidget tab)
{
var position = this.TabBar.ActionArea.GetChildIndex(plusTabButton);
if (tab is ChromeTab chromeTab)
{
chromeTab.PreviousTab = this.AllTabs.OfType<ChromeTab>().LastOrDefault();
if (chromeTab.PreviousTab != null)
{
chromeTab.PreviousTab.NextTab = chromeTab;
}
this.AddTab(tab, position);
chromeTab.CloseClicked += ChromeTab_CloseClicked;
this.ActiveTab = chromeTab;
}
}
private async void ChromeTab_CloseClicked(object sender, EventArgs e)
{
if (sender is ITab tab
&& tab.TabContent is PrinterTabPage)
{
await ApplicationController.Instance.ClearActivePrinter();
}
}
public Func<GuiWidget> NewTabPage { get; set; }
protected override void OnActiveTabChanged()
{
plusTabButton.LastTab = this.AllTabs.LastOrDefault();
base.OnActiveTabChanged();
}
}
public class SimpleTab : GuiWidget, ITab
{
public event EventHandler CloseClicked;