1#![warn(missing_docs)]
2
3mod context;
6mod error;
7mod event;
8mod handler;
9mod types;
10mod window;
11
12pub(crate) use context::*;
13pub use error::*;
14pub use event::*;
15pub use types::*;
16pub use window::*;
17
18use std::{
19 ffi::{c_char, c_void, CStr, CString},
20 panic::{catch_unwind, AssertUnwindSafe},
21};
22
23use webui_sys::*;
24
25use crate::handler::LoggerHandler;
26
27pub fn exit() {
29 unsafe { webui_exit() }
30}
31
32pub fn wait() {
34 unsafe { webui_wait() }
35}
36
37pub fn wait_async() -> bool {
43 unsafe { webui_wait_async() }
44}
45
46pub fn open_url(url: &str) {
48 let url = CString::new(url).unwrap();
49 unsafe { webui_open_url(url.as_ptr()) }
50}
51
52pub(crate) fn set_config(option: Config, status: bool) {
54 unsafe {
55 webui_set_config(option as _, status);
56 }
57}
58
59pub fn set_show_wait_connection(status: bool) {
62 set_config(Config::ShowWaitConnection, status);
63}
64
65pub fn set_ui_event_blocking(status: bool) {
69 set_config(Config::UiEventBlocking, status);
70}
71
72pub fn set_folder_monitor(status: bool) {
75 set_config(Config::FolderMonitor, status);
76}
77
78pub fn set_multi_client(status: bool) {
82 set_config(Config::MultiClient, status);
83}
84
85pub fn set_use_cookies(status: bool) {
89 set_config(Config::UseCookies, status);
90}
91
92pub fn set_asynchronous_response(status: bool) {
96 set_config(Config::AsynchronousResponse, status);
97}
98
99pub fn set_default_root_folder(path: &str) -> Result<(), WebUIError> {
101 let path = CString::new(path).unwrap();
102 let result = unsafe { webui_set_default_root_folder(path.as_ptr()) };
103 WebUIError::from_bool(result)
104}
105
106pub fn get_mime_type(file: &str) -> String {
108 let file = CString::new(file).unwrap();
109 unsafe {
110 let mine_type = webui_get_mime_type(file.as_ptr());
111 CStr::from_ptr(mine_type).to_string_lossy().to_string()
112 }
113}
114
115pub fn browser_exist(browser: Browser) -> bool {
117 unsafe { webui_browser_exist(browser as _) }
118}
119
120pub fn set_browser_folder(path: &str) {
122 let path = CString::new(path).unwrap();
123 unsafe { webui_set_browser_folder(path.as_ptr()) }
124}
125
126pub fn delete_all_profiles() {
128 unsafe { webui_delete_all_profiles() }
129}
130
131pub fn get_port() -> usize {
133 unsafe { webui_get_free_port() }
134}
135
136pub fn encode(text: &str) -> String {
138 let text = CString::new(text).unwrap();
139 let encoded = unsafe { webui_encode(text.as_ptr()) };
140 let result = unsafe { CStr::from_ptr(encoded as _).to_string_lossy().to_string() };
141 unsafe { webui_free(encoded as _) };
142 result
143}
144
145pub fn decode(text: &str) -> String {
147 let text = CString::new(text).unwrap();
148 let decoded = unsafe { webui_decode(text.as_ptr()) };
149 let result = unsafe { CStr::from_ptr(decoded as _).to_string_lossy().to_string() };
150 unsafe { webui_free(decoded as _) };
151 result
152}
153
154pub fn set_tls_certificate(certificate_pem: &str, private_key_pem: &str) -> Result<(), WebUIError> {
160 let certificate_pem = CString::new(certificate_pem).unwrap();
161 let private_key_pem = CString::new(private_key_pem).unwrap();
162 let result = unsafe { webui_set_tls_certificate(certificate_pem.as_ptr(), private_key_pem.as_ptr()) };
163 WebUIError::from_bool(result)
164}
165
166pub fn set_logger<F>(callback: F)
169where
170 F: LoggerHandler,
171{
172 extern "C" fn shim(level: usize, log: *const c_char, user_data: *mut c_void) {
173 let level: LoggerLevel = unsafe { std::mem::transmute(level) };
174 let log = unsafe { CStr::from_ptr(log).to_string_lossy().to_string() };
175 let callback = unsafe { &*(user_data as *mut Box<dyn LoggerHandler>) };
176 let _ = catch_unwind(AssertUnwindSafe(|| callback(level, &log)));
177 }
178 let user_data: Box<dyn LoggerHandler> = Box::new(callback);
179 let user_data = Box::into_raw(Box::new(user_data));
180 unsafe {
181 webui_set_logger(Some(shim), user_data as _);
182 }
183}
184
185pub unsafe fn clean() {
188 webui_clean();
189}
190
191pub(crate) fn get_last_error_number() -> usize {
193 unsafe { webui_get_last_error_number() }
194}
195
196pub(crate) fn get_last_error_message() -> String {
198 unsafe {
199 let message = webui_get_last_error_message();
200 CStr::from_ptr(message).to_string_lossy().to_string()
201 }
202}