Skip to content

Commit

Permalink
Renamed functions to snake_case and clarified concepts.
Browse files Browse the repository at this point in the history
  • Loading branch information
MKadaner committed Dec 6, 2023
1 parent 72e10ca commit 0167066
Showing 1 changed file with 37 additions and 36 deletions.
73 changes: 37 additions & 36 deletions far/vmenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,21 +140,22 @@ void VMenu::ResetCursor()
GetCursorType(PrevCursorVisible,PrevCursorSize);
}

template <std::ranges::random_access_range coll, std::indirect_unary_predicate<std::ranges::iterator_t<std::remove_cvref_t<coll>>> pred>
int FindNearest(coll&& Coll, const int Pos, pred&& Pred, const bool GoBackward, const bool DoWrap)
template <typename range>
requires (std::ranges::contiguous_range<range> && std::ranges::sized_range<range>)
int find_nearest(range&& Range, const int Pos, auto&& Pred, const bool GoBackward, const bool DoWrap)
{
using namespace std::views;

assert(0 <= Pos && Pos < static_cast<int>(Coll.size()));
assert(0 <= Pos && Pos < static_cast<int>(Range.size()));

const auto FindPos =
[&](const auto First, const auto Second)
{
const auto FindPosPart =
[&](const auto Part)
{
if (auto Filtered = Coll | Part | filter(std::forward<pred>(Pred)))
return static_cast<int>(&Filtered.front() - Coll.data());
if (auto Filtered = Range | Part | filter(std::forward<decltype(Pred)>(Pred)))
return static_cast<int>(&Filtered.front() - Range.data());

return -1;
};
Expand All @@ -174,24 +175,24 @@ int FindNearest(coll&& Coll, const int Pos, pred&& Pred, const bool GoBackward,
}

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

static bool ItemCanHaveFocus(MenuItemEx const& Item)
static bool item_can_have_focus(MenuItemEx const& Item)
{
return ItemFlagsAllowFocus(Item.Flags);
return item_flags_allow_focus(Item.Flags);
}

//может быть выбран
static bool ItemCanBeEntered(MenuItemEx const& Item)
static bool item_can_be_entered(MenuItemEx const& Item)
{
return ItemCanHaveFocus(Item) && !(Item.Flags & LIF_GRAYED);
return item_can_have_focus(Item) && !(Item.Flags & LIF_GRAYED);
}

//видимый
static bool ItemIsVisible(MenuItemEx const& Item)
static bool item_is_visible(MenuItemEx const& Item)
{
return !(Item.Flags & (LIF_HIDDEN | LIF_FILTERED));
}
Expand All @@ -206,11 +207,11 @@ void VMenu::UpdateItemFlags(int Pos, unsigned long long NewFlags)
if (Items[Pos].Flags & MIF_SUBMENU)
--ItemSubMenusCount;

if (!ItemIsVisible(Items[Pos]))
if (!item_is_visible(Items[Pos]))
--ItemHiddenCount;


if (!ItemFlagsAllowFocus(NewFlags))
if (!item_flags_allow_focus(NewFlags))
NewFlags &= ~LIF_SELECTED;

//remove selection
Expand Down Expand Up @@ -244,7 +245,7 @@ void VMenu::UpdateItemFlags(int Pos, unsigned long long NewFlags)
if (NewFlags&MIF_SUBMENU)
ItemSubMenusCount++;

if (!ItemIsVisible(Items[Pos]))
if (!item_is_visible(Items[Pos]))
ItemHiddenCount++;
}

Expand Down Expand Up @@ -274,7 +275,7 @@ int VMenu::SetSelectPos(int Pos, int Direct, bool stop_on_edge)
Pos = DoWrap ? 0 : ItemsSize - 1;
}

Pos = FindNearest(Items, Pos, ItemCanHaveFocus, GoBackward, DoWrap);
Pos = find_nearest(Items, Pos, item_can_have_focus, GoBackward, DoWrap);

