Skip to content

Commit 0222285

Browse files
committed
Add zoom feature using slider
1 parent fddc8be commit 0222285

File tree

3 files changed

+75
-10
lines changed

3 files changed

+75
-10
lines changed

src/NppJsonViewer/JsonViewDlg.cpp

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212

1313
constexpr int FILENAME_LEN_IN_TITLE = 16;
1414

15+
namespace SliderPercent
16+
{
17+
constexpr const int nDefault = 100;
18+
constexpr const int nMinZoom = 80;
19+
constexpr const int nMaxZoom = 250;
20+
}; // namespace SliderPercent
21+
1522
JsonViewDlg::JsonViewDlg(HINSTANCE hInstance, const NppData& nppData, const bool& isReady, int nCmdId, std::shared_ptr<Setting>& pSetting)
1623
: DockingDlgInterface(IDD_TREEDLG)
1724
, m_NppData(nppData)
@@ -895,6 +902,27 @@ void JsonViewDlg::EnableControls(const std::vector<DWORD>& ids, bool enable)
895902
EnableWindow(GetDlgItem(getHSelf(), id), enable ? TRUE : FALSE);
896903
}
897904

905+
auto JsonViewDlg::GetSliderPosition() const -> int
906+
{
907+
HWND hSlider = GetDlgItem(getHSelf(), IDC_ZOOM_SLIDER);
908+
int pos = static_cast<int>(SendMessage(hSlider, TBM_GETPOS, 0, 0));
909+
910+
return pos;
911+
}
912+
913+
void JsonViewDlg::SetSliderPosition(int pos) const
914+
{
915+
// Set slider position
916+
HWND hSlider = GetDlgItem(getHSelf(), IDC_ZOOM_SLIDER);
917+
SendMessage(hSlider, TBM_SETPOS, TRUE, pos);
918+
919+
// Set slider position in text value
920+
HWND hZoomPercent = GetDlgItem(getHSelf(), IDC_ZOOM_PERCENT);
921+
wchar_t zoomText[16] {};
922+
wsprintf(zoomText, L"%d%%", pos);
923+
SetWindowText(hZoomPercent, zoomText);
924+
}
925+
898926
void JsonViewDlg::SetTreeViewZoom(double dwZoomFactor)
899927
{
900928
HWND hTreeView = GetDlgItem(getHSelf(), IDC_TREE);
@@ -915,20 +943,32 @@ void JsonViewDlg::SetTreeViewZoom(double dwZoomFactor)
915943
InvalidateRect(hTreeView, nullptr, TRUE);
916944
}
917945

918-
void JsonViewDlg::HandleZoom(bool zoomIn)
946+
void JsonViewDlg::UpdateUIOnZoom(int zoomPercentage)
947+
{
948+
// Update slider
949+
SetSliderPosition(zoomPercentage);
950+
951+
// Update the Tree view
952+
double zoomFactor = zoomPercentage / 100.0;
953+
SetTreeViewZoom(zoomFactor);
954+
}
955+
956+
void JsonViewDlg::HandleZoomOnScroll(WPARAM wParam)
919957
{
920-
static double zoomLevel = 1.0; // Start at 100% zoom (Max 300% and min 80%)
958+
int pos = GetSliderPosition(); // Current slider position
959+
int delta = GET_WHEEL_DELTA_WPARAM(wParam);
921960

922-
if (zoomIn && zoomLevel < 3.0)
961+
// Adjust zoom based on scroll direction
962+
if (delta > 0 && pos < SliderPercent::nMaxZoom)
923963
{
924-
zoomLevel += 0.1; // Zoom in (max 300%)
964+
pos += 10; // Zoom in
925965
}
926-
else if (!zoomIn && zoomLevel > 0.8)
966+
else if (delta < 0 && pos > SliderPercent::nMinZoom)
927967
{
928-
zoomLevel -= 0.1; // Zoom out (min 80%)
968+
pos -= 10; // Zoom out
929969
}
930970

931-
SetTreeViewZoom(zoomLevel);
971+
UpdateUIOnZoom(pos);
932972
}
933973

934974
void JsonViewDlg::HandleTreeEvents(LPARAM lParam) const
@@ -1072,6 +1112,12 @@ INT_PTR JsonViewDlg::run_dlgProc(UINT message, WPARAM wParam, LPARAM lParam)
10721112
// Set default node path as JSON
10731113
SetDlgItemText(_hSelf, IDC_EDT_NODEPATH, JSON_ROOT);
10741114

