-
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_getch() to awaitable
- Loading branch information
1 parent
a8f90d3
commit 37b17be
Showing
6 changed files
with
151 additions
and
78 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,62 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
// Copyright The Music Player Daemon Project | ||
|
||
#include "KeyDialog.hxx" | ||
#include "ui/Keys.hxx" | ||
#include "ui/Options.hxx" | ||
#include "ui/Window.hxx" | ||
#include "Styles.hxx" | ||
|
||
using std::string_view_literals::operator""sv; | ||
|
||
void | ||
KeyDialog::OnLeave(const Window window) noexcept | ||
{ | ||
curs_set(0); | ||
|
||
if (ui_options.enable_colors) | ||
window.SetBackgroundStyle(Style::STATUS); | ||
} | ||
|
||
void | ||
KeyDialog::OnCancel() noexcept | ||
{ | ||
SetResult(-1); | ||
} | ||
|
||
void | ||
KeyDialog::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); | ||
|
||
SelectStyle(window, Style::INPUT); | ||
window.ClearToEol(); | ||
|
||
curs_set(1); | ||
} | ||
|
||
static constexpr bool | ||
IsCancelKey(int key) noexcept | ||
{ | ||
return key == KEY_CANCEL || key == KEY_SCANCEL || | ||
key == KEY_CLOSE || | ||
key == KEY_CTL('C') || | ||
key == KEY_CTL('G') || | ||
key == KEY_ESCAPE; | ||
} | ||
|
||
bool | ||
KeyDialog::OnKey(Window, int key) | ||
{ | ||
if (IsCancelKey(key)) | ||
Cancel(); | ||
else | ||
SetResult(key); | ||
|
||
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,77 @@ | ||
// 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> | ||
|
||
/** | ||
* A #ModalDialog that asks the user to press a key. | ||
* | ||
* This dialog is supposed to be awaited from a coroutine using | ||
* co_await. It suspends the caller while waiting for user input. | ||
*/ | ||
class KeyDialog final : public ModalDialog { | ||
const std::string_view prompt; | ||
|
||
std::coroutine_handle<> continuation; | ||
|
||
int result = -2; | ||
|
||
using Awaitable = Co::AwaitableHelper<KeyDialog, 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 | ||
*/ | ||
KeyDialog(ModalDock &_dock, std::string_view _prompt) noexcept | ||
:ModalDialog(_dock), prompt(_prompt) | ||
{ | ||
Show(); | ||
} | ||
|
||
~KeyDialog() noexcept { | ||
Hide(); | ||
} | ||
|
||
/** | ||
* Await completion of this dialog. | ||
* | ||
* @return an ncurses key code or -1 if the dialog was | ||
* canceled | ||
*/ | ||
Awaitable operator co_await() noexcept { | ||
return *this; | ||
} | ||
|
||
private: | ||
void SetResult(int _result) noexcept { | ||
result = _result; | ||
|
||
if (continuation) | ||
continuation.resume(); | ||
} | ||
|
||
bool IsReady() const noexcept { | ||
return result != -2; | ||
} | ||
|
||
int 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