Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 45 additions & 3 deletions plugins/grid/grid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "wayfire/plugin.hpp"
#include "wayfire/signal-definitions.hpp"
#include <wayfire/plugins/common/geometry-animation.hpp>
#include <wayfire/plugins/common/preview-indication.hpp>
#include "wayfire/plugins/grid.hpp"
#include "wayfire/plugins/crossfade.hpp"
#include <wayfire/window-manager.hpp>
Expand Down Expand Up @@ -55,6 +56,13 @@ class wayfire_grid : public wf::plugin_interface_t, public wf::per_output_tracke
std::vector<std::string> slots = {"unused", "bl", "b", "br", "l", "c", "r", "tl", "t", "tr"};
wf::ipc_activator_t bindings[10];
wf::ipc_activator_t restore{"grid/restore"};
wf::option_wrapper_t<int> snap_threshold{"move/snap_threshold"};
wf::option_wrapper_t<int> quarter_snap_threshold{"move/quarter_snap_threshold"};
struct
{
std::shared_ptr<wf::preview_indication_t> preview;
wf::grid::slot_t slot_id = wf::grid::SLOT_NONE;
} slot;

wf::plugin_activation_data_t grab_interface{
.name = "grid",
Expand Down Expand Up @@ -102,13 +110,47 @@ class wayfire_grid : public wf::plugin_interface_t, public wf::per_output_tracke
});
}

wf::get_core().connect(&grid_request_signal_cb);
wf::get_core().connect(&grid_update_slot_preview_signal_cb);
}

wf::signal::connection_t<wf::grid::grid_request_signal> grid_request_signal_cb =
[=] (wf::grid::grid_request_signal *ev)
wf::signal::connection_t<wf::grid::grid_update_slot_preview> grid_update_slot_preview_signal_cb =
[=] (wf::grid::grid_update_slot_preview_signal *ev)
{
ev->carried_out = true;
wf::grid::slot_t new_slot_id = ev->force_disable_preview ? wf::grid::slot_t::SLOT_NONE :
wf::grid::calc_slot(ev->output, ev->input, ev->snap_threshold, ev->quarter_snap_threshold);

/* No changes in the slot, just return */
if (slot.slot_id == new_slot_id)
{
return;
}

/* Destroy previous preview */
if (slot.preview)
{
auto input = ev->input;
slot.preview->set_target_geometry({input.x, input.y, 1, 1}, 0, true);
slot.preview = nullptr;
}

slot.slot_id = new_slot_id;

/* Show a preview overlay */
if (new_slot_id)
{
wf::geometry_t slot_geometry = wf::grid::get_slot_dimensions(ev->output, new_slot_id);
/* Unknown slot geometry, can't show a preview */
if ((slot_geometry.width <= 0) || (slot_geometry.height <= 0))
{
return;
}

auto input = ev->input;
slot.preview = std::make_shared<wf::preview_indication_t>(
wf::geometry_t{input.x, input.y, 1, 1}, ev->output, "move");
slot.preview->set_target_geometry(slot_geometry, 1);
}
};

void handle_new_output(wf::output_t *output) override
Expand Down
55 changes: 54 additions & 1 deletion plugins/grid/wayfire/plugins/grid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ namespace grid
* when: Emitted before move renders a grid indicator and sets the slot.
* carried_out: true if a plugin can handle move request to grid.
*/
struct grid_request_signal
struct grid_update_slot_preview_signal
{
/* True if a plugin handled this signal */
bool carried_out = false;
bool force_disable_preview = false;
wf::output_t *output;
/* input coordinates in output-local space */
wf::point_t input;
};

