Skip to content

Commit

Permalink
Improve shell scrolling.
Browse files Browse the repository at this point in the history
Increments-minor-version-of: ttyr-tty
  • Loading branch information
dajofrey committed Dec 16, 2023
1 parent 8ec7fed commit 5b633d2
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 52 deletions.
65 changes: 64 additions & 1 deletion src/lib/ttyr-tty/Shell/Shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,17 @@ typedef struct ttyr_tty_ShellSelection {
bool moved;
} ttyr_tty_ShellSelection;

typedef struct ttyr_tty_ShellScroll {
nh_PixelPosition Position;
nh_PixelPosition Current;
bool active;
nh_SystemTime LastScroll;
} ttyr_tty_ShellScroll;

typedef struct ttyr_tty_Shell {
ttyr_tty_ShellSocket Socket;
ttyr_tty_ShellSelection Selection;
ttyr_tty_ShellScroll Scroll;
ST *ST_p;
ttyr_tty_Row *Rows_p;
int ttyfd;
Expand Down Expand Up @@ -426,6 +434,37 @@ TTYR_TTY_BEGIN()
TTYR_TTY_SILENT_END()
}

static TTYR_TTY_RESULT ttyr_tty_handleFastScroll(
ttyr_tty_Shell *Shell_p)
{
TTYR_TTY_BEGIN()

nh_SystemTime Now = nh_core_getSystemTime();
if (nh_core_getSystemTimeDiffInSeconds(Shell_p->Scroll.LastScroll, Now) > 0.05f) {
TTYR_TTY_END(TTYR_TTY_SUCCESS)
}

int diff = Shell_p->Scroll.Current.y - Shell_p->Scroll.Position.y;
if (diff < 0) {
if ((Shell_p->scroll + abs(diff)) < Shell_p->ST_p->scrollup) {
Shell_p->scroll += abs(diff);
} else if (Shell_p->scroll < Shell_p->ST_p->scrollup) {
Shell_p->scroll = Shell_p->ST_p->scrollup;
}

} else if (diff > 0) {
if ((Shell_p->scroll - diff) > 0) {
Shell_p->scroll -= diff;
} else if (Shell_p->scroll > 0) {
Shell_p->scroll = 0;
}
}
Shell_p->Scroll.LastScroll = Now;
Shell_p->drawing = 1;

TTYR_TTY_END(TTYR_TTY_SUCCESS)
}

