mirror of
				https://github.com/swaywm/sway.git
				synced 2025-11-03 09:01:43 -05:00 
			
		
		
		
	Merge pull request #2420 from RyanDwyer/floating-move-to-workspace
Implement move to workspace on a floating container
This commit is contained in:
		
						commit
						c8a8216629
					
				
					 7 changed files with 100 additions and 34 deletions
				
			
		| 
						 | 
				
			
			@ -322,12 +322,23 @@ void container_get_box(struct sway_container *container, struct wlr_box *box);
 | 
			
		|||
void container_floating_translate(struct sway_container *con,
 | 
			
		||||
		double x_amount, double y_amount);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Choose an output for the floating container's new position.
 | 
			
		||||
 */
 | 
			
		||||
struct sway_container *container_floating_find_output(
 | 
			
		||||
		struct sway_container *con);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Move a floating container to a new layout-local position.
 | 
			
		||||
 */
 | 
			
		||||
void container_floating_move_to(struct sway_container *con,
 | 
			
		||||
		double lx, double ly);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Move a floating container to the center of the workspace.
 | 
			
		||||
 */
 | 
			
		||||
void container_floating_move_to_center(struct sway_container *con);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Mark a container as dirty if it isn't already. Dirty containers will be
 | 
			
		||||
 * included in the next transaction then unmarked as dirty.
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -16,6 +16,8 @@ struct sway_workspace {
 | 
			
		|||
 | 
			
		||||
extern char *prev_workspace_name;
 | 
			
		||||
 | 
			
		||||
struct sway_container *workspace_get_initial_output(const char *name);
 | 
			
		||||
 | 
			
		||||
char *workspace_next_name(const char *output_name);
 | 
			
		||||
 | 
			
		||||
bool workspace_switch(struct sway_container *workspace,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -100,7 +100,8 @@ static struct cmd_results *cmd_move_container(struct sway_container *current,
 | 
			
		|||
	// determine destination
 | 
			
		||||
	if (strcasecmp(argv[1], "workspace") == 0) {
 | 
			
		||||
		// move container to workspace x
 | 
			
		||||
		struct sway_container *ws;
 | 
			
		||||
		struct sway_container *ws = NULL;
 | 
			
		||||
		char *ws_name = NULL;
 | 
			
		||||
		if (strcasecmp(argv[2], "next") == 0 ||
 | 
			
		||||
				strcasecmp(argv[2], "prev") == 0 ||
 | 
			
		||||
				strcasecmp(argv[2], "next_on_output") == 0 ||
 | 
			
		||||
| 
						 | 
				
			
			@ -110,14 +111,13 @@ static struct cmd_results *cmd_move_container(struct sway_container *current,
 | 
			
		|||
		} else if (strcasecmp(argv[2], "back_and_forth") == 0) {
 | 
			
		||||
			if (!(ws = workspace_by_name(argv[2]))) {
 | 
			
		||||
				if (prev_workspace_name) {
 | 
			
		||||
					ws = workspace_create(NULL, prev_workspace_name);
 | 
			
		||||
					ws_name = strdup(prev_workspace_name);
 | 
			
		||||
				} else {
 | 
			
		||||
					return cmd_results_new(CMD_FAILURE, "move",
 | 
			
		||||
							"No workspace was previously active.");
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		} else {
 | 
			
		||||
			char *ws_name = NULL;
 | 
			
		||||
			if (strcasecmp(argv[2], "number") == 0) {
 | 
			
		||||
				// move "container to workspace number x"
 | 
			
		||||
				if (argc < 4) {
 | 
			
		||||
| 
						 | 
				
			
			@ -141,12 +141,26 @@ static struct cmd_results *cmd_move_container(struct sway_container *current,
 | 
			
		|||
					ws = workspace_by_name(ws_name);
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			if (!ws) {
 | 
			
		||||
				ws = workspace_create(NULL, ws_name);
 | 
			
		||||
			}
 | 
			
		||||
			free(ws_name);
 | 
			
		||||
		}
 | 
			
		||||
		if (!ws) {
 | 
			
		||||
			// We have to create the workspace, but if the container is
 | 
			
		||||
			// sticky and the workspace is going to be created on the same
 | 
			
		||||
			// output, we'll bail out first.
 | 
			
		||||
			if (container_is_floating(current) && current->is_sticky) {
 | 
			
		||||
				struct sway_container *old_output =
 | 
			
		||||
					container_parent(current, C_OUTPUT);
 | 
			
		||||
				struct sway_container *new_output =
 | 
			
		||||
					workspace_get_initial_output(ws_name);
 | 
			
		||||
				if (old_output == new_output) {
 | 
			
		||||
					free(ws_name);
 | 
			
		||||
					return cmd_results_new(CMD_FAILURE, "move",
 | 
			
		||||
							"Can't move sticky container to another workspace "
 | 
			
		||||
							"on the same output");
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
			ws = workspace_create(NULL, ws_name);
 | 
			
		||||
		}
 | 
			
		||||
		free(ws_name);
 | 
			
		||||
		destination = seat_get_focus_inactive(config->handler_context.seat, ws);
 | 
			
		||||
	} else if (strcasecmp(argv[1], "output") == 0) {
 | 
			
		||||
		struct sway_container *source = container_parent(current, C_OUTPUT);
 | 
			
		||||
| 
						 | 
				
			
			@ -173,6 +187,16 @@ static struct cmd_results *cmd_move_container(struct sway_container *current,
 | 
			
		|||
		return cmd_results_new(CMD_INVALID, "move", expected_syntax);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if (container_is_floating(current) && current->is_sticky) {
 | 
			
		||||
		struct sway_container *old_output = container_parent(current, C_OUTPUT);
 | 
			
		||||
		struct sway_container *new_output = destination->type == C_OUTPUT ?
 | 
			
		||||
			destination : container_parent(destination, C_OUTPUT);
 | 
			
		||||
		if (old_output == new_output) {
 | 
			
		||||
			return cmd_results_new(CMD_FAILURE, "move", "Can't move sticky "
 | 
			
		||||
					"container to another workspace on the same output");
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// move container, arrange windows and return focus
 | 
			
		||||
	container_move_to(current, destination);
 | 
			
		||||
	struct sway_container *focus =
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -717,12 +717,8 @@ void seat_set_focus_warp(struct sway_seat *seat,
 | 
			
		|||
 | 
			
		||||
	// If we've focused a floating container, bring it to the front.
 | 
			
		||||
	// We do this by putting it at the end of the floating list.
 | 
			
		||||
	// This must happen for both the pending and current children lists.
 | 
			
		||||
	if (container && container_is_floating(container)) {
 | 
			
		||||
		list_move_to_end(container->parent->children, container);
 | 
			
		||||
		if (container_has_ancestor(container, container->current.parent)) {
 | 
			
		||||
			list_move_to_end(container->parent->current.children, container);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// clean up unfocused empty workspace on new output
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1163,19 +1163,16 @@ void container_floating_translate(struct sway_container *con,
 | 
			
		|||
		double x_amount, double y_amount) {
 | 
			
		||||
	con->x += x_amount;
 | 
			
		||||
	con->y += y_amount;
 | 
			
		||||
	con->current.swayc_x += x_amount;
 | 
			
		||||
	con->current.swayc_y += y_amount;
 | 
			
		||||
	if (con->type == C_VIEW) {
 | 
			
		||||
		con->sway_view->x += x_amount;
 | 
			
		||||
		con->sway_view->y += y_amount;
 | 
			
		||||
		con->current.view_x += x_amount;
 | 
			
		||||
		con->current.view_y += y_amount;
 | 
			
		||||
	} else {
 | 
			
		||||
		for (int i = 0; i < con->children->length; ++i) {
 | 
			
		||||
			struct sway_container *child = con->children->items[i];
 | 
			
		||||
			container_floating_translate(child, x_amount, y_amount);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	container_set_dirty(con);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
| 
						 | 
				
			
			@ -1185,7 +1182,7 @@ void container_floating_translate(struct sway_container *con,
 | 
			
		|||
 * one, otherwise we'll choose whichever output is closest to the container's
 | 
			
		||||
 * center.
 | 
			
		||||
 */
 | 
			
		||||
static struct sway_container *container_floating_find_output(
 | 
			
		||||
struct sway_container *container_floating_find_output(
 | 
			
		||||
		struct sway_container *con) {
 | 
			
		||||
	double center_x = con->x + con->width / 2;
 | 
			
		||||
	double center_y = con->y + con->height / 2;
 | 
			
		||||
| 
						 | 
				
			
			@ -1219,9 +1216,7 @@ void container_floating_move_to(struct sway_container *con,
 | 
			
		|||
			"Expected a floating container")) {
 | 
			
		||||
		return;
 | 
			
		||||
	}
 | 
			
		||||
	desktop_damage_whole_container(con);
 | 
			
		||||
	container_floating_translate(con, lx - con->x, ly - con->y);
 | 
			
		||||
	desktop_damage_whole_container(con);
 | 
			
		||||
	struct sway_container *old_workspace = container_parent(con, C_WORKSPACE);
 | 
			
		||||
	struct sway_container *new_output = container_floating_find_output(con);
 | 
			
		||||
	if (!sway_assert(new_output, "Unable to find any output")) {
 | 
			
		||||
| 
						 | 
				
			
			@ -1239,6 +1234,17 @@ void container_floating_move_to(struct sway_container *con,
 | 
			
		|||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void container_floating_move_to_center(struct sway_container *con) {
 | 
			
		||||
	if (!sway_assert(container_is_floating(con),
 | 
			
		||||
			"Expected a floating container")) {
 | 
			
		||||
		return;
 | 
			
		||||
	}
 | 
			
		||||
	struct sway_container *ws = container_parent(con, C_WORKSPACE);
 | 
			
		||||
	double new_lx = ws->x + (ws->width - con->width) / 2;
 | 
			
		||||
	double new_ly = ws->y + (ws->height - con->height) / 2;
 | 
			
		||||
	container_floating_translate(con, new_lx - con->x, new_ly - con->y);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void container_set_dirty(struct sway_container *container) {
 | 
			
		||||
	if (container->dirty) {
 | 
			
		||||
		return;
 | 
			
		||||
| 
						 | 
				
			
			@ -1318,6 +1324,11 @@ void container_set_fullscreen(struct sway_container *container, bool enable) {
 | 
			
		|||
			container->y = container->saved_y;
 | 
			
		||||
			container->width = container->saved_width;
 | 
			
		||||
			container->height = container->saved_height;
 | 
			
		||||
			struct sway_container *output =
 | 
			
		||||
				container_floating_find_output(container);
 | 
			
		||||
			if (!container_has_ancestor(container, output)) {
 | 
			
		||||
				container_floating_move_to_center(container);
 | 
			
		||||
			}
 | 
			
		||||
		} else {
 | 
			
		||||
			container->width = container->saved_width;
 | 
			
		||||
			container->height = container->saved_height;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -150,22 +150,44 @@ void container_move_to(struct sway_container *container,
 | 
			
		|||
			|| container_has_ancestor(container, destination)) {
 | 
			
		||||
		return;
 | 
			
		||||
	}
 | 
			
		||||
	struct sway_container *old_parent = NULL;
 | 
			
		||||
	struct sway_container *new_parent = NULL;
 | 
			
		||||
	if (container_is_floating(container)) {
 | 
			
		||||
		// TODO
 | 
			
		||||
		return;
 | 
			
		||||
	}
 | 
			
		||||
	struct sway_container *old_parent = container_remove_child(container);
 | 
			
		||||
	container->width = container->height = 0;
 | 
			
		||||
	container->saved_width = container->saved_height = 0;
 | 
			
		||||
 | 
			
		||||
	struct sway_container *new_parent;
 | 
			
		||||
 | 
			
		||||
	if (destination->type == C_VIEW) {
 | 
			
		||||
		new_parent = container_add_sibling(destination, container);
 | 
			
		||||
		// Resolve destination into a workspace
 | 
			
		||||
		struct sway_container *new_ws = NULL;
 | 
			
		||||
		if (destination->type == C_OUTPUT) {
 | 
			
		||||
			new_ws = output_get_active_workspace(destination->sway_output);
 | 
			
		||||
		} else if (destination->type == C_WORKSPACE) {
 | 
			
		||||
			new_ws = destination;
 | 
			
		||||
		} else {
 | 
			
		||||
			new_ws = container_parent(destination, C_WORKSPACE);
 | 
			
		||||
		}
 | 
			
		||||
		if (!new_ws) {
 | 
			
		||||
			// This can happen if the user has run "move container to mark foo",
 | 
			
		||||
			// where mark foo is on a hidden scratchpad container.
 | 
			
		||||
			return;
 | 
			
		||||
		}
 | 
			
		||||
		struct sway_container *old_output =
 | 
			
		||||
			container_parent(container, C_OUTPUT);
 | 
			
		||||
		old_parent = container_remove_child(container);
 | 
			
		||||
		container_add_child(new_ws->sway_workspace->floating, container);
 | 
			
		||||
		// If changing output, center it within the workspace
 | 
			
		||||
		if (old_output != new_ws->parent && !container->is_fullscreen) {
 | 
			
		||||
			container_floating_move_to_center(container);
 | 
			
		||||
		}
 | 
			
		||||
	} else {
 | 
			
		||||
		new_parent = destination;
 | 
			
		||||
		container_add_child(destination, container);
 | 
			
		||||
		old_parent = container_remove_child(container);
 | 
			
		||||
		container->width = container->height = 0;
 | 
			
		||||
		container->saved_width = container->saved_height = 0;
 | 
			
		||||
 | 
			
		||||
		if (destination->type == C_VIEW) {
 | 
			
		||||
			new_parent = container_add_sibling(destination, container);
 | 
			
		||||
		} else {
 | 
			
		||||
			new_parent = destination;
 | 
			
		||||
			container_add_child(destination, container);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	wl_signal_emit(&container->events.reparent, old_parent);
 | 
			
		||||
 | 
			
		||||
	if (container->type == C_VIEW) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -18,7 +18,7 @@
 | 
			
		|||
#include "log.h"
 | 
			
		||||
#include "util.h"
 | 
			
		||||
 | 
			
		||||
static struct sway_container *get_workspace_initial_output(const char *name) {
 | 
			
		||||
struct sway_container *workspace_get_initial_output(const char *name) {
 | 
			
		||||
	struct sway_container *parent;
 | 
			
		||||
	// Search for workspace<->output pair
 | 
			
		||||
	int e = config->workspace_outputs->length;
 | 
			
		||||
| 
						 | 
				
			
			@ -48,7 +48,7 @@ static struct sway_container *get_workspace_initial_output(const char *name) {
 | 
			
		|||
struct sway_container *workspace_create(struct sway_container *output,
 | 
			
		||||
		const char *name) {
 | 
			
		||||
	if (output == NULL) {
 | 
			
		||||
		output = get_workspace_initial_output(name);
 | 
			
		||||
		output = workspace_get_initial_output(name);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	wlr_log(WLR_DEBUG, "Added workspace %s for output %s", name, output->name);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue