Skip to content

Commit

Permalink
* Use slightly different sorting criteria for quick switcher.
Browse files Browse the repository at this point in the history
  • Loading branch information
iProgramMC committed Apr 29, 2024
1 parent 5b840d4 commit ec2fcdf
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 16 deletions.
6 changes: 3 additions & 3 deletions src/discord/DiscordInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ void DiscordInstance::SearchSubGuild(std::vector<QuickMatch>& matches, Guild* pG

float fzc = CompareFuzzy(chan.m_name, queryPtr);
if (fzc != 0.0f)
matches.push_back(QuickMatch(true, chan.m_snowflake, fzc));
matches.push_back(QuickMatch(true, chan.m_snowflake, fzc, chan.m_name));
}
}

Expand All @@ -319,7 +319,7 @@ std::vector<QuickMatch> DiscordInstance::Search(const std::string& query)
// Calculate a fake fuzzy factor to avoid effects of sorting.
float ff = float(C_CHANNEL_HISTORY_MAX - i) / float(C_CHANNEL_HISTORY_MAX);

matches.push_back(QuickMatch(true, chan, ff));
matches.push_back(QuickMatch(true, chan, ff, pChan->m_name));
}
}
else
Expand Down Expand Up @@ -357,7 +357,7 @@ std::vector<QuickMatch> DiscordInstance::Search(const std::string& query)
float fzc = CompareFuzzy(gld.m_name, queryPtr);

if (fzc != 0.0f)
matches.push_back(QuickMatch(false, gld.m_snowflake, fzc));
matches.push_back(QuickMatch(false, gld.m_snowflake, fzc, gld.m_name));
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/discord/DiscordInstance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ struct ChannelHistory
class QuickMatch
{
public:
QuickMatch(bool channel, Snowflake id, float fzc) : m_isChannel(channel), m_id(id), m_fuzzy(fzc) {}
QuickMatch(bool channel, Snowflake id, float fzc, const std::string& name) :
m_isChannel(channel), m_id(id), m_fuzzy(fzc), m_name(name) {}
bool IsChannel() const { return m_isChannel; }
bool IsGuild() const { return !m_isChannel; }
Snowflake Id() const { return m_id; }
Expand All @@ -75,11 +76,16 @@ class QuickMatch
if (!m_isChannel && oth.m_isChannel)
return false;

int res = strcmp(m_name.c_str(), oth.m_name.c_str());
if (res != 0)
return res < 0;

return m_id < oth.m_id;
}

private:
Snowflake m_id = 0;
std::string m_name;
bool m_isChannel = false;
float m_fuzzy = 0;
};
Expand Down
53 changes: 41 additions & 12 deletions src/windows/QuickSwitcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

enum {
COL_CHANNEL_NAME,
COL_CATEGORY_NAME,
COL_GUILD_NAME,
};

Expand Down Expand Up @@ -35,10 +36,11 @@ struct QuickSwitchItem
int m_iconIdx;
Snowflake m_guildID;
std::string m_name;
std::string m_categ;
std::string m_guild;

QuickSwitchItem(QuickMatch& qm, int hi, const std::string& nm, const std::string& gl, Snowflake gi):
m_match(qm), m_iconIdx(hi), m_name(nm), m_guild(gl), m_guildID(gi) {};
QuickSwitchItem(QuickMatch& qm, int hi, const std::string& nm, const std::string& ct, const std::string& gl, Snowflake gi):
m_match(qm), m_iconIdx(hi), m_name(nm), m_categ(ct), m_guild(gl), m_guildID(gi) {};
};

static std::vector<QuickSwitchItem> g_qsItems;
Expand Down Expand Up @@ -95,6 +97,7 @@ void QuickSwitcher::OnUpdateQuery(HWND hWnd, const std::string& query)
{
std::string name = "";
std::string guildName = "";
std::string categName = "";

int iconIdx = ICN_GUILD;
Snowflake guildID = 0;
Expand All @@ -119,6 +122,10 @@ void QuickSwitcher::OnUpdateQuery(HWND hWnd, const std::string& query)

guildName = pGuild->m_name;
guildID = pChan->m_parentGuild;

Channel* pCateg = pGuild->GetChannel(pChan->m_parentCateg);
if (pCateg && pCateg != pChan)
categName = pCateg->m_name;
}
else if (match.IsGuild())
{
Expand All @@ -134,7 +141,7 @@ void QuickSwitcher::OnUpdateQuery(HWND hWnd, const std::string& query)
}

