mattercontrol/MatterControlLib/CustomWidgets/ThemeColorSelectorWidget.cs

144 lines
4.7 KiB
C#
Raw Normal View History

/*
2017-06-19 18:46:57 -07:00
Copyright (c) 2017, Kevin Pope, John Lewin
All rights reserved.
Redistribution and use in source and binary forms, with or without
2015-04-08 15:20:10 -07:00
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
2015-04-08 15:20:10 -07:00
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
2015-04-08 15:20:10 -07:00
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
2015-04-08 15:20:10 -07:00
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.
*/
2015-04-08 15:20:10 -07:00
2017-06-19 18:46:57 -07:00
using System;
using System.Linq;
2014-01-29 19:09:30 -08:00
using MatterHackers.Agg;
using MatterHackers.Agg.UI;
2017-06-19 18:46:57 -07:00
using MatterHackers.MatterControl.ConfigurationPage;
2016-05-12 07:30:11 -07:00
using MatterHackers.MatterControl.SlicerConfiguration;
2014-01-29 19:09:30 -08:00
namespace MatterHackers.MatterControl
{
public class ThemeColorSelectorWidget : FlowLayoutWidget
{
2017-06-19 18:46:57 -07:00
private ThemePreviewButton darkPreviewButton;
private ThemePreviewButton lightPreviewButton;
2017-06-16 21:50:24 -07:00
private int containerHeight = (int)(30 * GuiWidget.DeviceScale + .5);
private int colorSelectSize = (int)(28 * GuiWidget.DeviceScale + .5);
2015-04-08 15:20:10 -07:00
2017-06-19 18:46:57 -07:00
public ThemeColorSelectorWidget(ThemePreviewButton darkPreview, ThemePreviewButton lightPreview)
{
2015-04-08 15:20:10 -07:00
this.Padding = new BorderDouble(2, 0);
2017-06-19 18:46:57 -07:00
this.darkPreviewButton = darkPreview;
this.lightPreviewButton = lightPreview;
2016-04-29 07:45:15 -07:00
int themeCount = ActiveTheme.AvailableThemes.Count;
var allThemes = ActiveTheme.AvailableThemes;
2015-04-08 15:20:10 -07:00
2014-01-29 19:09:30 -08:00
int index = 0;
2015-04-08 15:20:10 -07:00
for (int x = 0; x < themeCount / 2; x++)
2014-01-29 19:09:30 -08:00
{
2017-06-19 18:46:57 -07:00
var themeButton = CreateThemeButton(allThemes[index], index);
themeButton.Width = containerHeight;
2017-06-19 18:46:57 -07:00
this.AddChild(themeButton);
2016-04-29 07:45:15 -07:00
2015-04-08 15:20:10 -07:00
index++;
}
2017-06-19 18:46:57 -07:00
2015-04-08 15:20:10 -07:00
this.Width = containerHeight * (themeCount / 2);
2014-01-29 19:09:30 -08:00
}
2017-06-19 18:46:57 -07:00
private int hoveredThemeIndex = 0;
private int midPoint = ActiveTheme.AvailableThemes.Count / 2;
2017-06-19 18:46:57 -07:00
public Button CreateThemeButton(IThemeColors darkTheme, int darkThemeIndex)
2015-04-08 15:20:10 -07:00
{
2016-04-29 07:45:15 -07:00
var normal = new GuiWidget(colorSelectSize, colorSelectSize);
normal.BackgroundColor = darkTheme.SourceColor;
2016-04-29 07:45:15 -07:00
var hover = new GuiWidget(colorSelectSize, colorSelectSize);
hover.BackgroundColor = darkTheme.SourceColor;
2016-04-29 07:45:15 -07:00
var pressed = new GuiWidget(colorSelectSize, colorSelectSize);
pressed.BackgroundColor = darkTheme.SourceColor;
2016-04-29 07:45:15 -07:00
var disabled = new GuiWidget(colorSelectSize, colorSelectSize);
2017-06-19 18:46:57 -07:00
int lightThemeIndex = darkThemeIndex + midPoint;
var lightTheme = ActiveTheme.AvailableThemes[lightThemeIndex];
var colorButton = new Button(0, 0, new ButtonViewStates(normal, hover, pressed, disabled));
colorButton.Cursor = Cursors.Hand;
2016-04-29 07:45:15 -07:00
colorButton.Click += (s, e) =>
2015-04-08 15:20:10 -07:00
{
2017-06-19 18:46:57 -07:00
// Determine if we should set the dark or light version of the theme
var activeThemeIndex = ActiveTheme.AvailableThemes.IndexOf(ActiveTheme.Instance);
2017-06-19 18:46:57 -07:00
bool useLightTheme = activeThemeIndex >= midPoint;
SetTheme(darkThemeIndex, useLightTheme);
};
2016-04-29 07:45:15 -07:00
colorButton.MouseEnterBounds += (s, e) =>
{
2017-06-19 18:46:57 -07:00
darkPreviewButton.SetThemeColors(darkTheme);
lightPreviewButton.SetThemeColors(lightTheme);
hoveredThemeIndex = darkThemeIndex;
};
2016-04-29 07:45:15 -07:00
colorButton.MouseLeaveBounds += (s, e) =>
{
2017-06-19 18:46:57 -07:00
// darkPreviewButton.SetThemeColors(ActiveTheme.Instance);
};
2015-04-08 15:20:10 -07:00
return colorButton;
}
2017-06-19 18:46:57 -07:00
private static void SetTheme(int themeIndex, bool useLightTheme)
{
if (useLightTheme)
{
themeIndex += (ActiveTheme.AvailableThemes.Count / 2);
}
// save it for this printer
SetTheme(ActiveTheme.AvailableThemes[themeIndex].Name);
}
public static void SetTheme(string themeName)
{
UiThread.RunOnIdle(() =>
{
UserSettings.Instance.set(UserSettingsKey.ActiveThemeName, themeName);
//Set new user selected Default
ActiveTheme.Instance = ActiveTheme.GetThemeColors(themeName);
2017-06-19 18:46:57 -07:00
// Explicitly fire ReloadAll in response to user interaction
ApplicationController.Instance.ReloadAll();
});
2017-06-19 18:46:57 -07:00
}
2014-01-29 19:09:30 -08:00
}
2015-04-08 15:20:10 -07:00
}