Store save dialog filename in DialogKind
This commit is contained in:
parent
162616f687
commit
88073536e2
4 changed files with 36 additions and 44 deletions
|
|
@ -80,8 +80,8 @@ impl Application for App {
|
||||||
Message::DialogSave => {
|
Message::DialogSave => {
|
||||||
if self.dialog_opt.is_none() {
|
if self.dialog_opt.is_none() {
|
||||||
let (dialog, command) = Dialog::new(
|
let (dialog, command) = Dialog::new(
|
||||||
DialogKind::SaveFile,
|
DialogKind::SaveFile { filename: "README.md".to_string() },
|
||||||
Some("README.md".into()),
|
None,
|
||||||
Message::DialogMessage,
|
Message::DialogMessage,
|
||||||
Message::DialogResult,
|
Message::DialogResult,
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -686,7 +686,7 @@ impl Application for App {
|
||||||
}
|
}
|
||||||
Message::NotifyWatcher(mut watcher_wrapper) => match watcher_wrapper.watcher_opt.take()
|
Message::NotifyWatcher(mut watcher_wrapper) => match watcher_wrapper.watcher_opt.take()
|
||||||
{
|
{
|
||||||
Some(mut watcher) => {
|
Some(watcher) => {
|
||||||
self.watcher_opt = Some((watcher, HashSet::new()));
|
self.watcher_opt = Some((watcher, HashSet::new()));
|
||||||
return self.update_watcher();
|
return self.update_watcher();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -33,13 +33,13 @@ pub enum DialogResult {
|
||||||
Open(Vec<PathBuf>),
|
Open(Vec<PathBuf>),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub enum DialogKind {
|
pub enum DialogKind {
|
||||||
OpenFile,
|
OpenFile,
|
||||||
OpenFolder,
|
OpenFolder,
|
||||||
OpenMultipleFiles,
|
OpenMultipleFiles,
|
||||||
OpenMultipleFolders,
|
OpenMultipleFolders,
|
||||||
SaveFile,
|
SaveFile { filename: String },
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DialogKind {
|
impl DialogKind {
|
||||||
|
|
@ -49,7 +49,7 @@ impl DialogKind {
|
||||||
Self::OpenFolder => fl!("open-folder"),
|
Self::OpenFolder => fl!("open-folder"),
|
||||||
Self::OpenMultipleFiles => fl!("open-multiple-files"),
|
Self::OpenMultipleFiles => fl!("open-multiple-files"),
|
||||||
Self::OpenMultipleFolders => fl!("open-multiple-folders"),
|
Self::OpenMultipleFolders => fl!("open-multiple-folders"),
|
||||||
Self::SaveFile => fl!("save-file"),
|
Self::SaveFile { .. } => fl!("save-file"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -58,7 +58,7 @@ impl DialogKind {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn save(&self) -> bool {
|
pub fn save(&self) -> bool {
|
||||||
matches!(self, Self::SaveFile)
|
matches!(self, Self::SaveFile { .. })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -197,7 +197,6 @@ impl PartialEq for WatcherWrapper {
|
||||||
struct App {
|
struct App {
|
||||||
core: Core,
|
core: Core,
|
||||||
flags: Flags,
|
flags: Flags,
|
||||||
filename: String,
|
|
||||||
filename_id: widget::Id,
|
filename_id: widget::Id,
|
||||||
modifiers: Modifiers,
|
modifiers: Modifiers,
|
||||||
nav_model: segmented_button::SingleSelectModel,
|
nav_model: segmented_button::SingleSelectModel,
|
||||||
|
|
@ -328,20 +327,8 @@ impl Application for App {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut filename = String::new();
|
|
||||||
let location = Location::Path(match &flags.path_opt {
|
let location = Location::Path(match &flags.path_opt {
|
||||||
Some(path) => {
|
Some(path) => path.to_path_buf(),
|
||||||
if path.is_dir() {
|
|
||||||
path.to_path_buf()
|
|
||||||
} else if let Some(parent) = path.parent() {
|
|
||||||
if let Some(filename_os) = path.file_name() {
|
|
||||||
filename = filename_os.to_str().unwrap_or_default().to_string();
|
|
||||||
}
|
|
||||||
parent.to_path_buf()
|
|
||||||
} else {
|
|
||||||
path.to_path_buf()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None => match env::current_dir() {
|
None => match env::current_dir() {
|
||||||
Ok(path) => path,
|
Ok(path) => path,
|
||||||
Err(_) => home_dir(),
|
Err(_) => home_dir(),
|
||||||
|
|
@ -349,12 +336,11 @@ impl Application for App {
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut tab = Tab::new(location, TabConfig::default());
|
let mut tab = Tab::new(location, TabConfig::default());
|
||||||
tab.dialog = Some(flags.kind);
|
tab.dialog = Some(flags.kind.clone());
|
||||||
|
|
||||||
let mut app = App {
|
let mut app = App {
|
||||||
core,
|
core,
|
||||||
flags,
|
flags,
|
||||||
filename,
|
|
||||||
filename_id: widget::Id::unique(),
|
filename_id: widget::Id::unique(),
|
||||||
modifiers: Modifiers::empty(),
|
modifiers: Modifiers::empty(),
|
||||||
nav_model: nav_model.build(),
|
nav_model: nav_model.build(),
|
||||||
|
|
@ -417,13 +403,15 @@ impl Application for App {
|
||||||
self.result_opt = Some(DialogResult::Cancel);
|
self.result_opt = Some(DialogResult::Cancel);
|
||||||
return window::close(self.main_window_id());
|
return window::close(self.main_window_id());
|
||||||
}
|
}
|
||||||
Message::Filename(filename) => {
|
Message::Filename(new_filename) => {
|
||||||
self.filename = filename;
|
if let DialogKind::SaveFile { filename } = &mut self.flags.kind {
|
||||||
|
*filename = new_filename.clone();
|
||||||
|
}
|
||||||
|
|
||||||
// Select based on filename
|
// Select based on filename
|
||||||
if let Some(items) = &mut self.tab.items_opt {
|
if let Some(items) = &mut self.tab.items_opt {
|
||||||
for item in items.iter_mut() {
|
for item in items.iter_mut() {
|
||||||
item.selected = item.name == self.filename;
|
item.selected = item.name == new_filename;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -471,16 +459,18 @@ impl Application for App {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Message::Save => {
|
Message::Save => {
|
||||||
if !self.filename.is_empty() {
|
if let DialogKind::SaveFile { filename } = &self.flags.kind {
|
||||||
if let Location::Path(tab_path) = &self.tab.location {
|
if !filename.is_empty() {
|
||||||
let path = tab_path.join(&self.filename);
|
if let Location::Path(tab_path) = &self.tab.location {
|
||||||
if path.exists() {
|
let path = tab_path.join(&filename);
|
||||||
//TODO: dialog or something?
|
if path.exists() {
|
||||||
log::warn!("{:?} exists", path);
|
//TODO: dialog or something?
|
||||||
|
log::warn!("{:?} exists", path);
|
||||||
|
}
|
||||||
|
self.result_opt = Some(DialogResult::Open(vec![path]));
|
||||||
|
return window::close(self.main_window_id());
|
||||||
}
|
}
|
||||||
self.result_opt = Some(DialogResult::Open(vec![path]));
|
}
|
||||||
return window::close(self.main_window_id());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Message::TabMessage(tab_message) => {
|
Message::TabMessage(tab_message) => {
|
||||||
|
|
@ -492,12 +482,12 @@ impl Application for App {
|
||||||
let updated = self.tab.update(tab_message, self.modifiers);
|
let updated = self.tab.update(tab_message, self.modifiers);
|
||||||
|
|
||||||
// Update filename box when anything is selected
|
// Update filename box when anything is selected
|
||||||
if self.flags.kind.save() {
|
if let DialogKind::SaveFile { filename } = &mut self.flags.kind {
|
||||||
if let Some(click_i) = click_i_opt {
|
if let Some(click_i) = click_i_opt {
|
||||||
if let Some(items) = &self.tab.items_opt {
|
if let Some(items) = &self.tab.items_opt {
|
||||||
if let Some(item) = items.get(click_i) {
|
if let Some(item) = items.get(click_i) {
|
||||||
if item.selected {
|
if item.selected {
|
||||||
self.filename = item.name.clone();
|
*filename = item.name.clone();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -510,8 +500,10 @@ impl Application for App {
|
||||||
}
|
}
|
||||||
Message::TabRescan(mut items) => {
|
Message::TabRescan(mut items) => {
|
||||||
// Select based on filename
|
// Select based on filename
|
||||||
for item in items.iter_mut() {
|
if let DialogKind::SaveFile { filename } = &self.flags.kind {
|
||||||
item.selected = item.name == self.filename;
|
for item in items.iter_mut() {
|
||||||
|
item.selected = &item.name == filename;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.tab.items_opt = Some(items);
|
self.tab.items_opt = Some(items);
|
||||||
|
|
@ -538,8 +530,8 @@ impl Application for App {
|
||||||
|
|
||||||
tab_column = tab_column.push(
|
tab_column = tab_column.push(
|
||||||
widget::row::with_children(vec![
|
widget::row::with_children(vec![
|
||||||
if self.flags.kind.save() {
|
if let DialogKind::SaveFile { filename } = &self.flags.kind {
|
||||||
widget::text_input("", &self.filename)
|
widget::text_input("", filename)
|
||||||
.id(self.filename_id.clone())
|
.id(self.filename_id.clone())
|
||||||
.on_input(Message::Filename)
|
.on_input(Message::Filename)
|
||||||
.on_submit(Message::Save)
|
.on_submit(Message::Save)
|
||||||
|
|
|
||||||
|
|
@ -594,7 +594,7 @@ impl Tab {
|
||||||
//TODO: prevent triple-click and beyond from opening file?
|
//TODO: prevent triple-click and beyond from opening file?
|
||||||
item.click_time = Some(Instant::now());
|
item.click_time = Some(Instant::now());
|
||||||
} else if modifiers.contains(Modifiers::CTRL)
|
} else if modifiers.contains(Modifiers::CTRL)
|
||||||
&& self.dialog.map_or(true, |x| x.multiple())
|
&& self.dialog.as_ref().map_or(true, |x| x.multiple())
|
||||||
{
|
{
|
||||||
// Holding control allows multiple selection
|
// Holding control allows multiple selection
|
||||||
item.click_time = None;
|
item.click_time = None;
|
||||||
|
|
@ -648,7 +648,7 @@ impl Tab {
|
||||||
if i == click_i {
|
if i == click_i {
|
||||||
item.selected = true;
|
item.selected = true;
|
||||||
} else if modifiers.contains(Modifiers::CTRL)
|
} else if modifiers.contains(Modifiers::CTRL)
|
||||||
&& self.dialog.map_or(true, |x| x.multiple())
|
&& self.dialog.as_ref().map_or(true, |x| x.multiple())
|
||||||
{
|
{
|
||||||
// Holding control allows multiple selection
|
// Holding control allows multiple selection
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -664,7 +664,7 @@ impl Tab {
|
||||||
self.view = view;
|
self.view = view;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if let Some(mut location) = cd {
|
if let Some(location) = cd {
|
||||||
if location != self.location {
|
if location != self.location {
|
||||||
self.location = location.clone();
|
self.location = location.clone();
|
||||||
self.items_opt = None;
|
self.items_opt = None;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue