use alloc::collections::BTreeMap;
use alloc::rc::Rc;
use alloc::string::{String, ToString};
use alloc::vec::Vec;
use core::cell::RefCell;
use crate::desktop::{FileAnswer, FileOptions, InputOptions};
use crate::runtime::{Object, Runtime, RuntimeError, RuntimeFailure, TypeName, Value, put};
pub fn input_box(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let prompt = text_of(values, 0);
let title = text_of(values, 1);
let options = text_of(values, 2);
let default = text_of(values, 3);
let options = the_options_of_the_input_box(&options, line)?;
let answer = rt
.desktop()
.input_box(&prompt, &title, &options, &default)
.map_err(|reason| RuntimeError {
message: reason,
line,
})?;
let mut properties = BTreeMap::new();
put(&mut properties, "value", Value::Text(answer.value));
put(&mut properties, "result", Value::Text(answer.result));
Ok(Value::Object(Rc::new(RefCell::new(Object {
class: "object".to_string(),
properties,
}))))
}
pub fn file_select(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let options = text_of(values, 0);
let root = text_of(values, 1);
let title = text_of(values, 2);
let filter = text_of(values, 3);
let options = the_options_of_the_file_select(&options, line)?;
if options.directory && !filter.is_empty() {
return Err(RuntimeError {
message: crate::runtime::invalid_parameter(4, "FileSelect"),
line,
});
}
let answer = rt
.desktop()
.file_select(&options, &root, &title, &filter)
.map_err(|reason| RuntimeError {
message: reason,
line,
})?;
Ok(match answer {
FileAnswer::One(path) => Value::Text(path),
FileAnswer::Many(paths) => {
let paths: Vec<Value> = paths.into_iter().map(Value::Text).collect();
match crate::classes::array::construct(&paths, rt, line) {
Ok(array) => array,
Err(RuntimeFailure::Error(error)) => return Err(error),
Err(RuntimeFailure::Detailed(error, _)) => return Err(error),
Err(_) => {
return Err(RuntimeError {
message: "the array of the files cannot be made.".to_string(),
line,
});
}
}
}
})
}
pub fn dir_select(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let starting = text_of(values, 0);
let prompt = text_of(values, 2);
let options = values.get(1).cloned().unwrap_or(Value::Int(1));
let options = options.to_number_strict().map_err(|_| RuntimeError {
message: crate::runtime::requires(2, "DirSelect", TypeName::Number, &options),
line,
})? as i64;
let folder = rt
.desktop()
.dir_select(&starting, options, &prompt)
.map_err(|reason| RuntimeError {
message: reason,
line,
})?;
Ok(Value::Text(folder))
}
pub(crate) fn the_options_of_the_input_box(
options: &str,
line: u32,
) -> Result<InputOptions, RuntimeError> {
match the_options_of_the_input_box_read(options) {
Ok(read) => Ok(read),
Err(_) => Err(invalid_option(line)),
}
}
fn the_options_of_the_input_box_read(options: &str) -> Result<InputOptions, String> {
let mut read = InputOptions::default();
for token in options.split([' ', '\t']).filter(|token| !token.is_empty()) {
let mut letters = token.chars();
let letter = letters.next().unwrap_or_default().to_ascii_lowercase();
let rest = letters.as_str();
match letter {
'x' => read.x = the_place_of_an_option(rest, token)?,
'y' => read.y = the_place_of_an_option(rest, token)?,
'w' => read.width = the_size_of_an_option(rest, token)?,
'h' => read.height = the_size_of_an_option(rest, token)?,
't' => read.timeout = the_timeout_of_an_option(rest, token)?,
'p' => {
if !rest.to_ascii_lowercase().starts_with("assword") {
return Err(token.to_string());
}
read.password = true;
}
_ => return Err(token.to_string()),
}
}
Ok(read)
}
pub(crate) fn the_option_that_failed(options: &str) -> Option<String> {
the_options_of_the_input_box_read(options).err()
}
fn the_place_of_an_option(rest: &str, token: &str) -> Result<Option<i64>, String> {
match the_number_of_an_option(rest) {
Ok(Some(number)) => Ok(Some(number as i64)),
Ok(None) => Ok(None),
Err(()) => Err(token.to_string()),
}
}
fn the_size_of_an_option(rest: &str, token: &str) -> Result<Option<i64>, String> {
match the_number_of_an_option(rest) {
Ok(Some(number)) if number >= 0.0 => Ok(Some(number as i64)),
Ok(Some(_)) => Err(token.to_string()),
Ok(None) => Ok(None),
Err(()) => Err(token.to_string()),
}
}
fn the_timeout_of_an_option(rest: &str, token: &str) -> Result<Option<f64>, String> {
match the_number_of_an_option(rest) {
Ok(Some(number)) => Ok(Some(number)),
Ok(None) => Ok(None),
Err(()) => Err(token.to_string()),
}
}
fn the_number_of_an_option(rest: &str) -> Result<Option<f64>, ()> {
if rest.is_empty() {
return Ok(None);
}
rest.parse::<f64>().map(Some).map_err(|_| ())
}
fn the_options_of_the_file_select(options: &str, line: u32) -> Result<FileOptions, RuntimeError> {
let mut read = FileOptions::default();
let trimmed = options.trim();
if trimmed.is_empty() {
return Ok(read);
}
let mut letters = trimmed.chars();
let first = letters.next().unwrap_or_default();
let rest = letters.as_str();
if first.is_ascii_digit() || first == '-' {
read.flags = trimmed
.parse::<i64>()
.map_err(|_| invalid_file_option(line))?;
return Ok(read);
}
let flags = if rest.is_empty() {
0
} else {
rest.parse::<i64>().map_err(|_| invalid_file_option(line))?
};
match first.to_ascii_lowercase() {
'd' => read.directory = true,
'm' => read.multiple = true,
's' => read.save = true,
_ => return Err(invalid_file_option(line)),
}
read.flags = flags;
Ok(read)
}
fn invalid_option(line: u32) -> RuntimeError {
RuntimeError {
message: "Invalid option.".to_string(),
line,
}
}
fn invalid_file_option(line: u32) -> RuntimeError {
RuntimeError {
message: crate::runtime::invalid_parameter(1, "FileSelect"),
line,
}
}
fn text_of(values: &[Value], index: usize) -> String {
values
.get(index)
.map(|value| value.to_text())
.unwrap_or_default()
}
pub fn tray_tip(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let text = text_of(values, 0);
let title = text_of(values, 1);
rt.desktop()
.notify(&title, &text)
.map_err(|reason| RuntimeError {
message: reason,
line,
})?;
Ok(Value::Text(String::new()))
}