From c7400a081153b5f5e5a384895f6840e2d9262cae Mon Sep 17 00:00:00 2001 From: cancel Date: Mon, 6 Jan 2020 03:31:51 +0900 Subject: [PATCH] Clean up some qnav layout logic --- term_util.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/term_util.c b/term_util.c index 6f2d200..fe4034b 100644 --- a/term_util.c +++ b/term_util.c @@ -110,25 +110,30 @@ void qnav_stack_push(Qblock* qb, int height, int width) { } #endif int top = 0, left = 0; - int totalheight = height + 2, totalwidth = width + 3; + int total_h = height + 2, total_w = width + 3; if (qnav_stack.count > 0) { WINDOW* w = qnav_stack.blocks[qnav_stack.count - 1]->outer_window; int prev_y, prev_x, prev_h, prev_w; getbegyx(w, prev_y, prev_x); getmaxyx(w, prev_h, prev_w); + // Start by trying to position the item to the right of the previous item. left = prev_x + prev_w + 0; int term_h, term_w; getmaxyx(stdscr, term_h, term_w); // Check if we'll run out of room if we position the new item to the right // of the existing item (with the same Y position.) - if (left + totalwidth > term_w) { + if (left + total_w > term_w) { // If we have enough room if we position just below the previous item in // the stack, do that instead of positioning to the right of it. - if (prev_x + totalwidth <= term_w && - totalheight < term_h - (prev_y + prev_h)) { + if (prev_x + total_w <= term_w && total_h < term_h - (prev_y + prev_h)) { top = prev_y + prev_h; left = prev_x; } + // If the item doesn't fit there, but it's less wide than the terminal, + // right-align it to the edge of the terminal. + else if (total_w < term_w) { + left = term_w - total_w; + } // Otherwise, just start the layout over at Y=0,X=0 else { left = 0; @@ -137,7 +142,7 @@ void qnav_stack_push(Qblock* qb, int height, int width) { } qnav_stack.blocks[qnav_stack.count] = qb; ++qnav_stack.count; - qb->outer_window = newwin(totalheight, totalwidth, top, left); + qb->outer_window = newwin(total_h, total_w, top, left); qb->content_window = derwin(qb->outer_window, height, width, 1, 1); qnav_stack.stack_changed = true; }