diff --git a/far/vmenu.cpp b/far/vmenu.cpp index 3eac0acf65d..2fea4f9e7e5 100644 --- a/far/vmenu.cpp +++ b/far/vmenu.cpp @@ -140,12 +140,13 @@ void VMenu::ResetCursor() GetCursorType(PrevCursorVisible,PrevCursorSize); } -template >> pred> -int FindNearest(coll&& Coll, const int Pos, pred&& Pred, const bool GoBackward, const bool DoWrap) +template + requires (std::ranges::contiguous_range && std::ranges::sized_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(Coll.size())); + assert(0 <= Pos && Pos < static_cast(Range.size())); const auto FindPos = [&](const auto First, const auto Second) @@ -153,8 +154,8 @@ int FindNearest(coll&& Coll, const int Pos, pred&& Pred, const bool GoBackward, const auto FindPosPart = [&](const auto Part) { - if (auto Filtered = Coll | Part | filter(std::forward(Pred))) - return static_cast(&Filtered.front() - Coll.data()); + if (auto Filtered = Range | Part | filter(std::forward(Pred))) + return static_cast(&Filtered.front() - Range.data()); return -1; }; @@ -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)); } @@ -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 @@ -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++; } @@ -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)) { @@ -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); } @@ -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; } @@ -603,7 +604,7 @@ void VMenu::RestoreFilteredItems() continue; i.Flags &= ~MIF_FILTERED; - if (ItemIsVisible(i)) + if (item_is_visible(i)) --ItemHiddenCount; } @@ -636,7 +637,7 @@ void VMenu::FilterStringUpdated() { CurItem.Flags &= ~LIF_FILTERED; - if (!ItemIsVisible(CurItem)) + if (!item_is_visible(CurItem)) { ++ItemHiddenCount; continue; @@ -680,12 +681,12 @@ void VMenu::FilterStringUpdated() PrevGroup = static_cast(index); if (LowerVisible == -2) { - if (ItemCanHaveFocus(CurItem)) + if (item_can_have_focus(CurItem)) UpperVisible = static_cast(index); } else if (LowerVisible == -1) { - if (ItemCanHaveFocus(CurItem)) + if (item_can_have_focus(CurItem)) LowerVisible = static_cast(index); } // Этот разделитель - оставить видимым @@ -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; @@ -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()) { @@ -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(Items.size()) && ItemCanHaveFocus(Items[MsPos])) + if (MsPos>=0 && MsPos(Items.size()) && item_can_have_focus(Items[MsPos])) { if (IntKeyState.MousePos.x != IntKeyState.MousePrevPos.x || IntKeyState.MousePos.y != IntKeyState.MousePrevPos.y || IsMouseButtonEvent(MouseEvent->dwEventFlags)) { @@ -1752,7 +1753,7 @@ int VMenu::GetVisualPos(int Pos) const if (Pos >= static_cast(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 @@ -1766,7 +1767,7 @@ int VMenu::VisualPosToReal(int VPos) const if (VPos >= GetShowItemCount()) return static_cast(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; } @@ -2136,7 +2137,7 @@ void VMenu::ShowMenu(bool IsParent) if (I < static_cast(Items.size())) { - if (!ItemIsVisible(Items[I])) + if (!item_is_visible(Items[I])) { Y--; continue; @@ -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])) @@ -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(Iterator - Items.cbegin()); if (ChangePos) @@ -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); } @@ -3002,10 +3003,10 @@ TEST_CASE("find.nearest.selectable.item") { for (const auto Pos : iota(0, static_cast(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); } } }; @@ -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); } }