Skip to content

Commit

Permalink
improv: code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
edfloreshz committed Nov 8, 2024
1 parent 6c479a0 commit 9329f5f
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 108 deletions.
44 changes: 16 additions & 28 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ thiserror = "1.0.65"
[dependencies.libcosmic]
git = "https://github.com/pop-os/libcosmic.git"
default-features = false
features = ["multi-window", "tokio", "winit", "wgpu", "dbus-config", "desktop"]
features = ["multi-window", "tokio", "winit", "wgpu", "desktop"]

[dependencies.i18n-embed]
version = "0.14"
Expand Down
48 changes: 17 additions & 31 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use cosmic::{
app, cosmic_config, cosmic_theme, executor, theme, widget, Application, ApplicationExt, Element,
};

use crate::app::config::{AppTheme, CONFIG_VERSION};
use crate::app::config::CONFIG_VERSION;
use crate::app::key_bind::key_binds;
use crate::content::Content;
use crate::details::Details;
Expand Down Expand Up @@ -181,22 +181,17 @@ impl Tasks {
}

fn settings(&self) -> Element<Message> {
let app_theme_selected = match self.config.app_theme {
AppTheme::Dark => 1,
AppTheme::Light => 2,
AppTheme::System => 0,
};
widget::settings::view_column(vec![widget::settings::section()
widget::settings::section()
.title(fl!("appearance"))
.add(
widget::settings::item::builder(fl!("theme")).control(widget::dropdown(
.add(widget::settings::item::item(
fl!("theme"),
widget::dropdown(
&self.app_themes,
Some(app_theme_selected),
|index| Message::Application(ApplicationAction::AppTheme(index)),
)),
)
.into()])
.into()
Some(self.config.app_theme.into()),
|theme| Message::Application(ApplicationAction::AppTheme(theme)),
),
))
.into()
}

fn create_nav_item(&mut self, list: &List) -> EntityMut<SingleSelect> {
Expand Down Expand Up @@ -376,7 +371,8 @@ impl Application for Tasks {
let content_tasks = self.content.update(message);
for content_task in content_tasks {
match content_task {
content::Task::Iced(task) => return task,
content::Task::Focus(id) => tasks
.push(self.update(Message::Application(ApplicationAction::Focus(id)))),
content::Task::GetTasks(list_id) => {
tasks.push(app::Task::perform(
todo::fetch_tasks(list_id, self.service.clone()),
Expand Down Expand Up @@ -465,12 +461,8 @@ impl Application for Tasks {
DialogPage::Calendar(Local::now().date_naive()),
))));
}
details::Task::Focus(id) => {
tasks.push(
self.update(Message::Application(ApplicationAction::Focus(id))),
);
}
details::Task::Iced(task) => return task,
details::Task::Focus(id) => tasks
.push(self.update(Message::Application(ApplicationAction::Focus(id)))),
}
}
}
Expand Down Expand Up @@ -667,13 +659,8 @@ impl Application for Tasks {
eprintln!("failed to get current executable path: {err}");
}
},
ApplicationAction::AppTheme(index) => {
let app_theme = match index {
1 => AppTheme::Dark,
2 => AppTheme::Light,
_ => AppTheme::System,
};
config_set!(app_theme, app_theme);
ApplicationAction::AppTheme(theme) => {
config_set!(app_theme, theme.into());
return self.update_config();
}
ApplicationAction::SystemThemeModeChange => {
Expand Down Expand Up @@ -714,8 +701,7 @@ impl Application for Tasks {
}

fn view(&self) -> Element<Self::Message> {
let content_view = self.content.view().map(Message::Content);
content_view
self.content.view().map(Message::Content)
}

fn dialog(&self) -> Option<Element<Message>> {
Expand Down
24 changes: 22 additions & 2 deletions src/app/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,30 @@ impl TasksConfig {

#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
pub enum AppTheme {
Dark,
Light,
#[default]
System,
Dark,
Light,
}

impl From<usize> for AppTheme {
fn from(value: usize) -> Self {
match value {
1 => AppTheme::Dark,
2 => AppTheme::Light,
_ => AppTheme::System,
}
}
}

impl From<AppTheme> for usize {
fn from(value: AppTheme) -> Self {
match value {
AppTheme::System => 0,
AppTheme::Dark => 1,
AppTheme::Light => 2,
}
}
}

impl AppTheme {
Expand Down
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 set_localization() {
pub fn localize() {
let localizer = localizer();
let requested_languages = i18n_embed::DesktopLanguageRequester::requested_languages();

Expand Down
65 changes: 31 additions & 34 deletions src/app/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,27 @@ use cosmic::Application;
use std::sync::Mutex;

use super::config::TasksConfig;
use super::localize::set_localization;
use super::localize::localize;
use super::Tasks;

pub fn init() -> (Settings, Flags) {
set_localization();
set_icon_cache();
set_logger();
migrate("com.system76.CosmicTasks");
migrate("dev.edfloreshz.Orderly");
let settings = get_app_settings();
let flags = get_flags();
(settings, flags)
}
pub fn settings() -> Settings {
localize();
icons();
log();
migrate(&["com.system76.CosmicTasks", "dev.edfloreshz.Orderly"]);

pub fn get_app_settings() -> Settings {
let config = TasksConfig::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()));
Settings::default()
.antialiasing(true)
.client_decorations(true)
.theme(config.app_theme.theme())
.size_limits(Limits::NONE.min_width(350.0).min_height(180.0))
.size(Size::new(700.0, 900.0))
.debug(false)
}

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

Flags {
Expand All @@ -48,13 +35,23 @@ pub fn get_flags() -> Flags {
}
}

pub fn migrate(prev_app_id: &str) {
let prev = dirs::data_local_dir().unwrap().join(prev_app_id);
let new = dirs::data_local_dir().unwrap().join(Tasks::APP_ID);
if prev.exists() {
match std::fs::rename(prev, new) {
Ok(()) => log::info!("migrated data to new directory"),
Err(err) => log::error!("error migrating data: {:?}", err),
pub fn log() {
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 migrate(prev_app_ids: &[&str]) {
for prev_app_id in prev_app_ids.iter() {
let prev = dirs::data_local_dir().unwrap().join(prev_app_id);
let new = dirs::data_local_dir().unwrap().join(Tasks::APP_ID);
if prev.exists() {
match std::fs::rename(prev, new) {
Ok(()) => log::info!("migrated data to new directory"),
Err(err) => log::error!("error migrating data: {:?}", err),
}
}
}
}
Loading

0 comments on commit 9329f5f

Please sign in to comment.