Skip to content

Commit

Permalink
UI中文件夹、播放列表、最近播放界面中处于搜索状态时,如果列表发生改变,则清除搜索状态
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongyang219 committed Aug 31, 2024
1 parent 6b7ddf6 commit 5cbed78
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 1 deletion.
9 changes: 8 additions & 1 deletion MusicPlayer2/Define.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,14 @@ const int EQU_STYLE_TABLE[9][EQU_CH_NUM] //均衡器预设
#define WM_RECENT_FOLDER_OR_PLAYLIST_CHANGED (WM_USER+140) //最近打开的文件夹或播放列表发生了改变
#define WM_SET_UI_FORCE_FRESH_FLAG (WM_USER+141) // 通知主窗口设置UI强制刷新标志m_ui_thread_para.ui_force_refresh

#define WM_NEXT_USER_MSG (WM_USER+142)
//通知主窗口清除UI中搜索框中搜索结果,其中wPara为搜索框关联列表元素的类型,在下面几行定义
//仅当列表元素的内容发生了改变但是总行数未变的情况下需要发送此消息,行数变化的情况已经在UiElement::ListElement::OnRowCountChanged中处理
#define WM_CLEAR_UI_SERCH_BOX ((WM_USER+142))
#define UI_LIST_TYPE_RECENT_PLAYED 0
#define UI_LIST_TYPE_FOLDER 1
#define UI_LIST_TYPE_PLAYLIST 2

#define WM_NEXT_USER_MSG (WM_USER+143)

#ifdef _DEBUG
#define ADD_TO_PLAYLIST_MAX_SIZE 10 //“添加到播放列表”子菜单中项目的最大数量(不能超过40)
Expand Down
27 changes: 27 additions & 0 deletions MusicPlayer2/MusicPlayerDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ BEGIN_MESSAGE_MAP(CMusicPlayerDlg, CMainDialogBase)
ON_MESSAGE(WM_SET_UI_FORCE_FRESH_FLAG, &CMusicPlayerDlg::OnSetUiForceFreshFlag)
ON_COMMAND(ID_MORE_RECENT_ITEMS, &CMusicPlayerDlg::OnMoreRecentItems)
ON_WM_NCCALCSIZE()
ON_MESSAGE(WM_CLEAR_UI_SERCH_BOX, &CMusicPlayerDlg::OnClearUiSerchBox)
END_MESSAGE_MAP()


Expand Down Expand Up @@ -6645,3 +6646,29 @@ void CMusicPlayerDlg::OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS* lpnc
}
CMainDialogBase::OnNcCalcSize(bCalcValidRects, lpncsp);
}


afx_msg LRESULT CMusicPlayerDlg::OnClearUiSerchBox(WPARAM wParam, LPARAM lParam)
{
for (auto& ui : m_ui_list)
{
CUserUi* cur_ui{ dynamic_cast<CUserUi*>(ui.get()) };
if (cur_ui != nullptr)
{
switch (wParam)
{
case UI_LIST_TYPE_RECENT_PLAYED:
cur_ui->ClearSearchResult<UiElement::RecentPlayedList>();
break;
case UI_LIST_TYPE_FOLDER:
cur_ui->ClearSearchResult<UiElement::MediaLibFolder>();
break;
case UI_LIST_TYPE_PLAYLIST:
cur_ui->ClearSearchResult<UiElement::MediaLibPlaylist>();
break;
}
}
}

return 0;
}
2 changes: 2 additions & 0 deletions MusicPlayer2/MusicPlayerDlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -539,4 +539,6 @@ class CMusicPlayerDlg : public CMainDialogBase
afx_msg void OnMoreRecentItems();
public:
afx_msg void OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS* lpncsp);
protected:
afx_msg LRESULT OnClearUiSerchBox(WPARAM wParam, LPARAM lParam);
};
1 change: 1 addition & 0 deletions MusicPlayer2/PlaylistMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,7 @@ bool CPlaylistMgr::SetSortMode(PlaylistSortMode sort_mode)
std::shared_lock<std::shared_mutex> lock(m_shared_mutex);
m_sort_mode = sort_mode;
SortPlaylist();
theApp.m_pMainWnd->PostMessage(WM_CLEAR_UI_SERCH_BOX, UI_LIST_TYPE_PLAYLIST);
return true;
}
return false;
Expand Down
1 change: 1 addition & 0 deletions MusicPlayer2/RecentFolderAndPlaylist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ void CRecentFolderAndPlaylist::Init()
//向主窗口发送通知
::SendMessage(AfxGetMainWnd()->GetSafeHwnd(), WM_RECENT_FOLDER_OR_PLAYLIST_CHANGED, 0, 0); // 重新初始化快捷菜单
::SendMessage(AfxGetMainWnd()->GetSafeHwnd(), WM_INIT_ADD_TO_MENU, 0, 0); // 重新初始化右键菜单中的“添加到播放列表”子菜单
theApp.m_pMainWnd->PostMessage(WM_CLEAR_UI_SERCH_BOX, UI_LIST_TYPE_RECENT_PLAYED); //清除UI中的搜索框
}

