<commit_before>#include "Hotkeys.h"

#include <CommCtrl.h>

#include "../../3RVX/HotkeyInfo.h"
#include "../../3RVX/HotkeyManager.h"
#include "../../3RVX/LanguageTranslator.h"
#include "../../3RVX/Logger.h"
#include "../../3RVX/Settings.h"
#include "../../3RVX/SkinInfo.h"

#include "HotkeyPrompt.h"
#include "KeyGrabber.h"
#include "../resource.h"

void Hotkeys::Initialize() {
    using std::placeholders::_1;

    INIT_CONTROL(LST_KEYS, ListView, _keyList);
    _keyList.OnItemChange = std::bind(&Hotkeys::OnKeyListItemChange, this, _1);
    INIT_CONTROL(BTN_ADD, Button, _add);
    _add.OnClick = std::bind(&Hotkeys::OnAddButtonClick, this);
    INIT_CONTROL(BTN_REMOVE, Button, _remove);
    _remove.OnClick = std::bind(&Hotkeys::OnRemoveButtonClick, this);

    INIT_CONTROL(GRP_EDITOR, GroupBox, _editorGroup);
    INIT_CONTROL(LBL_KEYS, Label, _keysLabel);
    INIT_CONTROL(BTN_KEYS, Button, _keys);
    _keys.OnClick = std::bind(&Hotkeys::OnKeysButtonClick, this);

    INIT_CONTROL(LBL_ACTION, Label, _actionLabel);
    INIT_CONTROL(CMB_ACTION, ComboBox, _action);
    _action.OnSelectionChange = std::bind(&Hotkeys::OnActionChange, this);

    INIT_CONTROL(LBL_ARG, Label, _argLabel);
    INIT_CONTROL(CHK_ARG, Checkbox, _argCheck);
    _argCheck.OnClick = std::bind(&Hotkeys::OnArgCheckChange, this);
    INIT_CONTROL(CMB_ARG, ComboBox, _argCombo);
    _argCombo.OnSelectionChange = std::bind(&Hotkeys::OnArgComboChange, this);
    INIT_CONTROL(ED_ARG, EditBox, _argEdit);
    _argEdit.OnTextChange = std::bind(&Hotkeys::OnArgEditTextChange, this);
    INIT_CONTROL(BTN_ARG, Button, _argButton);
    _argButton.OnClick = std::bind(&Hotkeys::OnArgButtonClick, this);
}

void Hotkeys::LoadSettings() {
    Settings *settings = Settings::Instance();
    _translator = settings->Translator();

    /* Translations */
    _hotkeysColumnStr = _translator->Translate(_hotkeysColumnStr);
    _actionColumnStr = _translator->Translate(_actionColumnStr);
    _amountVolArgStr = _translator->Translate(_amountVolArgStr);
    _unitsVolArgStr = _translator->Translate(_unitsVolArgStr);
    _percentVolArgStr = _translator->Translate(_percentVolArgStr);
    _keyArgStr = _translator->Translate(_keyArgStr);
    _driveArgStr = _translator->Translate(_driveArgStr);
    _pathArgStr = _translator->Translate(_pathArgStr);

    /* Make highlighted items span the entire row in the list view */
    _keyList.AddListExStyle(LVS_EX_FULLROWSELECT);

    RECT dims = _keyList.ScreenDimensions();
    int width = dims.right - dims.left;

    _keyList.AddColumn(_hotkeysColumnStr, (int) (width * .485));
    _keyList.AddColumn(_actionColumnStr, (int) (width * .445));

    for (std::wstring action : HotkeyInfo::ActionNames) {
        _action.AddItem(_translator->Translate(action));
    }

    std::unordered_map<int, HotkeyInfo> hotkeys = settings->Hotkeys();
    for (auto it = hotkeys.begin(); it != hotkeys.end(); ++it) {
        _keyInfo.push_back(it->second);
    }

    for (unsigned int i = 0; i < _keyInfo.size(); ++i) {
        HotkeyInfo hki = _keyInfo[i];
        std::wstring hkStr = HotkeyManager::HotkeysToString(hki.keyCombination);
        int idx = _keyList.AddItem(hkStr);
        LoadAction(idx, hki);
    }

    _keyList.Selection(0);
    LoadSelection();
}

