From c59e6a40b2d89650b3cec4f2d0abad38af46ec0b Mon Sep 17 00:00:00 2001 From: pommicket Date: Sun, 26 Feb 2023 23:17:08 -0500 Subject: ctrl+scroll to adjust text size --- command.c | 28 ++++++++++++++++------------ config.c | 1 + main.c | 37 +++++++++++++++++++++++++++++-------- ted.cfg | 2 ++ ted.h | 1 + 5 files changed, 49 insertions(+), 20 deletions(-) diff --git a/command.c b/command.c index 5f92719..36fd2d1 100644 --- a/command.c +++ b/command.c @@ -481,20 +481,24 @@ void command_execute(Ted *ted, Command c, i64 argument) { menu_open(ted, MENU_COMMAND_SELECTOR); break; - case CMD_TEXT_SIZE_INCREASE: { - i64 new_text_size = settings->text_size + argument; - if (new_text_size >= TEXT_SIZE_MIN && new_text_size <= TEXT_SIZE_MAX) { - settings->text_size = (u16)new_text_size; - ted_load_fonts(ted); + case CMD_TEXT_SIZE_INCREASE: + if (argument != 0) { + i64 new_text_size = settings->text_size + argument; + if (new_text_size >= TEXT_SIZE_MIN && new_text_size <= TEXT_SIZE_MAX) { + settings->text_size = (u16)new_text_size; + ted_load_fonts(ted); + } } - } break; - case CMD_TEXT_SIZE_DECREASE: { - i64 new_text_size = settings->text_size - argument; - if (new_text_size >= TEXT_SIZE_MIN && new_text_size <= TEXT_SIZE_MAX) { - settings->text_size = (u16)new_text_size; - ted_load_fonts(ted); + break; + case CMD_TEXT_SIZE_DECREASE: + if (argument != 0) { + i64 new_text_size = settings->text_size - argument; + if (new_text_size >= TEXT_SIZE_MIN && new_text_size <= TEXT_SIZE_MAX) { + settings->text_size = (u16)new_text_size; + ted_load_fonts(ted); + } } - } break; + break; case CMD_VIEW_ONLY: if (buffer) buffer->view_only = !buffer->view_only; diff --git a/config.c b/config.c index 390f1ac..a1b73a2 100644 --- a/config.c +++ b/config.c @@ -114,6 +114,7 @@ static SettingFloat const settings_float[] = { {"cursor-blink-time-on", &settings_zero.cursor_blink_time_on, 0, 1000, true}, {"cursor-blink-time-off", &settings_zero.cursor_blink_time_off, 0, 1000, true}, {"hover-time", &settings_zero.hover_time, 0, INFINITY, true}, + {"ctrl-scroll-adjust-text-size", &settings_zero.ctrl_scroll_adjust_text_size, -10, 10, true}, }; static SettingString const settings_string[] = { {"build-default-command", settings_zero.build_default_command, sizeof settings_zero.build_default_command, true}, diff --git a/main.c b/main.c index 043d335..dde5950 100644 --- a/main.c +++ b/main.c @@ -585,6 +585,7 @@ int main(int argc, char **argv) { } double start_time = time_get_seconds(); + double scroll_wheel_text_size_change = 0.0; while (!ted->quit) { double frame_start = time_get_seconds(); @@ -620,14 +621,20 @@ int main(int argc, char **argv) { command_execute(ted, CMD_QUIT, 1); break; case SDL_MOUSEWHEEL: { - // scroll with mouse wheel - Sint32 dx = event.wheel.x, dy = -event.wheel.y; - Autocomplete *ac = &ted->autocomplete; - if (ac->open && rect_contains_point(ac->rect, ted->mouse_pos)) { - autocomplete_scroll(ted, dy); - } else { - ted->scroll_total_x += dx; - ted->scroll_total_y += dy; + if (ctrl_down) { + // adjust text size with ctrl+scroll + Settings *settings = ted_active_settings(ted); + scroll_wheel_text_size_change += settings->ctrl_scroll_adjust_text_size * event.wheel.preciseY; + } else if (key_modifier == 0) { + // scroll with mouse wheel + Sint32 dx = event.wheel.x, dy = -event.wheel.y; + Autocomplete *ac = &ted->autocomplete; + if (ac->open && rect_contains_point(ac->rect, ted->mouse_pos)) { + autocomplete_scroll(ted, dy); + } else { + ted->scroll_total_x += dx; + ted->scroll_total_y += dy; + } } } break; case SDL_MOUSEBUTTONDOWN: { @@ -803,6 +810,20 @@ int main(int argc, char **argv) { Uint32 time_this_frame = SDL_GetTicks(); frame_dt = 0.001 * (time_this_frame - time_at_last_frame); time_at_last_frame = time_this_frame; + + + } + + { + // when the user ctrl+scrolls, only actually change the text size + // every 100ms, to avoid loading the font over and over again super fast + static double last_font_adjust = 0; + if (ted->frame_time - last_font_adjust > 0.1) { + last_font_adjust = ted->frame_time; + int dsize = (int)floor(scroll_wheel_text_size_change); + command_execute(ted, CMD_TEXT_SIZE_INCREASE, dsize); + scroll_wheel_text_size_change -= dsize; + } } TextBuffer *active_buffer = ted->active_buffer; diff --git a/ted.cfg b/ted.cfg index 03b69bb..517c206 100644 --- a/ted.cfg +++ b/ted.cfg @@ -68,6 +68,8 @@ max-file-size = 20000000 # absolute maximum file size. # ted will produce an error if a file larger than this is loaded. max-file-size-view-only = 100000000 +# how much ctrl+scroll wheel changes the text size (0 for no change, negative to invert change) +ctrl-scroll-adjust-text-size = 1.0 # whether to use vsync or not. you probably want this on. vsync = on diff --git a/ted.h b/ted.h index 1faf439..375049b 100644 --- a/ted.h +++ b/ted.h @@ -108,6 +108,7 @@ typedef struct { u32 colors[COLOR_COUNT]; float cursor_blink_time_on, cursor_blink_time_off; float hover_time; + float ctrl_scroll_adjust_text_size; u32 max_file_size; u32 max_file_size_view_only; u16 framerate_cap; -- cgit v1.2.3