Add home button, canonicalize paths

This commit is contained in:
Jeremy Soller 2024-01-03 17:04:08 -07:00
parent b0d7898cdd
commit eec07d9631
2 changed files with 31 additions and 20 deletions

View file

@ -77,6 +77,16 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}
fn home_dir() -> PathBuf {
match dirs::home_dir() {
Some(home) => home,
None => {
log::warn!("failed to locate home directory");
PathBuf::from("/")
}
}
}
#[derive(Clone, Debug)]
pub struct Flags {
config_handler: Option<cosmic_config::Config>,
@ -148,16 +158,6 @@ pub struct App {
}
impl App {
fn open_home(&mut self) -> Command<Message> {
match dirs::home_dir() {
Some(home) => self.open_tab(home),
None => {
log::warn!("failed to locate home directory");
self.open_tab("/")
}
}
}
fn open_tab<P: Into<PathBuf>>(&mut self, path: P) -> Command<Message> {
let tab = Tab::new(path);
self.tab_model
@ -263,7 +263,7 @@ impl Application for App {
}
if app.tab_model.iter().next().is_none() {
let _ = app.open_home();
let _ = app.open_tab(home_dir());
}
let command = app.update_title();
@ -355,14 +355,11 @@ impl Application for App {
}
Message::TabNew => {
let active = self.tab_model.active();
let path_opt = match self.tab_model.data::<Tab>(active) {
Some(tab) => Some(tab.path.clone()),
None => None,
let path = match self.tab_model.data::<Tab>(active) {
Some(tab) => tab.path.clone(),
None => home_dir(),
};
match path_opt {
Some(path) => return self.open_tab(path),
None => return self.open_home(),
}
return self.open_tab(path);
}
Message::ToggleContextPage(context_page) => {
if self.context_page == context_page {
@ -397,6 +394,10 @@ impl Application for App {
.on_press(Message::TabNew)
.padding(space_xxs)
.style(style::Button::Icon),
widget::button(widget::icon::from_name("go-home-symbolic").size(16).icon())
.on_press(Message::TabMessage(active, tab::Message::Home))
.padding(space_xxs)
.style(style::Button::Icon),
widget::button(widget::icon::from_name("go-up-symbolic").size(16).icon())
.on_press(Message::TabMessage(active, tab::Message::Parent))
.padding(space_xxs)

View file

@ -49,6 +49,7 @@ pub fn open_command(path: &PathBuf) -> process::Command {
#[derive(Clone, Copy, Debug)]
pub enum Message {
Click(usize),
Home,
Parent,
}
@ -69,9 +70,15 @@ pub struct Tab {
impl Tab {
pub fn new<P: Into<PathBuf>>(path: P) -> Self {
//TODO: store absolute path
let path = path.into();
let mut tab = Self {
path: path.into(),
path: match fs::canonicalize(&path) {
Ok(absolute) => absolute,
Err(err) => {
log::warn!("failed to canonicalize {:?}: {}", path, err);
path
}
},
context_menu: None,
items: Vec::new(),
};
@ -167,6 +174,9 @@ impl Tab {
}
}
}
Message::Home => {
cd = Some(crate::home_dir());
}
Message::Parent => {
if let Some(parent) = self.path.parent() {
cd = Some(parent.to_owned());