From 92644f43eb6a75e4d6c9c624fae0850dfd6470d9 Mon Sep 17 00:00:00 2001 From: NeilBrown Date: Sat, 2 Sep 2023 20:32:08 +1000 Subject: [PATCH] pane: add option arg to pane_has_focus() A second arg to pane_has_focus checks is the target pane as the focus from the given root. This is occasionally useful. Signed-off-by: NeilBrown --- core-pane.c | 11 +++++++++-- core.h | 5 ++++- lang-python.c | 11 +++++++++-- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/core-pane.c b/core-pane.c index 6a643e0f..5957054f 100644 --- a/core-pane.c +++ b/core-pane.c @@ -797,14 +797,21 @@ void pane_focus(struct pane *focus) call("pane:refocus", focus); } -bool pane_has_focus(struct pane *focus) +bool do_pane_has_focus(struct pane *focus, struct pane *root) { /* Would pane_focus change anything */ struct pane *p = focus; if (!p) return False; - for (; p->parent->parent->parent != p->parent->parent; p = p->parent) + /* We check down to the declared root, or to on1 above + * the global root. Where the focus of the global root + * is never matters. + */ + for (; + p != root + && p->parent->parent->parent != p->parent->parent; + p = p->parent) if (p->parent->focus != p) return False; return True; diff --git a/core.h b/core.h index 8987d3d4..43ff81a7 100644 --- a/core.h +++ b/core.h @@ -473,7 +473,10 @@ void pane_subsume(struct pane *p safe, struct pane *parent safe); void pane_close(struct pane *p safe); bool pane_resize(struct pane *p safe, int x, int y, int w, int h); void pane_focus(struct pane *p); -bool pane_has_focus(struct pane *p); +bool do_pane_has_focus(struct pane *p, struct pane *root); +#define pane_has_focus(...) VFUNC(pane_has_focus, __VA_ARGS__) +#define pane_has_focus1(p) do_pane_has_focus(p, NULL) +#define pane_has_focus2(p,r) do_pane_has_focus(p, r) void pane_damaged(struct pane *p, int type); void pane_clone_children(struct pane *from, struct pane *to); struct pane *pane_my_child(struct pane *p, struct pane *c); diff --git a/lang-python.c b/lang-python.c index 23fa8ae5..3217cd22 100644 --- a/lang-python.c +++ b/lang-python.c @@ -877,10 +877,17 @@ static PyObject *Pane_focus(Pane *self safe, PyObject *args) static PyObject *Pane_has_focus(Pane *self safe, PyObject *args) { + Pane *other = NULL; + int ret; + if (!pane_valid(self)) return NULL; - if (pane_has_focus(self->pane)) { + ret = PyArg_ParseTuple(args, "|O!", &PaneType, &other); + if (ret <= 0) + return NULL; + + if (pane_has_focus(self->pane, other ? other->pane : NULL)) { Py_INCREF(Py_True); return Py_True; } else { @@ -1504,7 +1511,7 @@ static const PyMethodDef pane_methods[] = { "Clone all children onto the target"}, {"take_focus", (PyCFunction)Pane_focus, METH_NOARGS, "Claim the focus for this pane"}, - {"has_focus", (PyCFunction)Pane_has_focus, METH_NOARGS, + {"has_focus", (PyCFunction)Pane_has_focus, METH_VARARGS, "Check if pane is focus of display"}, {"call", (void*)(PyCFunctionWithKeywords)Pane_call, METH_VARARGS|METH_KEYWORDS, "Call a command from a pane"}, -- 2.39.5