Skip to content

Commit

Permalink
refactor: make Application::window non-nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
SeaDve committed Feb 9, 2024
1 parent 1750e6a commit 367b72c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 27 deletions.
39 changes: 13 additions & 26 deletions src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,9 @@ impl Application {
})
}

pub fn window(&self) -> Option<Window> {
self.active_window().and_downcast().or_else(|| {
self.windows()
.into_iter()
.find_map(|w| w.downcast::<Window>().ok())
})
pub fn window(&self) -> Window {
self.active_window()
.map_or_else(|| Window::new(self), |w| w.downcast().unwrap())
}

pub fn send_record_success_notification(&self, recording_file: &gio::File) {
Expand All @@ -131,12 +128,8 @@ impl Application {
}

pub fn present_preferences_dialog(&self) {
if let Some(window) = self.window() {
let dialog = PreferencesDialog::new(self.settings());
dialog.present(&window);
} else {
tracing::warn!("Can't present preferences dialog without an active window");
}
let dialog = PreferencesDialog::new(self.settings());
dialog.present(&self.window());
}

pub fn run(&self) -> glib::ExitCode {
Expand All @@ -156,7 +149,9 @@ impl Application {
}

async fn quit_request(&self) -> glib::Propagation {
if let Some(window) = self.window() {
if let Some(window) = self.active_window() {
let window = window.downcast::<Window>().unwrap();

if window.is_busy() {
return window.run_quit_confirmation_dialog().await;
}
Expand All @@ -166,18 +161,14 @@ impl Application {
}

async fn try_show_uri(&self, uri: &str) {
let window = self.window();
if let Err(err) = gtk::FileLauncher::new(Some(&gio::File::for_uri(uri)))
.launch_future(self.window().as_ref())
.launch_future(Some(&window))
.await
{
if !err.matches(gio::IOErrorEnum::Cancelled) {
tracing::error!("Failed to launch default for uri `{}`: {:?}", uri, err);

if let Some(window) = self.window() {
window.present_error_dialog(&err.into());
} else {
tracing::error!("No window to present error");
}
window.present_error_dialog(&err.into());
}
}
}
Expand All @@ -203,7 +194,7 @@ impl Application {

glib::spawn_future_local(async move {
if let Err(err) = gtk::FileLauncher::new(Some(&gio::File::for_uri(&uri)))
.open_containing_folder_future(obj.window().as_ref())
.open_containing_folder_future(Some(&obj.window()))
.await
{
tracing::warn!("Failed to show items: {:?}", err);
Expand All @@ -216,11 +207,7 @@ impl Application {

let action_show_about = gio::SimpleAction::new("show-about", None);
action_show_about.connect_activate(clone!(@weak self as obj => move |_, _| {
if let Some(window) = obj.window() {
about::present_dialog(&window);
} else {
tracing::warn!("Can't present about dialog without an active window");
}
about::present_dialog(&obj.window());
}));
self.add_action(&action_show_about);

Expand Down
2 changes: 1 addition & 1 deletion src/recording.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ impl Recording {
// select area
if settings.capture_mode() == CaptureMode::Selection {
let data =
AreaSelector::present(Application::get().window().as_ref(), fd, response.streams())
AreaSelector::present(Some(&Application::get().window()), fd, response.streams())
.await?;
pipeline_builder.select_area_data(data);
}
Expand Down

0 comments on commit 367b72c

Please sign in to comment.