int idx = int(g_qsItems.size());
g_qsItems.push_back(QuickSwitchItem(match, iconIdx, name, guildName, guildID));
g_qsItems.push_back(QuickSwitchItem(match, iconIdx, name, categName, guildName, guildID));

LVITEM lv{};
lv.mask = LVIF_TEXT | LVIF_IMAGE;
Expand Down Expand Up @@ -186,18 +193,24 @@ void QuickSwitcher::HandleGetDispInfo(NMLVDISPINFO* pInfo)

static TCHAR buffer1[4096];
static TCHAR buffer2[4096];
static TCHAR buffer3[4096];
switch (pInfo->item.iSubItem)
{
case COL_CHANNEL_NAME: {
ConvertCppStringToTCharArray(item.m_name, buffer1, _countof(buffer1));
pInfo->item.pszText = buffer1;
break;
}
case COL_GUILD_NAME: {
ConvertCppStringToTCharArray(item.m_guild, buffer2, _countof(buffer2));
case COL_CATEGORY_NAME: {
ConvertCppStringToTCharArray(item.m_categ, buffer2, _countof(buffer2));
pInfo->item.pszText = buffer2;
break;
}
case COL_GUILD_NAME: {
ConvertCppStringToTCharArray(item.m_guild, buffer3, _countof(buffer3));
pInfo->item.pszText = buffer3;
break;
}
}
}

Expand Down Expand Up @@ -229,24 +242,35 @@ LRESULT QuickSwitcher::HandleCustomDraw(HWND hWnd, NMLVCUSTOMDRAW* pInfo)
case CDDS_PREPAINT:
return CDRF_NOTIFYITEMDRAW;

case CDDS_ITEMPREPAINT: {
case CDDS_ITEMPREPAINT:
return CDRF_NOTIFYSUBITEMDRAW;

case CDDS_SUBITEM | CDDS_ITEMPREPAINT: {
HWND hList = GetDlgItem(hWnd, IDC_CHANNEL_LIST);

int idx = pInfo->nmcd.dwItemSpec;
int sta = ListView_GetItemState(hList, idx, LVIS_SELECTED);

if (sta & LVIS_SELECTED) {
pInfo->clrTextBk = GetSysColor(COLOR_HIGHLIGHT);
pInfo->clrText = GetSysColor(COLOR_HIGHLIGHTTEXT);
SetBkColor(pInfo->nmcd.hdc, RGB(255, 0, 0));
return CDRF_NEWFONT;
}
else if (pInfo->iSubItem == COL_CATEGORY_NAME) {
pInfo->clrTextBk = GetSysColor(COLOR_WINDOW);
pInfo->clrText = GetSysColor(COLOR_GRAYTEXT);
}
else {
pInfo->clrTextBk = GetSysColor(COLOR_WINDOW);
pInfo->clrText = GetSysColor(COLOR_MENUTEXT);
}

return CDRF_DODEFAULT;
return CDRF_NEWFONT;
}

default:DbgPrintW("Unhandled draw stage %d", pInfo->nmcd.dwDrawStage);
}

return CDRF_SKIPDEFAULT;
return CDRF_DODEFAULT;
}

void QuickSwitcher::CreateImageList()
Expand Down Expand Up @@ -336,11 +360,16 @@ INT_PTR QuickSwitcher::DialogProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lP
LVCOLUMN col{};
col.mask = LVCF_SUBITEM | LVCF_TEXT | LVCF_WIDTH | LVCF_FMT;
col.pszText = TEXT("Channel");
col.cx = MulDiv(width, 70, 100);
col.cx = MulDiv(width, 50, 100);
col.iSubItem = 0;
col.fmt = LVCFMT_LEFT;
ListView_InsertColumn(hList, COL_CHANNEL_NAME, &col);

col.pszText = TEXT("Category");
col.cx = MulDiv(width, 20, 100);
col.fmt = LVCFMT_RIGHT;
ListView_InsertColumn(hList, COL_CATEGORY_NAME, &col);

col.pszText = TEXT("Server");
col.cx = MulDiv(width, 30, 100);
col.fmt = LVCFMT_RIGHT;
Expand Down

0 comments on commit ec2fcdf

Please sign in to comment.