Skip to content

Commit

Permalink
optimize size
Browse files Browse the repository at this point in the history
  • Loading branch information
a1ive committed Oct 26, 2023
1 parent 6ee0887 commit 829ec72
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 30 deletions.
4 changes: 0 additions & 4 deletions gnwinfo/gnwinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ void
nk_label_hover(struct nk_context* ctx, const char* str,
nk_flags alignment, struct nk_color color, nk_bool hover, nk_bool space);

void
nk_labelf_hover(struct nk_context* ctx,
nk_flags alignment, struct nk_color color, nk_bool hover, nk_bool space, const char* fmt, ...);

nk_bool
nk_combo_begin_color_dynamic(struct nk_context* ctx, struct nk_color color);

Expand Down
2 changes: 1 addition & 1 deletion gnwinfo/smart.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ draw_health(struct nk_context* ctx, CDI_SMART* smart, int disk, float height)
if (alarm <= 0)
alarm = 60;
n = cdi_get_int(smart, disk, CDI_INT_TEMPERATURE);
snprintf(tmp, sizeof(tmp), u8"%d \u00B0C", n);
snprintf(tmp, sizeof(tmp), u8"%d \u2103", n);
draw_rect(ctx, gnwinfo_get_color((double)n, (double) alarm, 90.0), tmp);
nk_group_end(ctx);
}
Expand Down
12 changes: 11 additions & 1 deletion gnwinfo/summary.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@

static CHAR m_buf[MAX_PATH];

static inline void
nk_labelf_hover(struct nk_context* ctx, nk_flags alignment, struct nk_color color, nk_bool hover, nk_bool space, const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
vsnprintf(m_buf, MAX_PATH, fmt, args);
va_end(args);
nk_label_hover(ctx, m_buf, alignment, color, hover, space);
}

LPCSTR
gnwinfo_get_node_attr(PNODE node, LPCSTR key)
{
Expand Down Expand Up @@ -317,7 +327,7 @@ draw_mem_capacity(struct nk_context* ctx)
gnwinfo_get_text(L"slots"),
capacity,
id[0] == '5' ? " MB" : "");
nk_label_hover(ctx, m_buf, NK_TEXT_LEFT, g_color_text_l, nk_false, nk_false);
nk_label_hover(ctx, m_buf, NK_TEXT_LEFT, g_color_text_l, nk_true, nk_false);
}

static VOID
Expand Down
89 changes: 66 additions & 23 deletions nuklear/nuklear.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
#include <assert.h>
#define NK_ASSERT(expr) assert(expr)

#define NK_MEMSET memset
#define NK_MEMCPY memcpy
#define NK_SIN sinf
#define NK_COS cosf
#define NK_STRTOD strtod

#define NK_IMPLEMENTATION
#include <nuklear.h>

Expand Down Expand Up @@ -91,13 +97,53 @@ nk_image_label(struct nk_context* ctx, struct nk_image img,
nk_widget_text(&win->buffer, bounds, str, len, &text, align, style->font);
}

static inline nk_bool
nk_hover_begin(struct nk_context* ctx, float width)
{
int x, y, w, h;
struct nk_window* win;
const struct nk_input* in;
struct nk_rect bounds;
int ret;

/* make sure that no nonblocking popup is currently active */
win = ctx->current;
in = &ctx->input;

w = nk_iceilf(width);
h = nk_iceilf(nk_null_rect.h);
x = nk_ifloorf(in->mouse.pos.x + 1) - (int)win->layout->clip.x;
if (w + x > (int)win->bounds.w) x = (int)win->bounds.w - w;
y = nk_ifloorf(in->mouse.pos.y + 1) - (int)win->layout->clip.y;

bounds.x = (float)x;
bounds.y = (float)y;
bounds.w = (float)w;
bounds.h = (float)h;

ret = nk_popup_begin(ctx, NK_POPUP_DYNAMIC,
"__##Tooltip##__", NK_WINDOW_NO_SCROLLBAR | NK_WINDOW_BORDER, bounds);
if (ret) win->layout->flags &= ~(nk_flags)NK_WINDOW_ROM;
win->popup.type = NK_PANEL_TOOLTIP;
ctx->current->layout->type = NK_PANEL_TOOLTIP;
return ret;
}

static inline void
nk_hover_end(struct nk_context* ctx)
{
ctx->current->seq--;
nk_popup_close(ctx);
nk_popup_end(ctx);
}

void
nk_label_hover(struct nk_context* ctx, const char* str,
nk_flags alignment, struct nk_color color, nk_bool hover, nk_bool space)
{
struct nk_window* win;
const struct nk_style* style;

int text_len;
struct nk_rect bounds;
struct nk_text text;

Expand All @@ -120,34 +166,31 @@ nk_label_hover(struct nk_context* ctx, const char* str,
nk_panel_alloc_space(&bounds, ctx);
}

text_len = nk_strlen(str);
text.padding.x = style->text.padding.x;
text.padding.y = style->text.padding.y;
text.background = style->window.background;
text.text = color;
nk_widget_text(&win->buffer, bounds, str, nk_strlen(str), &text, alignment, style->font);

if (!nk_window_has_focus(ctx))
hover = nk_false;

if (hover && nk_input_is_mouse_hovering_rect(&ctx->input, bounds))
nk_tooltip(ctx, str);
}
nk_widget_text(&win->buffer, bounds, str, text_len, &text, alignment, style->font);

static inline void
nk_labelfv_hover(struct nk_context* ctx, nk_flags alignment, struct nk_color color, nk_bool hover, nk_bool space, const char* fmt, va_list args)
{
char buf[256];
nk_strfmt(buf, NK_LEN(buf), fmt, args);
nk_label_hover(ctx, buf, alignment, color, hover, space);
}
if (hover
&& ctx->current == ctx->active // only show tooltip if the window is active
&& !(win->popup.win && (win->popup.type & NK_PANEL_SET_NONBLOCK)) // make sure that no nonblocking popup is currently active
&& nk_input_is_mouse_hovering_rect(&ctx->input, bounds))
{
/* calculate size of the text and tooltip */
float text_width = style->font->width(style->font->userdata, style->font->height, str, text_len)
+ (4 * style->window.padding.x);
float text_height = (style->font->height + 2 * style->window.padding.y);

void
nk_labelf_hover(struct nk_context* ctx, nk_flags alignment, struct nk_color color, nk_bool hover, nk_bool space, const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
nk_labelfv_hover(ctx, alignment, color, hover, space, fmt, args);
va_end(args);
/* execute tooltip and fill with text */
if (nk_hover_begin(ctx, text_width))
{
nk_layout_row_dynamic(ctx, text_height, 1);
nk_text_colored(ctx, str, text_len, NK_TEXT_LEFT, color);
nk_hover_end(ctx);
}
}
}

nk_bool
Expand Down
1 change: 0 additions & 1 deletion nuklear/nuklear.h
Original file line number Diff line number Diff line change
Expand Up @@ -29574,7 +29574,6 @@ nk_tooltip_begin(struct nk_context *ctx, float width)
w = nk_iceilf(width);
h = nk_iceilf(nk_null_rect.h);
x = nk_ifloorf(in->mouse.pos.x + 1) - (int)win->layout->clip.x;
if (w + x > (int)win->bounds.w) x = (int)win->bounds.w - w;
y = nk_ifloorf(in->mouse.pos.y + 1) - (int)win->layout->clip.y;

bounds.x = (float)x;
Expand Down

0 comments on commit 829ec72

Please sign in to comment.