-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
import
of config files and introduce color theme (#58)
* refactor: generalize CachedLayouts into CachedData Also fixed the immediate banner redrawing after updating layout. * feat!: move color definition into `theme` * refactor!: move `font_size` from `general` to TextProperty * fix: remove annoying messages about non-existent SVG by check * feat: introduce config importing using `use` keyword in TOML * refactor: move theme's definition to any place in config file * docs: add new section about `themes` and `imports`, reword sentences * docs: update information about moved font_size * docs: fix <hr> tag * docs: fix links and paragraph * docs: use brand name instead of 'Our'
- Loading branch information
Showing
20 changed files
with
1,014 additions
and
641 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,102 +1,58 @@ | ||
use std::{ | ||
collections::HashMap, | ||
path::{Path, PathBuf}, | ||
}; | ||
use std::path::{Path, PathBuf}; | ||
|
||
use log::{error, warn}; | ||
use log::warn; | ||
use render::widget::Widget; | ||
use shared::file_watcher::{FileState, FilesWatcher}; | ||
use shared::{ | ||
cached_data::{CacheUpdate, CachedValueError}, | ||
file_watcher::{FileState, FilesWatcher}, | ||
}; | ||
|
||
pub(super) struct CachedLayouts { | ||
layouts: HashMap<PathBuf, CachedLayout>, | ||
pub(super) struct CachedLayout { | ||
watcher: FilesWatcher, | ||
layout: Option<Widget>, | ||
} | ||
|
||
impl CachedLayouts { | ||
pub(super) fn get(&self, path: &PathBuf) -> Option<&CachedLayout> { | ||
self.layouts.get(path) | ||
} | ||
|
||
pub(super) fn update(&mut self) { | ||
self.layouts | ||
.values_mut() | ||
.for_each(|layout| match layout.check_updates() { | ||
FileState::Updated => layout.update(), | ||
FileState::NothingChanged | FileState::NotFound => (), | ||
}) | ||
impl CachedLayout { | ||
pub(super) fn layout(&self) -> Option<&Widget> { | ||
self.layout.as_ref() | ||
} | ||
|
||
pub(super) fn extend_by_paths(&mut self, paths: Vec<&PathBuf>) { | ||
self.layouts.retain(|path, _| paths.contains(&path)); | ||
|
||
for path_buf in paths { | ||
if self.layouts.contains_key(path_buf) { | ||
continue; | ||
} | ||
|
||
if let Some(cached_layout) = CachedLayout::from_path(path_buf) { | ||
self.layouts.insert(path_buf.to_owned(), cached_layout); | ||
fn load_layout(path: &Path) -> Option<Widget> { | ||
match filetype::parse_layout(path) { | ||
Ok(widget) => Some(widget), | ||
Err(err) => { | ||
warn!("The layout by path {path:?} is not valid. Error: {err}"); | ||
None | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl<'a> FromIterator<&'a PathBuf> for CachedLayouts { | ||
fn from_iter<T: IntoIterator<Item = &'a PathBuf>>(iter: T) -> Self { | ||
let layouts = iter | ||
.into_iter() | ||
.filter_map(|path_buf| { | ||
CachedLayout::from_path(path_buf).map(|cached_layout| (path_buf, cached_layout)) | ||
}) | ||
.fold(HashMap::new(), |mut acc, (path_buf, cached_layout)| { | ||
acc.insert(path_buf.to_owned(), cached_layout); | ||
acc | ||
}); | ||
impl CacheUpdate for CachedLayout { | ||
fn check_updates(&mut self) -> FileState { | ||
self.watcher.check_updates() | ||
} | ||
|
||
Self { layouts } | ||
fn update(&mut self) { | ||
self.layout = self.watcher.get_watching_path().and_then(Self::load_layout) | ||
} | ||
} | ||
|
||
pub(super) struct CachedLayout { | ||
watcher: FilesWatcher, | ||
layout: Option<Widget>, | ||
} | ||
impl<'a> TryFrom<&'a PathBuf> for CachedLayout { | ||
type Error = CachedValueError; | ||
|
||
impl CachedLayout { | ||
fn from_path(path_buf: &PathBuf) -> Option<Self> { | ||
fn try_from(path_buf: &'a PathBuf) -> Result<Self, Self::Error> { | ||
let watcher = match FilesWatcher::init(vec![path_buf]) { | ||
Ok(watcher) => watcher, | ||
Err(err) => { | ||
error!("Failed to init watcher for file {path_buf:?}. Error: {err}"); | ||
return None; | ||
return Err(CachedValueError::FailedInitWatcher { source: err }); | ||
} | ||
}; | ||
|
||
let layout = watcher | ||
.get_watching_path() | ||
.and_then(CachedLayout::load_layout); | ||
|
||
Some(CachedLayout { watcher, layout }) | ||
} | ||
|
||
pub(super) fn layout(&self) -> Option<&Widget> { | ||
self.layout.as_ref() | ||
} | ||
|
||
fn check_updates(&mut self) -> FileState { | ||
self.watcher.check_updates() | ||
} | ||
|
||
fn update(&mut self) { | ||
self.layout = self.watcher.get_watching_path().and_then(Self::load_layout) | ||
} | ||
|
||
fn load_layout(path: &Path) -> Option<Widget> { | ||
match filetype::parse_layout(path) { | ||
Ok(widget) => Some(widget), | ||
Err(err) => { | ||
warn!("The layout by path {path:?} is not valid. Error: {err}"); | ||
None | ||
} | ||
} | ||
Ok(CachedLayout { watcher, layout }) | ||
} | ||
} |
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
Oops, something went wrong.