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
use html_view::{ViewerOptions, ViewerResult, ViewerWaitMode};
use std::thread;
use std::time::Duration;
fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Starting refresh demo...");
// Create initial HTML content
let initial_html = r#"
<!DOCTYPE html>
<html>
<head>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}
.counter {
background: white;
padding: 40px;
border-radius: 20px;
box-shadow: 0 10px 40px rgba(0,0,0,0.3);
text-align: center;
}
h1 {
color: #667eea;
font-size: 48px;
margin: 0;
}
p {
color: #666;
font-size: 18px;
margin-top: 10px;
}
</style>
</head>
<body>
<div class="counter">
<h1>0</h1>
<p>Counter (refreshing every second)</p>
</div>
</body>
</html>
"#;
// Create viewer options in non-blocking mode
let mut options = ViewerOptions::inline_html(initial_html);
options.wait = ViewerWaitMode::NonBlocking;
options.window.title = Some("Refresh Demo - Live Counter".to_string());
options.window.width = Some(600);
options.window.height = Some(400);
// Open the viewer
println!("Opening viewer window...");
let result = html_view::open(options)?;
if let ViewerResult::NonBlocking(mut handle) = result {
println!("Viewer opened successfully!");
println!("The counter will update every second for 10 seconds.");
println!("You can close the window at any time.");
// Update the counter every second for 10 iterations
for i in 1..=10 {
// Wait 1 second
thread::sleep(Duration::from_secs(1));
// Check if the window is still open
if let Some(status) = handle.try_wait()? {
println!("Window closed by user: {:?}", status.reason);
break;
}
// Generate new HTML with updated counter
let updated_html = format!(
r#"
<!DOCTYPE html>
<html>
<head>
<style>
body {{
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}}
.counter {{
background: white;
padding: 40px;
border-radius: 20px;
box-shadow: 0 10px 40px rgba(0,0,0,0.3);
text-align: center;
animation: pulse 0.5s ease;
}}
@keyframes pulse {{
0% {{ transform: scale(1); }}
50% {{ transform: scale(1.05); }}
100% {{ transform: scale(1); }}
}}
h1 {{
color: #667eea;
font-size: 48px;
margin: 0;
}}
p {{
color: #666;
font-size: 18px;
margin-top: 10px;
}}
</style>
</head>
<body>
<div class="counter">
<h1>{}</h1>
<p>Counter (refreshing every second)</p>
</div>
</body>
</html>
"#,
i
);
// Refresh the viewer with new content
println!("Refreshing to counter value: {}", i);
handle.refresh_html(&updated_html)?;
}
println!("Demo complete! Closing viewer...");
thread::sleep(Duration::from_secs(1));
handle.terminate()?;
}
Ok(())
}