if (Pos != SelectPos && CheckFlags(VMENU_COMBOBOX | VMENU_LISTBOX))
{
Expand Down Expand Up @@ -336,12 +337,12 @@ void VMenu::UpdateSelectPos()
return;

// если selection стоит в некорректном месте - сбросим его
if (SelectPos >= 0 && !ItemCanHaveFocus(Items[SelectPos]))
if (SelectPos >= 0 && !item_can_have_focus(Items[SelectPos]))
SelectPos = -1;

for (const auto& [Item, Index]: enumerate(Items))
{
if (!ItemCanHaveFocus(Item))
if (!item_can_have_focus(Item))
{
Item.SetSelect(false);
}
Expand Down Expand Up @@ -505,7 +506,7 @@ int VMenu::DeleteItem(int ID, int Count)
if (Items[ID+I].Flags & MIF_SUBMENU)
--ItemSubMenusCount;

if (!ItemIsVisible(Items[ID+I]))
if (!item_is_visible(Items[ID+I]))
--ItemHiddenCount;
}

Expand Down Expand Up @@ -603,7 +604,7 @@ void VMenu::RestoreFilteredItems()
continue;

i.Flags &= ~MIF_FILTERED;
if (ItemIsVisible(i))
if (item_is_visible(i))
--ItemHiddenCount;
}

