changeset 610:90ac81e4c190

ui: reorder params
author David Demelier <markand@malikania.fr>
date Sat, 12 Aug 2023 09:44:49 +0200
parents d97674d33764
children f2aff78644db
files examples/example-label/example-label.c libmlk-ui/mlk/ui/gridmenu.c libmlk-ui/mlk/ui/label.c libmlk-ui/mlk/ui/label.h
diffstat 4 files changed, 58 insertions(+), 58 deletions(-) [+]
line wrap: on
line diff
--- a/examples/example-label/example-label.c	Fri Aug 11 18:01:28 2023 +0200
+++ b/examples/example-label/example-label.c	Sat Aug 12 09:44:49 2023 +0200
@@ -39,7 +39,7 @@
 
 /* Custom delegate/style for glowing one. */
 static struct mlk_style style_glow;
-static struct mlk_label_delegate delegate_glow;
+static struct mlk_label_if delegate_glow;
 static struct mlk_glower glower = {
 	.start  = 0xffce7fff,
 	.end    = 0xd58d6bff,
@@ -55,7 +55,7 @@
 		.label = {
 			.text = "The world is Malikania.",
 			.style = &style_glow,
-			.delegate = &delegate_glow
+			.iface = &delegate_glow
 		}
 	},
 	{
@@ -63,7 +63,7 @@
 		.label = {
 			.text = "Top left",
 			.style = &mlk_style,
-			.delegate = &mlk_label_delegate
+			.iface = &mlk_label_if
 		}
 	},
 	{
@@ -71,7 +71,7 @@
 		.label = {
 			.text = "Top",
 			.style = &mlk_style,
-			.delegate = &mlk_label_delegate
+			.iface = &mlk_label_if
 		}
 	},
 	{
@@ -79,7 +79,7 @@
 		.label = {
 			.text = "Top right",
 			.style = &mlk_style,
-			.delegate = &mlk_label_delegate
+			.iface = &mlk_label_if
 		}
 	},
 	{
@@ -87,7 +87,7 @@
 		.label = {
 			.text = "Right",
 			.style = &mlk_style,
-			.delegate = &mlk_label_delegate
+			.iface = &mlk_label_if
 		}
 	},
 	{
@@ -95,7 +95,7 @@
 		.label = {
 			.text = "Bottom right",
 			.style = &mlk_style,
-			.delegate = &mlk_label_delegate
+			.iface = &mlk_label_if
 		}
 	},
 	{
@@ -103,7 +103,7 @@
 		.label = {
 			.text = "Bottom",
 			.style = &mlk_style,
-			.delegate = &mlk_label_delegate
+			.iface = &mlk_label_if
 		}
 	},
 	{
@@ -111,7 +111,7 @@
 		.label = {
 			.text = "Bottom left",
 			.style = &mlk_style,
-			.delegate = &mlk_label_delegate
+			.iface = &mlk_label_if
 		}
 	},
 	{
@@ -119,7 +119,7 @@
 		.label = {
 			.text = "Left",
 			.style = &mlk_style,
-			.delegate = &mlk_label_delegate
+			.iface = &mlk_label_if
 		}
 	}
 };
@@ -127,11 +127,11 @@
 static struct mlk_label mouse_label = {
 	.text = "This one follows your mouse and is not aligned.",
 	.style = &mlk_style,
-	.delegate = &mlk_label_delegate
+	.iface = &mlk_label_if
 };
 
 static void
-delegate_glow_update(struct mlk_label_delegate *self, struct mlk_label *label, unsigned int ticks)
+delegate_glow_update(struct mlk_label_if *self, struct mlk_label *label, unsigned int ticks)
 {
 	(void)self;
 
@@ -147,6 +147,14 @@
 
 	if (mlk_example_init("example-label") < 0)
 		mlk_panic();
+	
+	/* Change default style. */
+	mlk_style.normal.color.text = 0x005162ff;
+
+	/* Copy default label delegate and style and adapt. */
+	style_glow = mlk_style;
+	delegate_glow = mlk_label_if;
+	delegate_glow.update = delegate_glow_update;
 
 	for (size_t i = 0; i < MLK_UTIL_SIZE(table); ++i) {
 		l = &table[i].label;
@@ -155,14 +163,6 @@
 	}
 
 	mlk_glower_init(&glower);
-
-	/* Change default style. */
-	mlk_style.normal.color.text = 0x005162ff;
-
-	/* Copy default label delegate and style and adapt. */
-	style_glow = mlk_style;
-	delegate_glow = mlk_label_delegate;
-	delegate_glow.update = delegate_glow_update;
 }
 
 static void
