Skip to content

Commit f066290

Browse files
Merge pull request #637 from NQNStudios:fix-default-key
I have certain UI expectations, like for the Yes/No dialogs to all respond to both the Y or N key. If there's a default button that responds to Enter, I want it to do that without clobbering its existing hotkey. Fix #600
2 parents e61abae + 5a9c2cd commit f066290

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

src/dialogxml/dialogs/dialog.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,6 @@ cDialog::cDialog(const DialogDefn& file, cDialog* p) : parent(p) {
160160

161161
extern fs::path progDir;
162162
void cDialog::loadFromFile(const DialogDefn& file){
163-
static const cKey enterKey = {true, key_enter};
164163
bg = defaultBackground;
165164
fname = file.id;
166165
try{
@@ -328,7 +327,7 @@ void cDialog::loadFromFile(const DialogDefn& file){
328327

329328
// Set the default button.
330329
if(hasControl(defaultButton))
331-
getControl(defaultButton).attachKey(enterKey);
330+
getControl(defaultButton).setDefault(true);
332331

333332
// Sort by tab order
334333
// First, fill any gaps that might have been left, using ones that had no specific tab order
@@ -886,8 +885,11 @@ bool cDialog::addLabelFor(std::string key, std::string label, eLabelPos where, s
886885

887886
void cDialog::process_keystroke(cKey keyHit){
888887
ctrlIter iter = controls.begin();
888+
bool enterKeyHit = keyHit.spec && keyHit.k == key_enter;
889889
while(iter != controls.end()){
890-
if(iter->second->isVisible() && iter->second->isClickable() && iter->second->getAttachedKey() == keyHit){
890+
if((iter->second->isVisible() && iter->second->isClickable())
891+
&& (iter->second->getAttachedKey() == keyHit || (iter->second->isDefault() && enterKeyHit))){
892+
891893
iter->second->setActive(true);
892894
draw();
893895
iter->second->playClickSound();

src/dialogxml/widgets/control.hpp

+5
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ class cControl {
165165
/// Retrieve the control's current keyboard shortcut as a human-readable string.
166166
/// @return the currently-assigned keyboard shortcut, or an empty string if none is assigned.
167167
std::string getAttachedKeyDescription() const;
168+
inline void setDefault(bool value) { isDefaultControl = value; }
169+
inline bool isDefault() { return isDefaultControl; }
168170
/// Attach an event handler to this control.
169171
/// @tparam t The type of event to attach.
170172
/// @param handler The event handler function or functor. Its signature depends on the event type.
@@ -443,6 +445,9 @@ class cControl {
443445
eFrameStyle frameStyle;
444446
/// The control's attached key.
445447
cKey key;
448+
/// Whether the control is the default control of its dialog.
449+
bool isDefaultControl = false;
450+
446451
/// Draw a frame around the control.
447452
/// @param amt How much to offset the frame from the control's bounding rect.
448453
/// @param med_or_lt true to use a darker colour for the frame.

0 commit comments

Comments
 (0)