Skip to content

Commit a133c76

Browse files
Adds opacity slider option to color panel (#155)
Adds opacity slider option to color panel
1 parent 6f8e771 commit a133c76

File tree

5 files changed

+28
-7
lines changed

5 files changed

+28
-7
lines changed

plugins/color_panel/common/channel_constants.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ namespace plugins_color_panel {
1818
const char kChannelName[] = "flutter/colorpanel";
1919

2020
const char kShowColorPanelMethod[] = "ColorPanel.Show";
21+
const char kColorPanelShowAlpha[] = "ColorPanel.ShowAlpha";
2122
const char kHideColorPanelMethod[] = "ColorPanel.Hide";
2223
const char kColorSelectedCallbackMethod[] = "ColorPanel.ColorSelectedCallback";
2324
const char kClosedCallbackMethod[] = "ColorPanel.ClosedCallback";

plugins/color_panel/common/channel_constants.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ extern const char kColorSelectedCallbackMethod[];
3232
// closed from the UI, rather than via a call to kHideColorPanelMethod.
3333
extern const char kClosedCallbackMethod[];
3434

35+
// The argument to show an opacity modifier on the panel. Default is true.
36+
extern const char kColorPanelShowAlpha[];
37+
3538
// Keys for the RGB color JSON object sent to kColorPanelCallback.
3639
// The values should be numbers between 0 and 1.
3740
extern const char kColorComponentRedKey[];

plugins/color_panel/lib/color_panel.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const String _kHideColorPanelMethod = 'ColorPanel.Hide';
2323
const String _kColorPanelColorSelectedCallback =
2424
'ColorPanel.ColorSelectedCallback';
2525
const String _kColorPanelClosedCallback = 'ColorPanel.ClosedCallback';
26+
const String _kColorPanelShowAlpha = 'ColorPanel.ShowAlpha';
2627
const String _kRedKey = 'red';
2728
const String _kGreenKey = 'green';
2829
const String _kBlueKey = 'blue';
@@ -55,14 +56,15 @@ class ColorPanel {
5556
///
5657
/// [callback] will be called when the user selects a color; it can be called
5758
/// an number of times depending on the interaction model of the native
58-
/// panel.
59+
/// panel. Set [showAlpha] to false to hide the color opacity modifier UI.
5960
///
6061
/// It is an error to call [show] if the panel is already showing.
61-
void show(ColorPanelCallback callback) {
62+
void show(ColorPanelCallback callback, {bool showAlpha = true}) {
6263
try {
6364
if (!showing) {
6465
_callback = callback;
65-
_platformChannel.invokeMethod(_kShowColorPanelMethod);
66+
_platformChannel.invokeMethod(_kShowColorPanelMethod,
67+
{_kColorPanelShowAlpha: showAlpha});
6668
} else {
6769
throw new StateError('Color panel is already shown');
6870
}

plugins/color_panel/linux/src/color_panel_plugin.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,12 @@ using flutter_desktop_embedding::MethodResult;
2929
// This is to avoid having the user import extra GTK headers.
3030
class ColorPanelPlugin::ColorPanel {
3131
public:
32-
explicit ColorPanel(ColorPanelPlugin *parent) {
32+
explicit ColorPanel(ColorPanelPlugin *parent,
33+
const Json::Value &method_args) {
3334
gtk_widget_ = gtk_color_chooser_dialog_new(kWindowTitle, nullptr);
35+
gtk_color_chooser_set_use_alpha(
36+
reinterpret_cast<GtkColorChooser *>(gtk_widget_),
37+
method_args[kColorPanelShowAlpha].asBool());
3438
gtk_widget_show_all(gtk_widget_);
3539
g_signal_connect(gtk_widget_, "close", G_CALLBACK(CloseCallback), parent);
3640
g_signal_connect(gtk_widget_, "response", G_CALLBACK(ResponseCallback),
@@ -99,7 +103,8 @@ void ColorPanelPlugin::HandleJsonMethodCall(
99103
if (color_panel_) {
100104
return;
101105
}
102-
color_panel_ = std::make_unique<ColorPanelPlugin::ColorPanel>(this);
106+
color_panel_ = std::make_unique<ColorPanelPlugin::ColorPanel>(
107+
this, method_call.GetArgumentsAsJson());
103108
} else if (method_call.method_name().compare(kHideColorPanelMethod) == 0) {
104109
result->Success();
105110
if (color_panel_ == nullptr) {

plugins/color_panel/macos/FLEColorPanelPlugin.mm

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,16 @@ - (NSString *)channel {
3333
- (void)handleMethodCall:(FLEMethodCall *)call result:(FLEMethodResult)result {
3434
BOOL handled = YES;
3535
if ([call.methodName isEqualToString:@(plugins_color_panel::kShowColorPanelMethod)]) {
36-
[self showColorPanel];
36+
if ([call.arguments isKindOfClass:[NSDictionary class]]) {
37+
BOOL showAlpha =
38+
[[call.arguments valueForKey:@(plugins_color_panel::kColorPanelShowAlpha)] boolValue];
39+
[self showColorPanelWithAlpha:showAlpha];
40+
} else {
41+
NSLog(@"Malformed call for %@. Expected an NSDictionary but got %@",
42+
@(plugins_color_panel::kShowColorPanelMethod),
43+
NSStringFromClass([call.arguments class]));
44+
handled = NO;
45+
}
3746
} else if ([call.methodName isEqualToString:@(plugins_color_panel::kHideColorPanelMethod)]) {
3847
[self hideColorPanel];
3948
} else {
@@ -47,9 +56,10 @@ - (void)handleMethodCall:(FLEMethodCall *)call result:(FLEMethodResult)result {
4756
/**
4857
* Configures the shared instance of NSColorPanel and makes it the frontmost & key window.
4958
*/
50-
- (void)showColorPanel {
59+
- (void)showColorPanelWithAlpha:(BOOL)showAlpha {
5160
NSColorPanel *sharedColor = [NSColorPanel sharedColorPanel];
5261
sharedColor.delegate = self;
62+
[sharedColor setShowsAlpha:showAlpha];
5363
[sharedColor setTarget:self];
5464
[sharedColor setAction:@selector(selectedColorDidChange)];
5565
if (!sharedColor.isKeyWindow) {

0 commit comments

Comments
 (0)