monorepo/fitnesstrax/app/src/components/date_field.rs

76 lines
2.3 KiB
Rust
Raw Normal View History

/*
Copyright 2024, Savanni D'Gerinel <savanni@luminescent-dreams.com>
This file is part of FitnessTrax.
FitnessTrax is free software: you can redistribute it and/or modify it under the terms of the GNU
General Public License as published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
FitnessTrax is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License along with FitnessTrax. If not, see <https://www.gnu.org/licenses/>.
*/
use crate::types::ParseError;
use chrono::Datelike;
use gtk::prelude::*;
use std::{cell::RefCell, rc::Rc};
#[derive(Clone)]
pub struct DateField {
value: Rc<RefCell<chrono::NaiveDate>>,
buffer: gtk::TextBuffer,
widget: gtk::TextView,
}
impl DateField {
pub fn new(date: chrono::NaiveDate) -> Self {
let buffer = gtk::TextBuffer::new(None);
let tag = buffer.create_tag(
Some("placeholder"),
&[("foreground", &String::from("grey"))],
);
buffer.set_text("regular text placeholder text");
let (start, end) = buffer.bounds();
let mut placeholder_start = start.clone();
placeholder_start.forward_chars(13);
let placeholder_markup = buffer.tag_table().lookup("placeholder").unwrap();
buffer.apply_tag(&placeholder_markup, &placeholder_start, &end);
let widget = gtk::TextView::builder()
.buffer(&buffer)
.editable(true)
.can_focus(true)
.height_request(50)
.width_request(200)
.build();
let s = Self {
value: Rc::new(RefCell::new(date)),
buffer,
widget,
};
s.widget.buffer().connect_text_notify({
let s = s.clone();
move |buffer| s.on_update(buffer)
});
s
}
pub fn widget(&self) -> gtk::Widget {
self.widget.clone().upcast::<gtk::Widget>()
}
fn on_update(&self, buffer: &gtk::TextBuffer) {
let (start, end) = buffer.bounds();
println!("[on_update] {}", buffer.text(&start, &end, true));
}
}