Skip to content

Commit 48da04e

Browse files
committed
Add user-callable scroll_into_view(), home() and end() member functions
1 parent a0c4d92 commit 48da04e

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

lib/include/elements/element/text.hpp

+5
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ namespace cycfi::elements
147147
void select_end(int pos);
148148
void select_all();
149149
void select_none();
150+
void scroll_into_view() { _scroll_into_view = true; }
151+
152+
void home();
153+
void end();
150154

151155
virtual void draw_selection(context const& ctx);
152156
virtual void draw_caret(context const& ctx);
@@ -199,6 +203,7 @@ namespace cycfi::elements
199203
bool _caret_started : 1;
200204
bool _read_only : 1;
201205
bool _enabled : 1;
206+
bool _scroll_into_view : 1;
202207
state_saver_set _state_savers;
203208
};
204209

lib/src/element/text.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ namespace cycfi { namespace elements
134134
, _caret_started{false}
135135
, _read_only{false}
136136
, _enabled{true}
137+
, _scroll_into_view{false}
137138
{}
138139

139140
struct basic_text_box::state_saver : std::enable_shared_from_this<basic_text_box::state_saver>
@@ -169,6 +170,12 @@ namespace cycfi { namespace elements
169170

170171
void basic_text_box::draw(context const& ctx)
171172
{
173+
if (_scroll_into_view)
174+
{
175+
scroll_into_view(ctx, false);
176+
_scroll_into_view = false;
177+
}
178+
172179
draw_selection(ctx);
173180
if (_enabled)
174181
{
@@ -882,6 +889,21 @@ namespace cycfi { namespace elements
882889
_select_start = _select_end = -1;
883890
}
884891

892+
void basic_text_box::home()
893+
{
894+
select_start(0);
895+
select_end(0);
896+
scroll_into_view();
897+
}
898+
899+
void basic_text_box::end()
900+
{
901+
auto end = get_text().size();
902+
select_start(end);
903+
select_end(end);
904+
scroll_into_view();
905+
}
906+
885907
bool basic_text_box::word_break(int index) const
886908
{
887909
return get_layout().word_break(index) == text_layout::allow_break || line_break(index);

0 commit comments

Comments
 (0)