commit b8f2762e5d7b778ba858d790f40c85aeab2f57ab
parent 991419d13fe4ec01c9898681cd5d9ab708486ed8
Author: lumidify <nobody@lumidify.org>
Date: Sat, 22 May 2021 18:11:16 +0200
Add text width to eliminate some magic numbers
Diffstat:
M | buffer.c | | | 4 | ++-- |
M | common.h | | | 2 | ++ |
M | ledit.c | | | 74 | ++++++++++++++++++++++++++++++++++++++++++++++---------------------------- |
3 files changed, 50 insertions(+), 30 deletions(-)
diff --git a/buffer.c b/buffer.c
@@ -189,7 +189,7 @@ static void
init_line(ledit_buffer *buffer, ledit_line *line) {
line->parent_buffer = buffer;
line->layout = pango_layout_new(buffer->state->context);
- pango_layout_set_width(line->layout, (buffer->state->w - 10) * PANGO_SCALE);
+ pango_layout_set_width(line->layout, (buffer->state->text_w) * PANGO_SCALE);
pango_layout_set_font_description(line->layout, buffer->state->font);
pango_layout_set_wrap(line->layout, PANGO_WRAP_WORD_CHAR);
pango_layout_set_attributes(line->layout, basic_attrs);
@@ -199,7 +199,7 @@ init_line(ledit_buffer *buffer, ledit_line *line) {
line->dirty = 1;
/* FIXME: does this set line height reasonably when no text yet? */
pango_layout_get_pixel_size(line->layout, &line->w, &line->h);
- line->w = buffer->state->w - 10;
+ line->w = buffer->state->text_w;
line->y_offset = 0;
}
diff --git a/common.h b/common.h
@@ -19,6 +19,8 @@ typedef struct {
int depth;
int w;
int h;
+ int text_w;
+ int text_h;
int scroll_dragging;
int scroll_grab_handle;
int selecting;
diff --git a/ledit.c b/ledit.c
@@ -1,3 +1,4 @@
+/* FIXME: Fix lag when scrolling */
/* FIXME: Fix lag when selecting with mouse */
/* FIXME: Use PANGO_PIXELS() */
/* FIXME: Fix cursor movement, especially buffer->trailing and writing at end of line */
@@ -138,6 +139,16 @@ static void get_new_line_softline(
int *new_line_ret, int *new_softline_ret
);
+#define SCROLLBAR_WIDTH 10
+#define SCROLL_STEP 10
+
+static void
+recalc_text_size(void) {
+ int bar_h = bottom_bar.mode_h;
+ state.text_w = state.w - SCROLLBAR_WIDTH;
+ state.text_h = state.h - bar_h;
+}
+
/* clipboard handling largely stolen from st (simple terminal) */
#define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
@@ -170,6 +181,7 @@ set_mode(enum ledit_mode mode) {
ledit_grow_draw(&state, bottom_bar.mode_draw, bottom_bar.mode_w, bottom_bar.mode_h);
XftDrawRect(bottom_bar.mode_draw->xftdraw, &state.bg, 0, 0, bottom_bar.mode_w, bottom_bar.mode_h);
pango_xft_render_layout(bottom_bar.mode_draw->xftdraw, &state.fg, bottom_bar.mode, 0, 0);
+ recalc_text_size(); /* probably not necessary, but whatever */
}
void
@@ -612,18 +624,18 @@ clear_key_stack(void) {
static void
get_scroll_pos_height(double *pos, double *height) {
- *height = ((double)state.h / buffer->total_height) * state.h;
+ *height = ((double)state.text_h / buffer->total_height) * state.text_h;
*pos = (buffer->display_offset /
- (buffer->total_height - state.h)) * (state.h - *height);
+ (buffer->total_height - state.text_h)) * (state.text_h - *height);
}
static void
set_scroll_pos(double pos) {
- buffer->display_offset = pos * (buffer->total_height / (double)state.h);
+ buffer->display_offset = pos * (buffer->total_height / (double)state.text_h);
if (buffer->display_offset < 0)
buffer->display_offset = 0;
- if (buffer->display_offset + state.h > buffer->total_height)
- buffer->display_offset = buffer->total_height - state.h;
+ if (buffer->display_offset + state.text_h > buffer->total_height)
+ buffer->display_offset = buffer->total_height - state.text_h;
}
static void
@@ -889,6 +901,7 @@ setup(int argc, char *argv[]) {
if (xsel.xtarget == None)
xsel.xtarget = XA_STRING;
+ recalc_text_size();
redraw();
}
@@ -925,9 +938,9 @@ redraw(void) {
final_y = buffer->display_offset - h;
final_h -= buffer->display_offset - h;
}
- if (dest_y + final_h > state.h) {
+ if (dest_y + final_h > state.text_h) {
final_h -= final_y + final_h -
- buffer->display_offset - state.h;
+ buffer->display_offset - state.text_h;
}
ledit_cache_pixmap *pix = ledit_get_cache_pixmap(
line->cache_index
@@ -942,7 +955,7 @@ redraw(void) {
cursor_displayed = 1;
}
}
- if (h + line->h >= buffer->display_offset + state.h)
+ if (h + line->h >= buffer->display_offset + state.text_h)
break;
h += line->h;
}
@@ -971,31 +984,33 @@ redraw(void) {
);
}
}
- if (buffer->total_height > state.h) {
+ if (buffer->total_height > state.text_h) {
XSetForeground(state.dpy, state.gc, state.scroll_bg.pixel);
XFillRectangle(
state.dpy, state.drawable, state.gc,
- state.w - 10, 0, 10, state.h
+ state.w - SCROLLBAR_WIDTH, 0, SCROLLBAR_WIDTH, state.text_h
);
XSetForeground(state.dpy, state.gc, state.fg.pixel);
double scroll_h, scroll_y;
get_scroll_pos_height(&scroll_y, &scroll_h);
XFillRectangle(
state.dpy, state.drawable, state.gc,
- state.w - 10, (int)round(scroll_y), 10, (int)round(scroll_h)
+ state.w - SCROLLBAR_WIDTH, (int)round(scroll_y),
+ SCROLLBAR_WIDTH, (int)round(scroll_h)
);
}
XSetForeground(state.dpy, state.gc, state.bg.pixel);
+ /* FIXME: allow different color for bar */
XFillRectangle(
state.dpy, state.drawable, state.gc,
- 0, state.h - bottom_bar.mode_h,
- state.w - 10, bottom_bar.mode_h
+ 0, state.text_h,
+ state.w, state.h - state.text_h
);
XCopyArea(
state.dpy, bottom_bar.mode_draw->pixmap,
state.drawable, state.gc,
0, 0, bottom_bar.mode_w, bottom_bar.mode_h,
- state.w - 10 - bottom_bar.mode_w, state.h - bottom_bar.mode_h
+ state.w - bottom_bar.mode_w, state.text_h
);
XdbeSwapInfo swap_info;
@@ -1160,7 +1175,7 @@ button_press(XEvent *event) {
get_scroll_pos_height(&scroll_y, &scroll_h);
x = event->xbutton.x;
y = event->xbutton.y;
- if (x >= state.w - 10) {
+ if (x >= state.text_w) {
state.scroll_dragging = 1;
state.scroll_grab_handle = y;
if (y < scroll_y || y > scroll_y + scroll_h) {
@@ -1168,7 +1183,7 @@ button_press(XEvent *event) {
set_scroll_pos(new_scroll_y);
}
return 1;
- } else {
+ } else if (y < state.text_h) {
int l, b;
xy_to_line_byte(x, y, &l, &b);
set_selection(l, b, l, b);
@@ -1183,18 +1198,19 @@ button_press(XEvent *event) {
}
break;
case Button4:
- buffer->display_offset -= 10;
+ buffer->display_offset -= SCROLL_STEP;
if (buffer->display_offset < 0)
buffer->display_offset = 0;
return 1;
case Button5:
- if (buffer->display_offset + state.h <
+ if (buffer->display_offset + state.text_h <
buffer->total_height) {
- buffer->display_offset += 10;
- if (buffer->display_offset + state.h >
- buffer->total_height)
+ buffer->display_offset += SCROLL_STEP;
+ if (buffer->display_offset + state.text_h >
+ buffer->total_height) {
buffer->display_offset =
- buffer->total_height - state.h;
+ buffer->total_height - state.text_h;
+ }
}
return 1;
}
@@ -1243,9 +1259,9 @@ ensure_cursor_shown(void) {
if (cursor_y < buffer->display_offset) {
buffer->display_offset = cursor_y;
} else if (cursor_y + strong.height / PANGO_SCALE >
- buffer->display_offset + state.h) {
+ buffer->display_offset + state.text_h) {
buffer->display_offset =
- cursor_y - state.h + strong.height / PANGO_SCALE;
+ cursor_y - state.text_h + strong.height / PANGO_SCALE;
}
}
@@ -1259,17 +1275,18 @@ resize_window(int w, int h) {
for (int i = 0; i < buffer->lines_num; i++) {
ledit_line *line = ledit_get_line(buffer, i);
/* 10 pixels for scrollbar */
- pango_layout_set_width(line->layout, (w - 10) * PANGO_SCALE);
+ pango_layout_set_width(line->layout, (w - SCROLLBAR_WIDTH) * PANGO_SCALE);
pango_layout_get_pixel_size(line->layout, &tmp_w, &tmp_h);
line->h = tmp_h;
- line->w = w - 10;
+ line->w = w - SCROLLBAR_WIDTH;
line->y_offset = buffer->total_height;
line->dirty = 1;
buffer->total_height += tmp_h;
}
+ recalc_text_size();
if (buffer->display_offset > 0 &&
- buffer->display_offset + state.h >= buffer->total_height) {
- buffer->display_offset = buffer->total_height - state.h;
+ buffer->display_offset + state.text_h >= buffer->total_height) {
+ buffer->display_offset = buffer->total_height - state.text_h;
if (buffer->display_offset < 0)
buffer->display_offset = 0;
}
@@ -1725,5 +1742,6 @@ key_press(XEvent event) {
&buffer->cur_line, &buffer->cur_index
);
}
+ /* FIXME: only do this when necessary */
ensure_cursor_shown();
}