mirror of
				https://github.com/swaywm/sway.git
				synced 2025-11-03 09:01:43 -05:00 
			
		
		
		
	change reap container approach
This commit is contained in:
		
							parent
							
								
									2c165e1288
								
							
						
					
					
						commit
						2992b72d61
					
				
					 5 changed files with 63 additions and 38 deletions
				
			
		| 
						 | 
					@ -128,11 +128,11 @@ struct sway_container *container_view_create(
 | 
				
			||||||
		struct sway_container *sibling, struct sway_view *sway_view);
 | 
							struct sway_container *sibling, struct sway_view *sway_view);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// TODO don't return the parent on destroy
 | 
					// TODO don't return the parent on destroy
 | 
				
			||||||
struct sway_container *container_destroy(struct sway_container *container);
 | 
					void container_destroy(struct sway_container *container);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct sway_container *container_workspace_destroy(struct sway_container *container);
 | 
					struct sway_container *container_workspace_destroy(struct sway_container *container);
 | 
				
			||||||
struct sway_container *container_output_destroy(struct sway_container *container);
 | 
					struct sway_container *container_output_destroy(struct sway_container *container);
 | 
				
			||||||
struct sway_container *container_view_destroy(struct sway_container *container);
 | 
					void container_view_destroy(struct sway_container *container);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct sway_container *container_close(struct sway_container *container);
 | 
					struct sway_container *container_close(struct sway_container *container);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -41,7 +41,9 @@ struct sway_container *container_add_sibling(struct sway_container *parent,
 | 
				
			||||||
struct sway_container *container_remove_child(struct sway_container *child);
 | 
					struct sway_container *container_remove_child(struct sway_container *child);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// TODO PRIVATE in tree.h
 | 
					// TODO PRIVATE in tree.h
 | 
				
			||||||
struct sway_container *container_reap_empty(struct sway_container *container);
 | 
					
 | 
				
			||||||
 | 
					struct sway_container *container_replace_child(struct sway_container *child,
 | 
				
			||||||
 | 
							struct sway_container *new_child);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// TODO move to tree.h
 | 
					// TODO move to tree.h
 | 
				
			||||||
void container_move_to(struct sway_container* container,
 | 
					void container_move_to(struct sway_container* container,
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,4 +1,5 @@
 | 
				
			||||||
#define _POSIX_C_SOURCE 200809L
 | 
					#define _POSIX_C_SOURCE 200809L
 | 
				
			||||||
 | 
					#include <assert.h>
 | 
				
			||||||
#include <stdint.h>
 | 
					#include <stdint.h>
 | 
				
			||||||
#include <stdlib.h>
 | 
					#include <stdlib.h>
 | 
				
			||||||
#include <string.h>
 | 
					#include <string.h>
 | 
				
			||||||
| 
						 | 
					@ -109,12 +110,55 @@ static struct sway_container *_container_destroy(struct sway_container *cont) {
 | 
				
			||||||
	return parent;
 | 
						return parent;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct sway_container *container_destroy(struct sway_container *cont) {
 | 
					static void reap_empty_func(struct sway_container *con, void *data) {
 | 
				
			||||||
	struct sway_container *parent = _container_destroy(cont);
 | 
						switch (con->type) {
 | 
				
			||||||
	parent = container_reap_empty(parent);
 | 
							case C_TYPES:
 | 
				
			||||||
 | 
							case C_ROOT:
 | 
				
			||||||
 | 
							case C_OUTPUT:
 | 
				
			||||||
 | 
								// dont reap these
 | 
				
			||||||
 | 
								break;
 | 
				
			||||||
 | 
							case C_WORKSPACE:
 | 
				
			||||||
 | 
								if (!workspace_is_visible(con) && con->children->length == 0) {
 | 
				
			||||||
 | 
									container_workspace_destroy(con);
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								break;
 | 
				
			||||||
 | 
							case C_CONTAINER:
 | 
				
			||||||
 | 
								if (con->children->length == 0) {
 | 
				
			||||||
 | 
									_container_destroy(con);
 | 
				
			||||||
 | 
								} else if (con->children->length == 1) {
 | 
				
			||||||
 | 
									struct sway_container *only_child = con->children->items[0];
 | 
				
			||||||
 | 
									if (only_child->type == C_CONTAINER) {
 | 
				
			||||||
 | 
										container_remove_child(only_child);
 | 
				
			||||||
 | 
										container_replace_child(con, only_child);
 | 
				
			||||||
 | 
										_container_destroy(con);
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							case C_VIEW:
 | 
				
			||||||
 | 
								break;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					struct sway_container *container_reap_empty(struct sway_container *container) {
 | 
				
			||||||
 | 
						struct sway_container *parent = container->parent;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						container_for_each_descendant_dfs(container, reap_empty_func, NULL);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return parent;
 | 
						return parent;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void container_destroy(struct sway_container *cont) {
 | 
				
			||||||
 | 
						if (cont == NULL) {
 | 
				
			||||||
 | 
							return;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (cont->children != NULL && cont->children->length) {
 | 
				
			||||||
 | 
							assert(false && "dont destroy containers with children");
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						_container_destroy(cont);
 | 
				
			||||||
 | 
						container_reap_empty(&root_container);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static void container_close_func(struct sway_container *container, void *data) {
 | 
					static void container_close_func(struct sway_container *container, void *data) {
 | 
				
			||||||
	if (container->type == C_VIEW) {
 | 
						if (container->type == C_VIEW) {
 | 
				
			||||||
		view_close(container->sway_view);
 | 
							view_close(container->sway_view);
 | 
				
			||||||
| 
						 | 
					@ -126,6 +170,8 @@ struct sway_container *container_close(struct sway_container *con) {
 | 
				
			||||||
		return NULL;
 | 
							return NULL;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						struct sway_container *parent = con->parent;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	switch (con->type) {
 | 
						switch (con->type) {
 | 
				
			||||||
		case C_TYPES:
 | 
							case C_TYPES:
 | 
				
			||||||
			wlr_log(L_ERROR, "tried to close an invalid container");
 | 
								wlr_log(L_ERROR, "tried to close an invalid container");
 | 
				
			||||||
| 
						 | 
					@ -148,7 +194,7 @@ struct sway_container *container_close(struct sway_container *con) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return con->parent;
 | 
						return parent;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct sway_container *container_output_create(
 | 
					struct sway_container *container_output_create(
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -103,32 +103,6 @@ void container_add_child(struct sway_container *parent,
 | 
				
			||||||
	child->parent = parent;
 | 
						child->parent = parent;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct sway_container *container_reap_empty(struct sway_container *container) {
 | 
					 | 
				
			||||||
	if (container == NULL) {
 | 
					 | 
				
			||||||
		return NULL;
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	wlr_log(L_DEBUG, "Reaping %p %s '%s'", container,
 | 
					 | 
				
			||||||
			container_type_to_str(container->type), container->name);
 | 
					 | 
				
			||||||
	while (container->type != C_ROOT && container->type != C_OUTPUT
 | 
					 | 
				
			||||||
			&& container->children && container->children->length == 0) {
 | 
					 | 
				
			||||||
		if (container->type == C_WORKSPACE) {
 | 
					 | 
				
			||||||
			if (!workspace_is_visible(container)) {
 | 
					 | 
				
			||||||
				struct sway_container *parent = container->parent;
 | 
					 | 
				
			||||||
				container_workspace_destroy(container);
 | 
					 | 
				
			||||||
				return parent;
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
			return container;
 | 
					 | 
				
			||||||
		} else if (container->type == C_CONTAINER) {
 | 
					 | 
				
			||||||
			struct sway_container *parent = container->parent;
 | 
					 | 
				
			||||||
			container_destroy(container);
 | 
					 | 
				
			||||||
			container = parent;
 | 
					 | 
				
			||||||
		} else {
 | 
					 | 
				
			||||||
			container = container->parent;
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return container;
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
struct sway_container *container_remove_child(struct sway_container *child) {
 | 
					struct sway_container *container_remove_child(struct sway_container *child) {
 | 
				
			||||||
	struct sway_container *parent = child->parent;
 | 
						struct sway_container *parent = child->parent;
 | 
				
			||||||
	for (int i = 0; i < parent->children->length; ++i) {
 | 
						for (int i = 0; i < parent->children->length; ++i) {
 | 
				
			||||||
| 
						 | 
					@ -309,6 +283,8 @@ void arrange_windows(struct sway_container *container,
 | 
				
			||||||
			container->children->length);
 | 
								container->children->length);
 | 
				
			||||||
		break;
 | 
							break;
 | 
				
			||||||
	case L_VERT:
 | 
						case L_VERT:
 | 
				
			||||||
 | 
							assert(container);
 | 
				
			||||||
 | 
							assert(container->children);
 | 
				
			||||||
		apply_vert_layout(container, x, y, width, height, 0,
 | 
							apply_vert_layout(container, x, y, width, height, 0,
 | 
				
			||||||
			container->children->length);
 | 
								container->children->length);
 | 
				
			||||||
		break;
 | 
							break;
 | 
				
			||||||
| 
						 | 
					@ -381,6 +357,7 @@ void apply_vert_layout(struct sway_container *container,
 | 
				
			||||||
		const double x, const double y,
 | 
							const double x, const double y,
 | 
				
			||||||
		const double width, const double height, const int start,
 | 
							const double width, const double height, const int start,
 | 
				
			||||||
		const int end) {
 | 
							const int end) {
 | 
				
			||||||
 | 
						assert(container);
 | 
				
			||||||
	int i;
 | 
						int i;
 | 
				
			||||||
	double scale = 0;
 | 
						double scale = 0;
 | 
				
			||||||
	// Calculate total height
 | 
						// Calculate total height
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -78,14 +78,12 @@ void view_close(struct sway_view *view) {
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct sway_container *container_view_destroy(struct sway_container *view) {
 | 
					void container_view_destroy(struct sway_container *view) {
 | 
				
			||||||
	if (!view) {
 | 
						if (!view) {
 | 
				
			||||||
		return NULL;
 | 
							return;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	wlr_log(L_DEBUG, "Destroying view '%s'", view->name);
 | 
						wlr_log(L_DEBUG, "Destroying view '%s'", view->name);
 | 
				
			||||||
	struct sway_container *parent = container_destroy(view);
 | 
						container_destroy(view);
 | 
				
			||||||
	arrange_windows(&root_container, -1, -1);
 | 
					 | 
				
			||||||
	return parent;
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void view_damage_whole(struct sway_view *view) {
 | 
					void view_damage_whole(struct sway_view *view) {
 | 
				
			||||||
| 
						 | 
					@ -164,6 +162,8 @@ void view_unmap(struct sway_view *view) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	view->swayc = NULL;
 | 
						view->swayc = NULL;
 | 
				
			||||||
	view->surface = NULL;
 | 
						view->surface = NULL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						arrange_windows(&root_container, -1, -1);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void view_update_position(struct sway_view *view, double ox, double oy) {
 | 
					void view_update_position(struct sway_view *view, double ox, double oy) {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue