commit c4dd79a

wf  ·  2026-06-08 16:19:19 +0000 UTC
parent a2abb60
Handle font field
2 files changed,  +37, -15
+0, -1
1@@ -6,7 +6,6 @@ An attempt at a module-based panel/bar for Wayland.
2 TODO
3 ----
4 
5-* Handle `font` field in module
6 * Provide sample config
7 
8 Dependencies
+37, -14
  1@@ -30,8 +30,6 @@
  2 	LOG("at line %d: incorrect value for option `%s`\n", elem.lineno, elem.name); return false
  3 
  4 enum side { SIDE_LEFT, SIDE_MIDDLE, SIDE_RIGHT };
  5-enum edge { EDGE_TOP, EDGE_RIGHT, EDGE_BOTTOM, EDGE_LEFT };
  6-
  7 struct module {
  8 	char *name, *command;
  9 	uint32_t interval;
 10@@ -49,6 +47,7 @@ struct module {
 11 	char *buf;
 12 	size_t buflen;
 13 	uint64_t next;
 14+	struct wld_font *font;
 15 };
 16 
 17 struct config {
 18@@ -633,16 +632,30 @@ setup(struct app *app) {
 19 			LOG("failed to start module `%s`\n", m->name);
 20 	}
 21 
 22+	for (int s = 0; s < 3; s++) {
 23+		for (size_t i = 0; i < MAXMOD; i++) {
 24+			struct module *m = app->panel.modules[s][i];
 25+			if (!m || !m->style.font || m->font)
 26+				continue;
 27+
 28+			m->font = wld_font_open_name(app->wld_fctx, m->style.font);
 29+			if (!m->font) {
 30+				LOG("couldn't open font `%s`\n", app->config.style.font);
 31+				return false;
 32+			}
 33+		}
 34+	}
 35+
 36 	return true;
 37 }
 38 
 39 static void
 40-module_draw(struct app *app, struct module *m, uint32_t x) {
 41+module_draw(struct app *app, struct module *m, struct wld_font *f, uint32_t x) {
 42 	struct wld_extents ext;
 43-	wld_font_text_extents(app->wld_font, m->buf, &ext);
 44+	wld_font_text_extents(f, m->buf, &ext);
 45 
 46 	uint32_t tw = (uint32_t)ext.advance;
 47-	uint32_t th = app->wld_font->height;
 48+	uint32_t th = f->height;
 49 
 50 	uint32_t mw = m->width ? m->width : tw + m->padding[1] + m->padding[3];
 51 	uint32_t mh = m->height ? m->height : th + m->padding[0] + m->padding[2];
 52@@ -655,14 +668,14 @@ module_draw(struct app *app, struct module *m, uint32_t x) {
 53 	int32_t cy = my + m->padding[0];
 54 
 55 	uint32_t tx = cx + (cw - tw) / 2;
 56-	int32_t ty = cy + ch / 2 + th / 2 - app->wld_font->descent;
 57+	int32_t ty = cy + ch / 2 + th / 2 - f->descent;
 58 
 59 	wld_fill_rectangle(app->wld_ren, m->style.border_color,
 60 		mx - m->style.border_width, my - m->style.border_width,
 61 		mw + m->style.border_width * 2, mh + m->style.border_width * 2);
 62 	wld_fill_rectangle(app->wld_ren, m->style.background,
 63 		mx, my, mw, mh);
 64-	wld_draw_text(app->wld_ren, app->wld_font, m->style.foreground,
 65+	wld_draw_text(app->wld_ren, f, m->style.foreground,
 66 		tx, ty, m->buf, m->buflen, NULL);
 67 }
 68 
 69@@ -680,10 +693,12 @@ draw(struct app *app) {
 70 			struct module *m = app->panel.modules[SIDE_LEFT][i];
 71 			if (!m || m->buflen == 0)
 72 				continue;
 73-			module_draw(app, m, x);
 74+
 75+			struct wld_font *font = m->font ? m->font : app->wld_font;
 76+			module_draw(app, m, font, x);
 77 
 78 			struct wld_extents ext;
 79-			wld_font_text_extents(app->wld_font, m->buf, &ext);
 80+			wld_font_text_extents(font, m->buf, &ext);
 81 			uint32_t w = m->width ? m->width : ext.advance + m->padding[1] + m->padding[3];
 82 
 83 			x += w;
 84@@ -700,8 +715,10 @@ draw(struct app *app) {
 85 			if (!m || m->buflen == 0)
 86 				continue;
 87 
 88+			struct wld_font *font = m->font ? m->font : app->wld_font;
 89+
 90 			struct wld_extents ext;
 91-			wld_font_text_extents(app->wld_font, m->buf, &ext);
 92+			wld_font_text_extents(font, m->buf, &ext);
 93 			uint32_t w = m->width ? m->width : ext.advance + m->padding[1] + m->padding[3];
 94 
 95 			tw += w;
 96@@ -713,10 +730,12 @@ draw(struct app *app) {
 97 			struct module *m = app->panel.modules[SIDE_MIDDLE][i];
 98 			if (!m || m->buflen == 0)
 99 				continue;
100-			module_draw(app, m, x);
101+
102+			struct wld_font *font = m->font ? m->font : app->wld_font;
103+			module_draw(app, m, font, x);
104 
105 			struct wld_extents ext;
106-			wld_font_text_extents(app->wld_font, m->buf, &ext);
107+			wld_font_text_extents(font, m->buf, &ext);
108 			uint32_t w = m->width ? m->width : ext.advance + m->padding[1] + m->padding[3];
109 
110 			x += w;
111@@ -732,12 +751,14 @@ draw(struct app *app) {
112 			if (!m || m->buflen == 0)
113 				continue;
114 
115+			struct wld_font *font = m->font ? m->font : app->wld_font;
116+
117 			struct wld_extents ext;
118-			wld_font_text_extents(app->wld_font, m->buf, &ext);
119+			wld_font_text_extents(font, m->buf, &ext);
120 			uint32_t w = m->width ? m->width : ext.advance + m->padding[1] + m->padding[3];
121 
122 			x -= w + m->margins[1];
123-			module_draw(app, m, x);
124+			module_draw(app, m, font, x);
125 		}
126 	}
127 
128@@ -855,6 +876,8 @@ cleanup(struct app *app) {
129 				free(m->name);
130 			if (m->command)
131 				free(m->command);
132+			if (m->font)
133+				wld_font_close(m->font);
134 			if (m->style.font)
135 				free(m->style.font);
136 			free(m);