--- a/libmlk-ui/mlk/ui/gridmenu.c	Fri Aug 11 18:01:28 2023 +0200
+++ b/libmlk-ui/mlk/ui/gridmenu.c	Sat Aug 12 09:44:49 2023 +0200
@@ -214,7 +214,7 @@
 
 	struct mlk_label label;
 
-	mlk_label_init(&label, menu->style, &mlk_label_delegate);
+	mlk_label_init(&label, &mlk_label_if, menu->style);
 	label.x = menu->x + menu->style->normal.geo.padding + (col * menu->eltw) + (col * menu->spacew);
 	label.y = menu->y + menu->style->normal.geo.padding + (row * menu->elth) + (row * menu->spaceh);
 	label.text = item;
--- a/libmlk-ui/mlk/ui/label.c	Fri Aug 11 18:01:28 2023 +0200
+++ b/libmlk-ui/mlk/ui/label.c	Sat Aug 12 09:44:49 2023 +0200
@@ -26,7 +26,7 @@
 #include "ui.h"
 
 static int
-query(struct mlk_label_delegate *self,
+query(struct mlk_label_if *self,
       const struct mlk_label *label,
       unsigned int *w,
       unsigned int *h)
@@ -37,7 +37,7 @@
 }
 
 static void
-draw(struct mlk_label_delegate *self, const struct mlk_label *label)
+draw(struct mlk_label_if *self, const struct mlk_label *label)
 {
 	(void)self;
 
@@ -60,23 +60,23 @@
 	);
 }
 
-struct mlk_label_delegate mlk_label_delegate = {
+struct mlk_label_if mlk_label_if = {
 	.query = query,
 	.draw = draw
 };
 
 void
 mlk_label_init(struct mlk_label *lbl,
-               struct mlk_style *st,
-               struct mlk_label_delegate *dt)
+               struct mlk_label_if *iface,
+               struct mlk_style *st)
 {
 	assert(lbl);
 
 	lbl->x = 0;
 	lbl->y = 0;
 	lbl->text = "";
+	lbl->iface = iface ? iface : &mlk_label_if;
 	lbl->style = st ? st : &mlk_style;
-	lbl->delegate = dt ? dt : &mlk_label_delegate;
 }
 
 int
@@ -84,8 +84,8 @@
 {
 	assert(lbl);
 
-	if (lbl->delegate->query)
-		return lbl->delegate->query(lbl->delegate, lbl, w, h);
+	if (lbl->iface->query)
+		return lbl->iface->query(lbl->iface, lbl, w, h);
 
 	if (w)
 		*w = 0;
@@ -100,8 +100,8 @@
 {
 	assert(lbl);
 
-	if (lbl->delegate->update)
-		lbl->delegate->update(lbl->delegate, lbl, ticks);
+	if (lbl->iface->update)
+		lbl->iface->update(lbl->iface, lbl, ticks);
 }
 
 void
@@ -109,8 +109,8 @@
 {
 	assert(lbl);
 
-	if (lbl->delegate->draw)
-		lbl->delegate->draw(lbl->delegate, lbl);
+	if (lbl->iface->draw)
+		lbl->iface->draw(lbl->iface, lbl);
 }
 
 void
@@ -118,6 +118,6 @@
 {
 	assert(lbl);
 
-	if (lbl->delegate->finish)
-		lbl->delegate->finish(lbl->delegate, lbl);
+	if (lbl->iface->finish)
+		lbl->iface->finish(lbl->iface, lbl);
 }
--- a/libmlk-ui/mlk/ui/label.h	Fri Aug 11 18:01:28 2023 +0200
+++ b/libmlk-ui/mlk/ui/label.h	Sat Aug 12 09:44:49 2023 +0200
@@ -25,7 +25,7 @@
  */
 
 struct mlk_label;
-struct mlk_label_delegate;
+struct mlk_label_if;
 struct mlk_style;
 
 /**
@@ -71,16 +71,16 @@
 	/**
 	 * (read-write, borrowed)
 	 *
-	 * Label delegate.
+	 * Label interface.
 	 */
-	struct mlk_label_delegate *delegate;
+	struct mlk_label_if *iface;
 };
 
 /**
- * \struct mlk_label_delegate
- * \brief Label delegate.
+ * \struct mlk_label_if
+ * \brief Label interface.
  */
