Move view marks properties to container struct

Like border properties, this will be needed to implement layout saving
and restoring.
This commit is contained in:
Ryan Dwyer 2018-10-31 21:27:38 +10:00
parent 480b03b734
commit 9fc736f4e1
16 changed files with 236 additions and 261 deletions

View file

@ -39,6 +39,7 @@ struct sway_container *container_create(struct sway_view *view) {
c->children = create_list();
c->current.children = create_list();
}
c->marks = create_list();
c->outputs = create_list();
wl_signal_init(&c->events.destroy);
@ -66,6 +67,13 @@ void container_destroy(struct sway_container *con) {
list_free(con->current.children);
list_free(con->outputs);
list_foreach(con->marks, free);
list_free(con->marks);
wlr_texture_destroy(con->marks_focused);
wlr_texture_destroy(con->marks_focused_inactive);
wlr_texture_destroy(con->marks_unfocused);
wlr_texture_destroy(con->marks_urgent);
if (con->view) {
if (con->view->container == con) {
con->view->container = NULL;
@ -996,9 +1004,7 @@ void container_discover_outputs(struct sway_container *con) {
double new_scale = new_output ? new_output->wlr_output->scale : -1;
if (old_scale != new_scale) {
container_update_title_textures(con);
if (con->view) {
view_update_marks_textures(con->view);
}
container_update_marks_textures(con);
}
}
@ -1218,3 +1224,142 @@ bool container_is_transient_for(struct sway_container *child,
child->view && ancestor->view &&
view_is_transient_for(child->view, ancestor->view);
}
static bool find_by_mark_iterator(struct sway_container *con, void *data) {
char *mark = data;
return container_has_mark(con, mark);
}
struct sway_container *container_find_mark(char *mark) {
return root_find_container(find_by_mark_iterator, mark);
}
bool container_find_and_unmark(char *mark) {
struct sway_container *con = root_find_container(
find_by_mark_iterator, mark);
if (!con) {
return false;
}
for (int i = 0; i < con->marks->length; ++i) {
char *con_mark = con->marks->items[i];
if (strcmp(con_mark, mark) == 0) {
free(con_mark);
list_del(con->marks, i);
container_update_marks_textures(con);
ipc_event_window(con, "mark");
return true;
}
}
return false;
}
void container_clear_marks(struct sway_container *con) {
list_foreach(con->marks, free);
con->marks->length = 0;
ipc_event_window(con, "mark");
}
bool container_has_mark(struct sway_container *con, char *mark) {
for (int i = 0; i < con->marks->length; ++i) {
char *item = con->marks->items[i];
if (strcmp(item, mark) == 0) {
return true;
}
}
return false;
}
void container_add_mark(struct sway_container *con, char *mark) {
list_add(con->marks, strdup(mark));
ipc_event_window(con, "mark");
}
static void update_marks_texture(struct sway_container *con,
struct wlr_texture **texture, struct border_colors *class) {
struct sway_output *output = container_get_effective_output(con);
if (!output) {
return;
}
if (*texture) {
wlr_texture_destroy(*texture);
*texture = NULL;
}
if (!con->marks->length) {
return;
}
size_t len = 0;
for (int i = 0; i < con->marks->length; ++i) {
char *mark = con->marks->items[i];
if (mark[0] != '_') {
len += strlen(mark) + 2;
}
}
char *buffer = calloc(len + 1, 1);
char *part = malloc(len + 1);
if (!sway_assert(buffer && part, "Unable to allocate memory")) {
free(buffer);
return;
}
for (int i = 0; i < con->marks->length; ++i) {
char *mark = con->marks->items[i];
if (mark[0] != '_') {
sprintf(part, "[%s]", mark);
strcat(buffer, part);
}
}
free(part);
double scale = output->wlr_output->scale;
int width = 0;
int height = con->title_height * scale;
cairo_t *c = cairo_create(NULL);
get_text_size(c, config->font, &width, NULL, NULL, scale, false,
"%s", buffer);
cairo_destroy(c);
cairo_surface_t *surface = cairo_image_surface_create(
CAIRO_FORMAT_ARGB32, width, height);
cairo_t *cairo = cairo_create(surface);
cairo_set_source_rgba(cairo, class->background[0], class->background[1],
class->background[2], class->background[3]);
cairo_paint(cairo);
PangoContext *pango = pango_cairo_create_context(cairo);
cairo_set_antialias(cairo, CAIRO_ANTIALIAS_BEST);
cairo_set_source_rgba(cairo, class->text[0], class->text[1],
class->text[2], class->text[3]);
cairo_move_to(cairo, 0, 0);
pango_printf(cairo, config->font, scale, false, "%s", buffer);
cairo_surface_flush(surface);
unsigned char *data = cairo_image_surface_get_data(surface);
int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
struct wlr_renderer *renderer = wlr_backend_get_renderer(
output->wlr_output->backend);
*texture = wlr_texture_from_pixels(
renderer, WL_SHM_FORMAT_ARGB8888, stride, width, height, data);
cairo_surface_destroy(surface);
g_object_unref(pango);
cairo_destroy(cairo);
free(buffer);
}
void container_update_marks_textures(struct sway_container *con) {
if (!config->show_marks) {
return;
}
update_marks_texture(con, &con->marks_focused,
&config->border_colors.focused);
update_marks_texture(con, &con->marks_focused_inactive,
&config->border_colors.focused_inactive);
update_marks_texture(con, &con->marks_unfocused,
&config->border_colors.unfocused);
update_marks_texture(con, &con->marks_urgent,
&config->border_colors.urgent);
container_damage_whole(con);
}