-
Notifications
You must be signed in to change notification settings - Fork 31
/
wf-autohide-window.cpp
332 lines (275 loc) · 9.36 KB
/
wf-autohide-window.cpp
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
#include "wf-autohide-window.hpp"
#include "wayfire-shell-unstable-v2-client-protocol.h"
#include <gtk-layer-shell.h>
#include <wf-shell-app.hpp>
#include <gdk/gdkwayland.h>
#include <glibmm.h>
#include <iostream>
#include <assert.h>
#define AUTOHIDE_SHOW_DELAY 300
#define AUTOHIDE_HIDE_DELAY 500
WayfireAutohidingWindow::WayfireAutohidingWindow(WayfireOutput *output,
const std::string& section) :
position{section + "/position"},
y_position{WfOption<int>{section + "/autohide_duration"}}
{
this->output = output;
this->set_decorated(false);
this->set_resizable(false);
gtk_layer_init_for_window(this->gobj());
gtk_layer_set_monitor(this->gobj(), output->monitor->gobj());
gtk_layer_set_namespace(this->gobj(), "$unfocus panel");
this->position.set_callback([=] () { this->update_position(); });
this->update_position();
this->signal_draw().connect_notify(
[=] (const Cairo::RefPtr<Cairo::Context>&) { update_margin(); });
this->signal_size_allocate().connect_notify(
[=] (Gtk::Allocation&) {
this->set_auto_exclusive_zone(this->has_auto_exclusive_zone);
this->setup_hotspot();
});
this->signal_focus_out_event().connect_notify(
[=] (const GdkEventFocus*) {
if (this->active_button)
unset_active_popover(*this->active_button);
});
}
WayfireAutohidingWindow::~WayfireAutohidingWindow()
{
if (this->edge_hotspot)
zwf_hotspot_v2_destroy(this->edge_hotspot);
if (this->panel_hotspot)
zwf_hotspot_v2_destroy(this->panel_hotspot);
}
wl_surface* WayfireAutohidingWindow::get_wl_surface() const
{
auto gdk_window = const_cast<GdkWindow*> (this->get_window()->gobj());
return gdk_wayland_window_get_wl_surface(gdk_window);
}
/** Verify that position is correct and return a correct position */
static std::string check_position(std::string position)
{
if (position == WF_WINDOW_POSITION_TOP)
return WF_WINDOW_POSITION_TOP;
if (position == WF_WINDOW_POSITION_BOTTOM)
return WF_WINDOW_POSITION_BOTTOM;
std::cerr << "Bad position in config file, defaulting to top" << std::endl;
return WF_WINDOW_POSITION_TOP;
}
static GtkLayerShellEdge get_anchor_edge(std::string position)
{
position = check_position(position);
if (position == WF_WINDOW_POSITION_TOP)
return GTK_LAYER_SHELL_EDGE_TOP;
if (position == WF_WINDOW_POSITION_BOTTOM)
return GTK_LAYER_SHELL_EDGE_BOTTOM;
assert(false); // not reached because check_position()
}
void WayfireAutohidingWindow::m_show_uncertain()
{
schedule_show(16); // add some delay to finish setting up the window
/* And don't forget to hide the window afterwards, if autohide is enabled */
if (should_autohide())
{
pending_hide = Glib::signal_timeout().connect([=] () {
schedule_hide(0);
return false;
}, AUTOHIDE_HIDE_DELAY);
}
}
void WayfireAutohidingWindow::update_position()
{
/* Reset old anchors */
gtk_layer_set_anchor(this->gobj(), GTK_LAYER_SHELL_EDGE_TOP, false);
gtk_layer_set_anchor(this->gobj(), GTK_LAYER_SHELL_EDGE_BOTTOM, false);
/* Set new anchor */
GtkLayerShellEdge anchor = get_anchor_edge(position);
gtk_layer_set_anchor(this->gobj(), anchor, true);
/* When the position changes, show an animation from the new edge. */
y_position.animate(-this->get_allocated_height(), -this->get_allocated_height());
setup_hotspot();
m_show_uncertain();
}
struct WayfireAutohidingWindowHotspotCallbacks
{
std::function<void()> on_enter;
std::function<void()> on_leave;
};
static void handle_hotspot_enter(void *data, zwf_hotspot_v2*)
{
auto cb = (WayfireAutohidingWindowHotspotCallbacks*) data;
cb->on_enter();
}
static void handle_hotspot_leave(void *data, zwf_hotspot_v2*)
{
auto cb = (WayfireAutohidingWindowHotspotCallbacks*) data;
cb->on_leave();
}
static zwf_hotspot_v2_listener hotspot_listener = {
.enter = handle_hotspot_enter,
.leave = handle_hotspot_leave,
};
/**
* An autohide window needs 2 hotspots.
* One of them is used to trigger autohide and is generally a tiny strip on the
* edge of the output.
*
* The other hotspot covers the whole window. It is used primarily to know when
* the input leaves the window, in which case we need to hide the window again.
*/
void WayfireAutohidingWindow::setup_hotspot()
{
if (!this->output->output)
return;
/* No need to recreate hotspots if the height didn't change */
if (this->get_allocated_height() == last_hotspot_height)
return;
this->last_hotspot_height = get_allocated_height();
if (this->edge_hotspot)
zwf_hotspot_v2_destroy(edge_hotspot);
if (this->panel_hotspot)
zwf_hotspot_v2_destroy(panel_hotspot);
auto position = check_position(this->position);
uint32_t edge = (position == WF_WINDOW_POSITION_TOP) ?
ZWF_OUTPUT_V2_HOTSPOT_EDGE_TOP : ZWF_OUTPUT_V2_HOTSPOT_EDGE_BOTTOM;
// TODO: make edge offset configurable
this->edge_hotspot = zwf_output_v2_create_hotspot(output->output,
edge, 20, AUTOHIDE_SHOW_DELAY);
this->panel_hotspot = zwf_output_v2_create_hotspot(output->output,
edge, this->get_allocated_height(), 0); // immediate
this->edge_callbacks =
std::make_unique<WayfireAutohidingWindowHotspotCallbacks> ();
this->panel_callbacks =
std::make_unique<WayfireAutohidingWindowHotspotCallbacks> ();
edge_callbacks->on_enter = [=] () {
schedule_show(0);
};
edge_callbacks->on_leave = [=] () {
// nothing
};
this->input_inside_panel = false;
panel_callbacks->on_enter = [=] () {
if (this->pending_hide.connected())
this->pending_hide.disconnect();
this->input_inside_panel = true;
};
panel_callbacks->on_leave = [=] () {
this->input_inside_panel = false;
if (this->should_autohide())
this->schedule_hide(AUTOHIDE_HIDE_DELAY);
};
zwf_hotspot_v2_add_listener(edge_hotspot, &hotspot_listener,
edge_callbacks.get());
zwf_hotspot_v2_add_listener(panel_hotspot, &hotspot_listener,
panel_callbacks.get());
}
void WayfireAutohidingWindow::set_auto_exclusive_zone(bool has_zone)
{
this->has_auto_exclusive_zone = has_zone;
int target_zone = has_zone ? get_allocated_height() : 0;
if (this->last_zone != target_zone)
{
gtk_layer_set_exclusive_zone(this->gobj(), target_zone);
last_zone = target_zone;
}
}
void WayfireAutohidingWindow::increase_autohide()
{
++autohide_counter;
if (should_autohide())
schedule_hide(0);
}
void WayfireAutohidingWindow::decrease_autohide()
{
autohide_counter = std::max(autohide_counter - 1, 0);
if (!should_autohide())
schedule_show(0);
}
bool WayfireAutohidingWindow::should_autohide() const
{
return autohide_counter && !this->active_button && !this->input_inside_panel;
}
bool WayfireAutohidingWindow::m_do_hide()
{
y_position.animate(-get_allocated_height());
update_margin();
return false; // disconnect
}
void WayfireAutohidingWindow::schedule_hide(int delay)
{
pending_show.disconnect();
if (delay == 0)
{
m_do_hide();
return;
}
if (!pending_hide.connected())
{
pending_hide = Glib::signal_timeout().connect(
sigc::mem_fun(this, &WayfireAutohidingWindow::m_do_hide), delay);
}
}
bool WayfireAutohidingWindow::m_do_show()
{
y_position.animate(0);
update_margin();
return false; // disconnect
}
void WayfireAutohidingWindow::schedule_show(int delay)
{
pending_hide.disconnect();
if (delay == 0)
{
m_do_show();
return;
}
if (!pending_show.connected())
{
pending_show = Glib::signal_timeout().connect(
sigc::mem_fun(this, &WayfireAutohidingWindow::m_do_show), delay);
}
}
bool WayfireAutohidingWindow::update_margin()
{
if (y_position.running())
{
gtk_layer_set_margin(this->gobj(),
get_anchor_edge(position), y_position);
this->queue_draw(); // XXX: is this necessary?
return true;
}
return false;
}
void WayfireAutohidingWindow::set_active_popover(WayfireMenuButton& button)
{
if (&button != this->active_button)
{
if (this->active_button)
{
this->popover_hide.disconnect();
this->active_button->set_active(false);
this->active_button->get_popover()->popdown();
}
this->active_button = &button;
this->popover_hide =
this->active_button->m_popover.signal_hide().connect_notify(
[this, &button] () { unset_active_popover(button); });
}
bool should_grab_focus = this->active_button->is_keyboard_interactive();
gtk_layer_set_keyboard_interactivity(this->gobj(), should_grab_focus);
this->active_button->set_has_focus(should_grab_focus);
schedule_show(0);
}
void WayfireAutohidingWindow::unset_active_popover(WayfireMenuButton& button)
{
if (!this->active_button || &button != this->active_button)
return;
this->active_button->set_has_focus(false);
this->active_button->set_active(false);
this->active_button->get_popover()->popdown();
this->active_button = nullptr;
this->popover_hide.disconnect();
gtk_layer_set_keyboard_interactivity(this->gobj(), false);
if (should_autohide())
schedule_hide(AUTOHIDE_HIDE_DELAY);
}