diff --git a/plugins/color_panel/gtk/color_panel_plugin.cc b/plugins/color_panel/gtk/color_panel_plugin.cc index dd148d607..4f2b7a094 100644 --- a/plugins/color_panel/gtk/color_panel_plugin.cc +++ b/plugins/color_panel/gtk/color_panel_plugin.cc @@ -18,6 +18,7 @@ // See color_panel.dart for documentation. const char kChannelName[] = "flutter/colorpanel"; +const char kNoScreenError[] = "No Screen"; const char kShowColorPanelMethod[] = "ColorPanel.Show"; const char kColorPanelShowAlpha[] = "ColorPanel.ShowAlpha"; const char kHideColorPanelMethod[] = "ColorPanel.Hide"; @@ -92,9 +93,14 @@ static FlMethodResponse* show_color_panel(FlColorPanelPlugin* self, gboolean use_alpha = use_alpha_value != nullptr ? fl_value_get_bool(use_alpha_value) : FALSE; + FlView* view = fl_plugin_registrar_get_view(self->registrar); + if (view == nullptr) { + return FL_METHOD_RESPONSE( + fl_method_error_response_new(kNoScreenError, nullptr, nullptr)); + } + self->color_chooser_dialog = GTK_COLOR_CHOOSER_DIALOG( gtk_color_chooser_dialog_new(kWindowTitle, nullptr)); - FlView* view = fl_plugin_registrar_get_view(self->registrar); GtkWindow* window = GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(view))); gtk_window_set_transient_for(GTK_WINDOW(self->color_chooser_dialog), window); gtk_color_chooser_set_use_alpha(GTK_COLOR_CHOOSER(self->color_chooser_dialog), diff --git a/plugins/file_chooser/gtk/file_chooser_plugin.cc b/plugins/file_chooser/gtk/file_chooser_plugin.cc index 4bad8269d..7c4fa948f 100644 --- a/plugins/file_chooser/gtk/file_chooser_plugin.cc +++ b/plugins/file_chooser/gtk/file_chooser_plugin.cc @@ -19,6 +19,7 @@ // See channel_controller.dart for documentation. const char kChannelName[] = "flutter/filechooser"; const char kBadArgumentsError[] = "Bad Arguments"; +const char kNoScreenError[] = "No Screen"; const char kShowOpenPanelMethod[] = "FileChooser.Show.Open"; const char kShowSavePanelMethod[] = "FileChooser.Show.Save"; const char kInitialDirectoryKey[] = "initialDirectory"; @@ -84,6 +85,11 @@ static FlMethodResponse* show_dialog(FlFileChooserPlugin* self, confirm_button_text = fl_value_get_string(value); FlView* view = fl_plugin_registrar_get_view(self->registrar); + if (view == nullptr) { + return FL_METHOD_RESPONSE( + fl_method_error_response_new(kNoScreenError, nullptr, nullptr)); + } + GtkWindow* window = GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(view))); g_autoptr(GtkFileChooserNative) dialog = GTK_FILE_CHOOSER_NATIVE(gtk_file_chooser_native_new( diff --git a/plugins/menubar/gtk/menubar_plugin.cc b/plugins/menubar/gtk/menubar_plugin.cc index 74d9be7dc..d1de8e59b 100644 --- a/plugins/menubar/gtk/menubar_plugin.cc +++ b/plugins/menubar/gtk/menubar_plugin.cc @@ -20,6 +20,7 @@ // See menu_channel.dart for documentation. const char kChannelName[] = "flutter/menubar"; const char kBadArgumentsError[] = "Bad Arguments"; +const char kNoScreenError[] = "No Screen"; const char kFailureError[] = "Failure"; const char kMenuSetMethod[] = "Menubar.SetMenu"; const char kMenuItemSelectedCallbackMethod[] = "Menubar.SelectedCallback"; @@ -145,6 +146,11 @@ static FlMethodResponse* menu_set(FlMenubarPlugin* self, FlValue* args) { } FlView* view = fl_plugin_registrar_get_view(self->registrar); + if (view == nullptr) { + return FL_METHOD_RESPONSE( + fl_method_error_response_new(kNoScreenError, nullptr, nullptr)); + } + GtkApplication* app = gtk_window_get_application( GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(view)))); if (app == nullptr) { @@ -213,8 +219,11 @@ FlMenubarPlugin* fl_menubar_plugin_new(FlPluginRegistrar* registrar) { // Add a GAction for the menubar to trigger. FlView* view = fl_plugin_registrar_get_view(self->registrar); - GtkApplication* app = gtk_window_get_application( - GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(view)))); + GtkApplication* app = nullptr; + if (view != nullptr) { + app = gtk_window_get_application( + GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(view)))); + } if (app != nullptr) { g_autoptr(GSimpleAction) inactive_action = g_simple_action_new("flutter-menu-inactive", nullptr); diff --git a/plugins/window_size/gtk/window_size_plugin.cc b/plugins/window_size/gtk/window_size_plugin.cc index 27dc457d8..a2d2c6637 100644 --- a/plugins/window_size/gtk/window_size_plugin.cc +++ b/plugins/window_size/gtk/window_size_plugin.cc @@ -19,6 +19,7 @@ // See window_size_channel.dart for documentation. const char kChannelName[] = "flutter/windowsize"; const char kBadArgumentsError[] = "Bad Arguments"; +const char kNoScreenError[] = "No Screen"; const char kGetScreenListMethod[] = "getScreenList"; const char kGetWindowInfoMethod[] = "getWindowInfo"; const char kSetWindowFrameMethod[] = "setWindowFrame"; @@ -49,12 +50,16 @@ G_DEFINE_TYPE(FlWindowSizePlugin, fl_window_size_plugin, g_object_get_type()) // Gets the window being controlled. GtkWindow* get_window(FlWindowSizePlugin* self) { FlView* view = fl_plugin_registrar_get_view(self->registrar); + if (view == nullptr) return nullptr; + return GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(view))); } // Gets the display connection. GdkDisplay* get_display(FlWindowSizePlugin* self) { FlView* view = fl_plugin_registrar_get_view(self->registrar); + if (view == nullptr) return nullptr; + return gtk_widget_get_display(GTK_WIDGET(view)); } @@ -97,6 +102,11 @@ static FlMethodResponse* get_screen_list(FlWindowSizePlugin* self) { g_autoptr(FlValue) screens = fl_value_new_list(); GdkDisplay* display = get_display(self); + if (display == nullptr) { + return FL_METHOD_RESPONSE( + fl_method_error_response_new(kNoScreenError, nullptr, nullptr)); + } + gint n_monitors = gdk_display_get_n_monitors(display); for (gint i = 0; i < n_monitors; i++) { GdkMonitor* monitor = gdk_display_get_monitor(display, i); @@ -109,6 +119,10 @@ static FlMethodResponse* get_screen_list(FlWindowSizePlugin* self) { // Gets information about the Flutter window. static FlMethodResponse* get_window_info(FlWindowSizePlugin* self) { GtkWindow* window = get_window(self); + if (window == nullptr) { + return FL_METHOD_RESPONSE( + fl_method_error_response_new(kNoScreenError, nullptr, nullptr)); + } g_autoptr(FlValue) window_info = fl_value_new_map(); @@ -158,6 +172,11 @@ static FlMethodResponse* set_window_frame(FlWindowSizePlugin* self, double height = fl_value_get_float(fl_value_get_list_value(args, 3)); GtkWindow* window = get_window(self); + if (window == nullptr) { + return FL_METHOD_RESPONSE( + fl_method_error_response_new(kNoScreenError, nullptr, nullptr)); + } + gtk_window_move(window, static_cast(x), static_cast(y)); gtk_window_resize(window, static_cast(width), static_cast(height)); @@ -183,6 +202,11 @@ static FlMethodResponse* set_window_minimum_size(FlWindowSizePlugin* self, double width = fl_value_get_float(fl_value_get_list_value(args, 0)); double height = fl_value_get_float(fl_value_get_list_value(args, 1)); + if (get_window() == nullptr) { + return FL_METHOD_RESPONSE( + fl_method_error_response_new(kNoScreenError, nullptr, nullptr)); + } + if (width >= 0 && height >= 0) { self->window_geometry.min_width = static_cast(width); self->window_geometry.min_height = static_cast(height); @@ -204,6 +228,11 @@ static FlMethodResponse* set_window_maximum_size(FlWindowSizePlugin* self, double width = fl_value_get_float(fl_value_get_list_value(args, 0)); double height = fl_value_get_float(fl_value_get_list_value(args, 1)); + if (get_window() == nullptr) { + return FL_METHOD_RESPONSE( + fl_method_error_response_new(kNoScreenError, nullptr, nullptr)); + } + self->window_geometry.max_width = static_cast(width); self->window_geometry.max_height = static_cast(height); @@ -221,6 +250,10 @@ static FlMethodResponse* set_window_title(FlWindowSizePlugin* self, } GtkWindow* window = get_window(self); + if (window == nullptr) { + return FL_METHOD_RESPONSE( + fl_method_error_response_new(kNoScreenError, nullptr, nullptr)); + } gtk_window_set_title(window, fl_value_get_string(args)); return FL_METHOD_RESPONSE(fl_method_success_response_new(nullptr));