Skip to content

Commit af7b303

Browse files
committed
Add should_close flag to close window after button action
1 parent 0091d96 commit af7b303

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

src/floating_window/app.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub struct VerificationCodeApp {
1717
source: String,
1818
created_at: Instant,
1919
lifetime: Duration,
20+
should_close: bool,
2021
}
2122

2223
impl VerificationCodeApp {
@@ -26,6 +27,7 @@ impl VerificationCodeApp {
2627
source,
2728
created_at: Instant::now(),
2829
lifetime: Duration::from_secs(600),
30+
should_close: false,
2931
}
3032
}
3133

@@ -137,7 +139,7 @@ impl VerificationCodeApp {
137139
}
138140
}
139141

140-
fn draw_content(&self, ui: &mut egui::Ui, ctx: &egui::Context) {
142+
fn draw_content(&mut self, ui: &mut egui::Ui, _ctx: &egui::Context) {
141143
let content_area = ui.max_rect().translate(CONTENT_OFFSET);
142144
let mut content_ui = ui.child_ui(
143145
content_area,
@@ -152,12 +154,11 @@ impl VerificationCodeApp {
152154

153155
let btn_response = self.custom_button(
154156
&mut content_ui,
155-
format!(
157+
&format!(
156158
"{}\n{}",
157159
t!("floating_window.code", code = self.code),
158160
t!("floating_window.from", source = self.source)
159-
)
160-
.as_str(),
161+
),
161162
);
162163

163164
if btn_response.clicked() {
@@ -175,6 +176,9 @@ impl VerificationCodeApp {
175176
}
176177
}
177178
}
179+
180+
// 执行完动作后标记窗口需要关闭
181+
self.should_close = true;
178182
}
179183
}
180184

@@ -239,7 +243,7 @@ impl VerificationCodeApp {
239243

240244
impl App for VerificationCodeApp {
241245
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
242-
if self.created_at.elapsed() > self.lifetime {
246+
if self.should_close || self.created_at.elapsed() > self.lifetime {
243247
let ctx_clone = ctx.clone();
244248
std::thread::spawn(move || {
245249
ctx_clone.send_viewport_cmd(egui::ViewportCommand::Close);

0 commit comments

Comments
 (0)