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 Jan 26, 2024
1 parent c47bd6f commit 130571a
Show file tree
Hide file tree
Showing 13 changed files with 150 additions and 116 deletions.
28 changes: 16 additions & 12 deletions final/fapplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* *
* This file is part of the FINAL CUT widget toolkit *
* *
* Copyright 2013-2023 Markus Gans *
* Copyright 2013-2024 Markus Gans *
* *
* FINAL CUT is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
Expand Down Expand Up @@ -1008,17 +1008,9 @@ void FApplication::determineClickedWidget (const FMouseData& md)
{
clicked_widget = FWidget::getClickedWidget();

if ( clicked_widget )
return; // The clicked widget was already found

if ( ! md.isLeftButtonPressed()
&& ! md.isLeftButtonDoubleClick()
&& ! md.isRightButtonPressed()
&& ! md.isMiddleButtonPressed()
&& ! md.isWheelUp()
&& ! md.isWheelDown()
&& ! md.isWheelLeft()
&& ! md.isWheelRight() )
// Check if the clicked widget has already been found
// or if the mouse has a non-press or wheel event
if ( clicked_widget || isNonActivatingMouseEvent(md) )
return;

const auto& mouse_position = md.getPos();
Expand All @@ -1035,6 +1027,18 @@ void FApplication::determineClickedWidget (const FMouseData& md)
setClickedWidget (clicked_widget);
}

//----------------------------------------------------------------------
auto FApplication::isNonActivatingMouseEvent (const FMouseData& md) -> bool
{
return md.isLeftButtonReleased()
|| md.isRightButtonReleased()
|| md.isMiddleButtonReleased()
|| md.isShiftKeyPressed()
|| md.isControlKeyPressed()
|| md.isMetaKeyPressed()
|| md.isMoved();
}

//----------------------------------------------------------------------
void FApplication::unsetMoveResizeMode (const FMouseData&)
{
Expand Down
3 changes: 2 additions & 1 deletion final/fapplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* *
* This file is part of the FINAL CUT widget toolkit *
* *
* Copyright 2013-2023 Markus Gans *
* Copyright 2013-2024 Markus Gans *
* *
* FINAL CUT is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
Expand Down Expand Up @@ -201,6 +201,7 @@ class FApplication : public FWidget
auto processAccelerator (const FWidget&) const -> bool;
void processTerminalFocus (const FKey&);
static void determineClickedWidget (const FMouseData&);
static auto isNonActivatingMouseEvent (const FMouseData&) -> bool;
static void unsetMoveResizeMode (const FMouseData&);
void sendMouseEvent (const FMouseData&) const;
void sendMouseMoveEvent ( const FMouseData&
Expand Down
18 changes: 17 additions & 1 deletion final/fwidget_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* *
* This file is part of the FINAL CUT widget toolkit *
* *
* Copyright 2019-2023 Markus Gans *
* Copyright 2019-2024 Markus Gans *
* *
* FINAL CUT is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
Expand Down Expand Up @@ -125,6 +125,22 @@ auto isEscapeKey (const FKey key) -> bool
|| key == FKey::Escape_mintty );
}

//----------------------------------------------------------------------
auto isExpandComboBoxKey (const FKey key) -> bool
{
return ( key == FKey::F4
|| key == FKey::Meta_down
|| key == FKey::Ctrl_down );
}

//----------------------------------------------------------------------
auto isCollapseComboBoxKey (const FKey key) -> bool
{
return ( key == FKey::Meta_up
|| key == FKey::Ctrl_up
|| isEscapeKey(key) );
}

//----------------------------------------------------------------------
auto getFirstFocusableWidget (const FObject::FObjectList& list) -> FWidget*
{
Expand Down
4 changes: 3 additions & 1 deletion final/fwidget_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* *
* This file is part of the FINAL CUT widget toolkit *
* *
* Copyright 2021-2023 Markus Gans *
* Copyright 2021-2024 Markus Gans *
* *
* FINAL CUT is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
Expand Down Expand Up @@ -54,6 +54,8 @@ auto isFocusPrevKey (const FKey) -> bool;
auto isDialogMenuKey (const FKey) -> bool;
auto isEnterKey (const FKey) -> bool;
auto isEscapeKey (const FKey) -> bool;
auto isExpandComboBoxKey (const FKey) -> bool;
auto isCollapseComboBoxKey (const FKey) -> bool;
auto getFirstFocusableWidget (const FObjectList&) -> FWidget*;
auto getLastFocusableWidget (const FObjectList&) -> FWidget*;
auto isInFWidgetList (const FWidgetList*, const FWidget*) -> bool;
Expand Down
43 changes: 18 additions & 25 deletions final/menu/fmenubar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* *
* This file is part of the FINAL CUT widget toolkit *
* *
* Copyright 2015-2023 Markus Gans *
* Copyright 2015-2024 Markus Gans *
* *
* FINAL CUT is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
Expand Down Expand Up @@ -669,6 +669,19 @@ void FMenuBar::unselectMenuItem (FMenuItem* item)
setSelectedItem(nullptr);
}

