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
36 changes: 36 additions & 0 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,7 @@ 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"};
std::shared_ptr<wf::preview_indication_t> slot_preview;

wf::plugin_activation_data_t grab_interface{
.name = "grid",
Expand Down Expand Up @@ -109,6 +111,40 @@ class wayfire_grid : public wf::plugin_interface_t, public wf::per_output_tracke
[=] (wf::grid::grid_request_signal *ev)
{
ev->carried_out = true;
wf::grid::slot_t new_slot_id = ev->force_off ? wf::grid::slot_t::SLOT_NONE :
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be way simpler if we let the move plugin calculate the slot (the helper function is available in the helper after all)? This way we don't have to pass the snap threshold, quarter snap threshold and we don't need force_off either - these can all live entirely in the move plugin.

Then in the signal we can maybe simply pass a slot_hint, which is a hint as to which slot should be activated. Then, your custom grid implementation (as you have said that you want to replace grid with custom slots) can recalculate the slot based on whatever thresholds and algorithms you want to.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disliked passing all of those variables as well, but I think it is necessary if keeping behavior identical. An alternate implementation of grid that is using regions different than the outputs otherwise wouldn't be able to use the same thresholds.

If the goal isn't to keep identical behavior, I would instead argue that the snap thresholds should live in the grid plugin and have a single snap threshold in the move plugin for when the grid plugin isn't in use. That can't be done because those same thresholds are used implicitly by the update_workspace_switch_timeout, which is now broken in the case where the grid plugin is not in use. I think it was already half-broken in that case, where the timer would keep getting reset if you keep moving the mouse even if you stay within the threshold. Both cases might not matter at all because that requires snapping to be enabled without a grid plugin loaded. I think the best solution would be to decouple workspace switching from snapping in the move plugin and doing a separate (though usually redundant) calculation for workspace switching and have it keep its own state.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternate implementation of grid that is using regions different than the outputs otherwise wouldn't be able to use the same thresholds.

This is not true, these values come from the configuration file. Another grid implementation wishing to keep up with core's move plugin can query the same options from the config file. Any plugin can query any other plugin's options.

think the best solution would be to decouple workspace switching from snapping in the move plugin

Certainly makes sense.

wf::grid::calc_slot(ev->output, ev->input, ev->snap_threshold, ev->quarter_snap_threshold);

/* No changes in the slot, just return */
if (ev->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;
}

ev->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
79 changes: 67 additions & 12 deletions plugins/grid/wayfire/plugins/grid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,6 @@ namespace wf
{
namespace grid
{
/**
* name: request
* on: core
* 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
{
/* True if a plugin handled this signal */
bool carried_out = false;
};

/**
* The slot where a view can be placed with grid.
* BL = bottom-left, TR = top-right, etc.
Expand All @@ -38,6 +26,24 @@ enum slot_t
SLOT_TR = 9,
};

/**
* name: request
* on: core
* 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
{
/* True if a plugin handled this signal */
bool carried_out = false;
bool force_off = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I needed some time to figure out what this does exactly, maybe we can rename it to force_hide_preview or similar? Also I think that at this point we could rename the signal to grid_update_slot_preview_signal, it is a bit longer but it shows much more clearly what the signal is all about.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had trouble with naming that field and that definitely an improvement. I wanted to keep my changes to a minimum, so I didn't think about changing the signal name.

wf::output_t *output;
wf::point_t input;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one would need a docstring to clarify what it is actually (something like "input position relative to the indicated output")

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do.

int snap_threshold = 0;
int quarter_snap_threshold = 0;
wf::grid::slot_t slot_id = SLOT_NONE;
};

/*
* 7 8 9
* 4 5 6
Expand Down Expand Up @@ -118,5 +124,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: 45 additions & 93 deletions plugins/single_plugins/move.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ 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::shared_data::ref_ptr_t<wf::move_drag::core_drag_t> drag_helper;
Expand All @@ -72,11 +71,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 +110,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 +295,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 +347,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,64 +367,6 @@ 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)
{
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))
{
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;
}

void update_workspace_switch_timeout(wf::grid::slot_t slot_id)
{
if ((workspace_switch_after == -1) || (slot_id == wf::grid::SLOT_NONE))
Expand Down Expand Up @@ -484,50 +425,61 @@ 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_off)
{
/* No changes in the slot, just return */
if (slot.slot_id == new_slot_id)
{
return;
}

/* Destroy previous preview */
if (slot.preview)
auto input = get_input_coords();
if (!is_snap_enabled() || !(output->get_relative_geometry() & input))
{
auto input = get_input_coords();
slot.preview->set_target_geometry({input.x, input.y, 1, 1}, 0, true);
slot.preview = nullptr;
force_off = true;
}

wf::grid::slot_t old_slot_id = slot.slot_id;
wf::grid::grid_request_signal grid_signal;
grid_signal.output = output;
grid_signal.input = input;
grid_signal.snap_threshold = snap_threshold;
grid_signal.quarter_snap_threshold = quarter_snap_threshold;
grid_signal.slot_id = slot.slot_id;
grid_signal.force_off = force_off;
wf::get_core().emit(&grid_signal);

if (grid_signal.carried_out || (new_slot_id == wf::grid::slot_t::SLOT_CENTER))
if (grid_signal.carried_out)
{
slot.slot_id = new_slot_id;
slot.slot_id = grid_signal.slot_id;
} else
{
slot.slot_id = new_slot_id = wf::grid::slot_t::SLOT_NONE;
slot.slot_id = wf::grid::calc_slot(output, input, snap_threshold, quarter_snap_threshold);
if (slot.slot_id != wf::grid::slot_t::SLOT_CENTER)
{
slot.slot_id = wf::grid::slot_t::SLOT_NONE;
}
}

/* Show a preview overlay */
if (new_slot_id)
if (old_slot_id == 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))
return;
}

/* if the grid plugin isn't loaded, we're handling the preview for maximize */
if (!grid_signal.carried_out) {
if (slot.preview)
{
return;
slot.preview->set_target_geometry({input.x, input.y, 1, 1}, 0, true);
slot.preview = nullptr;
}

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);
if (slot.slot_id)
{
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))
{
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(slot.slot_id);
}

/* Returns the currently used input coordinates in global compositor space */
Expand Down Expand Up @@ -582,7 +534,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