labwc/src/interactive.c

118 lines
3.5 KiB
C
Raw Normal View History

2021-09-24 21:45:48 +01:00
// SPDX-License-Identifier: GPL-2.0-only
2020-05-29 21:27:34 +01:00
#include "labwc.h"
2022-01-05 08:30:07 +01:00
static int
max_move_scale(double pos_cursor, double pos_current,
double size_current, double size_orig)
{
double anchor_frac = (pos_cursor - pos_current) / size_current;
int pos_new = pos_cursor - (size_orig * anchor_frac);
if (pos_new < pos_current) {
/* Clamp by using the old offsets of the maximized window */
pos_new = pos_current;
}
return pos_new;
}
void
2020-10-21 20:30:06 +01:00
interactive_begin(struct view *view, enum input_mode mode, uint32_t edges)
2020-05-29 21:27:34 +01:00
{
if (view->maximized) {
2022-01-05 08:30:07 +01:00
if (mode == LAB_INPUT_STATE_MOVE) {
int new_x = max_move_scale(view->server->seat.cursor->x,
view->x, view->w, view->natural_geometry.width);
2022-01-05 08:30:07 +01:00
int new_y = max_move_scale(view->server->seat.cursor->y,
view->y, view->h, view->natural_geometry.height);
view->natural_geometry.x = new_x;
view->natural_geometry.y = new_y;
2022-01-05 08:30:07 +01:00
view_maximize(view, false);
/*
* view_maximize() indirectly calls view->impl->configure
* which is async but we are using the current values in
* server->grab_box. We pretend the configure already
* happened by setting them manually.
*/
view->x = new_x;
view->y = new_y;
view->w = view->natural_geometry.width;
view->h = view->natural_geometry.height;
2022-01-05 08:30:07 +01:00
} else {
return;
}
}
view->tiled = 0;
2020-05-29 21:27:34 +01:00
/*
* This function sets up an interactive move or resize operation, where
2022-01-05 08:30:07 +01:00
* the compositor stops propagating pointer events to clients and
2020-05-29 21:27:34 +01:00
* instead consumes them itself, to move or resize windows.
*/
struct seat *seat = &view->server->seat;
2020-05-29 21:27:34 +01:00
struct server *server = view->server;
server->grabbed_view = view;
2020-10-21 20:30:06 +01:00
server->input_mode = mode;
2020-05-29 21:27:34 +01:00
/* Remember view and cursor positions at start of move/resize */
server->grab_x = seat->cursor->x;
server->grab_y = seat->cursor->y;
struct wlr_box box = {
.x = view->x, .y = view->y, .width = view->w, .height = view->h
};
memcpy(&server->grab_box, &box, sizeof(struct wlr_box));
2020-05-29 21:27:34 +01:00
server->resize_edges = edges;
switch (mode) {
case LAB_INPUT_STATE_MOVE:
cursor_set(&server->seat, "grab");
break;
case LAB_INPUT_STATE_RESIZE:
cursor_set(&server->seat, wlr_xcursor_get_resize_name(edges));
break;
default:
break;
}
2020-05-29 21:27:34 +01:00
}
2021-12-03 23:15:28 +00:00
void
interactive_end(struct view *view)
{
if (view->server->grabbed_view == view) {
bool should_snap = view->server->input_mode == LAB_INPUT_STATE_MOVE
&& rc.snap_edge_range;
view->server->input_mode = LAB_INPUT_STATE_PASSTHROUGH;
view->server->grabbed_view = NULL;
if (should_snap) {
int snap_range = rc.snap_edge_range;
struct wlr_box *area = &view->output->usable_area;
/* Translate into output local coordinates */
double cursor_x = view->server->seat.cursor->x;
double cursor_y = view->server->seat.cursor->y;
wlr_output_layout_output_coords(view->server->output_layout,
view->output->wlr_output, &cursor_x, &cursor_y);
if (cursor_x <= area->x + snap_range) {
view_snap_to_edge(view, "left");
} else if (cursor_x >= area->x + area->width - snap_range) {
view_snap_to_edge(view, "right");
} else if (cursor_y <= area->y + snap_range) {
if (rc.snap_top_maximize) {
view_maximize(view, true);
2022-04-04 20:53:36 +01:00
/*
* When unmaximizing later on restore
* original position
*/
view->natural_geometry.x =
2022-04-04 20:53:36 +01:00
view->server->grab_box.x;
view->natural_geometry.y =
2022-04-04 20:53:36 +01:00
view->server->grab_box.y;
} else {
view_snap_to_edge(view, "up");
}
} else if (cursor_y >= area->y + area->height - snap_range) {
view_snap_to_edge(view, "down");
}
}
}
}