~ruther/simple-clock

ref: 6fbd046c5cdec2a46aa06ef9753192095ad33555 simple-clock/source/src/display_view/clock_display_view.rs -rw-r--r-- 1.7 KiB
6fbd046c — Rutherther Merge pull request #1 from Rutherther/add-docs 1 year, 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use crate::{
    clock_display::{ClockDisplay, DisplayPart},
    clock_state::ClockState,
};

use super::{DisplayView, DisplayViewError};

pub struct ClockDisplayView {
    show_seconds: bool,
    show_date: bool,
}

impl Default for ClockDisplayView {
    fn default() -> Self {
        Self::new()
    }
}

impl ClockDisplayView {
    pub fn new() -> Self {
        Self {
            show_date: false,
            show_seconds: false,
        }
    }

    pub fn with_seconds() -> Self {
        Self {
            show_date: false,
            show_seconds: true,
        }
    }

    pub fn with_date() -> Self {
        Self {
            show_date: true,
            show_seconds: false,
        }
    }
}

impl DisplayView for ClockDisplayView {
    fn update_display(
        &mut self,
        state: &ClockState,
        display: &mut ClockDisplay,
    ) -> Result<(), DisplayViewError> {
        let calendar = state.calendar();

        if self.show_seconds {
            display.hide(DisplayPart::SideDisplay1);
            display.show_number(DisplayPart::SideDisplay2, calendar.seconds() as u32, true)?;
        } else if self.show_date {
            display.show_ordinal(DisplayPart::SideDisplay1, calendar.day() as u32, true)?;
            display.show_ordinal(DisplayPart::SideDisplay2, calendar.month() as u32, true)?;
        } else {
            display.hide(DisplayPart::SideDisplay1);
            display.hide(DisplayPart::SideDisplay2);
        }

        display.set_colon(calendar.seconds() % 2 == 0);
        display.show_number(
            DisplayPart::MainDisplay,
            (calendar.hours() as u32) * 100 + (calendar.minutes() as u32),
            true,
        )?;
        Ok(())
    }
}
Do not follow this link