-struct mlk_label_delegate {
+struct mlk_label_if {
 	/*
 	 * (read-write, borrowed, optional)
 	 *
@@ -93,13 +93,13 @@
 	 *
 	 * Query required dimensions to draw this label.
 	 *
-	 * \param self this delegate
+	 * \param self this interface
 	 * \param lbl the label to query
 	 * \param w the width destination (maybe NULL)
 	 * \param h the height destination (maybe NULL)
 	 * \return 0 on success or -1 on error
 	 */
-	int (*query)(struct mlk_label_delegate *self,
+	int (*query)(struct mlk_label_if *self,
 	             const struct mlk_label *lbl,
 	             unsigned int *w,
 	             unsigned int *h);
@@ -113,7 +113,7 @@
 	 * \param lbl the label to update
 	 * \param ticks number of ticks since last frame
 	 */
-	void (*update)(struct mlk_label_delegate *self,
+	void (*update)(struct mlk_label_if *self,
 	               struct mlk_label *lbl,
 	               unsigned int ticks);
 
@@ -122,28 +122,28 @@
 	 *
 	 * Draw this label.
 	 *
-	 * \param self this delegate
+	 * \param self this interface
 	 * \param lbl the label to update
 	 */
-	void (*draw)(struct mlk_label_delegate *self,
+	void (*draw)(struct mlk_label_if *self,
 	             const struct mlk_label *lbl);
 
 	/**
 	 * (read-write, optional)
 	 *
-	 * Cleanup this delegate associated with the label.
+	 * Cleanup data for this label.
 	 *
-	 * \param self this delegate
+	 * \param self this interface
 	 * \param lbl the underlying label
 	 */
-	void (*finish)(struct mlk_label_delegate *self,
+	void (*finish)(struct mlk_label_if *self,
 	               struct mlk_label *lbl);
 };
 
 /**
- * \brief Default stateless delegate for label.
+ * \brief Default stateless interface for label.
  */
-extern struct mlk_label_delegate mlk_label_delegate;
+extern struct mlk_label_if mlk_label_if;
 
 #if defined(__cplusplus)
 extern "C" {
@@ -156,34 +156,34 @@
  *
  * \pre lbl != NULL
  * \param lbl the label to default initialize
+ * \param dt interface to use (or NULL to use a default)
  * \param st style to use (or NULL to use a default)
- * \param dt delegate to use (or NULL to use a default)
  */
 void
 mlk_label_init(struct mlk_label *lbl,
-               struct mlk_style *st,
-               struct mlk_label_delegate *dt);
+               struct mlk_label_if *dt,
+	       struct mlk_style *st);
 
 /**
- * Invoke ::mlk_label_delegate::query.
+ * Invoke ::mlk_label_if::query.
  */
 int
 mlk_label_query(const struct mlk_label *lbl, unsigned int *w, unsigned int *h);
 
 /**
- * Invoke ::mlk_label_delegate::update.
+ * Invoke ::mlk_label_if::update.
  */
 void
 mlk_label_update(struct mlk_label *lbl, unsigned int ticks);
 
 /**
- * Invoke ::mlk_label_delegate::draw.
+ * Invoke ::mlk_label_if::draw.
  */
 void
 mlk_label_draw(const struct mlk_label *lbl);
 
 /**
- * Invoke ::mlk_label_delegate::finish.
+ * Invoke ::mlk_label_if::finish.
  */
 void
 mlk_label_finish(struct mlk_label *lbl);