void Hotkeys::SaveSettings() {
    if (_hWnd == NULL) {
        return;
    }

    /* If the edit box was the last thing the user touched, then we need to
     * manually sync the changes here. */
    UpdateEditArgument();

    CLOG(L"Saving: Hotkeys");
    Settings *settings = Settings::Instance();
    settings->Hotkeys(_keyInfo);
}

HotkeyInfo *Hotkeys::CurrentHotkeyInfo() {
    int selectionIdx = _keyList.Selection();
    if (selectionIdx == -1) {
        return NULL;
    }

    return &_keyInfo[selectionIdx];
}

void Hotkeys::LoadSelection() {
    int idx = _keyList.Selection();
    if (idx < 0) {
        _keys.Disable();
        _action.Disable();
        return;
    }
    LoadSelection(idx);
}

void Hotkeys::LoadSelection(int index) {
    HotkeyInfo selection = _keyInfo[index];
    _keys.Enable();
    _action.Enable();

    _keys.Text(L"");
    if (selection.keyCombination > 0) {
        std::wstring keyStr
            = HotkeyManager::HotkeysToString(selection.keyCombination);
        _keys.Text(keyStr);
        _keyList.ItemText(index, 0, keyStr);
    }

    /* Select the action in the combo box: */
    _action.Select(-1);
    int action = selection.action;

    /* Set argument controls to their default states: */
    DefaultArgControlStates();

    if (action >= 0) {
        _action.Select(action);<commit_msg>Hide arg controls and update states when all items are removed<commit_after>#include "Hotkeys.h"

#include <CommCtrl.h>

#include "../../3RVX/HotkeyInfo.h"
#include "../../3RVX/HotkeyManager.h"
#include "../../3RVX/LanguageTranslator.h"
#include "../../3RVX/Logger.h"
#include "../../3RVX/Settings.h"
#include "../../3RVX/SkinInfo.h"

#include "HotkeyPrompt.h"
#include "KeyGrabber.h"
#include "../resource.h"

void Hotkeys::Initialize() {
    using std::placeholders::_1;

    INIT_CONTROL(LST_KEYS, ListView, _keyList);
    _keyList.OnItemChange = std::bind(&Hotkeys::OnKeyListItemChange, this, _1);
    INIT_CONTROL(BTN_ADD, Button, _add);
    _add.OnClick = std::bind(&Hotkeys::OnAddButtonClick, this);
    INIT_CONTROL(BTN_REMOVE, Button, _remove);
    _remove.OnClick = std::bind(&Hotkeys::OnRemoveButtonClick, this);

    INIT_CONTROL(GRP_EDITOR, GroupBox, _editorGroup);
    INIT_CONTROL(LBL_KEYS, Label, _keysLabel);
    INIT_CONTROL(BTN_KEYS, Button, _keys);
    _keys.OnClick = std::bind(&Hotkeys::OnKeysButtonClick, this);

    INIT_CONTROL(LBL_ACTION, Label, _actionLabel);
    INIT_CONTROL(CMB_ACTION, ComboBox, _action);
    _action.OnSelectionChange = std::bind(&Hotkeys::OnActionChange, this);

    INIT_CONTROL(LBL_ARG, Label, _argLabel);
    INIT_CONTROL(CHK_ARG, Checkbox, _argCheck);
    _argCheck.OnClick = std::bind(&Hotkeys::OnArgCheckChange, this);
    INIT_CONTROL(CMB_ARG, ComboBox, _argCombo);
    _argCombo.OnSelectionChange = std::bind(&Hotkeys::OnArgComboChange, this);
    INIT_CONTROL(ED_ARG, EditBox, _argEdit);
    _argEdit.OnTextChange = std::bind(&Hotkeys::OnArgEditTextChange, this);
    INIT_CONTROL(BTN_ARG, Button, _argButton);
    _argButton.OnClick = std::bind(&Hotkeys::OnArgButtonClick, this);
}

void Hotkeys::LoadSettings() {
    Settings *settings = Settings::Instance();
    _translator = settings->Translator();

    /* Translations */
    _hotkeysColumnStr = _translator->Translate(_hotkeysColumnStr);
    _actionColumnStr = _translator->Translate(_actionColumnStr);
    _amountVolArgStr = _translator->Translate(_amountVolArgStr);
    _unitsVolArgStr = _translator->Translate(_unitsVolArgStr);
    _percentVolArgStr = _translator->Translate(_percentVolArgStr);
    _keyArgStr = _translator->Translate(_keyArgStr);
    _driveArgStr = _translator->Translate(_driveArgStr);
    _pathArgStr = _translator->Translate(_pathArgStr);

    /* Make highlighted items span the entire row in the list view */
    _keyList.AddListExStyle(LVS_EX_FULLROWSELECT);

    RECT dims = _keyList.ScreenDimensions();
    int width = dims.right - dims.left;

    _keyList.AddColumn(_hotkeysColumnStr, (int) (width * .485));
    _keyList.AddColumn(_actionColumnStr, (int) (width * .445));

    for (std::wstring action : HotkeyInfo::ActionNames) {
        _action.AddItem(_translator->Translate(action));
    }

    std::unordered_map<int, HotkeyInfo> hotkeys = settings->Hotkeys();
    for (auto it = hotkeys.begin(); it != hotkeys.end(); ++it) {
        _keyInfo.push_back(it->second);
    }

    for (unsigned int i = 0; i < _keyInfo.size(); ++i) {
        HotkeyInfo hki = _keyInfo[i];
        std::wstring hkStr = HotkeyManager::HotkeysToString(hki.keyCombination);
        int idx = _keyList.AddItem(hkStr);
        LoadAction(idx, hki);
    }

    _keyList.Selection(0);
    LoadSelection();
}

void Hotkeys::SaveSettings() {
    if (_hWnd == NULL) {
        return;
    }

    /* If the edit box was the last thing the user touched, then we need to
     * manually sync the changes here. */
    UpdateEditArgument();

    CLOG(L"Saving: Hotkeys");
    Settings *settings = Settings::Instance();
    settings->Hotkeys(_keyInfo);
}

HotkeyInfo *Hotkeys::CurrentHotkeyInfo() {
    int selectionIdx = _keyList.Selection();
    if (selectionIdx == -1) {
        return NULL;
    }

    return &_keyInfo[selectionIdx];
}

void Hotkeys::LoadSelection() {
    int idx = _keyList.Selection();
    if (idx < 0) {
        _keys.Text(L"");
        _keys.Disable();
        _action.Select(-1);
        _action.Disable();
        DefaultArgControlStates();
        return;
    }
    LoadSelection(idx);
}

void Hotkeys::LoadSelection(int index) {
    HotkeyInfo selection = _keyInfo[index];
    _keys.Enable();
    _action.Enable();

    _keys.Text(L"");
    if (selection.keyCombination > 0) {
        std::wstring keyStr
            = HotkeyManager::HotkeysToString(selection.keyCombination);
        _keys.Text(keyStr);
        _keyList.ItemText(index, 0, keyStr);
    }

    /* Select the action in the combo box: */
    _action.Select(-1);
    int action = selection.action;

    /* Set argument controls to their default states: */
    DefaultArgControlStates();

    if (action >= 0) {
        _action.Select(action);
{'fraction_non_alphanumeric': 0.13899283652304073, 'fraction_numerical': 0.004811290495028333, 'mean_word_length': 3.809254498714653, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7427978', 'n_tokens_mistral': 3133, 'n_tokens_neox': 3064, 'n_words': 574}