shithub: microui

Download patch

ref: 9aa16f4d5691e44c364b44a281880ccf03434249
parent: c34204ec97ce98391456df74f4461db9f64eb622
author: Sigrid Haflínudóttir <ftrvxmtrx@gmail.com>
date: Sun Jan 5 06:15:33 EST 2020

better layout sizing for the demo

--- a/demo/common.c
+++ b/demo/common.c
@@ -22,6 +22,8 @@
   logbuf_updated = 1;
 }
 
+#define text_width(s) (ctx->text_width(ctx->style->font, s, -1) + 6)
+#define text_height() ctx->text_height(ctx->style->font)
 
 static void test_window(mu_Context *ctx) {
   static mu_Container window;
@@ -44,9 +46,9 @@
     static int show_info = 0;
     if (mu_header(ctx, &show_info, "Window Info")) {
       char buf[64];
-      const int widths[] = { 54, -1 };
+      const int widths[] = { text_width("Position:"), -1 };
       mu_layout_row(ctx, 2, widths, 0);
-      mu_label(ctx,"Position:");
+      mu_label(ctx, "Position:");
       sprintf(buf, "%d, %d", window.rect.x, window.rect.y); mu_label(ctx, buf);
       mu_label(ctx, "Size:");
       sprintf(buf, "%d, %d", window.rect.w, window.rect.h); mu_label(ctx, buf);
@@ -68,7 +70,7 @@
     /* tree */
     static int show_tree = 1;
     if (mu_header(ctx, &show_tree, "Tree and Text")) {
-      int widths[] = { 140, -1 };
+      int widths[] = { text_width("Test 1a")+text_height()*2+text_width("Button 3")+6, -1 };
       mu_layout_row(ctx, 2, widths, 0);
       mu_layout_begin_column(ctx);
       static int states[8];
@@ -86,7 +88,8 @@
         mu_end_treenode(ctx);
       }
       if (mu_begin_treenode(ctx, &states[3], "Test 2")) {
-        const int widths[] = { 54, 54 };
+        int widths[2];
+        widths[0] = widths[1] = text_width("Button 3");
         mu_layout_row(ctx, 2, widths, 0);
         if (mu_button(ctx, "Button 3")) { write_log("Pressed button 3"); }
         if (mu_button(ctx, "Button 4")) { write_log("Pressed button 4"); }
@@ -119,7 +122,7 @@
       mu_layout_row(ctx, 2, widths, 74);
       /* sliders */
       mu_layout_begin_column(ctx);
-      widths[0] = 46;
+      widths[0] = text_width("Green:");
       mu_layout_row(ctx, 2, widths, 0);
       mu_label(ctx, "Red:");   mu_slider(ctx, &bg[0], 0, 255);
       mu_label(ctx, "Green:"); mu_slider(ctx, &bg[1], 0, 255);
@@ -165,7 +168,7 @@
     /* input textbox + submit button */
     static char buf[128];
     int submitted = 0;
-    widths[0] = -70;
+    widths[0] = -text_width("Submit")-8;
     mu_layout_row(ctx, 2, widths, 0);
     if (mu_textbox(ctx, buf, sizeof(buf)) & MU_RES_SUBMIT) {
       mu_set_focus(ctx, ctx->last_id);
@@ -221,8 +224,8 @@
   };
 
   if (mu_begin_window(ctx, &window, "Style Editor")) {
-    int sw = mu_get_container(ctx)->body.w * 0.14;
-    const int widths[] = { 80, sw, sw, sw, sw, -1 };
+    int sw = mu_max(text_width("255"), mu_get_container(ctx)->body.w * 0.14);
+    const int widths[] = { text_width("scrollthumb:"), sw, sw, sw, sw, -1 };
     mu_layout_row(ctx, 6, widths, 0);
     for (int i = 0; colors[i].label; i++) {
       mu_label(ctx, colors[i].label);
--- a/demo/plan9.c
+++ b/demo/plan9.c
@@ -50,6 +50,8 @@
 	ctx->text_width = r_get_text_width;
 	ctx->text_height = r_get_text_height;
 	ctx->style->font = display->defaultfont;
+	ctx->style->size.y = display->defaultfont->height;
+	ctx->style->title_height = ctx->style->size.y + 6;
 	r_init();
 	process_frame(ctx);