simplify style settings, improve naming (#44)

* simplify style settings by passing `style` directly to Widgets. No need to store it in `state` of widgets.
* remove unneeded things
* naming (state vs. widgets)
This commit is contained in:
Jens Krause
2025-01-07 19:02:57 +01:00
committed by GitHub
parent 8603a823e4
commit 468b4a5abf
6 changed files with 116 additions and 165 deletions

View File

@@ -73,26 +73,24 @@ pub enum Format {
}
#[derive(Debug, Clone)]
pub struct Clock<T> {
pub struct ClockState<T> {
initial_value: DurationEx,
current_value: DurationEx,
tick_value: DurationEx,
mode: Mode,
format: Format,
pub style: Style,
pub with_decis: bool,
phantom: PhantomData<T>,
}
pub struct ClockArgs {
pub struct ClockStateArgs {
pub initial_value: Duration,
pub current_value: Duration,
pub tick_value: Duration,
pub style: Style,
pub with_decis: bool,
}
impl<T> Clock<T> {
impl<T> ClockState<T> {
pub fn toggle_pause(&mut self) {
self.mode = if self.mode == Mode::Tick {
Mode::Pause
@@ -296,10 +294,6 @@ impl<T> Clock<T> {
self.update_format();
}
pub fn is_done(&self) -> bool {
self.mode == Mode::Done
}
fn update_format(&mut self) {
self.format = self.get_format();
}
@@ -324,13 +318,12 @@ impl<T> Clock<T> {
#[derive(Debug, Clone)]
pub struct Countdown {}
impl Clock<Countdown> {
pub fn new(args: ClockArgs) -> Self {
let ClockArgs {
impl ClockState<Countdown> {
pub fn new(args: ClockStateArgs) -> Self {
let ClockStateArgs {
initial_value,
current_value,
tick_value,
style,
with_decis,
} = args;
let mut instance = Self {
@@ -345,7 +338,6 @@ impl Clock<Countdown> {
Mode::Pause
},
format: Format::S,
style,
with_decis,
phantom: PhantomData,
};
@@ -394,13 +386,12 @@ impl Clock<Countdown> {
#[derive(Debug, Clone)]
pub struct Timer {}
impl Clock<Timer> {
pub fn new(args: ClockArgs) -> Self {
let ClockArgs {
impl ClockState<Timer> {
pub fn new(args: ClockStateArgs) -> Self {
let ClockStateArgs {
initial_value,
current_value,
tick_value,
style,
with_decis,
} = args;
let mut instance = Self {
@@ -416,7 +407,6 @@ impl Clock<Timer> {
},
format: Format::S,
phantom: PhantomData,
style,
with_decis,
};
// update format once
@@ -461,6 +451,7 @@ pub struct ClockWidget<T>
where
T: std::fmt::Debug,
{
style: Style,
phantom: PhantomData<T>,
}
@@ -468,8 +459,9 @@ impl<T> ClockWidget<T>
where
T: std::fmt::Debug,
{
pub fn new() -> Self {
pub fn new(style: Style) -> Self {
Self {
style,
phantom: PhantomData,
}
}
@@ -568,12 +560,12 @@ impl<T> StatefulWidget for ClockWidget<T>
where
T: std::fmt::Debug,
{
type State = Clock<T>;
type State = ClockState<T>;
fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
let with_decis = state.with_decis;
let format = state.format;
let symbol = state.style.get_digit_symbol();
let symbol = self.style.get_digit_symbol();
let widths = self.get_horizontal_lengths(&format, with_decis);
let area = center_horizontal(
area,