Skip to content

Commit

Permalink
refactor: cleaner implementation of app settings
Browse files Browse the repository at this point in the history
  • Loading branch information
edfloreshz committed May 8, 2024
1 parent de19fc4 commit 9b29b91
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 110 deletions.
29 changes: 14 additions & 15 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ mod key_bind;
pub mod localize;
pub mod markdown;
pub mod menu;
pub mod settings;

pub struct App {
core: Core,
nav_model: segmented_button::SingleSelectModel,
content: Content,
details: Details,
config_handler: Option<cosmic_config::Config>,
config: config::Config,
config: config::CosmicTasksConfig,
app_themes: Vec<String>,
context_page: ContextPage,
key_binds: HashMap<KeyBind, Action>,
Expand Down Expand Up @@ -109,7 +110,7 @@ pub enum DialogPage {
#[derive(Clone, Debug)]
pub struct Flags {
pub config_handler: Option<cosmic_config::Config>,
pub config: config::Config,
pub config: config::CosmicTasksConfig,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
Expand Down Expand Up @@ -222,7 +223,8 @@ impl App {
.insert()
.text(format!(
"{} {}",
list.icon.clone()
list.icon
.clone()
.unwrap_or(emojis::get_by_shortcode("pencil").unwrap().to_string()),
list.name.clone()
))
Expand Down Expand Up @@ -351,12 +353,10 @@ impl Application for App {
.width(spacing.space_l)
.height(spacing.space_l)
.align_y(Vertical::Center)
.align_x(Horizontal::Center)
.align_x(Horizontal::Center),
)
.on_press(Message::DialogUpdate(DialogPage::Icon(
emoji.to_string(),
)))
.into()
.on_press(Message::DialogUpdate(DialogPage::Icon(emoji.to_string())))
.into()
})
.collect();
let mut dialog = widget::dialog(fl!("icon-select"))
Expand All @@ -371,13 +371,15 @@ impl Application for App {
.control(
widget::container(scrollable(widget::row::with_children(vec![
widget::flex_row(icon_buttons).into(),
horizontal_space(Length::Fixed(spacing.space_s as f32)).into()
horizontal_space(Length::Fixed(spacing.space_s as f32)).into(),
])))
.height(Length::Fixed(300.0)),
.height(Length::Fixed(300.0)),
);

if !icon.is_empty() {
dialog = dialog.icon(widget::container(widget::text(icon.as_str()).size(spacing.space_l)));
dialog = dialog.icon(widget::container(
widget::text(icon.as_str()).size(spacing.space_l),
));
}

dialog
Expand Down Expand Up @@ -826,10 +828,7 @@ impl Application for App {
if let Some(list) = self.nav_model.active_data::<List>() {
let entity = self.nav_model.active();
let title = format!("{} {}", icon.clone(), list.name.clone());
self.nav_model.text_set(
entity,
title,
);
self.nav_model.text_set(entity, title);
}
if let Some(list) = self.nav_model.active_data_mut::<List>() {
list.icon = Some(icon);
Expand Down
99 changes: 24 additions & 75 deletions src/app/config.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,38 @@
use crate::app;
use crate::app::icon_cache::{IconCache, ICON_CACHE};
use crate::app::{App, Flags};
use cosmic::app::Settings;
use cosmic::iced::{Limits, Size};
use cosmic::widget::icon;
use crate::app::App;
use cosmic::{
cosmic_config::{self, cosmic_config_derive::CosmicConfigEntry, CosmicConfigEntry},
cosmic_config::{self, cosmic_config_derive::CosmicConfigEntry, Config, CosmicConfigEntry},
theme, Application,
};
use done_core::service::Services;
use serde::{Deserialize, Serialize};
use std::sync::Mutex;

pub const CONFIG_VERSION: u64 = 1;

#[derive(Clone, Default, Debug, Eq, PartialEq, Deserialize, Serialize, CosmicConfigEntry)]
pub struct Config {
pub struct CosmicTasksConfig {
pub app_theme: AppTheme,
}

impl CosmicTasksConfig {
pub fn config_handler() -> Option<Config> {
Config::new(App::APP_ID, CONFIG_VERSION).ok()
}

pub fn config() -> CosmicTasksConfig {
match Self::config_handler() {
Some(config_handler) => {
let config = CosmicTasksConfig::get_entry(&config_handler).unwrap_or_else(
|(errs, config)| {
log::info!("errors loading config: {:?}", errs);
config
},
);
config
}
None => CosmicTasksConfig::default(),
}
}
}

#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
pub enum AppTheme {
Dark,
Expand All @@ -36,68 +50,3 @@ impl AppTheme {
}
}
}

pub fn get_icon(name: &'static str, size: u16) -> icon::Icon {
let mut icon_cache = ICON_CACHE.get().unwrap().lock().unwrap();
icon_cache.get(name, size)
}

pub fn config() -> (Settings, Flags) {
logger();
app::localize::localize();
icons();
services();
let settings = settings();
let flags = flags();
(settings, flags)
}

pub fn flags() -> Flags {
let (config_handler, config) = get_config();

let flags = Flags {
config_handler,
config,
};
flags
}

pub fn settings() -> Settings {
let (_, config) = get_config();

let mut settings = Settings::default();
settings = settings.theme(config.app_theme.theme());
settings = settings.size_limits(Limits::NONE.min_width(400.0).min_height(180.0));
settings = settings.size(Size::new(800.0, 800.0));
settings = settings.debug(false);
settings
}

pub fn get_config() -> (Option<cosmic_config::Config>, Config) {
let (config_handler, config) = match cosmic_config::Config::new(App::APP_ID, CONFIG_VERSION) {
Ok(config_handler) => {
let config = Config::get_entry(&config_handler).unwrap_or_else(|(errs, config)| {
log::info!("errors loading config: {:?}", errs);
config
});
(Some(config_handler), config)
}
Err(err) => {
log::error!("failed to create config handler: {}", err);
(None, Config::default())
}
};
(config_handler, config)
}

pub fn logger() {
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("warn")).init();
}

pub fn icons() {
ICON_CACHE.get_or_init(|| Mutex::new(IconCache::new()));
}

pub fn services() {
Services::init(App::APP_ID);
}
7 changes: 6 additions & 1 deletion src/app/icon_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,17 @@ impl IconCache {
Self { cache }
}

pub fn get(&mut self, name: &'static str, size: u16) -> icon::Icon {
fn get_icon(&mut self, name: &'static str, size: u16) -> icon::Icon {
let handle = self
.cache
.entry(IconCacheKey { name, size })
.or_insert_with(|| icon::from_name(name).size(size).handle())
.clone();
icon::icon(handle).size(size)
}

pub fn get(name: &'static str, size: u16) -> icon::Icon {
let mut icon_cache = ICON_CACHE.get().unwrap().lock().unwrap();
icon_cache.get_icon(name, size)
}
}
2 changes: 1 addition & 1 deletion src/app/localize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub fn localizer() -> Box<dyn Localizer> {
Box::from(DefaultLocalizer::new(&*LANGUAGE_LOADER, &Localizations))
}

pub fn localize() {
pub fn set_localization() {
let localizer = localizer();
let requested_languages = i18n_embed::DesktopLanguageRequester::requested_languages();

Expand Down
56 changes: 56 additions & 0 deletions src/app/settings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use crate::app::icon_cache::{IconCache, ICON_CACHE};
use crate::app::{App, Flags};
use cosmic::app::Settings;
use cosmic::iced::{Limits, Size};
use cosmic::Application;
use done_core::service::Services;
use std::sync::Mutex;

use super::config::CosmicTasksConfig;
use super::localize::set_localization;

pub fn init() -> (Settings, Flags) {
set_localization();
set_icon_cache();
set_logger();
start_services();
let settings = get_app_settings();
let flags = get_flags();
(settings, flags)
}

pub fn get_app_settings() -> Settings {
let config = CosmicTasksConfig::config();

let mut settings = Settings::default();
settings = settings.theme(config.app_theme.theme());
settings = settings.size_limits(Limits::NONE.min_width(400.0).min_height(180.0));
settings = settings.size(Size::new(800.0, 800.0));
settings = settings.debug(false);
settings
}

pub fn set_logger() {
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("warn")).init();
}

pub fn set_icon_cache() {
ICON_CACHE.get_or_init(|| Mutex::new(IconCache::new()));
}

pub fn start_services() {
Services::init(App::APP_ID);
}

pub fn get_flags() -> Flags {
let (config_handler, config) = (
CosmicTasksConfig::config_handler(),
CosmicTasksConfig::config(),
);

let flags = Flags {
config_handler,
config,
};
flags
}
14 changes: 7 additions & 7 deletions src/content.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::app::config;
use crate::app::icon_cache::IconCache;
use cosmic::iced::alignment::{Horizontal, Vertical};
use cosmic::iced::{Alignment, Length, Subscription};
use cosmic::iced_widget::row;
Expand Down Expand Up @@ -59,7 +59,7 @@ impl Content {

fn list_header<'a>(&'a self, list: &'a List) -> Element<'a, Message> {
let spacing = theme::active().cosmic().spacing;
let export_button = widget::button(config::get_icon("share-symbolic", 18))
let export_button = widget::button(IconCache::get("share-symbolic", 18))
.style(theme::Button::Suggested)
.padding(spacing.space_xxs)
.on_press(Message::Export(self.tasks.values().cloned().collect()));
Expand Down Expand Up @@ -99,12 +99,12 @@ impl Content {
Message::Complete(id, value)
});

let delete_button = widget::button(config::get_icon("user-trash-full-symbolic", 18))
let delete_button = widget::button(IconCache::get("user-trash-full-symbolic", 18))
.padding(space_xxs)
.style(theme::Button::Destructive)
.on_press(Message::Delete(id));

let details_button = widget::button(config::get_icon("info-outline-symbolic", 18))
let details_button = widget::button(IconCache::get("info-outline-symbolic", 18))
.padding(space_xxs)
.style(theme::Button::Standard)
.on_press(Message::Select(item.clone()));
Expand Down Expand Up @@ -155,7 +155,7 @@ impl Content {

let container = widget::container(
widget::column::with_children(vec![
config::get_icon("task-past-due-symbolic", 56).into(),
IconCache::get("task-past-due-symbolic", 56).into(),
widget::text::title1(fl!("no-tasks")).into(),
widget::text(fl!("no-tasks-suggestion")).into(),
])
Expand Down Expand Up @@ -183,7 +183,7 @@ impl Content {
.on_submit(Message::AddTask)
.width(Length::Fill)
.into(),
widget::button(config::get_icon("mail-send-symbolic", 18))
widget::button(IconCache::get("mail-send-symbolic", 18))
.padding(space_xxs)
.style(theme::Button::Suggested)
.on_press(Message::AddTask)
Expand Down Expand Up @@ -283,7 +283,7 @@ impl Content {
let Some(ref list) = self.list else {
return widget::container(
widget::column::with_children(vec![
config::get_icon("applications-office-symbolic", 56).into(),
IconCache::get("applications-office-symbolic", 56).into(),
widget::text::title1(fl!("no-list-selected")).into(),
widget::text(fl!("no-list-suggestion")).into(),
])
Expand Down
19 changes: 9 additions & 10 deletions src/details.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::app::config;
use crate::app::icon_cache::IconCache;
use chrono::{NaiveDate, TimeZone, Utc};
use cosmic::iced::{Alignment, Length};
use cosmic::iced_widget::row;
Expand Down Expand Up @@ -51,17 +51,17 @@ impl Details {
let priority_model = segmented_button::ModelBuilder::default()
.insert(|entity| {
entity
.icon(config::get_icon("flag-outline-thin-symbolic", 16))
.icon(IconCache::get("flag-outline-thin-symbolic", 16))
.data(Priority::Low)
})
.insert(|entity| {
entity
.icon(config::get_icon("flag-outline-thick-symbolic", 16))
.icon(IconCache::get("flag-outline-thick-symbolic", 16))
.data(Priority::Normal)
})
.insert(|entity| {
entity
.icon(config::get_icon("flag-filled-symbolic", 16))
.icon(IconCache::get("flag-filled-symbolic", 16))
.data(Priority::High)
})
.build();
Expand Down Expand Up @@ -200,11 +200,10 @@ impl Details {
.on_input(move |title| Message::SetSubTaskTitle(id, title))
.on_submit(Message::SubTaskEditDone);

let delete_button =
widget::button(config::get_icon("user-trash-full-symbolic", 18))
.padding(space_xxs)
.style(widget::button::Style::Destructive)
.on_press(Message::DeleteSubTask(id));
let delete_button = widget::button(IconCache::get("user-trash-full-symbolic", 18))
.padding(space_xxs)
.style(widget::button::Style::Destructive)
.on_press(Message::DeleteSubTask(id));

let row = widget::row::with_capacity(3)
.align_items(Alignment::Center)
Expand Down Expand Up @@ -300,7 +299,7 @@ impl Details {
.on_submit(Message::AddTask)
.width(Length::Fill)
.into(),
widget::button(config::get_icon("mail-send-symbolic", 18))
widget::button(IconCache::get("mail-send-symbolic", 18))
.padding(space_xxs)
.on_press(Message::AddTask)
.into(),
Expand Down
Loading

0 comments on commit 9b29b91

Please sign in to comment.