Flutter Linux Embedder
fl_window_monitor.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <gtk/gtk.h>
6 
9 
11  GObject parent_instance;
12 
13  // Window being monitored.
14  GtkWindow* window;
15 
16  // Isolate to call callbacks with.
18 
19  // Callbacks.
20  void (*on_configure)(void);
21  void (*on_state_changed)(void);
22  void (*on_is_active_notify)(void);
23  void (*on_title_notify)(void);
24  void (*on_moved_to_rect)(int, int, int, int);
25  void (*on_close)(void);
26  void (*on_destroy)(void);
27 };
28 
29 G_DEFINE_TYPE(FlWindowMonitor, fl_window_monitor, G_TYPE_OBJECT)
30 
31 static gboolean configure_event_cb(FlWindowMonitor* self,
32  GdkEventConfigure* event) {
33  flutter::IsolateScope scope(self->isolate);
34  self->on_configure();
35 
36  return FALSE;
37 }
38 
39 static gboolean window_state_event_cb(FlWindowMonitor* self,
40  GdkEventWindowState* event) {
41  flutter::IsolateScope scope(self->isolate);
42  self->on_state_changed();
43 
44  return FALSE;
45 }
46 
47 static void is_active_notify_cb(FlWindowMonitor* self) {
48  flutter::IsolateScope scope(self->isolate);
49  self->on_is_active_notify();
50 }
51 
52 static void title_notify_cb(FlWindowMonitor* self) {
53  flutter::IsolateScope scope(self->isolate);
54  self->on_title_notify();
55 }
56 
57 static void moved_to_rect_cb(FlWindowMonitor* self,
58  GdkRectangle* flipped_rect,
59  GdkRectangle* final_rect,
60  gboolean flipped_x,
61  gboolean flipped_y) {
62  // According to the documentation, the final_rect can be null
63  // if the backend can't obtain it.
64  // Reference: https://docs.gtk.org/gdk3/signal.Window.moved-to-rect.html
65  if (final_rect == nullptr) {
66  return;
67  }
68 
69  flutter::IsolateScope scope(self->isolate);
70  self->on_moved_to_rect(final_rect->x, final_rect->y, final_rect->width,
71  final_rect->height);
72 }
73 
74 // The moved-to-rect signal is on the GdkWindow, which only exists while the
75 // widget is realized. Re-connected on each realize, since unrealizing destroys
76 // the GdkWindow (and with it this connection).
77 static void realize_cb(FlWindowMonitor* self) {
78  GdkWindow* window = gtk_widget_get_window(GTK_WIDGET(self->window));
79  if (window == nullptr) {
80  return;
81  }
82  g_signal_connect_object(window, "moved-to-rect", G_CALLBACK(moved_to_rect_cb),
83  self, G_CONNECT_SWAPPED);
84 }
85 
86 static gboolean delete_event_cb(FlWindowMonitor* self, GdkEvent* event) {
87  flutter::IsolateScope scope(self->isolate);
88  self->on_close();
89 
90  // Stop default behaviour of destroying the window.
91  return TRUE;
92 }
93 
94 static void destroy_cb(FlWindowMonitor* self) {
95  flutter::IsolateScope scope(self->isolate);
96  self->on_destroy();
97 }
98 
99 static void fl_window_monitor_dispose(GObject* object) {
100  FlWindowMonitor* self = FL_WINDOW_MONITOR(object);
101 
102  g_clear_object(&self->window);
103 
104  G_OBJECT_CLASS(fl_window_monitor_parent_class)->dispose(object);
105 }
106 
107 static void fl_window_monitor_class_init(FlWindowMonitorClass* klass) {
108  G_OBJECT_CLASS(klass)->dispose = fl_window_monitor_dispose;
109 }
110 
111 static void fl_window_monitor_init(FlWindowMonitor* self) {}
112 
113 G_MODULE_EXPORT FlWindowMonitor* fl_window_monitor_new(
114  GtkWindow* window,
115  void (*on_configure)(void),
116  void (*on_state_changed)(void),
117  void (*on_is_active_notify)(void),
118  void (*on_title_notify)(void),
119  void (*on_moved_to_rect)(int, int, int, int),
120  void (*on_close)(void),
121  void (*on_destroy)(void)) {
122  FlWindowMonitor* self =
123  FL_WINDOW_MONITOR(g_object_new(fl_window_monitor_get_type(), nullptr));
124 
125  self->window = GTK_WINDOW(g_object_ref(window));
126  // Callbacks are made in the isolate this was created in. There is not always
127  // an isolate, e.g. in unit tests, and the isolate this object is created with
128  // does nothing when entered.
129  if (Dart_CurrentIsolate() != nullptr) {
130  self->isolate = flutter::Isolate::Current();
131  }
132  self->on_configure = on_configure;
133  self->on_state_changed = on_state_changed;
134  self->on_is_active_notify = on_is_active_notify;
135  self->on_title_notify = on_title_notify;
136  self->on_moved_to_rect = on_moved_to_rect;
137  self->on_close = on_close;
138  self->on_destroy = on_destroy;
139  g_signal_connect_object(window, "configure-event",
140  G_CALLBACK(configure_event_cb), self,
141  G_CONNECT_SWAPPED);
142  g_signal_connect_object(window, "window-state-event",
143  G_CALLBACK(window_state_event_cb), self,
144  G_CONNECT_SWAPPED);
145  g_signal_connect_object(window, "notify::is-active",
146  G_CALLBACK(is_active_notify_cb), self,
147  G_CONNECT_SWAPPED);
148  g_signal_connect_object(window, "notify::title", G_CALLBACK(title_notify_cb),
149  self, G_CONNECT_SWAPPED);
150  g_signal_connect_object(window, "realize", G_CALLBACK(realize_cb), self,
151  G_CONNECT_SWAPPED);
152  if (gtk_widget_get_realized(GTK_WIDGET(window))) {
153  realize_cb(self);
154  }
155  g_signal_connect_object(window, "delete-event", G_CALLBACK(delete_event_cb),
156  self, G_CONNECT_SWAPPED);
157  g_signal_connect_object(window, "destroy", G_CALLBACK(destroy_cb), self,
158  G_CONNECT_SWAPPED);
159 
160  return self;
161 }
static Isolate Current()
Definition: isolate_scope.cc:9
G_DEFINE_TYPE(FlBasicMessageChannelResponseHandle, fl_basic_message_channel_response_handle, G_TYPE_OBJECT) static void fl_basic_message_channel_response_handle_dispose(GObject *object)
static void destroy_cb(FlWindowMonitor *self)
static void is_active_notify_cb(FlWindowMonitor *self)
static void fl_window_monitor_dispose(GObject *object)
static void fl_window_monitor_class_init(FlWindowMonitorClass *klass)
static void title_notify_cb(FlWindowMonitor *self)
static void moved_to_rect_cb(FlWindowMonitor *self, GdkRectangle *flipped_rect, GdkRectangle *final_rect, gboolean flipped_x, gboolean flipped_y)
static void realize_cb(FlWindowMonitor *self)
static gboolean configure_event_cb(FlWindowMonitor *self, GdkEventConfigure *event)
static gboolean window_state_event_cb(FlWindowMonitor *self, GdkEventWindowState *event)
G_MODULE_EXPORT FlWindowMonitor * fl_window_monitor_new(GtkWindow *window, void(*on_configure)(void), void(*on_state_changed)(void), void(*on_is_active_notify)(void), void(*on_title_notify)(void), void(*on_moved_to_rect)(int, int, int, int), void(*on_close)(void), void(*on_destroy)(void))
static void fl_window_monitor_init(FlWindowMonitor *self)
static gboolean delete_event_cb(FlWindowMonitor *self, GdkEvent *event)
void(* on_title_notify)(void)
void(* on_state_changed)(void)
void(* on_is_active_notify)(void)
flutter::Isolate isolate
void(* on_configure)(void)
void(* on_destroy)(void)
void(* on_close)(void)
void(* on_moved_to_rect)(int, int, int, int)