Skip to content

Commit

Permalink
Merge pull request #149 from rust3ds/fix/applets
Browse files Browse the repository at this point in the history
Applets fixup and feature implementation
  • Loading branch information
Meziu authored Jan 5, 2024
2 parents b1021c5 + 5948589 commit 947c3f4
Show file tree
Hide file tree
Showing 5 changed files with 386 additions and 69 deletions.
2 changes: 1 addition & 1 deletion ctru-rs/examples/file-explorer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ impl<'a> FileExplorer<'a> {
fn get_input_and_run(&mut self, action: impl FnOnce(&mut Self, String)) {
let mut keyboard = SoftwareKeyboard::default();

match keyboard.get_string(2048) {
match keyboard.get_string(2048, self.apt, self.gfx) {
Ok((path, Button::Right)) => {
// Clicked "OK".
action(self, path);
Expand Down
2 changes: 1 addition & 1 deletion ctru-rs/examples/mii-selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn main() {
mii_selector.set_title("Great Mii Selector!");

// Launch the Mii Selector and use its result to print the selected Mii's information.
match mii_selector.launch() {
match mii_selector.launch(&apt, &gfx) {
Ok(result) => {
println!("Mii type: {:?}", result.mii_type);
println!("Name: {:?}", result.mii_data.name);
Expand Down
31 changes: 24 additions & 7 deletions ctru-rs/examples/software-keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,37 @@
//!
//! This example showcases the use of the Software Keyboard applet to receive text input from the user.
use ctru::applets::swkbd::{Button, SoftwareKeyboard};
use ctru::applets::swkbd::{Button, CallbackResult, SoftwareKeyboard};
use ctru::prelude::*;

use std::ffi::CString;

fn main() {
let apt = Apt::new().unwrap();
let mut hid = Hid::new().unwrap();
let gfx = Gfx::new().unwrap();
let _console = Console::new(gfx.top_screen.borrow_mut());

// Prepares a software keyboard with two buttons: one to cancel input and one
// to accept it. You can also use `SoftwareKeyboard::new()` to launch the keyboard
// with different configurations.
let mut keyboard = SoftwareKeyboard::default();

// Custom filter callback to handle the given input.
// Using this callback it's possible to integrate the applet
// with custom error messages when the input is incorrect.
keyboard.set_filter_callback(Some(Box::new(|str| {
// The string is guaranteed to contain valid Unicode text, so we can safely unwrap and use it as a normal `&str`.
if str.to_str().unwrap().contains("boo") {
return (
CallbackResult::Retry,
Some(CString::new("Ah, you scared me!").unwrap()),
);
}

(CallbackResult::Ok, None)
})));

println!("Press A to enter some text or press Start to exit.");

while apt.main_loop() {
Expand All @@ -22,14 +44,9 @@ fn main() {

// Check if the user request to write some input.
if hid.keys_down().contains(KeyPad::A) {
// Prepares a software keyboard with two buttons: One to cancel input and one
// to accept it. You can also use `SoftwareKeyboard::new()` to launch the keyboard in different
// configurations.
let mut keyboard = SoftwareKeyboard::default();

// Raise the software keyboard. You can perform different actions depending on which
// software button the user pressed.
match keyboard.get_string(2048) {
match keyboard.get_string(2048, &apt, &gfx) {
Ok((text, Button::Right)) => println!("You entered: {text}"),
Ok((_, Button::Left)) => println!("Cancelled"),
Ok((_, Button::Middle)) => println!("How did you even press this?"),
Expand Down
15 changes: 10 additions & 5 deletions ctru-rs/src/applets/mii_selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
//! The selected Mii is readable as a [`Mii`].
use crate::mii::Mii;
use crate::services::{apt::Apt, gfx::Gfx};

use bitflags::bitflags;
use std::{ffi::CString, fmt};

Expand Down Expand Up @@ -252,15 +254,18 @@ impl MiiSelector {

/// Launch the Mii Selector.
///
/// Depending on the configuration, the Mii Selector window will appear either on the bottom screen (default behaviour) or the top screen (see [`Options::USE_TOP_SCREEN`]).
///
/// TODO: UNSAFE OPERATION, LAUNCHING APPLETS REQUIRES GRAPHICS, WITHOUT AN ACTIVE GFX THIS WILL CAUSE A SEGMENTATION FAULT.
/// Depending on the configuration, the Mii Selector window will appear either
/// on the bottom screen (default behaviour) or the top screen (see [`Options::USE_TOP_SCREEN`]).
///
/// # Example
///
/// ```no_run
/// # use std::error::Error;
/// # fn main() -> Result<(), Box<dyn Error>> {
/// # use ctru::services::{apt::Apt, gfx::Gfx};
/// #
/// # let gfx = Gfx::new().unwrap();
/// # let apt = Apt::new().unwrap();
/// #
/// use ctru::applets::mii_selector::{MiiSelector, Options};
///
Expand All @@ -270,13 +275,13 @@ impl MiiSelector {
/// let opts = Options::ENABLE_CANCEL & Options::ENABLE_GUESTS;
/// mii_selector.set_options(opts);
///
/// let result = mii_selector.launch()?;
/// let result = mii_selector.launch(&apt, &gfx)?;
/// #
/// # Ok(())
/// # }
/// ```
#[doc(alias = "miiSelectorLaunch")]
pub fn launch(&mut self) -> Result<Selection, Error> {
pub fn launch(&mut self, _apt: &Apt, _gfx: &Gfx) -> Result<Selection, Error> {
let mut return_val = Box::<ctru_sys::MiiSelectorReturn>::default();
unsafe { ctru_sys::miiSelectorLaunch(self.config.as_mut(), return_val.as_mut()) }

Expand Down
Loading

0 comments on commit 947c3f4

Please sign in to comment.