Skip to content

Commit

Permalink
make some receivers &mut and remove unnecessary receivers from traits
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd committed Nov 10, 2023
1 parent 61c8411 commit c7f04cc
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 32 deletions.
44 changes: 34 additions & 10 deletions v2/src/dialog.rs
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();
}
}
4 changes: 2 additions & 2 deletions v2/src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,6 @@ pub trait Rendering: Sized {
pub trait EventHandler {
fn handle_user_event(&mut self, event: UserEvent) -> Result<RenderingFlow>;
fn handle_menu_event(&mut self, item: MenuItem) -> Result<RenderingFlow>;
fn handle_exit(&self) -> Result<()>;
fn handle_error(&self, err: Error) -> RenderingFlow;
fn handle_exit(&mut self) -> Result<()>;
fn handle_error(&mut self, err: Error) -> RenderingFlow;
}
29 changes: 9 additions & 20 deletions v2/src/shiba.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use crate::renderer::{
use crate::watcher::{PathFilter, Watcher};
use anyhow::{Context as _, Error, Result};
use std::collections::VecDeque;
use std::fmt::Write as _;
use std::fs;
use std::marker::PhantomData;
use std::mem;
use std::path::{Path, PathBuf, MAIN_SEPARATOR};

Expand Down Expand Up @@ -189,17 +189,6 @@ impl PreviewContent {
}
}

fn alert_error<D: Dialog>(dialog: &D, 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);
dialog.alert(title, message);
}

pub struct Shiba<R: Rendering, O, W, D> {
renderer: R::Renderer,
opener: O,
Expand All @@ -208,7 +197,7 @@ pub struct Shiba<R: Rendering, O, W, D> {
config: Config,
preview: PreviewContent,
init_file: Option<PathBuf>,
dialog: D,
_dialog: PhantomData<D>,
}

impl<R, O, W, D> Shiba<R, O, W, D>
Expand All @@ -224,7 +213,7 @@ where
{
fn on_err<D: Dialog>(err: Error) -> Error {
let err = err.context("Could not launch application");
alert_error(&D::default(), &err);
D::alert(&err);
err
}
let mut rendering = R::new().map_err(on_err::<D>)?;
Expand Down Expand Up @@ -262,7 +251,7 @@ where
config,
preview: PreviewContent::default(),
init_file,
dialog: D::default(),
_dialog: PhantomData,
})
}

Expand Down Expand Up @@ -310,7 +299,7 @@ where
fn open_file(&mut self) -> Result<()> {
let extensions = self.config.watch().file_extensions();
let dir = self.config.dialog().default_dir()?;
let file = self.dialog.pick_file(&dir, extensions);
let file = D::pick_file(&dir, extensions);
#[cfg(windows)]
let file = file.and_then(|p| p.canonicalize().ok()); // Ensure \\? at the head of the path

Expand All @@ -324,7 +313,7 @@ where

fn open_dir(&mut self) -> Result<()> {
let dir = self.config.dialog().default_dir()?;
let dir = self.dialog.pick_dir(&dir);
let dir = D::pick_dir(&dir);
#[cfg(windows)]
let dir = dir.and_then(|p| p.canonicalize().ok()); // Ensure \\? at the head of the path

Expand Down Expand Up @@ -511,7 +500,7 @@ where
Ok(RenderingFlow::Continue)
}

fn handle_exit(&self) -> Result<()> {
fn handle_exit(&mut self) -> Result<()> {
log::debug!("Handling application exit");
let data_dir = self.config.data_dir();
if self.config.window().restore {
Expand All @@ -524,8 +513,8 @@ where
Ok(())
}

fn handle_error(&self, err: Error) -> RenderingFlow {
alert_error(&self.dialog, &err);
fn handle_error(&mut self, err: Error) -> RenderingFlow {
D::alert(&err);
RenderingFlow::Continue
}
}

0 comments on commit c7f04cc

Please sign in to comment.