This still isn't perfect at keeping the cursor on screen, but maybe this is good enough? I'm also considering not moving the cursor when panning with the mouse. If you're already at the mouse just select a pane on the screen while you're at it.
But perhaps this is rationalization.
7AKT7IKOP6VQW3CABAVXCEZTHYNAETVXGFIQWB2L6I7LM7T57HAAC S4U35JJQWC3CBNKHAEZ2DYEGUT5L3UYVAO6PJ5R7HSTD53KWVAXQC 5MR22SGZE5YDU5CAIY53GNJDA6HSWBPYPD6M3FRQ5ZUMCSKTYJRAC AJB4LFRBMIRBEDWJ3OW7GQIMD2BZBVQ62GH4TE2FISWZKSAHRF4QC OTIBCAUJ3KDQJLVDN3A536DLZGNRYMGJLORZVR3WLCGXGO6UGO6AC TZ3ZNGRMZ3DG33VVEZI74W5W5T73JYMEIYQ3SO6Y4A6FN3OUZIKQC DUQDA3U7VNWZSKRVACS6G3FTEB5VXRR7FJQU5NYZ4EFSGL3XUU5QC local left_sx = left_edge_sx(Cursor_pane.col)local left_px = left_sx - Display_settings.xif left_px > App.screen.width - Margin_right - Display_settings.column_width/2 thenCursor_pane.col = math.min(#Surface, col(Display_settings.x + App.screen.width - Margin_right - Display_settings.column_width/2))Cursor_pane.row = math.min(#Surface[Cursor_pane.col], Cursor_pane.row)end
local right_sx = left_edge_sx(Cursor_pane.col) + Display_settings.column_widthlocal right_px = right_sx - Display_settings.xif right_px < Margin_left + Display_settings.column_width/2 thenCursor_pane.col = math.min(#Surface, col(Display_settings.x + Margin_left + Display_settings.column_width/2))Cursor_pane.row = math.min(#Surface[Cursor_pane.col], Cursor_pane.row)end
Display_settings.x = math.max(Display_settings.x - Display_settings.column_width - Padding_horizontal, 0)
Display_settings.x = math.max(Display_settings.x - Margin_left - Display_settings.column_width - Margin_right - Padding_horizontal, 0)local left_sx = left_edge_sx(Cursor_pane.col)local left_px = left_sx - Display_settings.xif left_px > App.screen.width - Margin_right - Display_settings.column_width/2 thenCursor_pane.col = math.min(#Surface, col(Display_settings.x + App.screen.width - Margin_right - Display_settings.column_width/2))Cursor_pane.row = math.min(#Surface[Cursor_pane.col], Cursor_pane.row)end
Display_settings.x = Display_settings.x + Display_settings.column_width + Padding_horizontal
Display_settings.x = Display_settings.x + Margin_left + Display_settings.column_width + Margin_right + Padding_horizontallocal right_sx = left_edge_sx(Cursor_pane.col) + Display_settings.column_widthlocal right_px = right_sx - Display_settings.xif right_px < Margin_left + Display_settings.column_width/2 thenCursor_pane.col = math.min(#Surface, col(Display_settings.x + Margin_left + Display_settings.column_width/2))Cursor_pane.row = math.min(#Surface[Cursor_pane.col], Cursor_pane.row)end
function up_edge_sx(col, row)local result = Margin_topfor i=1,row-1 dolocal pane = Surface[col][i]result = result + Margin_above + Cache[pane.id].height + Margin_below + Padding_verticalendreturn resultendfunction down_edge_sx(col, row)local result = Margin_topfor i=1,row dolocal pane = Surface[col][i]result = result + Margin_above + Cache[pane.id].height + Margin_below + Padding_verticalendreturn result - Padding_verticalend