Skip to content

Commit

Permalink
FarGroupgh-750: Refactored VMenu::SetSelectPos.
Browse files Browse the repository at this point in the history
  • Loading branch information
MKadaner committed Nov 26, 2023
1 parent 5ffec8a commit f6760a7
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
1 change: 1 addition & 0 deletions far/headers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <mutex>
#include <numeric>
#include <optional>
#include <ranges>
#include <random>
#include <regex>
#include <set>
Expand Down
66 changes: 63 additions & 3 deletions far/vmenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,14 @@ void VMenu::ResetCursor()
}

//может иметь фокус
static bool ItemCanHaveFocus(unsigned long long const Flags)
static bool ItemCanHaveFocusFlags(unsigned long long const Flags)
{
return !(Flags & (LIF_DISABLE | LIF_HIDDEN | LIF_FILTERED | LIF_SEPARATOR));
}

static bool ItemCanHaveFocus(MenuItemEx const& Item)
{
return ItemCanHaveFocus(Item.Flags);
return ItemCanHaveFocusFlags(Item.Flags);
}

//может быть выбран
Expand Down Expand Up @@ -177,7 +177,7 @@ void VMenu::UpdateItemFlags(int Pos, unsigned long long NewFlags)
--ItemHiddenCount;


if (!ItemCanHaveFocus(NewFlags))
if (!ItemCanHaveFocusFlags(NewFlags))
NewFlags &= ~LIF_SELECTED;

//remove selection
Expand Down Expand Up @@ -228,6 +228,66 @@ int VMenu::SetSelectPos(int Pos, int Direct, bool stop_on_edge)
i.Flags &= ~LIF_SELECTED;
}

const auto DoWrap{ CheckFlags(VMENU_WRAPMODE) && !Direct && !stop_on_edge };
const auto GoBackward{ Direct < 0 };
const auto ItemsSize{ static_cast<int>(Items.size()) };

if (Pos < 0)
{
if (DoWrap)
{
Pos = ItemsSize - 1;
TopPos = Pos;
}
else
{
Pos = 0;
TopPos = 0;
}
}
else if (Pos >= ItemsSize)
{
if (DoWrap)
{
Pos = 0;
TopPos = 0;
}
else
{
Pos = ItemsSize - 1;
}
}

const auto FindPos{
[](const auto& Base, const auto First, const auto Second)
{
const auto FindPos{
[](const auto& Base, const auto View)
{
if (auto Found{ std::ranges::find_if(View, ItemCanHaveFocus) }; Found != std::ranges::end(View))
{
return static_cast<int>(std::ranges::distance(&*std::ranges::begin(Base), &*Found));
}

return -1;
} };

if (const auto Found{ FindPos(Base, Base | First) }; Found != -1) return Found;
if (const auto Found{ FindPos(Base, Base | Second) }; Found != -1) return Found;
return -1;
} };

int Found{
GoBackward
? (DoWrap
? FindPos(Items, std::views::take(Pos + 1) | std::views::reverse, std::views::drop(Pos + 1) | std::views::reverse)
: FindPos(Items, std::views::take(Pos + 1) | std::views::reverse, std::views::drop(Pos + 1)))
: (DoWrap
? FindPos(Items, std::views::drop(Pos), std::views::take(Pos))
: FindPos(Items, std::views::drop(Pos), std::views::take(Pos) | std::views::reverse))
};


for (int Pass=0, I=0;;I++)
{
if (Pos<0)
Expand Down

0 comments on commit f6760a7

Please sign in to comment.