-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make some receivers
&mut
and remove unnecessary receivers from traits
- Loading branch information
Showing
3 changed files
with
45 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,57 @@ | ||
use crate::config::FileExtensions; | ||
use anyhow::Error; | ||
use rfd::{FileDialog, MessageDialog, MessageLevel}; | ||
use std::fmt::Write as _; | ||
use std::path::{Path, PathBuf}; | ||
|
||
pub trait Dialog: Default { | ||
fn pick_file(&self, dir: &Path, extensions: &FileExtensions) -> Option<PathBuf>; | ||
fn pick_dir(&self, dir: &Path) -> Option<PathBuf>; | ||
fn alert(&self, title: impl Into<String>, message: impl Into<String>); | ||
#[non_exhaustive] | ||
pub enum DialogMessageLevel { | ||
Error, | ||
} | ||
|
||
#[derive(Default)] | ||
pub trait Dialog { | ||
fn pick_file(dir: &Path, extensions: &FileExtensions) -> Option<PathBuf>; | ||
|
||
fn pick_dir(dir: &Path) -> Option<PathBuf>; | ||
|
||
fn message(level: DialogMessageLevel, title: impl Into<String>, body: impl Into<String>); | ||
|
||
fn alert(error: &Error) { | ||
let mut errs = error.chain(); | ||
let title = format!("Error: {}", errs.next().unwrap()); | ||
let mut message = title.clone(); | ||
for err in errs { | ||
write!(message, "\n Caused by: {}", err).unwrap(); | ||
} | ||
log::error!("{}", message); | ||
Self::message(DialogMessageLevel::Error, title, message); | ||
} | ||
} | ||
|
||
// TODO: Consider to set parent window of dialog. rfd provides `set_parent` methods to dialogs. | ||
|
||
pub struct SystemDialog; | ||
|
||
impl Dialog for SystemDialog { | ||
fn pick_file(&self, dir: &Path, extensions: &FileExtensions) -> Option<PathBuf> { | ||
fn pick_file(dir: &Path, extensions: &FileExtensions) -> Option<PathBuf> { | ||
FileDialog::new() | ||
.add_filter("Markdown", extensions.as_slice()) | ||
.set_directory(dir) | ||
.pick_file() | ||
} | ||
|
||
fn pick_dir(&self, dir: &Path) -> Option<PathBuf> { | ||
fn pick_dir(dir: &Path) -> Option<PathBuf> { | ||
FileDialog::new().set_directory(dir).pick_folder() | ||
} | ||
|
||
fn alert(&self, title: impl Into<String>, message: impl Into<String>) { | ||
fn message(level: DialogMessageLevel, title: impl Into<String>, body: impl Into<String>) { | ||
let level = match level { | ||
DialogMessageLevel::Error => MessageLevel::Error, | ||
}; | ||
MessageDialog::new() | ||
.set_level(MessageLevel::Error) | ||
.set_level(level) | ||
.set_title(title.into()) | ||
.set_description(message.into()) | ||
.set_description(body.into()) | ||
.show(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters