pub struct Style {
pub fg: Colour,
pub bg: Colour,
pub attrs: Attrs,
}Expand description
How a cell paints: foreground, background, attributes.
Fields§
§fg: Colour§bg: Colour§attrs: AttrsImplementations§
Source§impl Style
impl Style
pub const fn new() -> Self
Sourcepub const fn fg(colour: Colour) -> Self
pub const fn fg(colour: Colour) -> Self
A style with this foreground.
Examples found in repository?
More examples
examples/graphics_chart.rs (line 50)
48fn paint_cells(pane: &mut Surface<Cells>, service: u32, place: Rect) {
49 const BLOCKS: [char; 8] = ['▁', '▂', '▃', '▄', '▅', '▆', '▇', '█'];
50 let style = Style::fg(Colour::ACCENT);
51 for col in 0..place.w {
52 let index = col * SAMPLES / place.w.max(1);
53 let eighths = (sample(service, index) * (place.h * 8) as f32) as u32;
54 for row in 0..place.h {
55 let from_bottom = place.h - 1 - row;
56 let here = eighths.saturating_sub(from_bottom * 8).min(8);
57 if here > 0 {
58 let block = BLOCKS[here as usize - 1];
59 pane.put(place.x + col, place.y + row, block, style);
60 }
61 }
62 }
63}
64
65/// The same chart in pixels: an area under a line, in the viewer's accent
66/// colour and transparent everywhere else.
67fn paint_pixels(canvas: &mut Surface<Pixels>, service: u32, accent: u32) {
68 let (width, height) = canvas.size();
69 let (r, g, b) = ((accent >> 16) as u8, (accent >> 8) as u8, accent as u8);
70 let (line, area) = (Rgba::rgb(r, g, b), Rgba::new(r, g, b, 64));
71 let point = |index: u32| {
72 let x = (index * (width - 1) / (SAMPLES - 1)) as i32;
73 let y = ((1.0 - sample(service, index)) * (height - 1) as f32) as i32;
74 (x, y)
75 };
76 for x in 0..width {
77 let index = (x * (SAMPLES - 1) / width.max(1)).min(SAMPLES - 2);
78 let (from, to) = (point(index), point(index + 1));
79 let run = (to.0 - from.0).max(1);
80 let y = from.1 + (to.1 - from.1) * (x as i32 - from.0) / run;
81 canvas.fill_rect(Area::new(x as i32, y, 1, height - y as u32), area);
82 }
83 for index in 0..SAMPLES - 1 {
84 canvas.line(point(index), point(index + 1), line);
85 }
86}
87
88impl Usage {
89 /// Paints the whole surface: the cells every viewer shows, then the
90 /// charts over them where this viewer has graphics. The data changed or
91 /// the buffer is new, so this runs on an event, and never per frame.
92 fn paint(&mut self) {
93 let (cols, rows) = self.pane.size();
94 if rows == 0 {
95 // No geometry yet: a resize will follow.
96 return;
97 }
98 let accent = view::theme().accent;
99 self.pane.clear();
100 self.pane.text(1, 0, "Usage", Style::fg(Colour::FG).bold());
101 for service in 0..SERVICES {
102 let top = TITLE_ROWS + service * SERVICE_ROWS;
103 let name = format!("Service {}", service + 1);
104 self.pane.text(1, top, &name, Style::fg(Colour::FG));
105 let place = Rect::new(1, top + 1, cols.saturating_sub(2), CHART_ROWS);
106 paint_cells(&mut self.pane, service, place);
107 // Runs only where the viewer shows real graphics. Elsewhere the
108 // cells just painted are the chart, and nothing else is needed.
109 let _ = self.pane.draw_graphic(service, place, |canvas| {
110 paint_pixels(canvas, service, accent);
111 });
112 }
113 let _ = self.pane.commit();
114 }examples/clock_card.rs (line 43)
32 fn frame(&mut self, frame: &Frame) {
33 let second = frame.now_ms() / 1000;
34 if self.shown != Some(second) {
35 self.shown = Some(second);
36 let (cols, _) = self.card.size();
37 let time = format!(
38 "{:02}:{:02}:{:02}",
39 second / 3600 % 24,
40 second / 60 % 60,
41 second % 60
42 );
43 self.card.text(0, 0, &time, Style::fg(Colour::FG).bold());
44 let fraction = (second % 60) as f32 / 60.0;
45 let track = Style::fg(Colour::RECEDE_FG);
46 self.card.bar(
47 Area::new(0, 1, cols, 1),
48 fraction,
49 Style::fg(Colour::ACCENT),
50 track,
51 );
52 // Publishes only the cells that changed.
53 let _ = self.card.commit();
54 }
55 // The next second boundary: an idle viewer wakes once a second.
56 frame.wake_at((second + 1) * 1000);
57 }examples/scroll_list.rs (line 47)
39 fn paint(&mut self) {
40 let (_, rows) = self.list.size();
41 if rows == 0 {
42 // No geometry yet: a resize will follow.
43 return;
44 }
45 self.dirty = false;
46 self.list.clear();
47 self.list.text(1, 0, "Items", Style::fg(Colour::FG).bold());
48 for item in 0..ITEMS {
49 let style = if item == self.selected {
50 Style::fg(Colour::ACCENT).bold()
51 } else {
52 Style::fg(Colour::FG)
53 };
54 let row = TITLE_ROWS + item;
55 self.list.text(1, row, &format!("Item {}", item + 1), style);
56 }
57 // The surface's last row, which is the bottom of the pane whenever
58 // the list is shorter than the pane: the surface is never shorter
59 // than where it shows.
60 self.list.text(
61 1,
62 rows - HINT_ROWS,
63 "Up, Down, Page Up and Page Down select",
64 Style::fg(Colour::RECEDE_FG),
65 );
66 let _ = self.list.commit();
67 }pub const fn with_fg(self, colour: Colour) -> Self
pub const fn with_bg(self, colour: Colour) -> Self
pub const fn with_attrs(self, attrs: Attrs) -> Self
Sourcepub const fn bold(self) -> Self
pub const fn bold(self) -> Self
Examples found in repository?
examples/clock_card.rs (line 43)
32 fn frame(&mut self, frame: &Frame) {
33 let second = frame.now_ms() / 1000;
34 if self.shown != Some(second) {
35 self.shown = Some(second);
36 let (cols, _) = self.card.size();
37 let time = format!(
38 "{:02}:{:02}:{:02}",
39 second / 3600 % 24,
40 second / 60 % 60,
41 second % 60
42 );
43 self.card.text(0, 0, &time, Style::fg(Colour::FG).bold());
44 let fraction = (second % 60) as f32 / 60.0;
45 let track = Style::fg(Colour::RECEDE_FG);
46 self.card.bar(
47 Area::new(0, 1, cols, 1),
48 fraction,
49 Style::fg(Colour::ACCENT),
50 track,
51 );
52 // Publishes only the cells that changed.
53 let _ = self.card.commit();
54 }
55 // The next second boundary: an idle viewer wakes once a second.
56 frame.wake_at((second + 1) * 1000);
57 }More examples
examples/graphics_chart.rs (line 100)
92 fn paint(&mut self) {
93 let (cols, rows) = self.pane.size();
94 if rows == 0 {
95 // No geometry yet: a resize will follow.
96 return;
97 }
98 let accent = view::theme().accent;
99 self.pane.clear();
100 self.pane.text(1, 0, "Usage", Style::fg(Colour::FG).bold());
101 for service in 0..SERVICES {
102 let top = TITLE_ROWS + service * SERVICE_ROWS;
103 let name = format!("Service {}", service + 1);
104 self.pane.text(1, top, &name, Style::fg(Colour::FG));
105 let place = Rect::new(1, top + 1, cols.saturating_sub(2), CHART_ROWS);
106 paint_cells(&mut self.pane, service, place);
107 // Runs only where the viewer shows real graphics. Elsewhere the
108 // cells just painted are the chart, and nothing else is needed.
109 let _ = self.pane.draw_graphic(service, place, |canvas| {
110 paint_pixels(canvas, service, accent);
111 });
112 }
113 let _ = self.pane.commit();
114 }examples/scroll_list.rs (line 47)
39 fn paint(&mut self) {
40 let (_, rows) = self.list.size();
41 if rows == 0 {
42 // No geometry yet: a resize will follow.
43 return;
44 }
45 self.dirty = false;
46 self.list.clear();
47 self.list.text(1, 0, "Items", Style::fg(Colour::FG).bold());
48 for item in 0..ITEMS {
49 let style = if item == self.selected {
50 Style::fg(Colour::ACCENT).bold()
51 } else {
52 Style::fg(Colour::FG)
53 };
54 let row = TITLE_ROWS + item;
55 self.list.text(1, row, &format!("Item {}", item + 1), style);
56 }
57 // The surface's last row, which is the bottom of the pane whenever
58 // the list is shorter than the pane: the surface is never shorter
59 // than where it shows.
60 self.list.text(
61 1,
62 rows - HINT_ROWS,
63 "Up, Down, Page Up and Page Down select",
64 Style::fg(Colour::RECEDE_FG),
65 );
66 let _ = self.list.commit();
67 }pub const fn dim(self) -> Self
pub const fn italic(self) -> Self
Trait Implementations§
impl Copy for Style
impl Eq for Style
impl StructuralPartialEq for Style
Auto Trait Implementations§
impl Forget for Style
impl Freeze for Style
impl Move for Style
impl RefUnwindSafe for Style
impl Send for Style
impl Sync for Style
impl Unpin for Style
impl UnsafeUnpin for Style
impl UnwindSafe for Style
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more