Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/core/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ void Application::LoadConfiguration() {
static_cast<uint8_t>(ReadIniInt(L"General", L"PrevDesktopKey", VK_OEM_3)));
VirtualDesktopSwitcher::SetPinAllDesktopsKey(
static_cast<uint8_t>(ReadIniInt(L"General", L"PinAllDesktopsKey", 'D')));
for (int i = 0; i < static_cast<int>(kMaxDesktops); ++i) {
for (int i = 0; i < kMaxDesktops; ++i) {
std::wstring keyName = L"DesktopKey" + std::to_wstring(i + 1);
VirtualDesktopSwitcher::SetDesktopKey(i,
static_cast<uint8_t>(ReadIniInt(L"General", keyName, '1' + i)));
Expand Down Expand Up @@ -410,6 +410,8 @@ void Application::SyncDesktopState() {
if (m_pOverlay) { m_pOverlay->ShowTemporarily(); }
}

m_pTrayIcon->UpdateTrayIcon(currentDesktop + 1);

if (m_pOverlay) {
auto emptyMask = m_switcher->GetDesktopEmptyMask();
m_pOverlay->SetDesktopState(desktopCount, currentDesktop, emptyMask);
Expand Down
6 changes: 6 additions & 0 deletions src/core/IndicatorConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ enum class DragSwitchMode : std::uint8_t {
Count = 4
};

enum class TrayIconMode : std::uint8_t {
Icon = 0,
Number = 1,
Count = 2
};

struct WindowRatio {
float x = 0.5f;
float y = 0.0f;
Expand Down
2 changes: 1 addition & 1 deletion src/core/VirtualDesktopHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ std::array<bool, kMaxDesktops> VirtualDesktopHelper::GetDesktopEmptyMask() const
}
}

for (UINT i = 0; i < desktopCount && i < kMaxDesktops; ++i) {
for (UINT i = 0; i < desktopCount && i < static_cast<UINT>(kMaxDesktops); ++i) {
emptyMask.at(i) = (windowCounts.at(i) == 0);
}
return emptyMask;
Expand Down
2 changes: 1 addition & 1 deletion src/core/VirtualDesktopSwitcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ LRESULT CALLBACK VirtualDesktopSwitcher::LowLevelKeyboardProc(int nCode, WPARAM
bool match = held != 0 && held == s_modMask;

if (match) {
for (int i = 0; i < static_cast<int>(kMaxDesktops); ++i) {
for (int i = 0; i < kMaxDesktops; ++i) {
if (pKeyboard->vkCode == s_desktopKeys.at(i)) {
PostMessage(s_active->m_hwnd, WM_SWITCH_DESKTOP, i, 0);
return 1;
Expand Down
2 changes: 1 addition & 1 deletion src/core/VirtualDesktopSwitcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class VirtualDesktopSwitcher {
static void SetPrevDesktopKey(uint8_t vk) { s_prevDesktopKey = vk; }
static void SetPinAllDesktopsKey(uint8_t vk) { s_pinAllDesktopsKey = vk; }
static void SetDesktopKey(int index, uint8_t vk) {
if (index >= 0 && index < static_cast<int>(kMaxDesktops)) { s_desktopKeys.at(index) = vk; }
if (index >= 0 && index < kMaxDesktops) { s_desktopKeys.at(index) = vk; }
}

[[nodiscard]] int GetDesktopCount() const;
Expand Down
2 changes: 1 addition & 1 deletion src/ui/DesktopIndicator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ void DesktopIndicator::RebuildText() {
return;
}
std::wstring text;
for (int i = 0; i < m_desktopCount && i < static_cast<int>(kMaxDesktops); ++i) {
for (int i = 0; i < m_desktopCount && i < kMaxDesktops; ++i) {
text += (i == m_currentDesktop) ? m_pCfg->currentSymbol
: m_emptyDesktops.at(i) ? m_pCfg->emptySymbol
: m_pCfg->otherSymbol;
Expand Down
344 changes: 276 additions & 68 deletions src/ui/TrayIcon.cpp

Large diffs are not rendered by default.

49 changes: 35 additions & 14 deletions src/ui/TrayIcon.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,24 @@

#include <shellapi.h>

#include <array>
#include <functional>
#include <memory>
#include <string>

#include "core/IndicatorConfig.h"
#include "util/Utils.h"

constexpr UINT WM_TRAYICON = WM_USER + 2;
constexpr UINT WM_TRAY_LANG_CHINESE = WM_USER + 50;
constexpr UINT WM_TRAY_LANG_ENGLISH = WM_USER + 51;
constexpr UINT CMD_COLOR_OPTIONS_BASE = WM_USER + 100;
constexpr UINT WM_TRAYICON = WM_USER + 2;
constexpr UINT CMD_TRAY_LANG_CHINESE = WM_USER + 50;
constexpr UINT CMD_TRAY_LANG_ENGLISH = WM_USER + 51;
constexpr UINT CMD_TRAY_ICON_MODE_ICON = WM_USER + 60;
constexpr UINT CMD_TRAY_ICON_MODE_NUMBER = WM_USER + 61;
constexpr UINT CMD_COLOR_OPTIONS_BASE = WM_USER + 100;
constexpr UINT CMD_TRAY_NUMBER_COLOR_BASE = WM_USER + 500;

constexpr int kTrayDefaultIconResource = 101;
class GdiplusGuard;

class TrayIcon {
public:
Expand All @@ -21,13 +30,16 @@ class TrayIcon {
TrayIcon(TrayIcon &&) = delete;
TrayIcon &operator=(TrayIcon &&) = delete;

TrayIcon() = default;
TrayIcon();
~TrayIcon();

bool Initialize(HWND hwnd, HINSTANCE hInstance);
bool Reinitialize();
void HandleMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
void UpdateTooltip(const std::wstring &tooltip);
void UpdateTrayIcon(int displayNumber);
void SetTrayIconMode(TrayIconMode mode);
void SetNumberColor(const std::wstring &hexColor);

void SetActivePositionPreset(PositionPreset preset) { m_activePositionPreset = preset; }
void SetEditModeCallback(std::function<void()> cb) { m_editModeFn = std::move(cb); }
Expand All @@ -42,12 +54,17 @@ class TrayIcon {
void SetDragSwitchModeCallback(std::function<void(int)> cb) { m_dragModeFn = std::move(cb); }

private:
NOTIFYICONDATAW m_nid{};
HMENU m_hMenu = nullptr;
bool m_autoStartEnabled = false;
PositionPreset m_activePositionPreset = PositionPreset::TopCenter;
int m_menuAveWidth = 6;
int m_dpi = 96;
NOTIFYICONDATAW m_nid{};
std::unique_ptr<GdiplusGuard> m_gdiplus;
HINSTANCE m_hInstance = nullptr;
HMENU m_hMenu = nullptr;
bool m_autoStartEnabled = false;
PositionPreset m_activePositionPreset = PositionPreset::TopCenter;
int m_dpi = 96;
int m_nTrayNumber = -1;
TrayIconMode m_iconMode = TrayIconMode::Icon;
COLORREF m_numberColor = RGB(41, 151, 255);
std::array<HICON, kMaxDesktops + 1> m_hNumberIcons{};

std::function<void(const std::wstring &)> m_colorFn;
std::function<void()> m_editModeFn;
Expand All @@ -60,9 +77,12 @@ class TrayIcon {
std::function<void(bool)> m_autoFocusFn;
std::function<void(int)> m_dragModeFn;

void BuildMenu();
void HandleCommand(WPARAM wParam);
void DrawColorSwatch(LPDRAWITEMSTRUCT dis) const;
void BuildMenu();
void HandleCommand(WPARAM wParam);
void DrawColorSwatch(LPDRAWITEMSTRUCT dis, UINT base) const;
HICON GetNumberIcon(int nNumber);
HICON GetTrayIconForNumber(int nDisplay);
static HICON CreateNumberIcon(int nNumber, COLORREF color, GdiplusGuard &gdiplus);

static void HandleRunAsAdmin();
static void HandleReset();
Expand All @@ -74,6 +94,7 @@ class TrayIcon {
void HandleToggleShow();
void HandleToggleAutoStart();
void HandleShowModeCommand(int mode);
void HandleNumberColorCommand(int colorIndex);
void HandlePositionCommand(PositionPreset preset);
void HandleColorCommand(int index);
};
4 changes: 4 additions & 0 deletions src/util/LangData_en.inc
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ std::pair{L"Menu.DragAlt", L"Hold Alt to Switch"},
std::pair{L"Menu.DragCtrl", L"Hold Ctrl to Switch"},
std::pair{L"Menu.DragNever", L"Never Switch"},
std::pair{L"Menu.AutoFocus", L"Cross-Monitor Focus"},
std::pair{L"Menu.TrayIcon", L"Tray Icon"},
std::pair{L"Menu.TrayIconIcon", L"Icon"},
std::pair{L"Menu.TrayIconNumber", L"Desktop Number"},
std::pair{L"Menu.TrayNumberColor", L"Number Color"},
std::pair{L"Tray.DefaultTip", L"Virtual Desktop Switcher"},
std::pair{L"Tooltip.Desktops", L"Desktops: "},
std::pair{L"Tooltip.Current", L"Current: "},
Expand Down
4 changes: 4 additions & 0 deletions src/util/LangData_zh.inc
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ std::pair{L"Menu.DragAlt", L"按住 Alt 键切换"},
std::pair{L"Menu.DragCtrl", L"按住 Ctrl 键切换"},
std::pair{L"Menu.DragNever", L"不切换"},
std::pair{L"Menu.AutoFocus", L"跨屏自动聚焦"},
std::pair{L"Menu.TrayIcon", L"托盘图标"},
std::pair{L"Menu.TrayIconIcon", L"图标"},
std::pair{L"Menu.TrayIconNumber", L"桌面编号"},
std::pair{L"Menu.TrayNumberColor", L"编号颜色"},
std::pair{L"Tray.DefaultTip", L"虚拟桌面切换器"},
std::pair{L"Tooltip.Desktops", L"桌面:"},
std::pair{L"Tooltip.Current", L"当前:"},
Expand Down
15 changes: 10 additions & 5 deletions src/util/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,23 @@

// --- Constants ---

constexpr size_t kMaxDesktops = 9;
constexpr int kMaxDesktops = 9;

constexpr std::array kPredefinedColors = {
L"#FFFFFF",
L"#E3E3E5",
L"#000000",
L"#FF5F5F",
L"#F6ABB7",
L"#FB7FA4",
L"#F59E0B",
L"#FF3B30",
L"#FF6B6B",
L"#FF9500",
L"#FFCC00",
L"#22C55E",
L"#4ECDC4",
L"#2997FF",
L"#5856D6",
L"#8771C6",
L"#FF2D55",
L"#FB7FA4",
L"#FF9A9E_#FECFEF",
L"#FEA5B7_#FFDE9B",
L"#A8EDEA_#FED6E3",
Expand Down
2 changes: 1 addition & 1 deletion xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ target("VirtualDesktopSwitcher")
add_defines("NOMINMAX")
add_cxflags("/GR-")
add_ldflags("/SUBSYSTEM:WINDOWS")
add_syslinks("shell32", "user32", "gdi32", "advapi32", "comctl32", "ole32", "dwmapi", "shcore")
add_syslinks("shell32", "user32", "gdi32", "advapi32", "comctl32", "ole32", "dwmapi", "shcore", "gdiplus")

add_packages("stb")