Adding vertical content alignment control.

This adds the option `content_alignment`, which allows the user to set
the vertical alignment of the notification's content (i.e. icon and
text) to either top (`CONTENT_TOP`), center (`CONTENT_CENTER`, default),
or bottom (`CONTENT_BOT`). The default preserves current behaviour,
while the other options fulfill #486.
This commit is contained in:
chronus 2020-01-25 16:35:38 +01:00
parent 3f3082efb3
commit 89d7a81b9c
5 changed files with 77 additions and 14 deletions

View File

@ -511,32 +511,65 @@ static void render_content(cairo_t *c, struct colored_layout *cl, int width)
int h_text; int h_text;
pango_layout_get_pixel_size(cl->l, NULL, &h_text); pango_layout_get_pixel_size(cl->l, NULL, &h_text);
if (cl->icon && settings.icon_position == ICON_LEFT) { int text_x = settings.h_padding,
cairo_move_to(c, cairo_image_surface_get_width(cl->icon) + 2 * settings.h_padding, text_y = settings.padding + h / 2 - h_text / 2;
settings.padding + h/2 - h_text/2);
} else if (cl->icon && settings.icon_position == ICON_RIGHT) { // text positioning
cairo_move_to(c, settings.h_padding, settings.padding + h/2 - h_text/2); if (cl->icon) {
} else { // vertical alignment
cairo_move_to(c, settings.h_padding, settings.padding); switch (settings.content_alignment) {
case CONTENT_TOP:
text_y = settings.padding;
break;
case CONTENT_BOTTOM:
text_y = h + settings.padding - h_text;
if (text_y < 0) text_y = settings.padding;
break;
default: // CONTENT_CENTER
break;
}
// icon position
switch (settings.icon_position) {
case ICON_LEFT:
text_x = cairo_image_surface_get_width(cl->icon) + 2 * settings.h_padding;
break;
default: // ICON_RIGHT
break;
}
} }
cairo_move_to(c, text_x, text_y);
cairo_set_source_rgb(c, cl->fg.r, cl->fg.g, cl->fg.b); cairo_set_source_rgb(c, cl->fg.r, cl->fg.g, cl->fg.b);
pango_cairo_update_layout(c, cl->l); pango_cairo_update_layout(c, cl->l);
pango_cairo_show_layout(c, cl->l); pango_cairo_show_layout(c, cl->l);
// icon positioning
if (cl->icon) { if (cl->icon) {
unsigned int image_width = cairo_image_surface_get_width(cl->icon), unsigned int image_width = cairo_image_surface_get_width(cl->icon),
image_height = cairo_image_surface_get_height(cl->icon), image_height = cairo_image_surface_get_height(cl->icon),
image_x, image_x = width - settings.h_padding - image_width,
image_y = settings.padding + h/2 - image_height/2; image_y = settings.padding + h/2 - image_height/2;
if (settings.icon_position == ICON_LEFT) { // vertical alignment
image_x = settings.h_padding; switch (settings.content_alignment) {
} else if (settings.icon_position == ICON_RIGHT){ case CONTENT_TOP:
image_x = width - settings.h_padding - image_width; image_y = settings.padding;
} else { break;
LOG_E("Tried to draw icon but icon position is not valid. %s:%d", __FILE__, __LINE__); case CONTENT_BOTTOM:
image_y = h + settings.padding - image_height;
if (image_y < 0 || image_y > h) image_y = settings.padding;
break;
default: // CONTENT_CENTER
break;
}
// icon position
switch (settings.icon_position) {
case ICON_LEFT:
image_x = settings.h_padding;
break;
default: // ICON_RIGHT
break;
} }
cairo_set_source_surface(c, cl->icon, image_x, image_y); cairo_set_source_surface(c, cl->icon, image_x, image_y);

View File

@ -103,6 +103,18 @@ bool string_parse_icon_position(const char *s, enum icon_position *ret)
return false; return false;
} }
bool string_parse_content_alignment(const char *s, enum content_alignment *ret)
{
ASSERT_OR_RET(STR_FULL(s), false);
ASSERT_OR_RET(ret, false);
STRING_PARSE_RET("top", CONTENT_TOP);
STRING_PARSE_RET("center", CONTENT_CENTER);
STRING_PARSE_RET("bottom", CONTENT_BOTTOM);
return false;
}
bool string_parse_markup_mode(const char *s, enum markup_mode *ret) bool string_parse_markup_mode(const char *s, enum markup_mode *ret)
{ {
ASSERT_OR_RET(STR_FULL(s), false); ASSERT_OR_RET(STR_FULL(s), false);

View File

@ -14,6 +14,7 @@ bool string_parse_ellipsize(const char *s, enum ellipsize *ret);
bool string_parse_follow_mode(const char *s, enum follow_mode *ret); bool string_parse_follow_mode(const char *s, enum follow_mode *ret);
bool string_parse_fullscreen(const char *s, enum behavior_fullscreen *ret); bool string_parse_fullscreen(const char *s, enum behavior_fullscreen *ret);
bool string_parse_icon_position(const char *s, enum icon_position *ret); bool string_parse_icon_position(const char *s, enum icon_position *ret);
bool string_parse_content_alignment(const char *s, enum content_alignment *ret);
bool string_parse_markup_mode(const char *s, enum markup_mode *ret); bool string_parse_markup_mode(const char *s, enum markup_mode *ret);
bool string_parse_mouse_action(const char *s, enum mouse_action *ret); bool string_parse_mouse_action(const char *s, enum mouse_action *ret);
bool string_parse_sepcolor(const char *s, struct separator_color_data *ret); bool string_parse_sepcolor(const char *s, struct separator_color_data *ret);

View File

@ -423,6 +423,21 @@ void load_settings(char *cmdline_config_path)
g_free(c); g_free(c);
} }
{
char *c = option_get_string(
"global",
"content_alignment", "-content_alignment", "center",
"Align icon and text top/center/bottom"
);
if (!string_parse_content_alignment(c, &settings.content_alignment)) {
if (c)
LOG_W("Unknown content alignment: '%s'", c);
settings.content_alignment = defaults.content_alignment;
}
g_free(c);
}
settings.min_icon_size = option_get_int( settings.min_icon_size = option_get_int(
"global", "global",
"min_icon_size", "-min_icon_size", defaults.min_icon_size, "min_icon_size", "-min_icon_size", defaults.min_icon_size,

View File

@ -11,6 +11,7 @@
enum alignment { ALIGN_LEFT, ALIGN_CENTER, ALIGN_RIGHT }; enum alignment { ALIGN_LEFT, ALIGN_CENTER, ALIGN_RIGHT };
enum ellipsize { ELLIPSE_START, ELLIPSE_MIDDLE, ELLIPSE_END }; enum ellipsize { ELLIPSE_START, ELLIPSE_MIDDLE, ELLIPSE_END };
enum icon_position { ICON_LEFT, ICON_RIGHT, ICON_OFF }; enum icon_position { ICON_LEFT, ICON_RIGHT, ICON_OFF };
enum content_alignment { CONTENT_TOP, CONTENT_CENTER, CONTENT_BOTTOM };
enum separator_color { SEP_FOREGROUND, SEP_AUTO, SEP_FRAME, SEP_CUSTOM }; enum separator_color { SEP_FOREGROUND, SEP_AUTO, SEP_FRAME, SEP_CUSTOM };
enum follow_mode { FOLLOW_NONE, FOLLOW_MOUSE, FOLLOW_KEYBOARD }; enum follow_mode { FOLLOW_NONE, FOLLOW_MOUSE, FOLLOW_KEYBOARD };
enum mouse_action { MOUSE_NONE, MOUSE_DO_ACTION, MOUSE_CLOSE_CURRENT, MOUSE_CLOSE_ALL }; enum mouse_action { MOUSE_NONE, MOUSE_DO_ACTION, MOUSE_CLOSE_CURRENT, MOUSE_CLOSE_ALL };
@ -75,6 +76,7 @@ struct settings {
char *browser; char *browser;
char **browser_cmd; char **browser_cmd;
enum icon_position icon_position; enum icon_position icon_position;
enum content_alignment content_alignment;
int min_icon_size; int min_icon_size;
int max_icon_size; int max_icon_size;
char *icon_path; char *icon_path;