Skip to content

Commit

Permalink
任务栏中的插件项目支持自由排序
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongyang219 committed Jan 4, 2022
1 parent 72356ad commit b6d9b42
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 105 deletions.
1 change: 1 addition & 0 deletions TrafficMonitor/DrawCommon.cpp
Expand Up @@ -21,6 +21,7 @@ void CDrawCommon::Create(CDC* pDC, CWnd* pMainWnd)
void CDrawCommon::SetFont(CFont* pfont)
{
m_pfont = pfont;
m_pDC->SelectObject(m_pfont);
}

void CDrawCommon::SetDC(CDC* pDC)
Expand Down
65 changes: 36 additions & 29 deletions TrafficMonitor/SetItemOrderDlg.cpp
Expand Up @@ -12,9 +12,9 @@
IMPLEMENT_DYNAMIC(CSetItemOrderDlg, CBaseDialog)

CSetItemOrderDlg::CSetItemOrderDlg(CWnd* pParent /*=nullptr*/)
: CBaseDialog(IDD_SELECT_ORDER_DIALOG, pParent), m_item_order(true)
: CBaseDialog(IDD_SELECT_ORDER_DIALOG, pParent), m_item_order(true)
{

m_item_order.Init();
}

CSetItemOrderDlg::~CSetItemOrderDlg()
Expand Down Expand Up @@ -86,23 +86,14 @@ BOOL CSetItemOrderDlg::OnInitDialog()

