Skip to content

Commit 367b72c

Browse files
committed
refactor: make Application::window non-nullable
1 parent 1750e6a commit 367b72c

File tree

2 files changed

+14
-27
lines changed

2 files changed

+14
-27
lines changed

src/application.rs

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,9 @@ impl Application {
105105
})
106106
}
107107

108-
pub fn window(&self) -> Option<Window> {
109-
self.active_window().and_downcast().or_else(|| {
110-
self.windows()
111-
.into_iter()
112-
.find_map(|w| w.downcast::<Window>().ok())
113-
})
108+
pub fn window(&self) -> Window {
109+
self.active_window()
110+
.map_or_else(|| Window::new(self), |w| w.downcast().unwrap())
114111
}
115112

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

133130
pub fn present_preferences_dialog(&self) {
134-
if let Some(window) = self.window() {
135-
let dialog = PreferencesDialog::new(self.settings());
136-
dialog.present(&window);
137-
} else {
138-
tracing::warn!("Can't present preferences dialog without an active window");
139-
}
131+
let dialog = PreferencesDialog::new(self.settings());
132+
dialog.present(&self.window());
140133
}
141134

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

158151
async fn quit_request(&self) -> glib::Propagation {
159-
if let Some(window) = self.window() {
152+
if let Some(window) = self.active_window() {
153+
let window = window.downcast::<Window>().unwrap();
154+
160155
if window.is_busy() {
161156
return window.run_quit_confirmation_dialog().await;
162157
}
@@ -166,18 +161,14 @@ impl Application {
166161
}
167162

168163
async fn try_show_uri(&self, uri: &str) {
164+
let window = self.window();
169165
if let Err(err) = gtk::FileLauncher::new(Some(&gio::File::for_uri(uri)))
170-
.launch_future(self.window().as_ref())
166+
.launch_future(Some(&window))
171167
.await
172168
{
173169
if !err.matches(gio::IOErrorEnum::Cancelled) {
174170
tracing::error!("Failed to launch default for uri `{}`: {:?}", uri, err);
175-
176-
if let Some(window) = self.window() {
177-
window.present_error_dialog(&err.into());
178-
} else {
179-
tracing::error!("No window to present error");
180-
}
171+
window.present_error_dialog(&err.into());
181172
}
182173
}
183174
}
@@ -203,7 +194,7 @@ impl Application {
203194

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

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

src/recording.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ impl Recording {
212212
// select area
213213
if settings.capture_mode() == CaptureMode::Selection {
214214
let data =
215-
AreaSelector::present(Application::get().window().as_ref(), fd, response.streams())
215+
AreaSelector::present(Some(&Application::get().window()), fd, response.streams())
216216
.await?;
217217
pipeline_builder.select_area_data(data);
218218
}

0 commit comments

Comments
 (0)