mattercontrol/MatterControlLib/CustomWidgets/DockingTabControl.cs

507 lines
14 KiB
C#
Raw Normal View History

/*
2017-06-12 14:19:04 -07:00
Copyright (c) 2017, Lars Brubaker, 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;
2017-06-12 14:19:04 -07:00
using System.Collections.Generic;
using System.Linq;
using MatterHackers.Agg;
using MatterHackers.Agg.Font;
using MatterHackers.Agg.Platform;
using MatterHackers.Agg.Transform;
2017-06-11 15:17:25 -07:00
using MatterHackers.Agg.UI;
using MatterHackers.Agg.VertexSource;
2017-06-11 15:17:25 -07:00
using MatterHackers.MatterControl.PartPreviewWindow;
using MatterHackers.VectorMath;
namespace MatterHackers.MatterControl.CustomWidgets
{
public enum DockSide { Left, Bottom, Right, Top };
2017-11-06 17:06:04 -08:00
public class DockingTabControl : FlowLayoutWidget
{
public int MinDockingWidth = 400 * (int)GuiWidget.DeviceScale;
protected GuiWidget widgetTodockTo;
private Dictionary<string, GuiWidget> allTabs = new Dictionary<string, GuiWidget>();
private PrinterConfig printer;
private ThemeConfig theme;
2018-07-12 09:22:28 -07:00
public DockingTabControl(GuiWidget widgetTodockTo, DockSide dockSide, PrinterConfig printer, ThemeConfig theme)
2017-11-06 17:06:04 -08:00
: base (FlowDirection.TopToBottom)
{
2018-07-12 09:22:28 -07:00
this.theme = theme;
this.printer = printer;
this.widgetTodockTo = widgetTodockTo;
this.DockSide = dockSide;
2018-01-12 09:52:02 -08:00
this.BorderColor = theme.MinimalShade;
this.Border = new BorderDouble(left: 1);
// Add dummy widget to ensure OnLoad fires
this.AddChild(new GuiWidget(10, 10));
}
public event EventHandler PinStatusChanged;
public bool ControlIsPinned
{
get => printer.ViewState.SliceSettingsTabPinned;
set
{
if (this.ControlIsPinned != value)
{
printer.ViewState.SliceSettingsTabPinned = value;
PinStatusChanged?.Invoke(this, null);
}
}
}
public DockSide DockSide { get; set; }
2017-06-11 15:17:25 -07:00
public void AddPage(string name, GuiWidget widget)
{
2017-06-12 10:01:50 -07:00
allTabs.Add(name, widget);
if (formHasLoaded)
{
Rebuild();
}
}
public void RemovePage(string name)
{
if (allTabs.ContainsKey(name))
{
allTabs.Remove(name);
this.Rebuild();
}
}
public override void OnLoad(EventArgs args)
{
base.OnLoad(args);
this.Rebuild();
2017-06-12 10:01:50 -07:00
}
2017-06-11 15:17:25 -07:00
public override void Initialize()
2017-06-12 10:01:50 -07:00
{
base.Initialize();
2017-11-06 17:06:04 -08:00
this.VAnchor = VAnchor.Stretch;
this.HAnchor = HAnchor.Fit;
}
private GuiWidget CreatePinButton()
{
string imageFile = this.ControlIsPinned ? "Pushpin_16x.png" : "PushpinUnpin_16x.png";
2018-05-06 07:57:15 -07:00
var imageWidget = new IconButton(AggContext.StaticData.LoadIcon(imageFile, 16, 16, theme.InvertIcons), theme)
{
Name = "Pin Settings Button"
};
imageWidget.Click += (s, e) =>
{
this.ControlIsPinned = !this.ControlIsPinned;
this.printer.ViewState.DockWindowFloating = false;
2017-09-28 23:36:10 -07:00
UiThread.RunOnIdle(this.Rebuild);
};
return imageWidget;
}
2017-09-28 23:42:43 -07:00
// Clamped to MinDockingWidth or value
private double _constrainedWidth;
private double ConstrainedWidth
{
2017-09-28 23:42:43 -07:00
get => Math.Max(MinDockingWidth, printer.ViewState.SliceSettingsWidth);
set
{
if (value > MinDockingWidth
&& _constrainedWidth != value)
{
_constrainedWidth = value;
printer.ViewState.SliceSettingsWidth = value;
}
}
}
private void Rebuild()
{
this.Focus();
foreach (var nameWidget in allTabs)
2017-06-11 15:17:25 -07:00
{
2017-06-12 10:01:50 -07:00
nameWidget.Value.Parent?.RemoveChild(nameWidget.Value);
nameWidget.Value.ClearRemovedFlag();
}
2017-06-11 15:17:25 -07:00
2017-11-06 17:06:04 -08:00
this.RemoveAllChildren();
2017-06-11 15:17:25 -07:00
SimpleTabs tabControl = null;
if (this.ControlIsPinned)
2017-06-12 10:01:50 -07:00
{
2018-05-21 16:42:40 -07:00
var resizePage = new LeftResizeContainer(theme)
2017-06-11 15:17:25 -07:00
{
Width = this.ConstrainedWidth,
VAnchor = VAnchor.Stretch,
SpliterBarColor = theme.SplitterBackground,
SplitterWidth = theme.SplitterWidth,
MinimumSize = new Vector2(this.MinDockingWidth, 0)
};
resizePage.BoundsChanged += (s, e) =>
{
this.ConstrainedWidth = resizePage.Width;
2017-06-11 15:17:25 -07:00
};
2017-06-12 14:19:04 -07:00
2018-01-21 21:07:14 -08:00
tabControl = new SimpleTabs(theme, this.CreatePinButton())
{
VAnchor = VAnchor.Stretch,
HAnchor = HAnchor.Stretch,
};
2018-05-08 17:23:55 -07:00
tabControl.TabBar.BackgroundColor = theme.TabBarBackground;
tabControl.ActiveTabChanged += (s, e) =>
{
printer.ViewState.SliceSettingsTabIndex = tabControl.SelectedTabIndex;
};
2017-06-12 10:01:50 -07:00
resizePage.AddChild(tabControl);
2017-11-06 17:06:04 -08:00
this.AddChild(resizePage);
2017-06-12 10:01:50 -07:00
}
2017-06-11 15:17:25 -07:00
int tabIndex = 0;
foreach (var kvp in allTabs)
2017-06-12 10:01:50 -07:00
{
string tabTitle = kvp.Key;
if (this.ControlIsPinned)
2017-06-11 15:17:25 -07:00
{
var content = new DockingWindowContent(this, kvp.Value, tabTitle, theme);
2017-06-12 14:19:04 -07:00
var tab = new ToolTab(
tabTitle,
tabControl,
content,
theme,
hasClose: kvp.Value is ConfigurePrinterWidget,
2018-01-12 14:05:04 -08:00
pointSize: theme.DefaultFontSize)
{
Name = tabTitle + " Tab",
InactiveTabColor = Color.Transparent,
ActiveTabColor = theme.TabBodyBackground
};
tab.CloseClicked += (s, e) =>
{
if (tab.Name == "Printer Tab")
{
printer.ViewState.ConfigurePrinterVisible = false;
}
};
tabControl.AddTab(tab);
2017-06-12 10:01:50 -07:00
}
else // control is floating
{
2018-05-21 16:42:40 -07:00
var resizeContainer = new LeftResizeContainer(theme)
2017-06-12 14:19:04 -07:00
{
Width = this.ConstrainedWidth,
2017-08-23 13:52:31 -07:00
VAnchor = VAnchor.Stretch,
HAnchor = HAnchor.Right,
SpliterBarColor = theme.SplitterBackground,
2018-04-07 22:51:10 -07:00
SplitterWidth = theme.SplitterWidth,
2017-06-12 14:19:04 -07:00
};
resizeContainer.AddChild(new DockingWindowContent(this, kvp.Value, tabTitle, theme)
2017-08-23 13:52:31 -07:00
{
BackgroundColor = theme.TabBodyBackground,
Width = this.ConstrainedWidth
2017-08-23 13:52:31 -07:00
});
int localTabIndex = tabIndex;
2017-08-23 13:52:31 -07:00
var settingsButton = new DockingTabButton(tabTitle, theme)
2017-08-23 13:52:31 -07:00
{
Name = $"{tabTitle} Sidebar",
PopupContent = resizeContainer,
PopupLayoutEngine = new UnpinnedLayoutEngine(resizeContainer, widgetTodockTo, DockSide)
2017-08-23 13:52:31 -07:00
};
settingsButton.Click += (s, e) =>
{
resizeContainer.Width = this.ConstrainedWidth;
this.printer.ViewState.SliceSettingsTabIndex = localTabIndex;
this.printer.ViewState.DockWindowFloating = true;
};
settingsButton.PopupWindowClosed += (s, e) =>
{
if (!ApplicationController.Instance.IsReloading)
{
this.printer.ViewState.DockWindowFloating = false;
}
};
this.AddChild(settingsButton);
if (this.printer.ViewState.DockWindowFloating
&& localTabIndex == this.printer.ViewState.SliceSettingsTabIndex)
{
UiThread.RunOnIdle(() =>
{
if (!settingsButton.HasBeenClosed && settingsButton.Parent != null)
{
settingsButton.ShowPopup();
}
});
}
2017-06-12 10:01:50 -07:00
}
tabIndex++;
2017-06-11 15:17:25 -07:00
}
if (this.ControlIsPinned)
{
tabControl.TabBar.Padding = new BorderDouble(right: theme.ToolbarPadding.Right);
if (printer.ViewState.SliceSettingsTabIndex < tabControl.TabCount)
{
tabControl.SelectedTabIndex = printer.ViewState.SliceSettingsTabIndex;
}
else
{
tabControl.SelectedTabIndex = 0;
}
}
2017-06-11 15:17:25 -07:00
}
private class DockingTabButton : PopupButton
{
private Color grayBorder;
private ThemeConfig theme;
public DockingTabButton(string tabTitle, ThemeConfig theme)
{
this.grayBorder = theme.GetBorderColor(theme.Colors.IsDarkTheme ? 45 : 55);
this.theme = theme;
this.HAnchor = HAnchor.Fit;
this.VAnchor = VAnchor.Fit | VAnchor.Center;
this.AlignToRightEdge = true;
this.MakeScrollable = false;
this.Border = new BorderDouble(right: 6);
this.BorderColor = grayBorder;
this.Margin = new BorderDouble(2, 8, 0, 0);
this.HoverColor = Color.Transparent;
var printer = new TypeFacePrinter(tabTitle, theme.DefaultFontSize * GuiWidget.DeviceScale);
var rotatedLabel = new VertexSourceApplyTransform(
printer,
Affine.NewRotation(MathHelper.DegreesToRadians(-90)));
var textBounds = rotatedLabel.GetBounds();
var bounds = new RectangleDouble(printer.TypeFaceStyle.DescentInPixels, textBounds.Bottom, printer.TypeFaceStyle.AscentInPixels, textBounds.Top);
rotatedLabel.Transform = ((Affine)rotatedLabel.Transform)
* Affine.NewTranslation(new Vector2(-printer.TypeFaceStyle.DescentInPixels, -bounds.Bottom));
this.AddChild(buttonView = new GuiWidget(bounds.Width, bounds.Height)
{
DoubleBuffer = true,
Margin = new BorderDouble(3, 1),
Selectable = false
});
buttonView.AfterDraw += (s, e) =>
{
2018-07-12 09:22:28 -07:00
e.Graphics2D.Render(rotatedLabel, theme.Colors.PrimaryTextColor);
};
}
public override void OnMouseEnterBounds(MouseEventArgs mouseEvent)
{
base.OnMouseEnterBounds(mouseEvent);
this.BorderColor = theme.Colors.PrimaryAccentColor;
}
public override void OnMouseLeaveBounds(MouseEventArgs mouseEvent)
{
base.OnMouseLeaveBounds(mouseEvent);
this.BorderColor = grayBorder;
}
}
2017-08-23 13:52:31 -07:00
private class DockingWindowContent : GuiWidget, IIgnoredPopupChild
{
internal DockingWindowContent(DockingTabControl dockingControl, GuiWidget child, string title, ThemeConfig theme)
2017-06-11 10:14:32 -07:00
{
2017-06-12 14:19:04 -07:00
var topToBottom = new FlowLayoutWidget(FlowDirection.TopToBottom)
2017-06-11 10:14:32 -07:00
{
VAnchor = VAnchor.Stretch,
HAnchor = HAnchor.Stretch
2017-06-11 10:14:32 -07:00
};
if (!dockingControl.ControlIsPinned)
2017-06-12 10:08:13 -07:00
{
var titleBar = new FlowLayoutWidget()
{
HAnchor = HAnchor.Stretch,
VAnchor = VAnchor.Fit,
2018-05-08 17:23:55 -07:00
BackgroundColor = theme.TabBarBackground,
};
2018-07-12 09:22:28 -07:00
titleBar.AddChild(new TextWidget(title, textColor: theme.Colors.PrimaryTextColor)
2017-06-12 14:19:04 -07:00
{
Margin = new BorderDouble(left: 8),
VAnchor = VAnchor.Center
2017-06-12 14:19:04 -07:00
});
titleBar.AddChild(new HorizontalSpacer());
2017-06-12 14:19:04 -07:00
titleBar.AddChild(dockingControl.CreatePinButton());
topToBottom.AddChild(titleBar);
}
2017-06-11 10:14:32 -07:00
topToBottom.AddChild(child);
2017-08-23 13:52:31 -07:00
HAnchor = HAnchor.Stretch;
VAnchor = VAnchor.Stretch;
2017-06-11 10:14:32 -07:00
AddChild(topToBottom);
}
}
}
public class UnpinnedLayoutEngine : IPopupLayoutEngine
{
protected GuiWidget widgetTodockTo;
private GuiWidget contentWidget;
private HashSet<GuiWidget> hookedParents = new HashSet<GuiWidget>();
private PopupWidget popupWidget;
public UnpinnedLayoutEngine(GuiWidget contentWidget, GuiWidget widgetTodockTo, DockSide dockSide)
{
this.contentWidget = contentWidget;
this.widgetTodockTo = widgetTodockTo;
DockSide = dockSide;
2017-08-23 13:52:31 -07:00
contentWidget.BoundsChanged += widgetRelativeTo_PositionChanged;
}
public DockSide DockSide { get; set; }
public double MaxHeight { get; private set; }
public void Closed()
{
// Unbind callbacks on parents for position_changed if we're closing
foreach (GuiWidget widget in hookedParents)
{
widget.PositionChanged -= widgetRelativeTo_PositionChanged;
widget.BoundsChanged -= widgetRelativeTo_PositionChanged;
}
2017-08-23 13:52:31 -07:00
hookedParents.Clear();
// Long lived originating item must be unregistered
widgetTodockTo.Closed -= widgetRelativeTo_Closed;
// Restore focus to originating widget on close
if (this.widgetTodockTo != null
&& !widgetTodockTo.HasBeenClosed)
{
// On menu close, select the first scrollable parent of the widgetRelativeTo
var scrollableParent = widgetTodockTo.Parents<ScrollableWidget>().FirstOrDefault();
if (scrollableParent != null)
{
scrollableParent.Focus();
}
}
}
public void ShowPopup(PopupWidget popupWidget)
{
this.popupWidget = popupWidget;
SystemWindow windowToAddTo = widgetTodockTo.Parents<SystemWindow>().FirstOrDefault();
windowToAddTo?.AddChild(popupWidget);
GuiWidget topParent = widgetTodockTo.Parent;
while (topParent.Parent != null
&& topParent as SystemWindow == null)
{
// Regrettably we don't know who it is that is the window that will actually think it is moving relative to its parent
// but we need to know anytime our widgetRelativeTo has been moved by any change, so we hook them all.
if (!hookedParents.Contains(topParent))
{
hookedParents.Add(topParent);
topParent.PositionChanged += widgetRelativeTo_PositionChanged;
topParent.BoundsChanged += widgetRelativeTo_PositionChanged;
}
topParent = topParent.Parent;
}
widgetRelativeTo_PositionChanged(widgetTodockTo, null);
widgetTodockTo.Closed += widgetRelativeTo_Closed;
}
private void widgetRelativeTo_Closed(object sender, EventArgs e)
{
// If the owning widget closed, so should we
popupWidget.CloseMenu();
}
private void widgetRelativeTo_PositionChanged(object sender, EventArgs e)
{
if (widgetTodockTo != null)
{
RectangleDouble bounds = widgetTodockTo.BoundsRelativeToParent;
GuiWidget topParent = widgetTodockTo.Parent;
while (topParent != null && topParent.Parent != null)
{
topParent.ParentToChildTransform.transform(ref bounds);
topParent = topParent.Parent;
}
switch (DockSide)
{
case DockSide.Left:
2017-06-18 10:27:55 -07:00
popupWidget.LocalBounds = new RectangleDouble(bounds.Left, bounds.Bottom, bounds.Left - contentWidget.Width, bounds.Top);
break;
case DockSide.Bottom:
throw new NotImplementedException();
case DockSide.Right:
2017-08-23 13:52:31 -07:00
popupWidget.HAnchor = HAnchor.Absolute;
popupWidget.LocalBounds = new RectangleDouble(bounds.Right - contentWidget.Width, bounds.Bottom, bounds.Right, bounds.Top);
break;
case DockSide.Top:
throw new NotImplementedException();
default:
throw new NotImplementedException();
}
}
}
}
2017-06-11 15:17:25 -07:00
}