/**
Expand Down Expand Up @@ -118,5 +122,54 @@ inline wf::geometry_t get_slot_dimensions(wf::output_t *output, int n)

return area;
}

/* Calculate the slot to which the view would be snapped if the input
* is released at output-local coordinates (x, y) */
inline wf::grid::slot_t calc_slot(wf::output_t *output, wf::point_t point, int snap_threshold, int quarter_snap_threshold)
{
auto g = output->workarea->get_workarea();

int threshold = snap_threshold;

bool is_left = point.x - g.x <= threshold;
bool is_right = g.x + g.width - point.x <= threshold;
bool is_top = point.y - g.y < threshold;
bool is_bottom = g.x + g.height - point.y < threshold;

bool is_far_left = point.x - g.x <= quarter_snap_threshold;
bool is_far_right = g.x + g.width - point.x <= quarter_snap_threshold;
bool is_far_top = point.y - g.y < quarter_snap_threshold;
bool is_far_bottom = g.x + g.height - point.y < quarter_snap_threshold;

wf::grid::slot_t slot = wf::grid::SLOT_NONE;
if ((is_left && is_far_top) || (is_far_left && is_top))
{
slot = wf::grid::SLOT_TL;
} else if ((is_right && is_far_top) || (is_far_right && is_top))
{
slot = wf::grid::SLOT_TR;
} else if ((is_right && is_far_bottom) || (is_far_right && is_bottom))
{
slot = wf::grid::SLOT_BR;
} else if ((is_left && is_far_bottom) || (is_far_left && is_bottom))
{
slot = wf::grid::SLOT_BL;
} else if (is_right)
{
slot = wf::grid::SLOT_RIGHT;
} else if (is_left)
{
slot = wf::grid::SLOT_LEFT;
} else if (is_top)
{
// Maximize when dragging to the top
slot = wf::grid::SLOT_CENTER;
} else if (is_bottom)
{
slot = wf::grid::SLOT_BOTTOM;
}

return slot;
}
}
}
138 changes: 47 additions & 91 deletions plugins/single_plugins/move.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class wayfire_move : public wf::per_output_plugin_instance_t,
wf::grid::slot_t slot_id = wf::grid::SLOT_NONE;
} slot;


wf::wl_timer<false> workspace_switch_timer;
wf::grid::slot_t workspace_switch_slot_id = wf::grid::SLOT_NONE;

wf::shared_data::ref_ptr_t<wf::move_drag::core_drag_t> drag_helper;

Expand All @@ -72,11 +72,11 @@ class wayfire_move : public wf::per_output_plugin_instance_t,
} else
{
input_grab->regrab_input();
update_slot(calc_slot(get_input_coords()));
update_slot(false);
}
} else
{
update_slot(wf::grid::SLOT_NONE);
update_slot(true);
}
};

Expand Down Expand Up @@ -111,7 +111,7 @@ class wayfire_move : public wf::per_output_plugin_instance_t,
wf::grid::get_tiled_edges_for_slot(slot.slot_id));

/* Update slot, will hide the preview as well */
update_slot(wf::grid::SLOT_NONE);
update_slot(true);
}

wf::get_core().default_wm->set_view_grabbed(ev->main_view, false);
Expand Down Expand Up @@ -296,7 +296,7 @@ class wayfire_move : public wf::per_output_plugin_instance_t,
}

this->input_grab->grab_input(wf::scene::layer::OVERLAY);
update_slot(wf::grid::SLOT_NONE);
update_slot(true);
return true;
}

Expand Down Expand Up @@ -348,7 +348,7 @@ class wayfire_move : public wf::per_output_plugin_instance_t,
drag_helper->set_pending_drag(grab_position);
drag_helper->start_drag(view, opts);
drag_helper->handle_motion(get_global_input_coords());
update_slot(wf::grid::SLOT_NONE);
update_slot(true);
return true;
}

Expand All @@ -368,66 +368,16 @@ class wayfire_move : public wf::per_output_plugin_instance_t,
drag_helper->handle_input_released();
}

/* Calculate the slot to which the view would be snapped if the input
* is released at output-local coordinates (x, y) */
wf::grid::slot_t calc_slot(wf::point_t point)
void update_workspace_switch_timeout(bool force_disable_timer)
{
if (!is_snap_enabled())
{
return wf::grid::SLOT_NONE;
}

auto g = output->workarea->get_workarea();
if (!(output->get_relative_geometry() & point))
{
return wf::grid::SLOT_NONE;
}

int threshold = snap_threshold;

bool is_left = point.x - g.x <= threshold;
bool is_right = g.x + g.width - point.x <= threshold;
bool is_top = point.y - g.y < threshold;
bool is_bottom = g.x + g.height - point.y < threshold;

bool is_far_left = point.x - g.x <= quarter_snap_threshold;
bool is_far_right = g.x + g.width - point.x <= quarter_snap_threshold;
bool is_far_top = point.y - g.y < quarter_snap_threshold;
bool is_far_bottom = g.x + g.height - point.y < quarter_snap_threshold;

wf::grid::slot_t slot = wf::grid::SLOT_NONE;
if ((is_left && is_far_top) || (is_far_left && is_top))
wf::grid::slot_t slot_id = force_disable_timer ? wf::grid::SLOT_NONE :
wf::grid::calc_slot(output, input, snap_threshold, quarter_snap_threshold);
if (slot_id == workspace_switch_slot_id)
{
slot = wf::grid::SLOT_TL;
} else if ((is_right && is_far_top) || (is_far_right && is_top))
{
slot = wf::grid::SLOT_TR;
} else if ((is_right && is_far_bottom) || (is_far_right && is_bottom))
{
slot = wf::grid::SLOT_BR;
} else if ((is_left && is_far_bottom) || (is_far_left && is_bottom))
{
slot = wf::grid::SLOT_BL;
} else if (is_right)
{
slot = wf::grid::SLOT_RIGHT;
} else if (is_left)
{
slot = wf::grid::SLOT_LEFT;
} else if (is_top)
{
// Maximize when dragging to the top
slot = wf::grid::SLOT_CENTER;
} else if (is_bottom)
{
slot = wf::grid::SLOT_BOTTOM;
return;
}

return slot;
}