1115+
// Set slider range from 80% to 200%
1116+
// Set initial position to 100% (no zoom)
1117+
HWND hSlider = GetDlgItem(getHSelf(), IDC_ZOOM_SLIDER);
1118+
SendMessage(hSlider, TBM_SETRANGE, TRUE, MAKELPARAM(SliderPercent::nMinZoom, SliderPercent::nMaxZoom));
1119+
SendMessage(hSlider, TBM_SETPOS, TRUE, SliderPercent::nDefault);
1120+
10751121
return TRUE;
10761122
}
10771123

@@ -1147,12 +1193,27 @@ INT_PTR JsonViewDlg::run_dlgProc(UINT message, WPARAM wParam, LPARAM lParam)
11471193
{
11481194
if (GetKeyState(VK_CONTROL) & 0x8000)
11491195
{
1150-
HandleZoom(GET_WHEEL_DELTA_WPARAM(wParam) > 0);
1196+
HandleZoomOnScroll(wParam);
1197+
return TRUE;
1198+
}
1199+
return FALSE;
1200+
}
1201+
1202+
case WM_HSCROLL:
1203+
{
1204+
HWND hSlider = GetDlgItem(getHSelf(), IDC_ZOOM_SLIDER);
1205+
1206+
if (reinterpret_cast<HWND>(lParam) == hSlider)
1207+
{
1208+
int pos = GetSliderPosition();
1209+
UpdateUIOnZoom(pos);
1210+
11511211
return TRUE;
11521212
}
11531213
return FALSE;
11541214
}
11551215

1216+
11561217
default:
11571218
return DockingDlgInterface::run_dlgProc(message, wParam, lParam);
11581219
}

src/NppJsonViewer/resource.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
#define IDC_CHK_IGNORE_COMMENT 1031
4343
#define IDC_CHK_JSON_HIGHLIGHT 1032
4444
#define IDC_CHK_REPLACE_UNDEFINED 1033
45+
#define IDC_ZOOM_SLIDER 1034
46+
#define IDC_ZOOM_PERCENT 1035
4547
#define IDM_COPY_TREEITEM 40001
4648
#define IDM_COPY_NODENAME 40002
4749
#define IDM_COPY_NODEVALUE 40003
@@ -55,7 +57,7 @@
5557
#ifndef APSTUDIO_READONLY_SYMBOLS
5658
#define _APS_NEXT_RESOURCE_VALUE 110
5759
#define _APS_NEXT_COMMAND_VALUE 40007
58-
#define _APS_NEXT_CONTROL_VALUE 1034
60+
#define _APS_NEXT_CONTROL_VALUE 1036
5961
#define _APS_NEXT_SYMED_VALUE 101
6062
#endif
6163
#endif

src/NppJsonViewer/resource.rc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ BEGIN
8888
CONTROL "",IDC_DIVIDER,"Static",SS_ETCHEDVERT,56,1,2,16
8989
EDITTEXT IDC_EDT_SEARCH,60,2,99,12,ES_AUTOHSCROLL
9090
PUSHBUTTON "",IDC_BTN_SEARCH,160,2,16,12,BS_ICON
91-
CONTROL "",IDC_TREE,"SysTreeView32",TVS_HASBUTTONS | TVS_HASLINES | TVS_DISABLEDRAGDROP | TVS_SHOWSELALWAYS | TVS_FULLROWSELECT | WS_HSCROLL | WS_TABSTOP,2,18,173,274,WS_EX_CLIENTEDGE
91+
CONTROL "",IDC_ZOOM_SLIDER,"msctls_trackbar32",TBS_AUTOTICKS | TBS_NOTICKS,2,16,58,12
92+
RTEXT "100%",IDC_ZOOM_PERCENT,60,16,22,12,NOT WS_GROUP,WS_EX_RIGHT
93+
CONTROL "",IDC_TREE,"SysTreeView32",TVS_HASBUTTONS | TVS_HASLINES | TVS_DISABLEDRAGDROP | TVS_SHOWSELALWAYS | TVS_FULLROWSELECT | WS_HSCROLL | WS_TABSTOP,2,30,173,264,WS_EX_CLIENTEDGE
9294
EDITTEXT IDC_EDT_NODEPATH,2,296,173,12,ES_AUTOHSCROLL | ES_READONLY
9395
END
9496

0 commit comments

Comments
 (0)