1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
//! Safe wrapper for wxWebView.
use crate::event::WxEvtHandler;
use crate::geometry::{Point, Size};
use crate::id::Id;
use crate::window::{WindowHandle, WxWidget};
// Window is used by new_from_composition for backwards compatibility
#[allow(unused_imports)]
use crate::window::Window;
use std::ffi::CString;
use std::os::raw::c_char;
use wxdragon_sys as ffi;
// WebView Zoom Types
#[repr(i32)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WebViewZoomType {
Layout = 0,
Text = 1,
}
impl From<WebViewZoomType> for i32 {
fn from(val: WebViewZoomType) -> Self {
val as i32
}
}
// WebView Zoom Levels (Standard levels, though it can be arbitrary)
#[repr(i32)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WebViewZoom {
Tiny = 0,
Small = 1,
Medium = 2,
Large = 3,
Largest = 4,
}
impl From<WebViewZoom> for i32 {
fn from(val: WebViewZoom) -> Self {
val as i32
}
}
// WebView Reload Flags
#[repr(i32)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WebViewReloadFlags {
Default = 0,
NoCache = 1,
}
impl From<WebViewReloadFlags> for i32 {
fn from(val: WebViewReloadFlags) -> Self {
val as i32
}
}
// WebView Find Flags
bitflags::bitflags! {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct WebViewFindFlags: i32 {
const WRAP = 0x0001;
const ENTIRE_WORD = 0x0002;
const MATCH_CASE = 0x0004;
const HIGHLIGHT_RESULT = 0x0008;
const BACKWARDS = 0x0010;
const DEFAULT = 0;
}
}
// WebView User Script Injection Time
#[repr(i32)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WebViewUserScriptInjectionTime {
AtDocumentStart = 0,
AtDocumentEnd = 1,
}
impl From<WebViewUserScriptInjectionTime> for i32 {
fn from(val: WebViewUserScriptInjectionTime) -> Self {
val as i32
}
}
// WebView Navigation Error
#[repr(i32)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WebViewNavigationError {
Connection = 0,
Certificate = 1,
Auth = 2,
Security = 3,
NotFound = 4,
Request = 5,
UserCancelled = 6,
Other = 7,
}
// WebView Browsing Data Types
bitflags::bitflags! {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct WebViewBrowsingDataTypes: i32 {
const COOKIES = 0x01;
const CACHE = 0x02;
const DOM_STORAGE = 0x04;
const OTHER = 0x08;
const ALL = 0x0f;
}
}
/// WebView Backend selection.
///
/// # Platform Support
/// - **Windows**: Prefers Edge (WebView2/Chromium) when available, falls back to IE (Trident).
/// The Edge backend requires the WebView2 runtime to be installed.
/// - **macOS**: Uses WebKit (Safari engine).
/// - **Linux**: Uses WebKit2GTK.
///
/// # IE Backend Limitations
/// The IE backend (used when Edge/WebView2 is not available on Windows) has significant
/// limitations:
/// - Many modern websites may not render correctly or may show a white screen
/// - Some zoom operations are not fully supported
/// - JavaScript execution may be limited
/// - For best results on Windows, ensure the WebView2 runtime is installed
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum WebViewBackend {
/// Default backend for the current platform.
/// Uses the platform's native web view implementation.
#[default]
Default,
/// Legacy Internet Explorer (Trident) backend for Windows.
/// Limited compatibility with modern websites.
IE,
/// Modern Edge (WebView2/Chromium) backend for Windows.
/// Requires WebView2 runtime.
Edge,
/// WebKit backend for macOS and Linux.
WebKit,
}
impl WebViewBackend {
/// Returns the wxWidgets backend identifier string.
pub fn as_str(&self) -> &'static str {
match self {
WebViewBackend::Default => "",
WebViewBackend::IE => "wxWebViewIE",
WebViewBackend::Edge => "wxWebViewEdge",
WebViewBackend::WebKit => "wxWebViewWebKit",
}
}
}
impl std::fmt::Display for WebViewBackend {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.as_str())
}
}
/// Represents a wxWebView widget.
///
/// WebView uses `WindowHandle` internally for safe memory management.
/// When the underlying window is destroyed (by calling `destroy()` or when
/// its parent is destroyed), the handle becomes invalid and all operations
/// become safe no-ops.
///
/// # Example
/// ```ignore
/// let webview = WebView::builder(&frame).url("https://example.com").build();
///
/// // WebView is Copy - no clone needed for closures!
/// webview.bind_loaded(move |_| {
/// // Safe: if webview was destroyed, this is a no-op
/// webview.load_url("https://rust-lang.org");
/// });
///
/// // After parent destruction, webview operations are safe no-ops
/// frame.destroy();
/// assert!(!webview.is_valid());
/// ```
#[derive(Clone, Copy)]
pub struct WebView {
/// Safe handle to the underlying wxWebView - automatically invalidated on destroy
handle: WindowHandle,
}
impl WebView {
/// Creates a new WebView builder.
pub fn builder(parent: &dyn WxWidget) -> WebViewBuilder<'_> {
WebViewBuilder::new(parent)
}
/// Creates a new WebView from a raw pointer.
/// This is intended for internal use by other widget wrappers.
#[allow(dead_code)]
pub(crate) fn from_ptr(ptr: *mut ffi::wxd_Window_t) -> Self {
Self {
handle: WindowHandle::new(ptr),
}
}
/// Creates a new WebView from a raw window pointer.
/// This is for backwards compatibility with widgets that compose WebView.
/// The parent_ptr parameter is ignored (kept for API compatibility).
#[allow(dead_code)]
pub(crate) fn new_from_composition(_window: Window, _parent_ptr: *mut ffi::wxd_Window_t) -> Self {
// Use the window's pointer to create a new WindowHandle
Self {
handle: WindowHandle::new(_window.as_ptr()),
}
}
/// Creates a new WebView (low-level constructor used by the builder)
#[allow(clippy::too_many_arguments)]
fn new_impl(
parent_ptr: *mut ffi::wxd_Window_t,
id: Id,
url: Option<&str>,
pos: Point,
size: Size,
style: i64,
name: Option<&str>,
backend: Option<&str>,
) -> Self {
assert!(!parent_ptr.is_null(), "WebView requires a parent");
let c_url = url.map(|s| CString::new(s).unwrap_or_default());
let c_name = name.map(|s| CString::new(s).unwrap_or_default());
let c_backend = backend.map(|s| CString::new(s).unwrap_or_default());
// Get raw pointers while keeping CStrings alive
let url_ptr = c_url.as_ref().map(|c| c.as_ptr()).unwrap_or(std::ptr::null());
let name_ptr = c_name.as_ref().map(|c| c.as_ptr()).unwrap_or(std::ptr::null());
let backend_ptr = c_backend.as_ref().map(|c| c.as_ptr()).unwrap_or(std::ptr::null());
let ptr = unsafe {
ffi::wxd_WebView_Create(
parent_ptr,
id,
url_ptr,
pos.into(),
size.into(),
style as _,
name_ptr,
backend_ptr,
)
};
if ptr.is_null() {
panic!("Failed to create WebView widget");
}
// Note: Zoom operations on IE backend are disabled in the C++ layer
// to avoid assertion failures. All zoom-related calls become no-ops on IE.
// Create a WindowHandle which automatically registers for destroy events
WebView {
handle: WindowHandle::new(ptr as *mut ffi::wxd_Window_t),
}
}
/// Helper to get raw webview pointer, returns null if widget has been destroyed
#[inline]
fn webview_ptr(&self) -> *mut ffi::wxd_WebView_t {
self.handle
.get_ptr()
.map(|p| p as *mut ffi::wxd_WebView_t)
.unwrap_or(std::ptr::null_mut())
}
fn read_string_with_retry(initial_capacity: usize, mut getter: impl FnMut(*mut c_char, i32) -> i32) -> String {
let mut buffer: Vec<c_char> = vec![0; initial_capacity];
let mut len = getter(buffer.as_mut_ptr(), buffer.len() as i32);
if len < 0 {
return String::new();
}
if len as usize >= buffer.len() {
buffer = vec![0; len as usize + 1];
len = getter(buffer.as_mut_ptr(), buffer.len() as i32);
if len < 0 {
return String::new();
}
}
let byte_slice = unsafe { std::slice::from_raw_parts(buffer.as_ptr() as *const u8, len as usize) };
String::from_utf8_lossy(byte_slice).to_string()
}
// --- Navigation ---
/// Loads the specified URL.
/// No-op if the webview has been destroyed.
pub fn load_url(&self, url: &str) {
let ptr = self.webview_ptr();
if ptr.is_null() {
return;
}
let c_url = CString::new(url).unwrap_or_default();
unsafe { ffi::wxd_WebView_LoadURL(ptr, c_url.as_ptr()) };
}
/// Reloads the current page.
/// No-op if the webview has been destroyed.
pub fn reload(&self, flags: WebViewReloadFlags) {
let ptr = self.webview_ptr();
if ptr.is_null() {
return;
}
unsafe { ffi::wxd_WebView_Reload(ptr, flags.into()) };
}
/// Stops the current page loading.
/// No-op if the webview has been destroyed.
pub fn stop(&self) {
let ptr = self.webview_ptr();
if ptr.is_null() {
return;
}
unsafe { ffi::wxd_WebView_Stop(ptr) };
}
/// Returns whether the webview can navigate back.
/// Returns false if the webview has been destroyed.
pub fn can_go_back(&self) -> bool {
let ptr = self.webview_ptr();
if ptr.is_null() {
return false;
}
unsafe { ffi::wxd_WebView_CanGoBack(ptr) }
}
/// Returns whether the webview can navigate forward.
/// Returns false if the webview has been destroyed.
pub fn can_go_forward(&self) -> bool {
let ptr = self.webview_ptr();
if ptr.is_null() {
return false;
}
unsafe { ffi::wxd_WebView_CanGoForward(ptr) }
}
/// Navigates back in the history.
/// No-op if the webview has been destroyed.
pub fn go_back(&self) {
let ptr = self.webview_ptr();
if ptr.is_null() {
return;
}
unsafe { ffi::wxd_WebView_GoBack(ptr) };
}
/// Navigates forward in the history.
/// No-op if the webview has been destroyed.
pub fn go_forward(&self) {
let ptr = self.webview_ptr();
if ptr.is_null() {
return;
}
unsafe { ffi::wxd_WebView_GoForward(ptr) };
}
/// Clears the navigation history.
/// No-op if the webview has been destroyed.
pub fn clear_history(&self) {
let ptr = self.webview_ptr();
if ptr.is_null() {
return;
}
unsafe { ffi::wxd_WebView_ClearHistory(ptr) };
}
// --- State ---
/// Returns whether the webview is currently busy loading a page.
/// Returns false if the webview has been destroyed.
pub fn is_busy(&self) -> bool {
let ptr = self.webview_ptr();
if ptr.is_null() {
return false;
}
unsafe { ffi::wxd_WebView_IsBusy(ptr) }
}
/// Returns the current URL.
/// Returns empty string if the webview has been destroyed.
pub fn get_current_url(&self) -> String {
let ptr = self.webview_ptr();
if ptr.is_null() {
return String::new();
}
unsafe { Self::read_string_with_retry(2048, |buf, len| ffi::wxd_WebView_GetCurrentURL(ptr, buf, len)) }
}
/// Returns the current page title.
/// Returns empty string if the webview has been destroyed.
pub fn get_current_title(&self) -> String {
let ptr = self.webview_ptr();
if ptr.is_null() {
return String::new();
}
unsafe { Self::read_string_with_retry(1024, |buf, len| ffi::wxd_WebView_GetCurrentTitle(ptr, buf, len)) }
}
/// Returns the page source (HTML).
/// Returns empty string if the webview has been destroyed.
pub fn get_page_source(&self) -> String {
let ptr = self.webview_ptr();
if ptr.is_null() {
return String::new();
}
// Page source can be large, use dynamic buffer resizing
unsafe {
// First call with moderate buffer to get the size
let mut buffer: Vec<c_char> = vec![0; 1024 * 64]; // 64KB initial buffer
let len = ffi::wxd_WebView_GetPageSource(ptr, buffer.as_mut_ptr(), buffer.len() as i32);
if len < 0 {
return String::new(); // Error
}
// Check if we need a larger buffer
if len >= buffer.len() as i32 {
// Allocate larger buffer and retry
buffer = vec![0; len as usize + 1];
let len2 = ffi::wxd_WebView_GetPageSource(ptr, buffer.as_mut_ptr(), buffer.len() as i32);
if len2 < 0 {
return String::new(); // Error on second call
}
}
let actual_len = std::cmp::min(len as usize, buffer.len() - 1);
let byte_slice = std::slice::from_raw_parts(buffer.as_ptr() as *const u8, actual_len);
String::from_utf8_lossy(byte_slice).to_string()
}
}
/// Returns the page text content (without HTML tags).
/// Returns empty string if the webview has been destroyed.
pub fn get_page_text(&self) -> String {
let ptr = self.webview_ptr();
if ptr.is_null() {
return String::new();
}
// Page text can be large, use dynamic buffer resizing
unsafe {
// First call with moderate buffer to get the size
let mut buffer: Vec<c_char> = vec![0; 1024 * 64]; // 64KB initial buffer
let len = ffi::wxd_WebView_GetPageText(ptr, buffer.as_mut_ptr(), buffer.len() as i32);
if len < 0 {
return String::new(); // Error
}
// Check if we need a larger buffer
if len >= buffer.len() as i32 {
// Allocate larger buffer and retry
buffer = vec![0; len as usize + 1];
let len2 = ffi::wxd_WebView_GetPageText(ptr, buffer.as_mut_ptr(), buffer.len() as i32);
if len2 < 0 {
return String::new(); // Error on second call
}
}
let actual_len = std::cmp::min(len as usize, buffer.len() - 1);
let byte_slice = std::slice::from_raw_parts(buffer.as_ptr() as *const u8, actual_len);
String::from_utf8_lossy(byte_slice).to_string()
}
}
// --- Zoom ---
/// Returns whether the zoom type can be set.
/// Returns false if the webview has been destroyed.
pub fn can_set_zoom_type(&self, zoom_type: WebViewZoomType) -> bool {
let ptr = self.webview_ptr();
if ptr.is_null() {
return false;
}
unsafe { ffi::wxd_WebView_CanSetZoomType(ptr, zoom_type.into()) }
}
/// Returns the current zoom level.
/// Returns Medium if the webview has been destroyed.
pub fn get_zoom(&self) -> WebViewZoom {
let ptr = self.webview_ptr();
if ptr.is_null() {
return WebViewZoom::Medium;
}
let val = unsafe { ffi::wxd_WebView_GetZoom(ptr) };
match val {
0 => WebViewZoom::Tiny,
1 => WebViewZoom::Small,
2 => WebViewZoom::Medium,
3 => WebViewZoom::Large,
4 => WebViewZoom::Largest,
_ => WebViewZoom::Medium,
}
}
/// Returns the current zoom type.
/// Returns Layout if the webview has been destroyed.
pub fn get_zoom_type(&self) -> WebViewZoomType {
let ptr = self.webview_ptr();
if ptr.is_null() {
return WebViewZoomType::Layout;
}
let val = unsafe { ffi::wxd_WebView_GetZoomType(ptr) };
match val {
0 => WebViewZoomType::Layout,
1 => WebViewZoomType::Text,
_ => WebViewZoomType::Layout,
}
}
/// Sets the zoom level.
/// No-op if the webview has been destroyed.
pub fn set_zoom(&self, zoom: WebViewZoom) {
let ptr = self.webview_ptr();
if ptr.is_null() {
return;
}
unsafe { ffi::wxd_WebView_SetZoom(ptr, zoom.into()) };
}
/// Sets the zoom type.
/// No-op if the webview has been destroyed.
pub fn set_zoom_type(&self, zoom_type: WebViewZoomType) {
let ptr = self.webview_ptr();
if ptr.is_null() {
return;
}
unsafe { ffi::wxd_WebView_SetZoomType(ptr, zoom_type.into()) };
}
// --- Scripting ---
/// Runs JavaScript code and returns the result.
/// Returns None if the webview has been destroyed or if there was an error.
pub fn run_script(&self, javascript: &str) -> Option<String> {
let ptr = self.webview_ptr();
if ptr.is_null() {
return None;
}
let c_script = CString::new(javascript).unwrap_or_default();
unsafe {
let mut buffer: Vec<c_char> = vec![0; 4096];
let len = ffi::wxd_WebView_RunScript(ptr, c_script.as_ptr(), buffer.as_mut_ptr(), buffer.len() as i32);
if len < 0 {
return None; // Error
}
// Check if we need a larger buffer
if len >= buffer.len() as i32 {
// Allocate larger buffer and retry
buffer = vec![0; len as usize + 1];
let len2 = ffi::wxd_WebView_RunScript(ptr, c_script.as_ptr(), buffer.as_mut_ptr(), buffer.len() as i32);
if len2 < 0 {
return None; // Error on second call
}
}
let actual_len = std::cmp::min(len as usize, buffer.len() - 1);
let byte_slice = std::slice::from_raw_parts(buffer.as_ptr() as *const u8, actual_len);
Some(String::from_utf8_lossy(byte_slice).to_string())
}
}
// --- Clipboard ---
/// Returns whether the webview can cut.
/// Returns false if the webview has been destroyed.
pub fn can_cut(&self) -> bool {
let ptr = self.webview_ptr();
if ptr.is_null() {
return false;
}
unsafe { ffi::wxd_WebView_CanCut(ptr) }
}
/// Returns whether the webview can copy.
/// Returns false if the webview has been destroyed.
pub fn can_copy(&self) -> bool {
let ptr = self.webview_ptr();
if ptr.is_null() {
return false;
}
unsafe { ffi::wxd_WebView_CanCopy(ptr) }
}
/// Returns whether the webview can paste.
/// Returns false if the webview has been destroyed.
pub fn can_paste(&self) -> bool {
let ptr = self.webview_ptr();
if ptr.is_null() {
return false;
}
unsafe { ffi::wxd_WebView_CanPaste(ptr) }
}
/// Cuts the selected content.
/// No-op if the webview has been destroyed.
pub fn cut(&self) {
let ptr = self.webview_ptr();
if ptr.is_null() {
return;
}
unsafe { ffi::wxd_WebView_Cut(ptr) };
}
/// Copies the selected content.
/// No-op if the webview has been destroyed.
pub fn copy(&self) {
let ptr = self.webview_ptr();
if ptr.is_null() {
return;
}
unsafe { ffi::wxd_WebView_Copy(ptr) };
}
/// Pastes content from clipboard.
/// No-op if the webview has been destroyed.
pub fn paste(&self) {
let ptr = self.webview_ptr();
if ptr.is_null() {
return;
}
unsafe { ffi::wxd_WebView_Paste(ptr) };
}
/// Returns whether the webview can undo.
/// Returns false if the webview has been destroyed.
pub fn can_undo(&self) -> bool {
let ptr = self.webview_ptr();
if ptr.is_null() {
return false;
}
unsafe { ffi::wxd_WebView_CanUndo(ptr) }
}
/// Returns whether the webview can redo.
/// Returns false if the webview has been destroyed.
pub fn can_redo(&self) -> bool {
let ptr = self.webview_ptr();
if ptr.is_null() {
return false;
}
unsafe { ffi::wxd_WebView_CanRedo(ptr) }
}
/// Undoes the last action.
/// No-op if the webview has been destroyed.
pub fn undo(&self) {
let ptr = self.webview_ptr();
if ptr.is_null() {
return;
}
unsafe { ffi::wxd_WebView_Undo(ptr) };
}
/// Redoes the last undone action.
/// No-op if the webview has been destroyed.
pub fn redo(&self) {
let ptr = self.webview_ptr();
if ptr.is_null() {
return;
}
unsafe { ffi::wxd_WebView_Redo(ptr) };
}
// --- Selection ---
/// Selects all content.
/// No-op if the webview has been destroyed.
pub fn select_all(&self) {
let ptr = self.webview_ptr();
if ptr.is_null() {
return;
}
unsafe { ffi::wxd_WebView_SelectAll(ptr) };
}
/// Returns whether there is a selection.
/// Returns false if the webview has been destroyed.
pub fn has_selection(&self) -> bool {
let ptr = self.webview_ptr();
if ptr.is_null() {
return false;
}
unsafe { ffi::wxd_WebView_HasSelection(ptr) }
}
/// Deletes the current selection.
/// No-op if the webview has been destroyed.
pub fn delete_selection(&self) {
let ptr = self.webview_ptr();
if ptr.is_null() {
return;
}
unsafe { ffi::wxd_WebView_DeleteSelection(ptr) };
}
/// Returns the selected text.
/// Returns empty string if the webview has been destroyed.
pub fn get_selected_text(&self) -> String {
let ptr = self.webview_ptr();
if ptr.is_null() {
return String::new();
}
unsafe { Self::read_string_with_retry(4096, |buf, len| ffi::wxd_WebView_GetSelectedText(ptr, buf, len)) }
}
/// Returns the selected HTML source.
/// Returns empty string if the webview has been destroyed.
pub fn get_selected_source(&self) -> String {
let ptr = self.webview_ptr();
if ptr.is_null() {
return String::new();
}
unsafe { Self::read_string_with_retry(4096, |buf, len| ffi::wxd_WebView_GetSelectedSource(ptr, buf, len)) }
}
/// Clears the current selection.
/// No-op if the webview has been destroyed.
pub fn clear_selection(&self) {
let ptr = self.webview_ptr();
if ptr.is_null() {
return;
}
unsafe { ffi::wxd_WebView_ClearSelection(ptr) };
}
// --- Editing ---
/// Returns whether the webview is editable.
/// Returns false if the webview has been destroyed.
pub fn is_editable(&self) -> bool {
let ptr = self.webview_ptr();
if ptr.is_null() {
return false;
}
unsafe { ffi::wxd_WebView_IsEditable(ptr) }
}
/// Sets whether the webview is editable.
/// No-op if the webview has been destroyed.
pub fn set_editable(&self, enable: bool) {
let ptr = self.webview_ptr();
if ptr.is_null() {
return;
}
unsafe { ffi::wxd_WebView_SetEditable(ptr, enable) };
}
// --- Printing ---
/// Opens the print dialog.
/// No-op if the webview has been destroyed.
pub fn print(&self) {
let ptr = self.webview_ptr();
if ptr.is_null() {
return;
}
unsafe { ffi::wxd_WebView_Print(ptr) };
}
// --- Context Menu & Dev Tools ---
/// Enables or disables the context menu.
/// No-op if the webview has been destroyed.
pub fn enable_context_menu(&self, enable: bool) {
let ptr = self.webview_ptr();
if ptr.is_null() {
return;
}
unsafe { ffi::wxd_WebView_EnableContextMenu(ptr, enable) };
}
/// Returns whether the context menu is enabled.
/// Returns false if the webview has been destroyed.
pub fn is_context_menu_enabled(&self) -> bool {
let ptr = self.webview_ptr();
if ptr.is_null() {
return false;
}
unsafe { ffi::wxd_WebView_IsContextMenuEnabled(ptr) }
}
/// Enables or disables access to developer tools.
/// No-op if the webview has been destroyed.
pub fn enable_access_to_dev_tools(&self, enable: bool) {
let ptr = self.webview_ptr();
if ptr.is_null() {
return;
}
unsafe { ffi::wxd_WebView_EnableAccessToDevTools(ptr, enable) };
}
/// Returns whether access to developer tools is enabled.
/// Returns false if the webview has been destroyed.
pub fn is_access_to_dev_tools_enabled(&self) -> bool {
let ptr = self.webview_ptr();
if ptr.is_null() {
return false;
}
unsafe { ffi::wxd_WebView_IsAccessToDevToolsEnabled(ptr) }
}
/// Shows the developer tools.
/// Returns false if the webview has been destroyed or if showing failed.
pub fn show_dev_tools(&self) -> bool {
let ptr = self.webview_ptr();
if ptr.is_null() {
return false;
}
unsafe { ffi::wxd_WebView_ShowDevTools(ptr) }
}
/// Enables or disables browser accelerator keys.
/// No-op if the webview has been destroyed.
pub fn enable_browser_accelerator_keys(&self, enable: bool) {
let ptr = self.webview_ptr();
if ptr.is_null() {
return;
}
unsafe { ffi::wxd_WebView_EnableBrowserAcceleratorKeys(ptr, enable) };
}
/// Returns whether browser accelerator keys are enabled.
/// Returns false if the webview has been destroyed.
pub fn are_browser_accelerator_keys_enabled(&self) -> bool {
let ptr = self.webview_ptr();
if ptr.is_null() {
return false;
}
unsafe { ffi::wxd_WebView_AreBrowserAcceleratorKeysEnabled(ptr) }
}
// --- Zoom Factor ---
/// Returns the current zoom factor.
/// Returns 1.0 if the webview has been destroyed.
pub fn get_zoom_factor(&self) -> f32 {
let ptr = self.webview_ptr();
if ptr.is_null() {
return 1.0;
}
unsafe { ffi::wxd_WebView_GetZoomFactor(ptr) }
}
/// Sets the zoom factor.
/// No-op if the webview has been destroyed.
pub fn set_zoom_factor(&self, zoom: f32) {
let ptr = self.webview_ptr();
if ptr.is_null() {
return;
}
unsafe { ffi::wxd_WebView_SetZoomFactor(ptr, zoom) };
}
// --- Page Loading ---
/// Sets the page content from HTML string.
/// No-op if the webview has been destroyed.
pub fn set_page(&self, html: &str, base_url: &str) {
let ptr = self.webview_ptr();
if ptr.is_null() {
return;
}
let c_html = CString::new(html).unwrap_or_default();
let c_base_url = CString::new(base_url).unwrap_or_default();
unsafe {
ffi::wxd_WebView_SetPage(ptr, c_html.as_ptr(), c_base_url.as_ptr());
}
}
/// Finds text in the page.
/// Returns 0 if the webview has been destroyed.
pub fn find(&self, text: &str, flags: WebViewFindFlags) -> i64 {
let ptr = self.webview_ptr();
if ptr.is_null() {
return 0;
}
let c_text = CString::new(text).unwrap_or_default();
unsafe { ffi::wxd_WebView_Find(ptr, c_text.as_ptr(), flags.bits()) as i64 }
}
// --- History ---
/// Enables or disables history.
/// No-op if the webview has been destroyed.
pub fn enable_history(&self, enable: bool) {
let ptr = self.webview_ptr();
if ptr.is_null() {
return;
}
unsafe { ffi::wxd_WebView_EnableHistory(ptr, enable) };
}
// --- Configuration ---
/// Sets the user agent string.
/// Returns false if the webview has been destroyed.
pub fn set_user_agent(&self, user_agent: &str) -> bool {
let ptr = self.webview_ptr();
if ptr.is_null() {
return false;
}
let c_user_agent = CString::new(user_agent).unwrap_or_default();
unsafe { ffi::wxd_WebView_SetUserAgent(ptr, c_user_agent.as_ptr()) }
}
/// Returns the user agent string.
/// Returns empty string if the webview has been destroyed.
pub fn get_user_agent(&self) -> String {
let ptr = self.webview_ptr();
if ptr.is_null() {
return String::new();
}
unsafe {
let mut buffer: Vec<c_char> = vec![0; 1024];
let len = ffi::wxd_WebView_GetUserAgent(ptr, buffer.as_mut_ptr(), buffer.len() as i32);
if len < 0 {
return String::new(); // Error
}
// Check if we need a larger buffer
if len >= buffer.len() as i32 {
// Allocate larger buffer and retry
buffer = vec![0; len as usize + 1];
let len2 = ffi::wxd_WebView_GetUserAgent(ptr, buffer.as_mut_ptr(), buffer.len() as i32);
if len2 < 0 {
return String::new(); // Error on second call
}
}
let actual_len = std::cmp::min(len as usize, buffer.len() - 1);
let byte_slice = std::slice::from_raw_parts(buffer.as_ptr() as *const u8, actual_len);
String::from_utf8_lossy(byte_slice).to_string()
}
}
/// Sets the proxy configuration.
/// Returns false if the webview has been destroyed.
pub fn set_proxy(&self, proxy: &str) -> bool {
let ptr = self.webview_ptr();
if ptr.is_null() {
return false;
}
let c_proxy = CString::new(proxy).unwrap_or_default();
unsafe { ffi::wxd_WebView_SetProxy(ptr, c_proxy.as_ptr()) }
}
// --- Advanced Scripting ---
/// Adds a script message handler.
/// Returns false if the webview has been destroyed.
pub fn add_script_message_handler(&self, name: &str) -> bool {
let ptr = self.webview_ptr();
if ptr.is_null() {
return false;
}
let c_name = CString::new(name).unwrap_or_default();
unsafe { ffi::wxd_WebView_AddScriptMessageHandler(ptr, c_name.as_ptr()) }
}
/// Removes a script message handler.
/// Returns false if the webview has been destroyed.
pub fn remove_script_message_handler(&self, name: &str) -> bool {
let ptr = self.webview_ptr();
if ptr.is_null() {
return false;
}
let c_name = CString::new(name).unwrap_or_default();
unsafe { ffi::wxd_WebView_RemoveScriptMessageHandler(ptr, c_name.as_ptr()) }
}
/// Adds a user script to be injected into pages.
/// Returns false if the webview has been destroyed.
pub fn add_user_script(&self, javascript: &str, injection_time: WebViewUserScriptInjectionTime) -> bool {
let ptr = self.webview_ptr();
if ptr.is_null() {
return false;
}
let c_javascript = CString::new(javascript).unwrap_or_default();
unsafe { ffi::wxd_WebView_AddUserScript(ptr, c_javascript.as_ptr(), injection_time as i32) }
}
/// Removes all user scripts.
/// No-op if the webview has been destroyed.
pub fn remove_all_user_scripts(&self) {
let ptr = self.webview_ptr();
if ptr.is_null() {
return;
}
unsafe { ffi::wxd_WebView_RemoveAllUserScripts(ptr) };
}
// --- Native Backend ---
/// Returns a pointer to the native backend.
/// Returns null if the webview has been destroyed.
pub fn get_native_backend(&self) -> *mut std::os::raw::c_void {
let ptr = self.webview_ptr();
if ptr.is_null() {
return std::ptr::null_mut();
}
unsafe { ffi::wxd_WebView_GetNativeBackend(ptr) }
}
/// Returns the backend name.
/// Returns empty string if the webview has been destroyed.
pub fn get_backend(&self) -> String {
let ptr = self.webview_ptr();
if ptr.is_null() {
return String::new();
}
unsafe { Self::read_string_with_retry(256, |buf, len| ffi::wxd_WebView_GetBackend(ptr, buf, len)) }
}
/// Checks if a specific WebView backend is available on the current system.
///
/// # Arguments
/// * `backend` - The backend to check.
///
/// # Returns
/// `true` if the backend is available and can be used, `false` otherwise.
///
/// # Example
/// ```no_run
/// use wxdragon::widgets::{WebView, WebViewBackend};
///
/// if WebView::is_backend_available(WebViewBackend::Edge) {
/// println!("Edge backend is available!");
/// }
/// ```
pub fn is_backend_available(backend: WebViewBackend) -> bool {
let c_backend = CString::new(backend.as_str()).unwrap_or_default();
unsafe { ffi::wxd_WebView_IsBackendAvailable(c_backend.as_ptr()) }
}
/// Returns the underlying WindowHandle for this webview.
pub fn window_handle(&self) -> WindowHandle {
self.handle
}
}
// Implement WebViewEvents trait for WebView
#[cfg(feature = "webview")]
use crate::event::WebViewEvents;
#[cfg(feature = "webview")]
impl WebViewEvents for WebView {}
// Manual WxWidget implementation for WebView (using WindowHandle)
impl WxWidget for WebView {
fn handle_ptr(&self) -> *mut ffi::wxd_Window_t {
self.handle.get_ptr().unwrap_or(std::ptr::null_mut())
}
fn is_valid(&self) -> bool {
self.handle.is_valid()
}
}
// Note: We don't implement Deref to Window because returning a reference
// to a temporary Window is unsound. Users can access window methods through
// the WxWidget trait methods directly.
// Implement WxEvtHandler for event binding
impl WxEvtHandler for WebView {
unsafe fn get_event_handler_ptr(&self) -> *mut ffi::wxd_EvtHandler_t {
self.handle.get_ptr().unwrap_or(std::ptr::null_mut()) as *mut ffi::wxd_EvtHandler_t
}
}
// Implement common event traits that all Window-based widgets support
impl crate::event::WindowEvents for WebView {}
// Use the widget_builder macro to generate the WebViewBuilder implementation
widget_builder!(
name: WebView,
parent_type: &'a dyn WxWidget,
style_type: WebViewStyle,
fields: {
url: Option<String> = None,
name: String = "webView".to_string(),
backend: WebViewBackend = WebViewBackend::Default
},
build_impl: |slf| {
let parent_ptr = slf.parent.handle_ptr();
WebView::new_impl(
parent_ptr,
slf.id,
slf.url.as_deref(),
slf.pos,
slf.size,
slf.style.bits(),
Some(slf.name.as_str()),
Some(slf.backend.as_str()),
)
}
);
// XRC Support - enables WebView to be created from XRC-managed pointers
#[cfg(feature = "xrc")]
impl crate::xrc::XrcSupport for WebView {
unsafe fn from_xrc_ptr(ptr: *mut ffi::wxd_Window_t) -> Self {
WebView {
handle: WindowHandle::new(ptr),
}
}
}
// Note: WebView doesn't have XRC support in wxWidgets, so we don't provide it either
// Users should create WebView programmatically using the builder pattern
// Define the WebViewStyle enum using the widget_style_enum macro
widget_style_enum!(
name: WebViewStyle,
doc: "Style flags for `WebView`.",
variants: {
Default: 0, "Default style."
},
default_variant: Default
);
// Enable widget casting for WebView
impl crate::window::FromWindowWithClassName for WebView {
fn class_name() -> &'static str {
"wxWebView"
}
unsafe fn from_ptr(ptr: *mut ffi::wxd_Window_t) -> Self {
WebView {
handle: WindowHandle::new(ptr),
}
}
}