Skip to content

Commit

Permalink
Add support for positioning of context menues where mouse cursor is
Browse files Browse the repository at this point in the history
  • Loading branch information
andoma committed Apr 25, 2016
1 parent 718375d commit 66b0239
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 38 deletions.
5 changes: 4 additions & 1 deletion glwskins/flat/ctxmenu/default_details.view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ $view.itemBookmark <- lookup($core.bookmarks.queries, $args.url);
}

widget(popup, {
.screenPositionX ?= $clone.popupScreenX;
.screenPositionY ?= $clone.popupScreenY;

clickable: true;
onEvent(click, {
Expand Down Expand Up @@ -80,7 +82,8 @@ widget(popup, {
height: 0.1em;
});

ITEM(_("Bookmarked"), {
ITEM(select($view.itemBookmark.model.subtype == "bookmark",
_("Bookmarked"), _("Bookmark")), {
fireEvent(deliverRef($core.bookmarks.eventSink, $args));
$clone.itemMenu = void;
}, select($view.itemBookmark.model.subtype == "bookmark",
Expand Down
11 changes: 6 additions & 5 deletions glwskins/flat/ctxmenu/home_details.view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#import "skin:https://theme.view"
#import "skin:https://popups/common.view"



Expand Down Expand Up @@ -27,19 +28,19 @@

widget(popup, {

.screenPositionX ?= $clone.popupScreenX;
.screenPositionY ?= $clone.popupScreenY;

clickable: true;
onEvent(click, {
$clone.itemMenu = void;
}, true, true, true);

widget(container_z, {
widget(quad, {
alpha: 0.7;
color: 0;
});
PopupBackdrop();

widget(container_y, {
padding:[0.1em, 0.5em];
padding: 0.5em;

widget(container_x, {
padding:[1em, 0.1em];
Expand Down
8 changes: 5 additions & 3 deletions glwskins/flat/pages/directory.view
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@

onEvent(videoInfo, {
$clone.itemData := $event;
$clone.itemData := currentEvent("prop");
$clone.itemMenu = "video";
});

onEvent(pluginInfo, {
$clone.itemData := $event;
$clone.itemData := currentEvent("prop");
$clone.itemMenu = "plugin";
});

onEvent(defaultInfo, {
$clone.itemData := $event;
$clone.itemData := currentEvent("prop");
$clone.popupScreenX = currentEvent("screenX");
$clone.popupScreenY = currentEvent("screenY");
$clone.itemMenu = "default";
});

Expand Down
6 changes: 4 additions & 2 deletions glwskins/flat/pages/home.view
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "skin:https://theme.view"

onEvent(homeInfo, {
$clone.itemData := $event;
$clone.itemData := currentEvent("prop");
$clone.popupScreenX = currentEvent("screenX");
$clone.popupScreenY = currentEvent("screenY");
$clone.itemMenu = "home";
});

Expand Down Expand Up @@ -122,7 +124,7 @@ widget(container_z, {

widget(container_z, {

alpha: iir(select($clone.itemMenu, 0.3, 1), 4);
alpha: iir(select($clone.itemMenu, 0.4, 1), 4);

widget(array, {
id: "main";
Expand Down
10 changes: 7 additions & 3 deletions src/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ event_create(event_type_t type, size_t size)
e->e_timestamp = arch_get_ts();
e->e_nav = NULL;
e->e_dtor = NULL;
e->e_clone = NULL;
atomic_set(&e->e_refcount, 1);
e->e_flags = 0;
e->e_type = type;
Expand Down Expand Up @@ -76,10 +77,12 @@ event_create_int3(event_type_t type, int v1, int v2, int v3)
/**
*
*/
void
event_t *
event_addref(event_t *e)
{
atomic_inc(&e->e_refcount);
if(e != NULL)
atomic_inc(&e->e_refcount);
return e;
}


Expand All @@ -89,6 +92,8 @@ event_addref(event_t *e)
void
event_release(event_t *e)
{
if(e == NULL)
return;
if(atomic_dec(&e->e_refcount))
return;
if(e->e_dtor != NULL)
Expand Down Expand Up @@ -229,7 +234,6 @@ event_t *
event_clone(const event_t *src)
{
if(src->e_clone == NULL) {
fprintf(stderr, "Event %s is not clonable\n", event_sprint(src));
return NULL;
}
return src->e_clone(src);
Expand Down
2 changes: 1 addition & 1 deletion src/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ void *event_create_int3(event_type_t type, int val1, int val2, int val3);

void event_release(event_t *e);

void event_addref(event_t *e);
event_t *event_addref(event_t *e); // Rename to event_retain()

event_t *event_create_str(event_type_t et, const char *url);

Expand Down
6 changes: 3 additions & 3 deletions src/ui/glw/glw.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,15 +272,15 @@ typedef struct glw_scope {
int gs_num_roots;

struct backend *gs_backend;
struct event *gs_event; // Captured event that trigged code

#define GLW_ROOT_SELF 0
#define GLW_ROOT_PARENT 1
#define GLW_ROOT_VIEW 2
#define GLW_ROOT_ARGS 3
#define GLW_ROOT_CLONE 4
#define GLW_ROOT_EVENT 5
#define GLW_ROOT_CORE 6
#define GLW_ROOT_static 7
#define GLW_ROOT_CORE 5
#define GLW_ROOT_static 6

prop_root_t gs_roots[GLW_ROOT_static];

Expand Down
2 changes: 1 addition & 1 deletion src/ui/glw/glw_event.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ typedef struct glw_event_external {
static void
glw_event_map_send_to_widget(glw_t *w, event_t *src, event_t *origin)
{
event_t *clone = origin ? event_clone(src) : NULL;
event_t *clone = src ? event_clone(src) : NULL;
if(clone == NULL) {
glw_event_to_widget(w, src);
return;
Expand Down
65 changes: 62 additions & 3 deletions src/ui/glw/glw_popup.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ typedef struct glw_popup {
int16_t width;
int16_t height;

float screen_x;
float screen_y;

int screen_cord_set;

} glw_popup_t;

/**
Expand Down Expand Up @@ -82,23 +87,77 @@ popup_render(glw_t *w, const glw_rctx_t *rc)
if(c == NULL || c->glw_flags & GLW_HIDDEN)
return;

int x1 = rc->rc_width / 2 - p->width / 2;
int x2 = x1 + p->width;
Vec3 point, dir;

glw_vec3_copy(point, glw_vec3_make(p->screen_x, p->screen_y, -2.41));
glw_vec3_sub(dir, point, glw_vec3_make(p->screen_x * 42.38,
p->screen_y * 42.38,
-100));
float x, y;

glw_widget_unproject(w->glw_matrix, &x, &y, point, dir);

int x1, y1;

int y1 = rc->rc_height / 2 - p->height / 2;
if(p->screen_cord_set) {
x1 = (x + 1.0f) * 0.5f * rc->rc_width;
y1 = (y + 1.0f) * 0.5f * rc->rc_height - p->height;
} else {
x1 = rc->rc_width / 2 - p->width / 2;
y1 = rc->rc_height / 2 - p->height / 2;
}

int x2 = x1 + p->width;
int y2 = y1 + p->height;


if(x2 > rc->rc_width) {
int spill = x2 - rc->rc_width;
x1 -= spill;
x2 -= spill;
}

if(y1 < 0) {
y2 -= y1;
y1 -= y1;
}
glw_reposition(&rc0, x1, y2, x2, y1);

glw_render0(c, &rc0);
}


/**
*
*/
static int
glw_popup_set_float_unresolved(glw_t *w, const char *a, float value,
glw_style_t *gs)
{
glw_popup_t *p = (glw_popup_t *)w;

if(!strcmp(a, "screenPositionX")) {
p->screen_x = value;
p->screen_cord_set = 1;
return GLW_SET_RERENDER_REQUIRED;
}

if(!strcmp(a, "screenPositionY")) {
p->screen_y = value;
p->screen_cord_set = 1;
return GLW_SET_RERENDER_REQUIRED;
}

return GLW_SET_NOT_RESPONDING;
}


static glw_class_t glw_popup = {
.gc_name = "popup",
.gc_instance_size = sizeof(glw_popup_t),
.gc_layout = popup_layout,
.gc_render = popup_render,
.gc_set_float_unresolved = glw_popup_set_float_unresolved,
};

GLW_REGISTER_CLASS(glw_popup);
Expand Down
4 changes: 3 additions & 1 deletion src/ui/glw/glw_scope.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ glw_scope_dup(const glw_scope_t *src, int retain_mask)

memcpy(o, src, sizeof(glw_scope_t));
o->gs_backend = backend_retain(src->gs_backend);
o->gs_event = event_addref(src->gs_event);

for(int i = 0; i < src->gs_num_roots; i++) {
if(!((1 << i) & retain_mask))
Expand All @@ -55,7 +56,6 @@ glw_scope_create(void)
o->gs_roots[GLW_ROOT_VIEW].name = "view";
o->gs_roots[GLW_ROOT_ARGS].name = "args";
o->gs_roots[GLW_ROOT_CLONE].name = "clone";
o->gs_roots[GLW_ROOT_EVENT].name = "event";
o->gs_roots[GLW_ROOT_CORE].name = "core";
o->gs_num_roots = GLW_ROOT_static;
o->gs_refcount = 1;
Expand All @@ -72,6 +72,8 @@ glw_scope_release(glw_scope_t *gs)

backend_release(gs->gs_backend);

event_release(gs->gs_event);

for(int i = 0; i < gs->gs_num_roots; i++)
prop_ref_dec(gs->gs_roots[i].p);
free(gs);
Expand Down
1 change: 0 additions & 1 deletion src/ui/glw/glw_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,6 @@ typedef struct glw_view_eval_context {
token_t *rpn;
struct glw_prop_sub_slist *sublist;
struct glw_prop_sub_slist sublist_rpnlocal;
struct event *event;
prop_t *tgtprop;

uint16_t dynamic_eval;
Expand Down
56 changes: 42 additions & 14 deletions src/ui/glw/glw_view_eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,8 @@ token_resolve_ex(glw_view_eval_context_t *ec, token_t *t, int type)
return NULL;
}

if((t->type == TOKEN_PROPERTY_NAME ||
t->type == TOKEN_PROPERTY_REF) && subscribe_prop(ec, t, type))
if((t->type == TOKEN_PROPERTY_NAME || t->type == TOKEN_PROPERTY_REF) &&
subscribe_prop(ec, t, type))
return NULL;

if(t->type != TOKEN_PROPERTY_SUBSCRIPTION)
Expand Down Expand Up @@ -3279,23 +3279,15 @@ glw_event_map_eval_block_fire(glw_t *w, glw_event_map_t *gem, event_t *src)
struct glw_prop_sub_slist l;

SLIST_INIT(&l);

memset(&n, 0, sizeof(n));

if(src != NULL && src->e_type == EVENT_PROP_ACTION) {
event_prop_action_t *epa = (event_prop_action_t *)src;
n.scope = glw_scope_dup(b->capture.scope, (1 << GLW_ROOT_EVENT));
n.scope->gs_roots[GLW_ROOT_EVENT].p = prop_ref_inc(epa->p);
} else {
n.scope = glw_scope_retain(b->capture.scope);
}

n.scope = glw_scope_dup(b->capture.scope, 0);
event_release(n.scope->gs_event);
n.scope->gs_event = event_addref(src);

n.gr = w->glw_root;
n.w = w;
n.passive_subscriptions = 1;
n.sublist = &l;
n.event = src;

body = glw_view_clone_chain(n.gr, b->capture.block, NULL);
glw_view_eval_static_block(body, &n);
Expand Down Expand Up @@ -3845,6 +3837,42 @@ glwf_fireEvent(glw_view_eval_context_t *ec, struct token *self,
}



/**
*
*/
static int
glwf_currentEvent(glw_view_eval_context_t *ec, struct token *self,
token_t **argv, unsigned int argc)
{
const char *s = token_as_string(argv[0]);
token_t *r = NULL;
event_t *e = ec->scope->gs_event;
if(s != NULL && e != NULL) {
if(!strcmp(s, "screenX") && e->e_flags & EVENT_SCREEN_POSITION) {
r = eval_alloc(self, ec, TOKEN_FLOAT);
r->t_float = e->e_screen_x;
} else if(!strcmp(s, "screenY") && e->e_flags & EVENT_SCREEN_POSITION) {
r = eval_alloc(self, ec, TOKEN_FLOAT);
r->t_float = e->e_screen_y;
} else if(!strcmp(s, "prop") && e->e_type == EVENT_PROP_ACTION) {
event_prop_action_t *epa = (event_prop_action_t *)e;
r = eval_alloc(self, ec, TOKEN_PROPERTY_REF);
r->t_prop = prop_ref_inc(epa->p);
}

}

if(r == NULL)
r = eval_alloc(self, ec, TOKEN_VOID);

eval_push(ec, r);
return 0;

}



/**
*
*/
Expand Down Expand Up @@ -7125,7 +7153,7 @@ static const token_func_t funcvec[] = {
{"targetedEvent", 2, glwf_targetedEvent},
{"deliverEvent", -1, glwf_deliverEvent},
{"deliverRef", 2, glwf_deliverRef},

{"currentEvent", 1, glwf_currentEvent},

{"onInactivity", 2, glwf_onInactivity, glwf_null_ctor,glwf_onInactivity_dtor},
{"changed", -1, glwf_changed, glwf_changed_ctor, glwf_changed_dtor},
Expand Down

0 comments on commit 66b0239

Please sign in to comment.