[PATCH] cwm window center functionality

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[PATCH] cwm window center functionality

Charles A Daniels
I've tweaked calmwm to add a function which centers the current window, and
resides it to be a fraction of the current screen size. I've been
running this patch for a while locally and have found it to be useful,
and have not noticed any side effects.

This feature is especially useful when switching monitor configurations
(i.e. docking / undocking a laptop), as it provides a way to force the
active window into a known position and size where it can easily be
located and moved / resized as needed. To my knowledge, no other
function really fulfills this purpose currently.

I am happy to tweak this code as needed based on feedback / testing.

~ Charles


--- calmwm.h.orig Tue Apr 30 23:43:40 2019
+++ calmwm.h Tue Apr 30 23:51:50 2019
@@ -396,6 +396,7 @@ void client_applysizehints(struct client_ctx *);
 void client_config(struct client_ctx *);
 struct client_ctx *client_current(struct screen_ctx *);
 void client_cycle(struct screen_ctx *, int);
+void client_center(struct client_ctx *);
 void client_remove(struct client_ctx *);
 void client_draw_border(struct client_ctx *);
 struct client_ctx *client_find(Window);
@@ -489,6 +490,7 @@ void screen_prop_win_draw(struct screen_ctx *,
 void kbfunc_cwm_status(void *, struct cargs *);
 void kbfunc_ptrmove(void *, struct cargs *);
 void kbfunc_client_snap(void *, struct cargs *);
+void kbfunc_client_center(void *, struct cargs *);
 void kbfunc_client_move(void *, struct cargs *);
 void kbfunc_client_resize(void *, struct cargs *);
 void kbfunc_client_close(void *, struct cargs *);
--- client.c.orig Tue Apr 30 23:45:04 2019
+++ client.c Tue Apr 30 23:53:10 2019
@@ -640,6 +640,31 @@ client_close(struct client_ctx *cc)
 }
 
 void
+client_center(struct client_ctx *cc)
+{
+
+ struct screen_ctx *sc = cc->sc;
+ struct geom area;
+
+ if (cc->flags & CLIENT_FREEZE)
+ return;
+
+ area = screen_area(sc,
+    cc->geom.x + cc->geom.w / 2,
+    cc->geom.y + cc->geom.h / 2, CWM_GAP);
+
+ cc->geom.w = area.w / 2;
+ cc->geom.h = area.h / 2;
+ cc->geom.x = area.x + area.w * 0.25;
+ cc->geom.y = area.y + area.h * 0.25;
+ client_applysizehints(cc);
+ client_resize(cc, 1);
+
+ /* Make sure the pointer stays within the window. */
+ client_ptr_inbound(cc, 0);
+}
+
+void
 client_setname(struct client_ctx *cc)
 {
  struct winname *wn;
--- conf.c.orig Tue Apr 30 23:45:04 2019
+++ conf.c Tue Apr 30 23:53:09 2019
@@ -110,6 +110,7 @@ static const struct {
  { FUNC_CC(window-snap-up-left, client_snap, (CWM_UP_LEFT)) },
  { FUNC_CC(window-snap-down-right, client_snap, (CWM_DOWN_RIGHT)) },
  { FUNC_CC(window-snap-down-left, client_snap, (CWM_DOWN_LEFT)) },
+ { FUNC_CC(window-center, client_center, 0) },
  { FUNC_CC(window-move, client_move, 0) },
  { FUNC_CC(window-move-up, client_move, (CWM_UP)) },
  { FUNC_CC(window-move-down, client_move, (CWM_DOWN)) },
@@ -258,6 +259,7 @@ static const struct {
  { "CMS-j", "window-resize-down-big" },
  { "CMS-k", "window-resize-up-big" },
  { "CMS-l", "window-resize-right-big" },
+ { "MS-space", "window-center" },
 },
 mouse_binds[] = {
  { "1", "menu-window" },
--- kbfunc.c.orig Tue Apr 30 23:45:04 2019
+++ kbfunc.c Tue Apr 30 23:51:01 2019
@@ -287,6 +287,12 @@ kbfunc_client_resize_mb(void *ctx, struct cargs *cargs
 }
 
 void
+kbfunc_client_center (void *ctx, struct cargs *cargs)
+{
+ client_center(ctx);
+}
+
+void
 kbfunc_client_snap(void *ctx, struct cargs *cargs)
 {
  struct client_ctx *cc = ctx;


Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] cwm window center functionality

Joe Davis
I'd prefer if centering were a seperate function to resizing to a
fraction of the screen size, as I'd like to be able to center a window
*after* resizing it to my liking.

x11/xdotool could be used by anybody needing the resizing functionality,
for example,

    bind-key 4-r "xdotool getactivewindow windowsize 50% 50%"

The fraction of the screen taken up would then be configurable, without
having to add another option.

Cheers,
Joe

Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] cwm window center functionality

Charles A Daniels
In reply to this post by Charles A Daniels
> I'd prefer if centering were a seperate function to resizing to a
> fraction of the screen size, as I'd like to be able to center a window
> *after* resizing it to my liking.

>  x11/xdotool could be used by anybody needing the resizing functionality,
>  for example,

>      bind-key 4-r "xdotool getactivewindow windowsize 50% 50%"
>
>  The fraction of the screen taken up would then be configurable, without
>  having to add another option.
>
>  Cheers,
>  Joe

This functionality actually actually started life as an xdotool script. However
I thought it would be more elegant to implement it in CWM itself.

I will concede that re-sizing should perhaps live elsewhere (separation of
concerns), either as an option/argument, as a separate function/binding, or via
external scripts such as xdotool.

I believe the sections of the patch that would need to be removed to disable
re-sizing are obvious, but I am happy to remove them and re-submit if it's more
convenient.

Would it be useful to add separate bind-able function which resides a window
either to an absolute size or to a fraction of screen size?

Further thoughts, feedback, etc?

~ Charles