Browse Source

Fix suggestions

pull/149/head
Andrea Ciliberti 12 months ago
parent
commit
5948589bdb
  1. 3
      ctru-rs/examples/software-keyboard.rs
  2. 20
      ctru-rs/src/applets/swkbd.rs

3
ctru-rs/examples/software-keyboard.rs

@ -5,7 +5,6 @@
use ctru::applets::swkbd::{Button, CallbackResult, SoftwareKeyboard}; use ctru::applets::swkbd::{Button, CallbackResult, SoftwareKeyboard};
use ctru::prelude::*; use ctru::prelude::*;
use std::borrow::Cow;
use std::ffi::CString; use std::ffi::CString;
fn main() { fn main() {
@ -27,7 +26,7 @@ fn main() {
if str.to_str().unwrap().contains("boo") { if str.to_str().unwrap().contains("boo") {
return ( return (
CallbackResult::Retry, CallbackResult::Retry,
Some(Cow::Owned(CString::new("Ah, you scared me!").unwrap())), Some(CString::new("Ah, you scared me!").unwrap()),
); );
} }

20
ctru-rs/src/applets/swkbd.rs

@ -9,20 +9,19 @@ use ctru_sys::{self, SwkbdState};
use bitflags::bitflags; use bitflags::bitflags;
use libc; use libc;
use std::borrow::Cow; use std::ffi::{CStr, CString};
use std::ffi::CStr;
use std::fmt::Display; use std::fmt::Display;
use std::iter::once; use std::iter::once;
use std::str; use std::str;
type CallbackFunction = dyn Fn(&CStr) -> (CallbackResult, Option<Cow<CStr>>); type CallbackFunction = dyn Fn(&CStr) -> (CallbackResult, Option<CString>);
/// Configuration structure to setup the Software Keyboard applet. /// Configuration structure to setup the Software Keyboard applet.
#[doc(alias = "SwkbdState")] #[doc(alias = "SwkbdState")]
pub struct SoftwareKeyboard<'a> { pub struct SoftwareKeyboard {
state: Box<SwkbdState>, state: Box<SwkbdState>,
callback: Option<Box<CallbackFunction>>, callback: Option<Box<CallbackFunction>>,
error_message: Option<Cow<'a, CStr>>, error_message: Option<CString>,
} }
/// Configuration structure to setup the Parental Lock applet. /// Configuration structure to setup the Parental Lock applet.
@ -207,7 +206,7 @@ bitflags! {
} }
} }
impl SoftwareKeyboard<'_> { impl SoftwareKeyboard {
/// Initialize a new configuration for the Software Keyboard applet depending on how many "exit" buttons are available to the user (1, 2 or 3). /// Initialize a new configuration for the Software Keyboard applet depending on how many "exit" buttons are available to the user (1, 2 or 3).
/// ///
/// # Example /// # Example
@ -409,7 +408,7 @@ impl SoftwareKeyboard<'_> {
/// if str.to_str().unwrap().contains("boo") { /// if str.to_str().unwrap().contains("boo") {
/// return ( /// return (
/// CallbackResult::Retry, /// CallbackResult::Retry,
/// Some(Cow::Owned(CString::new("Ah, you scared me!").unwrap())), /// Some(CString::new("Ah, you scared me!").unwrap()),
/// ); /// );
/// } /// }
/// ///
@ -441,6 +440,8 @@ impl SoftwareKeyboard<'_> {
if let Some(callback) = &mut (*this).callback { if let Some(callback) = &mut (*this).callback {
let (res, cstr) = callback(text); let (res, cstr) = callback(text);
// Due to how `libctru` operates, the user is expected to keep the error message alive until
// the end of the Software Keyboard prompt. We ensure that happens by saving it within the configuration.
(*this).error_message = cstr; (*this).error_message = cstr;
if let Some(newstr) = &(*this).error_message { if let Some(newstr) = &(*this).error_message {
@ -712,8 +713,7 @@ impl ParentalLock {
/// # /// #
/// # } /// # }
#[doc(alias = "swkbdInputText")] #[doc(alias = "swkbdInputText")]
#[allow(unused_variables)] pub fn launch(&mut self, _apt: &Apt, _gfx: &Gfx) -> Result<(), Error> {
pub fn launch(&mut self, apt: &Apt, gfx: &Gfx) -> Result<(), Error> {
unsafe { unsafe {
let mut buf = [0; 0]; let mut buf = [0; 0];
ctru_sys::swkbdInputText(self.state.as_mut(), buf.as_mut_ptr(), 0); ctru_sys::swkbdInputText(self.state.as_mut(), buf.as_mut_ptr(), 0);
@ -728,7 +728,7 @@ impl ParentalLock {
} }
/// Creates a new [`SoftwareKeyboard`] configuration set to using a [`Kind::Normal`] keyboard and 2 [`Button`]s. /// Creates a new [`SoftwareKeyboard`] configuration set to using a [`Kind::Normal`] keyboard and 2 [`Button`]s.
impl Default for SoftwareKeyboard<'_> { impl Default for SoftwareKeyboard {
fn default() -> Self { fn default() -> Self {
SoftwareKeyboard::new(Kind::Normal, ButtonConfig::LeftRight) SoftwareKeyboard::new(Kind::Normal, ButtonConfig::LeftRight)
} }

Loading…
Cancel
Save