const std::vector<CRecentFolderAndPlaylist::Item>& CRecentFolderAndPlaylist::GetItemList() const
Expand Down
1 change: 1 addition & 0 deletions MusicPlayer2/RecentFolderMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ bool CRecentFolderMgr::SetSortMode(FolderSortMode sort_mode)
std::shared_lock<std::shared_mutex> lock(m_shared_mutex);
m_sort_mode = sort_mode;
SortPath();
theApp.m_pMainWnd->PostMessage(WM_CLEAR_UI_SERCH_BOX, UI_LIST_TYPE_FOLDER);
return true;
}
return false;
Expand Down
15 changes: 15 additions & 0 deletions MusicPlayer2/UIElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1498,6 +1498,9 @@ void UiElement::ListElement::OnRowCountChanged()
{
//如果列表的行数有变化,则清除选中
SelectNone();
//清除搜索框
if (related_search_box != nullptr)
related_search_box->Clear();
}

void UiElement::ListElement::QuickSearch(const std::wstring& key_word)
Expand Down Expand Up @@ -2969,6 +2972,11 @@ void UiElement::TreeElement::QuickSearch(const std::wstring& key_word)
}
}

void UiElement::TreeElement::OnRowCountChanged()
{
//树控件的行数变化可能只是节点的展开或折叠,因此不执行基类中OnRowCountChanged的处理。
}

int UiElement::TreeElement::GetItemLevel(int row)
{
const Node* node = GetNodeByIndex(row);
Expand Down Expand Up @@ -3351,6 +3359,11 @@ void UiElement::SearchBox::OnKeyWordsChanged()
list_element->QuickSearch(key_word);
}

void UiElement::SearchBox::Clear()
{
search_box_ctrl->Clear();
}

void UiElement::SearchBox::Draw()
{
CalculateRect();
Expand Down Expand Up @@ -3409,6 +3422,8 @@ void UiElement::SearchBox::FindListElement()
if (!find_list_element)
{
list_element = FindRelatedElement<ListElement>(this);
if (list_element != nullptr)
list_element->SetRelatedSearchBox(this);
find_list_element = true; //找过一次没找到就不找了
}
}
Expand Down
6 changes: 6 additions & 0 deletions MusicPlayer2/UIElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,8 @@ namespace UiElement
int GetDisplayRowCount(); //获取要显示的行数。(处于搜索状态时返回搜索结果数量,正常状态下同GetRowCount)
bool IsRowDisplayed(int row); //判断一行是否显示。(仅处于搜索状态时不匹配的行会返回false)

void SetRelatedSearchBox(SearchBox* search_box) { related_search_box = search_box; }

int item_height{ 28 };
int font_size{ 9 };

Expand Down Expand Up @@ -367,6 +369,7 @@ namespace UiElement
private:
std::vector<int> search_result; //保存搜索结果的序号
bool searched{}; //是否处于搜索状态
SearchBox* related_search_box{}; //关联的键框
};


Expand Down Expand Up @@ -762,6 +765,7 @@ namespace UiElement
int GetRowCount() override;
//树控件不使用基类ListElement的搜索逻辑
virtual void QuickSearch(const std::wstring& key_word) override;
virtual void OnRowCountChanged() override;

std::map<int, CRect> collapsd_rects; //折叠标志的矩形区域(key是行)
int collaps_indicator_hover_row{ -1 }; //鼠标指向的折叠标志的行号
Expand Down Expand Up @@ -841,6 +845,8 @@ namespace UiElement
~SearchBox();
void InitSearchBoxControl(CWnd* pWnd); //初始化搜索框控件。pWnd:父窗口
void OnKeyWordsChanged();
void Clear();
ListElement* GetListElement() { return list_element; }

virtual void Draw() override;
virtual void MouseMove(CPoint point) override;
Expand Down
22 changes: 22 additions & 0 deletions MusicPlayer2/UserUi.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ class CUserUi :
void PlaylistLocateToCurrent(); //播放列表控件使正在播放的条目可见
void ListLocateToCurrent(); //ui中的所有列表使正在播放的条目可见
void InitSearchBox(CWnd* pWnd);

//清除所有搜索框的搜索状态。其中模板参数T是搜索框关联的列表元素的类型
template<class T>
void ClearSearchResult();

void SaveStatackElementIndex(CArchive& archive);
void LoadStatackElementIndex(CArchive& archive);

Expand Down Expand Up @@ -82,6 +87,23 @@ class CUserUi :
virtual void SwitchStackElement() override;
};

template<class T>
inline void CUserUi::ClearSearchResult()
{
IterateAllElementsInAllUi([&](UiElement::Element* element) ->bool {
UiElement::SearchBox* search_box{ dynamic_cast<UiElement::SearchBox*>(element) };
if (search_box != nullptr)
{
T* list_emelent = dynamic_cast<T*>(search_box->GetListElement());
if (list_emelent != nullptr)
{
search_box->Clear();
}
}
return false;
});
}

template<class T>
inline T* CUserUi::FindElement()
{
Expand Down

0 comments on commit 5cbed78

Please sign in to comment.