Skip to content

Commit

Permalink
improv: use text editor for notes
Browse files Browse the repository at this point in the history
  • Loading branch information
edfloreshz committed Nov 4, 2024
1 parent 46ea397 commit 9b55b4c
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,8 @@ impl Application for Tasks {
self.details.subtasks.clear();
self.details.sub_task_input_ids.clear();
self.details.task = Some(task.clone());
self.details.text_editor_content =
widget::text_editor::Content::with_text(&task.notes);
task.sub_tasks.into_iter().for_each(|task| {
let id = self.details.subtasks.insert(task);
self.details
Expand Down
2 changes: 1 addition & 1 deletion src/app/key_bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn key_binds() -> HashMap<KeyBind, Action> {

bind!([Ctrl], Key::Character("n".into()), NewList);
bind!([], Key::Named(Named::Delete), DeleteList);
bind!([], Key::Named(Named::Enter), RenameList);
bind!([Ctrl], Key::Character("r".into()), RenameList);
bind!([Shift], Key::Character("I".into()), Icon);
bind!([Ctrl], Key::Character("w".into()), WindowClose);
bind!([Ctrl, Shift], Key::Character("n".into()), WindowNew);
Expand Down
77 changes: 68 additions & 9 deletions src/details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use crate::app::icon_cache::IconCache;
use chrono::{NaiveDate, TimeZone, Utc};
use cosmic::iced::{Alignment, Length};
use cosmic::iced_widget::row;
use cosmic::widget::segmented_button;
use cosmic::widget::segmented_button::Entity;
use cosmic::widget::{segmented_button, text_editor};
use cosmic::{theme, widget, Element};
use slotmap::{DefaultKey, SecondaryMap, SlotMap};
use tasks_core::models::{self, Priority, Status};
Expand All @@ -17,12 +17,13 @@ pub struct Details {
pub subtasks: SlotMap<DefaultKey, models::Task>,
pub editing: SecondaryMap<DefaultKey, bool>,
pub sub_task_input_ids: SecondaryMap<DefaultKey, widget::Id>,
pub text_editor_content: widget::text_editor::Content,
}

#[derive(Debug, Clone)]
pub enum Message {
SetTitle(String),
SetNotes(String),
Editor(text_editor::Action),
Favorite(bool),
CompleteSubTask(DefaultKey, bool),
DeleteSubTask(DefaultKey),
Expand Down Expand Up @@ -70,20 +71,22 @@ impl Details {
subtasks: SlotMap::new(),
editing: SecondaryMap::new(),
sub_task_input_ids: SecondaryMap::new(),
text_editor_content: widget::text_editor::Content::new(),
}
}

pub fn update(&mut self, message: Message) -> Vec<Task> {
let mut tasks = vec![];
match message {
Message::SetTitle(title) => {
if let Some(ref mut task) = &mut self.task {
task.title.clone_from(&title);
Message::Editor(action) => {
if let Some(task) = &mut self.task {
self.text_editor_content.perform(action);
task.notes.clone_from(&self.text_editor_content.text());
}
}
Message::SetNotes(notes) => {
Message::SetTitle(title) => {
if let Some(ref mut task) = &mut self.task {
task.notes.clone_from(&notes);
task.title.clone_from(&title);
}
}
Message::Favorite(favorite) => {
Expand Down Expand Up @@ -251,8 +254,14 @@ impl Details {
.add(
widget::column::with_children(vec![
widget::text::body(fl!("notes")).into(),
widget::text_input(fl!("notes"), &task.notes)
.on_input(Message::SetNotes)
widget::text_editor(&self.text_editor_content)
.class(cosmic::theme::iced::TextEditor::Custom(Box::new(
text_editor_class,
)))
.padding(spacing.space_xxs)
.placeholder(fl!("notes"))
.height(100.0)
.on_action(Message::Editor)
.into(),
])
.spacing(spacing.space_xxs)
Expand Down Expand Up @@ -293,3 +302,53 @@ impl Details {
.into()
}
}

fn text_editor_class(
theme: &cosmic::Theme,
status: cosmic::widget::text_editor::Status,
) -> cosmic::iced_widget::text_editor::Style {
let cosmic = theme.cosmic();
let container = theme.current_container();

let mut background: cosmic::iced::Color = container.component.base.into();
background.a = 0.25;
let selection = cosmic.accent.base.into();
let value = cosmic.palette.neutral_9.into();
let mut placeholder = cosmic.palette.neutral_9;
placeholder.alpha = 0.7;
let placeholder = placeholder.into();
let icon = cosmic.background.on.into();

match status {
cosmic::iced_widget::text_editor::Status::Active
| cosmic::iced_widget::text_editor::Status::Disabled => {
cosmic::iced_widget::text_editor::Style {
background: background.into(),
border: cosmic::iced::Border {
radius: cosmic.corner_radii.radius_m.into(),
width: 2.0,
color: container.component.divider.into(),
},
icon,
placeholder,
value,
selection,
}
}
cosmic::iced_widget::text_editor::Status::Hovered
| cosmic::iced_widget::text_editor::Status::Focused => {
cosmic::iced_widget::text_editor::Style {
background: background.into(),
border: cosmic::iced::Border {
radius: cosmic.corner_radii.radius_m.into(),
width: 2.0,
color: cosmic::iced::Color::from(cosmic.accent.base),
},
icon,
placeholder,
value,
selection,
}
}
}
}

0 comments on commit 9b55b4c

Please sign in to comment.