use alloc::format;
use alloc::string::{String, ToString};
use alloc::vec::Vec;
use crate::builtins::{self, BuiltinHandler};
use crate::runtime::{Runtime, RuntimeError, Value};
use super::write_the_answer;
fn the_answer_that_goes_on(
rt: &mut Runtime<'_>,
name: &str,
values: &[Value],
line: u32,
answer: Result<Value, RuntimeError>,
error_level: bool,
) -> Result<Value, RuntimeError> {
match answer {
Ok(value) => {
write_the_answer(rt, name, values, 0, value.clone(), line)?;
if error_level {
rt.set_the_error_level(Value::Int(0));
}
Ok(value)
}
Err(failure) => {
write_the_answer(rt, name, values, 0, Value::Text(String::new()), line)?;
if error_level {
rt.set_the_error_level(Value::Int(1));
}
rt.note(format!(
"the command {name} did not do the thing: {}",
failure.message
));
Ok(Value::Text(String::new()))
}
}
}
pub(crate) fn win_get_text(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let answer = builtins::win_get_text(rt, values.get(1..).unwrap_or(&[]), line, 0);
the_answer_that_goes_on(rt, "WinGetText", values, line, answer, true)
}
pub(crate) fn win_get_class(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let answer = builtins::win_get_class(rt, values.get(1..).unwrap_or(&[]), line, 0);
the_answer_that_goes_on(rt, "WinGetClass", values, line, answer, false)
}
pub(crate) fn win_get_pos(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let answer = builtins::win_get_pos(rt, values, line, 0);
match answer {
Ok(value) => Ok(value),
Err(failure) => {
for index in 0..4 {
if values.get(index).is_some() {
write_the_answer(rt, "WinGetPos", values, index, Value::Text(String::new()), line)?;
}
}
rt.note(format!(
"the command WinGetPos did not do the thing: {}",
failure.message
));
Ok(Value::Text(String::new()))
}
}
}
pub(crate) fn win_get_active_title(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let answer = rt
.desktop()
.active_window_title()
.map(|title| title.unwrap_or_default())
.map(Value::Text)
.map_err(|reason| builtins::desktop_failure(reason, line));
the_answer_that_goes_on(rt, "WinGetActiveTitle", values, line, answer, false)
}
pub(crate) fn win_get_active_stats(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let title = rt
.desktop()
.active_window_title()
.map_err(|reason| builtins::desktop_failure(reason, line))?;
let Some(title) = title else {
return Ok(Value::Text(String::new()));
};
let geometry = rt
.desktop()
.window_geometry(&title, crate::settings::title_match_mode())
.map_err(|reason| builtins::desktop_failure(reason, line))?;
let Some(geometry) = geometry else {
return Ok(Value::Text(String::new()));
};
let places = [
Value::Text(title),
Value::Int(geometry.width),
Value::Int(geometry.height),
Value::Int(geometry.x),
Value::Int(geometry.y),
];
for (index, place) in places.iter().enumerate() {
if values.get(index).is_some() {
write_the_answer(rt, "WinGetActiveStats", values, index, place.clone(), line)?;
}
}
Ok(Value::Text(String::new()))
}
pub(crate) fn win_exist(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let title = values.first().map(|value| value.to_text()).unwrap_or_default();
if title.is_empty() {
return Ok(match rt.the_last_window_of_the_v1() {
Some(id) => Value::Text(the_identifier_of_the_v1(id)),
None => Value::Text("0x0".to_string()),
});
}
let mode = crate::settings::title_match_mode();
let windows = rt
.desktop()
.matching_windows(&title, mode)
.map_err(|reason| builtins::desktop_failure(reason, line))?;
match windows.first() {
Some(window) => {
rt.remember_the_last_window_of_the_v1(&window.id);
Ok(Value::Text(the_identifier_of_the_v1(&window.id)))
}
None => Ok(Value::Text("0x0".to_string())),
}
}
pub(crate) fn win_get(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let sub_command = values
.get(1)
.map(|value| value.to_text())
.unwrap_or_default();
let wanted = crate::desktop::TheWindowWanted::of(values, 2);
if sub_command.eq_ignore_ascii_case("list") {
return the_list_of_the_windows(rt, values, &wanted, line);
}
let answer = the_answer_of_the_sub_command(rt, &sub_command, &wanted, line);
the_answer_that_goes_on(rt, "WinGet", values, line, answer, false)
}
fn the_list_of_the_windows(
rt: &mut Runtime<'_>,
values: &[Value],
wanted: &crate::desktop::TheWindowWanted,
line: u32,
) -> Result<Value, RuntimeError> {
let windows = the_windows_that_answer(rt, wanted, line)?;
let count = windows.len() as i64;
write_the_answer(rt, "WinGet", values, 0, Value::Int(count), line)?;
let Some(name) = the_name_of_the_output(rt, values) else {
return Ok(Value::Int(count));
};
for (index, window) in windows.iter().enumerate() {
let identifier = the_identifier_of_the_v1(&window.id.to_string());
rt.assign_the_variable_of_the_pseudo_array(
&format!("{name}{}", index + 1),
Value::Text(identifier),
);
}
Ok(Value::Int(count))
}
fn the_windows_that_answer(
rt: &mut Runtime<'_>,
wanted: &crate::desktop::TheWindowWanted,
line: u32,
) -> Result<Vec<crate::desktop::WindowInfo>, RuntimeError> {
let windows = rt
.desktop()
.windows()
.map_err(|reason| builtins::desktop_failure(reason, line))?;
Ok(builtins::the_windows_of_the_listing(rt, &windows, wanted))
}
fn the_name_of_the_output(rt: &mut Runtime<'_>, values: &[Value]) -> Option<String> {
let Some(Value::Reference(reference)) = values.first() else {
return None;
};
let name = reference.name.clone();
if name.is_empty() {
None
} else {
let _ = rt;
Some(name)
}
}
fn the_identifier_of_the_v1(id: &str) -> String {
match id.parse::<u64>() {
Ok(number) => format!("0x{number:x}"),
Err(_) => id.to_string(),
}
}
fn the_answer_of_the_sub_command(
rt: &mut Runtime<'_>,
sub_command: &str,
wanted: &crate::desktop::TheWindowWanted,
line: u32,
) -> Result<Value, RuntimeError> {
let mode = crate::settings::title_match_mode();
let title = &wanted.title;
let uses_the_last_window = !title.is_empty();
match sub_command.to_ascii_lowercase().as_str() {
"id" => {
if uses_the_last_window {
let windows = the_windows_that_answer(rt, wanted, line)?;
return match windows.first() {
Some(window) => Ok(Value::Text(the_identifier_of_the_v1(&window.id.to_string()))),
None => Err(RuntimeError {
message: builtins::TARGET_WINDOW.to_string(),
line,
}),
};
}
match rt.the_last_window_of_the_v1() {
Some(id) => Ok(Value::Text(the_identifier_of_the_v1(id))),
None => Err(RuntimeError {
message: builtins::TARGET_WINDOW.to_string(),
line,
}),
}
}
"idlast" => {
if uses_the_last_window {
let windows = the_windows_that_answer(rt, wanted, line)?;
return match windows.last() {
Some(window) => Ok(Value::Text(the_identifier_of_the_v1(&window.id.to_string()))),
None => Err(RuntimeError {
message: builtins::TARGET_WINDOW.to_string(),
line,
}),
};
}
match rt.the_last_window_of_the_v1() {
Some(id) => Ok(Value::Text(the_identifier_of_the_v1(id))),
None => Err(RuntimeError {
message: builtins::TARGET_WINDOW.to_string(),
line,
}),
}
}
"count" => {
let windows = the_windows_that_answer(rt, wanted, line)?;
Ok(Value::Int(windows.len() as i64))
}
"minmax" => {
let state = rt
.desktop()
.window_min_max(title, mode)
.map_err(|reason| builtins::desktop_failure(reason, line))?;
match state {
Some(state) => Ok(Value::Int(state)),
None => Err(RuntimeError {
message: builtins::TARGET_WINDOW.to_string(),
line,
}),
}
}
"pid" => {
let pid = rt
.desktop()
.window_pid(title)
.map_err(|reason| builtins::desktop_failure(reason, line))?;
match pid {
Some(pid) => Ok(Value::Int(pid)),
None => Err(RuntimeError {
message: builtins::TARGET_WINDOW.to_string(),
line,
}),
}
}
"processname" | "processpath" => {
let pid = rt
.desktop()
.window_pid(title)
.map_err(|reason| builtins::desktop_failure(reason, line))?;
let Some(pid) = pid else {
return Err(RuntimeError {
message: builtins::TARGET_WINDOW.to_string(),
line,
});
};
let name = if sub_command.eq_ignore_ascii_case("processname") {
rt.desktop().process_name(Some(&pid.to_string()))
} else {
rt.desktop().process_path(Some(&pid.to_string()))
};
match name
.map_err(|reason| builtins::desktop_failure(reason, line))?
{
Some(name) => Ok(Value::Text(name)),
None => Err(RuntimeError {
message: builtins::TARGET_WINDOW.to_string(),
line,
}),
}
}
"transparent" => rt
.desktop()
.window_trans_color(title)
.map(Value::Text)
.map_err(|reason| builtins::desktop_failure(reason, line)),
"transcolor" => rt
.desktop()
.window_trans_color(title)
.map(Value::Text)
.map_err(|reason| builtins::desktop_failure(reason, line)),
"style" => rt
.desktop()
.window_style(title, false)
.map(Value::Int)
.map_err(|reason| builtins::desktop_failure(reason, line)),
"exstyle" => rt
.desktop()
.window_style(title, true)
.map(Value::Int)
.map_err(|reason| builtins::desktop_failure(reason, line)),
"controllist" | "controllisthwnd" => {
let joined = rt
.desktop()
.gui_controls()
.map_err(|reason| builtins::desktop_failure(reason, line))?;
Ok(Value::Text(joined))
}
_ => Err(RuntimeError {
message: format!("the sub-command {sub_command} of the WinGet is not known."),
line,
}),
}
}
pub(crate) fn win_wait(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let answer = builtins::win_wait(rt, &the_values_of_a_wait(values), line, 0);
the_wait_that_goes_on(rt, answer)
}
pub(crate) fn win_wait_active(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let answer = builtins::win_wait_active(rt, &the_values_of_a_wait(values), line, 0);
the_wait_that_goes_on(rt, answer)
}
pub(crate) fn win_wait_close(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let answer = builtins::win_wait_close(rt, &the_values_of_a_wait(values), line, 0);
the_wait_that_goes_on(rt, answer)
}
fn the_wait_that_goes_on(
rt: &mut Runtime<'_>,
answer: Result<Value, RuntimeError>,
) -> Result<Value, RuntimeError> {
match answer {
Ok(value) => {
let found = value.to_text();
if !found.is_empty() && found != "0" {
rt.remember_the_last_window_of_the_v1(&found);
rt.set_the_error_level(Value::Int(0));
} else {
rt.set_the_error_level(Value::Int(1));
}
Ok(value)
}
Err(failure) => {
rt.set_the_error_level(Value::Int(1));
rt.note(format!(
"the command of the wait did not do the thing: {}",
failure.message
));
Ok(Value::Int(0))
}
}
}
fn the_values_of_a_wait(values: &[Value]) -> Vec<Value> {
let mut answer: Vec<Value> = values.iter().take(5).cloned().collect();
while answer.len() < 3 {
answer.push(Value::Text(String::new()));
}
answer
}
fn the_act_on_a_window(
rt: &mut Runtime<'_>,
name: &str,
values: &[Value],
line: u32,
handler: BuiltinHandler,
) -> Result<Value, RuntimeError> {
let answer = handler(rt, values, line, 0);
match answer {
Ok(value) => {
rt.set_the_error_level(Value::Int(0));
Ok(value)
}
Err(failure) => {
rt.set_the_error_level(Value::Int(1));
rt.note(format!(
"the command {name} did not do the thing: {}",
failure.message
));
Ok(Value::Text(String::new()))
}
}
}
pub(crate) fn win_activate(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
the_act_on_a_window(rt, "WinActivate", values, line, builtins::win_activate)
}
pub(crate) fn win_activate_bottom(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
the_act_on_a_window(
rt,
"WinActivateBottom",
values,
line,
builtins::win_activate_bottom,
)
}
pub(crate) fn win_hide(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
the_act_on_a_window(rt, "WinHide", values, line, builtins::win_hide)
}
pub(crate) fn win_show(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
the_act_on_a_window(rt, "WinShow", values, line, builtins::win_show)
}
pub(crate) fn win_minimize(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
the_act_on_a_window(rt, "WinMinimize", values, line, builtins::win_minimize)
}
pub(crate) fn win_maximize(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
the_act_on_a_window(rt, "WinMaximize", values, line, builtins::win_maximize)
}
pub(crate) fn win_restore(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
the_act_on_a_window(rt, "WinRestore", values, line, builtins::win_restore)
}
pub(crate) fn win_move(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
the_act_on_a_window(rt, "WinMove", values, line, builtins::win_move)
}
pub(crate) fn win_set_title(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
the_act_on_a_window(rt, "WinSetTitle", values, line, builtins::win_set_title)
}
pub(crate) fn win_close(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
the_act_on_a_window(rt, "WinClose", values, line, builtins::win_close)
}
pub(crate) fn win_kill(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
the_act_on_a_window(rt, "WinKill", values, line, builtins::win_kill)
}
pub(crate) fn win_minimize_all(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
the_act_on_a_window(
rt,
"WinMinimizeAll",
values,
line,
builtins::win_minimize_all,
)
}
pub(crate) fn win_set(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let sub_command = values
.first()
.map(|value| value.to_text())
.unwrap_or_default();
let title = values.get(1).map(|value| value.to_text()).unwrap_or_default();
let answer = the_change_of_the_sub_command(rt, &sub_command, &title, line);
match answer {
Ok(value) => {
rt.set_the_error_level(Value::Int(0));
Ok(value)
}
Err(failure) => {
rt.set_the_error_level(Value::Int(1));
rt.note(format!(
"the command WinSet did not do the thing: {}",
failure.message
));
Ok(Value::Text(String::new()))
}
}
}
fn the_change_of_the_sub_command(
rt: &mut Runtime<'_>,
sub_command: &str,
title: &str,
line: u32,
) -> Result<Value, RuntimeError> {
let mode = crate::settings::title_match_mode();
match sub_command.to_ascii_lowercase().as_str() {
"alwaysontop" => {
let windows = rt
.desktop()
.matching_windows(title, mode)
.map_err(|reason| builtins::desktop_failure(reason, line))?;
let Some(window) = windows.first() else {
return Err(RuntimeError {
message: builtins::TARGET_WINDOW.to_string(),
line,
});
};
let info = rt
.desktop()
.windows()
.map_err(|reason| builtins::desktop_failure(reason, line))?;
let Some(info) = info.iter().find(|other| other.id.to_string() == window.id) else {
return Err(RuntimeError {
message: builtins::TARGET_WINDOW.to_string(),
line,
});
};
rt.desktop()
.set_always_on_top(info, true)
.map_err(|reason| builtins::desktop_failure(reason, line))?;
Ok(Value::Text(String::new()))
}
"style" | "exstyle" => {
let style = rt
.desktop()
.window_style(title, sub_command.eq_ignore_ascii_case("exstyle"))
.map_err(|reason| builtins::desktop_failure(reason, line))?;
rt.desktop()
.window_set_style(title, style, sub_command.eq_ignore_ascii_case("exstyle"))
.map_err(|reason| builtins::desktop_failure(reason, line))?;
Ok(Value::Text(String::new()))
}
"transparent" => {
let color = rt
.desktop()
.window_trans_color(title)
.map_err(|reason| builtins::desktop_failure(reason, line))?;
rt.desktop()
.window_set_trans_color(title, &color)
.map_err(|reason| builtins::desktop_failure(reason, line))?;
Ok(Value::Text(String::new()))
}
"transcolor" => {
let color = rt
.desktop()
.window_trans_color(title)
.map_err(|reason| builtins::desktop_failure(reason, line))?;
rt.desktop()
.window_set_trans_color(title, &color)
.map_err(|reason| builtins::desktop_failure(reason, line))?;
Ok(Value::Text(String::new()))
}
_ => Err(RuntimeError {
message: format!("the sub-command {sub_command} of the WinSet is not known."),
line,
}),
}
}
pub(crate) fn win_menu_select_item(
rt: &mut Runtime<'_>,
_values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
rt.desktop()
.gui_menu_bar("{}")
.map_err(|reason| builtins::desktop_failure(reason, line))?;
Err(RuntimeError {
message: "the menu of another program is not read by this runtime, and the menu of the window of the script is not read either.".to_string(),
line,
})
}