-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
screen_utils: convert screen_get_yesno() to an awaitable
- Loading branch information
1 parent
dc0ed2f
commit 199b1cb
Showing
7 changed files
with
183 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
// Copyright The Music Player Daemon Project | ||
|
||
#include "YesNoDialog.hxx" | ||
#include "ui/Bell.hxx" | ||
#include "ui/Keys.hxx" | ||
#include "ui/Options.hxx" | ||
#include "ui/Window.hxx" | ||
#include "Styles.hxx" | ||
#include "i18n.h" | ||
|
||
#include <ctype.h> | ||
|
||
using std::string_view_literals::operator""sv; | ||
|
||
void | ||
YesNoDialog::OnLeave(const Window window) noexcept | ||
{ | ||
noecho(); | ||
curs_set(0); | ||
|
||
if (ui_options.enable_colors) | ||
window.SetBackgroundStyle(Style::STATUS); | ||
} | ||
|
||
void | ||
YesNoDialog::OnCancel() noexcept | ||
{ | ||
SetResult(YesNoResult::CANCEL); | ||
} | ||
|
||
void | ||
YesNoDialog::Paint(const Window window) const noexcept | ||
{ | ||
if (ui_options.enable_colors) | ||
window.SetBackgroundStyle(Style::INPUT); | ||
|
||
SelectStyle(window, Style::STATUS_ALERT); | ||
window.String({0, 0}, prompt); | ||
window.String(" ["sv); | ||
window.String(YES_TRANSLATION); | ||
window.Char('/'); | ||
window.String(NO_TRANSLATION); | ||
window.String("] "sv); | ||
|
||
SelectStyle(window, Style::INPUT); | ||
window.ClearToEol(); | ||
|
||
echo(); | ||
curs_set(1); | ||
} | ||
|
||
static constexpr bool | ||
IsCancelKey(int key) noexcept | ||
{ | ||
return key == KEY_CANCEL || key == KEY_SCANCEL || | ||
key == KEY_CLOSE || | ||
key == KEY_UNDO || | ||
key == KEY_CTL('C') || | ||
key == KEY_CTL('G') || | ||
key == KEY_ESCAPE; | ||
} | ||
|
||
bool | ||
YesNoDialog::OnKey(Window, int key) | ||
{ | ||
/* NOTE: if one day a translator decides to use a multi-byte character | ||
for one of the yes/no keys, we'll have to parse it properly */ | ||
|
||
key = tolower(key); | ||
if (key == YES_TRANSLATION[0]) { | ||
Hide(); | ||
SetResult(YesNoResult::YES); | ||
} else if (key == NO_TRANSLATION[0]) { | ||
Hide(); | ||
SetResult(YesNoResult::NO); | ||
} else if (IsCancelKey(key)) { | ||
Cancel(); | ||
} else { | ||
Bell(); | ||
} | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
// Copyright The Music Player Daemon Project | ||
|
||
#pragma once | ||
|
||
#include "ModalDialog.hxx" | ||
#include "co/AwaitableHelper.hxx" | ||
|
||
#include <cstdint> | ||
#include <string_view> | ||
|
||
enum class YesNoResult : int8_t { | ||
CANCEL = -1, | ||
NO = false, | ||
YES = true, | ||
}; | ||
|
||
/** | ||
* A #ModalDialog that asks the user a yes/no question. | ||
* | ||
* This dialog is supposed to be awaited from a coroutine using | ||
* co_await. It suspends the caller while waiting for user input. | ||
*/ | ||
class YesNoDialog final : public ModalDialog { | ||
const std::string_view prompt; | ||
|
||
std::coroutine_handle<> continuation; | ||
|
||
bool ready = false; | ||
YesNoResult result; | ||
|
||
using Awaitable = Co::AwaitableHelper<YesNoDialog, false>; | ||
friend Awaitable; | ||
|
||
public: | ||
/** | ||
* @param _prompt the human-readable prompt to be displayed | ||
* (including question mark if desired); the pointed-by memory | ||
* is owned by the caller and must remain valid during the | ||
* lifetime of this dialog | ||
*/ | ||
YesNoDialog(ModalDock &_dock, std::string_view _prompt) noexcept | ||
:ModalDialog(_dock), prompt(_prompt) | ||
{ | ||
Show(); | ||
} | ||
|
||
~YesNoDialog() noexcept { | ||
Hide(); | ||
} | ||
|
||
/** | ||
* Await completion of this dialog. | ||
* | ||
* @return a YesNoResult | ||
*/ | ||
Awaitable operator co_await() noexcept { | ||
return *this; | ||
} | ||
|
||
private: | ||
void SetResult(YesNoResult _result) noexcept { | ||
result = _result; | ||
ready = true; | ||
|
||
if (continuation) | ||
continuation.resume(); | ||
} | ||
|
||
bool IsReady() const noexcept { | ||
return ready; | ||
} | ||
|
||
YesNoResult TakeValue() noexcept { | ||
return result; | ||
} | ||
|
||
public: | ||
/* virtual methodds from Modal */ | ||
void OnLeave(Window window) noexcept override; | ||
void OnCancel() noexcept override; | ||
void Paint(Window window) const noexcept override; | ||
bool OnKey(Window window, int key) override; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters