diff options
Diffstat (limited to 'sway/tree')
-rw-r--r-- | sway/tree/arrange.c | 64 | ||||
-rw-r--r-- | sway/tree/container.c | 336 | ||||
-rw-r--r-- | sway/tree/layout.c | 11 | ||||
-rw-r--r-- | sway/tree/view.c | 86 |
4 files changed, 402 insertions, 95 deletions
diff --git a/sway/tree/arrange.c b/sway/tree/arrange.c index 83bb20fb..37f4a066 100644 --- a/sway/tree/arrange.c +++ b/sway/tree/arrange.c @@ -86,6 +86,15 @@ static void apply_horiz_layout(struct sway_container *parent) { if (!num_children) { return; } + size_t parent_offset = 0; + if (parent->parent->layout == L_TABBED) { + parent_offset = container_titlebar_height(); + } else if (parent->parent->layout == L_STACKED) { + parent_offset = + container_titlebar_height() * parent->parent->children->length; + } + size_t parent_height = parent->height - parent_offset; + // Calculate total width of children double total_width = 0; for (size_t i = 0; i < num_children; ++i) { @@ -111,9 +120,9 @@ static void apply_horiz_layout(struct sway_container *parent) { "Calculating arrangement for %p:%d (will scale %f by %f)", child, child->type, child->width, scale); child->x = child_x; - child->y = parent->y; + child->y = parent->y + parent_offset; child->width = floor(child->width * scale); - child->height = parent->height; + child->height = parent_height; child_x += child->width; } // Make last child use remaining width of parent @@ -125,24 +134,33 @@ static void apply_vert_layout(struct sway_container *parent) { if (!num_children) { return; } + size_t parent_offset = 0; + if (parent->parent->layout == L_TABBED) { + parent_offset = container_titlebar_height(); + } else if (parent->parent->layout == L_STACKED) { + parent_offset = + container_titlebar_height() * parent->parent->children->length; + } + size_t parent_height = parent->height - parent_offset; + // Calculate total height of children double total_height = 0; for (size_t i = 0; i < num_children; ++i) { struct sway_container *child = parent->children->items[i]; if (child->height <= 0) { if (num_children > 1) { - child->height = parent->height / (num_children - 1); + child->height = parent_height / (num_children - 1); } else { - child->height = parent->height; + child->height = parent_height; } } total_height += child->height; } - double scale = parent->height / total_height; + double scale = parent_height / total_height; // Resize wlr_log(L_DEBUG, "Arranging %p vertically", parent); - double child_y = parent->y; + double child_y = parent->y + parent_offset; struct sway_container *child; for (size_t i = 0; i < num_children; ++i) { child = parent->children->items[i]; @@ -156,7 +174,33 @@ static void apply_vert_layout(struct sway_container *parent) { child_y += child->height; } // Make last child use remaining height of parent - child->height = parent->y + parent->height - child->y; + child->height = parent->y + parent_offset + parent_height - child->y; +} + +static void apply_tabbed_layout(struct sway_container *parent) { + if (!parent->children->length) { + return; + } + for (int i = 0; i < parent->children->length; ++i) { + struct sway_container *child = parent->children->items[i]; + child->x = parent->x; + child->y = parent->y; + child->width = parent->width; + child->height = parent->height; + } +} + +static void apply_stacked_layout(struct sway_container *parent) { + if (!parent->children->length) { + return; + } + for (int i = 0; i < parent->children->length; ++i) { + struct sway_container *child = parent->children->items[i]; + child->x = parent->x; + child->y = parent->y; + child->width = parent->width; + child->height = parent->height; + } } void arrange_children_of(struct sway_container *parent) { @@ -189,6 +233,12 @@ void arrange_children_of(struct sway_container *parent) { case L_VERT: apply_vert_layout(parent); break; + case L_TABBED: + apply_tabbed_layout(parent); + break; + case L_STACKED: + apply_stacked_layout(parent); + break; default: wlr_log(L_DEBUG, "TODO: arrange layout type %d", parent->layout); apply_horiz_layout(parent); diff --git a/sway/tree/container.c b/sway/tree/container.c index e47338e7..9cf18f61 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -73,6 +73,44 @@ static void container_close_notify(struct sway_container *container) { } } +static void container_update_textures_recursive(struct sway_container *con) { + container_update_title_textures(con); + + if (con->type == C_VIEW) { + view_update_marks_textures(con->sway_view); + } else { + for (int i = 0; i < con->children->length; ++i) { + struct sway_container *child = con->children->items[i]; + container_update_textures_recursive(child); + } + } +} + +static void handle_reparent(struct wl_listener *listener, + void *data) { + struct sway_container *container = + wl_container_of(listener, container, reparent); + struct sway_container *old_parent = data; + + struct sway_container *old_output = old_parent; + if (old_output != NULL && old_output->type != C_OUTPUT) { + old_output = container_parent(old_output, C_OUTPUT); + } + + struct sway_container *new_output = container->parent; + if (new_output != NULL && new_output->type != C_OUTPUT) { + new_output = container_parent(new_output, C_OUTPUT); + } + + if (old_output && new_output) { + float old_scale = old_output->sway_output->wlr_output->scale; + float new_scale = new_output->sway_output->wlr_output->scale; + if (old_scale != new_scale) { + container_update_textures_recursive(container); + } + } +} + struct sway_container *container_create(enum sway_container_type type) { // next id starts at 1 because 0 is assigned to root_container in layout.c static size_t next_id = 1; @@ -92,6 +130,9 @@ struct sway_container *container_create(enum sway_container_type type) { wl_signal_init(&c->events.destroy); wl_signal_init(&c->events.reparent); + wl_signal_add(&c->events.reparent, &c->reparent); + c->reparent.notify = handle_reparent; + return c; } @@ -411,80 +452,154 @@ struct sway_container *container_parent(struct sway_container *container, return container; } -struct sway_container *container_at(struct sway_container *parent, - double lx, double ly, +static struct sway_container *container_at_view(struct sway_container *swayc, + double ox, double oy, struct wlr_surface **surface, double *sx, double *sy) { - list_t *queue = get_bfs_queue(); - if (!queue) { + if (!sway_assert(swayc->type == C_VIEW, "Expected a view")) { return NULL; } + struct sway_view *sview = swayc->sway_view; + double view_sx = ox - sview->x; + double view_sy = oy - sview->y; - list_add(queue, parent); + double _sx, _sy; + struct wlr_surface *_surface = NULL; + switch (sview->type) { + case SWAY_VIEW_XWAYLAND: + _surface = wlr_surface_surface_at(sview->surface, + view_sx, view_sy, &_sx, &_sy); + break; + case SWAY_VIEW_XDG_SHELL_V6: + // the top left corner of the sway container is the + // coordinate of the top left corner of the window geometry + view_sx += sview->wlr_xdg_surface_v6->geometry.x; + view_sy += sview->wlr_xdg_surface_v6->geometry.y; + + _surface = wlr_xdg_surface_v6_surface_at( + sview->wlr_xdg_surface_v6, + view_sx, view_sy, &_sx, &_sy); + break; + case SWAY_VIEW_XDG_SHELL: + // the top left corner of the sway container is the + // coordinate of the top left corner of the window geometry + view_sx += sview->wlr_xdg_surface->geometry.x; + view_sy += sview->wlr_xdg_surface->geometry.y; + + _surface = wlr_xdg_surface_surface_at( + sview->wlr_xdg_surface, + view_sx, view_sy, &_sx, &_sy); + break; + } + if (_surface) { + *sx = _sx; + *sy = _sy; + *surface = _surface; + } + return swayc; +} - struct sway_container *swayc = NULL; - while (queue->length) { - swayc = queue->items[0]; - list_del(queue, 0); - if (swayc->type == C_VIEW) { - struct sway_view *sview = swayc->sway_view; - struct sway_container *soutput = container_parent(swayc, C_OUTPUT); - struct wlr_box *output_box = - wlr_output_layout_get_box( - root_container.sway_root->output_layout, - soutput->sway_output->wlr_output); - double ox = lx - output_box->x; - double oy = ly - output_box->y; - double view_sx = ox - sview->x; - double view_sy = oy - sview->y; - - double _sx, _sy; - struct wlr_surface *_surface; - switch (sview->type) { - case SWAY_VIEW_XWAYLAND: - _surface = wlr_surface_surface_at(sview->surface, - view_sx, view_sy, &_sx, &_sy); - break; - case SWAY_VIEW_XDG_SHELL_V6: - // the top left corner of the sway container is the - // coordinate of the top left corner of the window geometry - view_sx += sview->wlr_xdg_surface_v6->geometry.x; - view_sy += sview->wlr_xdg_surface_v6->geometry.y; - - _surface = wlr_xdg_surface_v6_surface_at( - sview->wlr_xdg_surface_v6, - view_sx, view_sy, &_sx, &_sy); - break; - case SWAY_VIEW_XDG_SHELL: - // the top left corner of the sway container is the - // coordinate of the top left corner of the window geometry - view_sx += sview->wlr_xdg_surface->geometry.x; - view_sy += sview->wlr_xdg_surface->geometry.y; - - _surface = wlr_xdg_surface_surface_at( - sview->wlr_xdg_surface, - view_sx, view_sy, &_sx, &_sy); - break; - } - if (_surface) { - *sx = _sx; - *sy = _sy; - *surface = _surface; - return swayc; - } - // Check the view's decorations - struct wlr_box swayc_box = { - .x = swayc->x, - .y = swayc->y, - .width = swayc->width, - .height = swayc->height, - }; - if (wlr_box_contains_point(&swayc_box, ox, oy)) { - return swayc; - } - } else { - list_cat(queue, swayc->children); +/** + * container_at for a container with layout L_TABBED. + */ +static struct sway_container *container_at_tabbed(struct sway_container *parent, + double ox, double oy, + struct wlr_surface **surface, double *sx, double *sy) { + if (oy < parent->y || oy > parent->y + parent->height) { + return NULL; + } + struct sway_seat *seat = input_manager_current_seat(input_manager); + + // Tab titles + int title_height = container_titlebar_height(); + if (oy < parent->y + title_height) { + int tab_width = parent->width / parent->children->length; + int child_index = (ox - parent->x) / tab_width; + if (child_index >= parent->children->length) { + child_index = parent->children->length - 1; + } + struct sway_container *child = parent->children->items[child_index]; + return seat_get_focus_inactive(seat, child); + } + + // Surfaces + struct sway_container *current = seat_get_active_child(seat, parent); + + return container_at(current, ox, oy, surface, sx, sy); +} + +/** + * container_at for a container with layout L_STACKED. + */ +static struct sway_container *container_at_stacked( + struct sway_container *parent, double ox, double oy, + struct wlr_surface **surface, double *sx, double *sy) { + if (oy < parent->y || oy > parent->y + parent->height) { + return NULL; + } + struct sway_seat *seat = input_manager_current_seat(input_manager); + + // Title bars + int title_height = container_titlebar_height(); + int child_index = (oy - parent->y) / title_height; + if (child_index < parent->children->length) { + struct sway_container *child = parent->children->items[child_index]; + return seat_get_focus_inactive(seat, child); + } + + // Surfaces + struct sway_container *current = seat_get_active_child(seat, parent); + + return container_at(current, ox, oy, surface, sx, sy); +} + +/** + * container_at for a container with layout L_HORIZ or L_VERT. + */ +static struct sway_container *container_at_linear(struct sway_container *parent, + double ox, double oy, + struct wlr_surface **surface, double *sx, double *sy) { + for (int i = 0; i < parent->children->length; ++i) { + struct sway_container *child = parent->children->items[i]; + struct wlr_box box = { + .x = child->x, + .y = child->y, + .width = child->width, + .height = child->height, + }; + if (wlr_box_contains_point(&box, ox, oy)) { + return container_at(child, ox, oy, surface, sx, sy); } } + return NULL; +} + +struct sway_container *container_at(struct sway_container *parent, + double ox, double oy, + struct wlr_surface **surface, double *sx, double *sy) { + if (!sway_assert(parent->type >= C_WORKSPACE, + "Expected workspace or deeper")) { + return NULL; + } + if (parent->type == C_VIEW) { + return container_at_view(parent, ox, oy, surface, sx, sy); + } + if (!parent->children->length) { + return NULL; + } + + switch (parent->layout) { + case L_HORIZ: + case L_VERT: + return container_at_linear(parent, ox, oy, surface, sx, sy); + case L_TABBED: + return container_at_tabbed(parent, ox, oy, surface, sx, sy); + case L_STACKED: + return container_at_stacked(parent, ox, oy, surface, sx, sy); + case L_FLOATING: + return NULL; // TODO + case L_NONE: + return NULL; + } return NULL; } @@ -658,19 +773,96 @@ void container_calculate_title_height(struct sway_container *container) { container->title_height = height; } +/** + * Calculate and return the length of the concatenated child titles. + * An example concatenated title is: V[Terminal, Firefox] + * If buffer is not NULL, also populate the buffer with the concatenated title. + */ +static size_t concatenate_child_titles(struct sway_container *parent, + char *buffer) { + size_t len = 2; // V[ + if (buffer) { + switch (parent->layout) { + case L_VERT: + strcpy(buffer, "V["); + break; + case L_HORIZ: + strcpy(buffer, "H["); + break; + case L_TABBED: + strcpy(buffer, "T["); + break; + case L_STACKED: + strcpy(buffer, "S["); + break; + case L_FLOATING: + strcpy(buffer, "F["); + break; + case L_NONE: + strcpy(buffer, "D["); + break; + } + } + + for (int i = 0; i < parent->children->length; ++i) { + if (i != 0) { + len += 1; + if (buffer) { + strcat(buffer, " "); + } + } + struct sway_container *child = parent->children->items[i]; + const char *identifier = NULL; + if (child->type == C_VIEW) { + identifier = view_get_class(child->sway_view); + if (!identifier) { + identifier = view_get_app_id(child->sway_view); + } + } else { + identifier = child->name; + } + if (identifier) { + len += strlen(identifier); + if (buffer) { + strcat(buffer, identifier); + } + } else { + len += 6; + if (buffer) { + strcat(buffer, "(null)"); + } + } + } + + len += 1; + if (buffer) { + strcat(buffer, "]"); + } + return len; +} + void container_notify_child_title_changed(struct sway_container *container) { if (!container || container->type != C_CONTAINER) { return; } - if (container->layout != L_TABBED && container->layout != L_STACKED) { - return; - } if (container->formatted_title) { free(container->formatted_title); } - // TODO: iterate children and concatenate their titles - container->formatted_title = strdup(""); + + size_t len = concatenate_child_titles(container, NULL); + char *buffer = calloc(len + 1, sizeof(char)); + if (!sway_assert(buffer, "Unable to allocate title string")) { + return; + } + concatenate_child_titles(container, buffer); + + container->name = buffer; + container->formatted_title = buffer; container_calculate_title_height(container); container_update_title_textures(container); container_notify_child_title_changed(container->parent); } + +size_t container_titlebar_height() { + return config->font_height + TITLEBAR_V_PADDING * 2; +} diff --git a/sway/tree/layout.c b/sway/tree/layout.c index ec1c6fe5..f8acdf6c 100644 --- a/sway/tree/layout.c +++ b/sway/tree/layout.c @@ -149,6 +149,8 @@ struct sway_container *container_remove_child(struct sway_container *child) { } } child->parent = NULL; + container_notify_child_title_changed(parent); + return parent; } @@ -182,6 +184,8 @@ void container_move_to(struct sway_container *container, container_sort_workspaces(new_parent); seat_set_focus(seat, new_parent); } + container_notify_child_title_changed(old_parent); + container_notify_child_title_changed(new_parent); if (old_parent) { arrange_children_of(old_parent); } @@ -234,9 +238,9 @@ static bool is_parallel(enum sway_container_layout layout, enum movement_direction dir) { switch (layout) { case L_TABBED: - case L_STACKED: case L_HORIZ: return dir == MOVE_LEFT || dir == MOVE_RIGHT; + case L_STACKED: case L_VERT: return dir == MOVE_UP || dir == MOVE_DOWN; default: @@ -485,6 +489,9 @@ void container_move(struct sway_container *container, } } + container_notify_child_title_changed(old_parent); + container_notify_child_title_changed(container->parent); + if (old_parent) { seat_set_focus(config->handler_context.seat, old_parent); seat_set_focus(config->handler_context.seat, container); @@ -832,6 +839,8 @@ struct sway_container *container_split(struct sway_container *child, container_add_child(cont, child); } + container_notify_child_title_changed(cont); + return cont; } diff --git a/sway/tree/view.c b/sway/tree/view.c index 648c1655..07157818 100644 --- a/sway/tree/view.c +++ b/sway/tree/view.c @@ -139,10 +139,20 @@ void view_autoconfigure(struct sway_view *view) { return; } - int other_views = 1; + struct sway_container *ws = container_parent(view->swayc, C_WORKSPACE); + + int other_views = 0; if (config->hide_edge_borders == E_SMART) { - struct sway_container *ws = container_parent(view->swayc, C_WORKSPACE); - other_views = container_count_descendants_of_type(ws, C_VIEW) - 1; + struct sway_container *con = view->swayc; + while (con != output) { + if (con->layout != L_TABBED && con->layout != L_STACKED) { + other_views += con->children ? con->children->length - 1 : 0; + if (other_views > 0) { + break; + } + } + con = con->parent; + } } view->border_top = view->border_bottom = true; @@ -151,47 +161,67 @@ void view_autoconfigure(struct sway_view *view) { if (config->hide_edge_borders == E_BOTH || config->hide_edge_borders == E_VERTICAL || (config->hide_edge_borders == E_SMART && !other_views)) { - view->border_left = view->swayc->x != 0; + view->border_left = view->swayc->x != ws->x; int right_x = view->swayc->x + view->swayc->width; - view->border_right = right_x != output->width; + view->border_right = right_x != ws->x + ws->width; } if (config->hide_edge_borders == E_BOTH || config->hide_edge_borders == E_HORIZONTAL || (config->hide_edge_borders == E_SMART && !other_views)) { - view->border_top = view->swayc->y != 0; + view->border_top = view->swayc->y != ws->y; int bottom_y = view->swayc->y + view->swayc->height; - view->border_bottom = bottom_y != output->height; + view->border_bottom = bottom_y != ws->y + ws->height; } } double x, y, width, height; x = y = width = height = 0; + double y_offset = 0; + + // In a tabbed or stacked container, the swayc's y is the top of the title + // area. We have to offset the surface y by the height of the title bar, and + // disable any top border because we'll always have the title bar. + if (view->swayc->parent->layout == L_TABBED) { + y_offset = container_titlebar_height(); + view->border_top = 0; + } else if (view->swayc->parent->layout == L_STACKED) { + y_offset = container_titlebar_height() + * view->swayc->parent->children->length; + view->border_top = 0; + } + switch (view->border) { case B_NONE: x = view->swayc->x; - y = view->swayc->y; + y = view->swayc->y + y_offset; width = view->swayc->width; - height = view->swayc->height; + height = view->swayc->height - y_offset; break; case B_PIXEL: x = view->swayc->x + view->border_thickness * view->border_left; - y = view->swayc->y + view->border_thickness * view->border_top; + y = view->swayc->y + view->border_thickness * view->border_top + y_offset; width = view->swayc->width - view->border_thickness * view->border_left - view->border_thickness * view->border_right; - height = view->swayc->height + height = view->swayc->height - y_offset - view->border_thickness * view->border_top - view->border_thickness * view->border_bottom; break; case B_NORMAL: - // Height is: border + title height + border + view height + border + // Height is: 1px border + 3px pad + title height + 3px pad + 1px border x = view->swayc->x + view->border_thickness * view->border_left; - y = view->swayc->y + config->font_height + view->border_thickness * 2; width = view->swayc->width - view->border_thickness * view->border_left - view->border_thickness * view->border_right; - height = view->swayc->height - config->font_height - - view->border_thickness * (2 + view->border_bottom); + if (y_offset) { + y = view->swayc->y + y_offset; + height = view->swayc->height - y_offset + - view->border_thickness * view->border_bottom; + } else { + y = view->swayc->y + container_titlebar_height(); + height = view->swayc->height - container_titlebar_height() + - view->border_thickness * view->border_bottom; + } break; } @@ -440,6 +470,7 @@ void view_map(struct sway_view *view, struct wlr_surface *wlr_surface) { input_manager_set_focus(input_manager, cont); view_update_title(view, false); + container_notify_child_title_changed(view->swayc->parent); view_execute_criteria(view); container_damage_whole(cont); @@ -863,3 +894,28 @@ void view_update_marks_textures(struct sway_view *view) { &config->border_colors.urgent); container_damage_whole(view->swayc); } + +bool view_is_visible(struct sway_view *view) { + if (!view->swayc) { + return false; + } + // Check view isn't in a tabbed or stacked container on an inactive tab + struct sway_seat *seat = input_manager_current_seat(input_manager); + struct sway_container *container = view->swayc; + while (container->type != C_WORKSPACE) { + if (container->parent->layout == L_TABBED || + container->parent->layout == L_STACKED) { + if (seat_get_active_child(seat, container->parent) != container) { + return false; + } + } + container = container->parent; + } + // Check view isn't hidden by another fullscreen view + struct sway_container *workspace = container; + if (workspace->sway_workspace->fullscreen && !view->is_fullscreen) { + return false; + } + // Check the workspace is visible + return workspace_is_visible(workspace); +} |