summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJari Vetoniemi <mailroxas@gmail.com>2015-01-18 04:23:18 +0200
committerJari Vetoniemi <mailroxas@gmail.com>2015-01-18 04:23:18 +0200
commitc04a2c41926fcdfc44c63736a0a9904bd43467f0 (patch)
treed229c77d4f7508aabced3f0e618085c046e0fcc7 /lib
parent92152cb5fb417eb81d687423eceda9f1884b1927 (diff)
downloadbemenu-c04a2c41926fcdfc44c63736a0a9904bd43467f0.tar.gz
bemenu-c04a2c41926fcdfc44c63736a0a9904bd43467f0.tar.bz2
bemenu-c04a2c41926fcdfc44c63736a0a9904bd43467f0.zip
Change scrollbar to represent content instead of index.
Diffstat (limited to 'lib')
-rw-r--r--lib/renderers/cairo.h10
-rw-r--r--lib/renderers/curses/curses.c20
2 files changed, 19 insertions, 11 deletions
diff --git a/lib/renderers/cairo.h b/lib/renderers/cairo.h
index 59f61d3..662f788 100644
--- a/lib/renderers/cairo.h
+++ b/lib/renderers/cairo.h
@@ -223,7 +223,8 @@ bm_cairo_paint(struct cairo *cairo, uint32_t width, uint32_t height, uint32_t ma
}
uint32_t posy = titleh;
- for (uint32_t l = 0, i = (menu->index / lines) * lines; l < lines && i < count && posy < max_height; ++i, ++l) {
+ const uint32_t page = (menu->index / lines) * lines;
+ for (uint32_t l = 0, i = page; l < lines && i < count && posy < max_height; ++i, ++l) {
bool highlighted = (items[i] == bm_menu_get_highlighted_item(menu));
if (highlighted) {
@@ -261,10 +262,11 @@ bm_cairo_paint(struct cairo *cairo, uint32_t width, uint32_t height, uint32_t ma
cairo_rectangle(cairo->cr, 0, titleh, 2, sheight);
cairo_fill(cairo->cr);
- const uint32_t size = sheight / lines;
- const uint32_t percent = (menu->index / (float)(count - 1)) * (sheight - size);
+ const float percent = fmin(((float)page / (count - lines)), 1.0f);
+ const uint32_t size = fmax(sheight * ((float)lines / count), 2.0f);
+ const uint32_t posy = percent * (sheight - size);
cairo_set_source_rgba(cairo->cr, paint.fg.r, paint.fg.b, paint.fg.g, paint.fg.a);
- cairo_rectangle(cairo->cr, 0, titleh + percent, 2, size);
+ cairo_rectangle(cairo->cr, 0, titleh + posy, 2, size);
cairo_fill(cairo->cr);
}
} else {
diff --git a/lib/renderers/curses/curses.c b/lib/renderers/curses/curses.c
index 1f81775..756091b 100644
--- a/lib/renderers/curses/curses.c
+++ b/lib/renderers/curses/curses.c
@@ -9,6 +9,7 @@
#include <locale.h>
#include <dlfcn.h>
#include <assert.h>
+#include <math.h>
#include <ncurses.h>
@@ -184,22 +185,24 @@ render(const struct bm_menu *menu)
attroff(COLOR_PAIR(1));
}
- uint32_t count, cl = 1;
- const uint32_t lines = getmaxy(curses.stdscr);
+ uint32_t count, cl = 0;
+ const uint32_t lines = fmax(getmaxy(curses.stdscr), 1) - 1;
if (lines > 1) {
uint32_t displayed = 0;
struct bm_item **items = bm_menu_get_filtered_items(menu, &count);
const bool scrollbar = (menu->scrollbar > BM_SCROLLBAR_NONE && (menu->scrollbar != BM_SCROLLBAR_AUTOHIDE || count > lines) ? true : false);
const int32_t offset_x = (scrollbar ? 2 : 0);
const int32_t prefix_x = (menu->prefix ? bm_utf8_string_screen_width(menu->prefix) : 0);
- for (uint32_t i = (menu->index / (lines - 1)) * (lines - 1); i < count && cl < lines; ++i) {
+
+ const uint32_t page = menu->index / lines * lines;
+ for (uint32_t i = page; i < count && cl < lines; ++i) {
bool highlighted = (items[i] == bm_menu_get_highlighted_item(menu));
int32_t color = (highlighted ? 2 : (bm_menu_item_is_selected(menu, items[i]) ? 1 : 0));
if (menu->prefix && highlighted) {
- draw_line(color, cl++, "%*s%s %s", offset_x, "", menu->prefix, (items[i]->text ? items[i]->text : ""));
+ draw_line(color, 1 + cl++, "%*s%s %s", offset_x, "", menu->prefix, (items[i]->text ? items[i]->text : ""));
} else {
- draw_line(color, cl++, "%*s%s%s", offset_x + prefix_x, "", (menu->prefix ? " " : ""), (items[i]->text ? items[i]->text : ""));
+ draw_line(color, 1 + cl++, "%*s%s%s", offset_x + prefix_x, "", (menu->prefix ? " " : ""), (items[i]->text ? items[i]->text : ""));
}
++displayed;
@@ -207,8 +210,11 @@ render(const struct bm_menu *menu)
if (scrollbar) {
attron(COLOR_PAIR(1));
- uint32_t percent = (menu->index / (float)(count - 1)) * (displayed - 1);
- mvprintw(1 + percent, 0, "▒");
+ const float percent = fmin(((float)page / (count - lines)), 1.0f);
+ const uint32_t size = fmax(lines * ((float)lines / count), 1.0f);
+ const uint32_t posy = percent * (lines - size);
+ for (uint32_t i = 0; i < size; ++i)
+ mvprintw(1 + posy + i, 0, "▒");
attroff(COLOR_PAIR(1));
}
}