Skip to content

Commit

Permalink
Use events when picked color changes, adjust the preset colors (micro…
Browse files Browse the repository at this point in the history
  • Loading branch information
gbaychev committed Nov 30, 2019
1 parent a223c8d commit 524ce1c
Show file tree
Hide file tree
Showing 7 changed files with 273 additions and 241 deletions.
142 changes: 97 additions & 45 deletions src/cascadia/TerminalApp/ColorPickupFlyout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,106 @@
#include "winrt/Windows.UI.Xaml.Media.h"
#include "winrt/Windows.UI.Xaml.Shapes.h"
#include "winrt/Windows.UI.Xaml.Interop.h"

#include <LibraryResources.h>

namespace winrt::TerminalApp::implementation
{
ColorPickupFlyout::ColorPickupFlyout()
{
InitializeComponent();
}

void ColorPickupFlyout::ColorButton_Click(IInspectable const& sender, Windows::UI::Xaml::RoutedEventArgs const&)
{
auto btn{ sender.as<Windows::UI::Xaml::Controls::Button>() };
auto rectangle{ btn.Content().as<Windows::UI::Xaml::Shapes::Rectangle>() };
auto rectClr{ rectangle.Fill().as<Windows::UI::Xaml::Media::SolidColorBrush>() };
SelectedColor(rectClr.Color());
Hide();
}

void ColorPickupFlyout::CustomColorButton_Click(Windows::Foundation::IInspectable const&, Windows::UI::Xaml::RoutedEventArgs const&)
{
auto targetType = this->FlyoutPresenterStyle().TargetType();
auto s = Windows::UI::Xaml::Style{ };
s.TargetType(targetType);
auto visibility = customColorPicker().Visibility();
if (visibility == winrt::Windows::UI::Xaml::Visibility::Collapsed)
{
customColorPicker().Visibility(winrt::Windows::UI::Xaml::Visibility::Visible);
auto setter = Windows::UI::Xaml::Setter(Windows::UI::Xaml::FrameworkElement::MinWidthProperty(), winrt::box_value(540));
s.Setters().Append(setter);
}
else
{
customColorPicker().Visibility(winrt::Windows::UI::Xaml::Visibility::Collapsed);
auto setter = Windows::UI::Xaml::Setter(Windows::UI::Xaml::FrameworkElement::MinWidthProperty(), winrt::box_value(0));
s.Setters().Append(setter);
}
this->FlyoutPresenterStyle(s);
}

Windows::UI::Xaml::DependencyProperty ColorPickupFlyout::m_SelectedColorProperty =
Windows::UI::Xaml::DependencyProperty::Register(
L"SelectedColor",
winrt::xaml_typename<winrt::Windows::UI::Color>(),
winrt::xaml_typename<TerminalApp::ColorPickupFlyout>(),
Windows::UI::Xaml::PropertyMetadata{ winrt::box_value(winrt::Windows::UI::Colors::Transparent()),
Windows::UI::Xaml::PropertyChangedCallback{ &ColorPickupFlyout::OnSelectedColorChanged } });

void ColorPickupFlyout::OnSelectedColorChanged(Windows::UI::Xaml::DependencyObject const&, Windows::UI::Xaml::DependencyPropertyChangedEventArgs const& /* e */)
// Method Description:
// - Default constructor, localizes the buttons and hooks
// up the event fired by the custom color picker, so that
// the tab color is set on the fly when selecting a non-preset color
// Arguments:
// - <none>
ColorPickupFlyout::ColorPickupFlyout()
{
InitializeComponent();

auto okText = RS_(L"Ok");
auto customColorText = RS_(L"TabCustomColorChoose");
auto clearColorText = RS_(L"TabColorClear");

btnOk().Content(winrt::box_value(okText));
btnCustomColor().Content(winrt::box_value(customColorText));
btnClearColor().Content(winrt::box_value(clearColorText));

customColorPicker().ColorChanged([this](const auto&, const Windows::UI::Xaml::Controls::ColorChangedEventArgs& args) {
_colorSelected(args.NewColor());
});
}

// Method Description:
// - Handler of the click event for the preset color swatches.
// Reads the color from the clicked rectangle and fires an event
// with the selected color. After that hides the flyout
// Arguments:
// - sender: the rectangle that got clicked
// Return Value:
// - <none>
void ColorPickupFlyout::ColorButton_Click(IInspectable const& sender, Windows::UI::Xaml::RoutedEventArgs const&)
{
auto btn{ sender.as<Windows::UI::Xaml::Controls::Button>() };
auto rectangle{ btn.Content().as<Windows::UI::Xaml::Shapes::Rectangle>() };
auto rectClr{ rectangle.Fill().as<Windows::UI::Xaml::Media::SolidColorBrush>() };
_colorSelected(rectClr.Color());
Hide();
}

// Method Description:
// - Handler of the clear color button. Clears the current
// color of the tab, if any. Hides the flyout after that
// Arguments:
// - <none>
// Return Value:
// - <none>
void ColorPickupFlyout::ClearColorButton_Click(IInspectable const&, Windows::UI::Xaml::RoutedEventArgs const&)
{
_colorCleared();
Hide();
}

// Method Description:
// - Handler of the select custom color button. Expands or collapses the flyout
// to show the color picker. In order to accomplish this a FlyoutPresenterStyle is used,
// in which a Style is embedded, containing the desired width
// Arguments:
// - <none>
// Return Value:
// - <none>
void ColorPickupFlyout::ShowColorPickerButton_Click(Windows::Foundation::IInspectable const&, Windows::UI::Xaml::RoutedEventArgs const&)
{
auto targetType = this->FlyoutPresenterStyle().TargetType();
auto s = Windows::UI::Xaml::Style{};
s.TargetType(targetType);
auto visibility = customColorPanel().Visibility();
if (visibility == winrt::Windows::UI::Xaml::Visibility::Collapsed)
{
customColorPanel().Visibility(winrt::Windows::UI::Xaml::Visibility::Visible);
auto setter = Windows::UI::Xaml::Setter(Windows::UI::Xaml::FrameworkElement::MinWidthProperty(), winrt::box_value(540));
s.Setters().Append(setter);
}
else
{
customColorPanel().Visibility(winrt::Windows::UI::Xaml::Visibility::Collapsed);
auto setter = Windows::UI::Xaml::Setter(Windows::UI::Xaml::FrameworkElement::MinWidthProperty(), winrt::box_value(0));
s.Setters().Append(setter);
}
this->FlyoutPresenterStyle(s);
}

// Method Description:
// - Handles the color selection of the color pickup. Gets
// the currently selected color and fires an event with it
// Arguments:
// - <none>
// Return Value:
// - <none>
void ColorPickupFlyout::CustomColorButton_Click(Windows::Foundation::IInspectable const&, Windows::UI::Xaml::RoutedEventArgs const&)
{
auto color = customColorPicker().Color();
_colorSelected(color);
Hide();
}

DEFINE_EVENT(ColorPickupFlyout, ColorSelected, _colorSelected, TerminalApp::ColorSelectedArgs);
DEFINE_EVENT(ColorPickupFlyout, ColorCleared, _colorCleared, TerminalApp::ColorClearedArgs);
}
27 changes: 9 additions & 18 deletions src/cascadia/TerminalApp/ColorPickupFlyout.h
Original file line number Diff line number Diff line change
@@ -1,35 +1,26 @@
#pragma once
#include "ColorPickupFlyout.g.h"
#include "../cascadia/inc/cppwinrt_utils.h"

namespace winrt::TerminalApp::implementation
{
struct ColorPickupFlyout : ColorPickupFlyoutT<ColorPickupFlyout>
{
ColorPickupFlyout();
void ColorButton_Click(Windows::Foundation::IInspectable const& sender, Windows::UI::Xaml::RoutedEventArgs const& args);
void CustomColorButton_Click(Windows::Foundation::IInspectable const& sender, Windows::UI::Xaml::RoutedEventArgs const& args);
ColorPickupFlyout();

winrt::Windows::UI::Color SelectedColor()
{
return winrt::unbox_value<winrt::Windows::UI::Color>(GetValue(m_SelectedColorProperty));
}
void ColorButton_Click(Windows::Foundation::IInspectable const& sender, Windows::UI::Xaml::RoutedEventArgs const& args);
void ShowColorPickerButton_Click(Windows::Foundation::IInspectable const& sender, Windows::UI::Xaml::RoutedEventArgs const& args);
void CustomColorButton_Click(Windows::Foundation::IInspectable const& sender, Windows::UI::Xaml::RoutedEventArgs const& args);
void ClearColorButton_Click(Windows::Foundation::IInspectable const& sender, Windows::UI::Xaml::RoutedEventArgs const& args);

void SelectedColor(winrt::Windows::UI::Color const& color)
{
SetValue(m_SelectedColorProperty, winrt::box_value(color));
}

static Windows::UI::Xaml::DependencyProperty SelectedColorProperty() { return m_SelectedColorProperty; }
static void OnSelectedColorChanged(Windows::UI::Xaml::DependencyObject const&, Windows::UI::Xaml::DependencyPropertyChangedEventArgs const&);

private:
static Windows::UI::Xaml::DependencyProperty m_SelectedColorProperty;
DECLARE_EVENT(ColorSelected, _colorSelected, TerminalApp::ColorSelectedArgs);
DECLARE_EVENT(ColorCleared, _colorCleared, TerminalApp::ColorClearedArgs);
};
}

namespace winrt::TerminalApp::factory_implementation
{
struct ColorPickupFlyout : ColorPickupFlyoutT<ColorPickupFlyout, implementation::ColorPickupFlyout>
{

};
}
17 changes: 9 additions & 8 deletions src/cascadia/TerminalApp/ColorPickupFlyout.idl
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// BgLabelControl.idl
namespace TerminalApp
{
[default_interface]
runtimeclass ColorPickupFlyout : Windows.UI.Xaml.Controls.Flyout
{
ColorPickupFlyout();
Windows.UI.Color SelectedColor{ get; };
static Windows.UI.Xaml.DependencyProperty SelectedColorProperty{ get; };
}
delegate void ColorSelectedArgs(Windows.UI.Color color);
delegate void ColorClearedArgs();

[default_interface] runtimeclass ColorPickupFlyout : Windows.UI.Xaml.Controls.Flyout
{
ColorPickupFlyout();
event ColorSelectedArgs ColorSelected;
event ColorClearedArgs ColorCleared;
}
}
Loading

0 comments on commit 524ce1c

Please sign in to comment.