Expand Down Expand Up @@ -636,7 +637,7 @@ void VMenu::FilterStringUpdated()
{
CurItem.Flags &= ~LIF_FILTERED;

if (!ItemIsVisible(CurItem))
if (!item_is_visible(CurItem))
{
++ItemHiddenCount;
continue;
Expand Down Expand Up @@ -680,12 +681,12 @@ void VMenu::FilterStringUpdated()
PrevGroup = static_cast<int>(index);
if (LowerVisible == -2)
{
if (ItemCanHaveFocus(CurItem))
if (item_can_have_focus(CurItem))
UpperVisible = static_cast<int>(index);
}
else if (LowerVisible == -1)
{
if (ItemCanHaveFocus(CurItem))
if (item_can_have_focus(CurItem))
LowerVisible = static_cast<int>(index);
}
// Этот разделитель - оставить видимым
Expand Down Expand Up @@ -832,7 +833,7 @@ long long VMenu::VMProcess(int OpCode, void* vParam, long long iParam)

const auto& Item = at(I);

if (!ItemCanHaveFocus(Item))
if (!item_can_have_focus(Item))
continue;

int Res = 0;
Expand Down Expand Up @@ -1179,7 +1180,7 @@ bool VMenu::ProcessKey(const Manager::Key& Key)

const auto ProcessEnter = [this]()
{
if (ItemCanBeEntered(Items[SelectPos]))
if (item_can_be_entered(Items[SelectPos]))
{
if (IsComboBox())
{
Expand Down Expand Up @@ -1697,7 +1698,7 @@ bool VMenu::ProcessMouse(const MOUSE_EVENT_RECORD *MouseEvent)
{
const auto MsPos = VisualPosToReal(GetVisualPos(TopPos) + MsY - m_Where.top - (m_BoxType == NO_BOX? 0 : 1));

if (MsPos>=0 && MsPos<static_cast<int>(Items.size()) && ItemCanHaveFocus(Items[MsPos]))
if (MsPos>=0 && MsPos<static_cast<int>(Items.size()) && item_can_have_focus(Items[MsPos]))
{
if (IntKeyState.MousePos.x != IntKeyState.MousePrevPos.x || IntKeyState.MousePos.y != IntKeyState.MousePrevPos.y || IsMouseButtonEvent(MouseEvent->dwEventFlags))
{
Expand Down Expand Up @@ -1752,7 +1753,7 @@ int VMenu::GetVisualPos(int Pos) const
if (Pos >= static_cast<int>(Items.size()))
return GetShowItemCount();

return std::count_if(Items.cbegin(), Items.cbegin() + Pos, [](const auto& Item) { return ItemIsVisible(Item); });
return std::count_if(Items.cbegin(), Items.cbegin() + Pos, [](const auto& Item) { return item_is_visible(Item); });
}

int VMenu::VisualPosToReal(int VPos) const
Expand All @@ -1766,7 +1767,7 @@ int VMenu::VisualPosToReal(int VPos) const
if (VPos >= GetShowItemCount())
return static_cast<int>(Items.size());

const auto ItemIterator = std::find_if(CONST_RANGE(Items, i) { return ItemIsVisible(i) && !VPos--; });
const auto ItemIterator = std::find_if(CONST_RANGE(Items, i) { return item_is_visible(i) && !VPos--; });
return ItemIterator != Items.cend()? ItemIterator - Items.cbegin() : -1;
}

Expand Down Expand Up @@ -2136,7 +2137,7 @@ void VMenu::ShowMenu(bool IsParent)

if (I < static_cast<int>(Items.size()))
{
if (!ItemIsVisible(Items[I]))
if (!item_is_visible(Items[I]))
{
Y--;
continue;
Expand Down Expand Up @@ -2386,7 +2387,7 @@ int VMenu::CheckHighlights(wchar_t CheckSymbol, int StartPos) const

for (const auto& I: irange(StartPos, Items.size()))
{
if (!ItemIsVisible(Items[I]))
if (!item_is_visible(Items[I]))
continue;

if (const auto Ch = GetHighlights(&Items[I]))
Expand Down Expand Up @@ -2484,7 +2485,7 @@ bool VMenu::CheckKeyHiOrAcc(DWORD Key, int Type, bool Translate, bool ChangePos,
FOR_CONST_RANGE(Items, Iterator)
{
auto& CurItem = *Iterator;
if (ItemCanHaveFocus(CurItem) && ((!Type && CurItem.AccelKey && Key == CurItem.AccelKey) || (Type && (CurItem.AutoHotkey || !CheckFlags(VMENU_SHOWAMPERSAND)) && IsKeyHighlighted(CurItem.Name, Key, Translate, CurItem.AutoHotkey))))
if (item_can_have_focus(CurItem) && ((!Type && CurItem.AccelKey && Key == CurItem.AccelKey) || (Type && (CurItem.AutoHotkey || !CheckFlags(VMENU_SHOWAMPERSAND)) && IsKeyHighlighted(CurItem.Name, Key, Translate, CurItem.AutoHotkey))))
{
NewPos=static_cast<int>(Iterator - Items.cbegin());
if (ChangePos)
Expand All @@ -2493,7 +2494,7 @@ bool VMenu::CheckKeyHiOrAcc(DWORD Key, int Type, bool Translate, bool ChangePos,
ShowMenu(true);
}

if ((!GetDialog() || CheckFlags(VMENU_COMBOBOX|VMENU_LISTBOX)) && ItemCanBeEntered(Items[SelectPos]))
if ((!GetDialog() || CheckFlags(VMENU_COMBOBOX|VMENU_LISTBOX)) && item_can_be_entered(Items[SelectPos]))
{
SetExitCode(NewPos);
}
Expand Down Expand Up @@ -3002,10 +3003,10 @@ TEST_CASE("find.nearest.selectable.item")
{
for (const auto Pos : iota(0, static_cast<int>(arr.size())))
{
REQUIRE(FindNearest(arr, Pos, Pred, false, false) == Found);
REQUIRE(FindNearest(arr, Pos, Pred, false, true) == Found);
REQUIRE(FindNearest(arr, Pos, Pred, true, false) == Found);
REQUIRE(FindNearest(arr, Pos, Pred, true, true) == Found);
REQUIRE(find_nearest(arr, Pos, Pred, false, false) == Found);
REQUIRE(find_nearest(arr, Pos, Pred, false, true) == Found);
REQUIRE(find_nearest(arr, Pos, Pred, true, false) == Found);
REQUIRE(find_nearest(arr, Pos, Pred, true, true) == Found);
}
} };

Expand Down Expand Up @@ -3039,7 +3040,7 @@ TEST_CASE("find.nearest.selectable.item")

for (const auto& TestDataPoint : TestDataPoints)
{
REQUIRE(FindNearest(arr, TestDataPoint.Pos, Pred, TestDataPoint.GoBackward, TestDataPoint.DoWrap) == TestDataPoint.Expected);
REQUIRE(find_nearest(arr, TestDataPoint.Pos, Pred, TestDataPoint.GoBackward, TestDataPoint.DoWrap) == TestDataPoint.Expected);
}
}

Expand Down

0 comments on commit 0167066

Please sign in to comment.