Skip to content

Commit 75c21b1

Browse files
committed
allow closing of windows
1 parent 95133df commit 75c21b1

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

src/main.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,14 @@ struct ChatMessage {
3939
content: String,
4040
}
4141

42-
struct WheelWindow(Vec<ChatMessage>);
42+
struct WheelWindow {
43+
open: bool,
44+
messages: Vec<ChatMessage>,
45+
}
4346

4447
impl Default for WheelWindow {
4548
fn default() -> Self {
46-
Self(vec![ChatMessage { role: User, content: String::new() }])
49+
Self { open: true, messages: vec![ChatMessage { role: User, content: String::new() }] }
4750
}
4851
}
4952

@@ -350,25 +353,23 @@ impl eframe::App for App {
350353
}
351354
});
352355

353-
let len = WHEEL_WINDOWS.lock().unwrap().len();
354-
for i in 0..len {
355-
egui::Window::new(format!("wheel {}", i)).show(ctx, |ui| {
356+
for (window_num, window) in WHEEL_WINDOWS.lock().unwrap().iter_mut().enumerate() {
357+
egui::Window::new(format!("wheel {}", window_num)).open(&mut window.open).show(ctx, |ui| {
356358
ScrollArea::vertical().stick_to_bottom(true).auto_shrink([false, false]).show(ui, |ui| {
357359
if ui.button("copy all to clipboard").clicked() {
358360
let mut text = "\n".to_string();
359361

360-
for entry in WHEEL_WINDOWS.lock().unwrap().get(i).unwrap().0.iter() {
362+
for entry in window.messages.iter() {
361363
text.push_str(&format!("[{}]: {}\n", entry.role, entry.content));
362364
}
363365

364366
ui.output_mut(|o| o.copied_text = text);
365367
}
366368
ui.label("[transcript goes here]");
367-
let mut wheel_windows = WHEEL_WINDOWS.lock().unwrap();
368369
let mut do_it = false;
369370
let mut do_it_j = 9999;
370-
for (j, entry) in wheel_windows.get_mut(i).unwrap().0.iter_mut().enumerate() {
371-
let id = Id::new(i * 1000 + j);
371+
for (j, entry) in window.messages.iter_mut().enumerate() {
372+
let id = Id::new(window_num * 1000 + j);
372373
let editor_has_focus = ui.ctx().memory(|m| m.has_focus(id));
373374

374375
if editor_has_focus && ui.input_mut(|i| i.consume_key(Modifiers::default(), Key::Tab)) {
@@ -451,15 +452,15 @@ impl eframe::App for App {
451452
ui.label("[command-enter to send]");
452453

453454
if do_it {
454-
let ref mut messages = wheel_windows.get_mut(i).unwrap().0;
455+
let ref mut messages = window.messages;
455456
messages.truncate(do_it_j + 1);
456457
Prompt { rowid: None, time_ms: now_ms(), prompt: messages.last().unwrap().content.clone() }
457458
.insert()
458459
.unwrap();
459460
let orig_messages = messages.clone();
460461
messages.push(ChatMessage { role: Assistant, content: String::new() });
461462
messages.push(ChatMessage { role: User, content: String::new() });
462-
ui.ctx().memory_mut(|m| m.request_focus(Id::new((i * 1000) + messages.len() - 1)));
463+
ui.ctx().memory_mut(|m| m.request_focus(Id::new((window_num * 1000) + messages.len() - 1)));
463464
let id = messages.len() - 2;
464465
let ctx_cloned = ctx.clone();
465466
let (trigger, tripwire) = Tripwire::new();
@@ -469,9 +470,9 @@ impl eframe::App for App {
469470
WHEEL_WINDOWS
470471
.lock()
471472
.unwrap()
472-
.get_mut(i)
473+
.get_mut(window_num)
473474
.unwrap()
474-
.0
475+
.messages
475476
.get_mut(id)
476477
.unwrap()
477478
.content

0 commit comments

Comments
 (0)