Skip to content

Commit

Permalink
Implement reset on double click on the zoomer value
Browse files Browse the repository at this point in the history
  • Loading branch information
SinghRajenM committed Nov 11, 2024
1 parent d74c5bd commit 13ac714
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 38 deletions.
50 changes: 15 additions & 35 deletions src/NppJsonViewer/JsonViewDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,14 @@

constexpr int FILENAME_LEN_IN_TITLE = 16;

namespace SliderPercent
{
constexpr const int nDefault = 100;
constexpr const int nMinZoom = 80;
constexpr const int nMaxZoom = 250;
}; // namespace SliderPercent

JsonViewDlg::JsonViewDlg(HINSTANCE hInstance, const NppData& nppData, const bool& isReady, int nCmdId, std::shared_ptr<Setting>& pSetting)
: DockingDlgInterface(IDD_TREEDLG)
, m_NppData(nppData)
, m_IsNppReady(isReady)
, m_nDlgId(nCmdId)
, m_pEditor(std::make_unique<ScintillaEditor>(nppData))
, m_pTreeView(std::make_unique<TreeViewCtrl>())
, m_pTreeViewZoom(std::make_unique<SliderCtrl>())
, m_pSetting(pSetting)
, m_pCurrFileName(std::make_unique<wchar_t[]>(FILENAME_LEN_IN_TITLE))
{
Expand Down Expand Up @@ -902,25 +896,14 @@ void JsonViewDlg::EnableControls(const std::vector<DWORD>& ids, bool enable)
EnableWindow(GetDlgItem(getHSelf(), id), enable ? TRUE : FALSE);
}

auto JsonViewDlg::GetSliderPosition() const -> int
auto JsonViewDlg::GetZoomLevel() const -> int
{
HWND hSlider = GetDlgItem(getHSelf(), IDC_ZOOM_SLIDER);
int pos = static_cast<int>(SendMessage(hSlider, TBM_GETPOS, 0, 0));

return pos;
return m_pTreeViewZoom->GetPosition();
}

void JsonViewDlg::SetSliderPosition(int pos) const
void JsonViewDlg::SetZoomLevel(int pos) const
{
// Set slider position
HWND hSlider = GetDlgItem(getHSelf(), IDC_ZOOM_SLIDER);
SendMessage(hSlider, TBM_SETPOS, TRUE, pos);

// Set slider position in text value
HWND hZoomPercent = GetDlgItem(getHSelf(), IDC_ZOOM_PERCENT);
wchar_t zoomText[16] {};
wsprintf(zoomText, L"%d%%", pos);
SetWindowText(hZoomPercent, zoomText);
m_pTreeViewZoom->SetPosition(pos);
}

void JsonViewDlg::SetTreeViewZoom(double dwZoomFactor) const
Expand All @@ -945,8 +928,8 @@ void JsonViewDlg::SetTreeViewZoom(double dwZoomFactor) const

void JsonViewDlg::UpdateUIOnZoom(int zoomPercentage) const
{
// Update slider
SetSliderPosition(zoomPercentage);
// Update zoom level on slider
SetZoomLevel(zoomPercentage);

// Update the Tree view
double zoomFactor = zoomPercentage / 100.0;
Expand All @@ -955,15 +938,17 @@ void JsonViewDlg::UpdateUIOnZoom(int zoomPercentage) const

void JsonViewDlg::HandleZoomOnScroll(WPARAM wParam) const
{
int pos = GetSliderPosition(); // Current slider position
int pos = GetZoomLevel(); // Current zoom level
int delta = GET_WHEEL_DELTA_WPARAM(wParam);

// Adjust zoom based on scroll direction
if (delta > 0 && pos < SliderPercent::nMaxZoom)
const auto& zoomRange = m_pTreeViewZoom->GetRange();
const bool isZoom = delta > 0;

if (isZoom && pos < zoomRange.m_nMaxZoom)
{
pos += 10; // Zoom in
}
else if (delta < 0 && pos > SliderPercent::nMinZoom)
else if (!isZoom && pos > zoomRange.m_nMinZoom)
{
pos -= 10; // Zoom out
}
Expand Down Expand Up @@ -1106,18 +1091,13 @@ INT_PTR JsonViewDlg::run_dlgProc(UINT message, WPARAM wParam, LPARAM lParam)
::SetWindowLongPtr(getHSelf(), GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this));

m_pTreeView->OnInit(getHSelf(), IDC_TREE);
m_pTreeViewZoom->OnInit(getHSelf(), IDC_ZOOM_SLIDER, IDC_ZOOM_PERCENT);

PrepareButtons();

// Set default node path as JSON
SetDlgItemText(_hSelf, IDC_EDT_NODEPATH, JSON_ROOT);

// Set slider range from 80% to 200%
// Set initial position to 100% (no zoom)
HWND hSlider = GetDlgItem(getHSelf(), IDC_ZOOM_SLIDER);
SendMessage(hSlider, TBM_SETRANGE, TRUE, MAKELPARAM(SliderPercent::nMinZoom, SliderPercent::nMaxZoom));
SendMessage(hSlider, TBM_SETPOS, TRUE, SliderPercent::nDefault);

return TRUE;
}

