Skip to content

Commit

Permalink
Reduce the complexity of some internal methods
Browse files Browse the repository at this point in the history
  • Loading branch information
gansm committed Mar 3, 2024
1 parent f753a6b commit 83ba487
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 25 deletions.
15 changes: 11 additions & 4 deletions final/widget/fbuttongroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,16 @@ inline auto FButtonGroup::findButtonIf (UnaryPredicate p) const -> FToggleButton
return nullptr;
}

//----------------------------------------------------------------------
inline auto FButtonGroup::needToUncheck ( const FToggleButton* toggle_button
, const FToggleButton* button ) const -> bool
{
return toggle_button
&& toggle_button != button
&& toggle_button->isChecked()
&& isRadioButton(toggle_button);
}

//----------------------------------------------------------------------
void FButtonGroup::cb_buttonToggled (const FToggleButton* button) const
{
Expand All @@ -448,10 +458,7 @@ void FButtonGroup::cb_buttonToggled (const FToggleButton* button) const
{
auto toggle_button = static_cast<FToggleButton*>(item);

if ( toggle_button
&& toggle_button != button
&& toggle_button->isChecked()
&& isRadioButton(toggle_button) )
if ( needToUncheck(toggle_button, button) )
{
toggle_button->unsetChecked();

Expand Down
1 change: 1 addition & 0 deletions final/widget/fbuttongroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ class FButtonGroup : public FScrollView
auto getCheckedRadioButton() -> FToggleButton*;
template <typename UnaryPredicate>
auto findButtonIf (UnaryPredicate) const -> FToggleButton*;
auto needToUncheck (const FToggleButton*, const FToggleButton*) const -> bool;

// Callback method
void cb_buttonToggled (const FToggleButton*) const;
Expand Down
53 changes: 32 additions & 21 deletions final/widget/fscrollbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,28 +338,9 @@ void FScrollbar::onTimer (FTimerEvent*)
addTimer(repeat_time);
}

// Timer stop condition
if ( ( scroll_type == ScrollType::PageBackward
&& slider_pos == slider_click_stop_pos )
|| ( scroll_type == ScrollType::PageForward
&& slider_pos == slider_click_stop_pos ) )
if ( shouldStopTimer() ) // Timer stop condition
{
const auto max_slider_pos = int(bar_length - slider_length);

if ( scroll_type == ScrollType::PageBackward
&& slider_pos == 0 )
{
jumpToClickPos(0); // Scroll to the start
processScroll();
}
else if ( scroll_type == ScrollType::PageForward
&& slider_pos == max_slider_pos )
{
jumpToClickPos (max_slider_pos); // Scroll to the end
processScroll();
}

delOwnTimers();
stopTimer();
return;
}

Expand Down Expand Up @@ -656,6 +637,15 @@ inline auto FScrollbar::isMouseOutsideScrollbar ( int mouse_x
|| mouse_y > int(getHeight());
}

//----------------------------------------------------------------------
inline auto FScrollbar::shouldStopTimer() const -> bool
{
return ( scroll_type == ScrollType::PageBackward
&& slider_pos == slider_click_stop_pos )
|| ( scroll_type == ScrollType::PageForward
&& slider_pos == slider_click_stop_pos );
}

//----------------------------------------------------------------------
void FScrollbar::jumpToClickPos (int x, int y)
{
Expand Down Expand Up @@ -735,6 +725,27 @@ inline void FScrollbar::handleJumpScroll (int mouse_x, int mouse_y)
processScroll();
}

//----------------------------------------------------------------------
void FScrollbar::stopTimer()
{
const auto max_slider_pos = int(bar_length - slider_length);

if ( scroll_type == ScrollType::PageBackward
&& slider_pos == 0 )
{
jumpToClickPos(0); // Scroll to the start
processScroll();
}
else if ( scroll_type == ScrollType::PageForward
&& slider_pos == max_slider_pos )
{
jumpToClickPos (max_slider_pos); // Scroll to the end
processScroll();
}

delOwnTimers();
}

//----------------------------------------------------------------------
void FScrollbar::avoidScrollOvershoot()
{
Expand Down
2 changes: 2 additions & 0 deletions final/widget/fscrollbar.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,11 @@ class FScrollbar : public FWidget
auto getHorizontalScrollType (int) const -> ScrollType;
auto getSliderClickPos (int, int) const -> int;
auto isMouseOutsideScrollbar (int, int) const -> bool;
auto shouldStopTimer() const -> bool;
void jumpToClickPos (int, int);
void jumpToClickPos (int);
void handleJumpScroll (int, int);
void stopTimer();
void avoidScrollOvershoot();
void processScroll();
void changeOnResize();
Expand Down

0 comments on commit 83ba487

Please sign in to comment.