Skip to content

Commit 3548b9b

Browse files
committed
auto select first container loaded
1 parent 278a887 commit 3548b9b

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

src/store/root_store.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@ use glib::subclass::prelude::*;
66
use glib::Properties;
77
use gtk::prelude::*;
88
use gtk::{gio, glib};
9+
use std::cell::OnceCell;
910
use std::cell::RefCell;
1011
use std::path::Path;
1112
use std::time::Duration;
1213
use tracing::debug;
1314
use tracing::error;
1415
use tracing::info;
15-
use std::cell::OnceCell;
1616

1717
use crate::container::Container;
18-
use crate::fakers::{Child, Command, CommandRunner, FdMode};
1918
use crate::distrobox;
2019
use crate::distrobox::CreateArgs;
2120
use crate::distrobox::Distrobox;
2221
use crate::distrobox::Status;
2322
use crate::distrobox_task::DistroboxTask;
23+
use crate::fakers::{Child, Command, CommandRunner, FdMode};
2424
use crate::gtk_utils::reconcile_list_by_key;
2525
use crate::remote_resource::RemoteResource;
2626
use crate::supported_terminals::{Terminal, TerminalRepository};
@@ -66,7 +66,9 @@ mod imp {
6666
Self {
6767
containers: gio::ListStore::new::<crate::container::Container>(),
6868
command_runner: OnceCell::new(),
69-
terminal_repository: RefCell::new(TerminalRepository::new(CommandRunner::new_null())),
69+
terminal_repository: RefCell::new(TerminalRepository::new(
70+
CommandRunner::new_null(),
71+
)),
7072
selected_container: Default::default(),
7173
current_view: Default::default(),
7274
current_dialog: Default::default(),
@@ -173,6 +175,7 @@ impl RootStore {
173175
glib::MainContext::ref_thread_default().spawn_local_with_priority(
174176
glib::Priority::LOW,
175177
async move {
178+
let previous_selected = this.selected_container().clone();
176179
let Ok(containers) = this.distrobox().list().await else {
177180
return;
178181
};
@@ -186,6 +189,12 @@ impl RootStore {
186189
|item| item.name(),
187190
&["name", "status-tag", "status-detail", "distro", "image"],
188191
);
192+
if previous_selected.is_none() {
193+
if let Some(first) = containers.first() {
194+
let container: &Container = first.downcast_ref().unwrap();
195+
this.set_selected_container(Some(container.clone()));
196+
}
197+
}
189198
},
190199
);
191200
}
@@ -389,25 +398,28 @@ impl RootStore {
389398
);
390399
cmd.stderr = FdMode::Pipe;
391400
cmd.stdout = FdMode::Pipe;
392-
let output = self.command_runner()
401+
let output = self
402+
.command_runner()
393403
.output(cmd)
394404
.await
395405
.map_err(|e| distrobox::Error::ResolveHostPath(e.to_string()));
396406

397-
398407
let is_from_sandbox = path.starts_with("/run/user");
399408

400409
// If the path is not from a flatpak sandbox, we assume it's a regular path, so we can skip the getfattr command error.
401410
// If the command was successful, but for some reason the output is empty, we also return the path as is.
402-
let stdout = if (output.is_err() && !is_from_sandbox) || output.as_ref().map_or(false, |o| o.stdout.is_empty()) {
411+
let stdout = if (output.is_err() && !is_from_sandbox)
412+
|| output.as_ref().map_or(false, |o| o.stdout.is_empty())
413+
{
403414
return Ok(path.to_string());
404415
} else {
405416
output?.stdout
406417
};
407418

408419
Ok(String::from_utf8(stdout)
409420
.map_err(|e| distrobox::Error::ParseOutput(e.to_string()))?
410-
.trim().to_string())
421+
.trim()
422+
.to_string())
411423
}
412424
}
413425

0 commit comments

Comments
 (0)