Expand Down Expand Up @@ -1205,7 +1185,7 @@ INT_PTR JsonViewDlg::run_dlgProc(UINT message, WPARAM wParam, LPARAM lParam)

if (reinterpret_cast<HWND>(lParam) == hSlider)
{
int pos = GetSliderPosition();
int pos = m_pTreeViewZoom->GetPosition();
UpdateUIOnZoom(pos);

return TRUE;
Expand Down
6 changes: 4 additions & 2 deletions src/NppJsonViewer/JsonViewDlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "PluginInterface.h"
#include "resource.h"
#include "TreeViewCtrl.h"
#include "SliderCtrl.h"
#include "ScintillaEditor.h"
#include "JsonHandler.h"
#include "JsonNode.h"
Expand Down Expand Up @@ -89,8 +90,8 @@ class JsonViewDlg
void ShowControls(const std::vector<DWORD>& ids, bool show);
void EnableControls(const std::vector<DWORD>& ids, bool enable);

auto GetSliderPosition() const -> int;
void SetSliderPosition(int pos) const;
auto GetZoomLevel() const -> int;
void SetZoomLevel(int pos) const;
void SetTreeViewZoom(double dwZoomFactor) const;
void UpdateUIOnZoom(int zoomPercentage) const;
void HandleZoomOnScroll(WPARAM wParam) const;
Expand Down Expand Up @@ -124,5 +125,6 @@ class JsonViewDlg
std::unique_ptr<wchar_t[]> m_pCurrFileName;
std::unique_ptr<ScintillaEditor> m_pEditor = nullptr;
std::unique_ptr<TreeViewCtrl> m_pTreeView = nullptr;
std::unique_ptr<SliderCtrl> m_pTreeViewZoom = nullptr;
std::shared_ptr<Setting> m_pSetting = nullptr;
};
2 changes: 2 additions & 0 deletions src/NppJsonViewer/NPPJSONViewer.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@
<ClCompile Include="ScintillaEditor.cpp" />
<ClCompile Include="SettingsDlg.cpp" />
<ClCompile Include="ShortcutCommand.cpp" />
<ClCompile Include="SliderCtrl.cpp" />
<ClCompile Include="TreeViewCtrl.cpp" />
</ItemGroup>
<ItemGroup>
Expand All @@ -221,6 +222,7 @@
<ClInclude Include="ScintillaEditor.h" />
<ClInclude Include="SettingsDlg.h" />
<ClInclude Include="ShortcutCommand.h" />
<ClInclude Include="SliderCtrl.h" />
<ClInclude Include="StopWatch.h" />
<ClCompile Include="TreeHandler.h" />
<ClInclude Include="TrackingStream.h" />
Expand Down
6 changes: 6 additions & 0 deletions src/NppJsonViewer/NPPJSONViewer.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
<ClCompile Include="TreeHandler.h">
<Filter>Header Files</Filter>
</ClCompile>
<ClCompile Include="SliderCtrl.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="resource.h">
Expand Down Expand Up @@ -134,6 +137,9 @@
<ClInclude Include="TrackingStream.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="SliderCtrl.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="resource.rc">
Expand Down
75 changes: 75 additions & 0 deletions src/NppJsonViewer/SliderCtrl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include "SliderCtrl.h"

#include <string>

SliderCtrl::SliderCtrl(const SliderRange& sliderRange)
: m_sliderRange(sliderRange)
{
}

SliderCtrl::~SliderCtrl()
{
// Restore the original window procedure on cleanup
SetWindowLongPtr(m_hSelf, GWLP_WNDPROC, (LONG_PTR)m_oldSliderProc);
}

