mattercontrol/PartPreviewWindow/PartPreviewContent.cs
John Lewin 9967e14328 Switch to constant padding with right margin on buttons
- Previously layout was using variable padding with left margin
2018-05-17 12:41:00 -07:00

334 lines
No EOL
11 KiB
C#

/*
Copyright (c) 2018, 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;
using System.Linq;
using MatterHackers.Agg;
using MatterHackers.Agg.Platform;
using MatterHackers.Agg.UI;
using MatterHackers.Localizations;
using MatterHackers.MatterControl.PartPreviewWindow.PlusTab;
using MatterHackers.MatterControl.SlicerConfiguration;
using MatterHackers.VectorMath;
namespace MatterHackers.MatterControl.PartPreviewWindow
{
public class PartPreviewContent : FlowLayoutWidget
{
private EventHandler unregisterEvents;
private ChromeTab printerTab = null;
private ChromeTabs tabControl;
public PartPreviewContent()
: base(FlowDirection.TopToBottom)
{
var printer = ApplicationController.Instance.ActivePrinter;
var theme = ApplicationController.Instance.Theme;
this.AnchorAll();
var extensionArea = new LeftClipFlowLayoutWidget()
{
BackgroundColor = theme.TabBarBackground,
VAnchor = VAnchor.Stretch,
Padding = new BorderDouble(left: 8)
};
tabControl = new ChromeTabs(extensionArea, theme)
{
VAnchor = VAnchor.Stretch,
HAnchor = HAnchor.Stretch,
BackgroundColor = ActiveTheme.Instance.PrimaryBackgroundColor,
BorderColor = theme.MinimalShade,
Border = new BorderDouble(left: 1),
NewTabPage = () =>
{
return new PlusTabPage(this, theme);
}
};
tabControl.ActiveTabChanged += (s, e) =>
{
if (this.tabControl.ActiveTab?.TabContent is PartTabPage tabPage)
{
var dragDropData = ApplicationController.Instance.DragDropData;
// Set reference on tab change
dragDropData.View3DWidget = tabPage.view3DWidget;
dragDropData.SceneContext = tabPage.sceneContext;
}
};
// Force the ActionArea to be as high as ButtonHeight
tabControl.TabBar.ActionArea.MinimumSize = new Vector2(0, theme.ButtonHeight);
tabControl.TabBar.BackgroundColor = theme.TabBarBackground;
tabControl.TabBar.BorderColor = theme.ActiveTabColor;
// Force common padding into top region
var padding = tabControl.TabBar.Padding;
tabControl.TabBar.Padding = padding.Clone(top: padding.Top * 2, bottom: 0);
//tabControl.TabBar.Padding = 0;
//tabControl.TabBar.ActionArea.Padding = padding.Clone(top: padding.Top * 2, bottom: 0);
Color selectedTabColor = ActiveTheme.Instance.TabLabelSelected;
// add in a what's new button
Button seeWhatsNewButton = theme.LinkButtonFactory.Generate("What's New...".Localize());
seeWhatsNewButton.Name = "What's New Link";
seeWhatsNewButton.ToolTipText = "See what's new in this version of MatterControl".Localize();
seeWhatsNewButton.VAnchor = VAnchor.Center;
seeWhatsNewButton.Margin = new Agg.BorderDouble(10, 0);
seeWhatsNewButton.Click += (s, e) => UiThread.RunOnIdle(() =>
{
DialogWindow.Show(new DesignSpaceGuide("What's New Tab", ""));
});
tabControl.TabBar.ActionArea.AddChild(seeWhatsNewButton);
// add in the update available button
Button updateAvailableButton = theme.LinkButtonFactory.Generate("Update Available".Localize());
updateAvailableButton.Visible = false;
// make the function inline so we don't have to create members for the buttons
EventHandler SetLinkButtonsVisability = (s, e) =>
{
if (UserSettings.Instance.HasLookedAtWhatsNew())
{
// hide it
seeWhatsNewButton.Visible = false;
}
if (UpdateControlData.Instance.UpdateStatus == UpdateControlData.UpdateStatusStates.UpdateAvailable)
{
updateAvailableButton.Visible = true;
// if we are going to show the update link hide the whats new link no matter what
seeWhatsNewButton.Visible = false;
}
else
{
updateAvailableButton.Visible = false;
}
};
UserSettings.Instance.Changed += SetLinkButtonsVisability;
Closed += (s, e) => UserSettings.Instance.Changed -= SetLinkButtonsVisability;
RunningInterval showUpdateInterval = null;
updateAvailableButton.VisibleChanged += (s, e) =>
{
if (!updateAvailableButton.Visible)
{
if(showUpdateInterval != null)
{
showUpdateInterval.Continue = false;
showUpdateInterval = null;
}
return;
}
showUpdateInterval = UiThread.SetInterval(() =>
{
double displayTime = 1;
double pulseTime = 1;
double totalSeconds = 0;
var textWidgets = updateAvailableButton.Descendants<TextWidget>().Where((w) => w.Visible == true).ToArray();
Color startColor = theme.Colors.PrimaryTextColor;
// Show a highlite on the button as the user did not click it
Animation flashBackground = null;
flashBackground = new Animation()
{
DrawTarget = updateAvailableButton,
FramesPerSecond = 10,
Update = (s1, updateEvent) =>
{
totalSeconds += updateEvent.SecondsPassed;
if (totalSeconds < displayTime)
{
double blend = AttentionGetter.GetFadeInOutPulseRatio(totalSeconds, pulseTime);
var color = new Color(startColor, (int)((1 - blend) * 255));
foreach (var textWidget in textWidgets)
{
textWidget.TextColor = color;
}
}
else
{
foreach (var textWidget in textWidgets)
{
textWidget.TextColor = startColor;
}
flashBackground.Stop();
}
}
};
flashBackground.Start();
}, 120);
};
updateAvailableButton.Name = "Update Available Link";
SetLinkButtonsVisability(this, null);
updateAvailableButton.ToolTipText = "There is a new update available for download".Localize();
updateAvailableButton.VAnchor = VAnchor.Center;
updateAvailableButton.Margin = new Agg.BorderDouble(10, 0);
updateAvailableButton.Click += (s, e) => UiThread.RunOnIdle(() =>
{
UiThread.RunOnIdle(() =>
{
UpdateControlData.Instance.CheckForUpdate();
DialogWindow.Show<CheckForUpdatesPage>();
});
});
tabControl.TabBar.ActionArea.AddChild(updateAvailableButton);
UpdateControlData.Instance.UpdateStatusChanged.RegisterEvent(SetLinkButtonsVisability, ref unregisterEvents);
this.AddChild(tabControl);
ActiveSliceSettings.SettingChanged.RegisterEvent((s, e) =>
{
if (e is StringEventArgs stringEvent
&& stringEvent.Data == SettingsKey.printer_name
&& printerTab != null)
{
printerTab.Text = ActiveSliceSettings.Instance.GetValue(SettingsKey.printer_name);
}
}, ref unregisterEvents);
ActiveSliceSettings.ActivePrinterChanged.RegisterEvent((s, e) =>
{
var activePrinter = ApplicationController.Instance.ActivePrinter;
// If ActivePrinter has been nulled and a printer tab is open, close it
var tab1 = tabControl.AllTabs.Skip(1).FirstOrDefault();
if ((activePrinter == null || !activePrinter.Settings.PrinterSelected)
&& tab1?.TabContent is PrinterTabPage)
{
tabControl.RemoveTab(tab1);
}
else
{
this.CreatePrinterTab(activePrinter, theme, activePrinter.Settings.GetValue(SettingsKey.printer_name));
}
}, ref unregisterEvents);
ApplicationController.Instance.NotifyPrintersTabRightElement(extensionArea);
// Show fixed start page
tabControl.AddTab(
new ChromeTab("Start".Localize(), tabControl, tabControl.NewTabPage(), theme, hasClose: false)
{
MinimumSize = new Vector2(0, theme.TabButtonHeight),
Name = "Start Tab",
Padding = new BorderDouble(15, 0)
});
// Add a tab for the current printer
if (ActiveSliceSettings.Instance.PrinterSelected)
{
this.CreatePrinterTab(printer, theme, printer.Settings.GetValue(SettingsKey.printer_name));
}
// Restore active tabs
foreach (var bed in ApplicationController.Instance.Workspaces)
{
this.CreatePartTab("New Part", bed, theme);
}
}
private ChromeTab CreatePrinterTab(PrinterConfig printer, ThemeConfig theme, string tabTitle)
{
// Printer page is in fixed position
var tab1 = tabControl.AllTabs.Skip(1).FirstOrDefault();
var printerTabPage = tab1?.TabContent as PrinterTabPage;
if (printerTabPage == null
|| printerTabPage.printer != printer)
{
// TODO - call save before remove
// printerTabPage.sceneContext.SaveChanges();
if (printerTabPage != null)
{
tabControl.RemoveTab(tab1);
}
printerTab = new ChromeTab(
tabTitle,
tabControl,
new PrinterTabPage(printer, theme, tabTitle.ToUpper()),
theme,
tabImageUrl: ApplicationController.Instance.GetFavIconUrl(oemName: printer.Settings.GetValue(SettingsKey.make)))
{
Name = "3D View Tab",
MinimumSize = new Vector2(120, theme.TabButtonHeight)
};
// Add printer into fixed position
tabControl.AddTab(printerTab, 1);
return printerTab;
}
else if (printerTab != null)
{
tabControl.ActiveTab = tab1;
return tab1 as ChromeTab;
}
return null;
}
public ChromeTab CreatePartTab(string tabTitle, BedConfig sceneContext, ThemeConfig theme)
{
var partTab = new ChromeTab(
tabTitle,
tabControl,
new PartTabPage(null, sceneContext, theme, "xxxxx"),
theme,
AggContext.StaticData.LoadIcon("part.png"))
{
Name = "newPart" + tabControl.AllTabs.Count(),
MinimumSize = new Vector2(120, theme.TabButtonHeight)
};
tabControl.AddTab(partTab);
return partTab;
}
public override void OnClosed(ClosedEventArgs e)
{
unregisterEvents?.Invoke(this, null);
base.OnClosed(e);
}
}
}