Skip to content

Commit

Permalink
Now keeps selection when modified.
Browse files Browse the repository at this point in the history
  • Loading branch information
sigma-axis committed Sep 26, 2024
1 parent 6442fd6 commit a8290b9
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions reactive_dlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -562,15 +562,17 @@ struct TrackLabel : SettingDlg {
if (clamp) val = std::clamp(val, info->value_min(), info->value_max());

// get the caret position, relative to the end of the text.
auto caret = len - get_edit_selection(info->hwnd_label).first;
auto [car_l, car_r] = get_edit_selection(info->hwnd_label);
car_l = len - car_l; car_r = len - car_r;
if (auto prec = info->precision(); prec > 1) {
int dec_places = static_cast<int>(0.5 + std::log10(prec));

// values have fraction part.
// preserve the caret position relative to the decimal point.
if (auto pt = std::wcschr(text, L'.'); pt != nullptr)
caret -= (text + len) - pt;
caret += 1 + dec_places;
if (auto pt = std::wcschr(text, L'.'); pt != nullptr) {
car_l -= (text + len) - pt; car_r -= (text + len) - pt;
}
car_l += 1 + dec_places; car_r += 1 + dec_places;

len = std::swprintf(text, std::size(text), L"%.*f", dec_places, val);
}
Expand All @@ -584,7 +586,8 @@ struct TrackLabel : SettingDlg {
::SetWindowTextW(info->hwnd_label, text);

// restore the caret position preserving the relative position to the decimal point.
set_edit_caret(info->hwnd_label, std::clamp(len - caret, 0, len));
car_l = std::clamp(len - car_l, 0, len); car_r = std::clamp(len - car_r, 0, len);
set_edit_selection(info->hwnd_label, car_l, car_r);

return true;
}
Expand Down Expand Up @@ -2330,7 +2333,7 @@ BOOL WINAPI DllMain(HINSTANCE hinst, DWORD fdwReason, LPVOID lpvReserved)
// 看板.
////////////////////////////////
#define PLUGIN_NAME "Reactive Dialog"
#define PLUGIN_VERSION "v1.80-beta1"
#define PLUGIN_VERSION "v1.80-beta2"
#define PLUGIN_AUTHOR "sigma-axis"
#define PLUGIN_INFO_FMT(name, ver, author) (name##" "##ver##" by "##author)
#define PLUGIN_INFO PLUGIN_INFO_FMT(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR)
Expand Down

0 comments on commit a8290b9

Please # to comment.