void SliderCtrl::OnInit(HWND hParent, int sliderID, int sliderInfoID)
{
m_hParent = hParent;
m_hSelf = GetDlgItem(m_hParent, sliderID);
m_hSelfInfo = GetDlgItem(m_hParent, sliderInfoID);

// Set slider range and initial position
SendMessage(m_hSelf, TBM_SETRANGE, TRUE, MAKELPARAM(m_sliderRange.m_nMinZoom, m_sliderRange.m_nMaxZoom));
SendMessage(m_hSelf, TBM_SETPOS, TRUE, m_sliderRange.m_nDefault);

UpdateInfo(m_sliderRange.m_nDefault);

// Subclass the slider control to handle double-click events
m_oldSliderProc = reinterpret_cast<WNDPROC>(SetWindowLongPtr(m_hSelfInfo, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(runWinProc)));

// Save this instance for access in the static window procedure
SetWindowLongPtr(m_hSelfInfo, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this));
}

auto SliderCtrl::GetPosition() const -> int
{
int pos = static_cast<int>(SendMessage(m_hSelf, TBM_GETPOS, 0, 0));
return pos;
}

void SliderCtrl::SetPosition(int pos) const
{
// Set slider position
SendMessage(m_hSelf, TBM_SETPOS, TRUE, pos);

// Set slider position in text value
UpdateInfo(pos);
}

void SliderCtrl::UpdateInfo(int zoomPercentage) const
{
std::wstring sliderInfoText = std::to_wstring(zoomPercentage) + L"%";
SetWindowText(m_hSelfInfo, sliderInfoText.c_str());
}

LRESULT SliderCtrl::runWinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
SliderCtrl* pThis = reinterpret_cast<SliderCtrl*>(GetWindowLongPtr(hWnd, GWLP_USERDATA));

if (pThis)
{
if (message == WM_LBUTTONDBLCLK)
{
// Reset slider to 100% on double-click
// Also notify its parent to adjust tree view as well
pThis->SetPosition(100);
SendMessage(pThis->m_hParent, WM_HSCROLL, NULL, reinterpret_cast<LPARAM>(pThis->m_hSelf));
}

// Call the original window procedure for other messages
return CallWindowProc(pThis->m_oldSliderProc, hWnd, message, wParam, lParam);
}

return FALSE;
}
52 changes: 52 additions & 0 deletions src/NppJsonViewer/SliderCtrl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#pragma once


#include <Windows.h>
#include <CommCtrl.h>

struct SliderRange
{
int m_nDefault = 100;
int m_nMinZoom = 80;
int m_nMaxZoom = 250;
}; // namespace SliderRange

class SliderCtrl
{
HWND m_hParent = nullptr;
HWND m_hSelf = nullptr;
HWND m_hSelfInfo = nullptr;
HWND m_hTreeView = nullptr;
WNDPROC m_oldSliderProc = nullptr;
const SliderRange m_sliderRange {};

public:
explicit SliderCtrl(const SliderRange& sliderRange = {});

~SliderCtrl();

void OnInit(HWND hParent, int sliderID, int sliderInfoID);

HWND GetSliderHandle() const
{
return m_hSelf;
}

HWND GetSliderInfoHandle() const
{
return m_hSelfInfo;
}

auto GetRange() const -> const SliderRange&
{
return m_sliderRange;
}

auto GetPosition() const -> int;
void SetPosition(int pos) const;
void UpdateInfo(int zoomPercentage) const;

private:
// Static window procedure for the slider
static INT_PTR CALLBACK runWinProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
};
2 changes: 1 addition & 1 deletion src/NppJsonViewer/resource.rc
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ BEGIN
EDITTEXT IDC_EDT_SEARCH,60,2,99,12,ES_AUTOHSCROLL
PUSHBUTTON "",IDC_BTN_SEARCH,160,2,16,12,BS_ICON
CONTROL "",IDC_ZOOM_SLIDER,"msctls_trackbar32",TBS_AUTOTICKS | TBS_NOTICKS,2,16,58,12
RTEXT "100%",IDC_ZOOM_PERCENT,60,16,22,12,NOT WS_GROUP,WS_EX_RIGHT
RTEXT "100%",IDC_ZOOM_PERCENT,60,16,22,12,SS_NOTIFY | NOT WS_GROUP,WS_EX_RIGHT
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
EDITTEXT IDC_EDT_NODEPATH,2,296,173,12,ES_AUTOHSCROLL | ES_READONLY
END
Expand Down

0 comments on commit 13ac714

Please sign in to comment.