-
-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathwindow_impl.cpp
More file actions
336 lines (288 loc) · 11.7 KB
/
window_impl.cpp
File metadata and controls
336 lines (288 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include <gdk/gdk.h>
#include <gtk/gtk.h>
#include "./debug_helper.h"
#include "./gtk_utils.h"
#include "./window_info.h"
#include "include/bitsdojo_window_linux/bitsdojo_window_plugin.h"
namespace bitsdojo_window {
static gboolean onMouseMoveHook(GSignalInvocationHint *ihint,
guint n_param_values,
const GValue *param_values, gpointer data);
static gboolean onMousePressHook(GSignalInvocationHint *ihint,
guint n_param_values,
const GValue *param_values, gpointer data);
static gboolean onMouseReleaseHook(GSignalInvocationHint *ihint,
guint n_param_values,
const GValue *param_values, gpointer data);
class BitsdojoWindowImpl : BitsdojoWindowGtk {
public:
GtkWindow *handle = nullptr;
GtkWidget *event_box = nullptr;
GdkWindowEdge currentEdge;
gboolean isOnEdge = false;
gboolean isMaximized = false;
gboolean isDragging = false;
gboolean isResizing = false;
GdkEventButton currentPressedEvent = GdkEventButton{};
gulong flutterButtonPressHandler = 0;
gboolean isFlutterButtonPressBlocked = false;
gulong mouseMoveHookID = 0;
gulong mousePressHookID = 0;
gulong mouseReleaseHookId = 0;
WindowInfo *_windowInfo = nullptr;
BitsdojoWindowImpl() {}
WindowInfo* _getWindowInfo() {
if (nullptr == _windowInfo) {
_windowInfo = bitsdojo_window::getWindowInfo(this->handle);
}
return _windowInfo;
}
void blockButtonPress() {
if (0 == flutterButtonPressHandler) {
flutterButtonPressHandler = g_signal_handler_find(
event_box, G_SIGNAL_MATCH_ID,
g_signal_lookup("button-press-event", GTK_TYPE_WIDGET), 0, NULL,
NULL, NULL);
}
if (isFlutterButtonPressBlocked) {
return;
}
g_signal_handler_block(event_box, flutterButtonPressHandler);
isFlutterButtonPressBlocked = true;
}
void unblockButtonPress() {
if (!isFlutterButtonPressBlocked) {
return;
}
isFlutterButtonPressBlocked = false;
g_signal_handler_unblock(event_box, flutterButtonPressHandler);
}
void updateEdge(int x, int y) {
GdkWindowEdge edge = this->currentEdge;
auto windowInfo = _getWindowInfo();
int width = windowInfo->width;
int height = windowInfo->height;
auto isOnEdge =
getWindowEdge(width, height, x, y, &edge, windowInfo->gripSize);
auto isMaximized = gtk_window_is_maximized(this->handle);
if ((edge != this->currentEdge) || (isOnEdge != this->isOnEdge) ||
(isMaximized != this->isMaximized)) {
this->isMaximized = isMaximized;
this->isOnEdge = isOnEdge;
this->currentEdge = edge;
this->updateMouseCursor();
}
}
void resetMouseCursor() {
GdkWindow *gdw = gtk_widget_get_window(GTK_WIDGET(this->handle));
GdkCursor *cursor =
gdk_cursor_new_from_name(gdk_window_get_display(gdw), "default");
gdk_window_set_cursor(gdw, cursor);
g_object_unref(cursor);
}
void updateMouseCursor() {
auto cursor_name = (isOnEdge && !isMaximized)
? getCursorForEdge(currentEdge)
: "default";
GdkWindow *gdw = gtk_widget_get_window(GTK_WIDGET(this->handle));
GdkCursor *cursor =
gdk_cursor_new_from_name(gdk_window_get_display(gdw), cursor_name);
gdk_window_set_cursor(gdw, cursor);
g_object_unref(cursor);
}
void getMousePositionInsideWindow(gint *x, gint *y) {
getMousePositionOnScreen(this->handle, x, y);
auto windowInfo = _getWindowInfo();
*x = *x - windowInfo->x;
*y = *y - windowInfo->y;
}
void setCustomFrame(gboolean value) {
if (false == value) {
return;
}
auto window = this->handle;
auto screen = gtk_window_get_screen(window);
gtk_window_set_decorated(window, FALSE);
auto rgba = gdk_screen_get_rgba_visual(screen);
if (rgba && gdk_screen_is_composited(screen)) {
gtk_widget_set_visual(GTK_WIDGET(window), rgba);
}
auto *backgroundStyle = gtk_css_provider_new();
gtk_css_provider_load_from_data(GTK_CSS_PROVIDER(backgroundStyle),
"window {\n"
" background:none;\n"
"}\n",
-1, NULL);
gtk_style_context_add_provider_for_screen(
screen, GTK_STYLE_PROVIDER(backgroundStyle),
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
g_object_unref(backgroundStyle);
}
void addHooks() {
mouseMoveHookID = g_signal_add_emission_hook(
g_signal_lookup("motion-notify-event", GTK_TYPE_WIDGET), 0,
onMouseMoveHook, this, NULL);
mousePressHookID = g_signal_add_emission_hook(
g_signal_lookup("button-press-event", GTK_TYPE_WIDGET), 0,
onMousePressHook, this, NULL);
mouseReleaseHookId = g_signal_add_emission_hook(
g_signal_lookup("button-release-event", GTK_TYPE_WIDGET), 0,
onMouseReleaseHook, this, NULL);
}
void findEventBox(GtkWidget *widget) {
GList *children;
GtkWidget *currentChild;
children = gtk_container_get_all_children(GTK_CONTAINER(widget));
while (children) {
currentChild = (GtkWidget *)children->data;
if (GTK_IS_EVENT_BOX(currentChild)) {
this->event_box = currentChild;
this->addHooks();
}
children = children->next;
}
}
};
static gboolean onMousePressHook(GSignalInvocationHint *ihint,
guint n_param_values,
const GValue *param_values, gpointer data) {
auto self = reinterpret_cast<BitsdojoWindowImpl *>(data);
gpointer instance = g_value_peek_pointer(param_values);
if (!GTK_IS_EVENT_BOX(instance)) {
return TRUE;
}
GdkEventButton *event =
(GdkEventButton *)(g_value_get_boxed(param_values + 1));
if (self->isOnEdge && !self->isMaximized) {
self->blockButtonPress();
self->isResizing = true;
gtk_window_begin_resize_drag(
self->handle, self->currentEdge, event->button,
static_cast<gint>(event->x_root), static_cast<gint>(event->y_root),
event->time);
}
memset(&self->currentPressedEvent, 0, sizeof(self->currentPressedEvent));
memcpy(&self->currentPressedEvent, event,
sizeof(self->currentPressedEvent));
return TRUE;
}
static gboolean onMouseReleaseHook(GSignalInvocationHint *ihint,
guint n_param_values,
const GValue *param_values, gpointer data) {
auto self = reinterpret_cast<BitsdojoWindowImpl *>(data);
gpointer instance = g_value_peek_pointer(param_values);
if (!GTK_IS_EVENT_BOX(instance)) {
return TRUE;
}
// GdkEventButton *event = (GdkEventButton*)(g_value_get_boxed(param_values
// + 1));
self->unblockButtonPress();
return TRUE;
}
static gboolean onMouseMoveHook(GSignalInvocationHint *ihint,
guint n_param_values,
const GValue *param_values, gpointer data) {
auto self = reinterpret_cast<BitsdojoWindowImpl *>(data);
gpointer instance = g_value_peek_pointer(param_values);
if (!GTK_IS_EVENT_BOX(instance)) {
return TRUE;
}
GdkEventMotion *event =
(GdkEventMotion *)(g_value_get_boxed(param_values + 1));
if (!event) return TRUE;
self->updateEdge(event->x, event->y);
return TRUE;
}
BitsdojoWindowImpl *_appWindow = nullptr;
BitsdojoWindowImpl *getAppWindowInstance() {
if (_appWindow == nullptr) {
_appWindow = new BitsdojoWindowImpl;
}
return _appWindow;
}
static GdkDevice* get_device(BitsdojoWindowImpl *self) {
auto window = self->handle;
auto screen = gtk_window_get_screen(window);
auto display = gdk_screen_get_display(screen);
auto seat = gdk_display_get_default_seat(display);
return gdk_seat_get_pointer(seat);
}
static gboolean onWindowEventAfter(GtkWidget *text_view, GdkEvent *event,
BitsdojoWindowImpl *self) {
if (event->type == GDK_ENTER_NOTIFY) {
if (nullptr == self->event_box) {
return FALSE;
}
if (self->isDragging) {
self->isDragging = false;
auto newEvent = (GdkEventButton *)gdk_event_new(GDK_BUTTON_RELEASE);
newEvent->x = self->currentPressedEvent.x;
newEvent->y = self->currentPressedEvent.y;
newEvent->button = self->currentPressedEvent.button;
newEvent->type = GDK_BUTTON_RELEASE;
newEvent->time = g_get_monotonic_time();
newEvent->device = get_device(self);
gboolean result;
g_signal_emit_by_name(self->event_box, "button-release-event",
newEvent, &result);
gdk_event_free((GdkEvent *)newEvent);
}
if (self->isResizing) {
self->isResizing = false;
}
self->unblockButtonPress();
gint x, y;
self->getMousePositionInsideWindow(&x, &y);
emitMouseMoveEvent(self->event_box, x, y, get_device(self));
} else if (event->type == GDK_LEAVE_NOTIFY) {
emitMouseMoveEvent(self->event_box,-1, -1, get_device(self));
} else {
// bitsdojo_window::printGdkEvent("event after", event->type);
}
return FALSE;
}
gboolean onWindowSizeMove(GtkWidget *widget, GdkEventConfigure *event,
BitsdojoWindowImpl *self) {
auto windowInfo = self->_getWindowInfo();
windowInfo->x = event->x;
windowInfo->y = event->y;
windowInfo->width = event->width;
windowInfo->height = event->height;
GdkRectangle screenRectangle;
getScreenRectForWindow(GTK_WINDOW(widget), &screenRectangle);
windowInfo->screenX = screenRectangle.x;
windowInfo->screenY = screenRectangle.y;
windowInfo->screenWidth = screenRectangle.width;
windowInfo->screenHeight = screenRectangle.height;
int scaleFactor = 0;
getScaleFactorForWindow(self->handle, &scaleFactor);
windowInfo->scaleFactor = scaleFactor;
return FALSE;
}
} // namespace bitsdojo_window
BitsdojoWindowGtk *bitsdojo_window_from(GtkWindow *window) {
auto appWindow = bitsdojo_window::getAppWindowInstance();
appWindow->handle = window;
return reinterpret_cast<BitsdojoWindowGtk *>(appWindow);
}
void enhanceFlutterView(GtkWidget *flutterView) {
auto appWindow = bitsdojo_window::getAppWindowInstance();
auto topLevelWindow = GTK_WINDOW(gtk_widget_get_toplevel(flutterView));
appWindow->handle = topLevelWindow;
g_signal_connect(topLevelWindow, "event-after",
G_CALLBACK(bitsdojo_window::onWindowEventAfter),
appWindow);
g_signal_connect(topLevelWindow, "configure-event",
G_CALLBACK(bitsdojo_window::onWindowSizeMove), appWindow);
appWindow->findEventBox(flutterView);
}
void startWindowDrag(GtkWindow *window) {
gint x, y;
bitsdojo_window::getMousePositionOnScreen(window, &x, &y);
auto appWindow = bitsdojo_window::getAppWindowInstance();
if (appWindow->handle == window) {
appWindow->isDragging = true;
}
gtk_window_begin_move_drag(window, 1, x, y,
(guint32)g_get_monotonic_time());
}