//----------------------------------------------------------------------
inline auto FMenuBar::isClickOnMenuEntry ( const FMouseEvent* ev
, const FMenuItem* item ) const -> bool
{
const int mouse_x = ev->getX();
const int mouse_y = ev->getY();
const int x1 = item->getX();
const int x2 = item->getX() + int(item->getWidth());
return mouse_x >= x1
&& mouse_x < x2
&& mouse_y == 1;
}

//----------------------------------------------------------------------
void FMenuBar::mouseDownOverList (const FMouseEvent* ev)
{
Expand All @@ -678,17 +691,12 @@ void FMenuBar::mouseDownOverList (const FMouseEvent* ev)
return;

focus_changed = false;
int mouse_x = ev->getX();
int mouse_y = ev->getY();

for (auto&& item : list)
{
int x1 = item->getX();
int x2 = item->getX() + int(item->getWidth());

if ( mouse_y == 1 )
if ( ev->getY() == 1 )
{
if ( mouse_x >= x1 && mouse_x < x2 )
if ( isClickOnMenuEntry(ev, item) )
selectMenuItem (item); // Mouse pointer over item
else
unselectMenuItem (item);
Expand All @@ -715,17 +723,9 @@ void FMenuBar::mouseUpOverList (const FMouseEvent* ev)
if ( list.empty() )
return;

int mouse_x = ev->getX();
int mouse_y = ev->getY();

for (auto&& item : list)
{
int x1 = item->getX();
int x2 = item->getX() + int(item->getWidth());

if ( mouse_y == 1
&& mouse_x >= x1
&& mouse_x < x2
if ( isClickOnMenuEntry(ev, item)
&& item->isEnabled()
&& item->isSelected() )
{
Expand Down Expand Up @@ -754,20 +754,13 @@ void FMenuBar::mouseMoveOverList (const FMouseEvent& ev)

focus_changed = false;
bool mouse_over_menubar{false};
int mouse_x = ev.getX();
int mouse_y = ev.getY();

if ( getTermGeometry().contains(ev.getTermPos()) )
mouse_over_menubar = true;

for (auto&& item : list)
{
int x1 = item->getX();
int x2 = item->getX() + int(item->getWidth());

if ( mouse_x >= x1
&& mouse_x < x2
&& mouse_y == 1 )
if ( isClickOnMenuEntry(&ev, item) )
{
// Mouse pointer over item
selectMenuItem(item);
Expand Down
3 changes: 2 additions & 1 deletion final/menu/fmenubar.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* *
* This file is part of the FINAL CUT widget toolkit *
* *
* Copyright 2015-2022 Markus Gans *
* Copyright 2015-2024 Markus Gans *
* *
* FINAL CUT is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
Expand Down Expand Up @@ -131,6 +131,7 @@ class FMenuBar : public FWindow
auto clickItem (FMenuItem*) -> bool;
void unselectMenuItem (FMenuItem*);
void selectMenuItem (FMenuItem*);
auto isClickOnMenuEntry (const FMouseEvent*, const FMenuItem*) const -> bool;
void mouseDownOverList (const FMouseEvent*);
void mouseUpOverList (const FMouseEvent*);
void mouseMoveOverList (const FMouseEvent&);
Expand Down
8 changes: 2 additions & 6 deletions final/widget/fcombobox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,16 +345,12 @@ void FComboBox::onKeyPress (FKeyEvent* ev)
onePosDown();
ev->accept();
}
else if ( key == FKey::Meta_up
|| key == FKey::Ctrl_up
|| isEscapeKey(key) )
else if ( isCollapseComboBoxKey(key) )
{
hideDropDown();
ev->accept();
}
else if ( key == FKey::F4
|| key == FKey::Meta_down
|| key == FKey::Ctrl_down )
else if ( isExpandComboBoxKey(key) )
{
showDropDown();
ev->accept();
Expand Down
38 changes: 22 additions & 16 deletions final/widget/flistbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,31 +299,19 @@ void FListBox::onMouseDoubleClick (FMouseEvent* ev)
//----------------------------------------------------------------------
void FListBox::onTimer (FTimerEvent*)
{
const std::size_t current_before = selection.current;
const int yoffset_before = scroll.yoffset;

if ( ( drag_scroll == DragScrollMode::Upward
|| drag_scroll == DragScrollMode::SelectUpward )
&& ! dragScrollUp() )
{
if ( canSkipDragScrolling() )
return;
}

if ( ( drag_scroll == DragScrollMode::Downward
|| drag_scroll == DragScrollMode::SelectDownward )
&& ! dragScrollDown() )
{
return;
}
const std::size_t current_before = selection.current;
const int yoffset_before = scroll.yoffset;

if ( current_before != selection.current )
{
data.inc_search.clear();
processRowChanged();

// Handle multiple selections
if ( drag_scroll == DragScrollMode::SelectUpward
|| drag_scroll == DragScrollMode::SelectDownward )
if ( isDragSelect() )
multiSelectionUpTo(selection.current);
}

Expand Down Expand Up @@ -462,6 +450,24 @@ inline auto FListBox::getString (FListBoxItems::iterator iter) -> FString
return iter->getText();
}

//----------------------------------------------------------------------
inline auto FListBox::isDragSelect() const -> bool
{
return drag_scroll == DragScrollMode::SelectUpward
|| drag_scroll == DragScrollMode::SelectDownward;
}

//----------------------------------------------------------------------
inline auto FListBox::canSkipDragScrolling() -> bool
{
bool is_upward_scroll ( drag_scroll == DragScrollMode::Upward
|| drag_scroll == DragScrollMode::SelectUpward );
bool is_downward_scroll ( drag_scroll == DragScrollMode::Downward
|| drag_scroll == DragScrollMode::SelectDownward );
return ( is_upward_scroll && ! dragScrollUp() )
|| ( is_downward_scroll && ! dragScrollDown() );
}

//----------------------------------------------------------------------
void FListBox::init()
{
Expand Down
2 changes: 2 additions & 0 deletions final/widget/flistbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,8 @@ class FListBox : public FWidget
auto isHorizontallyScrollable() const -> bool;
auto isVerticallyScrollable() const -> bool;
auto isCurrentLine (int) const -> bool;
auto isDragSelect() const -> bool;
auto canSkipDragScrolling() -> bool;

// Methods
void init();
Expand Down
51 changes: 26 additions & 25 deletions final/widget/flistview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2672,7 +2672,7 @@ inline void FListView::toggleCheckbox()
if ( isItemListEmpty() )
return;

auto item = getCurrentItem();
const auto item = getCurrentItem();

if ( item->isCheckable() )
toggleItemCheckState(item);
Expand All @@ -2681,8 +2681,7 @@ inline void FListView::toggleCheckbox()
//----------------------------------------------------------------------
inline void FListView::collapseAndScrollLeft()
{
const int position_before = selection.current_iter.getPosition();
auto item = getCurrentItem();
const auto item = getCurrentItem();

if ( scroll.xoffset != 0 || ! item || isItemListEmpty() )
{
Expand All @@ -2703,40 +2702,42 @@ inline void FListView::collapseAndScrollLeft()
return;
}

if ( ! item->hasParent() )
jumpToParentElement(item);
}

//----------------------------------------------------------------------
inline void FListView::jumpToParentElement (const FListViewItem* item)
{
if ( ! item->hasParent()
|| ! item->getParent()->isInstanceOf("FListViewItem") )
return;

// Jump to parent element
const auto& parent = item->getParent();
const int position_before = selection.current_iter.getPosition();
selection.current_iter.parentElement(); // Set the iterator to the parent

if ( parent->isInstanceOf("FListViewItem") )
{
selection.current_iter.parentElement();
if ( selection.current_iter.getPosition() >= scroll.first_line_position_before )
return;

if ( selection.current_iter.getPosition() < scroll.first_line_position_before )
{
const int difference = position_before - selection.current_iter.getPosition();
const int difference = position_before - selection.current_iter.getPosition();

if ( scroll.first_visible_line.getPosition() - difference >= 0 )
{
scroll.first_visible_line -= difference;
scroll.last_visible_line -= difference;
}
else
{
const int d = scroll.first_visible_line.getPosition();
scroll.first_visible_line -= d;
scroll.last_visible_line -= d;
}
}
if ( scroll.first_visible_line.getPosition() - difference >= 0 )
{
scroll.first_visible_line -= difference;
scroll.last_visible_line -= difference;
}
else
{
const int d = scroll.first_visible_line.getPosition();
scroll.first_visible_line -= d;
scroll.last_visible_line -= d;
}
}

//----------------------------------------------------------------------
inline void FListView::expandAndScrollRight()
{
const int xoffset_end = int(max_line_width) - int(getClientWidth());
auto item = getCurrentItem();
const auto item = getCurrentItem();

if ( isTreeView() && ! isItemListEmpty() && item
&& item->isExpandable() && ! item->isExpand() )
Expand Down
Loading

0 comments on commit 130571a

Please sign in to comment.