Disallow scrolling down too much
The ability to scroll past the end was rather unusual. You can still achieve that by searching and resizing the window now.
This commit is contained in:
parent
e06d9c9f71
commit
dcfc9bfd22
86
src/sdtui.c
86
src/sdtui.c
|
@ -1088,35 +1088,28 @@ done:
|
||||||
refresh ();
|
refresh ();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static ViewEntry *
|
||||||
|
entry_for_position (Application *self, guint32 position)
|
||||||
|
{
|
||||||
|
StardictIterator *iterator = stardict_iterator_new (self->dict, position);
|
||||||
|
ViewEntry *ve = NULL;
|
||||||
|
if (stardict_iterator_is_valid (iterator))
|
||||||
|
ve = view_entry_new (iterator);
|
||||||
|
g_object_unref (iterator);
|
||||||
|
return ve;
|
||||||
|
}
|
||||||
|
|
||||||
/// Just prepends a new view entry into the entries array.
|
/// Just prepends a new view entry into the entries array.
|
||||||
static ViewEntry *
|
static ViewEntry *
|
||||||
prepend_entry (Application *self, guint32 position)
|
prepend_entry (Application *self, guint32 position)
|
||||||
{
|
{
|
||||||
StardictIterator *iterator = stardict_iterator_new (self->dict, position);
|
ViewEntry *ve = entry_for_position (self, position);
|
||||||
ViewEntry *ve = view_entry_new (iterator);
|
|
||||||
g_object_unref (iterator);
|
|
||||||
|
|
||||||
g_ptr_array_add (self->entries, NULL);
|
g_ptr_array_add (self->entries, NULL);
|
||||||
memmove (self->entries->pdata + 1, self->entries->pdata,
|
memmove (self->entries->pdata + 1, self->entries->pdata,
|
||||||
sizeof ve * (self->entries->len - 1));
|
sizeof ve * (self->entries->len - 1));
|
||||||
return g_ptr_array_index (self->entries, 0) = ve;
|
return g_ptr_array_index (self->entries, 0) = ve;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Just appends a new view entry to the entries array.
|
|
||||||
static ViewEntry *
|
|
||||||
append_entry (Application *self, guint32 position)
|
|
||||||
{
|
|
||||||
ViewEntry *ve = NULL;
|
|
||||||
StardictIterator *iterator = stardict_iterator_new (self->dict, position);
|
|
||||||
if (stardict_iterator_is_valid (iterator))
|
|
||||||
{
|
|
||||||
ve = view_entry_new (iterator);
|
|
||||||
g_ptr_array_add (self->entries, ve);
|
|
||||||
}
|
|
||||||
g_object_unref (iterator);
|
|
||||||
return ve;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Counts the number of definitions available for seeing.
|
/// Counts the number of definitions available for seeing.
|
||||||
static guint
|
static guint
|
||||||
app_count_view_items (Application *self)
|
app_count_view_items (Application *self)
|
||||||
|
@ -1134,7 +1127,6 @@ app_count_view_items (Application *self)
|
||||||
static gboolean
|
static gboolean
|
||||||
app_scroll_up (Application *self, guint n)
|
app_scroll_up (Application *self, guint n)
|
||||||
{
|
{
|
||||||
gboolean success = TRUE;
|
|
||||||
guint n_definitions = app_count_view_items (self);
|
guint n_definitions = app_count_view_items (self);
|
||||||
while (n--)
|
while (n--)
|
||||||
{
|
{
|
||||||
|
@ -1146,10 +1138,7 @@ app_scroll_up (Application *self, guint n)
|
||||||
|
|
||||||
// We've reached the top
|
// We've reached the top
|
||||||
if (self->top_position == 0)
|
if (self->top_position == 0)
|
||||||
{
|
return FALSE;
|
||||||
success = FALSE;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
ViewEntry *ve = prepend_entry (self, --self->top_position);
|
ViewEntry *ve = prepend_entry (self, --self->top_position);
|
||||||
self->top_offset = ve->definitions_length - 1;
|
self->top_offset = ve->definitions_length - 1;
|
||||||
|
@ -1166,46 +1155,49 @@ app_scroll_up (Application *self, guint n)
|
||||||
(self->entries, self->entries->len - 1);
|
(self->entries, self->entries->len - 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return success;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Scroll down @a n entries. Doesn't redraw.
|
/// Scroll down @a n entries. Doesn't redraw.
|
||||||
static gboolean
|
static gboolean
|
||||||
app_scroll_down (Application *self, guint n)
|
app_scroll_down (Application *self, guint n)
|
||||||
{
|
{
|
||||||
gboolean success = TRUE;
|
|
||||||
guint n_definitions = app_count_view_items (self);
|
guint n_definitions = app_count_view_items (self);
|
||||||
while (n--)
|
while (n--)
|
||||||
{
|
{
|
||||||
if (self->entries->len == 0)
|
if (self->entries->len == 0)
|
||||||
{
|
return FALSE;
|
||||||
success = FALSE;
|
|
||||||
break;
|
// Simulate the movement first to disallow scrolling past the end
|
||||||
}
|
guint to_be_offset = self->top_offset;
|
||||||
|
guint to_be_definitions = n_definitions;
|
||||||
|
|
||||||
// TODO: try to disallow scrolling past the end
|
|
||||||
ViewEntry *first_entry = g_ptr_array_index (self->entries, 0);
|
ViewEntry *first_entry = g_ptr_array_index (self->entries, 0);
|
||||||
if (++self->top_offset >= first_entry->definitions_length)
|
if (++to_be_offset >= first_entry->definitions_length)
|
||||||
|
{
|
||||||
|
to_be_definitions -= first_entry->definitions_length;
|
||||||
|
to_be_offset = 0;
|
||||||
|
}
|
||||||
|
if ((gint) (to_be_definitions - to_be_offset) < LINES - TOP_BAR_CUTOFF)
|
||||||
|
{
|
||||||
|
ViewEntry *new_entry = entry_for_position
|
||||||
|
(self, self->top_position + self->entries->len);
|
||||||
|
if (!new_entry)
|
||||||
|
return FALSE;
|
||||||
|
|
||||||
|
g_ptr_array_add (self->entries, new_entry);
|
||||||
|
to_be_definitions += new_entry->definitions_length;
|
||||||
|
}
|
||||||
|
if (to_be_offset == 0)
|
||||||
{
|
{
|
||||||
n_definitions -= first_entry->definitions_length;
|
|
||||||
g_ptr_array_remove_index (self->entries, 0);
|
g_ptr_array_remove_index (self->entries, 0);
|
||||||
self->top_position++;
|
self->top_position++;
|
||||||
self->top_offset = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((gint) (n_definitions - self->top_offset) < LINES - TOP_BAR_CUTOFF)
|
self->top_offset = to_be_offset;
|
||||||
{
|
n_definitions = to_be_definitions;
|
||||||
ViewEntry *ve = append_entry (self,
|
|
||||||
self->top_position + self->entries->len);
|
|
||||||
if (ve != NULL)
|
|
||||||
n_definitions += ve->definitions_length;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return TRUE;
|
||||||
// Fix cursor to not point below the view items
|
|
||||||
if (self->selected >= n_definitions - self->top_offset)
|
|
||||||
self->selected = n_definitions - self->top_offset - 1;
|
|
||||||
return success;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Moves the selection one entry up.
|
/// Moves the selection one entry up.
|
||||||
|
@ -1261,8 +1253,8 @@ app_one_entry_down (Application *self)
|
||||||
// FIXME: selection can still get past the end
|
// FIXME: selection can still get past the end
|
||||||
if (first >= LINES - TOP_BAR_CUTOFF)
|
if (first >= LINES - TOP_BAR_CUTOFF)
|
||||||
{
|
{
|
||||||
self->selected = LINES - TOP_BAR_CUTOFF - 1;
|
|
||||||
app_scroll_down (self, first - (LINES - TOP_BAR_CUTOFF - 1));
|
app_scroll_down (self, first - (LINES - TOP_BAR_CUTOFF - 1));
|
||||||
|
self->selected = LINES - TOP_BAR_CUTOFF - 1;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
self->selected = first;
|
self->selected = first;
|
||||||
|
|
Loading…
Reference in New Issue