3 Commits
Author SHA1 Message Date
admin f36e02c2b1 Merge pull request #80 from Alpaq92/button-width
Buttons sized and drawn like GTK's; custom content; version 0.7.2
2026-10-20 15:43:05 +02:00
adminandClaude Opus 5.5 35586673e9 Version 0.7.2
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 15:41:24 +02:00
adminandClaude Opus 5.5 d0e3ae843c Buttons sized and drawn like GTK's: content width, centered icon, custom content
- .fit_content sizes an icon button to the icon's drawn width, a 12 px gap,
  the label and half the sheet's padding; a .box.w is then its least width,
  and the button rule's min-width applies.
- At any width the icon and label are centered together on the icon's drawn
  pixels; the icon follows the button font, or 'button .icon' in the sheet.
- reaktor_button_width, reaktor_button_accent_icon, .accent with .icon.
- reaktor_button_custom and .paint/.paint_user: the sheet's button with the
  inside painted by the app.
- Showcase: icon buttons sized to content and wrapping, a Width section with
  the four ways to size a button, and a Custom content section.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 15:41:20 +02:00
10 changed files with 403 additions and 120 deletions
+1 -1
View File
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.16)
project(reaktor VERSION 0.7.1 LANGUAGES C CXX)
project(reaktor VERSION 0.7.2 LANGUAGES C CXX)
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
set(_reaktor_top ON)
+13 -2
View File
@@ -63,6 +63,7 @@ struct img_slot {
char src[192];
unsigned hash;
int px, w, h;
float ink_l, ink_r;
};
static inline float
@@ -268,6 +269,7 @@ void img_cache_clear(App *app);
void img_cache_free(App *app);
struct nk_image icon_over(App *app, const char *src, int px, float over);
struct nk_image icon(App *app, const char *src, int px);
struct nk_image icon_ink(App *app, const char *src, int px, float *left, float *right);
const struct nk_user_font *pick_font(App *app, int px, int bold);
const struct nk_user_font *base_font(App *app);
void rebuild_font(App *app);
@@ -331,7 +333,14 @@ void a11y_dump_once(App *app);
void hot_push(App *app, struct nk_rect r, int cursor, int repaint);
#define REAKTOR_BUTTON_GAP 12.0f
float css_button_content_w(App *app, struct nk_context *ctx, const char *selector,
const struct nk_user_font *f, const char *icon_src,
const char *label);
const char *reaktor_ionicon_src(char out[192], const char *name, const char *hex, float sw);
const char *reaktor_button_src(char out[192], const char *ionicon);
int css_button_accent(App *app, struct nk_context *ctx, const char *selector,
const char *icon_src,
const char *label);
int reaktor_css_button_flat(App *app, struct nk_context *ctx, const char *icon,
const char *name, int accent);
@@ -436,8 +445,10 @@ int reaktor_css_button_look(App *app, struct nk_context *ctx,
const reaktor_button_look *k, const char *label);
int reaktor_button_label_as(App *app, struct nk_context *ctx,
const char *sel, const char *label);
int reaktor_button_accent_as(App *app, struct nk_context *ctx,
const char *sel, const char *label);
int reaktor_button_accent_as(App *app, struct nk_context *ctx, const char *sel,
const char *ionicon, const char *label);
int reaktor_css_button_custom(App *app, struct nk_context *ctx, const char *sel,
const char *name, reaktor_button_paint paint, void *user);
int reaktor_button_icon_as(App *app, struct nk_context *ctx, const char *sel,
const char *ionicon, const char *label);
void note_field_rect(App *app, struct nk_context *ctx, struct nk_rect bounds);
+44 -6
View File
@@ -174,6 +174,8 @@ img_lookup(App *app, const char *src, int px)
slot->px = px;
slot->s.tex = NULL;
slot->w = slot->h = 0;
slot->ink_l = 0.0f;
slot->ink_r = 1.0f;
slot->s.seen = app->ctx->seq;
if (!surf) return NULL;
@@ -193,9 +195,25 @@ img_lookup(App *app, const char *src, int px)
w /= 2;
h /= 2;
}
if (sdlsurf)
if (sdlsurf) {
int x, y, lo = w, hi = -1;
reaktor_unpremultiply((unsigned char *)sdlsurf->pixels, w, h,
sdlsurf->pitch);
for (y = 0; y < h; y++) {
const Uint32 *row = (const Uint32 *)((const Uint8 *)sdlsurf->pixels + y * sdlsurf->pitch);
for (x = 0; x < w; x++)
if (row[x] >> 24 > 32) {
if (x < lo) lo = x;
if (x > hi) hi = x;
}
}
if (hi >= lo) {
slot->ink_l = (float)lo / (float)w;
slot->ink_r = (float)(hi + 1) / (float)w;
}
}
tex = sdlsurf ? SDL_CreateTextureFromSurface(app->ren, sdlsurf) : NULL;
if (sdlsurf) SDL_DestroySurface(sdlsurf);
plutovg_surface_destroy(surf);
@@ -210,23 +228,43 @@ img_lookup(App *app, const char *src, int px)
return slot;
}
struct nk_image
icon_over(App *app, const char *src, int px, float over)
static struct nk_image
slot_image(App *app, const struct img_slot *slot)
{
int raster = (int)(px * reaktor_scale() * over + 0.5f);
struct img_slot *slot = img_lookup(app, src, raster);
if (!slot) return blank_image(app);
return nk_subimage_ptr(slot->s.tex, (nk_ushort)slot->w, (nk_ushort)slot->h,
nk_rect(0.0f, 0.0f, (float)slot->w, (float)slot->h));
}
static struct img_slot *
icon_slot(App *app, const char *src, int px, float over)
{
return img_lookup(app, src, (int)(px * reaktor_scale() * over + 0.5f));
}
struct nk_image
icon_over(App *app, const char *src, int px, float over)
{
return slot_image(app, icon_slot(app, src, px, over));
}
struct nk_image
icon(App *app, const char *src, int px)
{
return icon_over(app, src, px, 2.0f);
}
/* An icon, and where its drawn pixels start and end across its box, 0 to 1. */
struct nk_image
icon_ink(App *app, const char *src, int px, float *left, float *right)
{
struct img_slot *slot = icon_slot(app, src, px, 2.0f);
*left = slot ? slot->ink_l : 0.0f;
*right = slot ? slot->ink_r : 1.0f;
return slot_image(app, slot);
}
struct nk_image
reaktor_image_file(App *app, const char *src, int px)
{
+15 -28
View File
@@ -9,33 +9,18 @@ fit_button(reaktor_box *box, const reaktor_button_spec *s)
{
const struct nk_style_button *nb = &g_frame_ctx->style.button;
const char *sel = decl_rule_or(s->style, "button");
const struct nk_user_font *f = decl_style_font(sel);
reaktor_style st;
float pad_x, pad_y, border, edge, text = 0.0f, w;
float least = box->w, w;
char src[192];
box->w = box->h = 0.0f;
box->flags &= ~(REAKTOR_LAY_FILL_X | REAKTOR_LAY_FILL_Y);
decl_box_from_style(box, NULL, s->style, "button", nb->padding.x, nb->padding.y);
reaktor_style_get(sel, &st);
pad_x = st.matched ? st.pad_x : nb->padding.x;
pad_y = st.matched ? st.pad_y : nb->padding.y;
border = st.matched ? st.border : nb->border;
edge = border + (st.matched ? st.rounding : nb->rounding);
if (s->label)
text = reaktor_text_w(f, s->label);
w = text + 2.0f * (pad_x + (st.matched ? border : edge));
if (s->icon && s->label) {
float room = SDL_max(box->h * 0.5f - edge, 0.0f);
float icon = box->h - 2.0f * SDL_min(pad_y, room)
- 2.0f * nb->image_padding.x;
w = SDL_max(w, text + 2.0f * (3.0f * pad_x + nb->image_padding.x + icon));
} else if (s->icon) {
if (s->icon && !s->label)
w = box->h;
}
box->w = SDL_ceilf(w);
else
w = css_button_content_w(g_frame_app, g_frame_ctx, sel, decl_style_font(sel),
reaktor_button_src(src, s->icon), s->label);
box->w = SDL_ceilf(SDL_max(w, least));
}
static float
@@ -93,7 +78,11 @@ reaktor_button(const reaktor_button_spec *s)
}
fitted = reaktor_fit_label(g_frame_app, g_frame_ctx, r);
reaktor_note_mute(g_frame_app, 1);
if (s->flat && s->icon && !s->label) {
if (s->paint) {
hit = reaktor_css_button_custom(g_frame_app, g_frame_ctx, sel,
s->name ? s->name : s->label, s->paint,
s->paint_user);
} else if (s->flat && s->icon && !s->label) {
hit = reaktor_css_button_flat(g_frame_app, g_frame_ctx, s->icon, s->name,
s->accent);
} else if (s->icon && !s->label) {
@@ -102,12 +91,10 @@ reaktor_button(const reaktor_button_spec *s)
hit = reaktor_css_button_image(g_frame_app, g_frame_ctx, sel,
reaktor_ionicon(g_frame_app, s->icon, (int)px),
px, s->name);
} else if (s->icon)
hit = reaktor_button_icon_as(g_frame_app, g_frame_ctx, sel, s->icon, s->label);
else if (s->accent && !s->disabled)
hit = reaktor_button_accent_as(g_frame_app, g_frame_ctx, sel, s->label);
} else if (s->accent && !s->disabled)
hit = reaktor_button_accent_as(g_frame_app, g_frame_ctx, sel, s->icon, s->label);
else
hit = reaktor_button_label_as(g_frame_app, g_frame_ctx, sel, s->label);
hit = reaktor_button_icon_as(g_frame_app, g_frame_ctx, sel, s->icon, s->label);
reaktor_note_mute(g_frame_app, 0);
reaktor_unfit_label(g_frame_ctx, fitted);
if (s->disabled) {
+158 -39
View File
@@ -323,9 +323,132 @@ reaktor_visible(struct nk_color want, struct nk_color behind,
return contrast_ratio(a, b) < 1.12f ? fallback : want;
}
/* What the sheet's rule puts between a button's edge and its label. */
static float
button_inset(struct nk_context *ctx, const reaktor_style *st, int glyph)
{
const struct nk_style_button *nb = &ctx->style.button;
float pad = st->matched ? st->pad_x : nb->padding.x;
float edge = st->matched ? st->border : nb->border + nb->rounding;
/* An icon tightens the sides, as GTK's image-text buttons are. */
return (glyph ? pad * 0.5f : pad) + edge;
}
/* A button's icon: "button .icon" in the sheet sizes it and spaces it from
the label; unstyled, it follows the font. ink_l and ink_r are where its
drawn pixels start and end, since an Ionicon sits anywhere in its square. */
typedef struct button_glyph {
struct nk_image im;
float px, gap, ink_l, ink_r;
} button_glyph;
static void
glyph_of(App *app, struct nk_context *ctx, const char *selector, const char *icon_src,
button_glyph *g)
{
reaktor_style st;
char q[128];
SDL_snprintf(q, sizeof q, "%s .icon", selector);
reaktor_style_get(q, &st);
g->px = st.matched && st.width > 0.0f ? st.width
: SDL_roundf(ctx->style.font->height * 1.125f);
g->gap = st.matched && st.margin[REAKTOR_SIDE_RIGHT] > 0.0f
? st.margin[REAKTOR_SIDE_RIGHT] : REAKTOR_BUTTON_GAP;
g->im = icon_ink(app, icon_src, (int)g->px, &g->ink_l, &g->ink_r);
}
/* The icon and label side by side, and the button that holds them. */
static float
group_w(const struct nk_user_font *f, const button_glyph *g, const char *label)
{
return (label ? reaktor_text_w(f, label) : 0.0f) +
(g ? (g->ink_r - g->ink_l) * g->px + g->gap : 0.0f);
}
static float
content_w(struct nk_context *ctx, const char *selector, float group, int glyph)
{
reaktor_style st;
float w;
reaktor_style_get(selector, &st);
w = group + 2.0f * button_inset(ctx, &st, glyph);
return st.matched && st.min_width > w ? st.min_width : w;
}
float
css_button_content_w(App *app, struct nk_context *ctx, const char *selector,
const struct nk_user_font *f, const char *icon_src, const char *label)
{
button_glyph g;
if (icon_src) glyph_of(app, ctx, selector, icon_src, &g);
return content_w(ctx, selector, group_w(f, icon_src ? &g : NULL, label), icon_src != NULL);
}
static struct nk_color
state_ink(struct nk_context *ctx)
{
const struct nk_style_button *bs = &ctx->style.button;
nk_flags state = ctx->last_widget_state;
struct nk_color ink = (state & NK_WIDGET_STATE_HOVER) ? bs->text_hover
: (state & NK_WIDGET_STATE_ACTIVED) ? bs->text_active
: bs->text_normal;
return nk_rgb_factor(ink, bs->color_factor_text);
}
/* The glyph and label centred as one, as GTK and Qt set them: the label is
set flush right of a padding that leaves the pair in the middle. 0 when
the button is too narrow for both, and the label stands alone. */
static int
group_push(struct nk_context *ctx, const char *selector, struct nk_rect b,
const button_glyph *g, const char *label, struct nk_rect *at)
{
struct nk_style_button *bs = &ctx->style.button;
float group, side;
group = group_w(ctx->style.font, g, label);
if (b.w + 1.0f < content_w(ctx, selector, group, 1)) return 0;
side = (b.w - group) * 0.5f;
at->x = (float)(int)(b.x + side - g->ink_l * g->px + 0.5f);
at->y = (float)(int)(b.y + (b.h - g->px) * 0.5f);
at->w = at->h = g->px;
nk_style_push_flags(ctx, &bs->text_alignment, NK_TEXT_ALIGN_RIGHT | NK_TEXT_ALIGN_MIDDLE);
nk_style_push_vec2(ctx, &bs->padding, nk_vec2(side - bs->border - bs->rounding, bs->padding.y));
return 1;
}
/* Presses the button, with its icon beside the label when there is room,
in the label's color for the button's state. */
static int
group_press(App *app, struct nk_context *ctx, const char *selector, const char *icon_src,
const char *label)
{
button_glyph g;
struct nk_rect at;
int drawn = 0, clicked;
if (icon_src) {
glyph_of(app, ctx, selector, icon_src, &g);
drawn = group_push(ctx, selector, nk_widget_bounds(ctx), &g, label, &at);
}
reaktor_button_arm(ctx);
clicked = nk_button_label(ctx, label);
reaktor_chrome_disarm();
if (drawn) {
nk_style_pop_vec2(ctx);
nk_style_pop_flags(ctx);
nk_draw_image(nk_window_get_canvas(ctx), at, &g.im, state_ink(ctx));
}
return clicked;
}
int
css_button_accent(App *app, struct nk_context *ctx, const char *selector,
const char *label)
const char *icon_src, const char *label)
{
reaktor_style_frame f, a = { 0, 0, 0, 0, 0 };
unsigned char c[4], hov[4];
@@ -356,62 +479,58 @@ css_button_accent(App *app, struct nk_context *ctx, const char *selector,
a.colors = 4;
g_chrome.edge_hover = g_chrome.edge_active = app->accent;
reaktor_button_arm(ctx);
clicked = nk_button_label(ctx, label);
reaktor_chrome_disarm();
clicked = group_press(app, ctx, selector, icon_src, label);
reaktor_style_frame_pop(ctx, a);
reaktor_style_frame_pop(ctx, f);
return clicked;
}
static int
icon_place(struct nk_context *ctx, struct nk_rect b, float pad_x,
const char *label, struct nk_rect *at)
{
const struct nk_style_button *s = &ctx->style.button;
const struct nk_user_font *f = ctx->style.font;
float text = reaktor_text_w(f, label);
at->x = b.x + 2.0f * pad_x + s->image_padding.x;
at->y = b.y + s->padding.y + s->image_padding.y;
at->w = b.h - 2.0f * s->padding.y - 2.0f * s->image_padding.x;
at->h = b.h - 2.0f * s->padding.y - 2.0f * s->image_padding.y;
if (b.w >= text + 2.0f * (3.0f * pad_x + s->image_padding.x + at->w)) return 1;
/* A wide padding still leaves room once the icon moves to the edge. */
at->x = b.x + pad_x + s->image_padding.x;
return b.w >= text + 2.0f * (1.5f * pad_x + s->image_padding.x + at->w);
}
int
css_button_icon(App *app, struct nk_context *ctx, const char *selector,
const char *icon_src, const char *label)
{
reaktor_button_look k;
reaktor_style_frame f;
struct nk_rect b = nk_widget_bounds(ctx), at;
int clicked, drawn;
int clicked;
hot_push(app, b, REAKTOR_CURSOR_POINTER, 1);
hot_push(app, nk_widget_bounds(ctx), REAKTOR_CURSOR_POINTER, 1);
reaktor_note_here(app, ctx, REAKTOR_A11Y_BUTTON, label, 0);
reaktor_button_look_of(selector, &k);
f = push_button_look(app, ctx, &k);
drawn = icon_place(ctx, b, k.s.matched ? k.s.pad_x : ctx->style.button.padding.x,
label, &at);
reaktor_button_arm(ctx);
clicked = nk_button_label(ctx, label);
reaktor_chrome_disarm();
if (drawn) {
const struct nk_style_button *bs = &ctx->style.button;
nk_flags state = ctx->last_widget_state;
struct nk_image im = icon(app, icon_src, 18);
struct nk_color ink = (state & NK_WIDGET_STATE_HOVER) ? bs->text_hover
: (state & NK_WIDGET_STATE_ACTIVED) ? bs->text_active
: bs->text_normal;
clicked = group_press(app, ctx, selector, icon_src, label);
reaktor_style_frame_pop(ctx, f);
return clicked;
}
nk_draw_image(nk_window_get_canvas(ctx), at, &im,
nk_rgb_factor(ink, bs->color_factor_text));
/* The sheet's button with nothing in it; the caller paints the inside. */
int
reaktor_css_button_custom(App *app, struct nk_context *ctx, const char *sel,
const char *name, reaktor_button_paint paint, void *user)
{
reaktor_button_look k;
reaktor_style_frame f;
struct nk_rect b = nk_widget_bounds(ctx);
float in_x;
unsigned id;
int clicked;
hot_push(app, b, REAKTOR_CURSOR_POINTER, 1);
id = reaktor_note_here(app, ctx, REAKTOR_A11Y_BUTTON, name, 0);
reaktor_button_look_of(sel, &k);
in_x = button_inset(ctx, &k.s, 0);
f = push_button_look(app, ctx, &k);
reaktor_button_arm(ctx);
clicked = nk_button_text(ctx, "", 0);
reaktor_chrome_disarm();
if (paint) {
float in_y = k.s.matched ? k.s.pad_y : ctx->style.button.padding.y;
struct nk_rect c = nk_rect(b.x + in_x, b.y + in_y, b.w - 2.0f * in_x, b.h - 2.0f * in_y);
if (c.h < 0.0f) c = nk_rect(c.x, b.y, c.w, b.h);
if (c.w > 0.0f) paint(app, nk_window_get_canvas(ctx), c, state_ink(ctx), user);
}
reaktor_style_frame_pop(ctx, f);
if (reaktor_focus_activated(app, id)) clicked = 1;
return clicked;
}
+44 -16
View File
@@ -25,8 +25,8 @@ reaktor_font(App *app, int px, int bold)
#define ICON_HAIRLINE_BELOW 20
static const char *
icon_src(char out[192], const char *name, const char *hex, float sw)
const char *
reaktor_ionicon_src(char out[192], const char *name, const char *hex, float sw)
{
if (sw < 0.0f)
SDL_snprintf(out, 192, "external/ionicons/src/svg/%s.svg?stroke=%s", name, hex);
@@ -54,7 +54,7 @@ reaktor_ionicon(App *app, const char *name, int px)
{
char src[192];
return icon(app, icon_src(src, name, app->icon_hex,
return icon(app, reaktor_ionicon_src(src, name, app->icon_hex,
px < ICON_HAIRLINE_BELOW ? GLYPH_STROKE : -1.0f), px);
}
@@ -66,7 +66,7 @@ reaktor_ionicon_exact(App *app, const char *name, int px,
if (sw <= 0.0f) sw = px < ICON_HAIRLINE_BELOW ? GLYPH_STROKE : 1.0f;
reaktor_hex(stroke, hex);
return icon_over(app, icon_src(src, name, hex, sw), px, 1.0f);
return icon_over(app, reaktor_ionicon_src(src, name, hex, sw), px, 1.0f);
}
struct nk_image
@@ -75,7 +75,8 @@ reaktor_ionicon_col(App *app, const char *name, int px, struct nk_color stroke)
char hex[8], src[192];
reaktor_hex(stroke, hex);
return icon(app, icon_src(src, name, hex, px < ICON_HAIRLINE_BELOW ? GLYPH_STROKE : 1.0f), px);
return icon(app, reaktor_ionicon_src(src, name, hex,
px < ICON_HAIRLINE_BELOW ? GLYPH_STROKE : 1.0f), px);
}
struct nk_color
@@ -502,11 +503,20 @@ reaktor_button_label_as(App *app, struct nk_context *ctx, const char *sel,
return css_button(app, ctx, sel, label);
}
/* A button's icon is drawn white and tinted with its label's color. */
const char *
reaktor_button_src(char out[192], const char *ionicon)
{
return ionicon ? reaktor_ionicon_src(out, ionicon, "#ffffff", -1.0f) : NULL;
}
int
reaktor_button_accent_as(App *app, struct nk_context *ctx, const char *sel,
const char *label)
const char *ionicon, const char *label)
{
return css_button_accent(app, ctx, sel, label);
char src[192];
return css_button_accent(app, ctx, sel, reaktor_button_src(src, ionicon), label);
}
int
@@ -515,15 +525,37 @@ reaktor_button_icon_as(App *app, struct nk_context *ctx, const char *sel,
{
char src[192];
SDL_snprintf(src, sizeof(src),
"external/ionicons/src/svg/%s.svg?stroke=#ffffff", ionicon);
return css_button_icon(app, ctx, sel, src, label);
return css_button_icon(app, ctx, sel, reaktor_button_src(src, ionicon), label);
}
int
reaktor_button_accent(App *app, struct nk_context *ctx, const char *label)
{
return css_button_accent(app, ctx, "button", label);
return css_button_accent(app, ctx, "button", NULL, label);
}
int
reaktor_button_accent_icon(App *app, struct nk_context *ctx, const char *ionicon,
const char *label)
{
return reaktor_button_accent_as(app, ctx, "button", ionicon, label);
}
float
reaktor_button_width(App *app, struct nk_context *ctx, const char *ionicon,
const char *label)
{
char src[192];
return SDL_ceilf(css_button_content_w(app, ctx, "button", ctx->style.font,
reaktor_button_src(src, ionicon), label));
}
int
reaktor_button_custom(App *app, struct nk_context *ctx, const char *name,
reaktor_button_paint paint, void *user)
{
return reaktor_css_button_custom(app, ctx, "button", name, paint, user);
}
int
@@ -582,11 +614,7 @@ int
reaktor_button_icon(App *app, struct nk_context *ctx, const char *ionicon,
const char *label)
{
char src[192];
SDL_snprintf(src, sizeof(src),
"external/ionicons/src/svg/%s.svg?stroke=#ffffff", ionicon);
return css_button_icon(app, ctx, "button", src, label);
return reaktor_button_icon_as(app, ctx, "button", ionicon, label);
}
static void
+12 -4
View File
@@ -73,7 +73,7 @@ errors, so an internal function cannot slip back in.
| `reaktor_lang_pref(app)` | The language `--lang` asked for, or `NULL` |
| `reaktor_set_borderless(app, on)`, `reaktor_borderless(app)` | Switch the main window's frame off for a titlebar of your own, drawn with `.window.hit_test` |
| `reaktor_set_visible(app, on)` | Show or hide that App's window; with `.window.hidden`, the main window waits for it |
| `reaktor_version()` | Reaktor's version, such as `"0.7.1"` |
| `reaktor_version()` | Reaktor's version, such as `"0.7.2"` |
The `set` calls are for the main thread, take effect at the next frame, and do
nothing when nothing changes.
@@ -228,7 +228,7 @@ again), `.style` (a selector to read instead of the widget's own) and `.box`
| Widget | Call | Answers 1 | Its own fields |
| --- | --- | --- | --- |
| Button | `reaktor_button` | when pressed | `.label`, `.icon`, `.accent`, `.disabled`, `.repeat` (fires while held), `.keys`, `.on_press`, `.tooltip`, `.fit_content` (exactly as large as its label and icon, whatever `.box` asks) |
| Button | `reaktor_button` | when pressed | `.label`, `.icon`, `.accent`, `.disabled`, `.repeat` (fires while held), `.keys`, `.on_press`, `.tooltip`, `.fit_content` (as large as its label and icon need; a `.box.w` is then the least it takes, so short labels line up and long ones grow); `.accent` with `.icon` draws both; `.paint` and `.paint_user` draw the inside as `reaktor_button_custom` does, with `.label` measured for `.fit_content` and read as the name unless `.name` is set |
| Link | `reaktor_link` | when pressed | `.text`, `.active`, `.on_press`, or `.url` to open in the browser |
| Label | `reaktor_label` | — | `.text`, `.wrap`, `.align` (`REAKTOR_LEFT`, `_CENTER`, `_RIGHT`), `.color` (a token), `.value`, `.silent` (left out of the tree) |
| Icon | `reaktor_icon` | — | `.name` (an Ionicon) and `.accent`, or `.src` (any image asset: SVG, PNG, JPEG), stretched to its box; `.tooltip` |
@@ -253,8 +253,14 @@ again), `.style` (a selector to read instead of the widget's own) and `.box`
| Tree | `reaktor_tree` | when another node is chosen | `.count`, `.parent(node, user)`, `.label(node, user)`, `.icon(node, user)`, `.open` (a flag per node), `.selected` (a node), `.row_h` |
| Splitter | `reaktor_splitter` | while dragged | `.value` (the first child's share, 0 to 1), `.lo`, `.hi` (0.1 and 0.9 unset), `.vertical` |
The icon is 1.125 times the button font's height, 18 px at 16 px, unless the sheet sizes `button .icon` with `width`; its `margin-right` is then the gap. A `min-width` on the button's rule is the least `.fit_content` takes.
**An icon gives way to its label**: a button too narrow for both draws the
label alone. `.fit_content` makes it as large as both need.
label alone. `.fit_content` makes it as large as both need, as GTK and Qt size
a button: the label, the icon's drawn width and 12 px between them, plus half
the sheet's padding and its border on each side, since an icon tightens a
button as it does GTK's. At any width the icon and label are centered together
on the icon's drawn pixels, since an Ionicon sits anywhere in its square.
**A field writes back** its bytes into `.buf` and its length into `*.len`, and
reads both next frame, so the length must be the field's own: one from
@@ -821,7 +827,9 @@ out by hand inside a declared page. [`include/reaktor/ui.h`](../include/reaktor/
| `reaktor_fill_round`, `reaktor_edge_round` | Rounded fills and borders, smooth on the software renderer |
| `reaktor_hot`, `reaktor_hot_top`, `reaktor_hot_follow` | A rectangle that redraws on hover ([the frame loop](#the-frame-loop)), with a `REAKTOR_CURSOR_*` over it: default, pointer, text, or a horizontal or vertical resize |
| `reaktor_tip(app, rect, text)` | A tooltip over a rectangle you drew yourself, such as a bar of a chart; the text is copied, and the rectangle redraws as the pointer crosses it |
| `reaktor_button_label`, `reaktor_button_icon`, `reaktor_button_accent`, `reaktor_field_text`, `reaktor_slider_bar`, `reaktor_progress_bar`, `reaktor_chevron_at` | Styled controls inside hand-laid rows |
| `reaktor_button_label`, `reaktor_button_icon`, `reaktor_button_accent`, `reaktor_button_accent_icon`, `reaktor_field_text`, `reaktor_slider_bar`, `reaktor_progress_bar`, `reaktor_chevron_at` | Styled controls inside hand-laid rows |
| `reaktor_button_width(app, ctx, ionicon, label)` | The width `.fit_content` gives that button, for a hand-laid row; `ionicon` may be `NULL`. An icon button laid out at that width centers its icon and label together |
| `reaktor_button_custom(app, ctx, name, paint, user)` | The sheet's button with the inside left to `paint(app, canvas, content, ink, user)`: `content` is the box inside the padding, `ink` the text color for the button's state, and `name` what a reader hears. Fill, edge, hover, focus and the click stay the runtime's |
| `reaktor_menu_item`, `reaktor_menu_style_push`, `_pop`, `reaktor_menu_height`, `reaktor_menu_edge`, `reaktor_popup_rounding` | Menus and popups |
| `reaktor_note`, `_push`, `_pop`, `_here`, `_keys`, `_value`, `_bounds`, `reaktor_focus_activated` | The accessibility tree, and keyboard presses |
| `reaktor_focus_area`, `reaktor_focus_scroll` | A scrolling page body: the area and node focus moves within, and the rectangle it asks to see |
+7
View File
@@ -5,6 +5,7 @@
#include "reaktor/nuklear.h"
#include "reaktor/a11y.h"
#include "reaktor/widgets.h"
#ifndef REAKTOR_APP_FWD
#define REAKTOR_APP_FWD
@@ -68,6 +69,12 @@ int reaktor_button_label(App *app, struct reaktor_context *ctx,
const char *label);
int reaktor_button_icon(App *app, struct reaktor_context *ctx,
const char *ionicon, const char *label);
int reaktor_button_accent_icon(App *app, struct reaktor_context *ctx,
const char *ionicon, const char *label);
float reaktor_button_width(App *app, struct reaktor_context *ctx,
const char *ionicon, const char *label);
int reaktor_button_custom(App *app, struct reaktor_context *ctx, const char *name,
reaktor_button_paint paint, void *user);
reaktor_flags reaktor_field_text(App *app, struct reaktor_context *ctx,
reaktor_flags flags,
char *buf, int *len, int cap, const char *hint,
+6
View File
@@ -66,6 +66,10 @@ typedef struct reaktor_handler {
enum { REAKTOR_LEFT = 0, REAKTOR_CENTER, REAKTOR_RIGHT };
typedef void (*reaktor_button_paint)(App *app, struct reaktor_command_buffer *cv,
struct reaktor_rect content,
struct reaktor_color ink, void *user);
typedef struct reaktor_button_spec {
const char *label;
const char *icon;
@@ -80,6 +84,8 @@ typedef struct reaktor_button_spec {
unsigned char repeat;
unsigned char fit_content;
unsigned char flat;
reaktor_button_paint paint;
void *paint_user;
} reaktor_button_spec;
int reaktor_button(const reaktor_button_spec *s);
+103 -24
View File
@@ -13,6 +13,47 @@ static const struct { const char *icon, *label; } g_icons[] = {
};
#define ICON_N ((int)(sizeof(g_icons) / sizeof(g_icons[0])))
/* A count after the label, in a pill of the accent. */
static void
paint_inbox(App *app, struct reaktor_command_buffer *cv, struct reaktor_rect c,
struct reaktor_color ink, void *user)
{
const struct reaktor_user_font *f = reaktor_style_font(app, "button", 14, 0);
const char *n = (const char *)user;
float nw = f->width(f->userdata, f->height, n, (int)SDL_strlen(n)) + 14.0f;
float lw = f->width(f->userdata, f->height, "Inbox", 5);
float x = c.x + (c.w - lw - 8.0f - nw) * 0.5f;
float y = c.y + (c.h - f->height) * 0.5f;
struct reaktor_color accent = reaktor_token("--links", reaktor_rgb(0, 118, 209));
reaktor_draw_text(cv, reaktor_rect(x, y, lw, f->height), "Inbox", 5, f,
reaktor_rgba(0, 0, 0, 0), ink);
reaktor_fill_round(app, cv, reaktor_rect(x + lw + 8.0f, y - 2.0f, nw, f->height + 4.0f),
(f->height + 4.0f) * 0.5f, accent);
reaktor_draw_text(cv, reaktor_rect(x + lw + 15.0f, y, nw, f->height), n,
(int)SDL_strlen(n), f, reaktor_rgba(0, 0, 0, 0), reaktor_on(accent));
}
/* An icon over two lines of text, as a download list shows one. */
static void
paint_download(App *app, struct reaktor_command_buffer *cv, struct reaktor_rect c,
struct reaktor_color ink, void *user)
{
const struct reaktor_user_font *f = reaktor_style_font(app, "button", 14, 1);
const struct reaktor_user_font *small = reaktor_style_font(app, "button", 12, 0);
struct reaktor_image im = reaktor_ionicon(app, "download-outline", 24);
float y = c.y + (c.h - f->height - small->height - 2.0f) * 0.5f;
(void)user;
reaktor_draw_image(cv, reaktor_rect(c.x, c.y + (c.h - 24.0f) * 0.5f, 24.0f, 24.0f), &im, ink);
reaktor_draw_text(cv, reaktor_rect(c.x + 36.0f, y, c.w - 36.0f, f->height),
"reaktor-0.7.1-web.zip", 21, f, reaktor_rgba(0, 0, 0, 0), ink);
reaktor_draw_text(cv, reaktor_rect(c.x + 36.0f, y + f->height + 2.0f, c.w - 36.0f,
small->height),
"4.2 MB of 9.8 MB", 16, small, reaktor_rgba(0, 0, 0, 0),
reaktor_token("--text-muted", ink));
}
void
page_buttons(App *app, struct reaktor_context *ctx, showcase_state *s)
{
@@ -60,38 +101,76 @@ page_buttons(App *app, struct reaktor_context *ctx, showcase_state *s)
{ "chevron-forward-outline", "Forward" },
{ "menu-outline", "Menu" }
};
int row, col;
REAKTOR_ROW(.flags = REAKTOR_LAY_FILL_X | REAKTOR_LAY_WRAP,
.gap = ctx->style.window.spacing.x) {
for (i = 0; i < ICON_N; i++)
if (reaktor_button(&(reaktor_button_spec){
.label = g_icons[i].label, .icon = g_icons[i].icon,
.fit_content = 1 }))
s->presses++;
for (i = 0; i < 3; i++)
if (reaktor_button(&(reaktor_button_spec){
.label = nav[i].label, .icon = nav[i].icon,
.fit_content = 1 }))
s->presses++;
}
}
REAKTOR_COLUMN(.gap = ctx->style.window.spacing.y) {
for (row = 0; row * 4 < ICON_N; row++) {
REAKTOR_ROW(.h = 38.0f, .flags = REAKTOR_LAY_FILL_X,
.gap = ctx->style.window.spacing.x) {
for (col = 0; col < 4; col++) {
int k = row * 4 + col;
if (k >= ICON_N) break;
if (reaktor_button(&(reaktor_button_spec){
.label = g_icons[k].label,
.icon = g_icons[k].icon,
.box = { .flags = REAKTOR_LAY_FILL_X |
REAKTOR_LAY_FILL_Y } }))
s->presses++;
}
}
}
section(app, ctx, "Width",
"A button is as wide as its content with .fit_content, the way "
"GTK and Qt size one, and its icon and label are centered "
"together at any width. A .box.w with .fit_content is the least "
"it takes, so short labels line up while a long or translated one "
"still grows; .box.w alone is exact, and REAKTOR_LAY_FILL_X shares "
"the row out.");
api(app, ctx, ".fit_content / .box.w / REAKTOR_LAY_FILL_X / reaktor_button_width "
"/ reaktor_button_accent_icon");
{
static const char *const how[4] = { "Content", "At least 100", "Exactly 100",
"Shared" };
static const reaktor_box box[4] = {
{ 0 }, { .w = 100.0f }, { .w = 100.0f }, { .flags = REAKTOR_LAY_FILL_X } };
int m;
for (m = 0; m < 4; m++) {
REAKTOR_ROW(.h = 38.0f, .flags = REAKTOR_LAY_FILL_X,
.gap = ctx->style.window.spacing.x) {
for (i = 0; i < 3; i++)
if (reaktor_button(&(reaktor_button_spec){
.label = nav[i].label, .icon = nav[i].icon,
.box = { .flags = REAKTOR_LAY_FILL_X |
REAKTOR_LAY_FILL_Y } }))
s->presses++;
reaktor_label(&(reaktor_label_spec){ .text = how[m],
.box = { .w = 110.0f, .flags = REAKTOR_LAY_FILL_Y } });
if (reaktor_button(&(reaktor_button_spec){
.label = "Back", .icon = "chevron-back-outline",
.fit_content = m < 2, .box = box[m] }))
s->presses++;
if (reaktor_button(&(reaktor_button_spec){
.label = "Add folder", .icon = "folder-open-outline",
.fit_content = m < 2, .box = box[m] }))
s->presses++;
if (reaktor_button(&(reaktor_button_spec){
.label = "Wipe", .icon = "flame-outline", .accent = 1,
.fit_content = m < 2, .box = box[m] }))
s->presses++;
}
}
}
section(app, ctx, "Custom content",
"The sheet's button with nothing in it: its fill, edge, hover, "
"focus ring and click are the runtime's, and a callback paints "
"the inside, given the content box inside the padding and the "
"text color for the button's state. Its name is what a reader hears.");
api(app, ctx, ".paint / .paint_user / reaktor_button_custom(app, ctx, name, paint, user)");
REAKTOR_ROW(.h = 52.0f, .flags = REAKTOR_LAY_FILL_X,
.gap = ctx->style.window.spacing.x) {
if (reaktor_button(&(reaktor_button_spec){
.label = "Inbox, 12 new", .paint = paint_inbox, .paint_user = "12",
.box = { .w = 180.0f, .flags = REAKTOR_LAY_FILL_Y } }))
s->presses++;
if (reaktor_button(&(reaktor_button_spec){
.label = "reaktor-0.7.1-web.zip, 4.2 MB of 9.8 MB", .paint = paint_download,
.box = { .w = 280.0f, .flags = REAKTOR_LAY_FILL_Y } }))
s->presses++;
}
section(app, ctx, "Flat icons",
"An icon with no label and .flat set is a toolbar button: no "
"fill and no edge until the pointer is over it, then the sheet's "