Skip to main content

Style

Struct Style 

Source
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: Attrs

Implementations§

Source§

impl Style

Source

pub const DEFAULT: Self

Theme text on a transparent background.

Source

pub const fn new() -> Self

Source

pub const fn fg(colour: Colour) -> Self

A style with this foreground.

Examples found in repository?
examples/together_ui.rs (line 57)
53    fn paint(&mut self) {
54        let (cols, _) = self.card.size();
55        self.card.clear();
56        let line = format!("{} · {} builds", self.status.state, self.status.builds);
57        self.card.text(0, 0, &line, Style::fg(Colour::FG));
58        let hint = "rebuild";
59        let x = cols.saturating_sub(hint.len() as u32);
60        self.card
61            .text(x, 0, hint, Style::fg(Colour::ACCENT).underline());
62        let _ = self.card.commit();
63    }
More examples
Hide additional 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    }
Source

pub const fn with_fg(self, colour: Colour) -> Self

Source

pub const fn with_bg(self, colour: Colour) -> Self

Source

pub const fn with_attrs(self, attrs: Attrs) -> Self

Source

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
Hide additional 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    }
Source

pub const fn dim(self) -> Self

Source

pub const fn italic(self) -> Self

Source

pub const fn underline(self) -> Self

Examples found in repository?
examples/together_ui.rs (line 61)
53    fn paint(&mut self) {
54        let (cols, _) = self.card.size();
55        self.card.clear();
56        let line = format!("{} · {} builds", self.status.state, self.status.builds);
57        self.card.text(0, 0, &line, Style::fg(Colour::FG));
58        let hint = "rebuild";
59        let x = cols.saturating_sub(hint.len() as u32);
60        self.card
61            .text(x, 0, hint, Style::fg(Colour::ACCENT).underline());
62        let _ = self.card.commit();
63    }

Trait Implementations§

Source§

impl Clone for Style

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Style

Source§

impl Debug for Style

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Style

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Eq for Style

Source§

impl Hash for Style

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Style

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Style

Auto Trait Implementations§

§

impl Freeze 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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Resource for T
where T: 'static,

Source§

type Rep = Option<T>

The type which is actually stored in-memory for this resource. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.