mirror of
https://codeberg.org/dnkl/yambar.git
synced 2025-06-16 08:15:40 +02:00
particle/progress-bar: allow user to configure an on-click handler
Since we're typically interrested in *where* (on the progress-bar) the user clicked, we need a way to pass the clicked position to the handler. Normally, the on-click handler is expanded when a particle instantiates its exposable. At this point, we (obviously) don't have the click position. This is solved by expanding the handler a second time, when the bar is clicked. Thus, the user can use the "{where}" tag in the click handler. "where" will be expanded to a percentage value (0-100).
This commit is contained in:
parent
bd365405d7
commit
1f182b862e
3 changed files with 60 additions and 5 deletions
|
@ -83,6 +83,51 @@ expose(const struct exposable *exposable, cairo_t *cr, int x, int y, int height)
|
|||
}
|
||||
}
|
||||
|
||||
static void
|
||||
on_mouse(struct exposable *exposable, struct bar *bar, enum mouse_event event,
|
||||
int x, int y)
|
||||
{
|
||||
if (exposable->on_click == NULL) {
|
||||
exposable_default_on_mouse(exposable, bar, event, x, y);
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* Hack-warning!
|
||||
*
|
||||
* In order to pass the *clicked* position to the on_click
|
||||
* handler, we expand the handler *again* (first time would be
|
||||
* when the particle instantiated us).
|
||||
*
|
||||
* We pass a single tag, "where", which is a percentage value.
|
||||
*
|
||||
* Keep a reference to the un-expanded string, to be able to reset
|
||||
* it after executing the handler.
|
||||
*/
|
||||
|
||||
char *original = exposable->on_click;
|
||||
|
||||
assert(x >= 0 && x < exposable->width);
|
||||
long where = exposable->width > 0
|
||||
? 100 * x / exposable->width
|
||||
: 0;
|
||||
|
||||
struct tag_set tags = {
|
||||
.tags = (struct tag *[]){tag_new_int(NULL, "where", where)},
|
||||
.count = 1,
|
||||
};
|
||||
|
||||
exposable->on_click = tags_expand_template(exposable->on_click, &tags);
|
||||
tag_set_destroy(&tags);
|
||||
|
||||
/* Call default implementation, which will execute our handler */
|
||||
exposable_default_on_mouse(exposable, bar, event, x, y);
|
||||
|
||||
/* Reset handler string */
|
||||
free(exposable->on_click);
|
||||
exposable->on_click = original;
|
||||
}
|
||||
|
||||
static struct exposable *
|
||||
instantiate(const struct particle *particle, const struct tag_set *tags)
|
||||
{
|
||||
|
@ -120,11 +165,16 @@ instantiate(const struct particle *particle, const struct tag_set *tags)
|
|||
|
||||
assert(idx == epriv->count);
|
||||
|
||||
struct exposable *exposable = exposable_common_new(particle, NULL);
|
||||
char *on_click = tags_expand_template(particle->on_click_template, tags);
|
||||
|
||||
struct exposable *exposable = exposable_common_new(particle, on_click);
|
||||
free(on_click);
|
||||
|
||||
exposable->private = epriv;
|
||||
exposable->destroy = &exposable_destroy;
|
||||
exposable->begin_expose = &begin_expose;
|
||||
exposable->expose = &expose;
|
||||
exposable->on_mouse = &on_mouse;
|
||||
|
||||
enum tag_realtime_unit rt = tag->realtime(tag);
|
||||
|
||||
|
@ -164,7 +214,8 @@ particle_progress_bar_new(const char *tag, int width,
|
|||
struct particle *end_marker,
|
||||
struct particle *fill, struct particle *empty,
|
||||
struct particle *indicator,
|
||||
int left_margin, int right_margin)
|
||||
int left_margin, int right_margin,
|
||||
const char *on_click_template)
|
||||
{
|
||||
struct private *priv = malloc(sizeof(*priv));
|
||||
priv->tag = strdup(tag);
|
||||
|
@ -175,7 +226,8 @@ particle_progress_bar_new(const char *tag, int width,
|
|||
priv->empty = empty;
|
||||
priv->indicator = indicator;
|
||||
|
||||
struct particle *particle = particle_common_new(left_margin, right_margin, NULL);
|
||||
struct particle *particle = particle_common_new(
|
||||
left_margin, right_margin, on_click_template);
|
||||
particle->private = priv;
|
||||
particle->destroy = &particle_destroy;
|
||||
particle->instantiate = &instantiate;
|
||||
|
|
|
@ -5,4 +5,4 @@ struct particle * particle_progress_bar_new(
|
|||
const char *tag, int width,
|
||||
struct particle *start_marker, struct particle *end_marker,
|
||||
struct particle *fill, struct particle *empty, struct particle *indicator,
|
||||
int left_margin, int right_margin);
|
||||
int left_margin, int right_margin, const char *on_click_template);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue