SWOv git · main
SWOv - window overview for sway
C 97.3% Markdown 2.4%git clone https://git.christianimmanuel.de/sway/SWOv.gitwget https://git.christianimmanuel.de/sway/SWOv/archive/SWOv.tar.gzDont close after moving app
commit_message.txt | 18 ++++++++---------- sw_draw.h | 15 +++++++++++++++ swov.c | 27 ++++++++++++++++++++++----- 3 files changed, 45 insertions(+), 15 deletions(-) diff --git a/commit_message.txt b/commit_message.txt index b6fbb3e..d201d33 100644 --- a/commit_message.txt +++ b/commit_message.txt @@ -1,15 +1,13 @@ -sw_draw: size a font by its em square, not by ascent-to-descent +sw_draw: sw_font_h(), a font sized by its line height -sw_font(px) asked stb_truetype for ScaleForPixelHeight, which makes the -ascent-to-descent span px pixels tall. Every other toolkit — FreeType's -set_pixel_sizes, SDL_ttf's TTF_OpenFont, CSS — means the em square by a font -size. The gap is the font's line height over its em, 1.21 for Inter and -about 1.16 for DejaVu, so text came out roughly a fifth smaller than the -number asked for. It is ScaleForMappingEmToPixels now. +sw_font(px) means the em square, which is what a font size is everywhere +else. Some callers do not have a font size, they have a box: the wheel in +swas places a label at cy - height/2 and wants text that tall. For those, +sw_font_h(px) returns the face whose line height is about px, found by +measuring one and scaling. -Note for swbr, which still has its own copy of this code: rebuilding it -against this header makes its text that much larger, so its px values want -lowering by about a sixth to keep the bar looking the way it does. +Both are now available and neither guesses on the other's behalf: ask for a +size with sw_font, for a height with sw_font_h. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SuAfijAAYjLpcVwhCq3AF4 diff --git a/sw_draw.h b/sw_draw.h index 458796d..395fd4d 100644 --- a/sw_draw.h +++ b/sw_draw.h @@ -96,6 +96,9 @@ Font *sw_font(int px); /* cached per pixel size */ face by drawing each glyph twice, a third of a pixel apart. */ void sw_fonts_bold(const char *path); Font *sw_font_bold(int px); +/* A font whose line height is about px tall, for callers that size text by + the box it has to fill rather than by the font size. */ +Font *sw_font_h(int px); float sw_text_width(Font *f, const char *s); void sw_canvas_clip(Canvas *cv, int x0, int y0, int x1, int y1); @@ -451,6 +454,18 @@ static Font *sw_font_get(int px, bool bold) Font *sw_font(int px) { return sw_font_get(px, false); } Font *sw_font_bold(int px) { return sw_font_get(px, true); } +Font *sw_font_h(int px) +{ + Font *f; + int want; + if (px < 4) px = 4; + f = sw_font_get(px, false); + if (f->lineh <= 0.0f) return f; + want = (int)((float)px * (float)f->px / f->lineh + 0.5f); + if (want < 4) want = 4; + return want == f->px ? f : sw_font_get(want, false); +} + static unsigned sw_utf8_next(const char **p) { const unsigned char *s = (const unsigned char *)*p; diff --git a/swov.c b/swov.c index 5142d94..c894519 100644 --- a/swov.c +++ b/swov.c @@ -121,7 +121,7 @@ static uint64_t now_ms_(void) } #define APP_ID "swov" -#define SWOV_VERSION "1.3.3" +#define SWOV_VERSION "1.3.4" #ifndef SWOV_BUILD /* set by the Makefile: md5 of this file */ #define SWOV_BUILD "unknown" #endif @@ -958,9 +958,17 @@ static JV *sway_query(uint32_t type) return v; } +/* When sway was last told to do something by us. Moving or swapping a window + * makes sway focus it, which takes the keyboard off this overlay for a moment + * — and quitting on that is what closed the overview after a single + * rearrangement. Focus lost right after one of our own commands is our own + * doing, not the user leaving. */ +static uint64_t SELF_CMD_AT; + static bool sway_cmd(const char *fmt, ...) { if (!fmt) return false; + SELF_CMD_AT = now_ms_(); va_list ap; va_start(ap, fmt); int n = vsnprintf(NULL, 0, fmt, ap); @@ -6831,10 +6839,19 @@ static void handle_event(const SwEvent *e) case SW_EV_KB_LEAVE: /* Losing the keyboard normally means the overview's moment has - * passed. Not while we are the backdrop for a launcher we started: - * that launcher taking the focus is the whole point, and quitting - * here is what made the overview vanish the instant swas appeared. */ - if (C.quit_on_focus_loss && !SERVING && !BACKDROP) running = false; + * passed. Three times it does not: while we are the backdrop for a + * launcher we started, where that launcher taking the focus is the + * whole point; and in the moment after we moved a window ourselves, + * where sway focuses what it moved and hands the keyboard straight + * back. Going to a window or a workspace on purpose sets `running` + * itself, so nothing here is needed for those. */ + if (!C.quit_on_focus_loss || SERVING || BACKDROP) break; + if (SELF_CMD_AT && now_ms_() - SELF_CMD_AT < 1000) { + /* ask for it back, in case sway does not offer */ + sw_win_keyboard(WIN, SW_KBD_EXCLUSIVE); + break; + } + running = false; break; default: