Skip to content

Adds opacity slider option to color panel #155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions plugins/color_panel/common/channel_constants.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace plugins_color_panel {
const char kChannelName[] = "flutter/colorpanel";

const char kShowColorPanelMethod[] = "ColorPanel.Show";
const char kColorPanelShowAlpha[] = "ColorPanel.ShowAlpha";
const char kHideColorPanelMethod[] = "ColorPanel.Hide";
const char kColorSelectedCallbackMethod[] = "ColorPanel.ColorSelectedCallback";
const char kClosedCallbackMethod[] = "ColorPanel.ClosedCallback";
Expand Down
3 changes: 3 additions & 0 deletions plugins/color_panel/common/channel_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ extern const char kColorSelectedCallbackMethod[];
// closed from the UI, rather than via a call to kHideColorPanelMethod.
extern const char kClosedCallbackMethod[];

// The argument to show an opacity modifier on the panel. Default is true.
extern const char kColorPanelShowAlpha[];

// Keys for the RGB color JSON object sent to kColorPanelCallback.
// The values should be numbers between 0 and 1.
extern const char kColorComponentRedKey[];
Expand Down
8 changes: 5 additions & 3 deletions plugins/color_panel/lib/color_panel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const String _kHideColorPanelMethod = 'ColorPanel.Hide';
const String _kColorPanelColorSelectedCallback =
'ColorPanel.ColorSelectedCallback';
const String _kColorPanelClosedCallback = 'ColorPanel.ClosedCallback';
const String _kColorPanelShowAlpha = 'ColorPanel.ShowAlpha';
const String _kRedKey = 'red';
const String _kGreenKey = 'green';
const String _kBlueKey = 'blue';
Expand Down Expand Up @@ -55,14 +56,15 @@ class ColorPanel {
///
/// [callback] will be called when the user selects a color; it can be called
/// an number of times depending on the interaction model of the native
/// panel.
/// panel. Set [showAlpha] to false to hide the color opacity modifier UI.
///
/// It is an error to call [show] if the panel is already showing.
void show(ColorPanelCallback callback) {
void show(ColorPanelCallback callback, {bool showAlpha = true}) {
try {
if (!showing) {
_callback = callback;
_platformChannel.invokeMethod(_kShowColorPanelMethod);
_platformChannel.invokeMethod(_kShowColorPanelMethod,
{_kColorPanelShowAlpha: showAlpha});
} else {
throw new StateError('Color panel is already shown');
}
Expand Down
9 changes: 7 additions & 2 deletions plugins/color_panel/linux/src/color_panel_plugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ using flutter_desktop_embedding::MethodResult;
// This is to avoid having the user import extra GTK headers.
class ColorPanelPlugin::ColorPanel {
public:
explicit ColorPanel(ColorPanelPlugin *parent) {
explicit ColorPanel(ColorPanelPlugin *parent,
const Json::Value &method_args) {
gtk_widget_ = gtk_color_chooser_dialog_new(kWindowTitle, nullptr);
gtk_color_chooser_set_use_alpha(
reinterpret_cast<GtkColorChooser *>(gtk_widget_),
method_args[kColorPanelShowAlpha].asBool());
gtk_widget_show_all(gtk_widget_);
g_signal_connect(gtk_widget_, "close", G_CALLBACK(CloseCallback), parent);
g_signal_connect(gtk_widget_, "response", G_CALLBACK(ResponseCallback),
Expand Down Expand Up @@ -99,7 +103,8 @@ void ColorPanelPlugin::HandleJsonMethodCall(
if (color_panel_) {
return;
}
color_panel_ = std::make_unique<ColorPanelPlugin::ColorPanel>(this);
color_panel_ = std::make_unique<ColorPanelPlugin::ColorPanel>(
this, method_call.GetArgumentsAsJson());
} else if (method_call.method_name().compare(kHideColorPanelMethod) == 0) {
result->Success();
if (color_panel_ == nullptr) {
Expand Down
14 changes: 12 additions & 2 deletions plugins/color_panel/macos/FLEColorPanelPlugin.mm
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,16 @@ - (NSString *)channel {
- (void)handleMethodCall:(FLEMethodCall *)call result:(FLEMethodResult)result {
BOOL handled = YES;
if ([call.methodName isEqualToString:@(plugins_color_panel::kShowColorPanelMethod)]) {
[self showColorPanel];
if ([call.arguments isKindOfClass:[NSDictionary class]]) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this isn't true then the call is malformed, so I'd prefer that if the check fails the code NSLog an error message, then call result(FLEMethodNotImplemented); and return early.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SG

BOOL showAlpha =
[[call.arguments valueForKey:@(plugins_color_panel::kColorPanelShowAlpha)] boolValue];
[self showColorPanelWithAlpha:showAlpha];
} else {
NSLog(@"Malformed call for %@. Expected an NSDictionary but got %@",
@(plugins_color_panel::kShowColorPanelMethod),
NSStringFromClass([call.arguments class]));
handled = NO;
}
} else if ([call.methodName isEqualToString:@(plugins_color_panel::kHideColorPanelMethod)]) {
[self hideColorPanel];
} else {
Expand All @@ -47,9 +56,10 @@ - (void)handleMethodCall:(FLEMethodCall *)call result:(FLEMethodResult)result {
/**
* Configures the shared instance of NSColorPanel and makes it the frontmost & key window.
*/
- (void)showColorPanel {
- (void)showColorPanelWithAlpha:(BOOL)showAlpha {
NSColorPanel *sharedColor = [NSColorPanel sharedColorPanel];
sharedColor.delegate = self;
[sharedColor setShowsAlpha:showAlpha];
[sharedColor setTarget:self];
[sharedColor setAction:@selector(selectedColorDidChange)];
if (!sharedColor.isKeyWindow) {
Expand Down