void update_workspace_switch_timeout(wf::grid::slot_t slot_id)
{
workspace_switch_slot_id = slot_id;
if ((workspace_switch_after == -1) || (slot_id == wf::grid::SLOT_NONE))
{
workspace_switch_timer.disconnect();
Expand Down Expand Up @@ -484,50 +434,56 @@ class wayfire_move : public wf::per_output_plugin_instance_t,
});
}

void update_slot(wf::grid::slot_t new_slot_id)
void update_slot(bool force_disable_preview)
{
/* No changes in the slot, just return */
if (slot.slot_id == new_slot_id)
auto input = get_input_coords();
if (!is_snap_enabled() || !(output->get_relative_geometry() & input))
{
force_disable_preview = true;
}
wf::grid::slot_t old_slot_id = slot.slot_id;
wf::grid::grid_update_slot_preview_signal grid_signal;
grid_signal.output = output;
grid_signal.input = input;
grid_signal.force_disable_preview = force_disable_preview;
wf::get_core().emit(&grid_signal);

if (grid_signal.carried_out) {
update_workspace_switch_timeout(force_disable_preview);
return;
}

/* Destroy previous preview */
if (slot.preview)
/* if the grid plugin isn't loaded, we're handling the preview for maximize */
slot.slot_id = force_disable_timer ? wf::grid::SLOT_NONE :
wf::grid::calc_slot(output, input, snap_threshold, quarter_snap_threshold);
if (slot.slot_id != wf::grid::slot_t::SLOT_CENTER)
{
auto input = get_input_coords();
slot.preview->set_target_geometry({input.x, input.y, 1, 1}, 0, true);
slot.preview = nullptr;
slot.slot_id = wf::grid::slot_t::SLOT_NONE;
}

wf::grid::grid_request_signal grid_signal;
wf::get_core().emit(&grid_signal);

if (grid_signal.carried_out || (new_slot_id == wf::grid::slot_t::SLOT_CENTER))
if (old_slot_id == slot.slot_id)
{
slot.slot_id = new_slot_id;
} else
return;
}

if (slot.preview)
{
slot.slot_id = new_slot_id = wf::grid::slot_t::SLOT_NONE;
slot.preview->set_target_geometry({input.x, input.y, 1, 1}, 0, true);
slot.preview = nullptr;
}

/* Show a preview overlay */
if (new_slot_id)
if (slot.slot_id)
{
wf::geometry_t slot_geometry = wf::grid::get_slot_dimensions(output, new_slot_id);
/* Unknown slot geometry, can't show a preview */
if ((slot_geometry.width <= 0) || (slot_geometry.height <= 0))
wf::geometry_t slot_geometry = output->workarea->get_workarea();
/* only show a preview with valid geometry */
if ((slot_geometry.width > 0) || (slot_geometry.height > 0))
{
return;
slot.preview = std::make_shared<wf::preview_indication_t>(
wf::geometry_t{input.x, input.y, 1, 1}, output, "move");
slot.preview->set_target_geometry(slot_geometry, 1);
}

auto input = get_input_coords();
slot.preview = std::make_shared<wf::preview_indication_t>(
wf::geometry_t{input.x, input.y, 1, 1}, output, "move");
slot.preview->set_target_geometry(slot_geometry, 1);
}

update_workspace_switch_timeout(new_slot_id);
update_workspace_switch_timeout(force_disable_preview);
}

/* Returns the currently used input coordinates in global compositor space */
Expand Down Expand Up @@ -582,7 +538,7 @@ class wayfire_move : public wf::per_output_plugin_instance_t,
void handle_input_motion()
{
drag_helper->handle_motion(get_global_input_coords());
update_slot(calc_slot(get_input_coords()));
update_slot(false);
}

void fini() override
Expand Down