use crate::controls::{Control, UIElement};
use crate::error::{Error, Result};
use crate::events::{EventHandler, TextChangedEventArgs};
use parking_lot::RwLock;
use std::sync::Arc;
use windows::core::{w, PCWSTR};
use windows::Win32::{
Foundation::*,
System::LibraryLoader::GetModuleHandleW,
UI::WindowsAndMessaging::*,
};
const ES_LEFT: u32 = 0x0000;
const ES_AUTOHSCROLL: u32 = 0x0080;
#[derive(Clone, Debug)]
pub struct TextBox {
element: UIElement,
inner: Arc<TextBoxInner>,
}
#[derive(Debug)]
struct TextBoxInner {
text: RwLock<String>,
placeholder: RwLock<String>,
max_length: RwLock<Option<u32>>,
text_changed: EventHandler<TextChangedEventArgs>,
}
impl Control for TextBox {
fn create_control(&self, parent: HWND) -> Result<()> {
self.create(parent)
}
fn as_element(&self) -> &UIElement {
&self.element
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
}
impl TextBox {
pub fn new() -> Result<Self> {
let inner = Arc::new(TextBoxInner {
text: RwLock::new(String::new()),
placeholder: RwLock::new(String::new()),
max_length: RwLock::new(None),
text_changed: EventHandler::new(),
});
Ok(TextBox {
element: UIElement::empty(),
inner,
})
}
pub(crate) fn create(&self, parent: HWND) -> Result<()> {
unsafe {
let hinstance = GetModuleHandleW(None)?;
let text = self.inner.text.read().clone();
let text_wide: Vec<u16> = text.encode_utf16().chain(Some(0)).collect();
let hwnd = CreateWindowExW(
WINDOW_EX_STYLE(0),
w!("EDIT"),
PCWSTR(text_wide.as_ptr()),
WS_CHILD | WS_VISIBLE | WS_BORDER | WS_TABSTOP | WINDOW_STYLE(ES_LEFT | ES_AUTOHSCROLL),
0,
0,
200,
25,
parent,
HMENU(std::ptr::null_mut()),
HINSTANCE(hinstance.0),
None,
)?;
if hwnd.0.is_null() {
return Err(Error::control_creation("Failed to create text box"));
}
self.element.set_hwnd(hwnd);
self.element.set_width(200);
self.element.set_height(25);
Ok(())
}
}
pub fn text(&self) -> String {
self.inner.text.read().clone()
}
pub fn set_text(&self, text: impl Into<String>) -> Result<()> {
let text = text.into();
*self.inner.text.write() = text.clone();
let hwnd = self.element.hwnd();
if !hwnd.0.is_null() {
unsafe {
let text_wide: Vec<u16> = text.encode_utf16().chain(Some(0)).collect();
SetWindowTextW(hwnd, PCWSTR(text_wide.as_ptr()))?;
}
}
Ok(())
}
pub fn with_text(self, text: impl Into<String>) -> Result<Self> {
self.set_text(text)?;
Ok(self)
}
pub fn placeholder(&self) -> String {
self.inner.placeholder.read().clone()
}
pub fn set_placeholder(&self, placeholder: impl Into<String>) {
*self.inner.placeholder.write() = placeholder.into();
}
pub fn with_placeholder(self, placeholder: impl Into<String>) -> Self {
self.set_placeholder(placeholder);
self
}
pub fn max_length(&self) -> Option<u32> {
*self.inner.max_length.read()
}
pub fn set_max_length(&self, max_length: Option<u32>) {
*self.inner.max_length.write() = max_length;
}
pub fn with_max_length(self, max_length: Option<u32>) -> Self {
self.set_max_length(max_length);
self
}
pub fn text_changed(&self) -> &EventHandler<TextChangedEventArgs> {
&self.inner.text_changed
}
pub fn element(&self) -> &UIElement {
&self.element
}
pub fn hwnd(&self) -> HWND {
self.element.hwnd()
}
pub fn with_x(self, x: i32) -> Self {
self.element.set_x(x);
self
}
pub fn with_y(self, y: i32) -> Self {
self.element.set_y(y);
self
}
pub fn with_width(self, width: i32) -> Self {
self.element.set_width(width);
self
}
pub fn with_height(self, height: i32) -> Self {
self.element.set_height(height);
self
}
pub fn set_position(&self, x: i32, y: i32) -> Result<()> {
self.element.set_x(x);
self.element.set_y(y);
let hwnd = self.hwnd();
if !hwnd.0.is_null() {
unsafe {
SetWindowPos(hwnd, HWND(std::ptr::null_mut()), x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER)?;
}
}
Ok(())
}
pub fn set_size(&self, width: i32, height: i32) -> Result<()> {
self.element.set_width(width);
self.element.set_height(height);
let hwnd = self.hwnd();
if !hwnd.0.is_null() {
unsafe {
SetWindowPos(hwnd, HWND(std::ptr::null_mut()), 0, 0, width, height, SWP_NOMOVE | SWP_NOZORDER)?;
}
}
Ok(())
}
}
impl Default for TextBox {
fn default() -> Self {
Self::new().expect("Failed to create text box")
}
}
impl From<TextBox> for UIElement {
fn from(textbox: TextBox) -> Self {
textbox.element.clone()
}
}