void CSetItemOrderDlg::ShowItem()
{
//向列表中添加内建项目
//向列表中添加项目
m_list_ctrl.ResetContent();
auto item_list = m_item_order.GetAllDisplayItemsWithOrder();
m_item_coumt = static_cast<int>(item_list.size());
for (const auto& item : item_list)
{
m_list_ctrl.AddString(CTaskbarItemOrderHelper::GetItemDisplayName(item));
if (m_display_item & item)
m_list_ctrl.SetCheck(m_list_ctrl.GetCount() - 1, TRUE);
else
m_list_ctrl.SetCheck(m_list_ctrl.GetCount() - 1, FALSE);
}
//向列表中添加插件项目
for (const auto& plugin_item : theApp.m_plugins.GetPluginItems())
{
m_list_ctrl.AddString(plugin_item->GetItemName());
if (m_plugin_item.Contains(plugin_item->GetItemId()))
if (GetItemChecked(item))
m_list_ctrl.SetCheck(m_list_ctrl.GetCount() - 1, TRUE);
else
m_list_ctrl.SetCheck(m_list_ctrl.GetCount() - 1, FALSE);
Expand All @@ -122,6 +113,36 @@ void CSetItemOrderDlg::EnableDlgCtrl(UINT id, bool enable)
pCtrl->EnableWindow(enable);
}

bool CSetItemOrderDlg::GetItemChecked(CommonDisplayItem item)
{
if (item.is_plugin)
{
if (item.plugin_item != nullptr)
return m_plugin_item.Contains(item.plugin_item->GetItemId());
}
else
{
return m_display_item & item.item_type;
}
return false;
}

void CSetItemOrderDlg::SaveItemChecked(CommonDisplayItem item, bool checked)
{
if (item.is_plugin)
{
if (item.plugin_item != nullptr)
m_plugin_item.SetStrContained(item.plugin_item->GetItemId(), checked);
}
else
{
if (checked)
m_display_item |= item.item_type;
else
m_display_item &= ~item.item_type;
}
}


void CSetItemOrderDlg::OnBnClickedMoveUpButton()
{
Expand Down Expand Up @@ -188,26 +209,12 @@ void CSetItemOrderDlg::OnOK()
for (; i < static_cast<int>(item_list.size()); i++)
{
bool is_checked = (m_list_ctrl.GetCheck(i) != 0);
DisplayItem item = item_list[i];
if (is_checked)
m_display_item |= item;
else
m_display_item &= ~item;
CommonDisplayItem item = item_list[i];
SaveItemChecked(item, is_checked);
}

if (m_display_item == 0)
m_display_item = TDI_UP;

for (; i < m_list_ctrl.GetCount(); i++)
{
bool is_checked = (m_list_ctrl.GetCheck(i) != 0);
int plugin_index = i - static_cast<int>(item_list.size());
IPluginItem* plugin_item = theApp.m_plugins.GetItemByIndex(plugin_index);
if (plugin_item != nullptr)
{
m_plugin_item.SetStrContained(plugin_item->GetItemId(), is_checked);
}
}

CBaseDialog::OnOK();
}
17 changes: 10 additions & 7 deletions TrafficMonitor/SetItemOrderDlg.h
Expand Up @@ -5,11 +5,11 @@

class CSetItemOrderDlg : public CBaseDialog
{
DECLARE_DYNAMIC(CSetItemOrderDlg)
DECLARE_DYNAMIC(CSetItemOrderDlg)

public:
CSetItemOrderDlg(CWnd* pParent = nullptr); // 标准构造函数
virtual ~CSetItemOrderDlg();
CSetItemOrderDlg(CWnd* pParent = nullptr); // 标准构造函数
virtual ~CSetItemOrderDlg();

//设置/获取显示顺序
void SetItemOrder(const std::vector<int>& item_order);
Expand All @@ -22,9 +22,9 @@ class CSetItemOrderDlg : public CBaseDialog
void SetPluginDisplayItem(const StringSet& plugin_item);
const StringSet& GetPluginDisplayItem() const;

// 对话框数据
// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_SELECT_ORDER_DIALOG };
enum { IDD = IDD_SELECT_ORDER_DIALOG };
#endif

private:
Expand All @@ -35,15 +35,18 @@ class CSetItemOrderDlg : public CBaseDialog
int m_item_coumt{}; //除插件项目以外的项目的个数

protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
void ShowItem();
void EnableCtrl(int list_selected);
void EnableDlgCtrl(UINT id, bool enable);

bool GetItemChecked(CommonDisplayItem item);
void SaveItemChecked(CommonDisplayItem item, bool checked);

// 通过 CBaseDialog 继承
virtual CString GetDialogName() const override;

DECLARE_MESSAGE_MAP()
DECLARE_MESSAGE_MAP()
public:
virtual BOOL OnInitDialog();
afx_msg void OnBnClickedMoveUpButton();
Expand Down
66 changes: 39 additions & 27 deletions TrafficMonitor/TaskBarDlg.cpp
Expand Up @@ -697,15 +697,37 @@ void CTaskBarDlg::CalculateWindowSize()
theApp.m_taskbar_data.m_tbar_display_item |= TDI_UP; //至少显示一项

m_item_widths.clear();
//内置显示项目的宽度
std::map<DisplayItem, ItemWidth> item_widths;
//显示项目的宽度
std::map<CommonDisplayItem, ItemWidth> item_widths;

m_pDC->SelectObject(&m_font);
//计算标签宽度
//const auto& item_map = theApp.m_taskbar_data.disp_str.GetAllItems();
for (auto iter = AllDisplayItems.begin(); iter != AllDisplayItems.end(); ++iter)
for (auto iter = theApp.m_plugins.AllDisplayItemsWithPlugins().begin(); iter != theApp.m_plugins.AllDisplayItemsWithPlugins().end(); ++iter)
{
item_widths[*iter].label_width = m_pDC->GetTextExtent(theApp.m_taskbar_data.disp_str.Get(*iter).c_str()).cx;
if (iter->is_plugin)
{
auto plugin = iter->plugin_item;
if (plugin != nullptr)
{
int& label_width{ item_widths[*iter].label_width };
if (plugin->IsCustomDraw())
{
label_width = 0;
}
else
{
CString lable_text = theApp.m_taskbar_data.disp_str.Get(plugin).c_str();
if (!lable_text.IsEmpty())
lable_text += L' ';
label_width = m_pDC->GetTextExtent(lable_text).cx;
}
}
}
else
{
item_widths[*iter].label_width = m_pDC->GetTextExtent(theApp.m_taskbar_data.disp_str.Get(*iter).c_str()).cx;
}
}

//计算数值部分宽度
Expand Down Expand Up @@ -779,40 +801,30 @@ void CTaskBarDlg::CalculateWindowSize()
item_widths[TDI_HDD_TEMP].value_width = value_width;
item_widths[TDI_MAIN_BOARD_TEMP].value_width = value_width;

auto item_order{ theApp.m_taskbar_data.item_order.GetAllDisplayItemsWithOrder() };
for (const auto& item : item_order)
{
if (theApp.m_taskbar_data.m_tbar_display_item & item)
{
ItemWidthInfo width_info;
width_info.is_plugin = false;
width_info.item_type = item;
width_info.item_width = item_widths[item];
m_item_widths.push_back(width_info);
}
}

//计算插件项目的宽度
for (const auto& plugin : theApp.m_plugins.GetPluginItems())
{
int& value_width{ item_widths[plugin].value_width };
if (plugin != nullptr && theApp.m_taskbar_data.plugin_display_item.Contains(plugin->GetItemId()))
{
ItemWidthInfo width_info;
width_info.is_plugin = true;
width_info.plugin_item = plugin;
if (plugin->IsCustomDraw())
{
width_info.item_width.label_width = 0;
width_info.item_width.value_width = theApp.DPI(plugin->GetItemWidth());
value_width = theApp.DPI(plugin->GetItemWidth());
}
else
{
CString lable_text = theApp.m_taskbar_data.disp_str.Get(plugin).c_str();
if (!lable_text.IsEmpty())
lable_text += L' ';
width_info.item_width.label_width = m_pDC->GetTextExtent(lable_text).cx;
width_info.item_width.value_width = m_pDC->GetTextExtent(plugin->GetItemValueSampleText()).cx;
value_width = m_pDC->GetTextExtent(plugin->GetItemValueSampleText()).cx;
}
}
}

auto item_order{ theApp.m_taskbar_data.item_order.GetAllDisplayItemsWithOrder() };
for (const auto& item : item_order)
{
if (theApp.IsTaksbarItemDisplayed(item))
{
ItemWidthInfo width_info = item;
width_info.item_width = item_widths[item];
m_item_widths.push_back(width_info);
}
}
Expand Down
7 changes: 7 additions & 0 deletions TrafficMonitor/TaskBarDlg.h
Expand Up @@ -67,6 +67,13 @@ class CTaskBarDlg : public CDialogEx
struct ItemWidthInfo : public CommonDisplayItem
{
ItemWidth item_width;

ItemWidthInfo()
{}

ItemWidthInfo(const CommonDisplayItem& item)
: CommonDisplayItem(item)
{}
};

std::vector<ItemWidthInfo> m_item_widths; //任务栏窗口每个部分的宽度
Expand Down

0 comments on commit b6d9b42

Please sign in to comment.