Skip to content

Commit b95dabf

Browse files
committed
Use local toast_overlays in dialogs
1 parent 5075cb5 commit b95dabf

File tree

2 files changed

+41
-56
lines changed

2 files changed

+41
-56
lines changed

src/preferences_dialog.rs

Lines changed: 31 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::terminal_combo_row::TerminalComboRow;
44
use adw::prelude::*;
55
use adw::subclass::prelude::*;
66
use glib::{clone, derived_properties, Properties};
7-
use gtk::{glib, gio};
7+
use gtk::{gio, glib};
88
use std::cell::RefCell;
99
use tracing::error;
1010

@@ -16,7 +16,6 @@ mod imp {
1616
pub struct PreferencesDialog {
1717
#[property(get, set, construct)]
1818
pub root_store: RefCell<RootStore>,
19-
2019
pub terminal_combo_row: RefCell<Option<TerminalComboRow>>,
2120
pub delete_btn: gtk::Button,
2221
pub add_terminal_btn: gtk::Button,
@@ -38,19 +37,20 @@ mod imp {
3837
fn constructed(&self) {
3938
self.parent_constructed();
4039
let obj = self.obj();
41-
40+
4241
obj.set_title("Preferences");
4342

4443
let page = adw::PreferencesPage::new();
4544

4645
// Terminal Settings Group
4746
let terminal_group = adw::PreferencesGroup::new();
4847
terminal_group.set_title("Terminal Settings");
49-
48+
5049
// Initialize terminal combo row
5150
let terminal_combo_row = TerminalComboRow::new_with_params(obj.root_store());
52-
self.terminal_combo_row.replace(Some(terminal_combo_row.clone()));
53-
51+
self.terminal_combo_row
52+
.replace(Some(terminal_combo_row.clone()));
53+
5454
// Initialize delete button
5555
self.delete_btn.set_label("Delete");
5656
self.delete_btn.add_css_class("destructive-action");
@@ -148,7 +148,7 @@ impl PreferencesDialog {
148148
let imp = self.imp();
149149
if let (Some(terminal_combo_row), Some(delete_btn)) = (
150150
imp.terminal_combo_row.borrow().as_ref(),
151-
Some(&imp.delete_btn)
151+
Some(&imp.delete_btn),
152152
) {
153153
if let Some(selected) = terminal_combo_row.selected_item() {
154154
let selected_name = selected
@@ -171,13 +171,13 @@ impl PreferencesDialog {
171171
Some(row) => row.clone(),
172172
None => return,
173173
};
174-
174+
175175
let selected = terminal_combo_row
176176
.selected_item()
177177
.and_downcast_ref::<gtk::StringObject>()
178178
.unwrap()
179179
.string();
180-
180+
181181
let dialog = adw::AlertDialog::builder()
182182
.heading("Delete this terminal?")
183183
.body(format!(
@@ -205,25 +205,24 @@ impl PreferencesDialog {
205205
.delete_terminal(&selected)
206206
{
207207
Ok(_) => {
208-
glib::MainContext::ref_thread_default().spawn_local(
209-
async move {
210-
if let Some(terminal_combo_row) = this.imp().terminal_combo_row.borrow().as_ref() {
211-
terminal_combo_row.reload_terminals();
212-
terminal_combo_row.set_selected_by_name(
213-
&this.root_store()
214-
.terminal_repository()
215-
.default_terminal()
216-
.await
217-
.map(|x| x.name)
218-
.unwrap_or_default(),
219-
);
220-
}
221-
222-
this.add_toast(adw::Toast::new(
223-
"Terminal removed successfully",
224-
));
225-
},
226-
);
208+
glib::MainContext::ref_thread_default().spawn_local(async move {
209+
if let Some(terminal_combo_row) =
210+
this.imp().terminal_combo_row.borrow().as_ref()
211+
{
212+
terminal_combo_row.reload_terminals();
213+
terminal_combo_row.set_selected_by_name(
214+
&this
215+
.root_store()
216+
.terminal_repository()
217+
.default_terminal()
218+
.await
219+
.map(|x| x.name)
220+
.unwrap_or_default(),
221+
);
222+
}
223+
224+
this.add_toast(adw::Toast::new("Terminal removed successfully"));
225+
});
227226
}
228227
Err(err) => {
229228
error!(error = %err, "Failed to delete terminal");
@@ -260,9 +259,7 @@ impl PreferencesDialog {
260259
let program_entry = adw::EntryRow::builder().title("Program Path").build();
261260

262261
// Separator argument entry
263-
let separator_entry = adw::EntryRow::builder()
264-
.title("Separator Argument")
265-
.build();
262+
let separator_entry = adw::EntryRow::builder().title("Separator Argument").build();
266263

267264
group.add(&name_entry);
268265
group.add(&program_entry);
@@ -348,7 +345,9 @@ impl PreferencesDialog {
348345
// Show success toast
349346
let toast = adw::Toast::new("Custom terminal added successfully");
350347

351-
if let Some(terminal_combo_row) = this.imp().terminal_combo_row.borrow().as_ref() {
348+
if let Some(terminal_combo_row) =
349+
this.imp().terminal_combo_row.borrow().as_ref()
350+
{
352351
terminal_combo_row.reload_terminals();
353352
terminal_combo_row.set_selected_by_name(&terminal.name);
354353
}
@@ -366,21 +365,4 @@ impl PreferencesDialog {
366365

367366
custom_dialog.present(Some(self));
368367
}
369-
370-
fn add_toast(&self, toast: adw::Toast) {
371-
// Try to find a parent window with a toast overlay
372-
let mut widget = self.parent();
373-
while let Some(parent) = widget {
374-
if let Some(window) = parent.downcast_ref::<crate::window::DistroShelfWindow>() {
375-
window.imp().toast_overlay.add_toast(toast);
376-
return;
377-
}
378-
widget = parent.parent();
379-
}
380-
381-
// Fallback: create a simple alert dialog if no toast overlay is found
382-
let alert = adw::AlertDialog::new(toast.title().as_deref(), None);
383-
alert.add_response("ok", "OK");
384-
alert.present(Some(self));
385-
}
386368
}

src/window.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,8 @@ impl DistroShelfWindow {
585585

586586
toolbar_view.add_top_bar(&header_bar);
587587

588+
let toast_overlay = adw::ToastOverlay::new();
589+
588590
// Create main content
589591
let main_box = gtk::Box::new(gtk::Orientation::Vertical, 6);
590592

@@ -651,16 +653,16 @@ impl DistroShelfWindow {
651653

652654
// Add click handler to copy command to clipboard
653655
let command_str = command.to_string();
654-
let this = self.clone();
655656
let gesture = gtk::GestureClick::new();
656657
gesture.connect_pressed(clone!(
657658
#[weak]
658-
this,
659+
toast_overlay,
659660
move |_, _, _, _| {
660661
if let Some(display) = gdk::Display::default() {
661662
let clipboard = display.clipboard();
662663
clipboard.set_text(&command_str);
663-
this.add_toast(adw::Toast::new("Command copied to clipboard"));
664+
toast_overlay
665+
.add_toast(adw::Toast::new("Command copied to clipboard"));
664666
}
665667
}
666668
));
@@ -700,16 +702,16 @@ impl DistroShelfWindow {
700702

701703
// Add click handler to copy command to clipboard
702704
let command_str = command.to_string();
703-
let this = self.clone();
704705
let gesture = gtk::GestureClick::new();
705706
gesture.connect_pressed(clone!(
706707
#[weak]
707-
this,
708+
toast_overlay,
708709
move |_, _, _, _| {
709710
if let Some(display) = gdk::Display::default() {
710711
let clipboard = display.clipboard();
711712
clipboard.set_text(&command_str);
712-
this.add_toast(adw::Toast::new("Command copied to clipboard"));
713+
toast_overlay
714+
.add_toast(adw::Toast::new("Command copied to clipboard"));
713715
}
714716
}
715717
));
@@ -755,7 +757,8 @@ impl DistroShelfWindow {
755757
scrolled_window.set_child(Some(&content_box));
756758
main_box.append(&scrolled_window);
757759

758-
toolbar_view.set_content(Some(&main_box));
760+
toast_overlay.set_child(Some(&main_box));
761+
toolbar_view.set_content(Some(&toast_overlay));
759762
dialog.set_child(Some(&toolbar_view));
760763

761764
dialog.upcast()

0 commit comments

Comments
 (0)