static TTYR_TTY_RESULT ttyr_tty_updateShell(
ttyr_tty_Program *Program_p)
{
Expand All @@ -436,6 +475,10 @@ TTYR_TTY_BEGIN()

TTYR_TTY_CHECK(ttyr_tty_handleShellSocket(&Shell_p->Socket))

if (Shell_p->Scroll.active){
TTYR_TTY_CHECK(ttyr_tty_handleFastScroll(Shell_p))
}

FD_ZERO(&Shell_p->rfd);
FD_SET(Shell_p->ttyfd, &Shell_p->rfd);

Expand Down Expand Up @@ -803,6 +846,15 @@ TTYR_TTY_BEGIN()

// Handle mouse events.
if (Event.type == NH_WSI_EVENT_MOUSE) {
if (Event.Mouse.type == NH_WSI_MOUSE_MIDDLE && Event.Mouse.trigger == NH_WSI_TRIGGER_PRESS) {
Shell_p->Scroll.active = true;
Shell_p->Scroll.Position = Event.Window.Position;
Shell_p->Scroll.Current = Event.Window.Position;
Shell_p->Scroll.LastScroll = nh_core_getSystemTime();
}
if (Event.Mouse.type == NH_WSI_MOUSE_MIDDLE && Event.Mouse.trigger == NH_WSI_TRIGGER_RELEASE) {
Shell_p->Scroll.active = false;
}
if (Event.Mouse.type == NH_WSI_MOUSE_LEFT && Event.Mouse.trigger == NH_WSI_TRIGGER_PRESS) {
Shell_p->Selection.Start = Event.Mouse.Position;
Shell_p->Selection.startScroll = Shell_p->scroll;
Expand All @@ -824,13 +876,16 @@ TTYR_TTY_BEGIN()
ttyr_tty_handleShellSelection(Shell_p);
}
if (Event.Mouse.type == NH_WSI_MOUSE_MOVE) {
if (Shell_p->Selection.active == true) {
if (Shell_p->Selection.active) {
Shell_p->Selection.doubleClick = false;
Shell_p->Selection.draw = true;
Shell_p->Selection.Stop = Event.Mouse.Position;
Shell_p->Selection.stopScroll = Shell_p->scroll;
Shell_p->Selection.moved = true;
}
if (Shell_p->Scroll.active) {
Shell_p->Scroll.Current = Event.Window.Position;
}
}

if (Shell_p->ST_p->mode & TERM_MODE_ALTSCREEN) {
Expand Down Expand Up @@ -1008,6 +1063,14 @@ TTYR_TTY_BEGIN()
Glyphs_p[i].codepoint = topbar_p[i];
}

if (Shell_p->scroll > 0) {
NH_BYTE scroll_p[64];
sprintf(scroll_p, "%d/%d", Shell_p->scroll, Shell_p->ST_p->scrollup);
for (int i = 0, j = strlen(scroll_p)-1; i < strlen(scroll_p); ++i, --j) {
Glyphs_p[(width-4)-i].codepoint = scroll_p[j];
}
}

TTYR_TTY_DIAGNOSTIC_END(TTYR_TTY_SUCCESS)
}

Expand Down
37 changes: 7 additions & 30 deletions src/lib/ttyr-tty/TTY/ContextMenu.c
Original file line number Diff line number Diff line change
Expand Up @@ -434,18 +434,18 @@ TTYR_TTY_SILENT_END()
// MOUSE MENU ======================================================================================
// Mouse menu functions.

ttyr_tty_ContextMenu *ttyr_tty_createMouseMenu1(
ttyr_tty_ContextMenu *ttyr_tty_createMouseMenu(
int x, int y)
{
TTYR_TTY_BEGIN()

nh_encoding_UTF32String Menu = nh_encoding_initUTF32(128);
nh_encoding_appendUTF32Codepoint(&Menu, '{');

ttyr_tty_TTY *TTY_p = nh_core_getWorkloadArg();

ttyr_tty_Config Config = ttyr_tty_getConfig();
ttyr_tty_Program *Program_p = ttyr_tty_getCurrentProgram(&TTYR_TTY_MACRO_TAB(TTY_p->Window_p->Tile_p)->MicroWindow);

nh_encoding_UTF32String Menu = nh_encoding_initUTF32(128);
nh_encoding_appendUTF32Codepoint(&Menu, '{');

// Generate command menu.
if (Program_p && Program_p->Prototype_p && Program_p->Prototype_p->commands > 0) {
for (int i = 0; i < Program_p->Prototype_p->commands; ++i) {
Expand All @@ -455,31 +455,8 @@ TTYR_TTY_BEGIN()
}
}

nh_encoding_appendUTF32Codepoint(&Menu, '}');

NH_ENCODING_UTF32 *p = Menu.p;
ttyr_tty_ContextMenu *Menu_p = ttyr_tty_parseContextMenu(&p, NULL);
TTYR_TTY_CHECK_NULL_2(NULL, Menu_p)

ttyr_tty_computeContextMenuPosition(Menu_p, x, y, ((ttyr_tty_View*)TTY_p->Views.pp[0])->cols, ((ttyr_tty_View*)TTY_p->Views.pp[0])->rows);

nh_encoding_freeUTF32(&Menu);

TTYR_TTY_END(Menu_p)
}

ttyr_tty_ContextMenu *ttyr_tty_createMouseMenu2(
int x, int y)
{
TTYR_TTY_BEGIN()

nh_encoding_UTF32String Menu = nh_encoding_initUTF32(128);
nh_encoding_appendUTF32Codepoint(&Menu, '{');

ttyr_tty_TTY *TTY_p = nh_core_getWorkloadArg();
ttyr_tty_Config Config = ttyr_tty_getConfig();

bool border = false;
nh_encoding_appendUTF32Codepoint(&Menu, ',');
bool border = true;

if (Config.Menu.program && TTY_p->Prototypes.size > 1) {
ttyr_tty_Program *Program_p = ttyr_tty_getCurrentProgram(&TTYR_TTY_MACRO_TAB(TTY_p->Window_p->Tile_p)->MicroWindow);
Expand Down
6 changes: 1 addition & 5 deletions src/lib/ttyr-tty/TTY/ContextMenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,7 @@
ttyr_tty_ContextMenu *Menu_p, ttyr_tty_ContextMenu *Parent_p, NH_BOOL recursive, int x, int y
);

ttyr_tty_ContextMenu *ttyr_tty_createMouseMenu1(
int x, int y
);

ttyr_tty_ContextMenu *ttyr_tty_createMouseMenu2(
ttyr_tty_ContextMenu *ttyr_tty_createMouseMenu(
int x, int y
);

Expand Down
19 changes: 3 additions & 16 deletions src/lib/ttyr-tty/TTY/Macro.c
Original file line number Diff line number Diff line change
Expand Up @@ -508,22 +508,7 @@ TTYR_TTY_BEGIN()
Window_p->Tile_p = MacroTile_p;

if (Window_p->MouseMenu_p) {ttyr_tty_freeContextMenu(Window_p->MouseMenu_p);}
Window_p->MouseMenu_p = ttyr_tty_createMouseMenu1(col, Config.Titlebar.on ? row+1 : row);
TTYR_TTY_CHECK_NULL(Window_p->MouseMenu_p)
Window_p->MouseMenu_p->active = NH_TRUE;
Window_p->MouseMenu_p->cCol = cCol;
Window_p->MouseMenu_p->cRow = cRow;

Window_p->refreshGrid2 = NH_TRUE;
}

// Create mouse-menu on right-click and switch tiles.
if (Event.Mouse.type == NH_WSI_MOUSE_MIDDLE && Event.Mouse.trigger == NH_WSI_TRIGGER_PRESS && MicroTile_p) {
TTYR_TTY_MICRO_TAB(TTYR_TTY_MACRO_TAB(MacroTile_p))->Tile_p = MicroTile_p;
Window_p->Tile_p = MacroTile_p;

if (Window_p->MouseMenu_p) {ttyr_tty_freeContextMenu(Window_p->MouseMenu_p);}
Window_p->MouseMenu_p = ttyr_tty_createMouseMenu2(col, Config.Titlebar.on ? row+1 : row);
Window_p->MouseMenu_p = ttyr_tty_createMouseMenu(col, Config.Titlebar.on ? row+1 : row);
TTYR_TTY_CHECK_NULL(Window_p->MouseMenu_p)
Window_p->MouseMenu_p->active = NH_TRUE;
Window_p->MouseMenu_p->cCol = cCol;
Expand All @@ -545,6 +530,8 @@ TTYR_TTY_BEGIN()
TTYR_TTY_CHECK(ttyr_tty_handleTopBarInput(MacroTile_p, Event))
} else if (MicroTile_p && TTYR_TTY_MICRO_TILE(MicroTile_p)->Program_p) {
// Forward program hit.
Event.Window.Position.x = Event.Mouse.Position.x;
Event.Window.Position.y = Event.Mouse.Position.y;
Event.Mouse.Position.x = cCol;
Event.Mouse.Position.y = cRow - 1; // Subtract topbar.
TTYR_TTY_CHECK(TTYR_TTY_MICRO_TILE(MicroTile_p)->Program_p->Prototype_p->Callbacks.handleInput_f(
Expand Down

0 comments on commit 5b633d2

Please sign in to comment.