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(()) 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)] #[derive(Clone, Debug)]
pub struct Flags { pub struct Flags {
config_handler: Option<cosmic_config::Config>, config_handler: Option<cosmic_config::Config>,
@ -148,16 +158,6 @@ pub struct App {
} }
impl 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> { fn open_tab<P: Into<PathBuf>>(&mut self, path: P) -> Command<Message> {
let tab = Tab::new(path); let tab = Tab::new(path);
self.tab_model self.tab_model
@ -263,7 +263,7 @@ impl Application for App {
} }
if app.tab_model.iter().next().is_none() { if app.tab_model.iter().next().is_none() {
let _ = app.open_home(); let _ = app.open_tab(home_dir());
} }
let command = app.update_title(); let command = app.update_title();
@ -355,14 +355,11 @@ impl Application for App {
} }
Message::TabNew => { Message::TabNew => {
let active = self.tab_model.active(); let active = self.tab_model.active();
let path_opt = match self.tab_model.data::<Tab>(active) { let path = match self.tab_model.data::<Tab>(active) {
Some(tab) => Some(tab.path.clone()), Some(tab) => tab.path.clone(),
None => None, None => home_dir(),
}; };
match path_opt { return self.open_tab(path);
Some(path) => return self.open_tab(path),
None => return self.open_home(),
}
} }
Message::ToggleContextPage(context_page) => { Message::ToggleContextPage(context_page) => {
if self.context_page == context_page { if self.context_page == context_page {
@ -397,6 +394,10 @@ impl Application for App {
.on_press(Message::TabNew) .on_press(Message::TabNew)
.padding(space_xxs) .padding(space_xxs)
.style(style::Button::Icon), .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()) widget::button(widget::icon::from_name("go-up-symbolic").size(16).icon())
.on_press(Message::TabMessage(active, tab::Message::Parent)) .on_press(Message::TabMessage(active, tab::Message::Parent))
.padding(space_xxs) .padding(space_xxs)

View file

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